@cloudpss/compress 0.3.0-alpha.21 → 0.3.0-alpha.22

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.
Files changed (47) hide show
  1. package/README.md +25 -22
  2. package/benchmark.js +15 -12
  3. package/dist/zstd/index-browser.d.ts +2 -4
  4. package/dist/zstd/index-browser.js +2 -23
  5. package/dist/zstd/index-browser.js.map +1 -1
  6. package/dist/zstd/wasm/compress.d.ts +2 -0
  7. package/dist/zstd/wasm/compress.js +28 -0
  8. package/dist/zstd/wasm/compress.js.map +1 -0
  9. package/dist/zstd/wasm/decompress.d.ts +6 -0
  10. package/dist/zstd/wasm/decompress.js +30 -0
  11. package/dist/zstd/wasm/decompress.js.map +1 -0
  12. package/dist/zstd/wasm/module.d.ts +2 -0
  13. package/dist/zstd/wasm/module.js +4 -0
  14. package/dist/zstd/wasm/module.js.map +1 -0
  15. package/package.json +4 -3
  16. package/src/zstd/index-browser.ts +2 -26
  17. package/src/zstd/wasm/compress.ts +27 -0
  18. package/src/zstd/wasm/decompress.ts +37 -0
  19. package/src/zstd/wasm/module.ts +5 -0
  20. package/tsconfig.json +1 -2
  21. package/wasm/zstd.g.d.ts +16 -0
  22. package/wasm/zstd.g.js +29 -0
  23. package/dist/zstd/lib/errors/index.d.ts +0 -1
  24. package/dist/zstd/lib/errors/index.js +0 -12
  25. package/dist/zstd/lib/errors/index.js.map +0 -1
  26. package/dist/zstd/lib/index.node.d.ts +0 -3
  27. package/dist/zstd/lib/index.node.js +0 -31
  28. package/dist/zstd/lib/index.node.js.map +0 -1
  29. package/dist/zstd/lib/index.web.d.ts +0 -3
  30. package/dist/zstd/lib/index.web.js +0 -30
  31. package/dist/zstd/lib/index.web.js.map +0 -1
  32. package/dist/zstd/lib/module.d.ts +0 -3
  33. package/dist/zstd/lib/module.js +0 -29
  34. package/dist/zstd/lib/module.js.map +0 -1
  35. package/dist/zstd/lib/simple/compress.d.ts +0 -1
  36. package/dist/zstd/lib/simple/compress.js +0 -42
  37. package/dist/zstd/lib/simple/compress.js.map +0 -1
  38. package/dist/zstd/lib/simple/decompress.d.ts +0 -3
  39. package/dist/zstd/lib/simple/decompress.js +0 -42
  40. package/dist/zstd/lib/simple/decompress.js.map +0 -1
  41. package/dist/zstd/lib/wasm/zstd.d.ts +0 -2
  42. package/dist/zstd/lib/wasm/zstd.js +0 -324
  43. package/dist/zstd/lib/wasm/zstd.js.map +0 -1
  44. package/dist/zstd/lib/wasm/zstd.wasm.d.ts +0 -1
  45. package/dist/zstd/lib/wasm/zstd.wasm.js +0 -2
  46. package/dist/zstd/lib/wasm/zstd.wasm.js.map +0 -1
  47. package/update-zstd.ps1 +0 -11
package/README.md CHANGED
@@ -19,28 +19,31 @@ Be carful when handling binary files since their data might be confusing with ma
19
19
  yarn run v1.22.15
20
20
  $ node ./benchmark
21
21
  File: 四川-PSS独立-直流等值机_Main.cproj Raw: 12.1 MiB
