Skip to content

Commit 0154559

Browse files
authored
Add CORSWorkaround option + update isAbsoluteURL (#287)
* Fix isAbsoluteURL() regex * Option to skip CORS workaround
1 parent 131470e commit 0154559

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/master/implementation.browser.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const defaultPoolSize = typeof navigator !== "undefined" && navigator.har
77
? navigator.hardwareConcurrency
88
: 4
99

10-
const isAbsoluteURL = (value: string) => /^(file|https?:)?\/\//i.test(value)
10+
const isAbsoluteURL = (value: string) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value)
1111

1212
function createSourceBlobURL(code: string): string {
1313
const blob = new Blob(
@@ -34,12 +34,16 @@ function selectWorkerImplementation(): ImplementationExport {
3434
url = new URL(url, options._baseURL)
3535
} else if (typeof url === "string" && !isAbsoluteURL(url) && getBundleURL().match(/^file:\/\//i)) {
3636
url = new URL(url, getBundleURL().replace(/\/[^\/]+$/, "/"))
37-
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)
37+
if (options?.CORSWorkaround ?? true) {
38+
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)
39+
}
3840
}
3941
if (typeof url === "string" && isAbsoluteURL(url)) {
4042
// Create source code blob loading JS file via `importScripts()`
4143
// to circumvent worker CORS restrictions
42-
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)
44+
if (options?.CORSWorkaround ?? true) {
45+
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)
46+
}
4347
}
4448
super(url, options)
4549
}

src/types/master.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ export interface ThreadsWorkerOptions extends WorkerOptions {
8181
/** The size of a pre-allocated memory range used for generated code. */
8282
codeRangeSizeMb?: number
8383
}
84-
/** Data passed on to Node worker_threads */
84+
/** Data passed on to node.js worker_threads. */
8585
workerData?: any
86+
87+
/** Whether to apply CORS protection workaround. Defaults to true. */
88+
CORSWorkaround?: boolean
8689
}
8790

8891
/** Worker implementation. Either web worker or a node.js Worker class. */

0 commit comments

Comments
 (0)