@cloudpss/compress 0.3.2 → 0.3.3

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.
@@ -1,2 +1,2 @@
1
1
  /** 压缩 */
2
- export declare function compress(buf: Uint8Array, level?: number): Promise<Uint8Array>;
2
+ export declare function compress(buf: Uint8Array, level?: number): Uint8Array;
@@ -1,11 +1,10 @@
1
1
  import { Module } from './module.js';
2
2
  /** 压缩 */
3
- export async function compress(buf, level = 3) {
4
- const m = await Module;
5
- const bound = m._ZSTD_compressBound(buf.byteLength);
6
- const compressed = m._malloc(bound);
7
- const src = m._malloc(buf.byteLength);
8
- m.HEAPU8.set(buf, src);
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);
9
8
  try {
10
9
  /*
11
10
  @See https://zstd.docsforge.com/dev/api/ZSTD_compress/
@@ -15,15 +14,15 @@ export async function compress(buf, level = 3) {
15
14
  @return : compressed size written into `dst` (<= `dstCapacity),
16
15
  or an error code if it fails (which can be tested using ZSTD_isError()).
17
16
  */
18
- const sizeOrError = m._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
19
- if (m._ZSTD_isError(sizeOrError)) {
17
+ const sizeOrError = Module._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
18
+ if (Module._ZSTD_isError(sizeOrError)) {
20
19
  throw new Error(`Failed to compress with code ${sizeOrError}`);
21
20
  }
22
- return new Uint8Array(m.HEAPU8.buffer, compressed, sizeOrError).slice();
21
+ return new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
23
22
  }
24
23
  finally {
25
- m._free(compressed);
26
- m._free(src);
24
+ Module._free(compressed);
25
+ Module._free(src);
27
26
  }
28
27
  }
29
28
  //# sourceMappingURL=compress.js.map
@@ -1 +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,CAAC,KAAK,UAAU,QAAQ,CAAC,GAAe,EAAE,KAAK,GAAG,CAAC;IACrD,MAAM,CAAC,GAAG,MAAM,MAAM,CAAC;IACvB,MAAM,KAAK,GAAG,CAAC,CAAC,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACpD,MAAM,UAAU,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACpC,MAAM,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACtC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACvB,IAAI;QACA;;;;;;;MAOF;QACE,MAAM,WAAW,GAAG,CAAC,CAAC,cAAc,CAAC,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACpF,IAAI,CAAC,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,gCAAgC,WAAW,EAAE,CAAC,CAAC;SAClE;QACD,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC,KAAK,EAAE,CAAC;KAC3E;YAAS;QACN,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACpB,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAChB;AACL,CAAC"}
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"}
@@ -3,4 +3,4 @@ export declare type DecompressOption = {
3
3
  defaultHeapSize: number;
4
4
  };
5
5
  /** 解压 */
6
- export declare function decompress(buf: Uint8Array, opts?: DecompressOption): Promise<Uint8Array>;
6
+ export declare function decompress(buf: Uint8Array, opts?: DecompressOption): Uint8Array;
@@ -1,14 +1,11 @@
1
1
  import { Module } from './module.js';
2
2
  /** 解压 */
3
- export async function decompress(buf, opts) {
4
- var _a;
5
- const m = await Module;
6
- const src = m._malloc(buf.byteLength);
7
- m.HEAP8.set(buf, src);
8
- const contentSize = m._ZSTD_getFrameContentSize(src, buf.byteLength);
9
- // Use 1MB on default if it is failed to get content size.
10
- const size = contentSize === -1 ? (_a = opts === null || opts === void 0 ? void 0 : opts.defaultHeapSize) !== null && _a !== void 0 ? _a : 1024 * 1024 : contentSize;
11
- const heap = m._malloc(size);
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);
12
9
  try {
13
10
  /*
14
11
  @See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
@@ -17,17 +14,17 @@ export async function decompress(buf, opts) {
17
14
  If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
18
15
  @return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
19
16
  */
20
- const sizeOrError = m._ZSTD_decompress(heap, size, src, buf.byteLength);
21
- if (m._ZSTD_isError(sizeOrError)) {
17
+ const sizeOrError = Module._ZSTD_decompress(heap, size, src, buf.byteLength);
18
+ if (Module._ZSTD_isError(sizeOrError)) {
22
19
  throw new Error(`Failed to compress with code ${sizeOrError}`);
23
20
  }
24
21
  // Copy buffer
25
22
  // Uint8Array.prototype.slice() return copied buffer.
26
- return new Uint8Array(m.HEAPU8.buffer, heap, sizeOrError).slice();
23
+ return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
27
24
  }
28
25
  finally {
29
- m._free(heap);
30
- m._free(src);
26
+ Module._free(heap);
27
+ Module._free(src);
31
28
  }
32
29
  }