22
- gzip -d (wasm): 13.03(953 kiB) 374.37ms 196.20ms
23
- gzip -d (node): 12.65(981 kiB) 117.95ms 16.82ms
24
- gzip -d (pako): 12.65(981 kiB) 418.58ms 111.92ms
25
- zstd -4 (node): 17.37(714 kiB) 29.68ms 10.68ms
26
- zstd -3 (wasm): 17.40(713 kiB) 53.05ms 22.62ms
27
- br -3 (node): 18.32(677 kiB) 43.51ms 20.77ms
28
- br -1 (wasm): 3.90(3.11 MiB) 466.51ms 188.56ms
22
+ gzip -d (wasm): 13.03(953 kiB) 371.84ms 193.38ms
23
+ gzip -d (node): 12.65(981 kiB) 115.73ms 17.17ms
24
+ gzip -d (pako): 12.65(981 kiB) 381.96ms 113.91ms
25
+ zstd -4 (node): 17.37(714 kiB) 25.31ms 15.97ms
26
+ zstd -3 (Oz,wasm): 17.40(713 kiB) 53.01ms 24.49ms
27
+ zstd -3 (O3,wasm): 17.40(713 kiB) 30.39ms 16.01ms
28
+ br -3 (node): 18.32(677 kiB) 49.92ms 18.48ms
29
+ br -1 (wasm): 3.90(3.11 MiB) 450.04ms 171.55ms
29
30
  File: 电磁暂态方案 1_参数方案 1 (2).cjob Raw: 8.76 MiB
30
- gzip -d (wasm): 3.69(2.37 MiB) 702.11ms 204.35ms
31
- gzip -d (node): 3.68(2.38 MiB) 194.88ms 17.89ms
32
- gzip -d (pako): 3.68(2.38 MiB) 486.53ms 96.60ms
33
- zstd -4 (node): 3.45(2.54 MiB) 64.77ms 15.52ms
34
- zstd -3 (wasm): 3.47(2.52 MiB) 118.55ms 41.77ms
35
- br -3 (node): 3.63(2.41 MiB) 86.90ms 26.12ms
36
- br -1 (wasm): 2.92(3 MiB) 432.82ms 163.89ms
31
+ gzip -d (wasm): 3.69(2.37 MiB) 661.18ms 182.66ms
32
+ gzip -d (node): 3.68(2.38 MiB) 197.01ms 18.04ms
33
+ gzip -d (pako): 3.68(2.38 MiB) 456.53ms 101.31ms
34
+ zstd -4 (node): 3.45(2.54 MiB) 66.06ms 15.66ms
35
+ zstd -3 (Oz,wasm): 3.47(2.52 MiB) 119.57ms 36.27ms
36
+ zstd -3 (O3,wasm): 3.47(2.52 MiB) 66.22ms 18.07ms
37
+ br -3 (node): 3.63(2.41 MiB) 84.41ms 24.57ms
38
+ br -1 (wasm): 2.92(3 MiB) 396.91ms 168.17ms
37
39
  File: 电磁暂态方案 1_参数方案 1.cjob Raw: 24.1 MiB
