From d557383c9d048fbe1e4c2b8438b6c375c8f5ff63 Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Tue, 12 Dec 2023 17:18:42 -0800 Subject: [PATCH] For issue #44, remove the need for a pathToBitJS while unarchiving. --- archive/compress.js | 2 ++ archive/decompress-internal.js | 16 ++++++---------- archive/decompress.js | 12 +++++------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/archive/compress.js b/archive/compress.js index 5c1d08b..e98ef5c 100644 --- a/archive/compress.js +++ b/archive/compress.js @@ -130,6 +130,8 @@ export class Zipper { */ start(files, isLastFile) { return new Promise((resolve, reject) => { + // TODO: Only use Worker if it exists (like decompress). + // TODO: Remove need for pathToBitJS (like decompress). this.worker_ = new Worker(this.pathToBitJS + `archive/zip.js`); this.worker_.onerror = (evt) => { console.log('Worker error: message = ' + evt.message); diff --git a/archive/decompress-internal.js b/archive/decompress-internal.js index 299ab03..67a715f 100644 --- a/archive/decompress-internal.js +++ b/archive/decompress-internal.js @@ -9,6 +9,9 @@ * Copyright(c) 2021 Google Inc. */ +// TODO(bitjs): Consider moving all this back into decompress.js to simplify the code now that +// the implementation isn't tied to Web Workers so heavily. + import { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType, UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent, UnarchiveProgressEvent, UnarchiveStartEvent } from './events.js'; @@ -20,11 +23,12 @@ import { findMimeType } from '../file/sniffer.js'; * @property {Uint8Array} fileData */ +// TODO: Remove pathToBitJS completely from this. + /** * @typedef UnarchiverOptions * @property {string} pathToBitJS The path to the bitjs folder. * @property {boolean=} debug Set to true for verbose unarchiver logging. - * @property {ThreadingMode=} threadingMode The default is WEB_WORKER. */ /** @@ -72,13 +76,6 @@ export class Unarchiver extends EventTarget { */ this.connectPortFn_ = connectPortFn; - /** - * The path to the BitJS files. - * @type {string} - * @private - */ - this.pathToBitJS_ = options.pathToBitJS || '/'; - /** * @orivate * @type {boolean} @@ -160,8 +157,7 @@ export class Unarchiver extends EventTarget { const me = this; const messageChannel = new MessageChannel(); this.port_ = messageChannel.port1; - this.connectPortFn_(this.pathToBitJS_, - this.getScriptFileName(), messageChannel.port2).then(() => { + this.connectPortFn_(this.getScriptFileName(), messageChannel.port2).then(() => { this.port_.onerror = function (e) { console.log('Impl error: message = ' + e.message); throw e; diff --git a/archive/decompress.js b/archive/decompress.js index b4e07f2..99ec0d5 100644 --- a/archive/decompress.js +++ b/archive/decompress.js @@ -47,21 +47,19 @@ export { * (e.g. web browsers or deno), imports the implementation inside a Web Worker. Otherwise, it * dynamically imports the implementation inside the current JS context. * The MessagePort is used for communication between host and implementation. - * @param {string} pathToBitJS The path to the bitjs folder. * @param {string} implFilename The decompressor implementation filename relative to this path * (e.g. './unzip.js'). * @param {MessagePort} implPort The MessagePort to connect to the decompressor implementation. * @returns {Promise} The Promise resolves once the ports are connected. */ -const connectPortFn = async (pathToBitJS, implFilename, implPort) => { +const connectPortFn = async (implFilename, implPort) => { if (typeof Worker === 'undefined') { - return import(`${implFilename}`).then(implModule => { - implModule.connect(implPort) - }); + return import(`${implFilename}`).then(implModule => implModule.connect(implPort)); } - + return new Promise((resolve, reject) => { - const worker = new Worker(pathToBitJS + 'archive/unarchiver-webworker.js', { type: 'module' }); + const workerScriptPath = new URL(`./unarchiver-webworker.js`, import.meta.url).href; + const worker = new Worker(workerScriptPath, { type: 'module' }); worker.postMessage({ implSrc: implFilename }, [implPort]); resolve(); });