1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-05 02:19:24 +02:00

Commonize on port-connected behavior for compressors and decompressors (part of the refactor for issue #44)

This commit is contained in:
Jeff Schiller 2023-12-13 14:25:17 -08:00
parent 48766d0136
commit e6d13d9404
7 changed files with 114 additions and 89 deletions

View file

@ -8,6 +8,8 @@
* Copyright(c) 2023 Google Inc.
*/
import { getConnectedPort } from './common.js';
// NOTE: THIS IS A VERY HACKY WORK-IN-PROGRESS! THE API IS NOT FROZEN! USE AT YOUR OWN RISK!
/**
@ -59,28 +61,6 @@ export const CompressStatus = {
ERROR: 'error',
};
/**
* Connects the MessagePort to the compressor implementation (e.g. zip.js). If Workers exist
* (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} implFilename The compressor implementation filename relative to this path
* (e.g. './zip.js').
* @param {MessagePort} implPort The MessagePort to connect to the compressor implementation.
* @returns {Promise<void>} The Promise resolves once the ports are connected.
*/
const connectPortFn = async (implFilename, implPort) => {
if (typeof Worker === 'undefined') {
return import(`${implFilename}`).then(implModule => implModule.connect(implPort));
}
return new Promise((resolve, reject) => {
const workerScriptPath = new URL(`./webworker-wrapper.js`, import.meta.url).href;
const worker = new Worker(workerScriptPath, { type: 'module' });
worker.postMessage({ implSrc: implFilename }, [ implPort ]);
resolve();
});
};
/**
* A thing that zips files.
* NOTE: THIS IS A VERY HACKY WORK-IN-PROGRESS! THE API IS NOT FROZEN! USE AT YOUR OWN RISK!
@ -144,9 +124,7 @@ export class Zipper {
* of bytes.
*/
async start(files, isLastFile) {
const messageChannel = new MessageChannel();
this.port_ = messageChannel.port1;
await connectPortFn('./zip.js', messageChannel.port2);
this.port_ = await getConnectedPort('./zip.js');
return new Promise((resolve, reject) => {
this.port_.onerror = (evt) => {
console.log('Impl error: message = ' + evt.message);