38
- gzip -d (wasm): 4.18(5.77 MiB) 1379.40ms 460.05ms
39
- gzip -d (node): 4.17(5.79 MiB) 269.66ms 54.18ms
40
- gzip -d (pako): 4.17(5.79 MiB) 901.92ms 201.63ms
41
- zstd -4 (node): 4.29(5.62 MiB) 142.61ms 36.44ms
42
- zstd -3 (wasm): 4.26(5.65 MiB) 246.08ms 60.73ms
43
- br -3 (node): 4.25(5.67 MiB) 166.58ms 56.52ms
44
- br -1 (wasm): 3.05(7.91 MiB) 1032.87ms 406.81ms
45
- Done in 11.15s.
40
+ gzip -d (wasm): 4.18(5.77 MiB) 1314.32ms 455.43ms
41
+ gzip -d (node): 4.17(5.79 MiB) 254.84ms 47.28ms
42
+ gzip -d (pako): 4.17(5.79 MiB) 793.62ms 204.64ms
43
+ zstd -4 (node): 4.29(5.62 MiB) 232.69ms 38.53ms
44
+ zstd -3 (Oz,wasm): 4.26(5.65 MiB) 230.65ms 60.13ms
45
+ zstd -3 (O3,wasm): 4.26(5.65 MiB) 155.36ms 29.62ms
46
+ br -3 (node): 4.25(5.67 MiB) 167.06ms 52.79ms
47
+ br -1 (wasm): 3.05(7.91 MiB) 981.26ms 391.41ms
48
+ Done in 11.43s.
46
49
  ```
package/benchmark.js CHANGED
@@ -1,18 +1,20 @@
1
+ import prettyBytes from 'pretty-bytes';
2
+ import wasmFlate from 'wasm-flate';
3
+ import zlib from 'zlib';
4
+ import zstd from '@bokuweb/zstd-wasm';
5
+ import * as myZstd from './dist/zstd/index-browser.js';
6
+ import cppzst from '@xingrz/cppzst';
7
+ import pako from 'pako';
8
+ import brotli from 'brotli-wasm';
9
+ import files from '../../benchmark-files/index.js';
10
+
11
+ const pb = (size) => prettyBytes(size, { binary: true });
12
+ const t = (time) => time.toFixed(2) + 'ms';
13
+
1
14
  /* eslint-disable no-console */
2
15
  (async function () {
3
- const prettyBytes = require('pretty-bytes');
4
- const wasmFlate = require('wasm-flate');
5
- const zlib = require('zlib');
6
- const zstd = require('@bokuweb/zstd-wasm');
7
- const cppzst = require('@xingrz/cppzst');
8
- const pako = require('pako');
9
- const brotli = require('brotli-wasm');
10
- const files = require('../../benchmark-files');
11
16
  await zstd.init();
12
17
 
13
- const pb = (size) => prettyBytes(size, { binary: true });
14
- const t = (time) => time.toFixed(2) + 'ms';
15
-
16
18
  function createTest(name, compressor, decompressor) {
17
19
  function fn(data) {
18
20
  let start = performance.now();
@@ -32,7 +34,8 @@
32
34
  createTest('gzip -d (node)', zlib.gzipSync, zlib.gunzipSync),
33
35
  createTest('gzip -d (pako)', pako.gzip, pako.ungzip),
34
36
  createTest('zstd -4 (node)', (buf) => cppzst.compressSync(buf, { level: 4 }), cppzst.decompressSync),
35
- createTest('zstd -3 (wasm)', (buf) => zstd.compress(buf, 3), zstd.decompress),
37
+ createTest('zstd -3 (Oz,wasm)', (buf) => zstd.compress(buf, 3), zstd.decompress),
38
+ createTest('zstd -3 (O3,wasm)', (buf) => myZstd.compress(buf, 3), myZstd.decompress),
36
39
  createTest(
37
40
  'br -3 (node)',
38
41
  (buf) =>
@@ -1,4 +1,2 @@
1
- /** 压缩 */
2
- export declare function compress(data: Uint8Array): Promise<Uint8Array>;
3
- /** 解压 */
4
- export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
1
+ export { decompress } from './wasm/decompress.js';
2
+ export { compress } from './wasm/compress.js';
@@ -1,24 +1,3 @@
1
- import { decompress as d } from './lib/simple/decompress.js';
2
- import { compress as c } from './lib/simple/compress.js';
3
- import { Module, waitInitialized } from './lib/module.js';
4
- import { data } from './lib/wasm/zstd.wasm.js';
5
- let wasm;
6
- if (typeof Buffer !== 'undefined' && typeof Buffer.from == 'function') {
7
- wasm = Buffer.from(data, 'base64');
8
- }
9
- else {
10
- wasm = 'data:application/wasm;base64,' + data;
11
- }
12
- Module.init(wasm);
13
- const init = waitInitialized();
14
- /** 压缩 */
15
- export async function compress(data) {
16
- await init;
17
- return c(data, 3);
18
- }
19
- /** 解压 */
20
- export async function decompress(data) {
21
- await init;
22
- return d(data);
23
- }
1
+ export { decompress } from './wasm/decompress.js';
2
+ export { compress } from './wasm/compress.js';
24
3
  //# sourceMappingURL=index-browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index-browser.js","sourceRoot":"","sources":["../../src/zstd/index-browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,CAAC,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,QAAQ,IAAI,CAAC,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,IAAI,IAAI,CAAC;AACT,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,IAAI,IAAI,UAAU,EAAE;IACnE,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;CACtC;KAAM;IACH,IAAI,GAAG,+BAA+B,GAAG,IAAI,CAAC;CACjD;AAEA,MAAwD,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACrE,MAAM,IAAI,GAAG,eAAe,EAAE,CAAC;AAE/B,SAAS;AACT,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAgB;IAC3C,MAAM,IAAI,CAAC;IACX,OAAO,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;AACtB,CAAC;AAED,SAAS;AACT,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAgB;IAC7C,MAAM,IAAI,CAAC;IACX,OAAO,CAAC,CAAC,IAAI,CAAe,CAAC;AACjC,CAAC"}
1
+ {"version":3,"file":"index-browser.js","sourceRoot":"","sources":["../../src/zstd/index-browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,2 @@
1
+ /** 压缩 */
2
+ export declare function compress(buf: Uint8Array, level?: number): Uint8Array;
@@ -0,0 +1,28 @@
1
+ import { Module } from './module.js';
2
+ /** 压缩 */
3
+ export function compress(buf, level = 3) {
4
+ const bound = Module._ZSTD_compressBound(buf.byteLength);
5
+ const compressed = Module._malloc(bound);
6
+ const src = Module._malloc(buf.byteLength);
7
+ Module.HEAPU8.set(buf, src);
8
+ try {
9
+ /*
10
+ @See https://zstd.docsforge.com/dev/api/ZSTD_compress/
11
+ size_t ZSTD_compress( void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel);
12
+ Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
13
+ Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
14
+ @return : compressed size written into `dst` (<= `dstCapacity),
15
+ or an error code if it fails (which can be tested using ZSTD_isError()).
16
+ */
17
+ const sizeOrError = Module._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
18
+ if (Module._ZSTD_isError(sizeOrError)) {
19
+ throw new Error(`Failed to compress with code ${sizeOrError}`);
20
+ }
21
+ return new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
22
+ }
23
+ finally {
24
+ Module._free(compressed);
25
+ Module._free(src);
26
+ }
27
+ }
28
+ //# sourceMappingURL=compress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compress.js","sourceRoot":"","sources":["../../../src/zstd/wasm/compress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,SAAS;AACT,MAAM,UAAU,QAAQ,CAAC,GAAe,EAAE,KAAK,GAAG,CAAC;IAC/C,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACzD,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACzC,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC3C,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI;QACA;;;;;;;MAOF;QACE,MAAM,WAAW,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACzF,IAAI,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;YACnC,MAAM,IAAI,KAAK,CAAC,gCAAgC,WAAW,EAAE,CAAC,CAAC;SAClE;QACD,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC,KAAK,EAAE,CAAC;KAChF;YAAS;QACN,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACzB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KACrB;AACL,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** DecompressOption */
2
+ export declare type DecompressOption = {
3
+ defaultHeapSize: number;
4
+ };
5
+ /** 解压 */
6
+ export declare function decompress(buf: Uint8Array, opts?: DecompressOption): Uint8Array;
@@ -0,0 +1,30 @@
1
+ import { Module } from './module.js';
2
+ /** 解压 */
3
+ export function decompress(buf, opts = { defaultHeapSize: 1024 * 1024 }) {
4
+ const src = Module._malloc(buf.byteLength);
5
+ Module.HEAP8.set(buf, src);
6
+ const contentSize = Module._ZSTD_getFrameContentSize(src, buf.byteLength);
7
+ const size = contentSize === -1 ? opts.defaultHeapSize ?? 1024 * 1024 : contentSize;
8
+ const heap = Module._malloc(size);
9
+ try {
10
+ /*
11
+ @See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
12
+ compressedSize : must be the exact size of some number of compressed and/or skippable frames.
13
+ dstCapacity is an upper bound of originalSize to regenerate.
14
+ If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
15
+ @return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
16
+ */
17
+ const sizeOrError = Module._ZSTD_decompress(heap, size, src, buf.byteLength);
18
+ if (Module._ZSTD_isError(sizeOrError)) {
19
+ throw new Error(`Failed to compress with code ${sizeOrError}`);
20
+ }
21
+ // Copy buffer
22
+ // Uint8Array.prototype.slice() return copied buffer.
23
+ return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
24
+ }
25
+ finally {
26
+ Module._free(heap);
27
+ Module._free(src);
28
+ }
29
+ }
30
+ //# sourceMappingURL=decompress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decompress.js","sourceRoot":"","sources":["../../../src/zstd/wasm/decompress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAOrC,SAAS;AACT,MAAM,UAAU,UAAU,CACtB,GAAe,EACf,OAAyB,EAAE,eAAe,EAAE,IAAI,GAAG,IAAI,EAAE;IAEzD,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC3C,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC3B,MAAM,WAAW,GAAG,MAAM,CAAC,yBAAyB,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IAC1E,MAAM,IAAI,GAAG,WAAW,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,IAAI,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC;IACpF,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI;QACA;;;;;;MAMF;QACE,MAAM,WAAW,GAAG,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;QAC7E,IAAI,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;YACnC,MAAM,IAAI,KAAK,CAAC,gCAAgC,WAAW,EAAE,CAAC,CAAC;SAClE;QACD,cAAc;QACd,qDAAqD;QACrD,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,KAAK,EAAE,CAAC;KAC1E;YAAS;QACN,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KACrB;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ declare const Module: import("../../../wasm/zstd.g.js").Module;
2
+ export { Module };
@@ -0,0 +1,4 @@
1
+ import createModule from '../../../wasm/zstd.g.js';
2
+ const Module = await createModule();
3
+ export { Module };
4
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.js","sourceRoot":"","sources":["../../../src/zstd/wasm/module.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,yBAAyB,CAAC;AAEnD,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudpss/compress",
3
- "version": "0.3.0-alpha.21",
3
+ "version": "0.3.0-alpha.22",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,8 +16,8 @@
16
16
  "./dist/zstd/index.js": "./dist/zstd/index-browser.js"
