|
@@ -9,37 +9,22 @@
|
|
|
const COMPRESSION_LEVEL = 7;
|
|
const COMPRESSION_LEVEL = 7;
|
|
|
const NO_ZLIB_HEADER = -1;
|
|
const NO_ZLIB_HEADER = -1;
|
|
|
const CHUNK_SIZE = 32 * 1024;
|
|
const CHUNK_SIZE = 32 * 1024;
|
|
|
- const map = {};
|
|
|
|
|
- const memory = new WebAssembly.Memory({
|
|
|
|
|
- initial: 1,
|
|
|
|
|
- maximum: 1024, // 64MB
|
|
|
|
|
- });
|
|
|
|
|
- const env = {
|
|
|
|
|
- memory,
|
|
|
|
|
- writeToJs(ptr, size) {
|
|
|
|
|
- const o = map[ptr];
|
|
|
|
|
- o.onData(new Uint8Array(memory.buffer, dstPtr, size));
|
|
|
|
|
- },
|
|
|
|
|
- _abort: errno => { console.error(`Error: ${errno}`) },
|
|
|
|
|
- _grow: () => { },
|
|
|
|
|
- };
|
|
|
|
|
- const ins = (await WebAssembly.instantiateStreaming(fetch('js/zlib-1.3.1.wasm'), { env })).instance;
|
|
|
|
|
-
|
|
|
|
|
- const srcPtr = ins.exports._malloc(CHUNK_SIZE);
|
|
|
|
|
- const dstPtr = ins.exports._malloc(CHUNK_SIZE);
|
|
|
|
|
|
|
+ const Module = await import('zlib-1.3.2.mjs');
|
|
|
|
|
+ Module.map = {};
|
|
|
|
|
+ const srcPtr = Module.__malloc(CHUNK_SIZE);
|
|
|
|
|
+ const dstPtr = Module.__malloc(CHUNK_SIZE);
|
|
|
|
|
|
|
|
class RawDef {
|
|
class RawDef {
|
|
|
constructor() {
|
|
constructor() {
|
|
|
- this.zstreamPtr = ins.exports._createDeflateContext(COMPRESSION_LEVEL, NO_ZLIB_HEADER);
|
|
|
|
|
- map[this.zstreamPtr] = this;
|
|
|
|
|
|
|
+ this.zstreamPtr = Module.__createDeflateContext(COMPRESSION_LEVEL, NO_ZLIB_HEADER);
|
|
|
|
|
+ Module.map[this.zstreamPtr] = this;
|
|
|
this.offset = 0;
|
|
this.offset = 0;
|
|
|
this.buff = new Uint8Array(CHUNK_SIZE);
|
|
this.buff = new Uint8Array(CHUNK_SIZE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
deflate(chunk, flush) {
|
|
deflate(chunk, flush) {
|
|
|
- const src = new Uint8Array(memory.buffer, srcPtr, chunk.length);
|
|
|
|
|
- src.set(chunk);
|
|
|
|
|
- ins.exports._deflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE, flush);
|
|
|
|
|
|
|
+ Module.HEAPU8.set(chunk, srcPtr);
|
|
|
|
|
+ Module.__deflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE, flush);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
onData(chunk) {
|
|
onData(chunk) {
|
|
@@ -53,32 +38,27 @@
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
destroy() {
|
|
|
- ins.exports._freeDeflateContext(this.zstreamPtr);
|
|
|
|
|
- delete map[this.zstreamPtr];
|
|
|
|
|
|
|
+ Module.__freeDeflateContext(this.zstreamPtr);
|
|
|
|
|
+ delete Module.map[this.zstreamPtr];
|
|
|
this.buff = null;
|
|
this.buff = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
getBuffer() {
|
|
getBuffer() {
|
|
|
- const res = new Uint8Array(this.offset);
|
|
|
|
|
- for (let i = 0; i < this.offset; ++i) {
|
|
|
|
|
- res[i] = this.buff[i];
|
|
|
|
|
- }
|
|
|
|
|
- return res;
|
|
|
|
|
|
|
+ return Buffer.from(this.buff.buffer, 0, this.offset);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class RawInf {
|
|
class RawInf {
|
|
|
constructor() {
|
|
constructor() {
|
|
|
- this.zstreamPtr = ins.exports._createInflateContext(NO_ZLIB_HEADER);
|
|
|
|
|
- map[this.zstreamPtr] = this;
|
|
|
|
|
|
|
+ this.zstreamPtr = Module.__createInflateContext(NO_ZLIB_HEADER);
|
|
|
|
|
+ Module.map[this.zstreamPtr] = this;
|
|
|
this.offset = 0;
|
|
this.offset = 0;
|
|
|
this.buff = new Uint8Array(CHUNK_SIZE);
|
|
this.buff = new Uint8Array(CHUNK_SIZE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
inflate(chunk) {
|
|
inflate(chunk) {
|
|
|
- const src = new Uint8Array(memory.buffer, srcPtr, chunk.length);
|
|
|
|
|
- src.set(chunk);
|
|
|
|
|
- ins.exports._inflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE);
|
|
|
|
|
|
|
+ Module.HEAPU8.set(chunk, srcPtr);
|
|
|
|
|
+ Module.__inflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
onData(chunk) {
|
|
onData(chunk) {
|
|
@@ -92,17 +72,13 @@
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
destroy() {
|
|
|
- ins.exports._freeInflateContext(this.zstreamPtr);
|
|
|
|
|
- delete map[this.zstreamPtr];
|
|
|
|
|
|
|
+ Module.__freeInflateContext(this.zstreamPtr);
|
|
|
|
|
+ delete Module.map[this.zstreamPtr];
|
|
|
this.buff = null;
|
|
this.buff = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
getBuffer() {
|
|
getBuffer() {
|
|
|
- const res = new Uint8Array(this.offset);
|
|
|
|
|
- for (let i = 0; i < this.offset; ++i) {
|
|
|
|
|
- res[i] = this.buff[i];
|
|
|
|
|
- }
|
|
|
|
|
- return res;
|
|
|
|
|
|
|
+ return Buffer.from(this.buff.buffer, 0, this.offset);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|