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: 1 comment 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.
To clarify,
fflate
offers two ZIP functions:zipSync
andzip
.zipSync
runs synchronously and without any Web Workers.zip
runs asynchronously and uses Web Workers. There is no reason to runzip
in an environment that does not support blob-based Web Workers; the only reason you'd use it instead ofzipSync
is to take advantage of multicore compression using worker threads. The way to disable Web Workers is simply to usezipSync
instead.I recommend just conditionally using
zip
orzipSync
based on the user options. Having two implementations is not as convoluted as it see…