Skip to content

Commit 63e84da

Browse files
committed
Issue #28: Removed build step for bitjs.io now that all browsers (Firefox 114+) support ES Module Workers.
1 parent fce8d69 commit 63e84da

26 files changed

+842
-2891
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [1.1.0] - 2023-05-28
6+
7+
### Added
8+
9+
- Starter thinking around a Media API.
10+
11+
### Changed
12+
13+
- Change console.warn to a console.error when importing archive.js.
14+
15+
### Removed
16+
17+
- Removed build step for bitjs.io now that all browsers (Firefox 114+) support ES Module Workers.
18+
19+
## [1.0.11] - 2023-02-15
20+
21+
### Added
22+
23+
- Add sniffer support for the ICO format.
24+
- Add unit test coverage via c8.
25+
26+
### Fixed
27+
28+
- Fixes for the audio/flac codec type.

archive/archive.js

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { UnarchiveAppendEvent, UnarchiveErrorEvent, UnarchiveEvent, UnarchiveEve
1414
UnarchiveProgressEvent, UnarchiveStartEvent, Unarchiver,
1515
UnrarrerInternal, UntarrerInternal, UnzipperInternal,
1616
getUnarchiverInternal } from './decompress-internal.js';
17+
import { Unzipper, Unrarrer, Untarrer, getUnarchiver } from './decompress.js';
1718

