Skip to content

Commit f0840f7

Browse files
committed
Add offline usage example to README
Issue: #34
1 parent 4b035c1 commit f0840f7

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

README.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ stacktrace-gps - Turn partial code location into precise code location
55
This library accepts a code location (in the form of a [StackFrame](https://github.com/stacktracejs/stackframe)) and
66
returns a new StackFrame with a more accurate location (using [source maps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/)) and guessed function names.
77

8+
This is primarily a browser-centric library, but can be used with node.js. See the [Offline Usage section](#offline-usage) below.
9+
810
## Usage
911
```js
1012
var stackframe = new StackFrame({fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284});
@@ -17,15 +19,34 @@ var gps = new StackTraceGPS();
1719

1820
// Pinpoint actual function name and source-mapped location
1921
gps.pinpoint(stackframe).then(callback, errback);
20-
=> Promise(StackFrame({functionName: 'fun', fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
22+
//===> Promise(StackFrame({functionName: 'fun', fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
2123

2224
// Better location/name information from source maps
2325
gps.getMappedLocation(stackframe).then(callback, errback);
24-
=> Promise(StackFrame({fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
26+
//===> Promise(StackFrame({fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
2527

2628
// Get function name from location information
2729
gps.findFunctionName(stackframe).then(callback, errback);
28-
=> Promise(StackFrame({functionName: 'fun', fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284}), Error)
30+
//===> Promise(StackFrame({functionName: 'fun', fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284}), Error)
31+
```
32+
33+
### Offline Usage
34+
With a bit of preparation, you can use this library offline in any environment. Any encountered fileNames not in the cache return resolved
35+
Promises with the original StackFrame. StackTraceGPS will make a best effort to provide as good of response with what is given and will
36+
fallback to the original StackFrame if nothing better could be found.
37+
38+
```js
39+
var stack = ErrorStackParser.parse(new Error('boom'));
40+
console.assert(stack[0] == new StackFrame({fileName: 'http://localhost:9999/file.min.js', lineNumber: 1, columnNumber: 32}));
41+
42+
var sourceCache = {'http://localhost:9999/file.min.js': 'var foo=function(){};function bar(){}var baz=eval("XXX");\n//# sourceMappingURL=file.js.map'};
43+
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"}';
44+
var sourceMapConsumerCache = {'http://localhost:4000/file.js.map': new SourceMap.SourceMapConsumer(sourceMap)};
45+
46+
var gps = new StackTraceGPS({offline: true, sourceCache: sourceCache, sourceMapConsumerCache: sourceMapConsumerCache});
47+
gps.pinpoint(stack[0]).then(function(betterStackFrame) {
48+
console.assert(betterStackFrame === new StackFrame({functionName: 'bar', fileName: 'http://localhost:9999/file.js', lineNumber: 2, columnNumber: 9}));
49+
});
2950
```
3051

3152
## Installation
@@ -39,7 +60,8 @@ https://raw.githubusercontent.com/stacktracejs/stacktrace-gps/master/dist/stackt
3960

4061
#### `new StackTraceGPS(/*optional*/ options)` => StackTraceGPS
4162
options: Object
42-
* **sourceCache: Object (String URL => String Source)** - Pre-populate source cache to avoid network requests
63+
* **sourceCache: Object (String URL : String Source)** - Pre-populate source cache to avoid network requests
64+
* **sourceMapConsumerCache: Object (Source Mapping URL : SourceMap.SourceMapConsumer)** - Pre-populate source cache to avoid network requests
4365
* **offline: Boolean (default false)** - Set to `true` to prevent all network requests
4466
* **ajax: Function (String URL => Promise(responseText))** - Function to be used for making X-Domain requests
4567
* **atob: Function (String => String)** - Function to convert base64-encoded strings to their original representation

0 commit comments

Comments
 (0)