Skip to content
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
10 changes: 7 additions & 3 deletions src/master/implementation.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion src/types/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down