@cloudpss/compress 0.3.2 → 0.3.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/dist/zstd/wasm/compress.d.ts +1 -1
- package/dist/zstd/wasm/compress.js +10 -11
- package/dist/zstd/wasm/compress.js.map +1 -1
- package/dist/zstd/wasm/decompress.d.ts +1 -1
- package/dist/zstd/wasm/decompress.js +11 -14
- package/dist/zstd/wasm/decompress.js.map +1 -1
- package/dist/zstd/wasm/module.d.ts +2 -1
- package/dist/zstd/wasm/module.js +2 -1
- package/dist/zstd/wasm/module.js.map +1 -1
- package/package.json +2 -2
- package/src/zstd/wasm/compress.ts +10 -11
- package/src/zstd/wasm/decompress.ts +14 -13
- package/src/zstd/wasm/module.ts +3 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** 压缩 */
|
|
2
|
-
export declare function compress(buf: Uint8Array, level?: number):
|
|
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
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
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 =
|
|
19
|
-
if (
|
|
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(
|
|
21
|
+
return new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
|
|
23
22
|
}
|
|
24
23
|
finally {
|
|
25
|
-
|
|
26
|
-
|
|
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,
|
|
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):
|
|
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
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
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 =
|
|
21
|
-
if (
|
|
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(
|
|
23
|
+
return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
|
|
27
24
|
}
|
|
28
25
|
finally {
|
|
29
|
-
|
|
30
|
-
|
|
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,
|
|
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"}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
declare const Module: import("../../../wasm/zstd.g.js").Module;
|
|
2
|
+
export { Module };
|
package/dist/zstd/wasm/module.js
CHANGED
|
@@ -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,
|
|
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.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@bokuweb/zstd-wasm": "^0.0.14",
|
|
31
|
-
"@types/node": "^16.11.
|
|
31
|
+
"@types/node": "^16.11.12",
|
|
32
32
|
"@types/pako": "^1.0.2",
|
|
33
33
|
"brotli-wasm": "^1.1.0",
|
|
34
34
|
"pretty-bytes": "^5.6.0",
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { Module } from './module.js';
|
|
2
2
|
|
|
3
3
|
/** 压缩 */
|
|
4
|
-
export
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
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 =
|
|
20
|
-
if (
|
|
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(
|
|
22
|
+
return new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
|
|
24
23
|
} finally {
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
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 =
|
|
26
|
-
if (
|
|
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(
|
|
32
|
+
return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
|
|
32
33
|
} finally {
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
Module._free(heap);
|
|
35
|
+
Module._free(src);
|
|
35
36
|
}
|
|
36
37
|
}
|
package/src/zstd/wasm/module.ts
CHANGED