1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 09:39:16 +02:00
bitjs/archive/decompress.js

84 lines
2.6 KiB
JavaScript

/**
* decompress.js
*
* Provides base functionality for unarchiving/decompression.
*
* Licensed under the MIT License
*
* Copyright(c) 2021 Google Inc.
*/
import { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEventType,
UnarchiveExtractEvent, UnarchiveFinishEvent, UnarchiveInfoEvent,
UnarchiveProgressEvent, UnarchiveStartEvent, Unarchiver,
UnrarrerInternal, UntarrerInternal, UnzipperInternal,
getUnarchiverInternal } from './decompress-internal.js';
export {
UnarchiveAppendEvent,
UnarchiveErrorEvent,
UnarchiveEvent,
UnarchiveEventType,
UnarchiveExtractEvent,
UnarchiveFinishEvent,
UnarchiveInfoEvent,
UnarchiveProgressEvent,
UnarchiveStartEvent,
Unarchiver,
}
/**
* All extracted files returned by an Unarchiver will implement
* the following interface:
*/
/**
* @typedef UnarchivedFile
* @property {string} filename
* @property {Uint8Array} fileData
*/
/**
* The goal is to make this testable - send getUnarchiver() an array buffer of
* an archive, call start on the unarchiver, expect the returned result.
*
* Problem: It relies on Web Workers, and that won't work in a nodejs context.
* Solution: Make archive.js very thin, have it feed web-specific things into
* an internal module that is isomorphic JavaScript.
*
* TODO:
* - write unit tests for archive-internal.js that use the nodejs Worker
* equivalent.
* - maybe use @pgriess/node-webworker or @audreyt/node-webworker-threads or
* just node's worker_threads ?
*/
const createWorkerFn = (scriptFilename) => new Worker(scriptFilename);
// Thin wrappers of compressors for clients who want to construct a specific
// unarchiver themselves rather than use getUnarchiver().
export class Unzipper extends UnzipperInternal {
constructor(ab, options) { super(ab, createWorkerFn, options); }
}
export class Unrarrer extends UnrarrerInternal {
constructor(ab, options) { super(ab, createWorkerFn, options); }
}
export class Untarrer extends UntarrerInternal {
constructor(ab, options) { super(ab, createWorkerFn, options); }
}
/**
* 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 is marked
* as Transferable and sent to a Worker thread once start() is called.
* @param {Object|string} options An optional object of options, or a string
* representing where the path to the unarchiver script files.
* @return {Unarchiver}
*/
export function getUnarchiver(ab, options = {}) {
return getUnarchiverInternal(ab, createWorkerFn, options);
}