source-map-support
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "source-map-support", | ||
"description": "Fixes stack traces for files with source maps", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"main": "./source-map-support.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -9,3 +9,3 @@ # Source Map Support | ||
This module takes effect globally and should be initialized by inserting a call to `require('source-map-support')` at the top of your code. | ||
This module takes effect globally and should be initialized by inserting `require('source-map-support').install()` at the top of your code. | ||
@@ -18,3 +18,3 @@ ### CoffeeScript Demo | ||
require 'source-map-support' | ||
require('source-map-support').install() | ||
foo = -> | ||
@@ -25,8 +25,4 @@ bar = -> throw new Error 'this is a demo' | ||
$ npm install source-map-support | ||
$ git clone https://github.com/michaelficarra/CoffeeScriptRedux.git | ||
$ cd CoffeeScriptRedux && npm install && cd .. | ||
$ CoffeeScriptRedux/bin/coffee --js -i demo.coffee > demo.js | ||
$ echo '//@ sourceMappingURL=demo.js.map' >> demo.js | ||
$ CoffeeScriptRedux/bin/coffee --source-map -i demo.coffee > demo.js.map | ||
$ npm install source-map-support coffee-script | ||
$ node_modules/coffee-script/bin/coffee --map --compile demo.coffee | ||
$ node demo | ||
@@ -36,8 +32,8 @@ | ||
bar = -> throw new Error 'this is a demo' | ||
^ | ||
^ | ||
Error: this is a demo | ||
at bar (demo.coffee:4:19) | ||
at bar (demo.coffee:4:21) | ||
at foo (demo.coffee:5:3) | ||
at Object.<anonymous> (demo.coffee:6:3) | ||
at Object.<anonymous> (demo.coffee:6:6) | ||
at Object.<anonymous> (demo.coffee:6) | ||
at Object.<anonymous> (demo.coffee:2) | ||
at Module._compile (module.js:449:26) | ||
@@ -50,4 +46,2 @@ at Object.Module._extensions..js (module.js:467:10) | ||
Note that the steps above are just to demonstrate this library. If you want good stack traces for your own CoffeeScript project, you can run coffee files directly with CoffeeScriptRedux (use the `--eval` flag) and the stack traces will have the correct CoffeeScript line numbers. | ||
### TypeScript Demo | ||
@@ -60,3 +54,3 @@ | ||
declare function require(name: string); | ||
require('source-map-support'); | ||
require('source-map-support').install(); | ||
class Foo { | ||
@@ -86,4 +80,12 @@ constructor() { this.bar(); } | ||
### Options | ||
This module installs two things: a change to the `stack` property on `Error` objects and a handler for uncaught exceptions that mimics node's default exception handler (the handler can be seen in the demos above). You may want to disable the handler if you have your own uncaught exception handler. This can be done by passing an argument to the installer: | ||
require('source-map-support').install({ | ||
handleUncaughtExceptions: false | ||
}); | ||
### License | ||
This code is available under the [MIT license](http://opensource.org/licenses/MIT). |
@@ -92,3 +92,3 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer; | ||
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi | ||
Error.prepareStackTrace = function(error, stack) { | ||
function prepareStackTrace(error, stack) { | ||
// Store source maps in a cache so we don't load them more than once when | ||
@@ -101,6 +101,6 @@ // formatting a single stack trace (don't cache them forever though in case | ||
}).join(''); | ||
}; | ||
} | ||
// Mimic node's stack trace printing when an exception escapes the process | ||
process.on('uncaughtException', function(error) { | ||
function handleUncaughtExceptions(error) { | ||
if (!error || !error.stack) { | ||
@@ -130,2 +130,22 @@ console.log('Uncaught exception:', error); | ||
process.exit(); | ||
}); | ||
} | ||
exports.install = function(options) { | ||
Error.prepareStackTrace = prepareStackTrace; | ||
// Configure options | ||
options = options || {}; | ||
var installHandler = 'handleUncaughtExceptions' in options ? | ||
options.handleUncaughtExceptions : true; | ||
// Provide the option to not install the uncaught exception handler. This is | ||
// to support other uncaught exception handlers (in test frameworks, for | ||
// example). If this handler is not installed and there are no other uncaught | ||
// exception handlers, uncaught exceptions will be caught by node's built-in | ||
// exception handler and the process will still be terminated. However, the | ||
// generated JavaScript code will be shown above the stack trace instead of | ||
// the original source code. | ||
if (installHandler) { | ||
process.on('uncaughtException', handleUncaughtExceptions); | ||
} | ||
}; |
104
test.js
@@ -1,21 +0,20 @@ | ||
require('./source-map-support'); | ||
require('./source-map-support').install(); | ||
var SourceMapGenerator = require('source-map').SourceMapGenerator; | ||
var child_process = require('child_process'); | ||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
// Create a source map | ||
var sourceMap = new SourceMapGenerator({ | ||
file: '.generated.js', | ||
sourceRoot: '.' | ||
}); | ||
for (var i = 1; i <= 100; i++) { | ||
sourceMap.addMapping({ | ||
generated: { line: i, column: 1 }, | ||
original: { line: 1000 + i, column: 100 + i }, | ||
source: 'line' + i + '.js' | ||
function compareStackTrace(source, expected) { | ||
var sourceMap = new SourceMapGenerator({ | ||
file: '.generated.js', | ||
sourceRoot: '.' | ||
}); | ||
} | ||
function compareStackTrace(source, expected) { | ||
for (var i = 1; i <= 100; i++) { | ||
sourceMap.addMapping({ | ||
generated: { line: i, column: 1 }, | ||
original: { line: 1000 + i, column: 100 + i }, | ||
source: 'line' + i + '.js' | ||
}); | ||
} | ||
fs.writeFileSync('.generated.js.map', sourceMap); | ||
@@ -35,2 +34,30 @@ fs.writeFileSync('.generated.js', 'exports.test = function() {' + | ||
function compareStdout(done, source, expected) { | ||
var sourceMap = new SourceMapGenerator({ | ||
file: '.generated.js', | ||
sourceRoot: '.' | ||
}); | ||
sourceMap.addMapping({ | ||
generated: { line: 1, column: 1 }, | ||
original: { line: 1, column: 1 }, | ||
source: '.original.js' | ||
}); | ||
fs.writeFileSync('.original.js', 'this is the original code'); | ||
fs.writeFileSync('.generated.js.map', sourceMap); | ||
fs.writeFileSync('.generated.js', source.join('\n') + | ||
'//@ sourceMappingURL=.generated.js.map'); | ||
child_process.exec('node ./.generated', function(error, stdout, stderr) { | ||
expected = expected.join('\n'); | ||
try { | ||
assert.equal((stdout + stderr).slice(0, expected.length), expected); | ||
} catch (e) { | ||
return done(e); | ||
} | ||
fs.unlinkSync('.generated.js'); | ||
fs.unlinkSync('.generated.js.map'); | ||
fs.unlinkSync('.original.js'); | ||
done(); | ||
}); | ||
} | ||
it('normal throw', function() { | ||
@@ -130,1 +157,50 @@ compareStackTrace([ | ||
}); | ||
it('default options', function(done) { | ||
compareStdout(done, [ | ||
'', | ||
'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, [ | ||
'', | ||
'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, [ | ||
'', | ||
'function foo() { throw new Error("this is the error"); }', | ||
'require("./source-map-support").install({ handleUncaughtExceptions: false });', | ||
'process.nextTick(foo);' | ||
], [ | ||
'', | ||
__dirname + '/.generated.js:2', | ||
'function foo() { throw new Error("this is the error"); }', | ||
' ^', | ||
'Error: this is the error', | ||
' at foo (./.original.js:1:1)' | ||
]); | ||
}); |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
14672
324
85
1