Web Workers throws a CSP error - could we disable them? #205
-
|
Hello, I'm using fflate in a new project Excel-Export-Vanilla which sometime throws an error but only when the file is big enough and is starting to use Web Workers. Googling the error, I see that I might to adjust the CSP (Content Security Policy) rules, maybe something like Basically, I'd like to know if there's a different approach to this error and/or if browser.js:12 Refused to create a worker from 'blob:http://localhost:8888/2667b3f9-5c8d-4510-9087-7b6cf1d24531'
because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline'".
Note that 'worker-src' was not explicitly set, so 'script-src' is used as a fallback.
...
browser.js:12 Uncaught (in promise) DOMException: Failed to construct 'Worker': Access to the script at
'blob:http://localhost:8888/2667b3f9-5c8d-4510-9087-7b6cf1d24531' is denied by the document's Content Security Policy.For confirmation, adding |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
To clarify,
I recommend just conditionally using import { zip, zipSync } from 'fflate';
const zipWorker = files => {
return new Promise((resolve, reject) => {
zip(files, (err, result) => err ? reject(err) : resolve(result));
});
}
const zipNoWorker = async files => {
return zipSync(files);
}
const zipFunc = canUseWorkers ? zipWorker : zipNoWorker;
async function main() {
const result = await zipFunc({ 'yourfile.bin': new Uint8Array(10) });
console.log(result); // your zip file
} |
Beta Was this translation helpful? Give feedback.
-
|
Hi @101arrowz, Thanks for clarification, I have the same error with |
Beta Was this translation helpful? Give feedback.
To clarify,
fflateoffers two ZIP functions:zipSyncandzip.zipSyncruns synchronously and without any Web Workers.zipruns asynchronously and uses Web Workers. There is no reason to runzipin an environment that does not support blob-based Web Workers; the only reason you'd use it instead ofzipSyncis to take advantage of multicore compression using worker threads. The way to disable Web Workers is simply to usezipSyncinstead.I recommend just conditionally using
ziporzipSyncbased on the user options. Having two implementations is not as convoluted as it see…