33
30
  //# sourceMappingURL=decompress.js.map
@@ -1 +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,CAAC,KAAK,UAAU,UAAU,CAAC,GAA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T,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"}
@@ -1 +1,2 @@
1
- export declare const Module: Promise<import("../../../wasm/zstd.g.js").Module>;
1
+ declare const Module: import("../../../wasm/zstd.g.js").Module;
2
+ export { Module };
@@ -1,3 +1,4 @@
1
1
  import createModule from '../../../wasm/zstd.g.js';
2
- export const Module = createModule();
2
+ const Module = await createModule();
3
+ export { Module };
3
4
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"module.js","sourceRoot":"","sources":["../../../src/zstd/wasm/module.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,yBAAyB,CAAC;AAEnD,MAAM,CAAC,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC"}
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.2",
3
+ "version": "0.3.3",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,12 +1,11 @@
1
1
  import { Module } from './module.js';
2
2
 
3
3
  /** 压缩 */
4
- export async function compress(buf: Uint8Array, level = 3): Promise<Uint8Array> {
5
- const m = await Module;
6
- const bound = m._ZSTD_compressBound(buf.byteLength);
7
- const compressed = m._malloc(bound);
8
- const src = m._malloc(buf.byteLength);
9
- m.HEAPU8.set(buf, src);
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);
10
9
  try {
11
10
  /*
12
11
  @See https://zstd.docsforge.com/dev/api/ZSTD_compress/
@@ -16,13 +15,13 @@ export async function compress(buf: Uint8Array, level = 3): Promise<Uint8Array>
16
15
  @return : compressed size written into `dst` (<= `dstCapacity),
17
16
  or an error code if it fails (which can be tested using ZSTD_isError()).
18
17
  */
19
- const sizeOrError = m._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
20
- if (m._ZSTD_isError(sizeOrError)) {
18
+ const sizeOrError = Module._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
19
+ if (Module._ZSTD_isError(sizeOrError)) {
21
20
  throw new Error(`Failed to compress with code ${sizeOrError}`);
22
21
  }
23
- return new Uint8Array(m.HEAPU8.buffer, compressed, sizeOrError).slice();
22
+ return new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
24
23
  } finally {
25
- m._free(compressed);
26
- m._free(src);
24
+ Module._free(compressed);
25
+ Module._free(src);
27
26
  }
28
27
  }
@@ -6,14 +6,15 @@ export type DecompressOption = {
6
6
  };
7
7
 
8
8
  /** 解压 */
9
- export async function decompress(buf: Uint8Array, opts?: DecompressOption): Promise<Uint8Array> {
10
- const m = await Module;
11
- const src = m._malloc(buf.byteLength);
12
- m.HEAP8.set(buf, src);
13
- const contentSize = m._ZSTD_getFrameContentSize(src, buf.byteLength);
14
- // Use 1MB on default if it is failed to get content size.
15
- const size = contentSize === -1 ? opts?.defaultHeapSize ?? 1024 * 1024 : contentSize;
16
- const heap = m._malloc(size);
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);
17
18
  try {
18
19
  /*
19
20
  @See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
@@ -22,15 +23,15 @@ export async function decompress(buf: Uint8Array, opts?: DecompressOption): Prom
22
23
  If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
23
24
  @return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
24
25
  */
25
- const sizeOrError = m._ZSTD_decompress(heap, size, src, buf.byteLength);
26
- if (m._ZSTD_isError(sizeOrError)) {
26
+ const sizeOrError = Module._ZSTD_decompress(heap, size, src, buf.byteLength);
27
+ if (Module._ZSTD_isError(sizeOrError)) {
27
28
  throw new Error(`Failed to compress with code ${sizeOrError}`);
28
29
  }
29
30
  // Copy buffer
30
31
  // Uint8Array.prototype.slice() return copied buffer.
31
- return new Uint8Array(m.HEAPU8.buffer, heap, sizeOrError).slice();
32
+ return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
32
33
  } finally {
33
- m._free(heap);
34
- m._free(src);
34
+ Module._free(heap);
35
+ Module._free(src);
35
36
  }
36
37
  }
@@ -1,3 +1,5 @@
1
1
  import createModule from '../../../wasm/zstd.g.js';
2
2
 
3
- export const Module = createModule();
3
+ const Module = await createModule();
4
+
5
+ export { Module };