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

Remove unzip/unrar/untar worker dependency on archive.js so that we can turn it into an ES module for Issue #16

This commit is contained in:
codedread 2020-04-02 09:35:31 -07:00
parent 1b00fdaae3
commit 78a11e1b12
4 changed files with 72 additions and 42 deletions

View file

@ -111,8 +111,8 @@ bitjs.archive.UnarchiveProgressEvent = class extends bitjs.archive.UnarchiveEven
* @param {number} totalCompressedBytesRead * @param {number} totalCompressedBytesRead
*/ */
constructor(currentFilename, currentFileNumber, currentBytesUnarchivedInFile, constructor(currentFilename, currentFileNumber, currentBytesUnarchivedInFile,
currentBytesUnarchived, totalUncompressedBytesInArchive, totalFilesInArchive, currentBytesUnarchived, totalUncompressedBytesInArchive, totalFilesInArchive,
totalCompressedBytesRead) { totalCompressedBytesRead) {
super(bitjs.archive.UnarchiveEvent.Type.PROGRESS); super(bitjs.archive.UnarchiveEvent.Type.PROGRESS);
this.currentFilename = currentFilename; this.currentFilename = currentFilename;
@ -231,21 +231,53 @@ bitjs.archive.Unarchiver = class {
} }
} }
/**
* Create an UnarchiveEvent out of the object sent back from the Worker.
* @param {Object} obj
* @return {bitjs.archive.UnarchiveEvent}
* @private
*/
createUnarchiveEvent_(obj) {
switch (obj.type) {
case bitjs.archive.UnarchiveEvent.Type.START:
return new bitjs.archive.UnarchiveStartEvent();
case bitjs.archive.UnarchiveEvent.Type.PROGRESS:
return new bitjs.archive.UnarchiveProgressEvent(
obj.currentFilename,
obj.currentFileNumber,
obj.currentBytesUnarchivedInFile,
obj.currentBytesUnarchived,
obj.totalUncompressedBytesInArchive,
obj.totalFilesInArchive,
obj.totalCompressedBytesRead);
case bitjs.archive.UnarchiveEvent.Type.EXTRACT:
return new bitjs.archive.UnarchiveExtractEvent(obj.unarchivedFile);
case bitjs.archive.UnarchiveEvent.Type.FINISH:
return new bitjs.archive.UnarchiveFinishEvent(obj.metadata);
case bitjs.archive.UnarchiveEvent.Type.INFO:
return new bitjs.archive.UnarchiveInfoEvent(obj.msg);
case bitjs.archive.UnarchiveEvent.Type.ERROR:
return new bitjs.archive.UnarchiveErrorEvent(obj.msg);
}
}
/** /**
* Receive an event and pass it to the listener functions. * Receive an event and pass it to the listener functions.
* *
* @param {bitjs.archive.UnarchiveEvent} e * @param {bitjs.archive.UnarchiveEvent} e
* @private * @private
*/ */
handleWorkerEvent_(e) { handleWorkerEvent_(obj) {
if ((e instanceof bitjs.archive.UnarchiveEvent || e.type) && const type = obj.type;
this.listeners_[e.type] instanceof Array) { if (type && Object.values(bitjs.archive.UnarchiveEvent.Type).includes(type) &&
this.listeners_[e.type].forEach(function (listener) { listener(e) }); this.listeners_[obj.type] instanceof Array) {
if (e.type == bitjs.archive.UnarchiveEvent.Type.FINISH) { const evt = this.createUnarchiveEvent_(obj);
this.worker_.terminate(); this.listeners_[evt.type].forEach(function (listener) { listener(evt) });
if (evt.type == bitjs.archive.UnarchiveEvent.Type.FINISH) {
this.worker_.terminate();
} }
} else { } else {
console.log(e); console.log(`Unknown object received from worker: ${obj}`);
} }
} }
@ -258,18 +290,16 @@ bitjs.archive.Unarchiver = class {
if (scriptFileName) { if (scriptFileName) {
this.worker_ = new Worker(scriptFileName); this.worker_ = new Worker(scriptFileName);
this.worker_.onerror = function(e) { this.worker_.onerror = function (e) {
console.log('Worker error: message = ' + e.message); console.log('Worker error: message = ' + e.message);
throw e; throw e;
}; };
this.worker_.onmessage = function(e) { this.worker_.onmessage = function (e) {
if (typeof e.data == 'string') { if (typeof e.data == 'string') {
// Just log any strings the workers pump our way. // Just log any strings the workers pump our way.
console.log(e.data); console.log(e.data);
} else { } else {
// Assume that it is an UnarchiveEvent. Some browsers preserve the 'type'
// so that instanceof UnarchiveEvent returns true, but others do not.
me.handleWorkerEvent_(e.data); me.handleWorkerEvent_(e.data);
} }
}; };
@ -289,7 +319,7 @@ bitjs.archive.Unarchiver = class {
*/ */
update(ab) { update(ab) {
if (this.worker_) { if (this.worker_) {
this.worker_.postMessage({bytes: ab}); this.worker_.postMessage({ bytes: ab });
} }
} }
@ -347,7 +377,7 @@ bitjs.archive.Untarrer = class extends bitjs.archive.Unarchiver {
* @param {string=} opt_pathToBitJS Path to the unarchiver script files. * @param {string=} opt_pathToBitJS Path to the unarchiver script files.
* @return {bitjs.archive.Unarchiver} * @return {bitjs.archive.Unarchiver}
*/ */
bitjs.archive.GetUnarchiver = function(ab, opt_pathToBitJS) { bitjs.archive.GetUnarchiver = function (ab, opt_pathToBitJS) {
if (ab.byteLength < 10) { if (ab.byteLength < 10) {
return null; return null;
} }

View file

@ -15,7 +15,6 @@
importScripts('../io/bitstream-worker.js'); importScripts('../io/bitstream-worker.js');
importScripts('../io/bytestream-worker.js'); importScripts('../io/bytestream-worker.js');
importScripts('../io/bytebuffer-worker.js'); importScripts('../io/bytebuffer-worker.js');
importScripts('archive.js');
importScripts('rarvm.js'); importScripts('rarvm.js');
const UnarchiveState = { const UnarchiveState = {
@ -41,21 +40,22 @@ let totalFilesInArchive = 0;
// Helper functions. // Helper functions.
const info = function (str) { const info = function (str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); postMessage({ type: 'info', msg: str });
}; };
const err = function (str) { const err = function (str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); postMessage({ type: 'error', msg: str });
}; };
const postProgress = function () { const postProgress = function () {
postMessage(new bitjs.archive.UnarchiveProgressEvent( postMessage({
type: 'progress',
currentFilename, currentFilename,
currentFileNumber, currentFileNumber,
currentBytesUnarchivedInFile, currentBytesUnarchivedInFile,
currentBytesUnarchived, currentBytesUnarchived,
totalUncompressedBytesInArchive, totalUncompressedBytesInArchive,
totalFilesInArchive, totalFilesInArchive,
parseInt(bytestream.getNumBytesRead(), 10), totalCompressedBytesRead: bytestream.getNumBytesRead(),
)); });
}; };
// shows a byte value as its hex representation // shows a byte value as its hex representation
@ -1377,7 +1377,7 @@ function unrar() {
localFile.unrar(); localFile.unrar();
if (localFile.isValid) { if (localFile.isValid) {
postMessage(new bitjs.archive.UnarchiveExtractEvent(localFile)); postMessage({ type: 'extract', unarchivedFile: localFile });
postProgress(); postProgress();
} }
} else if (localFile.header.packSize == 0 && localFile.header.unpackedSize == 0) { } else if (localFile.header.packSize == 0 && localFile.header.unpackedSize == 0) {
@ -1410,7 +1410,7 @@ onmessage = function (event) {
totalUncompressedBytesInArchive = 0; totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0; totalFilesInArchive = 0;
allLocalFiles = []; allLocalFiles = [];
postMessage(new bitjs.archive.UnarchiveStartEvent()); postMessage({ type: 'start' });
} else { } else {
bytestream.push(bytes); bytestream.push(bytes);
} }
@ -1440,7 +1440,7 @@ onmessage = function (event) {
try { try {
unrar(); unrar();
unarchiveState = UnarchiveState.FINISHED; unarchiveState = UnarchiveState.FINISHED;
postMessage(new bitjs.archive.UnarchiveFinishEvent()); postMessage({ type: 'finish', metadata: {} });
} catch (e) { } catch (e) {
if (typeof e === 'string' && e.startsWith('Error! Overflowed')) { if (typeof e === 'string' && e.startsWith('Error! Overflowed')) {
if (logToConsole) { if (logToConsole) {

View file

@ -12,7 +12,6 @@
// This file expects to be invoked as a Worker (see onmessage below). // This file expects to be invoked as a Worker (see onmessage below).
importScripts('../io/bytestream-worker.js'); importScripts('../io/bytestream-worker.js');
importScripts('archive.js');
const UnarchiveState = { const UnarchiveState = {
NOT_STARTED: 0, NOT_STARTED: 0,
@ -37,21 +36,22 @@ let totalFilesInArchive = 0;
// Helper functions. // Helper functions.
const info = function (str) { const info = function (str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); postMessage({ type: 'info', msg: str });
}; };
const err = function (str) { const err = function (str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); postMessage({ type: 'error', msg: str });
}; };
const postProgress = function () { const postProgress = function () {
postMessage(new bitjs.archive.UnarchiveProgressEvent( postMessage({
type: 'progress',
currentFilename, currentFilename,
currentFileNumber, currentFileNumber,
currentBytesUnarchivedInFile, currentBytesUnarchivedInFile,
currentBytesUnarchived, currentBytesUnarchived,
totalUncompressedBytesInArchive, totalUncompressedBytesInArchive,
totalFilesInArchive, totalFilesInArchive,
bytestream.getNumBytesRead(), totalCompressedBytesRead: bytestream.getNumBytesRead(),
)); });
}; };
// Removes all characters from the first zero-byte in the string onwards. // Removes all characters from the first zero-byte in the string onwards.
@ -146,7 +146,7 @@ const untar = function () {
currentFileNumber = totalFilesInArchive++; currentFileNumber = totalFilesInArchive++;
currentBytesUnarchivedInFile = oneLocalFile.size; currentBytesUnarchivedInFile = oneLocalFile.size;
currentBytesUnarchived += oneLocalFile.size; currentBytesUnarchived += oneLocalFile.size;
postMessage(new bitjs.archive.UnarchiveExtractEvent(oneLocalFile)); postMessage({ type: 'extract', unarchivedFile: oneLocalFile });
postProgress(); postProgress();
} }
} }
@ -179,7 +179,7 @@ onmessage = function (event) {
totalFilesInArchive = 0; totalFilesInArchive = 0;
allLocalFiles = []; allLocalFiles = [];
postMessage(new bitjs.archive.UnarchiveStartEvent()); postMessage({ type: 'start' });
unarchiveState = UnarchiveState.UNARCHIVING; unarchiveState = UnarchiveState.UNARCHIVING;
@ -191,7 +191,7 @@ onmessage = function (event) {
try { try {
untar(); untar();
unarchiveState = UnarchiveState.FINISHED; unarchiveState = UnarchiveState.FINISHED;
postMessage(new bitjs.archive.UnarchiveFinishEvent()); postMessage({ type: 'finish', metadata: {} });
} catch (e) { } catch (e) {
if (typeof e === 'string' && e.startsWith('Error! Overflowed')) { if (typeof e === 'string' && e.startsWith('Error! Overflowed')) {
// Overrun the buffer. // Overrun the buffer.

View file

@ -16,7 +16,6 @@
importScripts('../io/bitstream-worker.js'); importScripts('../io/bitstream-worker.js');
importScripts('../io/bytebuffer-worker.js'); importScripts('../io/bytebuffer-worker.js');
importScripts('../io/bytestream-worker.js'); importScripts('../io/bytestream-worker.js');
//importScripts('archive.js');
const UnarchiveState = { const UnarchiveState = {
NOT_STARTED: 0, NOT_STARTED: 0,
@ -41,21 +40,22 @@ let totalFilesInArchive = 0;
// Helper functions. // Helper functions.
const info = function (str) { const info = function (str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); postMessage({ type: 'info', msg: str });
}; };
const err = function (str) { const err = function (str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); postMessage({ type: 'error', msg: str });
}; };
const postProgress = function () { const postProgress = function () {
postMessage(new bitjs.archive.UnarchiveProgressEvent( postMessage({
type: 'progress',
currentFilename, currentFilename,
currentFileNumber, currentFileNumber,
currentBytesUnarchivedInFile, currentBytesUnarchivedInFile,
currentBytesUnarchived, currentBytesUnarchived,
totalUncompressedBytesInArchive, totalUncompressedBytesInArchive,
totalFilesInArchive, totalFilesInArchive,
bytestream.getNumBytesRead(), totalCompressedBytesRead: bytestream.getNumBytesRead(),
)); });
}; };
const zLocalFileHeaderSignature = 0x04034b50; const zLocalFileHeaderSignature = 0x04034b50;
@ -547,7 +547,7 @@ function unzip() {
oneLocalFile.unzip(); oneLocalFile.unzip();
if (oneLocalFile.fileData != null) { if (oneLocalFile.fileData != null) {
postMessage(new bitjs.archive.UnarchiveExtractEvent(oneLocalFile)); postMessage({ type: 'extract', unarchivedFile: oneLocalFile });
postProgress(); postProgress();
} }
} }
@ -644,7 +644,7 @@ function unzip() {
bytestream = bstream.tee(); bytestream = bstream.tee();
unarchiveState = UnarchiveState.FINISHED; unarchiveState = UnarchiveState.FINISHED;
postMessage(new bitjs.archive.UnarchiveFinishEvent(metadata)); postMessage({ type: 'finish', metadata });
} }
// event.data.file has the first ArrayBuffer. // event.data.file has the first ArrayBuffer.
@ -670,7 +670,7 @@ onmessage = function (event) {
currentBytesUnarchived = 0; currentBytesUnarchived = 0;
allLocalFiles = []; allLocalFiles = [];
postMessage(new bitjs.archive.UnarchiveStartEvent()); postMessage({ type: 'start' });
unarchiveState = UnarchiveState.UNARCHIVING; unarchiveState = UnarchiveState.UNARCHIVING;