Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loadScriptsOnMainThread breaks when using a regexp #540

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ node_modules/
/index.mjs
/index.d.ts
.idea
.history
.history
tests/integrations/load-scripts-on-main-thread/snippet.js
5 changes: 5 additions & 0 deletions scripts/build-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export function buildIntegration(opts: BuildOptions): RollupOptions {
file: join(opts.distIntegrationDir, 'index.mjs'),
format: 'es',
},
{
file: join(opts.testsDir, 'integrations', 'load-scripts-on-main-thread', 'snippet.js'),
format: 'umd',
name: 'partytownIntegration',
},
];

return {
Expand Down
2 changes: 2 additions & 0 deletions src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export { SCRIPT_TYPE } from '../lib/utils';
export type {
PartytownConfig,
PartytownForwardProperty,
PartytownForwardPropertyWithSettings,
PartytownForwardPropertySettings,
ApplyHook,
GetHook,
SetHook,
Expand Down
11 changes: 2 additions & 9 deletions src/integration/snippet.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { serializeConfig } from '../lib/utils';
import type { PartytownConfig } from '../lib/types';

export const createSnippet = (config: PartytownConfig | undefined | null, snippetCode: string) => {
const { forward = [], ...filteredConfig } = config || {};

const configStr = JSON.stringify(filteredConfig, (k, v) => {
if (typeof v === 'function') {
v = String(v);
if (v.startsWith(k + '(')) {
v = 'function ' + v;
}
}
return v;
});
const configStr = serializeConfig(filteredConfig);

return [
`!(function(w,p,f,c){`,
Expand Down
11 changes: 2 additions & 9 deletions src/lib/sandbox/read-main-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isValidMemberName,
len,
noop,
serializeConfig,
} from '../utils';
import { config, docImpl, libPath, mainWindow } from './main-globals';
import {
Expand Down Expand Up @@ -61,15 +62,7 @@ export const readMainPlatform = () => {
readImplementation('Node', textNode),
];

const $config$ = JSON.stringify(config, (k, v) => {
if (typeof v === 'function') {
v = String(v);
if (v.startsWith(k + '(')) {
v = 'function ' + v;
}
}
return v;
});
const $config$ = serializeConfig(config);

const initWebWorkerData: InitWebWorkerData = {
$config$,
Expand Down
7 changes: 6 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export type InterfaceMember =

export interface WebWorkerContext {
$asyncMsgTimer$?: any;
$config$: PartytownConfig;
$config$: PartytownInternalConfig;
$importScripts$: (...urls: string[]) => void;
$initWindowMedia$?: InitWindowMedia;
$interfaces$: InterfaceInfo[];
Expand Down Expand Up @@ -522,6 +522,10 @@ export interface PartytownConfig {
nonce?: string;
}

export type PartytownInternalConfig = Omit<PartytownConfig, 'loadScriptsOnMainThread'> & {
loadScriptsOnMainThread?: ['regexp' | 'string', string][];
};

export type PartytownForwardPropertySettings = {
preserveBehavior?: boolean;
};
Expand Down Expand Up @@ -715,6 +719,7 @@ export interface WorkerInstance {
}

export interface WorkerNode extends WorkerInstance, Node {
id?: string | undefined | null;
gioboa marked this conversation as resolved.
Show resolved Hide resolved
type: string | undefined;
}

Expand Down
41 changes: 41 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {
ApplyPath,
MainWindow,
PartytownConfig,
PartytownForwardProperty,
PartytownForwardPropertySettings,
PartytownForwardPropertyWithSettings,
PartytownInternalConfig,
RandomId,
StringIndexable,
} from './types';
Expand Down Expand Up @@ -205,3 +207,42 @@ export const emptyObjectValue = (propertyName: string): [] | {} => {

return {};
};

function escapeRegExp(input: string) {
return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

export function testIfMustLoadScriptOnMainThread(
config: PartytownInternalConfig,
value: string
): boolean {
return (
config.loadScriptsOnMainThread
?.map(([type, value]) => new RegExp(type === 'string' ? escapeRegExp(value) : value))
.some((regexp) => regexp.test(value)) ?? false
);
}

export function serializeConfig(config: PartytownConfig) {
return JSON.stringify(config, (key, value) => {
if (typeof value === 'function') {
value = String(value);
if (value.startsWith(key + '(')) {
value = 'function ' + value;
}
}
if (key === 'loadScriptsOnMainThread') {
value = (
value as Required<PartytownConfig | PartytownInternalConfig>['loadScriptsOnMainThread']
).map((scriptUrl) =>
Array.isArray(scriptUrl)
? scriptUrl
: [
typeof scriptUrl === 'string' ? 'string' : 'regexp',
typeof scriptUrl === 'string' ? scriptUrl : scriptUrl.source,
]
) satisfies Required<PartytownInternalConfig>['loadScriptsOnMainThread'];
}
return value;
});
}
17 changes: 10 additions & 7 deletions src/lib/web-worker/init-web-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
webWorkerlocalStorage,
webWorkerSessionStorage,
} from './worker-constants';
import type { InitWebWorkerData } from '../types';
import type { PartytownConfig } from '@builder.io/partytown/integration';
import type { InitWebWorkerData, PartytownInternalConfig } from '../types';

export const initWebWorker = (initWebWorkerData: InitWebWorkerData) => {
const config: PartytownConfig = (webWorkerCtx.$config$ = JSON.parse(initWebWorkerData.$config$));
const config: PartytownInternalConfig = (webWorkerCtx.$config$ = JSON.parse(
initWebWorkerData.$config$
));
const locOrigin = initWebWorkerData.$origin$;
webWorkerCtx.$importScripts$ = importScripts.bind(self);
webWorkerCtx.$interfaces$ = initWebWorkerData.$interfaces$;
Expand All @@ -24,9 +25,11 @@ export const initWebWorker = (initWebWorkerData: InitWebWorkerData) => {
delete (self as any).postMessage;
delete (self as any).WorkerGlobalScope;

(commaSplit('resolveUrl,get,set,apply') as any).map((configName: keyof PartytownConfig) => {
if (config[configName]) {
config[configName] = new Function('return ' + config[configName])();
(commaSplit('resolveUrl,get,set,apply') as any).map(
(configName: keyof PartytownInternalConfig) => {
if (config[configName]) {
config[configName] = new Function('return ' + config[configName])();
}
}
});
);
};
9 changes: 7 additions & 2 deletions src/lib/web-worker/worker-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
webWorkerCtx,
WinIdKey,
} from './worker-constants';
import { defineConstructorName, SCRIPT_TYPE, SCRIPT_TYPE_EXEC } from '../utils';
import {
defineConstructorName,
SCRIPT_TYPE,
SCRIPT_TYPE_EXEC,
testIfMustLoadScriptOnMainThread,
} from '../utils';
import { getInstanceStateValue } from './worker-state';
import { insertIframe, runScriptContent } from './worker-exec';
import { isScriptJsType } from './worker-script';
Expand Down Expand Up @@ -59,7 +64,7 @@ export const createNodeCstr = (
// @ts-ignore
const scriptId = newNode.id;
const loadOnMainThread =
scriptId && config.loadScriptsOnMainThread?.includes?.(scriptId);
scriptId && testIfMustLoadScriptOnMainThread(config, scriptId);

if (loadOnMainThread) {
setter(newNode, ['type'], 'text/javascript');
Expand Down
8 changes: 3 additions & 5 deletions src/lib/web-worker/worker-script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { definePrototypePropertyDescriptor } from '../utils';
import { definePrototypePropertyDescriptor, testIfMustLoadScriptOnMainThread } from '../utils';
import { getInstanceStateValue, setInstanceStateValue } from './worker-state';
import { getter, setter } from './worker-proxy';
import { HTMLSrcElementDescriptorMap } from './worker-src-element';
Expand Down Expand Up @@ -26,10 +26,8 @@ export const patchHTMLScriptElement = (WorkerHTMLScriptElement: any, env: WebWor
setter(this, ['dataset', 'ptsrc'], orgUrl);
}

if (this.type && config.loadScriptsOnMainThread) {
const shouldExecuteScriptViaMainThread = config.loadScriptsOnMainThread.some(
(scriptUrl) => new RegExp(scriptUrl).test(url)
);
if (this.type) {
const shouldExecuteScriptViaMainThread = testIfMustLoadScriptOnMainThread(config, url);

if (shouldExecuteScriptViaMainThread) {
setter(this, ['type'], 'text/javascript');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(() => {
self.backgroundTestScriptRanInTheBackgroundFlag = self.name !== '';
document.body.classList.add('background-completed');
})();
Loading
Loading