Skip to content

Commit

Permalink
For issue #44, remove the need for a pathToBitJS while unarchiving.
Browse files Browse the repository at this point in the history
  • Loading branch information
codedread committed Dec 13, 2023
1 parent f71c893 commit d557383
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 2 additions & 0 deletions archive/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 6 additions & 10 deletions archive/decompress-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*/

/**
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions archive/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>} 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();
});
Expand Down

0 comments on commit d557383

Please sign in to comment.