1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-04 10:09:16 +02:00

Rework decompression implementations to not be Workers, but instead communicate to the host via a MessagePort. This doesn't address issue #44 directly yet (still need a way to swap out port connection with a non-Web Worker).

This commit is contained in:
Jeff Schiller 2023-12-11 00:12:42 -08:00
parent 1ab380c7c4
commit df5c5cfa0f
6 changed files with 185 additions and 107 deletions

View file

@ -11,9 +11,6 @@
// of a BitStream so that it throws properly when not enough bytes are
// present.
// This file expects to be invoked as a Worker (see onmessage below).
// TODO: Make this a plain ES Module and then write a thin wrapper around it for Worker-ization.
// This will allow us to write proper unit tests for it.
import { BitStream } from '../io/bitstream.js';
import { ByteStream } from '../io/bytestream.js';
import { ByteBuffer } from '../io/bytebuffer.js';
@ -26,6 +23,9 @@ const UnarchiveState = {
FINISHED: 3,
};
/** @type {MessagePort} */
let hostPort;
// State - consider putting these into a class.
let unarchiveState = UnarchiveState.NOT_STARTED;
let bytestream = null;
@ -42,13 +42,13 @@ let totalFilesInArchive = 0;
// Helper functions.
const info = function (str) {
postMessage({ type: 'info', msg: str });
hostPort.postMessage({ type: 'info', msg: str });
};
const err = function (str) {
postMessage({ type: 'error', msg: str });
hostPort.postMessage({ type: 'error', msg: str });
};
const postProgress = function () {
postMessage({
hostPort.postMessage({
type: 'progress',
currentFilename,
currentFileNumber,
@ -1380,7 +1380,7 @@ function unrar() {
localFile.unrar();
if (localFile.isValid) {
postMessage({ type: 'extract', unarchivedFile: localFile }, [localFile.fileData.buffer]);
hostPort.postMessage({ type: 'extract', unarchivedFile: localFile }, [localFile.fileData.buffer]);
postProgress();
}
} else if (localFile.header.packSize == 0 && localFile.header.unpackedSize == 0) {
@ -1398,7 +1398,7 @@ function unrar() {
// event.data.file has the first ArrayBuffer.
// event.data.bytes has all subsequent ArrayBuffers.
onmessage = function (event) {
const onmessage = function (event) {
const bytes = event.data.file || event.data.bytes;
logToConsole = !!event.data.logToConsole;
@ -1413,7 +1413,7 @@ onmessage = function (event) {
totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0;
allLocalFiles = [];
postMessage({ type: 'start' });
hostPort.postMessage({ type: 'start' });
} else {
bytestream.push(bytes);
}
@ -1443,7 +1443,7 @@ onmessage = function (event) {
try {
unrar();
unarchiveState = UnarchiveState.FINISHED;
postMessage({ type: 'finish', metadata: {} });
hostPort.postMessage({ type: 'finish', metadata: {} });
} catch (e) {
if (typeof e === 'string' && e.startsWith('Error! Overflowed')) {
if (logToConsole) {
@ -1459,3 +1459,15 @@ onmessage = function (event) {
}
}
};
/**
* Connect the host to the unrar implementation with the given MessagePort.
* @param {MessagePort} port
*/
export function connect(port) {
if (hostPort) {
throw `hostPort already connected`;
}
hostPort = port;
port.onmessage = onmessage;
}