Add CORSWorkaround option + update isAbsoluteURL - #287
Conversation
The error that I got in #284 is now changed to this: worker.js:1Uncaught ReferenceError: requireisnotdefinedatworker.js:1(anonymous) @ worker.js:1C:\Users\myproject\ide-json\node_modules\threads\dist\master\spawn.js:35Uncaught(inpromise) Error: Timeout: Didnotreceiveaninitmessagefromworkerafter10000ms.Makesuretheworkercallsexpose().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! 🎉 |
andywer
commented
Aug 4, 2020
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.
aminya
commented
Aug 9, 2020
andywer
commented
Aug 9, 2020
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. classWebWorkerextendsWorker{constructor(url,options){if(typeofurl==="string"&&options&&options._baseURL){url=newURL(url,options._baseURL);}elseif(typeofurl==="string"&&!isAbsoluteURL(url)&&get_bundle_url_browser_1.getBundleURL().match(/^file:\/\//i)){console.log("comes here")//HEREurl=newURL(url,get_bundle_url_browser_1.getBundleURL().replace(/\/[^\/]+$/,"/"));// do we need this?? this is the problemurl=createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);}if(typeofurl==="string"&&isAbsoluteURL(url)){// Create source code blob loading JS file via `importScripts()`// to circumvent worker CORS restrictionsurl=createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);}super(url,options);}} |
andywer
commented
Aug 10, 2020
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 |
andywer
commented
Aug 13, 2020
@aminya Any news? |
@andywer no difference: C:\Users\aminy\Docum…ation.browser.js:40Refusedtocreateaworkerfrom'blob:file:///6be44486-60a8-4fa6-9cf6-0b0137592712'becauseitviolatesthefollowingContentSecurityPolicydirective: "script-src 'self' 'unsafe-eval'".Notethat'worker-src'wasnotexplicitlyset,so'script-src'isusedasafallback.As I said the problem is this line: |
andywer
commented
Aug 14, 2020
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. |
andywer
commented
Aug 14, 2020
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: |
andywer
commented
Aug 15, 2020
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. |
aminya
commented
Aug 25, 2020
@andywer I added the option. Should it be documented anywhere? I don't know where to add the docs. |
This reverts commit a0a40be.
aminya
commented
Jan 10, 2021
This is ready to go too. |
andywer
commented
Jan 10, 2021
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. |
* Fix isAbsoluteURL() regex * Option to skip CORS workaround


Fixes#284
Closes#286