From 7f9225dddb477b48581a1fca0f9b2eec0a022278 Mon Sep 17 00:00:00 2001 From: aminya Date: Mon, 3 Aug 2020 14:00:52 -0500 Subject: [PATCH 1/8] remove incorrect url code --- src/master/implementation.browser.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index b2d898ee..c4fa51c2 100644 --- a/src/master/implementation.browser.ts +++ b/src/master/implementation.browser.ts @@ -34,7 +34,6 @@ 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 (typeof url === "string" && isAbsoluteURL(url)) { // Create source code blob loading JS file via `importScripts()` From 09a08187216fb0aca880aeacec3b2c87f97b68cb Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Aug 2020 00:46:37 -0500 Subject: [PATCH 2/8] Revert "remove incorrect url code" This reverts commit a0a40beb036d4ee7ed169c270f4f511f784df7c3. --- src/master/implementation.browser.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index c4fa51c2..b2d898ee 100644 --- a/src/master/implementation.browser.ts +++ b/src/master/implementation.browser.ts @@ -34,6 +34,7 @@ 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 (typeof url === "string" && isAbsoluteURL(url)) { // Create source code blob loading JS file via `importScripts()` From eeb90f1a147ff7d436cf3cf58270aec7d0fc677f Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Aug 2020 00:48:29 -0500 Subject: [PATCH 3/8] fix: add blob to the regex --- src/master/implementation.browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index b2d898ee..9393ce9b 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) => /^(blob:|file:|https?:)?\/\//i.test(value) function createSourceBlobURL(code: string): string { const blob = new Blob( From ea183b41732086bd328941b71fce6dd848a700f2 Mon Sep 17 00:00:00 2001 From: Andy Wermke Date: Mon, 3 Aug 2020 18:45:24 +0200 Subject: [PATCH 4/8] Fix isAbsoluteURL() regex --- src/master/implementation.browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index 9393ce9b..347c9f16 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) => /^(blob:|file:|https?:)?\/\//i.test(value) +const isAbsoluteURL = (value: string) => /^(file:|https?:)?\/\//i.test(value) function createSourceBlobURL(code: string): string { const blob = new Blob( From 2d46ccf10218d2c8528ffc2acdd29a933b8683b2 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Thu, 13 Aug 2020 23:05:00 -0500 Subject: [PATCH 5/8] fix: change isAbsoluteURL function --- src/master/implementation.browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index 347c9f16..a40ea7a7 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) => /^(file:|https?:)?\/\//i.test(value) || /^blob:/.test(value) function createSourceBlobURL(code: string): string { const blob = new Blob( From 7a84a253354afff807875f521e9c2305d77b114a Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 25 Aug 2020 08:59:14 -0500 Subject: [PATCH 6/8] use isAbsoluteURL from is-absolute-url https://github.com/sindresorhus/is-absolute-url/blob/8a57ccec064d925640860674cd32e7182eeb3596/index.js#L15 --- src/master/implementation.browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index a40ea7a7..e7344ba1 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) || /^blob:/.test(value) +const isAbsoluteURL = (value: string) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value) function createSourceBlobURL(code: string): string { const blob = new Blob( From 0c5812deb6c4ce50fa53b66693d0392cb9ae42a8 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 25 Aug 2020 09:11:12 -0500 Subject: [PATCH 7/8] option to skip CORSWorkaround --- src/master/implementation.browser.ts | 8 ++++++-- src/types/master.ts | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/master/implementation.browser.ts b/src/master/implementation.browser.ts index e7344ba1..87846b97 100644 --- a/src/master/implementation.browser.ts +++ b/src/master/implementation.browser.ts @@ -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..cd8299f3 100644 --- a/src/types/master.ts +++ b/src/types/master.ts @@ -83,6 +83,9 @@ export interface ThreadsWorkerOptions extends WorkerOptions { } /** Data passed on to Node worker_threads */ workerData?: any + + /** apply CORS protection workaround */ + CORSWorkaround?: boolean } /** Worker implementation. Either web worker or a node.js Worker class. */ From 0ee09d67de52e4558f485c99ebba4bd8ceccd34a Mon Sep 17 00:00:00 2001 From: Andy Wermke Date: Sun, 10 Jan 2021 21:58:14 +0100 Subject: [PATCH 8/8] Tiny docblock improvement --- src/types/master.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types/master.ts b/src/types/master.ts index cd8299f3..f3f72a7d 100644 --- a/src/types/master.ts +++ b/src/types/master.ts @@ -81,10 +81,10 @@ 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 - /** apply CORS protection workaround */ + /** Whether to apply CORS protection workaround. Defaults to true. */ CORSWorkaround?: boolean }