1819
export {
1920
UnarchiveAppendEvent,
@@ -26,65 +27,7 @@ export {
2627
UnarchiveProgressEvent,
2728
UnarchiveStartEvent,
2829
Unarchiver,
30+
Unzipper, Unrarrer, Untarrer, getUnarchiver
2931
}
3032

31-
/**
32-
* All extracted files returned by an Unarchiver will implement
33-
* the following interface:
34-
*/
35-
36-
/**
37-
* @typedef UnarchivedFile
38-
* @property {string} filename
39-
* @property {Uint8Array} fileData
40-
*/
41-
42-
/**
43-
* The goal is to make this testable - send getUnarchiver() an array buffer of
44-
* an archive, call start on the unarchiver, expect the returned result.
45-
*
46-
* Problem: It relies on Web Workers, and that won't work in a nodejs context.
47-
* Solution: Make archive.js very thin, have it feed web-specific things into
48-
* an internal module that is isomorphic JavaScript.
49-
*
50-
* TODO:
51-
* - write unit tests for archive-internal.js that use the nodejs Worker
52-
* equivalent.
53-
* - maybe use @pgriess/node-webworker or @audreyt/node-webworker-threads or
54-
* just node's worker_threads ?
55-
*/
56-
57-
const createWorkerFn = (scriptFilename) => new Worker(scriptFilename);
58-
59-
function warn() {
60-
console.warn(`Stop using archive.js and use decompress.js instead. This module will be removed.`);
61-
}
62-
63-
// Thin wrappers of unarchivers for clients who want to construct a specific
64-
// unarchiver themselves rather than use getUnarchiver().
65-
export class Unzipper extends UnzipperInternal {
66-
constructor(ab, options) { warn(); super(ab, createWorkerFn, options); }
67-
}
68-
69-
export class Unrarrer extends UnrarrerInternal {
70-
constructor(ab, options) { warn(); super(ab, createWorkerFn, options); }
71-
}
72-
73-
export class Untarrer extends UntarrerInternal {
74-
constructor(ab, options) { warn(); super(ab, createWorkerFn, options); }
75-
}
76-
77-
/**
78-
* Factory method that creates an unarchiver based on the byte signature found
79-
* in the arrayBuffer.
80-
* @param {ArrayBuffer} ab The ArrayBuffer to unarchive. Note that this ArrayBuffer
81-
* must not be referenced after calling this method, as the ArrayBuffer is marked
82-
* as Transferable and sent to a Worker thread once start() is called.
83-
* @param {Object|string} options An optional object of options, or a string
84-
* representing where the path to the unarchiver script files.
85-
* @returns {Unarchiver}
86-
*/
87-
export function getUnarchiver(ab, options = {}) {
88-
warn();
89-
return getUnarchiverInternal(ab, createWorkerFn, options);
90-
}
33+
console.error(`bitjs: Stop importing archive.js, this module will be removed. Import decompress.js instead.`);

archive/decompress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export {
5353
* just node's worker_threads ?
5454
*/
5555

56-
const createWorkerFn = (scriptFilename) => new Worker(scriptFilename);
56+
const createWorkerFn = (scriptFilename) => new Worker(scriptFilename, { type: 'module' });
5757

5858
// Thin wrappers of compressors for clients who want to construct a specific
5959
// unarchiver themselves rather than use getUnarchiver().

archive/rarvm.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Copyright(c) 2017 Google Inc.
77
*/
88

9+
import { BitStream } from '../io/bitstream.js';
10+
911
/**
1012
* CRC Implementation.
1113
*/
@@ -104,13 +106,13 @@ function CRC(startCRC, arr) {
104106
/**
105107
* RarVM Implementation.
106108
*/
107-
const VM_MEMSIZE = 0x40000;
108-
const VM_MEMMASK = (VM_MEMSIZE - 1);
109-
const VM_GLOBALMEMADDR = 0x3C000;
110-
const VM_GLOBALMEMSIZE = 0x2000;
111-
const VM_FIXEDGLOBALSIZE = 64;
112-
const MAXWINSIZE = 0x400000;
113-
const MAXWINMASK = (MAXWINSIZE - 1);
109+
export const VM_MEMSIZE = 0x40000;
110+
export const VM_MEMMASK = (VM_MEMSIZE - 1);
111+
export const VM_GLOBALMEMADDR = 0x3C000;
112+
export const VM_GLOBALMEMSIZE = 0x2000;
113+
export const VM_FIXEDGLOBALSIZE = 64;
114+
export const MAXWINSIZE = 0x400000;
115+
export const MAXWINMASK = (MAXWINSIZE - 1);
114116

115117
/**
116118
*/
@@ -448,7 +450,7 @@ const StdList = [
448450
/**
449451
* @constructor
450452
*/
451-
class RarVM {
453+
export class RarVM {
452454
constructor() {
453455
/** @private {Uint8Array} */
454456
this.mem_ = null;
@@ -486,7 +488,7 @@ class RarVM {
486488
/**
487489
* @param {VM_PreparedOperand} op
488490
* @param {boolean} byteMode
489-
* @param {bitjs.io.BitStream} bstream A rtl bit stream.
491+
* @param {BitStream} bstream A rtl bit stream.
490492
*/
491493
decodeArg(op, byteMode, bstream) {
492494
const data = bstream.peekBits(16);
@@ -808,7 +810,7 @@ class RarVM {
808810

809811
//InitBitInput();
810812
//memcpy(InBuf,Code,Min(CodeSize,BitInput::MAX_SIZE));
811-
const bstream = new bitjs.io.BitStream(code.buffer, true /* rtl */);
813+
const bstream = new BitStream(code.buffer, true /* rtl */);
812814

813815
// Calculate the single byte XOR checksum to check validity of VM code.
814816
let xorSum = 0;
@@ -970,7 +972,7 @@ class RarVM {
970972
/**
971973
* Static function that reads in the next set of bits for the VM
972974
* (might return 4, 8, 16 or 32 bits).
973-
* @param {bitjs.io.BitStream} bstream A RTL bit stream.
975+
* @param {BitStream} bstream A RTL bit stream.
974976
* @returns {number} The value of the bits read.
975977
*/
976978
static readData(bstream) {

archive/unrar.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// present.
1313

1414
// This file expects to be invoked as a Worker (see onmessage below).
15-
importScripts('../io/bitstream-worker.js');
16-
importScripts('../io/bytestream-worker.js');
17-
importScripts('../io/bytebuffer-worker.js');
18-
importScripts('rarvm.js');
15+
import { BitStream } from '../io/bitstream.js';
16+
import { ByteStream } from '../io/bytestream.js';
17+
import { ByteBuffer } from '../io/bytebuffer.js';
18+
import { VM_GLOBALMEMADDR, VM_GLOBALMEMSIZE, VM_FIXEDGLOBALSIZE, MAXWINMASK } from './rarvm.js';
1919

2020
const UnarchiveState = {
2121
NOT_STARTED: 0,
@@ -86,7 +86,7 @@ const ENDARC_HEAD = 0x7b;
8686
*/
8787
class RarVolumeHeader {
8888
/**
89-
* @param {bitjs.io.ByteStream} bstream
89+
* @param {ByteStream} bstream
9090
*/
9191
constructor(bstream) {
9292
let headBytesRead = 0;
@@ -316,19 +316,19 @@ const RD = { //rep decode
316316
};
317317

318318
/**
319-
* @type {Array<bitjs.io.ByteBuffer>}
319+
* @type {Array<ByteBuffer>}
320320
*/
321321
const rOldBuffers = [];
322322

323323
/**
324324
* The current buffer we are unpacking to.
325-
* @type {bitjs.io.ByteBuffer}
325+
* @type {ByteBuffer}
326326
*/
327327
let rBuffer;
328328

329329
/**
330330
* The buffer of the final bytes after filtering (only used in Unpack29).
331-
* @type {bitjs.io.ByteBuffer}
331+
* @type {ByteBuffer}
332332
*/
333333
let wBuffer;
334334

@@ -346,7 +346,7 @@ let wBuffer;
346346

347347
/**
348348
* Read in Huffman tables for RAR
349-
* @param {bitjs.io.BitStream} bstream
349+
* @param {BitStream} bstream
350350
*/
351351
function RarReadTables(bstream) {
352352
const BitLength = new Array(rBC);
@@ -491,7 +491,7 @@ function RarMakeDecodeTables(BitLength, offset, dec, size) {
491491

492492
// TODO: implement
493493
/**
494-
* @param {bitjs.io.BitStream} bstream
494+
* @param {BitStream} bstream
495495
* @param {boolean} Solid
496496
*/
497497
function Unpack15(bstream, Solid) {
@@ -500,7 +500,7 @@ function Unpack15(bstream, Solid) {
500500

501501
/**
502502
* Unpacks the bit stream into rBuffer using the Unpack20 algorithm.
503-
* @param {bitjs.io.BitStream} bstream
503+
* @param {BitStream} bstream
504504
* @param {boolean} Solid
505505
*/
506506
function Unpack20(bstream, Solid) {
@@ -694,7 +694,7 @@ function InitFilters() {
694694
*/
695695
function RarAddVMCode(firstByte, vmCode) {
696696
VM.init();
697-
const bstream = new bitjs.io.BitStream(vmCode.buffer, true /* rtl */);
697+
const bstream = new BitStream(vmCode.buffer, true /* rtl */);
698698

699699
let filtPos;
700700
if (firstByte & 0x80) {
@@ -864,7 +864,7 @@ function RarAddVMCode(firstByte, vmCode) {
864864

865865

866866
/**
867-
* @param {!bitjs.io.BitStream} bstream
867+
* @param {!BitStream} bstream
868868
*/
869869
function RarReadVMCode(bstream) {
870870
const firstByte = bstream.readBits(8);
@@ -886,7 +886,7 @@ function RarReadVMCode(bstream) {
886886

887887
/**
888888
* Unpacks the bit stream into rBuffer using the Unpack29 algorithm.
889-
* @param {bitjs.io.BitStream} bstream
889+
* @param {BitStream} bstream
890890
* @param {boolean} Solid
891891
*/
892892
function Unpack29(bstream, Solid) {
@@ -1258,9 +1258,9 @@ function unpack(v) {
12581258
// TODO: implement what happens when unpVer is < 15
12591259
const Ver = v.header.unpVer <= 15 ? 15 : v.header.unpVer;
12601260
const Solid = v.header.flags.LHD_SOLID;
1261-
const bstream = new bitjs.io.BitStream(v.fileData.buffer, true /* rtl */, v.fileData.byteOffset, v.fileData.byteLength);
1261+
const bstream = new BitStream(v.fileData.buffer, true /* rtl */, v.fileData.byteOffset, v.fileData.byteLength);
12621262

1263-
rBuffer = new bitjs.io.ByteBuffer(v.header.unpackedSize);
1263+
rBuffer = new ByteBuffer(v.header.unpackedSize);
12641264

12651265
if (logToConsole) {
12661266
info('Unpacking ' + v.filename + ' RAR v' + Ver);
@@ -1276,7 +1276,7 @@ function unpack(v) {
12761276
break;
12771277
case 29: // rar 3.x compression
12781278
case 36: // alternative hash
1279-
wBuffer = new bitjs.io.ByteBuffer(rBuffer.data.length);
1279+
wBuffer = new ByteBuffer(rBuffer.data.length);
12801280
Unpack29(bstream, Solid);
12811281
break;
12821282
} // switch(method)
@@ -1290,7 +1290,7 @@ function unpack(v) {
12901290
*/
12911291
class RarLocalFile {
12921292
/**
1293-
* @param {bitjs.io.ByteStream} bstream
1293+
* @param {ByteStream} bstream
12941294
*/
12951295
constructor(bstream) {
12961296
this.header = new RarVolumeHeader(bstream);
@@ -1325,7 +1325,7 @@ class RarLocalFile {
13251325

13261326
// Create a new buffer and copy it over.
13271327
const len = this.header.packSize;
1328-
const newBuffer = new bitjs.io.ByteBuffer(len);
1328+
const newBuffer = new ByteBuffer(len);
13291329
newBuffer.insertBytes(this.fileData);
13301330
this.fileData = newBuffer.data;
13311331
} else {
@@ -1402,7 +1402,7 @@ onmessage = function (event) {
14021402

14031403
// This is the very first time we have been called. Initialize the bytestream.
14041404
if (!bytestream) {
1405-
bytestream = new bitjs.io.ByteStream(bytes);
1405+
bytestream = new ByteStream(bytes);
14061406

14071407
currentFilename = '';
14081408
currentFileNumber = 0;

archive/untar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// This file expects to be invoked as a Worker (see onmessage below).
14-
importScripts('../io/bytestream-worker.js');
14+
import { ByteStream } from '../io/bytestream.js';
1515

1616
const UnarchiveState = {
1717
NOT_STARTED: 0,
@@ -166,7 +166,7 @@ onmessage = function (event) {
166166

167167
// This is the very first time we have been called. Initialize the bytestream.
168168
if (!bytestream) {
169-
bytestream = new bitjs.io.ByteStream(bytes);
169+
bytestream = new ByteStream(bytes);
170170
} else {
171171
bytestream.push(bytes);
172172
}

0 commit comments

Comments
 (0)