17
17
  },
18
18
  "scripts": {
19
- "start": "pwsh ./update-zstd.ps1 && yarn clean && tsc --watch",
20
- "build": "pwsh ./update-zstd.ps1 && yarn clean && tsc",
19
+ "start": "yarn clean && tsc --watch",
20
+ "build": "yarn clean && tsc",
21
21
  "prepublishOnly": "yarn build",
22
22
  "clean": "rimraf dist",
23
23
  "benchmark": "node ./benchmark"
@@ -32,6 +32,7 @@
32
32
  "@types/pako": "^1.0.2",
33
33
  "brotli-wasm": "^1.1.0",
34
34
  "pretty-bytes": "^5.6.0",
35
+ "type-fest": "^2.5.2",
35
36
  "wasm-flate": "1.0.2"
36
37
  }
37
38
  }
@@ -1,26 +1,2 @@
1
- import { decompress as d } from './lib/simple/decompress.js';
2
- import { compress as c } from './lib/simple/compress.js';
3
- import { Module, waitInitialized } from './lib/module.js';
4
- import { data } from './lib/wasm/zstd.wasm.js';
5
-
6
- let wasm;
7
- if (typeof Buffer !== 'undefined' && typeof Buffer.from == 'function') {
8
- wasm = Buffer.from(data, 'base64');
9
- } else {
10
- wasm = 'data:application/wasm;base64,' + data;
11
- }
12
-
13
- (Module as { init: (wasm: string | Uint8Array) => void }).init(wasm);
14
- const init = waitInitialized();
15
-
16
- /** 压缩 */
17
- export async function compress(data: Uint8Array): Promise<Uint8Array> {
18
- await init;
19
- return c(data, 3);
20
- }
21
-
22
- /** 解压 */
23
- export async function decompress(data: Uint8Array): Promise<Uint8Array> {
24
- await init;
25
- return d(data) as Uint8Array;
26
- }
1
+ export { decompress } from './wasm/decompress.js';
2
+ export { compress } from './wasm/compress.js';
@@ -0,0 +1,27 @@
1
+ import { Module } from './module.js';
2
+
3
+ /** 压缩 */
4
+ export function compress(buf: Uint8Array, level = 3): Uint8Array {
5
+ const bound = Module._ZSTD_compressBound(buf.byteLength);
6
+ const compressed = Module._malloc(bound);
7
+ const src = Module._malloc(buf.byteLength);
8
+ Module.HEAPU8.set(buf, src);
9
+ try {
10
+ /*
11
+ @See https://zstd.docsforge.com/dev/api/ZSTD_compress/
12
+ size_t ZSTD_compress( void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel);
13
+ Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
14
+ Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
15
+ @return : compressed size written into `dst` (<= `dstCapacity),
16
+ or an error code if it fails (which can be tested using ZSTD_isError()).
17
+ */
18
+ const sizeOrError = Module._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
19
+ if (Module._ZSTD_isError(sizeOrError)) {
20
+ throw new Error(`Failed to compress with code ${sizeOrError}`);
21
+ }
22
+ return new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
23
+ } finally {
24
+ Module._free(compressed);
25
+ Module._free(src);
26
+ }
27
+ }
@@ -0,0 +1,37 @@
1
+ import { Module } from './module.js';
2
+
3
+ /** DecompressOption */
4
+ export type DecompressOption = {
5
+ defaultHeapSize: number;
6
+ };
7
+
8
+ /** 解压 */
9
+ export function decompress(
10
+ buf: Uint8Array,
11
+ opts: DecompressOption = { defaultHeapSize: 1024 * 1024 }, // Use 1MB on default if it is failed to get content size.
12
+ ): Uint8Array {
13
+ const src = Module._malloc(buf.byteLength);
14
+ Module.HEAP8.set(buf, src);
15
+ const contentSize = Module._ZSTD_getFrameContentSize(src, buf.byteLength);
16
+ const size = contentSize === -1 ? opts.defaultHeapSize ?? 1024 * 1024 : contentSize;
17
+ const heap = Module._malloc(size);
18
+ try {
19
+ /*
20
+ @See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
21
+ compressedSize : must be the exact size of some number of compressed and/or skippable frames.
22
+ dstCapacity is an upper bound of originalSize to regenerate.
23
+ If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
24
+ @return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
25
+ */
26
+ const sizeOrError = Module._ZSTD_decompress(heap, size, src, buf.byteLength);
27
+ if (Module._ZSTD_isError(sizeOrError)) {
28
+ throw new Error(`Failed to compress with code ${sizeOrError}`);
29
+ }
30
+ // Copy buffer
31
+ // Uint8Array.prototype.slice() return copied buffer.
32
+ return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
33
+ } finally {
34
+ Module._free(heap);
35
+ Module._free(src);
36
+ }
37
+ }
@@ -0,0 +1,5 @@
1
+ import createModule from '../../../wasm/zstd.g.js';
2
+
3
+ const Module = await createModule();
4
+
5
+ export { Module };
package/tsconfig.json CHANGED
@@ -2,7 +2,6 @@
2
2
  "extends": "../../tsconfig",
3
3
  "include": ["./src/"],
4
4
  "compilerOptions": {
5
- "outDir": "./dist",
6
- "allowJs": true
5
+ "outDir": "./dist"
7
6
  }
8
7
  }
@@ -0,0 +1,16 @@
1
+ /* eslint-disable jsdoc/require-jsdoc */
2
+ import { Opaque } from 'type-fest';
3
+ type Ptr = Opaque<number, 'void*'>;
4
+ type Module = {
5
+ readonly HEAP8: Int8Array;
6
+ readonly HEAPU8: Uint8Array;
7
+
8
+ _ZSTD_isError(code: number): number;
9
+ _ZSTD_compressBound(size: number): number;
10
+ _ZSTD_compress(outPtr: number, outSize: number, inPtr: number, inSize: number, level: number): number;
11
+ _ZSTD_getFrameContentSize(src: number, size: number): number;
12
+ _ZSTD_decompress(outPtr: number, outSize: number, inPtr: number, inSize: number): number;
13
+ _malloc(size: number): Ptr;
14
+ _free(ptr: Ptr): void;
15
+ };
16
+ export default function createModule(): Promise<Module>;