Skip to content

Commit a098132

Browse files
committed
fix(root): find global context (window/self/global) in a more safe way
Closes #1930
1 parent 6bd3423 commit a098132

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

spec/util/root-spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect } from 'chai';
2+
import { root } from '../../dist/cjs/util/root';
3+
4+
/** @test {root} */
5+
describe('root', () => {
6+
it('should exist', () => {
7+
expect(typeof root).to.equal('object');
8+
});
9+
});

src/util/root.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
let objectTypes = {
2-
'boolean': false,
3-
'function': true,
4-
'object': true,
5-
'number': false,
6-
'string': false,
7-
'undefined': false
8-
};
9-
101
declare let global: NodeJS.Global;
112
declare let module: any;
123
declare let exports: any;
@@ -18,9 +9,17 @@ declare module NodeJS {
189
}
1910
}
2011

21-
export let root: any = (objectTypes[typeof self] && self) || (objectTypes[typeof window] && window);
12+
/**
13+
* window: browser in DOM main thread
14+
* self: browser in WebWorker
15+
* global: Node.js/other
16+
*/
17+
export const root: any = (
18+
typeof window == 'object' && window.window === window && window
19+
|| typeof self == 'object' && self.self === self && self
20+
|| typeof global == 'object' && global.global === global && global
21+
);
2222

23-
let freeGlobal = objectTypes[typeof global] && global;
24-
if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
25-
root = freeGlobal;
23+
if (!root) {
24+
throw new Error('RxJS could not find any global context (window, self, global)');
2625
}

0 commit comments

Comments
 (0)