Skip to content

Commit

Permalink
fix: delete postMessage/importScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jan 27, 2022
1 parent 9d2f6c0 commit eeccb43
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
2 changes: 0 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 13 additions & 14 deletions src/lib/web-worker/init-web-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/web-worker/worker-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
InterfaceInfo,
PostMessageData,
RefHandler,
StorageItem,
WebWorkerContext,
WebWorkerEnvironment,
} from '../types';
Expand All @@ -27,6 +28,9 @@ export const postMessages: PostMessageData[] = [];
export const webWorkerCtx: WebWorkerContext = {} as any;
export const webWorkerInterfaces: InterfaceInfo[] = [];

export const webWorkerlocalStorage = /*#__PURE__*/ new Map<string, StorageItem[]>();
export const webWorkerSessionStorage = /*#__PURE__*/ new Map<string, StorageItem[]>();

export const environments: { [winId: number]: WebWorkerEnvironment } = {};

export const cachedDimensions = /*#__PURE__*/ new Map<string, any>();
Expand Down
7 changes: 4 additions & 3 deletions src/lib/web-worker/worker-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
envGlobalConstructors,
environments,
postMessages,
webWorkerCtx,
webWorkerlocalStorage,
webWorkerSessionStorage,
WinIdKey,
} from './worker-constants';
import { getEnv } from './worker-environment';
Expand Down Expand Up @@ -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 = {};
Expand Down

1 comment on commit eeccb43

@vercel
Copy link

@vercel vercel bot commented on eeccb43 Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.