Repository: novocaine/sourcemapped-stacktrace Branch: master Commit: d775db97c6d1 Files: 14 Total size: 18.8 KB Directory structure: gitextract_l1zd_6h9/ ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── demo/ │ ├── .babelrc │ ├── .gitignore │ ├── bork.es6 │ ├── bork_coffee.coffee │ ├── make.sh │ └── smst.html ├── index.d.ts ├── package.json ├── sourcemapped-stacktrace.js └── webpack.config.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules dist ================================================ FILE: .npmignore ================================================ node_modules ================================================ FILE: LICENSE ================================================ Copyright (c) 2016, James Salter All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of sourcemapped-stacktrace nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================ FILE: README.md ================================================ This is a simple module for applying source maps to JS stack traces in the browser. ## The problem this solves You have Error.stack() in JS (maybe you're logging a trace, or you're looking at traces in Jasmine or Mocha), and you need to apply a sourcemap so you can understand whats happening because you're using some fancy compiled-to-js thing like coffeescript or traceur. Unfortunately, the browser only applies sourcemaps when the trace is viewed in its console, not to the underlying stack object, so you're out of luck. ## Demo http://novocaine.github.io/sourcemapped-stacktrace-demo/public_html/smst.html ## Install from npm ``` npm install sourcemapped-stacktrace ``` https://www.npmjs.com/package/sourcemapped-stacktrace The npm bundle contains dist/sourcemapped-stacktrace.js, if that's what you're after. The built product is not held in this repo. ## Setup Include sourcemapped-stacktrace.js into your page using either an AMD module loader or a plain old script include. As an AMD module it exposes the method 'mapStackTrace'. If an AMD loader is not found this function will be set on window.sourceMappedStackTrace.mapStackTrace. ## API ### mapStackTrace(stack, done [, opts]) Re-map entries in a stacktrace using sourcemaps if available. **Arguments:** - *stack*: (str) The stacktrace from the browser. - *done*: Callback invoked with the transformed stacktrace (an Array of Strings) passed as the first argument - *opts*: Optional options object containing: - *filter*: Function that filters each stacktrace line. It is invoked with _(line)_ and should return truthy/ falsy value. Sources which do not pass the filter won't be processed. - *cacheGlobally*: Boolean. If `true`, sourcemaps are cached across multiple `mapStackTrace()` calls, allowing for better performance if called repeatedly, or when browser's cache is disabled. Defaults to `false`. - *sync*: Boolean. Whether to use synchronous ajax to load the sourcemaps. **Supported browsers** - Chrome - Firefox - Safari - Internet Explorer 11 and up - Microsoft Edge ## Example ```javascript try { // break something bork(); } catch (e) { // pass e.stack to window.mapStackTrace window.mapStackTrace(e.stack, function(mappedStack) { // do what you want with mappedStack here console.log(mappedStack.join("\n")); }, { filter: function (line) { // process only sources containing `spec.js` return /(spec\.js)/.test(line); } }); } ``` ## Longer Explanation Several modern browsers support sourcemaps when viewing stack traces from errors in their native console, but as of the time of writing there is no support for applying a sourcemap to the (highly non-standardised) [Error.prototype.stack](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack). Error.prototype.stack can be used for logging errors and for displaying errors in test frameworks, and it is not very convenient to have unmapped traces in either of those use cases. This module fetches all the scripts referenced by the stack trace, determines whether they have an applicable sourcemap, fetches the sourcemap from the server, then uses the [Mozilla source-map library](https://github.com/mozilla/source-map/) to do the mapping. Browsers that support sourcemaps don't offer a standardised sourcemap API, so we have to do all that work ourselves. The nice part about doing it ourselves is that the library could be extended to work in browsers that don't support sourcemaps, which could be good for logging and debugging problems. Currently, only Chrome and Firefox are supported, but it would be easy to support those formats by ripping off [stacktrace.js](https://github.com/stacktracejs/stacktrace.js/). ## Known issues * Doesn't support exception formats of any browser other than Chrome and Firefox * Only supports JS containing //# sourceMappingURL= declarations (i.e. no support for the SourceMap: HTTP header (yet) * Some prominent sourcemap generators (including CoffeeScript, Traceur, Babel) don't emit a list of 'names' in the source-map, which means that frames from transpiled code will have (unknown) instead of the original function name. Those generators should support this feature better. ================================================ FILE: demo/.babelrc ================================================ { "presets": ["es2015"] } ================================================ FILE: demo/.gitignore ================================================ bork.babel.js bork_coffee.js *.map sourcemapped-stacktrace.js ================================================ FILE: demo/bork.es6 ================================================ class BabelBorker { bork() { throw new Error("bork from es6"); } } window.babel_bork = () => new BabelBorker().bork() ================================================ FILE: demo/bork_coffee.coffee ================================================ class CoffeeBorker bork: () -> throw new Error("Bork from coffeescript") window.coffee_bork = () -> new CoffeeBorker().bork() ================================================ FILE: demo/make.sh ================================================ # (don't forget to run npm install first, to update dist/smst.js) babel bork.es6 -s true > bork.babel.js coffee -c -m bork_coffee.coffee cp ../dist/sourcemapped-stacktrace.js . ================================================ FILE: demo/smst.html ================================================
A demo of printing a stack trace from an error thrown within transpiled code.
Source-mapped Stack trace (pointing to original code):
Unmapped Stack trace (pointing to transpiled code):
Alternatively, try throwing and not catching, to compare the trace in your browser's console:
================================================ FILE: index.d.ts ================================================ declare module 'sourcemapped-stacktrace' { export interface MapStackTraceOptions { /** Filter function applied to each stackTrace line. Lines which do not pass the filter won't be processesd. */ filter?: (line: string) => boolean /** Whether to cache sourcemaps globally across multiple calls. */ cacheGlobally?: boolean /** Whether to use synchronous ajax to load the sourcemaps. */ sync?: boolean } /** * Re-map entries in a stacktrace using sourcemaps if available. * * @param stack The stacktrace from the browser (`error.stack`). * @param done Callback invoked with the transformed stacktrace. * @param opts Options object. */ export function mapStackTrace(stack: string | undefined, done: (mappedStack: string[]) => void, opts?: MapStackTraceOptions): void } ================================================ FILE: package.json ================================================ { "name": "sourcemapped-stacktrace", "version": "1.1.11", "homepage": "https://github.com/novocaine/sourcemapped-stacktrace", "license": "BSD-3-Clause", "author": { "name": "James Salter", "email": "iteration@gmail.com" }, "main": "dist/sourcemapped-stacktrace.js", "description": "A simple module for applying source maps to JS stack traces in the browser.", "dependencies": { "source-map": "0.5.6" }, "devDependencies": { "babel-preset-es2015": "^6.9.0", "webpack": "^1.12.0" }, "repository": { "type": "git", "url": "http://github.com/novocaine/sourcemapped-stacktrace" }, "scripts": { "prepublish": "webpack" } } ================================================ FILE: sourcemapped-stacktrace.js ================================================ /* * sourcemapped-stacktrace.js * created by James Salter