Skip to content

Commit 37d3fb7

Browse files
Use es5-ext/global as a more robust way to resolve browser's window
The [es5-ext/global][es5g] module is based on Mathias Bynens' [polyfill][poly] for [`globalThis`][gT]. It uses the same robust technique but is more conservative in that it does not add a `globalThis` property to the resolved object, i.e. `window` in a browser and `global` in Node. It also lacks the "old IE" fallback seen in a [demo][demo] of the original polyfill, but that is easily provided in this commit. [es5g]: https://github.com/medikoo/es5-ext/blob/master/global.js [poly]: https://mathiasbynens.be/notes/globalthis [gT]: https://github.com/tc39/proposal-global [demo]: https://mathiasbynens.be/demo/globalthis-ie
1 parent 022e15d commit 37d3fb7

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/browser.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
var _global = (function () {
2-
if (!this && typeof global !== 'undefined') {
3-
return global;
4-
}
5-
return this;
6-
})();
7-
var NativeWebSocket = _global.WebSocket || _global.MozWebSocket;
1+
var _globalThis;
2+
try {
3+
_globalThis = require('es5-ext/global');
4+
} catch (error) {
5+
} finally {
6+
if (!_globalThis && typeof window !== 'undefined') { _globalThis = window; }
7+
if (!_globalThis) { throw new Error('Could not determine global this'); }
8+
}
9+
10+
var NativeWebSocket = _globalThis.WebSocket || _globalThis.MozWebSocket;
811
var websocket_version = require('./version');
912

1013

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"dependencies": {
3030
"debug": "^2.2.0",
31+
"es5-ext": "^0.10.50",
3132
"nan": "^2.14.0",
3233
"typedarray-to-buffer": "^3.1.5",
3334
"yaeti": "^0.0.6"

0 commit comments

Comments
 (0)