[d565449] | 1 | stacktrace-gps - Turn partial code location into precise code location
|
---|
| 2 | ===================
|
---|
| 3 | [![Build Status](https://img.shields.io/github/workflow/status/stacktracejs/stacktrace-gps/Continuous%20Integration/master?logo=github&style=flat-square)](https://github.com/stacktracejs/stacktrace-gps/actions?query=workflow%3AContinuous+Integration+branch%3Amaster)
|
---|
| 4 | [![Coverage Status](https://img.shields.io/coveralls/stacktracejs/stacktrace-gps.svg?style=flat-square)](https://coveralls.io/r/stacktracejs/stacktrace-gps?branch=master)
|
---|
| 5 | [![GitHub license](https://img.shields.io/github/license/stacktracejs/stacktrace-gps.svg?style=flat-square)](https://opensource.org/licenses/MIT)
|
---|
| 6 |
|
---|
| 7 | This library accepts a code location (in the form of a [StackFrame](https://github.com/stacktracejs/stackframe)) and
|
---|
| 8 | returns a new StackFrame with a more accurate location (using [source maps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/)) and guessed function names.
|
---|
| 9 |
|
---|
| 10 | This is primarily a browser-centric library, but can be used with node.js. See the [Offline Usage section](#offline-usage) below.
|
---|
| 11 |
|
---|
| 12 | ## Usage
|
---|
| 13 | ```js
|
---|
| 14 | var stackframe = new StackFrame({fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284});
|
---|
| 15 | var callback = function myCallback(foundFunctionName) { console.log(foundFunctionName); };
|
---|
| 16 |
|
---|
| 17 | // Such meta. Wow
|
---|
| 18 | var errback = function myErrback(error) { console.log(StackTrace.fromError(error)); };
|
---|
| 19 |
|
---|
| 20 | var gps = new StackTraceGPS();
|
---|
| 21 |
|
---|
| 22 | // Pinpoint actual function name and source-mapped location
|
---|
| 23 | gps.pinpoint(stackframe).then(callback, errback);
|
---|
| 24 | //===> Promise(StackFrame({functionName: 'fun', fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
|
---|
| 25 |
|
---|
| 26 | // Better location/name information from source maps
|
---|
| 27 | gps.getMappedLocation(stackframe).then(callback, errback);
|
---|
| 28 | //===> Promise(StackFrame({fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
|
---|
| 29 |
|
---|
| 30 | // Get function name from location information
|
---|
| 31 | gps.findFunctionName(stackframe).then(callback, errback);
|
---|
| 32 | //===> Promise(StackFrame({functionName: 'fun', fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284}), Error)
|
---|
| 33 | ```
|
---|
| 34 |
|
---|
| 35 | ### Offline Usage
|
---|
| 36 | With a bit of preparation, you can use this library offline in any environment. Any encountered fileNames not in the cache return resolved
|
---|
| 37 | Promises with the original StackFrame. StackTraceGPS will make a best effort to provide as good of response with what is given and will
|
---|
| 38 | fallback to the original StackFrame if nothing better could be found.
|
---|
| 39 |
|
---|
| 40 | ```js
|
---|
| 41 | var stack = ErrorStackParser.parse(new Error('boom'));
|
---|
| 42 | console.assert(stack[0] == new StackFrame({fileName: 'http://localhost:9999/file.min.js', lineNumber: 1, columnNumber: 32}));
|
---|
| 43 |
|
---|
| 44 | var sourceCache = {'http://localhost:9999/file.min.js': 'var foo=function(){};function bar(){}var baz=eval("XXX");\n//# sourceMappingURL=file.js.map'};
|
---|
| 45 | var sourceMap = '{"version":3,"sources":["./file.js"],"sourceRoot":"http://localhost:4000/","names":["foo","bar","baz","eval"],"mappings":"AAAA,GAAIA,KAAM,YACV,SAASC,QACT,GAAIC,KAAMC,KAAK","file":"file.min.js"}';
|
---|
| 46 | var sourceMapConsumerCache = {'http://localhost:4000/file.js.map': new SourceMap.SourceMapConsumer(sourceMap)};
|
---|
| 47 |
|
---|
| 48 | var gps = new StackTraceGPS({offline: true, sourceCache: sourceCache, sourceMapConsumerCache: sourceMapConsumerCache});
|
---|
| 49 | gps.pinpoint(stack[0]).then(function(betterStackFrame) {
|
---|
| 50 | console.assert(betterStackFrame === new StackFrame({functionName: 'bar', fileName: 'http://localhost:9999/file.js', lineNumber: 2, columnNumber: 9}));
|
---|
| 51 | });
|
---|
| 52 | ```
|
---|
| 53 |
|
---|
| 54 | ## Installation
|
---|
| 55 | ```
|
---|
| 56 | npm install stacktrace-gps
|
---|
| 57 | bower install stacktrace-gps
|
---|
| 58 | https://raw.githubusercontent.com/stacktracejs/stacktrace-gps/master/dist/stacktrace-gps.min.js
|
---|
| 59 | ```
|
---|
| 60 |
|
---|
| 61 | ## API
|
---|
| 62 |
|
---|
| 63 | #### `new StackTraceGPS(/*optional*/ options)` => StackTraceGPS
|
---|
| 64 | options: Object
|
---|
| 65 | * **sourceCache: Object (String URL : String Source)** - Pre-populate source cache to avoid network requests
|
---|
| 66 | * **sourceMapConsumerCache: Object (Source Mapping URL : SourceMap.SourceMapConsumer)** - Pre-populate source cache to avoid network requests
|
---|
| 67 | * **offline: Boolean (default false)** - Set to `true` to prevent all network requests
|
---|
| 68 | * **ajax: Function (String URL => Promise(responseText))** - Function to be used for making X-Domain requests
|
---|
| 69 | * **atob: Function (String => String)** - Function to convert base64-encoded strings to their original representation
|
---|
| 70 |
|
---|
| 71 | #### `.pinpoint(stackframe)` => Promise(StackFrame)
|
---|
| 72 | Enhance function name and use source maps to produce a better StackFrame.
|
---|
| 73 | * **stackframe** - [StackFrame](https://github.com/stacktracejs/stackframe) or like object
|
---|
| 74 | e.g. {fileName: 'path/to/file.js', lineNumber: 100, columnNumber: 5}
|
---|
| 75 |
|
---|
| 76 | #### `.findFunctionName(stackframe)` => Promise(StackFrame)
|
---|
| 77 | Enhance function name and use source maps to produce a better StackFrame.
|
---|
| 78 | * **stackframe** - [StackFrame](https://github.com/stacktracejs/stackframe) or like object
|
---|
| 79 |
|
---|
| 80 | #### `.getMappedLocation(stackframe)` => Promise(StackFrame)
|
---|
| 81 | Enhance function name and use source maps to produce a better StackFrame.
|
---|
| 82 | * **stackframe** - [StackFrame](https://github.com/stacktracejs/stackframe) or like object
|
---|
| 83 |
|
---|
| 84 | ## Browser Support
|
---|
| 85 | [![Sauce Test Status](https://saucelabs.com/browser-matrix/stacktracejs.svg)](https://saucelabs.com/u/stacktracejs)
|
---|
| 86 |
|
---|
| 87 | Functions that rely on [Source Maps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/)
|
---|
| 88 | (`pinpoint` and `getMappedLocation`) require recent browsers.
|
---|
| 89 |
|
---|
| 90 | ## Contributing
|
---|
| 91 | Want to be listed as a *Contributor*? Start with the [Contributing Guide](.github/CONTRIBUTING.md)!
|
---|