Skip to content

Commit 35e442d

Browse files
committed
Merge pull request #111 from tugend/master
Support for forcing the mode (auto, browser or node) (fix #102)
2 parents 263ae33 + 3c344dc commit 35e442d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ require('source-map-support').install({
9191
});
9292
```
9393

94+
The module will by default assume a browser environment if XMLHttpRequest and window are defined. If either of these do not exist it will instead assume a node environment.
95+
In some rare cases, e.g. when running a browser emulation and where both variables are also set, you can explictly specify the environment to be either 'browser' or 'node'.
96+
97+
```js
98+
require('source-map-support').install({
99+
environment: 'node'
100+
});
101+
```
102+
94103
## Demos
95104

96105
#### Basic Demo

source-map-support.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var alreadyInstalled = false;
88
// If true, the caches are reset before a stack trace formatting operation
99
var emptyCacheBetweenOperations = false;
1010

11+
// Supports {browser, node, auto}
12+
var environment = "auto";
13+
1114
// Maps a file path to a string containing the file contents
1215
var fileContentsCache = {};
1316

@@ -18,6 +21,10 @@ var sourceMapCache = {};
1821
var reSourceMap = /^data:application\/json[^,]+base64,/;
1922

2023
function isInBrowser() {
24+
if (environment === "browser")
25+
return true;
26+
if (environment === "node")
27+
return false;
2128
return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function'));
2229
}
2330

@@ -403,9 +410,16 @@ exports.install = function(options) {
403410
options = options || {};
404411
var installHandler = 'handleUncaughtExceptions' in options ?
405412
options.handleUncaughtExceptions : true;
413+
406414
emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
407415
options.emptyCacheBetweenOperations : false;
408416

417+
if (options.environment) {
418+
environment = options.environment;
419+
if (["node", "browser", "auto"].indexOf(environment) === -1)
420+
throw new Error("environment " + environment + " was unknown. Available options are {auto, browser, node}")
421+
}
422+
409423
// Allow sources to be found by methods other than reading the files
410424
// directly from disk.
411425
if (options.retrieveFile)

0 commit comments

Comments
 (0)