@cloudpss/compress 0.3.1 → 0.3.2
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 +11 -10
- package/dist/zstd/wasm/compress.js.map +1 -1
- package/dist/zstd/wasm/decompress.d.ts +1 -1
- package/dist/zstd/wasm/decompress.js +13 -11
- package/dist/zstd/wasm/decompress.js.map +1 -1
- package/dist/zstd/wasm/module.d.ts +1 -2
- package/dist/zstd/wasm/module.js +1 -2
- package/dist/zstd/wasm/module.js.map +1 -1
- package/package.json +1 -1
- package/src/zstd/wasm/compress.ts +11 -10
- package/src/zstd/wasm/decompress.ts +13 -14
- package/src/zstd/wasm/module.ts +1 -3
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** 压缩 */
|
|
2
|
-
export declare function compress(buf: Uint8Array, level?: number): Uint8Array
|
|
2
|
+
export declare function compress(buf: Uint8Array, level?: number): Promise<Uint8Array>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Module } from './module.js';
|
|
2
2
|
/** 压缩 */
|
|
3
|
-
export function compress(buf, level = 3) {
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
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);
|
|
8
9
|
try {
|
|
9
10
|
/*
|
|
10
11
|
@See https://zstd.docsforge.com/dev/api/ZSTD_compress/
|
|
@@ -14,15 +15,15 @@ export function compress(buf, level = 3) {
|
|
|
14
15
|
@return : compressed size written into `dst` (<= `dstCapacity),
|
|
15
16
|
or an error code if it fails (which can be tested using ZSTD_isError()).
|
|
16
17
|
*/
|
|
17
|
-
const sizeOrError =
|
|
18
|
-
if (
|
|
18
|
+
const sizeOrError = m._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
|
|
19
|
+
if (m._ZSTD_isError(sizeOrError)) {
|
|
19
20
|
throw new Error(`Failed to compress with code ${sizeOrError}`);
|
|
20
21
|
}
|
|
21
|
-
return new Uint8Array(
|
|
22
|
+
return new Uint8Array(m.HEAPU8.buffer, compressed, sizeOrError).slice();
|
|
22
23
|
}
|
|
23
24
|
finally {
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
m._free(compressed);
|
|
26
|
+
m._free(src);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
//# 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,UAAU,QAAQ,CAAC,GAAe,EAAE,KAAK,GAAG,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,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"}
|
|
@@ -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): Uint8Array
|
|
6
|
+
export declare function decompress(buf: Uint8Array, opts?: DecompressOption): Promise<Uint8Array>;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Module } from './module.js';
|
|
2
2
|
/** 解压 */
|
|
3
|
-
export function decompress(buf, opts
|
|
3
|
+
export async function decompress(buf, opts) {
|
|
4
4
|
var _a;
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
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);
|
|
10
12
|
try {
|
|
11
13
|
/*
|
|
12
14
|
@See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
|
|
@@ -15,17 +17,17 @@ export function decompress(buf, opts = { defaultHeapSize: 1024 * 1024 }) {
|
|
|
15
17
|
If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
|
|
16
18
|
@return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
|
|
17
19
|
*/
|
|
18
|
-
const sizeOrError =
|
|
19
|
-
if (
|
|
20
|
+
const sizeOrError = m._ZSTD_decompress(heap, size, src, buf.byteLength);
|
|
21
|
+
if (m._ZSTD_isError(sizeOrError)) {
|
|
20
22
|
throw new Error(`Failed to compress with code ${sizeOrError}`);
|
|
21
23
|
}
|
|
22
24
|
// Copy buffer
|
|
23
25
|
// Uint8Array.prototype.slice() return copied buffer.
|
|
24
|
-
return new Uint8Array(
|
|
26
|
+
return new Uint8Array(m.HEAPU8.buffer, heap, sizeOrError).slice();
|
|
25
27
|
}
|
|
26
28
|
finally {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
m._free(heap);
|
|
30
|
+
m._free(src);
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
//# 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,UAAU,UAAU,
|
|
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,GAAe,EAAE,IAAuB;;IACrE,MAAM,CAAC,GAAG,MAAM,MAAM,CAAC;IACvB,MAAM,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACtC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtB,MAAM,WAAW,GAAG,CAAC,CAAC,yBAAyB,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IACrE,0DAA0D;IAC1D,MAAM,IAAI,GAAG,WAAW,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,eAAe,mCAAI,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC;IACrF,MAAM,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,IAAI;QACA;;;;;;MAMF;QACE,MAAM,WAAW,GAAG,CAAC,CAAC,gBAAgB,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;QACxE,IAAI,CAAC,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,gCAAgC,WAAW,EAAE,CAAC,CAAC;SAClE;QACD,cAAc;QACd,qDAAqD;QACrD,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,KAAK,EAAE,CAAC;KACrE;YAAS;QACN,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACd,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAChB;AACL,CAAC"}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
declare const Module: import("../../../wasm/zstd.g.js").Module
|
|
2
|
-
export { Module };
|
|
1
|
+
export declare const Module: Promise<import("../../../wasm/zstd.g.js").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,MAAM,GAAG,
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Module } from './module.js';
|
|
2
2
|
|
|
3
3
|
/** 压缩 */
|
|
4
|
-
export function compress(buf: Uint8Array, level = 3): Uint8Array {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
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);
|
|
9
10
|
try {
|
|
10
11
|
/*
|
|
11
12
|
@See https://zstd.docsforge.com/dev/api/ZSTD_compress/
|
|
@@ -15,13 +16,13 @@ export function compress(buf: Uint8Array, level = 3): Uint8Array {
|
|
|
15
16
|
@return : compressed size written into `dst` (<= `dstCapacity),
|
|
16
17
|
or an error code if it fails (which can be tested using ZSTD_isError()).
|
|
17
18
|
*/
|
|
18
|
-
const sizeOrError =
|
|
19
|
-
if (
|
|
19
|
+
const sizeOrError = m._ZSTD_compress(compressed, bound, src, buf.byteLength, level);
|
|
20
|
+
if (m._ZSTD_isError(sizeOrError)) {
|
|
20
21
|
throw new Error(`Failed to compress with code ${sizeOrError}`);
|
|
21
22
|
}
|
|
22
|
-
return new Uint8Array(
|
|
23
|
+
return new Uint8Array(m.HEAPU8.buffer, compressed, sizeOrError).slice();
|
|
23
24
|
} finally {
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
m._free(compressed);
|
|
26
|
+
m._free(src);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
@@ -6,15 +6,14 @@ export type DecompressOption = {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
/** 解压 */
|
|
9
|
-
export function decompress(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
)
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const heap = Module._malloc(size);
|
|
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);
|
|
18
17
|
try {
|
|
19
18
|
/*
|
|
20
19
|
@See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
|
|
@@ -23,15 +22,15 @@ export function decompress(
|
|
|
23
22
|
If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
|
|
24
23
|
@return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
|
|
25
24
|
*/
|
|
26
|
-
const sizeOrError =
|
|
27
|
-
if (
|
|
25
|
+
const sizeOrError = m._ZSTD_decompress(heap, size, src, buf.byteLength);
|
|
26
|
+
if (m._ZSTD_isError(sizeOrError)) {
|
|
28
27
|
throw new Error(`Failed to compress with code ${sizeOrError}`);
|
|
29
28
|
}
|
|
30
29
|
// Copy buffer
|
|
31
30
|
// Uint8Array.prototype.slice() return copied buffer.
|
|
32
|
-
return new Uint8Array(
|
|
31
|
+
return new Uint8Array(m.HEAPU8.buffer, heap, sizeOrError).slice();
|
|
33
32
|
} finally {
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
m._free(heap);
|
|
34
|
+
m._free(src);
|
|
36
35
|
}
|
|
37
36
|
}
|
package/src/zstd/wasm/module.ts
CHANGED