Add CORSWorkaround option + update isAbsoluteURL#287
Conversation
|
The error that I got in #284 is now changed to this: worker.js:1 Uncaught ReferenceError: require is not defined
at worker.js:1
(anonymous) @ worker.js:1C:\Users\myproject\ide-json\node_modules\threads\dist\master\spawn.js:35 Uncaught (in promise) Error: Timeout: Did not receive an init message from worker after 10000ms. Make sure the worker calls expose().
at C:\Users\myproject\ide-json\node_modules\threads\dist\master\spawn.js:35I think now the worker is working properly, but the code is written in a Node fashion. Now probably it is a matter of testing this on another version of Atom. I noticed that Electron needs some options for enabling Node workers: Edit:I tested on my version of Atom that enables multi-threading and the errors are all gone! 🎉 |
|
Looking good, but we can't just drop the CORS fix like that. Can you try reverting this change here and instead change the regex to this? /^(blob:|file:|https?:)?\/\//i |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Yeah, we need that. But maybe this little change could already straighten things (in the WebWorker constructor)? - }
- if (typeof url === "string" && isAbsoluteURL(url)) {
+ } else if (typeof url === "string" && isAbsoluteURL(url)) {I don't have any time today, but maybe that helps already :) |
|
This does not fix the issue. The code does not enter this condition at all. The problem happens in the second branch of We need to have a better detection for when the CORS protection is needed. In this case, it is adversely affecting the code. class WebWorker extends Worker {
constructor(url, options) {
if (typeof url === "string" && options && options._baseURL) {
url = new URL(url, options._baseURL);
}
else if (typeof url === "string" && !isAbsoluteURL(url) && get_bundle_url_browser_1.getBundleURL().match(/^file:\/\//i)) {
console.log("comes here") //HERE
url = new URL(url, get_bundle_url_browser_1.getBundleURL().replace(/\/[^\/]+$/, "/"));
// do we need this?? this is the problem
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)});`);
}
super(url, options);
}
} |
|
Ahhh, you know what? I think we need to fix the - const isAbsoluteURL = (value: string) => /^(file|https?:)?\/\//i.test(value)
+ const isAbsoluteURL = (value: string) => /^(file:|https?:)?\/\//i.test(value) || /^blob:/.test(value)As the |
|
@aminya Any news? |
|
@andywer no difference: C:\Users\aminy\Docum…ation.browser.js:40 Refused to create a worker from 'blob:file:///6be44486-60a8-4fa6-9cf6-0b0137592712' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval'". Note that 'worker-src' was not explicitly set, so 'script-src' is used as a fallback.As I said the problem is this line: |
Sure, but it should not execute that line in the first place as the condition for that So weird. |
|
@andywer But the input Why not use a package like https://www.npmjs.com/package/is-absolute-url, which covers all the possibilities. |
|
Ahhh, that happens when you don't run the code yourself… Of course, you are right!
Could do, but I also try to find a balance where we don't rely on too many dependencies. So for these classic one-liners I try to avoid introducing additional deps. But yeah, maybe those one-liners should be based more closely on existing code 😉
What is your Content Security Policy, btw? We have been using this CSP in an Electron/Cordova app: |
So what is the solution now? Should we use a better is-absolute-url function?
Using tree-shaking and The benefit of these libraries is that they are well tested and they usually promise to do this simple functionality well!
Atom's CSP is this: |
|
Let's improve the function we have, so that it matches I take it you are working with Atom and cannot change the CSP? 🙈 So if the code works when this line of code is not run, then let's just add an option to opt-out of the CORS workaround. |
|
@andywer I added the option. Should it be documented anywhere? I don't know where to add the docs. |
This reverts commit a0a40be.
889ddbc to
0c5812d
Compare
|
This is ready to go too. |
|
Great stuff! 👍
Maybe, but as it's quite specific and hopefully not going to be needed too often, I think we can live with only documenting the code inline (also shown in IntelliSense) for now. |


Fixes #284
Closes #286