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

For issue #40, support DEFLATE compression in Zipper where the runtime supports it via CompressionStream.

This commit is contained in:
Jeff Schiller 2024-01-25 01:17:35 -08:00
parent c07aa83e12
commit 6c19e3a908
4 changed files with 88 additions and 18 deletions

View file

@ -76,11 +76,24 @@ export class Zipper {
*/
constructor(options) {
/**
* @type {ZipCompressionMethod}
* @type {CompressorOptions}
* @private
*/
this.zipOptions = options;
this.zipCompressionMethod = options.zipCompressionMethod || ZipCompressionMethod.STORE;
if (this.zipCompressionMethod === ZipCompressionMethod.DEFLATE) throw `DEFLATE not supported.`;
if (!Object.values(ZipCompressionMethod).includes(this.zipCompressionMethod)) {
throw `Compression method ${this.zipCompressionMethod} not supported`;
}
if (this.zipCompressionMethod === ZipCompressionMethod.DEFLATE) {
// As per https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream, NodeJS only
// supports deflate-raw from 21.2.0+ (Nov 2023). https://nodejs.org/en/blog/release/v21.2.0.
try {
new CompressionStream('deflate-raw');
} catch (err) {
throw `CompressionStream with deflate-raw not supported by JS runtime: ${err}`;
}
}
/**
* @type {CompressStatus}
@ -155,7 +168,7 @@ export class Zipper {
};
this.compressState = CompressStatus.READY;
this.appendFiles(files, isLastFile);
this.port_.postMessage({ files, isLastFile, compressionMethod: this.zipCompressionMethod});
});
}
@ -170,4 +183,4 @@ export class Zipper {
this.byteArray.set(oldArray);
this.byteArray.set(newBytes, oldArray.byteLength);
}
}
}