Skip to content

Commit f71c893

Browse files
committed
Fix issue #44, make decompress work in NodeJS (use Worker only where possible).
1 parent 5ad8989 commit f71c893

File tree

6 files changed

+59
-34
lines changed

6 files changed

+59
-34
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.1.7] - 2023-12-11
6+
7+
### Changed
8+
9+
- decompress: Allow unarchiving in Node.
10+
511
## [1.1.6] - 2023-10-25
612

713
### Changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66

77
A set of dependency-free JavaScript modules to handle binary data in JS (using Typed Arrays). Includes:
88

9-
* bitjs/archive: Unarchiving files (unzip, unrar, untar) in the browser, implemented as Web Workers and allowing progressively unarchiving while streaming.
10-
* bitjs/codecs: Get the codec info of media containers in a ISO RFC6381 MIME type string
9+
* bitjs/archive: Unarchiving files (unzip, unrar, untar) in JavaScript,
10+
implemented as Web Workers where supported, and allowing progressive
11+
unarchiving while streaming.
12+
* bitjs/codecs: Get the codec info of media containers in a ISO RFC6381
13+
MIME type string
1114
* bitjs/file: Detect the type of file from its binary signature.
1215
* bitjs/image: Conversion of WebP images to PNG or JPEG.
13-
* bitjs/io: Low-level classes for interpreting binary data (BitStream, ByteStream). For example, reading or peeking at N bits at a time.
16+
* bitjs/io: Low-level classes for interpreting binary data (BitStream
17+
ByteStream). For example, reading or peeking at N bits at a time.
1418

1519
## Installation
1620

1721
Install it using your favourite package manager, the package is registered under `@codedread/bitjs`.
1822
```bash
19-
$ npm install @codedread/bitjs
23+
npm install @codedread/bitjs
2024
```
2125
or
2226
```bash
23-
$ yarn add @codedread/bitjs
27+
yarn add @codedread/bitjs
2428
```
2529

2630
### Using in Node
@@ -44,7 +48,7 @@ const { getFullMIMEString } = await import('@codedread/bitjs');
4448

4549
### bitjs.archive
4650

47-
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.
51+
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).
4852

4953
#### Decompressing
5054

@@ -88,6 +92,24 @@ unzipper.update(anArrayBufferWithMoreBytes);
8892
unzipper.update(anArrayBufferWithYetMoreBytes);
8993
```
9094

95+
##### A NodeJS Example
96+
97+
```javascript
98+
import * as fs from 'fs';
99+
import { getUnarchiver } from './archive/decompress.js';
100+
101+
const nodeBuffer = fs.readFileSync('comic.cbz');
102+
const ab = nodeBuffer.buffer.slice(nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.length);
103+
const unarchiver = getUnarchiver(ab, { pathToBitJS: './' });
104+
unarchiver.addEventListener('progress', () => process.stdout.write('.'));
105+
unarchiver.addEventListener('extract', (evt) => {
106+
const extractedFile = evt.unarchivedFile;
107+
console.log(`${extractedFile.filename} (${extractedFile.fileData.byteLength} bytes)`);
108+
});
109+
unarchiver.addEventListener('finish', () => console.log(`Done!`));
110+
unarchiver.start();
111+
```
112+
91113
#### Compressing
92114

93115
The Zipper only supports creating zip files without compression (store only) for now. The interface

archive/decompress-internal.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ import { findMimeType } from '../file/sniffer.js';
2020
* @property {Uint8Array} fileData
2121
*/
2222

23-
/**
24-
* An enum for threading mode. Currently supporting only WebWorkers.
25-
*/
26-
export const ThreadingMode = {
27-
WEB_WORKER: 'WEB_WORKER',
28-
}
29-
3023
/**
3124
* @typedef UnarchiverOptions
3225
* @property {string} pathToBitJS The path to the bitjs folder.
@@ -232,7 +225,7 @@ export class UnzipperInternal extends Unarchiver {
232225
}
233226

234227
getMIMEType() { return 'application/zip'; }
235-
getScriptFileName() { return 'archive/unzip.js'; }
228+
getScriptFileName() { return './unzip.js'; }
236229
}
237230

238231
export class UnrarrerInternal extends Unarchiver {
@@ -241,7 +234,7 @@ export class UnrarrerInternal extends Unarchiver {
241234
}
242235

243236
getMIMEType() { return 'application/x-rar-compressed'; }
244-
getScriptFileName() { return 'archive/unrar.js'; }
237+
getScriptFileName() { return './unrar.js'; }
245238
}
246239

247240
export class UntarrerInternal extends Unarchiver {
@@ -250,7 +243,7 @@ export class UntarrerInternal extends Unarchiver {
250243
}
251244

252245
getMIMEType() { return 'application/x-tar'; }
253-
getScriptFileName() { return 'archive/untar.js'; };
246+
getScriptFileName() { return './untar.js'; };
254247
}
255248

256249
/**

archive/decompress.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export {
2828
}
2929

3030
/**
31-
* All extracted files returned by an Unarchiver will implement
32-
* the following interface:
33-
*/
31+
* All extracted files returned by an Unarchiver will implement
32+
* the following interface:
33+
*/
3434

3535
/**
3636
* @typedef UnarchivedFile
@@ -43,23 +43,27 @@ export {
4343
*/
4444

4545
/**
46-
* Creates a WebWorker with the given decompressor implementation (e.g. unzip.js)
47-
* and transfers a MessagePort for communication. Returns a Promise to the Worker.
46+
* Connects the MessagePort to the unarchiver implementation (e.g. unzip.js). If Workers exist
47+
* (e.g. web browsers or deno), imports the implementation inside a Web Worker. Otherwise, it
48+
* dynamically imports the implementation inside the current JS context.
49+
* The MessagePort is used for communication between host and implementation.
4850
* @param {string} pathToBitJS The path to the bitjs folder.
49-
* @param {string} implFilename The decompressor implementation filename
50-
* relative to this path (e.g. './unzip.js').
51-
* @param {MessagePort} implPort The MessagePort to connect to the decompressor
52-
* implementation.
53-
* @returns {Promise<*>} Returns a Promise that resolves to the Worker object.
51+
* @param {string} implFilename The decompressor implementation filename relative to this path
52+
* (e.g. './unzip.js').
53+
* @param {MessagePort} implPort The MessagePort to connect to the decompressor implementation.
54+
* @returns {Promise<void>} The Promise resolves once the ports are connected.
5455
*/
55-
const connectPortFn = (pathToBitJS, implFilename, implPort) => {
56-
return new Promise((resolve, reject) => {
57-
const worker = new Worker(pathToBitJS + 'archive/unarchiver-webworker.js', {
58-
type: 'module'
56+
const connectPortFn = async (pathToBitJS, implFilename, implPort) => {
57+
if (typeof Worker === 'undefined') {
58+
return import(`${implFilename}`).then(implModule => {
59+
implModule.connect(implPort)
5960
});
61+
}
6062

61-
worker.postMessage({ implSrc: (pathToBitJS + implFilename), }, [implPort]);
62-
resolve(worker);
63+
return new Promise((resolve, reject) => {
64+
const worker = new Worker(pathToBitJS + 'archive/unarchiver-webworker.js', { type: 'module' });
65+
worker.postMessage({ implSrc: implFilename }, [implPort]);
66+
resolve();
6367
});
6468
};
6569

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codedread/bitjs",
3-
"version": "1.1.6",
3+
"version": "1.1.7",
44
"description": "Binary Tools for JavaScript",
55
"homepage": "https://github.com/codedread/bitjs",
66
"author": "Jeff Schiller",

types/archive/decompress.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)