From eeccb43fff2b708696b5252394d84591ce899979 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 27 Jan 2022 05:04:19 -0600 Subject: [PATCH] fix: delete postMessage/importScripts --- src/lib/types.ts | 2 -- src/lib/web-worker/init-web-worker.ts | 27 +++++++++++++------------- src/lib/web-worker/worker-constants.ts | 4 ++++ src/lib/web-worker/worker-window.ts | 7 ++++--- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index 4d4ed89b..e6698e00 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -117,9 +117,7 @@ export interface WebWorkerContext { $importScripts$: (...urls: string[]) => void; $isInitialized$?: number; $libPath$: string; - $localStorage$: StorageItem[]; $postMessage$: (msg: MessageFromWorkerToSandbox, arr?: any[]) => void; - $sessionStorage$: StorageItem[]; $sharedDataBuffer$?: SharedArrayBuffer; lastLog?: string; } diff --git a/src/lib/web-worker/init-web-worker.ts b/src/lib/web-worker/init-web-worker.ts index 3365c643..9fb2a70b 100644 --- a/src/lib/web-worker/init-web-worker.ts +++ b/src/lib/web-worker/init-web-worker.ts @@ -5,29 +5,21 @@ import type { InitWebWorkerData } from '../types'; import { Node } from './worker-node'; import type { PartytownConfig } from '@builder.io/partytown/intergration'; import { Performance } from './worker-performance'; -import { webWorkerCtx } from './worker-constants'; +import { webWorkerCtx, webWorkerlocalStorage, webWorkerSessionStorage } from './worker-constants'; import { Window } from './worker-window'; export const initWebWorker = (initWebWorkerData: InitWebWorkerData) => { const config: PartytownConfig = (webWorkerCtx.$config$ = JSON.parse(initWebWorkerData.$config$)); - - const functionify = (configName: keyof PartytownConfig) => { - if (config[configName]) { - config[configName] = new Function('return ' + config[configName])(); - } - }; - - const fnConfigs: (keyof PartytownConfig)[] = ['resolveUrl', 'get', 'set', 'apply']; - fnConfigs.map(functionify); - webWorkerCtx.$importScripts$ = importScripts.bind(self); webWorkerCtx.$libPath$ = initWebWorkerData.$libPath$; - webWorkerCtx.$localStorage$ = new Map([[self.origin, initWebWorkerData.$localStorage$]]); - webWorkerCtx.$sessionStorage$ = new Map([[self.origin, initWebWorkerData.$sessionStorage$]]); webWorkerCtx.$postMessage$ = (postMessage as any).bind(self); webWorkerCtx.$sharedDataBuffer$ = initWebWorkerData.$sharedDataBuffer$; - (self as any).postMessage = (self as any).importScripts = undefined; + webWorkerlocalStorage.set(origin, initWebWorkerData.$localStorage$); + webWorkerSessionStorage.set(origin, initWebWorkerData.$sessionStorage$); + + delete (self as any).postMessage; + delete (self as any).importScripts; (self as any).Node = Node; (self as any).Window = Window; @@ -38,6 +30,13 @@ export const initWebWorker = (initWebWorkerData: InitWebWorkerData) => { patchPrototypes(); + const fnConfigs: (keyof PartytownConfig)[] = ['resolveUrl', 'get', 'set', 'apply']; + fnConfigs.map((configName: keyof PartytownConfig) => { + if (config[configName]) { + config[configName] = new Function('return ' + config[configName])(); + } + }); + webWorkerCtx.$isInitialized$ = 1; logWorker(`Initialized web worker`); diff --git a/src/lib/web-worker/worker-constants.ts b/src/lib/web-worker/worker-constants.ts index a26b250e..c4646adc 100644 --- a/src/lib/web-worker/worker-constants.ts +++ b/src/lib/web-worker/worker-constants.ts @@ -2,6 +2,7 @@ import type { InterfaceInfo, PostMessageData, RefHandler, + StorageItem, WebWorkerContext, WebWorkerEnvironment, } from '../types'; @@ -27,6 +28,9 @@ export const postMessages: PostMessageData[] = []; export const webWorkerCtx: WebWorkerContext = {} as any; export const webWorkerInterfaces: InterfaceInfo[] = []; +export const webWorkerlocalStorage = /*#__PURE__*/ new Map(); +export const webWorkerSessionStorage = /*#__PURE__*/ new Map(); + export const environments: { [winId: number]: WebWorkerEnvironment } = {}; export const cachedDimensions = /*#__PURE__*/ new Map(); diff --git a/src/lib/web-worker/worker-window.ts b/src/lib/web-worker/worker-window.ts index 51876b2f..d76fb71b 100644 --- a/src/lib/web-worker/worker-window.ts +++ b/src/lib/web-worker/worker-window.ts @@ -9,7 +9,8 @@ import { envGlobalConstructors, environments, postMessages, - webWorkerCtx, + webWorkerlocalStorage, + webWorkerSessionStorage, WinIdKey, } from './worker-constants'; import { getEnv } from './worker-environment'; @@ -132,8 +133,8 @@ export class Window extends WorkerInstance { _this.cancelIdleCallback = (id: number) => clearTimeout(id); // add storage APIs to the window - addStorageApi(_this, 'localStorage', webWorkerCtx.$localStorage$); - addStorageApi(_this, 'sessionStorage', webWorkerCtx.$sessionStorage$); + addStorageApi(_this, 'localStorage', webWorkerlocalStorage); + addStorageApi(_this, 'sessionStorage', webWorkerSessionStorage); if (isIframeWindow) { historyState = {};