1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-05 10:29:24 +02:00

Rename DecompressorOptions to UnarchiverOptions. Add a ThreadingMode enum (only WebWorkers for now). Remove need to keep a handle to the decompressor impl from Unarchiver.

This commit is contained in:
Jeff Schiller 2023-12-11 08:19:53 -08:00
parent ac6807edec
commit 8e1f8b139a
2 changed files with 19 additions and 27 deletions

View file

@ -18,9 +18,17 @@ import { findMimeType } from '../file/sniffer.js';
*/ */
/** /**
* @typedef DecompressorOptions * An enum for threading mode. Currently supporting only WebWorkers.
*/
export const ThreadingMode = {
WEB_WORKER: 'WEB_WORKER',
}
/**
* @typedef UnarchiverOptions
* @property {string} pathToBitJS The path to the bitjs folder. * @property {string} pathToBitJS The path to the bitjs folder.
* @property {boolean=} debug Set to true for verbose decompressor logging. * @property {boolean=} debug Set to true for verbose unarchiver logging.
* @property {ThreadingMode=} threadingMode The default is WEB_WORKER.
*/ */
/** /**
@ -172,14 +180,7 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
/** /**
* Base class for all Unarchivers. * Base class for all Unarchivers.
*/ */
export class Unarchiver extends EventTarget { export class Unarchiver extends EventTarget {
/**
* A handle to the decompressor implementation context.
* @type {Worker|*}
* @private
*/
implRef_;
/** /**
* The client-side port that sends messages to, and receives messages from the * The client-side port that sends messages to, and receives messages from the
* decompressor implementation. * decompressor implementation.
@ -194,7 +195,7 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
* to the decompress implementation. * to the decompress implementation.
* @param {Function(string, MessagePort):Promise<*>} connectPortFn A function that takes a path * @param {Function(string, MessagePort):Promise<*>} connectPortFn A function that takes a path
* to a JS decompression implementation (unzip.js) and connects it to a MessagePort. * to a JS decompression implementation (unzip.js) and connects it to a MessagePort.
* @param {DecompressorOptions|string} options An optional object of options, or a string * @param {UnarchiverOptions|string} options An optional object of options, or a string
* representing where the BitJS files are located. The string version of this argument is * representing where the BitJS files are located. The string version of this argument is
* deprecated. * deprecated.
*/ */
@ -203,7 +204,7 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
if (typeof options === 'string') { if (typeof options === 'string') {
console.warn(`Deprecated: Don't send a raw string to Unarchiver()`); console.warn(`Deprecated: Don't send a raw string to Unarchiver()`);
console.warn(` send DecompressorOptions instead.`); console.warn(` send UnarchiverOptions instead.`);
options = { 'pathToBitJS': options }; options = { 'pathToBitJS': options };
} }
@ -310,10 +311,7 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
const messageChannel = new MessageChannel(); const messageChannel = new MessageChannel();
this.port_ = messageChannel.port1; this.port_ = messageChannel.port1;
this.connectPortFn_(this.pathToBitJS_, this.connectPortFn_(this.pathToBitJS_,
this.getScriptFileName(), this.getScriptFileName(), messageChannel.port2).then(() => {
messageChannel.port2).then((implRef) => {
this.implRef_ = implRef;
this.port_.onerror = function (e) { this.port_.onerror = function (e) {
console.log('Impl error: message = ' + e.message); console.log('Impl error: message = ' + e.message);
throw e; throw e;
@ -368,12 +366,6 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
this.port_.close(); this.port_.close();
this.port_ = null; this.port_ = null;
} }
if (this.implRef_) {
if (this.implRef_ instanceof Worker) {
this.implRef_.terminate();
this.implRef_ = null;
}
}
} }
} }

View file

@ -39,15 +39,15 @@ export {
*/ */
/** /**
* @typedef {import('./decompress-internal.js').DecompressorOptions} DecompressorOptions * @typedef {import('./decompress-internal.js').UnarchiverOptions} UnarchiverOptions
*/ */
/** /**
* Creates a WebWorker with the given decompressor implementation (i.e. unzip.js) * Creates a WebWorker with the given decompressor implementation (e.g. unzip.js)
* and transfers a MessagePort for communication. Returns a Promise to the Worker. * and transfers a MessagePort for communication. Returns a Promise to the Worker.
* @param {string} pathToBitJS The path to the bitjs folder. * @param {string} pathToBitJS The path to the bitjs folder.
* @param {string} implFilename The decompressor implementation filename * @param {string} implFilename The decompressor implementation filename
* relative to the bitjs root (e.g. archive/unzip.js) * relative to this path (e.g. './unzip.js').
* @param {MessagePort} implPort The MessagePort to connect to the decompressor * @param {MessagePort} implPort The MessagePort to connect to the decompressor
* implementation. * implementation.
* @returns {Promise<*>} Returns a Promise that resolves to the Worker object. * @returns {Promise<*>} Returns a Promise that resolves to the Worker object.
@ -83,9 +83,9 @@ export class Untarrer extends UntarrerInternal {
* @param {ArrayBuffer} ab The ArrayBuffer to unarchive. Note that this ArrayBuffer * @param {ArrayBuffer} ab The ArrayBuffer to unarchive. Note that this ArrayBuffer
* must not be referenced after calling this method, as the ArrayBuffer may be * must not be referenced after calling this method, as the ArrayBuffer may be
* transferred to a different JS context once start() is called. * transferred to a different JS context once start() is called.
* @param {DecompressorOptions|string} options An optional object of options, or a * @param {UnarchiverOptions|string} options An optional object of options, or a
* string representing where the path to the unarchiver script files. The latter * string representing where the path to the unarchiver script files. The latter
* is now deprecated (use DecompressorOptions). * is now deprecated (use UnarchiverOptions).
* @returns {Unarchiver} * @returns {Unarchiver}
*/ */
export function getUnarchiver(ab, options = {}) { export function getUnarchiver(ab, options = {}) {