mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Remove decompress-internal.js and pathToBitJS for decompress.
This commit is contained in:
parent
d557383c9d
commit
eeb228a52b
9 changed files with 369 additions and 482 deletions
|
@ -100,7 +100,7 @@ unzipper.update(anArrayBufferWithYetMoreBytes);
|
|||
|
||||
const nodeBuffer = fs.readFileSync('comic.cbz');
|
||||
const ab = nodeBuffer.buffer.slice(nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.length);
|
||||
const unarchiver = getUnarchiver(ab, { pathToBitJS: './' });
|
||||
const unarchiver = getUnarchiver(ab);
|
||||
unarchiver.addEventListener('progress', () => process.stdout.write('.'));
|
||||
unarchiver.addEventListener('extract', (evt) => {
|
||||
const extractedFile = evt.unarchivedFile;
|
||||
|
|
|
@ -14,8 +14,7 @@
|
|||
import { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType,
|
||||
UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent,
|
||||
UnarchiveProgressEvent, UnarchiveStartEvent } from './events.js';
|
||||
import { Unarchiver } from './decompress-internal.js';
|
||||
import { Unzipper, Unrarrer, Untarrer, getUnarchiver } from './decompress.js';
|
||||
import { Unarchiver, Unzipper, Unrarrer, Untarrer, getUnarchiver } from './decompress.js';
|
||||
|
||||
export {
|
||||
UnarchiveAppendEvent,
|
||||
|
|
|
@ -1,284 +0,0 @@
|
|||
/**
|
||||
* decompress-internal.js
|
||||
*
|
||||
* Provides base functionality for unarchiving, extracted here as an internal
|
||||
* module for unit testing. Import decompress.js instead.
|
||||
*
|
||||
* Licensed under the MIT License
|
||||
*
|
||||
* 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';
|
||||
import { findMimeType } from '../file/sniffer.js';
|
||||
|
||||
/**
|
||||
* @typedef UnarchivedFile
|
||||
* @property {string} filename
|
||||
* @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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base class for all Unarchivers.
|
||||
*/
|
||||
export class Unarchiver extends EventTarget {
|
||||
/**
|
||||
* The client-side port that sends messages to, and receives messages from the
|
||||
* decompressor implementation.
|
||||
* @type {MessagePort}
|
||||
* @private
|
||||
*/
|
||||
port_;
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be
|
||||
* referenced once it is sent to the Unarchiver, since it is marked as Transferable and sent
|
||||
* to the decompress implementation.
|
||||
* @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.
|
||||
* @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
|
||||
* deprecated.
|
||||
*/
|
||||
constructor(arrayBuffer, connectPortFn, options = {}) {
|
||||
super();
|
||||
|
||||
if (typeof options === 'string') {
|
||||
console.warn(`Deprecated: Don't send a raw string to Unarchiver()`);
|
||||
console.warn(` send UnarchiverOptions instead.`);
|
||||
options = { 'pathToBitJS': options };
|
||||
}
|
||||
|
||||
/**
|
||||
* The ArrayBuffer object.
|
||||
* @type {ArrayBuffer}
|
||||
* @protected
|
||||
*/
|
||||
this.ab = arrayBuffer;
|
||||
|
||||
/**
|
||||
* A factory method that connects a port to the decompress implementation.
|
||||
* @type {Function(MessagePort): Promise<*>}
|
||||
* @private
|
||||
*/
|
||||
this.connectPortFn_ = connectPortFn;
|
||||
|
||||
/**
|
||||
* @orivate
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.debugMode_ = !!(options.debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The MIME type of the archive.
|
||||
* @protected.
|
||||
*/
|
||||
getMIMEType() {
|
||||
throw 'Subclasses of Unarchiver must overload getMIMEType()';
|
||||
}
|
||||
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The script filename.
|
||||
* @protected.
|
||||
*/
|
||||
getScriptFileName() {
|
||||
throw 'Subclasses of Unarchiver must overload getScriptFileName()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an UnarchiveEvent out of the object sent back from the implementation.
|
||||
* @param {Object} obj
|
||||
* @returns {UnarchiveEvent}
|
||||
* @private
|
||||
*/
|
||||
createUnarchiveEvent_(obj) {
|
||||
switch (obj.type) {
|
||||
case UnarchiveEventType.START:
|
||||
return new UnarchiveStartEvent();
|
||||
case UnarchiveEventType.PROGRESS:
|
||||
return new UnarchiveProgressEvent(
|
||||
obj.currentFilename,
|
||||
obj.currentFileNumber,
|
||||
obj.currentBytesUnarchivedInFile,
|
||||
obj.currentBytesUnarchived,
|
||||
obj.totalUncompressedBytesInArchive,
|
||||
obj.totalFilesInArchive,
|
||||
obj.totalCompressedBytesRead);
|
||||
case UnarchiveEventType.EXTRACT:
|
||||
return new UnarchiveExtractEvent(obj.unarchivedFile);
|
||||
case UnarchiveEventType.FINISH:
|
||||
return new UnarchiveFinishEvent(obj.metadata);
|
||||
case UnarchiveEventType.INFO:
|
||||
return new UnarchiveInfoEvent(obj.msg);
|
||||
case UnarchiveEventType.ERROR:
|
||||
return new UnarchiveErrorEvent(obj.msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive an event and pass it to the listener functions.
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @private
|
||||
*/
|
||||
handlePortEvent_(obj) {
|
||||
const type = obj.type;
|
||||
if (type && Object.values(UnarchiveEventType).includes(type)) {
|
||||
const evt = this.createUnarchiveEvent_(obj);
|
||||
this.dispatchEvent(evt);
|
||||
if (evt.type == UnarchiveEventType.FINISH) {
|
||||
this.stop();
|
||||
}
|
||||
} else {
|
||||
console.log(`Unknown object received from port: ${obj}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the unarchive by connecting the ports and sending the first ArrayBuffer.
|
||||
*/
|
||||
start() {
|
||||
const me = this;
|
||||
const messageChannel = new MessageChannel();
|
||||
this.port_ = messageChannel.port1;
|
||||
this.connectPortFn_(this.getScriptFileName(), messageChannel.port2).then(() => {
|
||||
this.port_.onerror = function (e) {
|
||||
console.log('Impl error: message = ' + e.message);
|
||||
throw e;
|
||||
};
|
||||
|
||||
this.port_.onmessage = function (e) {
|
||||
if (typeof e.data == 'string') {
|
||||
// Just log any strings the port pumps our way.
|
||||
console.log(e.data);
|
||||
} else {
|
||||
me.handlePortEvent_(e.data);
|
||||
}
|
||||
};
|
||||
|
||||
const ab = this.ab;
|
||||
this.port_.postMessage({
|
||||
file: ab,
|
||||
logToConsole: this.debugMode_,
|
||||
}, [ab]);
|
||||
this.ab = null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds more bytes to the unarchiver.
|
||||
* @param {ArrayBuffer} ab The ArrayBuffer with more bytes in it. If opt_transferable is
|
||||
* set to true, this ArrayBuffer must not be referenced after calling update(), since it
|
||||
* is marked as Transferable and sent to the implementation.
|
||||
* @param {boolean=} opt_transferable Optional boolean whether to mark this ArrayBuffer
|
||||
* as a Tranferable object, which means it can no longer be referenced outside of
|
||||
* the implementation context.
|
||||
*/
|
||||
update(ab, opt_transferable = false) {
|
||||
const numBytes = ab.byteLength;
|
||||
if (this.port_) {
|
||||
// Send the ArrayBuffer over, and mark it as a Transferable object if necessary.
|
||||
if (opt_transferable) {
|
||||
this.port_.postMessage({ bytes: ab }, [ab]);
|
||||
} else {
|
||||
this.port_.postMessage({ bytes: ab });
|
||||
}
|
||||
}
|
||||
|
||||
this.dispatchEvent(new UnarchiveAppendEvent(numBytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the port to the decompressor implementation and terminates it.
|
||||
*/
|
||||
stop() {
|
||||
if (this.port_) {
|
||||
this.port_.close();
|
||||
this.port_ = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class UnzipperInternal extends Unarchiver {
|
||||
constructor(arrayBuffer, connectPortFn, options) {
|
||||
super(arrayBuffer, connectPortFn, options);
|
||||
}
|
||||
|
||||
getMIMEType() { return 'application/zip'; }
|
||||
getScriptFileName() { return './unzip.js'; }
|
||||
}
|
||||
|
||||
export class UnrarrerInternal extends Unarchiver {
|
||||
constructor(arrayBuffer, connectPortFn, options) {
|
||||
super(arrayBuffer, connectPortFn, options);
|
||||
}
|
||||
|
||||
getMIMEType() { return 'application/x-rar-compressed'; }
|
||||
getScriptFileName() { return './unrar.js'; }
|
||||
}
|
||||
|
||||
export class UntarrerInternal extends Unarchiver {
|
||||
constructor(arrayBuffer, connectPortFn, options) {
|
||||
super(arrayBuffer, connectPortFn, options);
|
||||
}
|
||||
|
||||
getMIMEType() { return 'application/x-tar'; }
|
||||
getScriptFileName() { return './untar.js'; };
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method that creates an unarchiver based on the byte signature found
|
||||
* in the arrayBuffer.
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {Function(string):Promise<*>} connectPortFn A function that connects the impl port.
|
||||
* @param {Object|string} options An optional object of options, or a string representing where
|
||||
* the path to the unarchiver script files.
|
||||
* @returns {Unarchiver}
|
||||
*/
|
||||
export function getUnarchiverInternal(ab, connectPortFn, options = {}) {
|
||||
if (ab.byteLength < 10) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let unarchiver = null;
|
||||
const mimeType = findMimeType(ab);
|
||||
|
||||
if (mimeType === 'application/x-rar-compressed') { // Rar!
|
||||
unarchiver = new UnrarrerInternal(ab, connectPortFn, options);
|
||||
} else if (mimeType === 'application/zip') { // PK (Zip)
|
||||
unarchiver = new UnzipperInternal(ab, connectPortFn, options);
|
||||
} else { // Try with tar
|
||||
unarchiver = new UntarrerInternal(ab, connectPortFn, options);
|
||||
}
|
||||
return unarchiver;
|
||||
}
|
||||
|
||||
// Re-export things that used to live here.
|
||||
// TODO: When up-revving to a major new version, remove these exports?
|
||||
export {
|
||||
UnarchiveAppendEvent,
|
||||
UnarchiveErrorEvent,
|
||||
UnarchiveEvent,
|
||||
UnarchiveEventType,
|
||||
UnarchiveExtractEvent,
|
||||
UnarchiveFinishEvent,
|
||||
UnarchiveInfoEvent,
|
||||
UnarchiveProgressEvent,
|
||||
UnarchiveStartEvent,
|
||||
};
|
|
@ -11,8 +11,7 @@
|
|||
import { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType,
|
||||
UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent,
|
||||
UnarchiveProgressEvent, UnarchiveStartEvent } from './events.js';
|
||||
import { Unarchiver, UnrarrerInternal, UntarrerInternal, UnzipperInternal,
|
||||
getUnarchiverInternal } from './decompress-internal.js';
|
||||
import { findMimeType } from '../file/sniffer.js';
|
||||
|
||||
export {
|
||||
UnarchiveAppendEvent,
|
||||
|
@ -24,7 +23,6 @@ export {
|
|||
UnarchiveInfoEvent,
|
||||
UnarchiveProgressEvent,
|
||||
UnarchiveStartEvent,
|
||||
Unarchiver,
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +37,8 @@ export {
|
|||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('./decompress-internal.js').UnarchiverOptions} UnarchiverOptions
|
||||
* @typedef UnarchiverOptions
|
||||
* @property {boolean=} debug Set to true for verbose unarchiver logging.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -65,31 +64,256 @@ const connectPortFn = async (implFilename, implPort) => {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Base class for all Unarchivers.
|
||||
*/
|
||||
export class Unarchiver extends EventTarget {
|
||||
/**
|
||||
* The client-side port that sends messages to, and receives messages from the
|
||||
* decompressor implementation.
|
||||
* @type {MessagePort}
|
||||
* @private
|
||||
*/
|
||||
port_;
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be
|
||||
* referenced once it is sent to the Unarchiver, since it is marked as Transferable and sent
|
||||
* to the decompress implementation.
|
||||
* @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.
|
||||
* @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
|
||||
* deprecated.
|
||||
*/
|
||||
constructor(arrayBuffer, connectPortFn, options = {}) {
|
||||
super();
|
||||
|
||||
if (typeof options === 'string') {
|
||||
console.warn(`Deprecated: Don't send a raw string to Unarchiver()`);
|
||||
console.warn(` send UnarchiverOptions instead.`);
|
||||
options = { };
|
||||
}
|
||||
|
||||
/**
|
||||
* The ArrayBuffer object.
|
||||
* @type {ArrayBuffer}
|
||||
* @protected
|
||||
*/
|
||||
this.ab = arrayBuffer;
|
||||
|
||||
/**
|
||||
* A factory method that connects a port to the decompress implementation.
|
||||
* @type {Function(MessagePort): Promise<*>}
|
||||
* @private
|
||||
*/
|
||||
this.connectPortFn_ = connectPortFn;
|
||||
|
||||
/**
|
||||
* @orivate
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.debugMode_ = !!(options.debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The MIME type of the archive.
|
||||
* @protected.
|
||||
*/
|
||||
getMIMEType() {
|
||||
throw 'Subclasses of Unarchiver must overload getMIMEType()';
|
||||
}
|
||||
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The script filename.
|
||||
* @protected.
|
||||
*/
|
||||
getScriptFileName() {
|
||||
throw 'Subclasses of Unarchiver must overload getScriptFileName()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an UnarchiveEvent out of the object sent back from the implementation.
|
||||
* @param {Object} obj
|
||||
* @returns {UnarchiveEvent}
|
||||
* @private
|
||||
*/
|
||||
createUnarchiveEvent_(obj) {
|
||||
switch (obj.type) {
|
||||
case UnarchiveEventType.START:
|
||||
return new UnarchiveStartEvent();
|
||||
case UnarchiveEventType.PROGRESS:
|
||||
return new UnarchiveProgressEvent(
|
||||
obj.currentFilename,
|
||||
obj.currentFileNumber,
|
||||
obj.currentBytesUnarchivedInFile,
|
||||
obj.currentBytesUnarchived,
|
||||
obj.totalUncompressedBytesInArchive,
|
||||
obj.totalFilesInArchive,
|
||||
obj.totalCompressedBytesRead);
|
||||
case UnarchiveEventType.EXTRACT:
|
||||
return new UnarchiveExtractEvent(obj.unarchivedFile);
|
||||
case UnarchiveEventType.FINISH:
|
||||
return new UnarchiveFinishEvent(obj.metadata);
|
||||
case UnarchiveEventType.INFO:
|
||||
return new UnarchiveInfoEvent(obj.msg);
|
||||
case UnarchiveEventType.ERROR:
|
||||
return new UnarchiveErrorEvent(obj.msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive an event and pass it to the listener functions.
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @private
|
||||
*/
|
||||
handlePortEvent_(obj) {
|
||||
const type = obj.type;
|
||||
if (type && Object.values(UnarchiveEventType).includes(type)) {
|
||||
const evt = this.createUnarchiveEvent_(obj);
|
||||
this.dispatchEvent(evt);
|
||||
if (evt.type == UnarchiveEventType.FINISH) {
|
||||
this.stop();
|
||||
}
|
||||
} else {
|
||||
console.log(`Unknown object received from port: ${obj}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the unarchive by connecting the ports and sending the first ArrayBuffer.
|
||||
*/
|
||||
start() {
|
||||
const me = this;
|
||||
const messageChannel = new MessageChannel();
|
||||
this.port_ = messageChannel.port1;
|
||||
this.connectPortFn_(this.getScriptFileName(), messageChannel.port2).then(() => {
|
||||
this.port_.onerror = function (e) {
|
||||
console.log('Impl error: message = ' + e.message);
|
||||
throw e;
|
||||
};
|
||||
|
||||
this.port_.onmessage = function (e) {
|
||||
if (typeof e.data == 'string') {
|
||||
// Just log any strings the port pumps our way.
|
||||
console.log(e.data);
|
||||
} else {
|
||||
me.handlePortEvent_(e.data);
|
||||
}
|
||||
};
|
||||
|
||||
const ab = this.ab;
|
||||
this.port_.postMessage({
|
||||
file: ab,
|
||||
logToConsole: this.debugMode_,
|
||||
}, [ab]);
|
||||
this.ab = null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds more bytes to the unarchiver.
|
||||
* @param {ArrayBuffer} ab The ArrayBuffer with more bytes in it. If opt_transferable is
|
||||
* set to true, this ArrayBuffer must not be referenced after calling update(), since it
|
||||
* is marked as Transferable and sent to the implementation.
|
||||
* @param {boolean=} opt_transferable Optional boolean whether to mark this ArrayBuffer
|
||||
* as a Tranferable object, which means it can no longer be referenced outside of
|
||||
* the implementation context.
|
||||
*/
|
||||
update(ab, opt_transferable = false) {
|
||||
const numBytes = ab.byteLength;
|
||||
if (this.port_) {
|
||||
// Send the ArrayBuffer over, and mark it as a Transferable object if necessary.
|
||||
if (opt_transferable) {
|
||||
this.port_.postMessage({ bytes: ab }, [ab]);
|
||||
} else {
|
||||
this.port_.postMessage({ bytes: ab });
|
||||
}
|
||||
}
|
||||
|
||||
this.dispatchEvent(new UnarchiveAppendEvent(numBytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the port to the decompressor implementation and terminates it.
|
||||
*/
|
||||
stop() {
|
||||
if (this.port_) {
|
||||
this.port_.close();
|
||||
this.port_ = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Thin wrappers of decompressors for clients who want to construct a specific
|
||||
// unarchiver themselves rather than use getUnarchiver().
|
||||
export class Unzipper extends UnzipperInternal {
|
||||
constructor(ab, options) { super(ab, connectPortFn, options); }
|
||||
export class Unzipper extends Unarchiver {
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {UnarchiverOptions} options
|
||||
*/
|
||||
constructor(ab, options = {}) {
|
||||
super(ab, connectPortFn, options);
|
||||
}
|
||||
|
||||
getMIMEType() { return 'application/zip'; }
|
||||
getScriptFileName() { return './unzip.js'; }
|
||||
}
|
||||
|
||||
export class Unrarrer extends UnrarrerInternal {
|
||||
constructor(ab, options) { super(ab, connectPortFn, options); }
|
||||
export class Unrarrer extends Unarchiver {
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {UnarchiverOptions} options
|
||||
*/
|
||||
constructor(ab, options = {}) {
|
||||
super(ab, connectPortFn, options);
|
||||
}
|
||||
|
||||
getMIMEType() { return 'application/x-rar-compressed'; }
|
||||
getScriptFileName() { return './unrar.js'; }
|
||||
}
|
||||
|
||||
export class Untarrer extends UntarrerInternal {
|
||||
constructor(ab, options) { super(ab, connectPortFn, options); }
|
||||
export class Untarrer extends Unarchiver {
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {UnarchiverOptions} options
|
||||
*/
|
||||
constructor(ab, options = {}) {
|
||||
super(ab, connectPortFn, options);
|
||||
}
|
||||
|
||||
getMIMEType() { return 'application/x-tar'; }
|
||||
getScriptFileName() { return './untar.js'; };
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method that creates an unarchiver based on the byte signature found
|
||||
* in the 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
|
||||
* transferred to a different JS context once start() is called.
|
||||
* @param {UnarchiverOptions|string} options An optional object of options, or a
|
||||
* string representing where the path to the unarchiver script files. The latter
|
||||
* is now deprecated (use UnarchiverOptions).
|
||||
* @returns {Unarchiver}
|
||||
*/
|
||||
* Factory method that creates an unarchiver based on the byte signature found
|
||||
* in the 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
|
||||
* transferred to a different JS context once start() is called.
|
||||
* @param {UnarchiverOptions|string} options An optional object of options, or a
|
||||
* string representing where the path to the unarchiver script files. The latter
|
||||
* is now deprecated (use UnarchiverOptions).
|
||||
* @returns {Unarchiver}
|
||||
*/
|
||||
export function getUnarchiver(ab, options = {}) {
|
||||
return getUnarchiverInternal(ab, connectPortFn, options);
|
||||
if (ab.byteLength < 10) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let unarchiver = null;
|
||||
const mimeType = findMimeType(ab);
|
||||
|
||||
if (mimeType === 'application/x-rar-compressed') { // Rar!
|
||||
unarchiver = new Unrarrer(ab, connectPortFn, options);
|
||||
} else if (mimeType === 'application/zip') { // PK (Zip)
|
||||
unarchiver = new Unzipper(ab, connectPortFn, options);
|
||||
} else { // Try with tar
|
||||
unarchiver = new Untarrer(ab, connectPortFn, options);
|
||||
}
|
||||
return unarchiver;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,8 @@
|
|||
/**
|
||||
* A WebWorker wrapper for a decompress implementation. Upon creation and being
|
||||
* sent its first message, it dynamically loads the correct decompressor and
|
||||
* connects the message port
|
||||
* connects the message port. All other communication takes place over the
|
||||
* MessageChannel.
|
||||
*/
|
||||
|
||||
/** @type {MessagePort} */
|
||||
|
|
149
types/archive/decompress-internal.d.ts
vendored
149
types/archive/decompress-internal.d.ts
vendored
|
@ -1,149 +0,0 @@
|
|||
/**
|
||||
* Factory method that creates an unarchiver based on the byte signature found
|
||||
* in the arrayBuffer.
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {Function(string):Promise<*>} connectPortFn A function that connects the impl port.
|
||||
* @param {Object|string} options An optional object of options, or a string representing where
|
||||
* the path to the unarchiver script files.
|
||||
* @returns {Unarchiver}
|
||||
*/
|
||||
export function getUnarchiverInternal(ab: ArrayBuffer, connectPortFn: any, options?: any | string): Unarchiver;
|
||||
export namespace ThreadingMode {
|
||||
const WEB_WORKER: string;
|
||||
}
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
/**
|
||||
* Base class for all Unarchivers.
|
||||
*/
|
||||
export class Unarchiver extends EventTarget {
|
||||
/**
|
||||
* @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be
|
||||
* referenced once it is sent to the Unarchiver, since it is marked as Transferable and sent
|
||||
* to the decompress implementation.
|
||||
* @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.
|
||||
* @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
|
||||
* deprecated.
|
||||
*/
|
||||
constructor(arrayBuffer: ArrayBuffer, connectPortFn: any, options?: UnarchiverOptions | string);
|
||||
/**
|
||||
* The client-side port that sends messages to, and receives messages from the
|
||||
* decompressor implementation.
|
||||
* @type {MessagePort}
|
||||
* @private
|
||||
*/
|
||||
private port_;
|
||||
/**
|
||||
* The ArrayBuffer object.
|
||||
* @type {ArrayBuffer}
|
||||
* @protected
|
||||
*/
|
||||
protected ab: ArrayBuffer;
|
||||
/**
|
||||
* A factory method that connects a port to the decompress implementation.
|
||||
* @type {Function(MessagePort): Promise<*>}
|
||||
* @private
|
||||
*/
|
||||
private connectPortFn_;
|
||||
/**
|
||||
* The path to the BitJS files.
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
private pathToBitJS_;
|
||||
/**
|
||||
* @orivate
|
||||
* @type {boolean}
|
||||
*/
|
||||
debugMode_: boolean;
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The MIME type of the archive.
|
||||
* @protected.
|
||||
*/
|
||||
protected getMIMEType(): string;
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The script filename.
|
||||
* @protected.
|
||||
*/
|
||||
protected getScriptFileName(): string;
|
||||
/**
|
||||
* Create an UnarchiveEvent out of the object sent back from the implementation.
|
||||
* @param {Object} obj
|
||||
* @returns {UnarchiveEvent}
|
||||
* @private
|
||||
*/
|
||||
private createUnarchiveEvent_;
|
||||
/**
|
||||
* Receive an event and pass it to the listener functions.
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @private
|
||||
*/
|
||||
private handlePortEvent_;
|
||||
/**
|
||||
* Starts the unarchive by connecting the ports and sending the first ArrayBuffer.
|
||||
*/
|
||||
start(): void;
|
||||
/**
|
||||
* Adds more bytes to the unarchiver.
|
||||
* @param {ArrayBuffer} ab The ArrayBuffer with more bytes in it. If opt_transferable is
|
||||
* set to true, this ArrayBuffer must not be referenced after calling update(), since it
|
||||
* is marked as Transferable and sent to the implementation.
|
||||
* @param {boolean=} opt_transferable Optional boolean whether to mark this ArrayBuffer
|
||||
* as a Tranferable object, which means it can no longer be referenced outside of
|
||||
* the implementation context.
|
||||
*/
|
||||
update(ab: ArrayBuffer, opt_transferable?: boolean | undefined): void;
|
||||
/**
|
||||
* Closes the port to the decompressor implementation and terminates it.
|
||||
*/
|
||||
stop(): void;
|
||||
}
|
||||
export class UnzipperInternal extends Unarchiver {
|
||||
constructor(arrayBuffer: any, connectPortFn: any, options: any);
|
||||
}
|
||||
export class UnrarrerInternal extends Unarchiver {
|
||||
constructor(arrayBuffer: any, connectPortFn: any, options: any);
|
||||
}
|
||||
export class UntarrerInternal extends Unarchiver {
|
||||
constructor(arrayBuffer: any, connectPortFn: any, options: any);
|
||||
}
|
||||
export type UnarchivedFile = {
|
||||
filename: string;
|
||||
fileData: Uint8Array;
|
||||
};
|
||||
export type UnarchiverOptions = {
|
||||
/**
|
||||
* The path to the bitjs folder.
|
||||
*/
|
||||
pathToBitJS: string;
|
||||
/**
|
||||
* Set to true for verbose unarchiver logging.
|
||||
*/
|
||||
debug?: boolean | undefined;
|
||||
/**
|
||||
* The default is WEB_WORKER.
|
||||
*/
|
||||
threadingMode?: {
|
||||
WEB_WORKER: string;
|
||||
} | undefined;
|
||||
};
|
||||
import { UnarchiveAppendEvent } from "./events.js";
|
||||
import { UnarchiveErrorEvent } from "./events.js";
|
||||
import { UnarchiveEvent } from "./events.js";
|
||||
import { UnarchiveEventType } from "./events.js";
|
||||
import { UnarchiveExtractEvent } from "./events.js";
|
||||
import { UnarchiveFinishEvent } from "./events.js";
|
||||
import { UnarchiveInfoEvent } from "./events.js";
|
||||
import { UnarchiveProgressEvent } from "./events.js";
|
||||
import { UnarchiveStartEvent } from "./events.js";
|
||||
export { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType, UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent, UnarchiveProgressEvent, UnarchiveStartEvent };
|
||||
//# sourceMappingURL=decompress-internal.d.ts.map
|
|
@ -1 +0,0 @@
|
|||
{"version":3,"file":"decompress-internal.d.ts","sourceRoot":"","sources":["../../archive/decompress-internal.js"],"names":[],"mappings":"AA+PA;;;;;;;;GAQG;AACF,0CANU,WAAW,gCAEX,MAAO,MAAM,GAEX,UAAU,CAkBtB;;;;AA3PD;;;;;GAKG;AAEH;;GAEG;AACH;IASE;;;;;;;;;OASG;IACH,yBATW,WAAW,gCAKX,iBAAiB,GAAC,MAAM,EAuClC;IArDD;;;;;OAKG;IACH,cAAM;IAqBJ;;;;OAIG;IACH,cAHU,WAAW,CAGA;IAErB;;;;OAIG;IACH,uBAAmC;IAEnC;;;;OAIG;IACH,qBAA8C;IAE9C;;;OAGG;IACH,YAFU,OAAO,CAEkB;IAGrC;;;;OAIG;IACH,yBAHa,MAAM,CAKlB;IAED;;;;OAIG;IACH,+BAHa,MAAM,CAKlB;IAED;;;;;OAKG;IACH,8BAsBC;IAED;;;;;OAKG;IACH,yBAWC;IAED;;OAEG;IACH,cA2BC;IAED;;;;;;;;OAQG;IACH,WAPW,WAAW,qBAGX,OAAO,oBAgBjB;IAED;;OAEG;IACH,aAKC;CACF;AAED;IACE,gEAEC;CAIF;AAED;IACE,gEAEC;CAIF;AAED;IACE,gEAEC;CAIF;;cA3Oa,MAAM;cACN,UAAU;;;;;;iBAYV,MAAM;;;;YACN,OAAO"}
|
141
types/archive/decompress.d.ts
vendored
141
types/archive/decompress.d.ts
vendored
|
@ -1,30 +1,130 @@
|
|||
/**
|
||||
* Factory method that creates an unarchiver based on the byte signature found
|
||||
* in the 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
|
||||
* transferred to a different JS context once start() is called.
|
||||
* @param {UnarchiverOptions|string} options An optional object of options, or a
|
||||
* string representing where the path to the unarchiver script files. The latter
|
||||
* is now deprecated (use UnarchiverOptions).
|
||||
* @returns {Unarchiver}
|
||||
*/
|
||||
* Factory method that creates an unarchiver based on the byte signature found
|
||||
* in the 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
|
||||
* transferred to a different JS context once start() is called.
|
||||
* @param {UnarchiverOptions|string} options An optional object of options, or a
|
||||
* string representing where the path to the unarchiver script files. The latter
|
||||
* is now deprecated (use UnarchiverOptions).
|
||||
* @returns {Unarchiver}
|
||||
*/
|
||||
export function getUnarchiver(ab: ArrayBuffer, options?: UnarchiverOptions | string): Unarchiver;
|
||||
export class Unzipper extends UnzipperInternal {
|
||||
constructor(ab: any, options: any);
|
||||
/**
|
||||
* Base class for all Unarchivers.
|
||||
*/
|
||||
export class Unarchiver extends EventTarget {
|
||||
/**
|
||||
* @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be
|
||||
* referenced once it is sent to the Unarchiver, since it is marked as Transferable and sent
|
||||
* to the decompress implementation.
|
||||
* @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.
|
||||
* @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
|
||||
* deprecated.
|
||||
*/
|
||||
constructor(arrayBuffer: ArrayBuffer, connectPortFn: any, options?: UnarchiverOptions | string);
|
||||
/**
|
||||
* The client-side port that sends messages to, and receives messages from the
|
||||
* decompressor implementation.
|
||||
* @type {MessagePort}
|
||||
* @private
|
||||
*/
|
||||
private port_;
|
||||
/**
|
||||
* The ArrayBuffer object.
|
||||
* @type {ArrayBuffer}
|
||||
* @protected
|
||||
*/
|
||||
protected ab: ArrayBuffer;
|
||||
/**
|
||||
* A factory method that connects a port to the decompress implementation.
|
||||
* @type {Function(MessagePort): Promise<*>}
|
||||
* @private
|
||||
*/
|
||||
private connectPortFn_;
|
||||
/**
|
||||
* @orivate
|
||||
* @type {boolean}
|
||||
*/
|
||||
debugMode_: boolean;
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The MIME type of the archive.
|
||||
* @protected.
|
||||
*/
|
||||
protected getMIMEType(): string;
|
||||
/**
|
||||
* This method must be overridden by the subclass to return the script filename.
|
||||
* @returns {string} The script filename.
|
||||
* @protected.
|
||||
*/
|
||||
protected getScriptFileName(): string;
|
||||
/**
|
||||
* Create an UnarchiveEvent out of the object sent back from the implementation.
|
||||
* @param {Object} obj
|
||||
* @returns {UnarchiveEvent}
|
||||
* @private
|
||||
*/
|
||||
private createUnarchiveEvent_;
|
||||
/**
|
||||
* Receive an event and pass it to the listener functions.
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @private
|
||||
*/
|
||||
private handlePortEvent_;
|
||||
/**
|
||||
* Starts the unarchive by connecting the ports and sending the first ArrayBuffer.
|
||||
*/
|
||||
start(): void;
|
||||
/**
|
||||
* Adds more bytes to the unarchiver.
|
||||
* @param {ArrayBuffer} ab The ArrayBuffer with more bytes in it. If opt_transferable is
|
||||
* set to true, this ArrayBuffer must not be referenced after calling update(), since it
|
||||
* is marked as Transferable and sent to the implementation.
|
||||
* @param {boolean=} opt_transferable Optional boolean whether to mark this ArrayBuffer
|
||||
* as a Tranferable object, which means it can no longer be referenced outside of
|
||||
* the implementation context.
|
||||
*/
|
||||
update(ab: ArrayBuffer, opt_transferable?: boolean | undefined): void;
|
||||
/**
|
||||
* Closes the port to the decompressor implementation and terminates it.
|
||||
*/
|
||||
stop(): void;
|
||||
}
|
||||
export class Unrarrer extends UnrarrerInternal {
|
||||
constructor(ab: any, options: any);
|
||||
export class Unzipper extends Unarchiver {
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {UnarchiverOptions} options
|
||||
*/
|
||||
constructor(ab: ArrayBuffer, options?: UnarchiverOptions);
|
||||
}
|
||||
export class Untarrer extends UntarrerInternal {
|
||||
constructor(ab: any, options: any);
|
||||
export class Unrarrer extends Unarchiver {
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {UnarchiverOptions} options
|
||||
*/
|
||||
constructor(ab: ArrayBuffer, options?: UnarchiverOptions);
|
||||
}
|
||||
export class Untarrer extends Unarchiver {
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
* @param {UnarchiverOptions} options
|
||||
*/
|
||||
constructor(ab: ArrayBuffer, options?: UnarchiverOptions);
|
||||
}
|
||||
export type UnarchivedFile = {
|
||||
filename: string;
|
||||
fileData: Uint8Array;
|
||||
};
|
||||
export type UnarchiverOptions = import('./decompress-internal.js').UnarchiverOptions;
|
||||
import { Unarchiver } from "./decompress-internal.js";
|
||||
export type UnarchiverOptions = {
|
||||
/**
|
||||
* Set to true for verbose unarchiver logging.
|
||||
*/
|
||||
debug?: boolean | undefined;
|
||||
};
|
||||
import { UnarchiveAppendEvent } from "./events.js";
|
||||
import { UnarchiveErrorEvent } from "./events.js";
|
||||
import { UnarchiveEvent } from "./events.js";
|
||||
|
@ -34,8 +134,5 @@ import { UnarchiveFinishEvent } from "./events.js";
|
|||
import { UnarchiveInfoEvent } from "./events.js";
|
||||
import { UnarchiveProgressEvent } from "./events.js";
|
||||
import { UnarchiveStartEvent } from "./events.js";
|
||||
import { UnzipperInternal } from "./decompress-internal.js";
|
||||
import { UnrarrerInternal } from "./decompress-internal.js";
|
||||
import { UntarrerInternal } from "./decompress-internal.js";
|
||||
export { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType, UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent, UnarchiveProgressEvent, UnarchiveStartEvent, Unarchiver };
|
||||
export { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType, UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent, UnarchiveProgressEvent, UnarchiveStartEvent };
|
||||
//# sourceMappingURL=decompress.d.ts.map
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"decompress.d.ts","sourceRoot":"","sources":["../../archive/decompress.js"],"names":[],"mappings":"AAmFA;;;;;;;;;;EAUE;AACF,kCARU,WAAW,YAGX,iBAAiB,GAAC,MAAM,GAGtB,UAAU,CAIrB;AAzBD;IACE,mCAA+D;CAChE;AAED;IACE,mCAA+D;CAChE;AAED;IACE,mCAA+D;CAChE;;cA7Ca,MAAM;cACN,UAAU;;gCAIX,OAAO,0BAA0B,EAAE,iBAAiB"}
|
||||
{"version":3,"file":"decompress.d.ts","sourceRoot":"","sources":["../../archive/decompress.js"],"names":[],"mappings":"AAmSA;;;;;;;;;;GAUG;AACH,kCARW,WAAW,YAGX,iBAAiB,GAAC,MAAM,GAGtB,UAAU,CAkBtB;AA5PD;;GAEG;AACH;IASE;;;;;;;;;OASG;IACH,yBATW,WAAW,gCAKX,iBAAiB,GAAC,MAAM,EAgClC;IA9CD;;;;;OAKG;IACH,cAAM;IAqBJ;;;;OAIG;IACH,cAHU,WAAW,CAGA;IAErB;;;;OAIG;IACH,uBAAmC;IAEnC;;;OAGG;IACH,YAFU,OAAO,CAEkB;IAGrC;;;;OAIG;IACH,yBAHa,MAAM,CAKlB;IAED;;;;OAIG;IACH,+BAHa,MAAM,CAKlB;IAED;;;;;OAKG;IACH,8BAsBC;IAED;;;;;OAKG;IACH,yBAWC;IAED;;OAEG;IACH,cA0BC;IAED;;;;;;;;OAQG;IACH,WAPW,WAAW,qBAGX,OAAO,oBAgBjB;IAED;;OAEG;IACH,aAKC;CACF;AAID;IACE;;;OAGG;IACH,gBAHW,WAAW,YACX,iBAAiB,EAI3B;CAIF;AAED;IACE;;;OAGG;IACH,gBAHW,WAAW,YACX,iBAAiB,EAI3B;CAIF;AAED;IACE;;;OAGG;IACH,gBAHW,WAAW,YACX,iBAAiB,EAI3B;CAIF;;cA/Pa,MAAM;cACN,UAAU;;;;;;YAKV,OAAO"}
|
Loading…
Add table
Add a link
Reference in a new issue