From 98ddbe95d9e51aa0c3f9777fe0d3ce2c6d703a55 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Mon, 4 Nov 2019 18:58:00 -0500 Subject: [PATCH] Bugfix worker script --- z-worker.js | 7 +++++-- zip.js | 10 +++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/z-worker.js b/z-worker.js index 3e4019e..878cc72 100644 --- a/z-worker.js +++ b/z-worker.js @@ -31,8 +31,11 @@ var tasks = {}; function doImportScripts(msg) { - if (msg.scripts && msg.scripts.length > 0) - importScripts.apply(undefined, msg.scripts); + if (msg.scripts) + msg.scripts.forEach(js => { + eval(js); + }); + // importScripts.apply(undefined, msg.scripts); postMessage({type: 'importScripts'}); } diff --git a/zip.js b/zip.js index 7bd14f3..9d2ce7b 100644 --- a/zip.js +++ b/zip.js @@ -875,7 +875,7 @@ const basePath = import.meta.url.replace(/[^\/]*$/, ''); deflater: [basePath + 'z-worker.js', basePath + 'deflate.js'], inflater: [basePath + 'z-worker.js', basePath + 'inflate.js'] }; - function createWorker(type, callback, onerror) { + async function createWorker(type, callback, onerror) { if (obj.zip.workerScripts !== null && obj.zip.workerScriptsPath !== null) { onerror(new Error('Either zip.workerScripts or zip.workerScriptsPath may be set, not both.')); return; @@ -892,10 +892,14 @@ const basePath = import.meta.url.replace(/[^\/]*$/, ''); scripts = DEFAULT_WORKER_SCRIPTS[type].slice(0); scripts[0] = (obj.zip.workerScriptsPath || '') + scripts[0]; } - var worker = new Worker(scripts[0]); + const _readScript = async (src, text) => { + const res = await fetch(src); + return await (text ? res.text() : res.blob()); + }; + var worker = new Worker(URL.createObjectURL(await _readScript(scripts[0], false))); // record total consumed time by inflater/deflater/crc32 in this worker worker.codecTime = worker.crcTime = 0; - worker.postMessage({ type: 'importScripts', scripts: scripts.slice(1) }); + worker.postMessage({ type: 'importScripts', scripts: await Promise.all(scripts.slice(1).map(src => _readScript(src, true))) }); worker.addEventListener('message', onmessage); function onmessage(ev) { var msg = ev.data;