1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
require('./source-map-support').install({
emptyCacheBetweenOperations: true // Needed to be able to test for failure
});
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var child_process = require('child_process');
var assert = require('assert');
var fs = require('fs');
var bufferFrom = Buffer.from;
function compareLines(actual, expected) {
assert(actual.length >= expected.length, 'got ' + actual.length + ' lines but expected at least ' + expected.length + ' lines');
for (var i = 0; i < expected.length; i++) {
// Some tests are regular expressions because the output format changed slightly between node v0.9.2 and v0.9.3
if (expected[i] instanceof RegExp) {
assert(expected[i].test(actual[i]), JSON.stringify(actual[i]) + ' does not match ' + expected[i]);
} else {
assert.equal(actual[i], expected[i]);
}
}
}
function createEmptySourceMap() {
return new SourceMapGenerator({
file: '.generated.js',
sourceRoot: '.'
});
}
function createSourceMapWithGap() {
var sourceMap = createEmptySourceMap();
sourceMap.addMapping({
generated: { line: 100, column: 0 },
original: { line: 100, column: 0 },
source: '.original.js'
});
return sourceMap;
}
function createSingleLineSourceMap() {
var sourceMap = createEmptySourceMap();
sourceMap.addMapping({
generated: { line: 1, column: 0 },
original: { line: 1, column: 0 },
source: '.original.js'
});
return sourceMap;
}
function createSecondLineSourceMap() {
var sourceMap = createEmptySourceMap();
sourceMap.addMapping({
generated: { line: 2, column: 0 },
original: { line: 1, column: 0 },
source: '.original.js'
});
return sourceMap;
}
function createMultiLineSourceMap() {
var sourceMap = createEmptySourceMap();
for (var i = 1; i <= 100; i++) {
sourceMap.addMapping({
generated: { line: i, column: 0 },
original: { line: 1000 + i, column: 99 + i },
source: 'line' + i + '.js'
});
}
return sourceMap;
}
function createMultiLineSourceMapWithSourcesContent() {
var sourceMap = createEmptySourceMap();
var original = new Array(1001).join('\n');
for (var i = 1; i <= 100; i++) {
sourceMap.addMapping({
generated: { line: i, column: 0 },
original: { line: 1000 + i, column: 4 },
source: 'original.js'
});
original += ' line ' + i + '\n';
}
sourceMap.setSourceContent('original.js', original);
return sourceMap;
}
function compareStackTrace(sourceMap, source, expected) {
// Check once with a separate source map
fs.writeFileSync('.generated.js.map', String(sourceMap));
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
source.join('\n') + '};//@ sourceMappingURL=.generated.js.map');
try {
delete require.cache[require.resolve('./.generated')];
require('./.generated').test();
} catch (e) {
compareLines(e.stack.split(/\r\n|\n/), expected);
}
fs.unlinkSync('.generated.js');
fs.unlinkSync('.generated.js.map');
// Check again with an inline source map (in a data URL)
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
source.join('\n') + '};//@ sourceMappingURL=data:application/json;base64,' +
bufferFrom(sourceMap.toString()).toString('base64'));
try {
delete require.cache[require.resolve('./.generated')];
require('./.generated').test();
} catch (e) {
compareLines(e.stack.split(/\r\n|\n/), expected);
}
fs.unlinkSync('.generated.js');
}
function compareStdout(done, sourceMap, source, expected) {
fs.writeFileSync('.original.js', 'this is the original code');
fs.writeFileSync('.generated.js.map', String(sourceMap));
fs.writeFileSync('.generated.js', source.join('\n') +
'//@ sourceMappingURL=.generated.js.map');
child_process.exec('node ./.generated', function(error, stdout, stderr) {
try {
compareLines(
(stdout + stderr)
.trim()
.split(/\r\n|\n/)
.filter(function (line) { return line !== '' }), // Empty lines are not relevant.
expected
);
} catch (e) {
return done(e);
}
fs.unlinkSync('.generated.js');
fs.unlinkSync('.generated.js.map');
fs.unlinkSync('.original.js');
done();
});
}
it('normal throw', function() {
compareStackTrace(createMultiLineSourceMap(), [
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
]);
});
it('does not crash on missing process object', function() {
var proc = process;
compareStackTrace(createMultiLineSourceMap(), [
'global.process = null;',
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?line2\.js:1002:102\)$/
]);
global.process = proc;
});
it('does not crash on missing process.stderr', function() {
var stderr = process.stderr;
compareStackTrace(createMultiLineSourceMap(), [
'process.stderr = null;',
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?line2\.js:1002:102\)$/
]);
process.stderr = stderr;
});
/* The following test duplicates some of the code in
* `normal throw` but triggers file read failure.
*/
it('fs.readFileSync failure', function() {
compareStackTrace(createMultiLineSourceMap(), [
'var fs = require("fs");',
'var rfs = fs.readFileSync;',
'fs.readFileSync = function() {',
' throw new Error("no rfs for you");',
'};',
'try {',
' throw new Error("test");',
'} finally {',
' fs.readFileSync = rfs;',
'}'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?line7\.js:1007:107\)$/
]);
});
it('throw inside function', function() {
compareStackTrace(createMultiLineSourceMap(), [
'function foo() {',
' throw new Error("test");',
'}',
'foo();'
], [
'Error: test',
/^ at foo \((?:.*[/\\])?line2\.js:1002:102\)$/,
/^ at Object\.exports\.test \((?:.*[/\\])?line4\.js:1004:104\)$/
]);
});
it('throw inside function inside function', function() {
compareStackTrace(createMultiLineSourceMap(), [
'function foo() {',
' function bar() {',
' throw new Error("test");',
' }',
' bar();',
'}',
'foo();'
], [
'Error: test',
/^ at bar \((?:.*[/\\])?line3\.js:1003:103\)$/,
/^ at foo \((?:.*[/\\])?line5\.js:1005:105\)$/,
/^ at Object\.exports\.test \((?:.*[/\\])?line7\.js:1007:107\)$/
]);
});
it('eval', function() {
compareStackTrace(createMultiLineSourceMap(), [
'eval("throw new Error(\'test\')");'
], [
'Error: test',
// Before Node 4, `Object.eval`, after just `eval`.
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \((?:.*[/\\])?line1\.js:1001:101\)/,
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
]);
});
it('eval inside eval', function() {
compareStackTrace(createMultiLineSourceMap(), [
'eval("eval(\'throw new Error(\\"test\\")\')");'
], [
'Error: test',
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \(eval at (<anonymous>|exports.test) \((?:.*[/\\])?line1\.js:1001:101\)/,
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \((?:.*[/\\])?line1\.js:1001:101\)/,
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
]);
});
it('eval inside function', function() {
compareStackTrace(createMultiLineSourceMap(), [
'function foo() {',
' eval("throw new Error(\'test\')");',
'}',
'foo();'
], [
'Error: test',
/^ at eval \(eval at foo \((?:.*[/\\])?line2\.js:1002:102\)/,
/^ at foo \((?:.*[/\\])?line2\.js:1002:102\)/,
/^ at Object\.exports\.test \((?:.*[/\\])?line4\.js:1004:104\)$/
]);
});
it('eval with sourceURL', function() {
compareStackTrace(createMultiLineSourceMap(), [
'eval("throw new Error(\'test\')//@ sourceURL=sourceURL.js");'
], [
'Error: test',
/^ at (?:Object\.)?eval \(sourceURL\.js:1:7\)$/,
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
]);
});
it('eval with sourceURL inside eval', function() {
compareStackTrace(createMultiLineSourceMap(), [
'eval("eval(\'throw new Error(\\"test\\")//@ sourceURL=sourceURL.js\')");'
], [
'Error: test',
/^ at (?:Object\.)?eval \(sourceURL\.js:1:7\)$/,
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \((?:.*[/\\])?line1\.js:1001:101\)/,
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
]);
});
it('native function', function() {
compareStackTrace(createSingleLineSourceMap(), [
'[1].map(function(x) { throw new Error(x); });'
], [
'Error: 1',
/[/\\].original\.js/,
/at Array\.map \((native|<anonymous>)\)/
]);
});
it('function constructor', function() {
compareStackTrace(createMultiLineSourceMap(), [
'throw new Function(")");'
], [
/SyntaxError: Unexpected token '?\)'?/,
]);
});
it('throw with empty source map', function() {
compareStackTrace(createEmptySourceMap(), [
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated.js:1:34\)$/
]);
});
it('throw in Timeout with empty source map', function(done) {
compareStdout(done, createEmptySourceMap(), [
'require("./source-map-support").install();',
'setTimeout(function () {',
' throw new Error("this is the error")',
'})'
], [
/[/\\].generated.js:3$/,
' throw new Error("this is the error")',
/^ \^$/,
'Error: this is the error',
/^ at ((null)|(Timeout))\._onTimeout \((?:.*[/\\])?.generated\.js:3:11\)$/
]);
});
it('throw with source map with gap', function() {
compareStackTrace(createSourceMapWithGap(), [
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated\.js:1:34\)$/
]);
});
it('sourcesContent with data URL', function() {
compareStackTrace(createMultiLineSourceMapWithSourcesContent(), [
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?original\.js:1001:5\)$/
]);
});
it('finds the last sourceMappingURL', function() {
compareStackTrace(createMultiLineSourceMapWithSourcesContent(), [
'//# sourceMappingURL=missing.map.js', // NB: compareStackTrace adds another source mapping.
'throw new Error("test");'
], [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?original\.js:1002:5\)$/
]);
});
it('maps original name from source', function() {
var sourceMap = createEmptySourceMap();
sourceMap.addMapping({
generated: { line: 2, column: 8 },
original: { line: 1000, column: 10 },
source: '.original.js',
});
sourceMap.addMapping({
generated: { line: 4, column: 0 },
original: { line: 1002, column: 1 },
source: ".original.js",
name: "myOriginalName"
});
compareStackTrace(sourceMap, [
'function foo() {',
' throw new Error("test");',
'}',
'foo();'
], [
'Error: test',
/^ at myOriginalName \((?:.*[/\\])?\.original.js:1000:11\)$/,
/^ at Object\.exports\.test \((?:.*[/\\])?\.original.js:1002:2\)$/
]);
});
it('default options', function(done) {
compareStdout(done, createSecondLineSourceMap(), [
'',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install();',
'process.nextTick(foo);',
'process.nextTick(function() { process.exit(1); });'
], [
/[/\\].original\.js:1$/,
'this is the original code',
'^',
'Error: this is the error',
/^ at foo \((?:.*[/\\])?\.original\.js:1:1\)$/
]);
});
it('handleUncaughtExceptions is true', function(done) {
compareStdout(done, createSecondLineSourceMap(), [
'',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install({ handleUncaughtExceptions: true });',
'process.nextTick(foo);'
], [
/[/\\].original\.js:1$/,
'this is the original code',
'^',
'Error: this is the error',
/^ at foo \((?:.*[/\\])?\.original\.js:1:1\)$/
]);
});
it('handleUncaughtExceptions is false', function(done) {
compareStdout(done, createSecondLineSourceMap(), [
'',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install({ handleUncaughtExceptions: false });',
'process.nextTick(foo);'
], [
/[/\\].generated.js:2$/,
'function foo() { throw new Error("this is the error"); }',
// Before Node 4, the arrow points on the `new`, after on the
// `throw`.
/^ (?: )?\^$/,
'Error: this is the error',
/^ at foo \((?:.*[/\\])?.original\.js:1:1\)$/
]);
});
it('default options with empty source map', function(done) {
compareStdout(done, createEmptySourceMap(), [
'',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install();',
'process.nextTick(foo);'
], [
/[/\\].generated.js:2$/,
'function foo() { throw new Error("this is the error"); }',
/^ (?: )?\^$/,
'Error: this is the error',
/^ at foo \((?:.*[/\\])?.generated.js:2:24\)$/
]);
});
it('default options with source map with gap', function(done) {
compareStdout(done, createSourceMapWithGap(), [
'',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install();',
'process.nextTick(foo);'
], [
/[/\\].generated.js:2$/,
'function foo() { throw new Error("this is the error"); }',
/^ (?: )?\^$/,
'Error: this is the error',
/^ at foo \((?:.*[/\\])?.generated.js:2:24\)$/
]);
});
it('specifically requested error source', function(done) {
compareStdout(done, createSecondLineSourceMap(), [
'',
'function foo() { throw new Error("this is the error"); }',
'var sms = require("./source-map-support");',
'sms.install({ handleUncaughtExceptions: false });',
'process.on("uncaughtException", function (e) { console.log("SRC:" + sms.getErrorSource(e)); });',
'process.nextTick(foo);'
], [
/^SRC:.*[/\\]\.original\.js:1$/,
'this is the original code',
'^'
]);
});
it('sourcesContent', function(done) {
compareStdout(done, createMultiLineSourceMapWithSourcesContent(), [
'',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install();',
'process.nextTick(foo);',
'process.nextTick(function() { process.exit(1); });'
], [
/[/\\]original\.js:1002$/,
' line 2',
' ^',
'Error: this is the error',
/^ at foo \((?:.*[/\\])?original\.js:1002:5\)$/
]);
});
it('missing source maps should also be cached', function(done) {
compareStdout(done, createSingleLineSourceMap(), [
'',
'var count = 0;',
'function foo() {',
' console.log(new Error("this is the error").stack.split("\\n").slice(0, 2).join("\\n"));',
'}',
'require("./source-map-support").install({',
' overrideRetrieveSourceMap: true,',
' retrieveSourceMap: function(name) {',
' if (/\\.generated.js$/.test(name)) count++;',
' return null;',
' }',
'});',
'process.nextTick(foo);',
'process.nextTick(foo);',
'process.nextTick(function() { console.log(count); });',
], [
'Error: this is the error',
/^ at foo \((?:.*[/\\])?.generated.js:4:15\)$/,
'Error: this is the error',
/^ at foo \((?:.*[/\\])?.generated.js:4:15\)$/,
'1', // The retrieval should only be attempted once
]);
});
it('should consult all retrieve source map providers', function(done) {
compareStdout(done, createSingleLineSourceMap(), [
'',
'var count = 0;',
'function foo() {',
' console.log(new Error("this is the error").stack.split("\\n").slice(0, 2).join("\\n"));',
'}',
'require("./source-map-support").install({',
' retrieveSourceMap: function(name) {',
' if (/\\.generated.js$/.test(name)) count++;',
' return undefined;',
' }',
'});',
'require("./source-map-support").install({',
' retrieveSourceMap: function(name) {',
' if (/\\.generated.js$/.test(name)) {',
' count++;',
' return ' + JSON.stringify({url: '.original.js', map: createMultiLineSourceMapWithSourcesContent().toJSON()}) + ';',
' }',
' }',
'});',
'process.nextTick(foo);',
'process.nextTick(foo);',
'process.nextTick(function() { console.log(count); });',
], [
'Error: this is the error',
/^ at foo \((?:.*[/\\])?original\.js:1004:5\)$/,
'Error: this is the error',
/^ at foo \((?:.*[/\\])?original\.js:1004:5\)$/,
'1', // The retrieval should only be attempted once
]);
});
it('should allow for runtime inline source maps', function(done) {
var sourceMap = createMultiLineSourceMapWithSourcesContent();
fs.writeFileSync('.generated.jss', 'foo');
compareStdout(function(err) {
fs.unlinkSync('.generated.jss');
done(err);
}, createSingleLineSourceMap(), [
'require("./source-map-support").install({',
' hookRequire: true',
'});',
'require.extensions[".jss"] = function(module, filename) {',
' module._compile(',
JSON.stringify([
'',
'var count = 0;',
'function foo() {',
' console.log(new Error("this is the error").stack.split("\\n").slice(0, 2).join("\\n"));',
'}',
'process.nextTick(foo);',
'process.nextTick(foo);',
'process.nextTick(function() { console.log(count); });',
'//@ sourceMappingURL=data:application/json;charset=utf8;base64,' + bufferFrom(sourceMap.toString()).toString('base64')
].join('\n')),
', filename);',
'};',
'require("./.generated.jss");',
], [
'Error: this is the error',
/^ at foo \(.*[/\\]original\.js:1004:5\)$/,
'Error: this is the error',
/^ at foo \(.*[/\\]original\.js:1004:5\)$/,
'0', // The retrieval should only be attempted once
]);
});
/* The following test duplicates some of the code in
* `compareStackTrace` but appends a charset to the
* source mapping url.
*/
it('finds source maps with charset specified', function() {
var sourceMap = createMultiLineSourceMap()
var source = [ 'throw new Error("test");' ];
var expected = [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
];
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
source.join('\n') + '};//@ sourceMappingURL=data:application/json;charset=utf8;base64,' +
bufferFrom(sourceMap.toString()).toString('base64'));
try {
delete require.cache[require.resolve('./.generated')];
require('./.generated').test();
} catch (e) {
compareLines(e.stack.split(/\r\n|\n/), expected);
}
fs.unlinkSync('.generated.js');
});
/* The following test duplicates some of the code in
* `compareStackTrace` but appends some code and a
* comment to the source mapping url.
*/
it('allows code/comments after sourceMappingURL', function() {
var sourceMap = createMultiLineSourceMap()
var source = [ 'throw new Error("test");' ];
var expected = [
'Error: test',
/^ at Object\.exports\.test \((?:.*[/\\])?line1\.js:1001:101\)$/
];
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
source.join('\n') + '};//# sourceMappingURL=data:application/json;base64,' +
bufferFrom(sourceMap.toString()).toString('base64') +
'\n// Some comment below the sourceMappingURL\nvar foo = 0;');
try {
delete require.cache[require.resolve('./.generated')];
require('./.generated').test();
} catch (e) {
compareLines(e.stack.split(/\r\n|\n/), expected);
}
fs.unlinkSync('.generated.js');
});
it('handleUncaughtExceptions is true with existing listener', function(done) {
var source = [
'process.on("uncaughtException", function() { /* Silent */ });',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install();',
'process.nextTick(foo);',
'//@ sourceMappingURL=.generated.js.map'
];
fs.writeFileSync('.original.js', 'this is the original code');
fs.writeFileSync('.generated.js.map', String(createSingleLineSourceMap()));
fs.writeFileSync('.generated.js', source.join('\n'));
child_process.exec('node ./.generated', function(error, stdout, stderr) {
fs.unlinkSync('.generated.js');
fs.unlinkSync('.generated.js.map');
fs.unlinkSync('.original.js');
assert.equal((stdout + stderr).trim(), '');
done();
});
});
it('normal console.trace', function(done) {
compareStdout(done, createMultiLineSourceMap(), [
'require("./source-map-support").install();',
'console.trace("test");'
], [
'Trace: test',
/^ at Object\.<anonymous> \((?:.*[/\\])?line2\.js:1002:102\)$/
]);
});
|