@myrialabs/zipkit 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +329 -0
- package/dist/bench/corpus.d.ts +21 -0
- package/dist/bench/corpus.d.ts.map +1 -0
- package/dist/bench/corpus.js +92 -0
- package/dist/bench/corpus.js.map +1 -0
- package/dist/checksum.d.ts +29 -0
- package/dist/checksum.d.ts.map +1 -0
- package/dist/checksum.js +35 -0
- package/dist/checksum.js.map +1 -0
- package/dist/cli.d.ts +17 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +427 -0
- package/dist/cli.js.map +1 -0
- package/dist/codecs/brotli.d.ts +6 -0
- package/dist/codecs/brotli.d.ts.map +1 -0
- package/dist/codecs/brotli.js +15 -0
- package/dist/codecs/brotli.js.map +1 -0
- package/dist/codecs/bzip2.d.ts +6 -0
- package/dist/codecs/bzip2.d.ts.map +1 -0
- package/dist/codecs/bzip2.js +15 -0
- package/dist/codecs/bzip2.js.map +1 -0
- package/dist/codecs/deflate.d.ts +6 -0
- package/dist/codecs/deflate.d.ts.map +1 -0
- package/dist/codecs/deflate.js +25 -0
- package/dist/codecs/deflate.js.map +1 -0
- package/dist/codecs/gzip.d.ts +6 -0
- package/dist/codecs/gzip.d.ts.map +1 -0
- package/dist/codecs/gzip.js +25 -0
- package/dist/codecs/gzip.js.map +1 -0
- package/dist/codecs/image.d.ts +5 -0
- package/dist/codecs/image.d.ts.map +1 -0
- package/dist/codecs/image.js +16 -0
- package/dist/codecs/image.js.map +1 -0
- package/dist/codecs/index.d.ts +18 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +18 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/codecs/lz4.d.ts +6 -0
- package/dist/codecs/lz4.d.ts.map +1 -0
- package/dist/codecs/lz4.js +17 -0
- package/dist/codecs/lz4.js.map +1 -0
- package/dist/codecs/lzma.d.ts +6 -0
- package/dist/codecs/lzma.d.ts.map +1 -0
- package/dist/codecs/lzma.js +15 -0
- package/dist/codecs/lzma.js.map +1 -0
- package/dist/codecs/snappy.d.ts +6 -0
- package/dist/codecs/snappy.d.ts.map +1 -0
- package/dist/codecs/snappy.js +14 -0
- package/dist/codecs/snappy.js.map +1 -0
- package/dist/codecs/video.d.ts +13 -0
- package/dist/codecs/video.d.ts.map +1 -0
- package/dist/codecs/video.js +26 -0
- package/dist/codecs/video.js.map +1 -0
- package/dist/codecs/xz.d.ts +6 -0
- package/dist/codecs/xz.d.ts.map +1 -0
- package/dist/codecs/xz.js +30 -0
- package/dist/codecs/xz.js.map +1 -0
- package/dist/codecs/zlib.d.ts +6 -0
- package/dist/codecs/zlib.d.ts.map +1 -0
- package/dist/codecs/zlib.js +15 -0
- package/dist/codecs/zlib.js.map +1 -0
- package/dist/codecs/zstd.d.ts +6 -0
- package/dist/codecs/zstd.d.ts.map +1 -0
- package/dist/codecs/zstd.js +24 -0
- package/dist/codecs/zstd.js.map +1 -0
- package/dist/compress.d.ts +20 -0
- package/dist/compress.d.ts.map +1 -0
- package/dist/compress.js +83 -0
- package/dist/compress.js.map +1 -0
- package/dist/delta.d.ts +30 -0
- package/dist/delta.d.ts.map +1 -0
- package/dist/delta.js +44 -0
- package/dist/delta.js.map +1 -0
- package/dist/detect.d.ts +46 -0
- package/dist/detect.d.ts.map +1 -0
- package/dist/detect.js +86 -0
- package/dist/detect.js.map +1 -0
- package/dist/dictionary.d.ts +37 -0
- package/dist/dictionary.d.ts.map +1 -0
- package/dist/dictionary.js +71 -0
- package/dist/dictionary.js.map +1 -0
- package/dist/engine.d.ts +102 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +204 -0
- package/dist/engine.js.map +1 -0
- package/dist/fsa.d.ts +54 -0
- package/dist/fsa.d.ts.map +1 -0
- package/dist/fsa.js +45 -0
- package/dist/fsa.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +21 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +42 -0
- package/dist/internal.js.map +1 -0
- package/dist/middleware/elysia.d.ts +15 -0
- package/dist/middleware/elysia.d.ts.map +1 -0
- package/dist/middleware/elysia.js +43 -0
- package/dist/middleware/elysia.js.map +1 -0
- package/dist/middleware/express.d.ts +17 -0
- package/dist/middleware/express.d.ts.map +1 -0
- package/dist/middleware/express.js +62 -0
- package/dist/middleware/express.js.map +1 -0
- package/dist/middleware/hono.d.ts +17 -0
- package/dist/middleware/hono.d.ts.map +1 -0
- package/dist/middleware/hono.js +41 -0
- package/dist/middleware/hono.js.map +1 -0
- package/dist/middleware/index.d.ts +14 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +14 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/shared.d.ts +29 -0
- package/dist/middleware/shared.d.ts.map +1 -0
- package/dist/middleware/shared.js +47 -0
- package/dist/middleware/shared.js.map +1 -0
- package/dist/pack.d.ts +36 -0
- package/dist/pack.d.ts.map +1 -0
- package/dist/pack.js +75 -0
- package/dist/pack.js.map +1 -0
- package/dist/parallel/index.d.ts +56 -0
- package/dist/parallel/index.d.ts.map +1 -0
- package/dist/parallel/index.js +158 -0
- package/dist/parallel/index.js.map +1 -0
- package/dist/sevenzip/index.d.ts +45 -0
- package/dist/sevenzip/index.d.ts.map +1 -0
- package/dist/sevenzip/index.js +646 -0
- package/dist/sevenzip/index.js.map +1 -0
- package/dist/streams/index.d.ts +32 -0
- package/dist/streams/index.d.ts.map +1 -0
- package/dist/streams/index.js +74 -0
- package/dist/streams/index.js.map +1 -0
- package/dist/string.d.ts +38 -0
- package/dist/string.d.ts.map +1 -0
- package/dist/string.js +64 -0
- package/dist/string.js.map +1 -0
- package/dist/tar/index.d.ts +99 -0
- package/dist/tar/index.d.ts.map +1 -0
- package/dist/tar/index.js +380 -0
- package/dist/tar/index.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +22 -0
- package/dist/types.js.map +1 -0
- package/dist/workers/index.d.ts +58 -0
- package/dist/workers/index.d.ts.map +1 -0
- package/dist/workers/index.js +158 -0
- package/dist/workers/index.js.map +1 -0
- package/dist/workers/worker.d.ts +18 -0
- package/dist/workers/worker.d.ts.map +1 -0
- package/dist/workers/worker.js +45 -0
- package/dist/workers/worker.js.map +1 -0
- package/dist/zip/crypto/aes.d.ts +25 -0
- package/dist/zip/crypto/aes.d.ts.map +1 -0
- package/dist/zip/crypto/aes.js +156 -0
- package/dist/zip/crypto/aes.js.map +1 -0
- package/dist/zip/crypto/winzip.d.ts +28 -0
- package/dist/zip/crypto/winzip.d.ts.map +1 -0
- package/dist/zip/crypto/winzip.js +87 -0
- package/dist/zip/crypto/winzip.js.map +1 -0
- package/dist/zip/crypto/zipcrypto.d.ts +14 -0
- package/dist/zip/crypto/zipcrypto.d.ts.map +1 -0
- package/dist/zip/crypto/zipcrypto.js +65 -0
- package/dist/zip/crypto/zipcrypto.js.map +1 -0
- package/dist/zip/datetime.d.ts +9 -0
- package/dist/zip/datetime.d.ts.map +1 -0
- package/dist/zip/datetime.js +13 -0
- package/dist/zip/datetime.js.map +1 -0
- package/dist/zip/index.d.ts +134 -0
- package/dist/zip/index.d.ts.map +1 -0
- package/dist/zip/index.js +473 -0
- package/dist/zip/index.js.map +1 -0
- package/dist/zip/stream.d.ts +38 -0
- package/dist/zip/stream.d.ts.map +1 -0
- package/dist/zip/stream.js +240 -0
- package/dist/zip/stream.js.map +1 -0
- package/dist/zipkit.d.ts +74 -0
- package/dist/zipkit.d.ts.map +1 -0
- package/dist/zipkit.js +168 -0
- package/dist/zipkit.js.map +1 -0
- package/engine/dist/zipkit-engine.mjs +2 -0
- package/engine/dist/zipkit-engine.wasm +0 -0
- package/package.json +137 -0
package/dist/detect.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format auto-detection from magic bytes.
|
|
3
|
+
*
|
|
4
|
+
* Recognizes the container/codec of a buffer by inspecting its header. Two
|
|
5
|
+
* groups are reported:
|
|
6
|
+
*
|
|
7
|
+
* - **Directly decompressible** by {@link import('./compress.js').decompress}:
|
|
8
|
+
* gzip, zlib, zstd.
|
|
9
|
+
* - **Recognized but not auto-decoded here** — ZIP and the standard external
|
|
10
|
+
* framings (tar, xz, 7z, standard bzip2, the LZ4 *frame*). These need a
|
|
11
|
+
* dedicated reader (\`@myrialabs/zipkit/zip\`, \`@myrialabs/zipkit/tar\`, …) or an explicit codec, so
|
|
12
|
+
* {@link import('./compress.js').decompress} points you at the right API
|
|
13
|
+
* rather than guessing.
|
|
14
|
+
*
|
|
15
|
+
* ZipKit's own brotli, snappy, raw LZ4 *block*, and length-prefixed lzma/bzip2
|
|
16
|
+
* streams have no leading magic and return `undefined`; decode them with an
|
|
17
|
+
* explicit codec via `decompressWith`.
|
|
18
|
+
*/
|
|
19
|
+
/** Formats {@link import('./compress.js').decompress} can decode directly. */
|
|
20
|
+
const DIRECT = new Set(['gzip', 'zlib', 'zstd']);
|
|
21
|
+
/** Whether {@link decompress} can reverse a detected format without extra API. */
|
|
22
|
+
export function isDirectlyDecompressible(fmt) {
|
|
23
|
+
return DIRECT.has(fmt);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Identify the format of a buffer, or `undefined` if it has no recognizable
|
|
27
|
+
* signature.
|
|
28
|
+
*
|
|
29
|
+
* Detectable: **gzip, zlib, zstd, ZIP, tar, xz, 7z, standard bzip2, and the LZ4
|
|
30
|
+
* frame**. Headerless or ZipKit-framed codecs — brotli, snappy, the raw LZ4
|
|
31
|
+
* *block* written by {@link import('./codecs/lz4.js').lz4}, and the
|
|
32
|
+
* length-prefixed lzma/bzip2 streams ZipKit emits — have no leading magic and
|
|
33
|
+
* return `undefined`; decode them with an explicit codec via `decompressWith`.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* detectFormat(gzipBytes); // 'gzip'
|
|
38
|
+
* detectFormat(zipBytes); // 'zip'
|
|
39
|
+
* detectFormat(tarBytes); // 'tar'
|
|
40
|
+
* detectFormat(brotliBytes); // undefined
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function detectFormat(data) {
|
|
44
|
+
if (data.length < 2)
|
|
45
|
+
return undefined;
|
|
46
|
+
const b0 = data[0];
|
|
47
|
+
const b1 = data[1];
|
|
48
|
+
// gzip — 1f 8b
|
|
49
|
+
if (b0 === 0x1f && b1 === 0x8b)
|
|
50
|
+
return 'gzip';
|
|
51
|
+
// ZIP — "PK\x03\x04" (local file header) or "PK\x05\x06" (empty archive EOCD)
|
|
52
|
+
if (b0 === 0x50 && b1 === 0x4b && (data[2] === 0x03 || data[2] === 0x05)) {
|
|
53
|
+
return 'zip';
|
|
54
|
+
}
|
|
55
|
+
// zstd — little-endian magic 0xFD2FB528
|
|
56
|
+
if (b0 === 0x28 && b1 === 0xb5 && data[2] === 0x2f && data[3] === 0xfd)
|
|
57
|
+
return 'zstd';
|
|
58
|
+
// xz — FD "7zXZ" 00
|
|
59
|
+
if (b0 === 0xfd && b1 === 0x37 && data[2] === 0x7a && data[3] === 0x58 && data[4] === 0x5a && data[5] === 0x00) {
|
|
60
|
+
return 'xz';
|
|
61
|
+
}
|
|
62
|
+
// 7z — "7z" BC AF 27 1C
|
|
63
|
+
if (b0 === 0x37 && b1 === 0x7a && data[2] === 0xbc && data[3] === 0xaf && data[4] === 0x27 && data[5] === 0x1c) {
|
|
64
|
+
return '7z';
|
|
65
|
+
}
|
|
66
|
+
// LZ4 frame — 04 22 4D 18 (the standard frame, not ZipKit's raw block)
|
|
67
|
+
if (b0 === 0x04 && b1 === 0x22 && data[2] === 0x4d && data[3] === 0x18)
|
|
68
|
+
return 'lz4-frame';
|
|
69
|
+
// Standard bzip2 — "BZh" then a 1–9 block-size digit. ZipKit's own bzip2()
|
|
70
|
+
// emits a length-prefixed frame, not this, so its output won't match here.
|
|
71
|
+
if (b0 === 0x42 && b1 === 0x5a && data[2] === 0x68 && data[3] >= 0x31 && data[3] <= 0x39) {
|
|
72
|
+
return 'bzip2';
|
|
73
|
+
}
|
|
74
|
+
// tar (ustar) — "ustar" magic at offset 257 of the first 512-byte header.
|
|
75
|
+
if (data.length >= 263 && data[257] === 0x75 && data[258] === 0x73 && data[259] === 0x74 && data[260] === 0x61 && data[261] === 0x72) {
|
|
76
|
+
return 'tar';
|
|
77
|
+
}
|
|
78
|
+
// zlib — CMF/FLG: low nibble of CMF is 8 (deflate) and the 16-bit header is
|
|
79
|
+
// a multiple of 31. Checked last because the test is structural, not a fixed
|
|
80
|
+
// magic, so it must not shadow the signatures above.
|
|
81
|
+
if ((b0 & 0x0f) === 0x08 && (b0 >> 4) <= 7 && ((b0 << 8) | b1) % 31 === 0) {
|
|
82
|
+
return 'zlib';
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=detect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect.js","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AASH,8EAA8E;AAC9E,MAAM,MAAM,GAAgC,IAAI,GAAG,CAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE9F,kFAAkF;AAClF,MAAM,UAAU,wBAAwB,CAAC,GAAmB;IAC3D,OAAO,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,YAAY,CAAC,IAAgB;IAC5C,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IACtC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAE,CAAC;IACpB,MAAM,EAAE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IAAI;QAAE,OAAO,WAAW,CAAC;IAE3F,2EAA2E;IAC3E,2EAA2E;IAC3E,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,KAAK,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,CAAC,CAAE,IAAI,IAAI,IAAI,IAAI,CAAC,CAAC,CAAE,IAAI,IAAI,EAAE,CAAC;QAC5F,OAAO,OAAO,CAAC;IAChB,CAAC;IAED,0EAA0E;IAC1E,IAAI,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC;QACtI,OAAO,KAAK,CAAC;IACd,CAAC;IAED,4EAA4E;IAC5E,6EAA6E;IAC7E,qDAAqD;IACrD,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;QAC3E,OAAO,MAAM,CAAC;IACf,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zstd dictionary compression — the big win for many small, *similar* payloads
|
|
3
|
+
* (log lines, JSON records, RPC messages, chat-history entries). Train a
|
|
4
|
+
* dictionary once from representative samples, then compress each small payload
|
|
5
|
+
* against it: the shared structure lives in the dictionary instead of being
|
|
6
|
+
* repeated in every frame, so tiny inputs shrink dramatically.
|
|
7
|
+
*
|
|
8
|
+
* Backed by the engine's `ZDICT_trainFromBuffer` + `ZSTD_*_usingDict`. The
|
|
9
|
+
* resulting frames are standard zstd-with-dictionary — interoperable with any
|
|
10
|
+
* zstd that is given the same dictionary.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { trainDictionary, compressWithDictionary, decompressWithDictionary } from '@myrialabs/zipkit';
|
|
15
|
+
* const dict = await trainDictionary(logLines); // Uint8Array[]
|
|
16
|
+
* const packed = await compressWithDictionary(oneLine, dict);
|
|
17
|
+
* const back = await decompressWithDictionary(packed, dict);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import type { CompressOptions, DecompressOptions } from './types.js';
|
|
21
|
+
/** Options for {@link trainDictionary}. */
|
|
22
|
+
export interface TrainOptions {
|
|
23
|
+
/** Target dictionary size in bytes (default 112 640 — zstd's own default). */
|
|
24
|
+
maxSize?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Train a zstd dictionary from representative `samples`. More, smaller samples
|
|
28
|
+
* that share structure train a better dictionary; zstd recommends at least ~100
|
|
29
|
+
* samples. Throws {@link ZipKitError} if training fails (typically too few or
|
|
30
|
+
* too-uniform samples).
|
|
31
|
+
*/
|
|
32
|
+
export declare function trainDictionary(samples: Uint8Array[], opts?: TrainOptions): Promise<Uint8Array>;
|
|
33
|
+
/** Compress `data` against `dict`. Level follows the usual zstd 1–22 scale. */
|
|
34
|
+
export declare function compressWithDictionary(data: Uint8Array, dict: Uint8Array, opts?: CompressOptions): Promise<Uint8Array>;
|
|
35
|
+
/** Decompress `data` produced by {@link compressWithDictionary} with the same `dict`. */
|
|
36
|
+
export declare function decompressWithDictionary(data: Uint8Array, dict: Uint8Array, opts?: DecompressOptions): Promise<Uint8Array>;
|
|
37
|
+
//# sourceMappingURL=dictionary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dictionary.d.ts","sourceRoot":"","sources":["../src/dictionary.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAKH,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAErE,2CAA2C;AAC3C,MAAM,WAAW,YAAY;IAC5B,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAwBrG;AAED,+EAA+E;AAC/E,wBAAsB,sBAAsB,CAC3C,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,UAAU,EAChB,IAAI,CAAC,EAAE,eAAe,GACpB,OAAO,CAAC,UAAU,CAAC,CAOrB;AAED,yFAAyF;AACzF,wBAAsB,wBAAwB,CAC7C,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,UAAU,EAChB,IAAI,CAAC,EAAE,iBAAiB,GACtB,OAAO,CAAC,UAAU,CAAC,CAMrB"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zstd dictionary compression — the big win for many small, *similar* payloads
|
|
3
|
+
* (log lines, JSON records, RPC messages, chat-history entries). Train a
|
|
4
|
+
* dictionary once from representative samples, then compress each small payload
|
|
5
|
+
* against it: the shared structure lives in the dictionary instead of being
|
|
6
|
+
* repeated in every frame, so tiny inputs shrink dramatically.
|
|
7
|
+
*
|
|
8
|
+
* Backed by the engine's `ZDICT_trainFromBuffer` + `ZSTD_*_usingDict`. The
|
|
9
|
+
* resulting frames are standard zstd-with-dictionary — interoperable with any
|
|
10
|
+
* zstd that is given the same dictionary.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { trainDictionary, compressWithDictionary, decompressWithDictionary } from '@myrialabs/zipkit';
|
|
15
|
+
* const dict = await trainDictionary(logLines); // Uint8Array[]
|
|
16
|
+
* const packed = await compressWithDictionary(oneLine, dict);
|
|
17
|
+
* const back = await decompressWithDictionary(packed, dict);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { getEngine } from './engine.js';
|
|
21
|
+
import { levelForMode, runAsync } from './internal.js';
|
|
22
|
+
import { ZipKitError } from './types.js';
|
|
23
|
+
/**
|
|
24
|
+
* Train a zstd dictionary from representative `samples`. More, smaller samples
|
|
25
|
+
* that share structure train a better dictionary; zstd recommends at least ~100
|
|
26
|
+
* samples. Throws {@link ZipKitError} if training fails (typically too few or
|
|
27
|
+
* too-uniform samples).
|
|
28
|
+
*/
|
|
29
|
+
export async function trainDictionary(samples, opts) {
|
|
30
|
+
if (samples.length === 0)
|
|
31
|
+
throw new ZipKitError('trainDictionary needs at least one sample');
|
|
32
|
+
const e = await getEngine();
|
|
33
|
+
const maxSize = opts?.maxSize ?? 112_640;
|
|
34
|
+
// Concatenate the samples and build the parallel u32-LE size table.
|
|
35
|
+
let total = 0;
|
|
36
|
+
for (const s of samples)
|
|
37
|
+
total += s.length;
|
|
38
|
+
const concat = new Uint8Array(total);
|
|
39
|
+
const sizes = new Uint8Array(samples.length * 4);
|
|
40
|
+
const sizeView = new DataView(sizes.buffer);
|
|
41
|
+
let off = 0;
|
|
42
|
+
for (let i = 0; i < samples.length; i++) {
|
|
43
|
+
concat.set(samples[i], off);
|
|
44
|
+
sizeView.setUint32(i * 4, samples[i].length, true);
|
|
45
|
+
off += samples[i].length;
|
|
46
|
+
}
|
|
47
|
+
e.setAux(sizes); // staged size table for the trainer
|
|
48
|
+
const dict = e.zstdTrainDict(concat, samples.length, maxSize);
|
|
49
|
+
if (dict.length === 0) {
|
|
50
|
+
throw new ZipKitError('Dictionary training failed — provide more, more varied samples');
|
|
51
|
+
}
|
|
52
|
+
return dict;
|
|
53
|
+
}
|
|
54
|
+
/** Compress `data` against `dict`. Level follows the usual zstd 1–22 scale. */
|
|
55
|
+
export async function compressWithDictionary(data, dict, opts) {
|
|
56
|
+
const e = await getEngine();
|
|
57
|
+
const level = levelForMode(opts, 1, 22, { speed: 3, balanced: 19, ratio: 22 });
|
|
58
|
+
return runAsync(() => {
|
|
59
|
+
e.setAux(dict);
|
|
60
|
+
return e.zstdCompressDict(data, level);
|
|
61
|
+
}, opts, data.length);
|
|
62
|
+
}
|
|
63
|
+
/** Decompress `data` produced by {@link compressWithDictionary} with the same `dict`. */
|
|
64
|
+
export async function decompressWithDictionary(data, dict, opts) {
|
|
65
|
+
const e = await getEngine();
|
|
66
|
+
return runAsync(() => {
|
|
67
|
+
e.setAux(dict);
|
|
68
|
+
return e.zstdDecompressDict(data);
|
|
69
|
+
}, opts, data.length);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=dictionary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dictionary.js","sourceRoot":"","sources":["../src/dictionary.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AASzC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,OAAqB,EAAE,IAAmB;IAC/E,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,WAAW,CAAC,2CAA2C,CAAC,CAAC;IAC7F,MAAM,CAAC,GAAG,MAAM,SAAS,EAAE,CAAC;IAC5B,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,OAAO,CAAC;IAEzC,oEAAoE;IACpE,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,CAAC,IAAI,OAAO;QAAE,KAAK,IAAI,CAAC,CAAC,MAAM,CAAC;IAC3C,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,IAAI,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I,CAAC,CAAC;QACf,OAAO,CAAC,CAAC,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;AACvB,CAAC;AAED,yFAAyF;AACzF,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC7C,IAAgB,EAChB,IAAgB,EAChB,IAAwB;IAExB,MAAM,CAAC,GAAG,MAAM,SAAS,EAAE,CAAC;IAC5B,OAAO,QAAQ,CAAC,GAAG,EAAE;QACpB,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACf,OAAO,CAAC,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;AACvB,CAAC"}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loader for the unified ZipKit Wasm engine (Emscripten ES module).
|
|
3
|
+
*
|
|
4
|
+
* One module, all codecs: gzip/deflate/zlib (libdeflate), lz4, zstd (+ ultra),
|
|
5
|
+
* brotli, snappy, lzma, bzip2, qoi (image), frame-delta (video). The ABI is
|
|
6
|
+
* deliberately tiny — persistent buffers, one copy in and one copy out per
|
|
7
|
+
* call, no allocation crossing the JS/Wasm boundary.
|
|
8
|
+
*
|
|
9
|
+
* The engine is the low-level escape hatch. Most users want the high-level
|
|
10
|
+
* {@link import('./zipkit.js').ZipKit} class or the named codec helpers from
|
|
11
|
+
* the package root; reach for `ZipKitEngine` only when you need raw codec
|
|
12
|
+
* access without runtime dispatch.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { ZipKitEngine } from '@myrialabs/zipkit/engine';
|
|
17
|
+
* const engine = await ZipKitEngine.load();
|
|
18
|
+
* const packed = engine.zstdCompress(bytes, 19);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* The raw Wasm engine: every codec exposed as a synchronous
|
|
23
|
+
* `Uint8Array -> Uint8Array` method. Construct it with the async
|
|
24
|
+
* {@link ZipKitEngine.load} factory (Wasm instantiation is async), or call
|
|
25
|
+
* {@link getEngine} for a process-wide lazily-instantiated singleton.
|
|
26
|
+
*/
|
|
27
|
+
export declare class ZipKitEngine {
|
|
28
|
+
private m;
|
|
29
|
+
private constructor();
|
|
30
|
+
/** Instantiate a fresh engine. Prefer {@link getEngine} to avoid reloading. */
|
|
31
|
+
static load(): Promise<ZipKitEngine>;
|
|
32
|
+
private call;
|
|
33
|
+
gzipCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
34
|
+
gzipDecompress(d: Uint8Array): Uint8Array;
|
|
35
|
+
deflateCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
36
|
+
deflateDecompress(d: Uint8Array): Uint8Array;
|
|
37
|
+
zlibCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
38
|
+
zlibDecompress(d: Uint8Array): Uint8Array;
|
|
39
|
+
/**
|
|
40
|
+
* CRC-32 (IEEE 802.3) of `d`, optionally continuing from a prior `seed`.
|
|
41
|
+
* Returns the checksum directly — libdeflate's SIMD path, far faster than a
|
|
42
|
+
* byte-at-a-time table. Used by the ZIP container.
|
|
43
|
+
*/
|
|
44
|
+
crc32(d: Uint8Array, seed?: number): number;
|
|
45
|
+
lz4Compress(d: Uint8Array): Uint8Array;
|
|
46
|
+
lz4Decompress(d: Uint8Array): Uint8Array;
|
|
47
|
+
zstdCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
48
|
+
zstdDecompress(d: Uint8Array): Uint8Array;
|
|
49
|
+
brotliCompress(d: Uint8Array, quality?: number): Uint8Array;
|
|
50
|
+
brotliDecompress(d: Uint8Array): Uint8Array;
|
|
51
|
+
snappyCompress(d: Uint8Array): Uint8Array;
|
|
52
|
+
snappyDecompress(d: Uint8Array): Uint8Array;
|
|
53
|
+
/** Max-ratio zstd (ultra + long-distance matching), up to level 22. */
|
|
54
|
+
zstdMaxCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
55
|
+
/** LZMA — highest general-purpose ratio. */
|
|
56
|
+
lzmaCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
57
|
+
lzmaDecompress(d: Uint8Array): Uint8Array;
|
|
58
|
+
/**
|
|
59
|
+
* Decode a raw LZMA2 stream (used by the 7z reader). `prop` is the single
|
|
60
|
+
* LZMA2 dictionary-size property byte; `outSize` is the known unpacked size.
|
|
61
|
+
*/
|
|
62
|
+
lzma2Decompress(d: Uint8Array, prop: number, outSize: number): Uint8Array;
|
|
63
|
+
/** xz — the standard `.xz` container around LZMA2 (full streaming codec). */
|
|
64
|
+
xzCompress(d: Uint8Array, level?: number): Uint8Array;
|
|
65
|
+
xzDecompress(d: Uint8Array): Uint8Array;
|
|
66
|
+
/**
|
|
67
|
+
* Stage `dict` into the engine's auxiliary buffer for the next dictionary
|
|
68
|
+
* call. Held until the next {@link setAux}, so set it immediately before
|
|
69
|
+
* {@link zstdCompressDict}/{@link zstdDecompressDict} or {@link zstdTrainDict}.
|
|
70
|
+
*/
|
|
71
|
+
setAux(dict: Uint8Array): void;
|
|
72
|
+
/**
|
|
73
|
+
* Train a zstd dictionary of up to `dictCapacity` bytes from `samples`. Stage
|
|
74
|
+
* the per-sample sizes (u32 LE) via {@link setAux} first; `samples` is the
|
|
75
|
+
* concatenation of every sample. Returns the dictionary bytes (empty on
|
|
76
|
+
* failure, e.g. too few samples).
|
|
77
|
+
*/
|
|
78
|
+
zstdTrainDict(samples: Uint8Array, nSamples: number, dictCapacity: number): Uint8Array;
|
|
79
|
+
/** Compress `d` with the dictionary previously staged via {@link setAux}. */
|
|
80
|
+
zstdCompressDict(d: Uint8Array, level?: number): Uint8Array;
|
|
81
|
+
/** Decompress `d` with the dictionary previously staged via {@link setAux}. */
|
|
82
|
+
zstdDecompressDict(d: Uint8Array): Uint8Array;
|
|
83
|
+
/** bzip2 — Burrows–Wheeler transform. */
|
|
84
|
+
bzip2Compress(d: Uint8Array, level?: number): Uint8Array;
|
|
85
|
+
bzip2Decompress(d: Uint8Array): Uint8Array;
|
|
86
|
+
/** QOI — lossless image (raw RGB/RGBA pixels in, QOI bytes out). */
|
|
87
|
+
qoiEncode(pixels: Uint8Array, width: number, height: number, channels: number): Uint8Array;
|
|
88
|
+
qoiDecode(d: Uint8Array): Uint8Array;
|
|
89
|
+
/**
|
|
90
|
+
* Frame-delta — lossless temporal predictor for video-like streams. Pair
|
|
91
|
+
* `encode -> zstd/lz4` on the way in and `lz4/zstd -> decode` on the way out.
|
|
92
|
+
*/
|
|
93
|
+
frameDeltaEncode(d: Uint8Array, frameSize: number): Uint8Array;
|
|
94
|
+
frameDeltaDecode(d: Uint8Array, frameSize: number): Uint8Array;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Return the process-wide engine, instantiating the Wasm module on first call
|
|
98
|
+
* and reusing it thereafter. This is what every high-level helper uses, so the
|
|
99
|
+
* 1.4 MB module loads at most once per process.
|
|
100
|
+
*/
|
|
101
|
+
export declare function getEngine(): Promise<ZipKitEngine>;
|
|
102
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AA4CH;;;;;GAKG;AACH,qBAAa,YAAY;IACxB,OAAO,CAAC,CAAC,CAAM;IACf,OAAO;IAIP,+EAA+E;WAClE,IAAI,IAAI,OAAO,CAAC,YAAY,CAAC;IAK1C,OAAO,CAAC,IAAI;IAYZ,YAAY,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,SAAI,GAAG,UAAU;IAGlD,cAAc,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU;IAGzC,eAAe,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,SAAI,GAAG,UAAU;IAGrD,iBAAiB,CA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AE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,UAAU;IAUtF,6EAA6E;IAC7E,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,SAAK,GAAG,UAAU;IAGvD,+EAA+E;IAC/E,kBAAkB,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU;IAI7C,yCAAyC;IACzC,aAAa,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,SAAI,GAAG,UAAU;IAGnD,eAAe,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU;IAI1C,oEAAoE;IACpE,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU;IAG1F,SAAS,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU;IAIpC;;;OAGG;IACH,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU;IAG9D,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU;CAG9D;AAID;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,OAAO,CAAC,YAAY,CAAC,CAEjD"}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loader for the unified ZipKit Wasm engine (Emscripten ES module).
|
|
3
|
+
*
|
|
4
|
+
* One module, all codecs: gzip/deflate/zlib (libdeflate), lz4, zstd (+ ultra),
|
|
5
|
+
* brotli, snappy, lzma, bzip2, qoi (image), frame-delta (video). The ABI is
|
|
6
|
+
* deliberately tiny — persistent buffers, one copy in and one copy out per
|
|
7
|
+
* call, no allocation crossing the JS/Wasm boundary.
|
|
8
|
+
*
|
|
9
|
+
* The engine is the low-level escape hatch. Most users want the high-level
|
|
10
|
+
* {@link import('./zipkit.js').ZipKit} class or the named codec helpers from
|
|
11
|
+
* the package root; reach for `ZipKitEngine` only when you need raw codec
|
|
12
|
+
* access without runtime dispatch.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { ZipKitEngine } from '@myrialabs/zipkit/engine';
|
|
17
|
+
* const engine = await ZipKitEngine.load();
|
|
18
|
+
* const packed = engine.zstdCompress(bytes, 19);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import ZipKitModule from '../engine/dist/zipkit-engine.mjs';
|
|
22
|
+
/**
|
|
23
|
+
* The raw Wasm engine: every codec exposed as a synchronous
|
|
24
|
+
* `Uint8Array -> Uint8Array` method. Construct it with the async
|
|
25
|
+
* {@link ZipKitEngine.load} factory (Wasm instantiation is async), or call
|
|
26
|
+
* {@link getEngine} for a process-wide lazily-instantiated singleton.
|
|
27
|
+
*/
|
|
28
|
+
export class ZipKitEngine {
|
|
29
|
+
m;
|
|
30
|
+
constructor(m) {
|
|
31
|
+
this.m = m;
|
|
32
|
+
}
|
|
33
|
+
/** Instantiate a fresh engine. Prefer {@link getEngine} to avoid reloading. */
|
|
34
|
+
static async load() {
|
|
35
|
+
const m = (await ZipKitModule());
|
|
36
|
+
return new ZipKitEngine(m);
|
|
37
|
+
}
|
|
38
|
+
call(fn, data) {
|
|
39
|
+
const m = this.m;
|
|
40
|
+
const p = m._zk_input_ptr(data.length);
|
|
41
|
+
m.HEAPU8.set(data, p);
|
|
42
|
+
fn(data.length);
|
|
43
|
+
const op = m._zk_result_ptr();
|
|
44
|
+
const ol = m._zk_result_len() >>> 0;
|
|
45
|
+
// slice() copies out of the (possibly growable) Wasm heap into a
|
|
46
|
+
// standalone buffer the caller owns.
|
|
47
|
+
return m.HEAPU8.slice(op, op + ol);
|
|
48
|
+
}
|
|
49
|
+
gzipCompress(d, level = 9) {
|
|
50
|
+
return this.call((l) => this.m._zk_gzip_compress(l, level), d);
|
|
51
|
+
}
|
|
52
|
+
gzipDecompress(d) {
|
|
53
|
+
return this.call((l) => this.m._zk_gzip_decompress(l), d);
|
|
54
|
+
}
|
|
55
|
+
deflateCompress(d, level = 9) {
|
|
56
|
+
return this.call((l) => this.m._zk_deflate_compress(l, level), d);
|
|
57
|
+
}
|
|
58
|
+
deflateDecompress(d) {
|
|
59
|
+
return this.call((l) => this.m._zk_deflate_decompress(l, 0), d);
|
|
60
|
+
}
|
|
61
|
+
zlibCompress(d, level = 9) {
|
|
62
|
+
return this.call((l) => this.m._zk_zlib_compress(l, level), d);
|
|
63
|
+
}
|
|
64
|
+
zlibDecompress(d) {
|
|
65
|
+
return this.call((l) => this.m._zk_zlib_decompress(l, 0), d);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* CRC-32 (IEEE 802.3) of `d`, optionally continuing from a prior `seed`.
|
|
69
|
+
* Returns the checksum directly — libdeflate's SIMD path, far faster than a
|
|
70
|
+
* byte-at-a-time table. Used by the ZIP container.
|
|
71
|
+
*/
|
|
72
|
+
crc32(d, seed = 0) {
|
|
73
|
+
const m = this.m;
|
|
74
|
+
const p = m._zk_input_ptr(d.length);
|
|
75
|
+
m.HEAPU8.set(d, p);
|
|
76
|
+
return m._zk_crc32(d.length, seed >>> 0) >>> 0;
|
|
77
|
+
}
|
|
78
|
+
lz4Compress(d) {
|
|
79
|
+
return this.call((l) => this.m._zk_lz4_compress(l), d);
|
|
80
|
+
}
|
|
81
|
+
lz4Decompress(d) {
|
|
82
|
+
return this.call((l) => this.m._zk_lz4_decompress(l), d);
|
|
83
|
+
}
|
|
84
|
+
zstdCompress(d, level = 19) {
|
|
85
|
+
return this.call((l) => this.m._zk_zstd_compress(l, level), d);
|
|
86
|
+
}
|
|
87
|
+
zstdDecompress(d) {
|
|
88
|
+
return this.call((l) => this.m._zk_zstd_decompress(l), d);
|
|
89
|
+
}
|
|
90
|
+
brotliCompress(d, quality = 11) {
|
|
91
|
+
return this.call((l) => this.m._zk_brotli_compress(l, quality), d);
|
|
92
|
+
}
|
|
93
|
+
brotliDecompress(d) {
|
|
94
|
+
return this.call((l) => this.m._zk_brotli_decompress(l), d);
|
|
95
|
+
}
|
|
96
|
+
snappyCompress(d) {
|
|
97
|
+
return this.call((l) => this.m._zk_snappy_compress(l), d);
|
|
98
|
+
}
|
|
99
|
+
snappyDecompress(d) {
|
|
100
|
+
return this.call((l) => this.m._zk_snappy_decompress(l), d);
|
|
101
|
+
}
|
|
102
|
+
/** Max-ratio zstd (ultra + long-distance matching), up to level 22. */
|
|
103
|
+
zstdMaxCompress(d, level = 22) {
|
|
104
|
+
return this.call((l) => this.m._zk_zstd_max_compress(l, level), d);
|
|
105
|
+
}
|
|
106
|
+
// (decompress with zstdDecompress — same frame format)
|
|
107
|
+
/** LZMA — highest general-purpose ratio. */
|
|
108
|
+
lzmaCompress(d, level = 9) {
|
|
109
|
+
return this.call((l) => this.m._zk_lzma_compress(l, level), d);
|
|
110
|
+
}
|
|
111
|
+
lzmaDecompress(d) {
|
|
112
|
+
return this.call((l) => this.m._zk_lzma_decompress(l), d);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Decode a raw LZMA2 stream (used by the 7z reader). `prop` is the single
|
|
116
|
+
* LZMA2 dictionary-size property byte; `outSize` is the known unpacked size.
|
|
117
|
+
*/
|
|
118
|
+
lzma2Decompress(d, prop, outSize) {
|
|
119
|
+
return this.call((l) => this.m._zk_lzma2_decompress(l, prop, outSize), d);
|
|
120
|
+
}
|
|
121
|
+
/** xz — the standard `.xz` container around LZMA2 (full streaming codec). */
|
|
122
|
+
xzCompress(d, level = 6) {
|
|
123
|
+
const out = this.call((l) => this.m._zk_xz_compress(l, level), d);
|
|
124
|
+
if (!this.m._zk_xz_ok())
|
|
125
|
+
throw new Error('xz compression failed');
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
xzDecompress(d) {
|
|
129
|
+
// An empty result is valid (a stream can decode to zero bytes), so success
|
|
130
|
+
// is read from the engine's explicit flag, not the output length.
|
|
131
|
+
const out = this.call((l) => this.m._zk_xz_decompress(l), d);
|
|
132
|
+
if (!this.m._zk_xz_ok())
|
|
133
|
+
throw new Error('xz decompression failed (corrupt stream or unsupported filter)');
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Stage `dict` into the engine's auxiliary buffer for the next dictionary
|
|
138
|
+
* call. Held until the next {@link setAux}, so set it immediately before
|
|
139
|
+
* {@link zstdCompressDict}/{@link zstdDecompressDict} or {@link zstdTrainDict}.
|
|
140
|
+
*/
|
|
141
|
+
setAux(dict) {
|
|
142
|
+
const m = this.m;
|
|
143
|
+
const p = m._zk_input_ptr(dict.length);
|
|
144
|
+
m.HEAPU8.set(dict, p);
|
|
145
|
+
m._zk_set_aux(dict.length);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Train a zstd dictionary of up to `dictCapacity` bytes from `samples`. Stage
|
|
149
|
+
* the per-sample sizes (u32 LE) via {@link setAux} first; `samples` is the
|
|
150
|
+
* concatenation of every sample. Returns the dictionary bytes (empty on
|
|
151
|
+
* failure, e.g. too few samples).
|
|
152
|
+
*/
|
|
153
|
+
zstdTrainDict(samples, nSamples, dictCapacity) {
|
|
154
|
+
const m = this.m;
|
|
155
|
+
const p = m._zk_input_ptr(samples.length);
|
|
156
|
+
m.HEAPU8.set(samples, p);
|
|
157
|
+
m._zk_zstd_train_dict(samples.length, nSamples, dictCapacity);
|
|
158
|
+
const op = m._zk_result_ptr();
|
|
159
|
+
const ol = m._zk_result_len() >>> 0;
|
|
160
|
+
return m.HEAPU8.slice(op, op + ol);
|
|
161
|
+
}
|
|
162
|
+
/** Compress `d` with the dictionary previously staged via {@link setAux}. */
|
|
163
|
+
zstdCompressDict(d, level = 19) {
|
|
164
|
+
return this.call((l) => this.m._zk_zstd_compress_dict(l, level), d);
|
|
165
|
+
}
|
|
166
|
+
/** Decompress `d` with the dictionary previously staged via {@link setAux}. */
|
|
167
|
+
zstdDecompressDict(d) {
|
|
168
|
+
return this.call((l) => this.m._zk_zstd_decompress_dict(l), d);
|
|
169
|
+
}
|
|
170
|
+
/** bzip2 — Burrows–Wheeler transform. */
|
|
171
|
+
bzip2Compress(d, level = 9) {
|
|
172
|
+
return this.call((l) => this.m._zk_bzip2_compress(l, level), d);
|
|
173
|
+
}
|
|
174
|
+
bzip2Decompress(d) {
|
|
175
|
+
return this.call((l) => this.m._zk_bzip2_decompress(l), d);
|
|
176
|
+
}
|
|
177
|
+
/** QOI — lossless image (raw RGB/RGBA pixels in, QOI bytes out). */
|
|
178
|
+
qoiEncode(pixels, width, height, channels) {
|
|
179
|
+
return this.call((l) => this.m._zk_qoi_encode(l, width, height, channels), pixels);
|
|
180
|
+
}
|
|
181
|
+
qoiDecode(d) {
|
|
182
|
+
return this.call((l) => this.m._zk_qoi_decode(l), d);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Frame-delta — lossless temporal predictor for video-like streams. Pair
|
|
186
|
+
* `encode -> zstd/lz4` on the way in and `lz4/zstd -> decode` on the way out.
|
|
187
|
+
*/
|
|
188
|
+
frameDeltaEncode(d, frameSize) {
|
|
189
|
+
return this.call((l) => this.m._zk_frame_delta_encode(l, frameSize), d);
|
|
190
|
+
}
|
|
191
|
+
frameDeltaDecode(d, frameSize) {
|
|
192
|
+
return this.call((l) => this.m._zk_frame_delta_decode(l, frameSize), d);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
let singleton;
|
|
196
|
+
/**
|
|
197
|
+
* Return the process-wide engine, instantiating the Wasm module on first call
|
|
198
|
+
* and reusing it thereafter. This is what every high-level helper uses, so the
|
|
199
|
+
* 1.4 MB module loads at most once per process.
|
|
200
|
+
*/
|
|
201
|
+
export function getEngine() {
|
|
202
|
+
return (singleton ??= ZipKitEngine.load());
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;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I,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;IACpF,CAAC;IACD,SAAS,CAAC,CAAa;QACtB,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACtD,CAAC;IAED;;;OAGG;IACH,gBAAgB,CAAC,CAAa,EAAE,SAAiB;QAChD,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;IACzE,CAAC;IACD,gBAAgB,CAAC,CAAa,EAAE,SAAiB;QAChD,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;IACzE,CAAC;CACD;AAED,IAAI,SAA4C,CAAC;AAEjD;;;;GAIG;AACH,MAAM,UAAU,SAAS;IACxB,OAAO,CAAC,SAAS,KAAK,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;AAC5C,CAAC"}
|
package/dist/fsa.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File System Access API helpers (browser).
|
|
3
|
+
*
|
|
4
|
+
* Bridges the browser's `FileSystemFileHandle` to ZipKit's streaming writer so
|
|
5
|
+
* you can zip large local files straight to disk without first reading them all
|
|
6
|
+
* into memory: {@link zipToFileHandle} pipes {@link zipStream} into the handle's
|
|
7
|
+
* writable, and {@link entriesFromFileHandles} turns picked files into the entry
|
|
8
|
+
* stream it consumes.
|
|
9
|
+
*
|
|
10
|
+
* The handle shapes are typed structurally (not via the DOM lib), so this module
|
|
11
|
+
* imports cleanly in Node/Bun too — it just needs real handles at call time.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { zipToFileHandle, entriesFromFileHandles } from '@myrialabs/zipkit/fsa';
|
|
16
|
+
* const out = await window.showSaveFilePicker({ suggestedName: 'archive.zip' });
|
|
17
|
+
* const picked = await window.showOpenFilePicker({ multiple: true });
|
|
18
|
+
* await zipToFileHandle(out, entriesFromFileHandles(picked, h => h));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { type ZipStreamOptions } from './zip/index.js';
|
|
22
|
+
import type { ZipEntryInput, ZipMethod } from './zip/index.js';
|
|
23
|
+
/** The slice of `File` ZipKit reads. */
|
|
24
|
+
export interface FileLike {
|
|
25
|
+
name: string;
|
|
26
|
+
lastModified: number;
|
|
27
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
28
|
+
}
|
|
29
|
+
/** The slice of `FileSystemFileHandle` ZipKit reads from. */
|
|
30
|
+
export interface ReadableFileHandle {
|
|
31
|
+
getFile(): Promise<FileLike>;
|
|
32
|
+
}
|
|
33
|
+
/** The slice of `FileSystemFileHandle` ZipKit writes to. */
|
|
34
|
+
export interface WritableFileHandle {
|
|
35
|
+
createWritable(): Promise<WritableStream<Uint8Array>>;
|
|
36
|
+
}
|
|
37
|
+
/** How to name and compress an entry derived from a file handle. */
|
|
38
|
+
export interface FileHandleEntryOptions {
|
|
39
|
+
/** Archive path for the file (default: the file's own `name`). */
|
|
40
|
+
name?: (file: FileLike) => string;
|
|
41
|
+
/** Compression method (default `'deflate'`). */
|
|
42
|
+
method?: ZipMethod;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Turn picked file handles into the entry stream {@link zipStream} consumes,
|
|
46
|
+
* reading each file only when its turn comes (peak memory = one file at a time).
|
|
47
|
+
*/
|
|
48
|
+
export declare function entriesFromFileHandles(handles: Iterable<ReadableFileHandle>, opts?: FileHandleEntryOptions): AsyncGenerator<ZipEntryInput>;
|
|
49
|
+
/**
|
|
50
|
+
* Stream a ZIP archive built from `entries` straight into a `FileSystemFileHandle`
|
|
51
|
+
* obtained via `showSaveFilePicker`. The archive is never fully buffered.
|
|
52
|
+
*/
|
|
53
|
+
export declare function zipToFileHandle(handle: WritableFileHandle, entries: Iterable<ZipEntryInput> | AsyncIterable<ZipEntryInput>, opts?: ZipStreamOptions): Promise<void>;
|
|
54
|
+
//# sourceMappingURL=fsa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fsa.d.ts","sourceRoot":"","sources":["../src/fsa.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE/D,wCAAwC;AACxC,MAAM,WAAW,QAAQ;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;CACpC;AAED,6DAA6D;AAC7D,MAAM,WAAW,kBAAkB;IAClC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC7B;AAED,4DAA4D;AAC5D,MAAM,WAAW,kBAAkB;IAClC,cAAc,IAAI,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC;CACtD;AAED,oEAAoE;AACpE,MAAM,WAAW,sBAAsB;IACtC,kEAAkE;IAClE,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,MAAM,CAAC;IAClC,gDAAgD;IAChD,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAED;;;GAGG;AACH,wBAAuB,sBAAsB,CAC5C,OAAO,EAAE,QAAQ,CAAC,kBAAkB,CAAC,EACrC,IAAI,GAAE,sBAA2B,GAC/B,cAAc,CAAC,aAAa,CAAC,CAU/B;AAED;;;GAGG;AACH,wBAAsB,eAAe,CACpC,MAAM,EAAE,kBAAkB,EAC1B,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,GAAG,aAAa,CAAC,aAAa,CAAC,EAC/D,IAAI,CAAC,EAAE,gBAAgB,GACrB,OAAO,CAAC,IAAI,CAAC,CAGf"}
|
package/dist/fsa.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File System Access API helpers (browser).
|
|
3
|
+
*
|
|
4
|
+
* Bridges the browser's `FileSystemFileHandle` to ZipKit's streaming writer so
|
|
5
|
+
* you can zip large local files straight to disk without first reading them all
|
|
6
|
+
* into memory: {@link zipToFileHandle} pipes {@link zipStream} into the handle's
|
|
7
|
+
* writable, and {@link entriesFromFileHandles} turns picked files into the entry
|
|
8
|
+
* stream it consumes.
|
|
9
|
+
*
|
|
10
|
+
* The handle shapes are typed structurally (not via the DOM lib), so this module
|
|
11
|
+
* imports cleanly in Node/Bun too — it just needs real handles at call time.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { zipToFileHandle, entriesFromFileHandles } from '@myrialabs/zipkit/fsa';
|
|
16
|
+
* const out = await window.showSaveFilePicker({ suggestedName: 'archive.zip' });
|
|
17
|
+
* const picked = await window.showOpenFilePicker({ multiple: true });
|
|
18
|
+
* await zipToFileHandle(out, entriesFromFileHandles(picked, h => h));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { zipStream } from './zip/index.js';
|
|
22
|
+
/**
|
|
23
|
+
* Turn picked file handles into the entry stream {@link zipStream} consumes,
|
|
24
|
+
* reading each file only when its turn comes (peak memory = one file at a time).
|
|
25
|
+
*/
|
|
26
|
+
export async function* entriesFromFileHandles(handles, opts = {}) {
|
|
27
|
+
for (const handle of handles) {
|
|
28
|
+
const file = await handle.getFile();
|
|
29
|
+
yield {
|
|
30
|
+
name: opts.name ? opts.name(file) : file.name,
|
|
31
|
+
data: new Uint8Array(await file.arrayBuffer()),
|
|
32
|
+
method: opts.method,
|
|
33
|
+
mtime: file.lastModified
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Stream a ZIP archive built from `entries` straight into a `FileSystemFileHandle`
|
|
39
|
+
* obtained via `showSaveFilePicker`. The archive is never fully buffered.
|
|
40
|
+
*/
|
|
41
|
+
export async function zipToFileHandle(handle, entries, opts) {
|
|
42
|
+
const writable = await handle.createWritable();
|
|
43
|
+
await zipStream(entries, opts).pipeTo(writable);
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=fsa.js.map
|
package/dist/fsa.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fsa.js","sourceRoot":"","sources":["../src/fsa.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,SAAS,EAAyB,MAAM,gBAAgB,CAAC;AA4BlE;;;GAGG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,sBAAsB,CAC5C,OAAqC,EACrC,OAA+B,EAAE;IAEjC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC9B,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;QACpC,MAAM;YACL,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI;YAC7C,IAAI,EAAE,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;YAC9C,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,KAAK,EAAE,IAAI,CAAC,YAAY;SACxB,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,MAA0B,EAC1B,OAA+D,EAC/D,IAAuB;IAEvB,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,cAAc,EAAE,CAAC;IAC/C,MAAM,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACjD,CAAC"}
|