mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Fix issue #44, make decompress work in NodeJS (use Worker only where possible).
This commit is contained in:
parent
5ad89892bd
commit
f71c893f63
6 changed files with 59 additions and 34 deletions
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [1.1.7] - 2023-12-11
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- decompress: Allow unarchiving in Node.
|
||||||
|
|
||||||
## [1.1.6] - 2023-10-25
|
## [1.1.6] - 2023-10-25
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
34
README.md
34
README.md
|
@ -6,21 +6,25 @@
|
||||||
|
|
||||||
A set of dependency-free JavaScript modules to handle binary data in JS (using Typed Arrays). Includes:
|
A set of dependency-free JavaScript modules to handle binary data in JS (using Typed Arrays). Includes:
|
||||||
|
|
||||||
* bitjs/archive: Unarchiving files (unzip, unrar, untar) in the browser, implemented as Web Workers and allowing progressively unarchiving while streaming.
|
* bitjs/archive: Unarchiving files (unzip, unrar, untar) in JavaScript,
|
||||||
* bitjs/codecs: Get the codec info of media containers in a ISO RFC6381 MIME type string
|
implemented as Web Workers where supported, and allowing progressive
|
||||||
|
unarchiving while streaming.
|
||||||
|
* bitjs/codecs: Get the codec info of media containers in a ISO RFC6381
|
||||||
|
MIME type string
|
||||||
* bitjs/file: Detect the type of file from its binary signature.
|
* bitjs/file: Detect the type of file from its binary signature.
|
||||||
* bitjs/image: Conversion of WebP images to PNG or JPEG.
|
* bitjs/image: Conversion of WebP images to PNG or JPEG.
|
||||||
* bitjs/io: Low-level classes for interpreting binary data (BitStream, ByteStream). For example, reading or peeking at N bits at a time.
|
* bitjs/io: Low-level classes for interpreting binary data (BitStream
|
||||||
|
ByteStream). For example, reading or peeking at N bits at a time.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Install it using your favourite package manager, the package is registered under `@codedread/bitjs`.
|
Install it using your favourite package manager, the package is registered under `@codedread/bitjs`.
|
||||||
```bash
|
```bash
|
||||||
$ npm install @codedread/bitjs
|
npm install @codedread/bitjs
|
||||||
```
|
```
|
||||||
or
|
or
|
||||||
```bash
|
```bash
|
||||||
$ yarn add @codedread/bitjs
|
yarn add @codedread/bitjs
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using in Node
|
### Using in Node
|
||||||
|
@ -44,7 +48,7 @@ const { getFullMIMEString } = await import('@codedread/bitjs');
|
||||||
|
|
||||||
### bitjs.archive
|
### bitjs.archive
|
||||||
|
|
||||||
This package includes objects for unarchiving binary data in popular archive formats (zip, rar, tar) providing unzip, unrar and untar capabilities via JavaScript in the browser. A prototype version of a compressor that creates Zip files is also present. The decompression/compression actually happens inside a Web Worker.
|
This package includes objects for unarchiving binary data in popular archive formats (zip, rar, tar) providing unzip, unrar and untar capabilities via JavaScript in the browser. A prototype version of a compressor that creates Zip files is also present. The decompression/compression actually happens inside a Web Worker, when the runtime supports it (browsers, deno).
|
||||||
|
|
||||||
#### Decompressing
|
#### Decompressing
|
||||||
|
|
||||||
|
@ -88,6 +92,24 @@ unzipper.update(anArrayBufferWithMoreBytes);
|
||||||
unzipper.update(anArrayBufferWithYetMoreBytes);
|
unzipper.update(anArrayBufferWithYetMoreBytes);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### A NodeJS Example
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import { getUnarchiver } from './archive/decompress.js';
|
||||||
|
|
||||||
|
const nodeBuffer = fs.readFileSync('comic.cbz');
|
||||||
|
const ab = nodeBuffer.buffer.slice(nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.length);
|
||||||
|
const unarchiver = getUnarchiver(ab, { pathToBitJS: './' });
|
||||||
|
unarchiver.addEventListener('progress', () => process.stdout.write('.'));
|
||||||
|
unarchiver.addEventListener('extract', (evt) => {
|
||||||
|
const extractedFile = evt.unarchivedFile;
|
||||||
|
console.log(`${extractedFile.filename} (${extractedFile.fileData.byteLength} bytes)`);
|
||||||
|
});
|
||||||
|
unarchiver.addEventListener('finish', () => console.log(`Done!`));
|
||||||
|
unarchiver.start();
|
||||||
|
```
|
||||||
|
|
||||||
#### Compressing
|
#### Compressing
|
||||||
|
|
||||||
The Zipper only supports creating zip files without compression (store only) for now. The interface
|
The Zipper only supports creating zip files without compression (store only) for now. The interface
|
||||||
|
|
|
@ -20,13 +20,6 @@ import { findMimeType } from '../file/sniffer.js';
|
||||||
* @property {Uint8Array} fileData
|
* @property {Uint8Array} fileData
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* An enum for threading mode. Currently supporting only WebWorkers.
|
|
||||||
*/
|
|
||||||
export const ThreadingMode = {
|
|
||||||
WEB_WORKER: 'WEB_WORKER',
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef UnarchiverOptions
|
* @typedef UnarchiverOptions
|
||||||
* @property {string} pathToBitJS The path to the bitjs folder.
|
* @property {string} pathToBitJS The path to the bitjs folder.
|
||||||
|
@ -232,7 +225,7 @@ export class UnzipperInternal extends Unarchiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
getMIMEType() { return 'application/zip'; }
|
getMIMEType() { return 'application/zip'; }
|
||||||
getScriptFileName() { return 'archive/unzip.js'; }
|
getScriptFileName() { return './unzip.js'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UnrarrerInternal extends Unarchiver {
|
export class UnrarrerInternal extends Unarchiver {
|
||||||
|
@ -241,7 +234,7 @@ export class UnrarrerInternal extends Unarchiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
getMIMEType() { return 'application/x-rar-compressed'; }
|
getMIMEType() { return 'application/x-rar-compressed'; }
|
||||||
getScriptFileName() { return 'archive/unrar.js'; }
|
getScriptFileName() { return './unrar.js'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UntarrerInternal extends Unarchiver {
|
export class UntarrerInternal extends Unarchiver {
|
||||||
|
@ -250,7 +243,7 @@ export class UntarrerInternal extends Unarchiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
getMIMEType() { return 'application/x-tar'; }
|
getMIMEType() { return 'application/x-tar'; }
|
||||||
getScriptFileName() { return 'archive/untar.js'; };
|
getScriptFileName() { return './untar.js'; };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,23 +43,27 @@ export {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a WebWorker with the given decompressor implementation (e.g. unzip.js)
|
* Connects the MessagePort to the unarchiver implementation (e.g. unzip.js). If Workers exist
|
||||||
* and transfers a MessagePort for communication. Returns a Promise to the Worker.
|
* (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} pathToBitJS The path to the bitjs folder.
|
||||||
* @param {string} implFilename The decompressor implementation filename
|
* @param {string} implFilename The decompressor implementation filename relative to this path
|
||||||
* relative to this path (e.g. './unzip.js').
|
* (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<void>} The Promise resolves once the ports are connected.
|
||||||
* @returns {Promise<*>} Returns a Promise that resolves to the Worker object.
|
|
||||||
*/
|
*/
|
||||||
const connectPortFn = (pathToBitJS, implFilename, implPort) => {
|
const connectPortFn = async (pathToBitJS, implFilename, implPort) => {
|
||||||
return new Promise((resolve, reject) => {
|
if (typeof Worker === 'undefined') {
|
||||||
const worker = new Worker(pathToBitJS + 'archive/unarchiver-webworker.js', {
|
return import(`${implFilename}`).then(implModule => {
|
||||||
type: 'module'
|
implModule.connect(implPort)
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
worker.postMessage({ implSrc: (pathToBitJS + implFilename), }, [implPort]);
|
return new Promise((resolve, reject) => {
|
||||||
resolve(worker);
|
const worker = new Worker(pathToBitJS + 'archive/unarchiver-webworker.js', { type: 'module' });
|
||||||
|
worker.postMessage({ implSrc: implFilename }, [implPort]);
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@codedread/bitjs",
|
"name": "@codedread/bitjs",
|
||||||
"version": "1.1.6",
|
"version": "1.1.7",
|
||||||
"description": "Binary Tools for JavaScript",
|
"description": "Binary Tools for JavaScript",
|
||||||
"homepage": "https://github.com/codedread/bitjs",
|
"homepage": "https://github.com/codedread/bitjs",
|
||||||
"author": "Jeff Schiller",
|
"author": "Jeff Schiller",
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"decompress.d.ts","sourceRoot":"","sources":["../../archive/decompress.js"],"names":[],"mappings":"AAgFA;;;;;;;;;;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;;cAzCa,MAAM;cACN,UAAU;;gCAIX,OAAO,0BAA0B,EAAE,iBAAiB"}
|
{"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"}
|
Loading…
Add table
Add a link
Reference in a new issue