@cloudpss/compress 0.4.12 → 0.4.13
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/benchmark.js +70 -49
- package/dist/gzip/index-browser.d.ts +4 -4
- package/dist/gzip/index-browser.js +9 -9
- package/dist/gzip/index.d.ts +4 -4
- package/dist/gzip/index.js +21 -21
- package/dist/index.d.ts +17 -17
- package/dist/index.js +58 -58
- package/dist/zstd/index-browser.d.ts +2 -2
- package/dist/zstd/index-browser.js +2 -2
- package/dist/zstd/index.d.ts +4 -4
- package/dist/zstd/index.js +37 -13
- package/dist/zstd/index.js.map +1 -1
- package/dist/zstd/wasm/compress.d.ts +2 -2
- package/dist/zstd/wasm/compress.js +27 -27
- package/dist/zstd/wasm/compress.js.map +1 -1
- package/dist/zstd/wasm/decompress.d.ts +2 -6
- package/dist/zstd/wasm/decompress.js +31 -29
- package/dist/zstd/wasm/decompress.js.map +1 -1
- package/dist/zstd/wasm/module.d.ts +2 -2
- package/dist/zstd/wasm/module.js +3 -3
- package/package.json +7 -5
- package/src/zstd/index.ts +32 -8
- package/src/zstd/wasm/compress.ts +8 -8
- package/src/zstd/wasm/decompress.ts +10 -15
- package/wasm/zstd.g.d.ts +1 -0
- package/wasm/zstd.g.js +15 -17
package/benchmark.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
1
2
|
import prettyBytes from 'pretty-bytes';
|
|
2
3
|
import wasmFlate from 'wasm-flate';
|
|
3
4
|
import zlib from 'zlib';
|
|
@@ -5,58 +6,78 @@ import zstd from '@bokuweb/zstd-wasm';
|
|
|
5
6
|
import * as myZstd from './dist/zstd/index-browser.js';
|
|
6
7
|
import cppzst from '@xingrz/cppzst';
|
|
7
8
|
import pako from 'pako';
|
|
8
|
-
import brotli from 'brotli-wasm';
|
|
9
9
|
import files from '../../benchmark-files/index.js';
|
|
10
|
+
import { createRequire } from 'node:module';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
const
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
13
|
+
const brotli = createRequire(import.meta.url)('brotli-wasm');
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
(
|
|
16
|
-
|
|
15
|
+
const t = (/** @type {number} */ time) => time.toFixed(2) + 'ms';
|
|
16
|
+
const pb = (/** @type {number} */ size) => prettyBytes(size, { binary: true });
|
|
17
|
+
|
|
18
|
+
await zstd.init();
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
/** 生成测试 */
|
|
21
|
+
function createTest(
|
|
22
|
+
/** @type {string} */ name,
|
|
23
|
+
/** @type {(value: Uint8Array) => Uint8Array} */ compressor,
|
|
24
|
+
/** @type {(value: Uint8Array) => Uint8Array} */ decompressor,
|
|
25
|
+
) {
|
|
26
|
+
/** 测试函数 */
|
|
27
|
+
function fn(/** @type {Buffer} */ data) {
|
|
28
|
+
let start = performance.now();
|
|
29
|
+
const compressed = compressor(data);
|
|
30
|
+
const compressTime = performance.now() - start;
|
|
31
|
+
start = performance.now();
|
|
32
|
+
const decompressed = decompressor(compressed);
|
|
33
|
+
const decompressTime = performance.now() - start;
|
|
34
|
+
console.assert(data.equals(decompressed), name, `unmatched`);
|
|
35
|
+
return { compressed, compressTime, decompressTime };
|
|
31
36
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
37
|
+
Object.defineProperty(fn, 'name', { value: name });
|
|
38
|
+
return fn;
|
|
39
|
+
}
|
|
40
|
+
const tests = [
|
|
41
|
+
createTest('gzip -d (wasm)', wasmFlate.gzip_encode_raw, wasmFlate.gzip_decode_raw),
|
|
42
|
+
createTest('gzip -d (node)', zlib.gzipSync, zlib.gunzipSync),
|
|
43
|
+
createTest('gzip -d (pako)', pako.gzip, pako.ungzip),
|
|
44
|
+
createTest('zstd -3 (Oz,wasm)', (buf) => zstd.compress(buf, 3), zstd.decompress),
|
|
45
|
+
createTest('zstd -3 (O3,wasm)', (buf) => myZstd.compress(buf, 3), myZstd.decompress),
|
|
46
|
+
createTest('zstd -4 (O3,wasm)', (buf) => myZstd.compress(buf, 4), myZstd.decompress),
|
|
47
|
+
createTest('zstd -4 (node)', (buf) => cppzst.compressSync(buf, { level: 4 }), cppzst.decompressSync),
|
|
48
|
+
createTest('zstd -5 (O3,wasm)', (buf) => myZstd.compress(buf, 5), myZstd.decompress),
|
|
49
|
+
createTest('zstd -5 (node)', (buf) => cppzst.compressSync(buf, { level: 5 }), cppzst.decompressSync),
|
|
50
|
+
createTest('zstd -10 (O3,wasm)', (buf) => myZstd.compress(buf, 10), myZstd.decompress),
|
|
51
|
+
createTest('zstd -10 (node)', (buf) => cppzst.compressSync(buf, { level: 10 }), cppzst.decompressSync),
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-argument
|
|
53
|
+
createTest('br -1 (wasm)', (buf) => brotli.compress(buf, { quality: 1 }), brotli.decompress),
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-argument
|
|
55
|
+
createTest('br -3 (wasm)', (buf) => brotli.compress(buf, { quality: 3 }), brotli.decompress),
|
|
56
|
+
createTest(
|
|
57
|
+
'br -3 (node)',
|
|
58
|
+
(buf) =>
|
|
59
|
+
zlib.brotliCompressSync(buf, {
|
|
60
|
+
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 3 },
|
|
61
|
+
}),
|
|
62
|
+
zlib.brotliDecompressSync,
|
|
63
|
+
),
|
|
64
|
+
createTest(
|
|
65
|
+
`br -5 (node)`,
|
|
66
|
+
(buf) =>
|
|
67
|
+
zlib.brotliCompressSync(buf, {
|
|
68
|
+
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 5 },
|
|
69
|
+
}),
|
|
70
|
+
zlib.brotliDecompressSync,
|
|
71
|
+
),
|
|
72
|
+
];
|
|
73
|
+
for await (const { file, data } of files()) {
|
|
74
|
+
console.log(`File: ${file} \tRaw: ${pb(data.length)}`);
|
|
75
|
+
for (const test of tests) {
|
|
76
|
+
const result = test(data);
|
|
77
|
+
const compressed = result.compressed.length;
|
|
78
|
+
const ratio = (data.length / result.compressed.length).toFixed(2);
|
|
79
|
+
console.log(
|
|
80
|
+
` ${test.name}: \t${ratio}(${pb(compressed)}) \t${t(result.compressTime)} \t${t(result.decompressTime)}`,
|
|
81
|
+
);
|
|
61
82
|
}
|
|
62
|
-
}
|
|
83
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** 压缩 */
|
|
2
|
-
export declare function compress(data: Uint8Array): Uint8Array;
|
|
3
|
-
/** 解压 */
|
|
4
|
-
export declare function decompress(data: Uint8Array): Uint8Array;
|
|
1
|
+
/** 压缩 */
|
|
2
|
+
export declare function compress(data: Uint8Array): Uint8Array;
|
|
3
|
+
/** 解压 */
|
|
4
|
+
export declare function decompress(data: Uint8Array): Uint8Array;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { gzip, ungzip } from 'pako';
|
|
2
|
-
/** 压缩 */
|
|
3
|
-
export function compress(data) {
|
|
4
|
-
return gzip(data);
|
|
5
|
-
}
|
|
6
|
-
/** 解压 */
|
|
7
|
-
export function decompress(data) {
|
|
8
|
-
return ungzip(data);
|
|
9
|
-
}
|
|
1
|
+
import { gzip, ungzip } from 'pako';
|
|
2
|
+
/** 压缩 */
|
|
3
|
+
export function compress(data) {
|
|
4
|
+
return gzip(data);
|
|
5
|
+
}
|
|
6
|
+
/** 解压 */
|
|
7
|
+
export function decompress(data) {
|
|
8
|
+
return ungzip(data);
|
|
9
|
+
}
|
|
10
10
|
//# sourceMappingURL=index-browser.js.map
|
package/dist/gzip/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** 压缩 */
|
|
2
|
-
export declare function compress(data: Uint8Array): Promise<Uint8Array>;
|
|
3
|
-
/** 解压 */
|
|
4
|
-
export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
|
|
1
|
+
/** 压缩 */
|
|
2
|
+
export declare function compress(data: Uint8Array): Promise<Uint8Array>;
|
|
3
|
+
/** 解压 */
|
|
4
|
+
export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
|
package/dist/gzip/index.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { gzip, gunzip } from 'zlib';
|
|
2
|
-
/** 压缩 */
|
|
3
|
-
export function compress(data) {
|
|
4
|
-
return new Promise((resolve, reject) => {
|
|
5
|
-
gzip(data, (err, result) => {
|
|
6
|
-
if (err || !result)
|
|
7
|
-
reject(err);
|
|
8
|
-
resolve(new Uint8Array(result.buffer, result.byteOffset, result.length));
|
|
9
|
-
});
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
/** 解压 */
|
|
13
|
-
export function decompress(data) {
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
gunzip(data, (err, result) => {
|
|
16
|
-
if (err || !result)
|
|
17
|
-
reject(err);
|
|
18
|
-
resolve(new Uint8Array(result.buffer, result.byteOffset, result.length));
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
}
|
|
1
|
+
import { gzip, gunzip } from 'zlib';
|
|
2
|
+
/** 压缩 */
|
|
3
|
+
export function compress(data) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
gzip(data, (err, result) => {
|
|
6
|
+
if (err || !result)
|
|
7
|
+
reject(err);
|
|
8
|
+
resolve(new Uint8Array(result.buffer, result.byteOffset, result.length));
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/** 解压 */
|
|
13
|
+
export function decompress(data) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
gunzip(data, (err, result) => {
|
|
16
|
+
if (err || !result)
|
|
17
|
+
reject(err);
|
|
18
|
+
resolve(new Uint8Array(result.buffer, result.byteOffset, result.length));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
/** 可用的压缩算法 */
|
|
2
|
-
export type CompressionAlgorithm = 'zstd' | 'gzip' | 'none';
|
|
3
|
-
/** 压缩算法的魔数 */
|
|
4
|
-
export declare const MAGIC_NUMBERS: {
|
|
5
|
-
zstd: number[];
|
|
6
|
-
gzip: number[];
|
|
7
|
-
};
|
|
8
|
-
/** 使用指定算法压缩 */
|
|
9
|
-
export declare function compress(data: Uint8Array, algorithm?: CompressionAlgorithm): CompressionResult;
|
|
10
|
-
/** 压缩/解压缩结果 */
|
|
11
|
-
export type CompressionResult = Promise<Uint8Array> & {
|
|
12
|
-
algorithm: CompressionAlgorithm;
|
|
13
|
-
};
|
|
14
|
-
/** 探测压缩算法 */
|
|
15
|
-
export declare function detectCompression(data: Uint8Array): CompressionAlgorithm;
|
|
16
|
-
/** 探测压缩算法并解压缩 */
|
|
17
|
-
export declare function decompress(data: Uint8Array): CompressionResult;
|
|
1
|
+
/** 可用的压缩算法 */
|
|
2
|
+
export type CompressionAlgorithm = 'zstd' | 'gzip' | 'none';
|
|
3
|
+
/** 压缩算法的魔数 */
|
|
4
|
+
export declare const MAGIC_NUMBERS: {
|
|
5
|
+
zstd: number[];
|
|
6
|
+
gzip: number[];
|
|
7
|
+
};
|
|
8
|
+
/** 使用指定算法压缩 */
|
|
9
|
+
export declare function compress(data: Uint8Array, algorithm?: CompressionAlgorithm): CompressionResult;
|
|
10
|
+
/** 压缩/解压缩结果 */
|
|
11
|
+
export type CompressionResult = Promise<Uint8Array> & {
|
|
12
|
+
algorithm: CompressionAlgorithm;
|
|
13
|
+
};
|
|
14
|
+
/** 探测压缩算法 */
|
|
15
|
+
export declare function detectCompression(data: Uint8Array): CompressionAlgorithm;
|
|
16
|
+
/** 探测压缩算法并解压缩 */
|
|
17
|
+
export declare function decompress(data: Uint8Array): CompressionResult;
|
package/dist/index.js
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
/** 压缩算法的魔数 */
|
|
2
|
-
export const MAGIC_NUMBERS = {
|
|
3
|
-
zstd: [0x28, 0xb5, 0x2f, 0xfd],
|
|
4
|
-
gzip: [0x1f, 0x8b],
|
|
5
|
-
};
|
|
6
|
-
const modules = {
|
|
7
|
-
zstd: () => import('./zstd/index.js'),
|
|
8
|
-
gzip: () => import('./gzip/index.js'),
|
|
9
|
-
};
|
|
10
|
-
/** 加载相关库 */
|
|
11
|
-
function loadLib(algorithm) {
|
|
12
|
-
if (!(algorithm in modules))
|
|
13
|
-
throw new Error(`Unknown compression algorithm: ${algorithm}`);
|
|
14
|
-
return modules[algorithm]();
|
|
15
|
-
}
|
|
16
|
-
/** 使用指定算法压缩 */
|
|
17
|
-
export function compress(data, algorithm = 'zstd') {
|
|
18
|
-
if (algorithm === 'none') {
|
|
19
|
-
const ret = Promise.resolve(data);
|
|
20
|
-
ret.algorithm = 'none';
|
|
21
|
-
return ret;
|
|
22
|
-
}
|
|
23
|
-
const ret = loadLib(algorithm).then((lib) => lib.compress(data));
|
|
24
|
-
ret.algorithm = algorithm;
|
|
25
|
-
return ret;
|
|
26
|
-
}
|
|
27
|
-
/** 探测压缩算法 */
|
|
28
|
-
export function detectCompression(data) {
|
|
29
|
-
for (const key in MAGIC_NUMBERS) {
|
|
30
|
-
const k = key;
|
|
31
|
-
const magicNumber = MAGIC_NUMBERS[k];
|
|
32
|
-
if (data.length <= magicNumber.length)
|
|
33
|
-
continue;
|
|
34
|
-
let match = true;
|
|
35
|
-
for (let i = 0; i < magicNumber.length; i++) {
|
|
36
|
-
if (data[i] !== magicNumber[i]) {
|
|
37
|
-
match = false;
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (match)
|
|
42
|
-
return k;
|
|
43
|
-
}
|
|
44
|
-
return 'none';
|
|
45
|
-
}
|
|
46
|
-
/** 探测压缩算法并解压缩 */
|
|
47
|
-
export function decompress(data) {
|
|
48
|
-
const algorithm = detectCompression(data);
|
|
49
|
-
if (algorithm === 'none') {
|
|
50
|
-
// 不是压缩数据
|
|
51
|
-
const result = Promise.resolve(data);
|
|
52
|
-
result.algorithm = 'none';
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
const result = loadLib(algorithm).then(({ decompress }) => decompress(data));
|
|
56
|
-
result.algorithm = algorithm;
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
1
|
+
/** 压缩算法的魔数 */
|
|
2
|
+
export const MAGIC_NUMBERS = {
|
|
3
|
+
zstd: [0x28, 0xb5, 0x2f, 0xfd],
|
|
4
|
+
gzip: [0x1f, 0x8b],
|
|
5
|
+
};
|
|
6
|
+
const modules = {
|
|
7
|
+
zstd: () => import('./zstd/index.js'),
|
|
8
|
+
gzip: () => import('./gzip/index.js'),
|
|
9
|
+
};
|
|
10
|
+
/** 加载相关库 */
|
|
11
|
+
function loadLib(algorithm) {
|
|
12
|
+
if (!(algorithm in modules))
|
|
13
|
+
throw new Error(`Unknown compression algorithm: ${algorithm}`);
|
|
14
|
+
return modules[algorithm]();
|
|
15
|
+
}
|
|
16
|
+
/** 使用指定算法压缩 */
|
|
17
|
+
export function compress(data, algorithm = 'zstd') {
|
|
18
|
+
if (algorithm === 'none') {
|
|
19
|
+
const ret = Promise.resolve(data);
|
|
20
|
+
ret.algorithm = 'none';
|
|
21
|
+
return ret;
|
|
22
|
+
}
|
|
23
|
+
const ret = loadLib(algorithm).then((lib) => lib.compress(data));
|
|
24
|
+
ret.algorithm = algorithm;
|
|
25
|
+
return ret;
|
|
26
|
+
}
|
|
27
|
+
/** 探测压缩算法 */
|
|
28
|
+
export function detectCompression(data) {
|
|
29
|
+
for (const key in MAGIC_NUMBERS) {
|
|
30
|
+
const k = key;
|
|
31
|
+
const magicNumber = MAGIC_NUMBERS[k];
|
|
32
|
+
if (data.length <= magicNumber.length)
|
|
33
|
+
continue;
|
|
34
|
+
let match = true;
|
|
35
|
+
for (let i = 0; i < magicNumber.length; i++) {
|
|
36
|
+
if (data[i] !== magicNumber[i]) {
|
|
37
|
+
match = false;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (match)
|
|
42
|
+
return k;
|
|
43
|
+
}
|
|
44
|
+
return 'none';
|
|
45
|
+
}
|
|
46
|
+
/** 探测压缩算法并解压缩 */
|
|
47
|
+
export function decompress(data) {
|
|
48
|
+
const algorithm = detectCompression(data);
|
|
49
|
+
if (algorithm === 'none') {
|
|
50
|
+
// 不是压缩数据
|
|
51
|
+
const result = Promise.resolve(data);
|
|
52
|
+
result.algorithm = 'none';
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
const result = loadLib(algorithm).then(({ decompress }) => decompress(data));
|
|
56
|
+
result.algorithm = algorithm;
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
59
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { decompress } from './wasm/decompress.js';
|
|
2
|
-
export { compress } from './wasm/compress.js';
|
|
1
|
+
export { decompress } from './wasm/decompress.js';
|
|
2
|
+
export { compress } from './wasm/compress.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { decompress } from './wasm/decompress.js';
|
|
2
|
-
export { compress } from './wasm/compress.js';
|
|
1
|
+
export { decompress } from './wasm/decompress.js';
|
|
2
|
+
export { compress } from './wasm/compress.js';
|
|
3
3
|
//# sourceMappingURL=index-browser.js.map
|
package/dist/zstd/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** 压缩 */
|
|
2
|
-
export declare function compress(data: Uint8Array): Promise<Uint8Array>;
|
|
3
|
-
/** 解压 */
|
|
4
|
-
export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
|
|
1
|
+
/** 压缩 */
|
|
2
|
+
export declare function compress(data: Uint8Array, level?: number): Promise<Uint8Array>;
|
|
3
|
+
/** 解压 */
|
|
4
|
+
export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
|
package/dist/zstd/index.js
CHANGED
|
@@ -1,14 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
/** 导入库 */
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
3
|
+
async function importLib() {
|
|
4
|
+
try {
|
|
5
|
+
const { compress, decompress } = await import('@xingrz/cppzst');
|
|
6
|
+
return {
|
|
7
|
+
compress: async (data, level) => {
|
|
8
|
+
const buf = Buffer.from(data.buffer, data.byteOffset, data.length);
|
|
9
|
+
const result = await compress(buf, { level: level ?? 4 });
|
|
10
|
+
return new Uint8Array(result.buffer, result.byteOffset, result.length);
|
|
11
|
+
},
|
|
12
|
+
decompress: async (data) => {
|
|
13
|
+
const buf = Buffer.from(data.buffer, data.byteOffset, data.length);
|
|
14
|
+
const result = await decompress(buf);
|
|
15
|
+
return new Uint8Array(result.buffer, result.byteOffset, result.length);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
catch (ex) {
|
|
20
|
+
const { compress, decompress } = await import('./index-browser.js');
|
|
21
|
+
return {
|
|
22
|
+
compress: (data, level) => Promise.resolve(compress(data, level)),
|
|
23
|
+
decompress: (data) => Promise.resolve(decompress(data)),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const lib = importLib();
|
|
28
|
+
/** 压缩 */
|
|
29
|
+
export async function compress(data, level) {
|
|
30
|
+
const { compress } = await lib;
|
|
31
|
+
return await compress(data, level);
|
|
32
|
+
}
|
|
33
|
+
/** 解压 */
|
|
34
|
+
export async function decompress(data) {
|
|
35
|
+
const { decompress } = await lib;
|
|
36
|
+
return await decompress(data);
|
|
37
|
+
}
|
|
14
38
|
//# sourceMappingURL=index.js.map
|
package/dist/zstd/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/zstd/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/zstd/index.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,4EAA4E;AAC5E,KAAK,UAAU,SAAS;IACpB,IAAI;QACA,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAC;QAChE,OAAO;YACH,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;gBAC5B,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;gBACnE,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC;gBAC1D,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAC3E,CAAC;YACD,UAAU,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;gBACvB,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;gBACnE,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,GAAG,CAAC,CAAC;gBACrC,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAC3E,CAAC;SACJ,CAAC;KACL;IAAC,OAAO,EAAE,EAAE;QACT,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;QACpE,OAAO;YACH,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACjE,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;SAC1D,CAAC;KACL;AACL,CAAC;AAED,MAAM,GAAG,GAAG,SAAS,EAAE,CAAC;AAExB,SAAS;AACT,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAgB,EAAE,KAAc;IAC3D,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,GAAG,CAAC;IAC/B,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACvC,CAAC;AAED,SAAS;AACT,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAgB;IAC7C,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,GAAG,CAAC;IACjC,OAAO,MAAM,UAAU,CAAC,IAAI,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/** 压缩 */
|
|
2
|
-
export declare function compress(buf: Uint8Array, level?: number): Uint8Array;
|
|
1
|
+
/** 压缩 */
|
|
2
|
+
export declare function compress(buf: Uint8Array, level?: number): Uint8Array;
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { Module } from './module.js';
|
|
2
|
-
/** 压缩 */
|
|
3
|
-
export function compress(buf, level =
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
}
|
|
1
|
+
import { Module } from './module.js';
|
|
2
|
+
/** 压缩 */
|
|
3
|
+
export function compress(buf, level = 4) {
|
|
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
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,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;;;;;;;
|
|
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;;;;;;;UAOE;QACF,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"}
|
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
export
|
|
3
|
-
defaultHeapSize: number;
|
|
4
|
-
};
|
|
5
|
-
/** 解压 */
|
|
6
|
-
export declare function decompress(buf: Uint8Array, opts?: DecompressOption): Uint8Array;
|
|
1
|
+
/** 解压 */
|
|
2
|
+
export declare function decompress(buf: Uint8Array): Uint8Array;
|
|
@@ -1,30 +1,32 @@
|
|
|
1
|
-
import { Module } from './module.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
import { Module } from './module.js';
|
|
2
|
+
// Use 1MB on default if it is failed to get content size.
|
|
3
|
+
const DEFAULT_HEAP_SIZE = 1024 * 1024;
|
|
4
|
+
/** 解压 */
|
|
5
|
+
export function decompress(buf) {
|
|
6
|
+
const src = Module._malloc(buf.byteLength);
|
|
7
|
+
Module.HEAP8.set(buf, src);
|
|
8
|
+
const contentSize = Module._ZSTD_getFrameContentSize(src, buf.byteLength);
|
|
9
|
+
const size = contentSize === -1 ? DEFAULT_HEAP_SIZE : contentSize;
|
|
10
|
+
const heap = Module._malloc(size);
|
|
11
|
+
try {
|
|
12
|
+
/*
|
|
13
|
+
@See https://zstd.docsforge.com/dev/api/ZSTD_decompress/
|
|
14
|
+
compressedSize : must be the exact size of some number of compressed and/or skippable frames.
|
|
15
|
+
dstCapacity is an upper bound of originalSize to regenerate.
|
|
16
|
+
If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
|
|
17
|
+
@return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
|
|
18
|
+
*/
|
|
19
|
+
const sizeOrError = Module._ZSTD_decompress(heap, size, src, buf.byteLength);
|
|
20
|
+
if (Module._ZSTD_isError(sizeOrError)) {
|
|
21
|
+
throw new Error(`Failed to compress with code ${sizeOrError}`);
|
|
22
|
+
}
|
|
23
|
+
// Copy buffer
|
|
24
|
+
// Uint8Array.prototype.slice() return copied buffer.
|
|
25
|
+
return new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
Module._free(heap);
|
|
29
|
+
Module._free(src);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
30
32
|
//# 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;
|
|
1
|
+
{"version":3,"file":"decompress.js","sourceRoot":"","sources":["../../../src/zstd/wasm/decompress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,0DAA0D;AAC1D,MAAM,iBAAiB,GAAG,IAAI,GAAG,IAAI,CAAC;AAEtC,SAAS;AACT,MAAM,UAAU,UAAU,CAAC,GAAe;IACtC,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,iBAAiB,CAAC,CAAC,CAAC,WAAW,CAAC;IAClE,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI;QACA;;;;;;UAME;QACF,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,2 +1,2 @@
|
|
|
1
|
-
declare const Module: import("../../../wasm/zstd.g.js").Module;
|
|
2
|
-
export { Module };
|
|
1
|
+
declare const Module: import("../../../wasm/zstd.g.js").Module;
|
|
2
|
+
export { Module };
|
package/dist/zstd/wasm/module.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import createModule from '../../../wasm/zstd.g.js';
|
|
2
|
-
const Module = await createModule();
|
|
3
|
-
export { Module };
|
|
1
|
+
import createModule from '../../../wasm/zstd.g.js';
|
|
2
|
+
const Module = await createModule();
|
|
3
|
+
export { Module };
|
|
4
4
|
//# sourceMappingURL=module.js.map
|