-
Notifications
You must be signed in to change notification settings - Fork 443
/
worker-window.ts
643 lines (581 loc) · 21.2 KB
/
worker-window.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import { addStorageApi } from './worker-storage';
import {
ApplyPath,
CallType,
InstanceId,
InterfaceType,
NodeName,
WebWorkerEnvironment,
WinDocId,
WinId,
WorkerInstance,
WorkerNode,
WorkerNodeConstructors,
WorkerWindow,
} from '../types';
import {
ABOUT_BLANK,
ApplyPathKey,
commaSplit,
environments,
eventTargetMethods,
InstanceDataKey,
InstanceIdKey,
InstanceStateKey,
NamespaceKey,
postMessages,
webWorkerCtx,
WinIdKey,
} from './worker-constants';
import { createCustomElementRegistry } from './worker-custom-elements';
import {
cachedDimensionMethods,
cachedDimensionProps,
cachedProps,
definePrototypeNodeType,
getOrCreateNodeInstance,
} from './worker-constructors';
import { callMethod, constructGlobal, getter, setter } from './worker-proxy';
import { createCSSStyleDeclarationCstr } from './worker-css-style-declaration';
import { createCSSStyleSheetConstructor } from './worker-style';
import { createImageConstructor } from './worker-image';
import { createNavigator } from './worker-navigator';
import { createNodeCstr } from './worker-node';
import { createPerformanceConstructor } from './worker-performance';
import {
debug,
defineConstructorName,
defineProperty,
definePrototypeProperty,
definePrototypeValue,
getConstructorName,
len,
randomId,
} from '../utils';
import {
getInstanceStateValue,
hasInstanceStateValue,
setInstanceStateValue,
} from './worker-state';
import { getInitWindowMedia, htmlMedia, windowMediaConstructors } from './worker-media';
import { logWorker, normalizedWinId } from '../log';
import {
patchDocument,
patchDocumentElementChild,
patchDocumentFragment,
patchHTMLHtmlElement,
} from './worker-document';
import { patchElement } from './worker-element';
import { patchHTMLAnchorElement } from './worker-anchor';
import { patchHTMLFormElement } from './worker-form';
import { patchHTMLIFrameElement } from './worker-iframe';
import { patchHTMLScriptElement } from './worker-script';
import { patchSvgElement } from './worker-svg';
import { resolveUrl } from './worker-exec';
import { createNodeListCstr } from './worker-serialization';
import { createNamedNodeMapCstr } from './worker-named-node-map';
export const createWindow = (
$winId$: WinId,
$parentWinId$: WinId,
url: string,
$visibilityState$?: string,
isIframeWindow?: boolean,
isDocumentImplementation?: boolean
) => {
let cstrInstanceId: InstanceId | undefined;
let cstrNodeName: string | undefined;
let cstrNamespace: string | undefined;
let cstrPrevInstance: WorkerNode | undefined;
// base class all Nodes/Elements/Global Constructors will extend
const WorkerBase = class implements WorkerInstance {
[WinIdKey]: WinId;
[InstanceIdKey]: InstanceId;
[ApplyPathKey]: string[];
[InstanceDataKey]: any;
[NamespaceKey]: string | undefined;
[InstanceStateKey]: { [key: string]: any };
constructor(
winId?: WinId,
instanceId?: InstanceId,
applyPath?: ApplyPath,
instanceData?: any,
namespace?: string
) {
this[WinIdKey] = winId || $winId$;
this[InstanceIdKey] = instanceId || cstrInstanceId || randomId();
this[ApplyPathKey] = applyPath || [];
this[InstanceDataKey] = instanceData || cstrNodeName;
this[NamespaceKey] = namespace || cstrNamespace;
this[InstanceStateKey] = (cstrPrevInstance && cstrPrevInstance[InstanceStateKey]) || {};
cstrInstanceId = cstrNodeName = cstrNamespace = undefined;
}
};
const WorkerLocation = defineConstructorName(
class extends URL {
assign() {
logWorker(`location.assign(), noop`);
}
reload() {
logWorker(`location.reload(), noop`);
}
replace() {
logWorker(`location.replace(), noop`);
}
},
'Location'
);
const $location$ = new WorkerLocation(url);
const $isSameOrigin$ =
$location$.origin === webWorkerCtx.$origin$ || $location$.origin === ABOUT_BLANK;
const $isTopWindow$ = $parentWinId$ === $winId$;
const env: WebWorkerEnvironment = {} as any;
const getChildEnvs = () => {
let childEnv: WebWorkerEnvironment[] = [];
let envWinId: string;
let otherEnv: WebWorkerEnvironment;
for (envWinId in environments) {
otherEnv = environments[envWinId];
if (otherEnv.$parentWinId$ === $winId$ && !otherEnv.$isTopWindow$) {
childEnv.push(otherEnv);
}
}
return childEnv;
};
// window global eveything will live within
const WorkerWindow = defineConstructorName(
class extends WorkerBase implements WorkerWindow {
constructor() {
super($winId$, $winId$);
let win: WorkerWindow = this;
let value: any;
let historyState: any;
let hasInitializedMedia = 0;
let initWindowMedia = () => {
if (!hasInitializedMedia) {
getInitWindowMedia()(
WorkerBase,
WorkerEventTargetProxy,
env,
win,
windowMediaConstructors
);
hasInitializedMedia = 1;
}
};
let nodeCstrs: WorkerNodeConstructors = {};
let $createNode$ = (
nodeName: string,
instanceId: InstanceId,
namespace?: string,
prevInstance?: WorkerNode
): WorkerNode => {
if (htmlMedia.includes(nodeName)) {
initWindowMedia();
}
const NodeCstr = nodeCstrs[nodeName]
? nodeCstrs[nodeName]
: nodeName.includes('-')
? nodeCstrs.UNKNOWN
: nodeCstrs.I;
cstrInstanceId = instanceId;
cstrNodeName = nodeName;
cstrNamespace = namespace;
cstrPrevInstance = prevInstance;
return new NodeCstr() as any;
};
win.Window = WorkerWindow;
win.name = name + (debug ? `${normalizedWinId($winId$)} (${$winId$})` : ($winId$ as any));
createNodeCstr(win, env, WorkerBase);
createNodeListCstr(win);
createNamedNodeMapCstr(win, WorkerBase);
createCSSStyleDeclarationCstr(win, WorkerBase, 'CSSStyleDeclaration');
createPerformanceConstructor(win, WorkerBase, 'Performance');
createCustomElementRegistry(win, nodeCstrs);
// define all of the global constructors that should live on window
webWorkerCtx.$interfaces$.map(
([cstrName, superCstrName, members, interfaceType, nodeName]) => {
const SuperCstr = TrapConstructors[cstrName]
? WorkerTrapProxy
: superCstrName === 'EventTarget'
? WorkerEventTargetProxy
: superCstrName === 'Object'
? WorkerBase
: win[superCstrName];
const Cstr = (win[cstrName] = defineConstructorName(
interfaceType === InterfaceType.EnvGlobalConstructor
? class extends WorkerBase {
// create the constructor and set as a prop on window
constructor(...args: any[]) {
super();
constructGlobal(this, cstrName, args);
}
}
: win[cstrName] || class extends SuperCstr {},
cstrName
));
if (nodeName) {
// this is a node name, such as #text or an element's tagname, like all caps DIV
nodeCstrs[nodeName] = Cstr;
}
members.map(([memberName, memberType, staticValue]) => {
if (!(memberName in Cstr.prototype) && !(memberName in SuperCstr.prototype)) {
// member not already in the constructor's prototype
if (typeof memberType === 'string') {
definePrototypeProperty(Cstr, memberName, {
get(this: WorkerInstance) {
if (!hasInstanceStateValue(this, memberName)) {
const instanceId = this[InstanceIdKey];
const applyPath = [...this[ApplyPathKey], memberName];
const PropCstr: typeof WorkerBase = win[memberType];
if (PropCstr) {
setInstanceStateValue(
this,
memberName,
new PropCstr($winId$, instanceId, applyPath)
);
}
}
return getInstanceStateValue(this, memberName);
},
set(this: WorkerInstance, value) {
setInstanceStateValue(this, memberName, value);
},
});
} else {
// interface type
if (memberType === InterfaceType.Function) {
// method that should access main
definePrototypeValue(Cstr, memberName, function (this: Node, ...args: any[]) {
return callMethod(this, [memberName], args);
});
} else if (memberType > 0) {
// property
if (staticValue !== undefined) {
// static property that doesn't change
// and no need to access main
definePrototypeValue(Cstr, memberName, staticValue);
} else {
// property getter/setter that should access main
definePrototypeProperty(Cstr, memberName, {
get(this: WorkerNode) {
return getter(this, [memberName]);
},
set(this: WorkerNode, value) {
return setter(this, [memberName], value);
},
});
}
}
}
}
});
}
);
// we already assigned the same prototypes found on the main thread's Window
// to the worker's Window, but actually it assigned a few that are already on
// the web worker's global we can use instead. So manually set which web worker
// globals we can reuse, instead of calling the main access.
// These same window properties will be assigned to the window instance
// when Window is constructed, and these won't make calls to the main thread.
commaSplit(
'atob,btoa,crypto,indexedDB,setTimeout,setInterval,clearTimeout,clearInterval'
).map((globalName) => {
delete (WorkerWindow as any).prototype[globalName];
if (!(globalName in win)) {
// global properties already in the web worker global
value = (self as any)[globalName];
if (value != null) {
// function examples: atob(), fetch()
// object examples: crypto, indexedDB
// boolean examples: isSecureContext, crossOriginIsolated
win[globalName] =
typeof value === 'function' && !value.toString().startsWith('class')
? value.bind(self)
: value;
}
}
});
// assign web worker global properties to the environment window
// window.Promise = self.Promise
Object.getOwnPropertyNames(self).map((globalName) => {
if (!(globalName in win)) {
win[globalName] = (self as any)[globalName];
}
});
windowMediaConstructors.map((cstrName) =>
defineProperty(win, cstrName, {
get() {
// lazy load media constructors if called, replacing this getter
initWindowMedia();
return win[cstrName];
},
})
);
if ('trustedTypes' in (self as any)) {
// https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
win.trustedTypes = (self as any).trustedTypes;
}
// patch this window's global constructors with some additional props
patchElement(win.Element, win.HTMLElement);
patchDocument(win.Document, env, isDocumentImplementation);
patchDocumentFragment(win.DocumentFragment);
patchHTMLAnchorElement(win.HTMLAnchorElement, env);
patchHTMLFormElement(win.HTMLFormElement);
patchHTMLIFrameElement(win.HTMLIFrameElement, env);
patchHTMLScriptElement(win.HTMLScriptElement, env);
patchSvgElement(win.SVGGraphicsElement);
patchDocumentElementChild(win.HTMLHeadElement, env);
patchDocumentElementChild(win.HTMLBodyElement, env);
patchHTMLHtmlElement(win.HTMLHtmlElement, env);
createCSSStyleSheetConstructor(win, 'CSSStyleSheet');
definePrototypeNodeType(win.Comment, 8);
definePrototypeNodeType(win.DocumentType, 10);
Object.assign(env, {
$winId$,
$parentWinId$,
$window$: new Proxy(win, {
get: (win, propName: any) => {
if (typeof propName === 'string' && !isNaN(propName as any)) {
// https://developer.mozilla.org/en-US/docs/Web/API/Window/frames
let frame = getChildEnvs()[propName as any];
return frame ? frame.$window$ : undefined;
} else if (webWorkerCtx.$config$.mainWindowAccessors?.includes(propName)) {
return getter(this, [propName]);
} else {
return win[propName];
}
},
has: () =>
// window "has" any and all props, this is especially true for global variables
// that are meant to be assigned to window, but without "window." prefix,
// like: <script>globalProp = true</script>
true,
}) as any,
$document$: $createNode$(NodeName.Document, $winId$ + '.' + WinDocId.document) as any,
$documentElement$: $createNode$(
NodeName.DocumentElement,
$winId$ + '.' + WinDocId.documentElement
) as any,
$head$: $createNode$(NodeName.Head, $winId$ + '.' + WinDocId.head) as any,
$body$: $createNode$(NodeName.Body, $winId$ + '.' + WinDocId.body) as any,
$location$,
$visibilityState$,
$isSameOrigin$,
$isTopWindow$,
$createNode$,
});
// requestAnimationFrame() is provided by Chrome in a web worker, but not Safari
win.requestAnimationFrame = (cb: (ts: number) => void) =>
setTimeout(() => cb(performance.now()), 9);
win.cancelAnimationFrame = (id: number) => clearTimeout(id);
// ensure requestIdleCallback() happens in the worker and doesn't call to main
// it's also not provided by Safari
win.requestIdleCallback = (
cb: (opts: { didTimeout: boolean; timeRemaining: () => number }) => void,
start?: number
) => {
start = Date.now();
return setTimeout(
() =>
cb({
didTimeout: false,
timeRemaining: () => Math.max(0, 50 - (Date.now() - start!)),
}),
1
);
};
win.cancelIdleCallback = (id: number) => clearTimeout(id);
// add storage APIs to the window
addStorageApi(win, 'localStorage', $isSameOrigin$, env);
addStorageApi(win, 'sessionStorage', $isSameOrigin$, env);
if (!$isSameOrigin$) {
win.indexeddb = undefined;
}
if (isIframeWindow) {
historyState = {};
win.history = {
pushState(stateObj: any) {
historyState = stateObj;
},
replaceState(stateObj: any) {
historyState = stateObj;
},
get state() {
return historyState;
},
length: 0,
};
win.indexeddb = undefined;
} else {
const originalPushState: Window['history']['pushState'] = win.history.pushState.bind(
win.history
);
const originalReplaceState: Window['history']['replaceState'] =
win.history.replaceState.bind(win.history);
win.history.pushState = (stateObj: any, _: string, newUrl?: string) => {
if (env.$propagateHistoryChange$ !== false) {
originalPushState(stateObj, _, newUrl);
}
};
win.history.replaceState = (stateObj: any, _: string, newUrl?: string) => {
if (env.$propagateHistoryChange$ !== false) {
originalReplaceState(stateObj, _, newUrl);
}
};
}
win.Worker = undefined;
}
addEventListener = (...args: any[]) => {
if (args[0] === 'load') {
if (env.$runWindowLoadEvent$) {
setTimeout(() => args[1]({ type: 'load' }));
}
} else {
callMethod(this, ['addEventListener'], args, CallType.NonBlocking);
}
};
get body() {
return env.$body$;
}
get document() {
return env.$document$;
}
get documentElement() {
return env.$documentElement$;
}
fetch(input: string | URL | Request, init: any) {
input = typeof input === 'string' || input instanceof URL ? String(input) : input.url;
return fetch(resolveUrl(env, input, 'fetch'), init);
}
get frames() {
// this is actually just the window, which is what handles "length" and window[0]
// https://developer.mozilla.org/en-US/docs/Web/API/Window/frames
return env.$window$;
}
get frameElement() {
if ($isTopWindow$) {
// this is the top window, not in an iframe
return null;
} else {
// the winId of an iframe's window is the same
// as the instanceId of the containing iframe element
return getOrCreateNodeInstance($parentWinId$, $winId$, NodeName.IFrame);
}
}
get globalThis() {
return env.$window$;
}
get head() {
return env.$head$;
}
get length() {
// https://developer.mozilla.org/en-US/docs/Web/API/Window/length
return getChildEnvs().length;
}
get location() {
return $location$;
}
set location(loc: any) {
$location$.href = loc + '';
}
get Image() {
return createImageConstructor(env);
}
get navigator() {
return createNavigator(env);
}
get origin() {
return $location$.origin;
}
set origin(_) {}
get parent(): any {
for (let envWinId in environments) {
if (environments[envWinId].$winId$ === $parentWinId$) {
return environments[envWinId].$window$;
}
}
return env.$window$;
}
postMessage(...args: any[]) {
if (environments[args[0]]) {
if (len(postMessages) > 50) {
postMessages.splice(0, 5);
}
postMessages.push({
$winId$: args[0],
$data$: JSON.stringify(args[1]),
});
args = args.slice(1);
}
callMethod(this, ['postMessage'], args, CallType.NonBlockingNoSideEffect);
}
get self() {
return env.$window$;
}
get top(): any {
for (let envWinId in environments) {
if (environments[envWinId].$isTopWindow$) {
return environments[envWinId].$window$;
}
}
return env.$window$;
}
get window() {
return env.$window$;
}
get XMLHttpRequest() {
const Xhr = XMLHttpRequest;
const str = String(Xhr);
const ExtendedXhr = defineConstructorName(
class extends Xhr {
open(...args: any[]) {
args[1] = resolveUrl(env, args[1], 'xhr');
(super.open as any)(...args);
}
set withCredentials(_: any) {}
toString() {
return str;
}
},
getConstructorName(Xhr)
);
ExtendedXhr.prototype.constructor.toString = () => str;
return ExtendedXhr;
}
},
'Window'
);
// extends WorkerBase, but also a proxy so certain constructors like style.color work
const WorkerTrapProxy = class extends WorkerBase {
constructor(winId: WinId, instanceId: InstanceId, applyPath?: ApplyPath, nodeName?: string) {
super(winId, instanceId, applyPath, nodeName);
return new Proxy(this, {
get(instance, propName) {
return getter(instance, [propName]);
},
set(instance, propName, propValue) {
setter(instance, [propName], propValue);
return true;
},
});
}
};
const WorkerEventTargetProxy = class extends WorkerBase {};
eventTargetMethods.map(
(methodName) =>
((WorkerEventTargetProxy as any).prototype[methodName] = function (...args: any[]) {
return callMethod(this, [methodName], args, CallType.NonBlocking);
})
);
cachedProps(WorkerWindow, 'devicePixelRatio');
cachedDimensionProps(WorkerWindow);
cachedDimensionMethods(WorkerWindow, ['getComputedStyle']);
new WorkerWindow();
return env;
};
// Trap Constructors are ones where all properties have
// proxy traps, such as dataset.name
const TrapConstructors: { [cstrName: string]: 1 } = {
DOMStringMap: 1,
NamedNodeMap: 1,
};