diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index b2d898ee..87846b97 100644 --- a/src/master/implementation.browser.ts +++ b/src/master/implementation.browser.ts @@ -7,7 +7,7 @@ export const defaultPoolSize = typeof navigator !== "undefined" && navigator.har ? navigator.hardwareConcurrency : 4 -const isAbsoluteURL = (value: string) => /^(file|https?:)?\/\//i.test(value) +const isAbsoluteURL = (value: string) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value) function createSourceBlobURL(code: string): string { const blob = new Blob( @@ -34,12 +34,16 @@ function selectWorkerImplementation(): ImplementationExport { url = new URL(url, options._baseURL) } else if (typeof url === "string" && !isAbsoluteURL(url) && getBundleURL().match(/^file:\/\//i)) { url = new URL(url, getBundleURL().replace(/\/[^\/]+$/, "/")) - url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`) + if (options?.CORSWorkaround ?? true) { + url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`) + } } if (typeof url === "string" && isAbsoluteURL(url)) { // Create source code blob loading JS file via `importScripts()` // to circumvent worker CORS restrictions - url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`) + if (options?.CORSWorkaround ?? true) { + url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`) + } } super(url, options) } diff --git a/src/types/master.ts b/src/types/master.ts index 0cae55ea..f3f72a7d 100644 --- a/src/types/master.ts +++ b/src/types/master.ts @@ -81,8 +81,11 @@ export interface ThreadsWorkerOptions extends WorkerOptions { /** The size of a pre-allocated memory range used for generated code. */ codeRangeSizeMb?: number } - /** Data passed on to Node worker_threads */ + /** Data passed on to node.js worker_threads. */ workerData?: any + + /** Whether to apply CORS protection workaround. Defaults to true. */ + CORSWorkaround?: boolean } /** Worker implementation. Either web worker or a node.js Worker class. */