@cloudpss/compress 0.3.0-alpha.28 → 0.3.0-beta.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/index.d.ts +7 -8
- package/dist/index.js +10 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +19 -14
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
/** 可用的压缩算法 */
|
|
2
|
-
export declare type
|
|
2
|
+
export declare type CompressionAlgorithm = 'zstd' | 'gzip' | 'none';
|
|
3
3
|
/** 压缩算法的魔数 */
|
|
4
4
|
export declare const MAGIC_NUMBERS: {
|
|
5
5
|
zstd: number[];
|
|
6
6
|
gzip: number[];
|
|
7
7
|
};
|
|
8
8
|
/** 使用指定算法压缩 */
|
|
9
|
-
export declare function compress(data: Uint8Array, algorithm?:
|
|
10
|
-
/**
|
|
11
|
-
declare type
|
|
12
|
-
algorithm:
|
|
9
|
+
export declare function compress(data: Uint8Array, algorithm?: CompressionAlgorithm): CompressionResult;
|
|
10
|
+
/** 压缩/解压缩结果 */
|
|
11
|
+
export declare type CompressionResult = Promise<Uint8Array> & {
|
|
12
|
+
algorithm: CompressionAlgorithm;
|
|
13
13
|
};
|
|
14
14
|
/** 探测压缩算法 */
|
|
15
|
-
export declare function detectCompression(data: Uint8Array):
|
|
15
|
+
export declare function detectCompression(data: Uint8Array): CompressionAlgorithm;
|
|
16
16
|
/** 探测压缩算法并解压缩 */
|
|
17
|
-
export declare function decompress(data: Uint8Array):
|
|
18
|
-
export {};
|
|
17
|
+
export declare function decompress(data: Uint8Array): CompressionResult;
|
package/dist/index.js
CHANGED
|
@@ -10,15 +10,19 @@ const modules = {
|
|
|
10
10
|
/** 加载相关库 */
|
|
11
11
|
function loadLib(algorithm) {
|
|
12
12
|
if (!(algorithm in modules))
|
|
13
|
-
throw new Error();
|
|
13
|
+
throw new Error(`Unknown compression algorithm: ${algorithm}`);
|
|
14
14
|
return modules[algorithm]();
|
|
15
15
|
}
|
|
16
16
|
/** 使用指定算法压缩 */
|
|
17
|
-
export
|
|
18
|
-
if (algorithm === 'none')
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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;
|
|
22
26
|
}
|
|
23
27
|
/** 探测压缩算法 */
|
|
24
28
|
export function detectCompression(data) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc;AACd,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC;IAC9B,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;CACrB,CAAC;AAEF,MAAM,OAAO,GAAG;IACZ,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,iBAAiB,CAAC;IACrC,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,iBAAiB,CAAC;CACxC,CAAC;AAEF,YAAY;AACZ,SAAS,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc;AACd,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC;IAC9B,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;CACrB,CAAC;AAEF,MAAM,OAAO,GAAG;IACZ,IAAI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KAAK,WAAW,CAAC,CAAC,CAAC,EAAE;gBAC5B,KAAK,GAAG,KAAK,CAAC;gBACd,MAAM;aACT;SACJ;QACD,IAAI,KAAK;YAAE,OAAO,CAAC,CAAC;KACvB;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,iBAAiB;AACjB,MAAM,UAAU,UAAU,CAAC,IAAgB;IACvC,MAAM,SAAS,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAE1C,IAAI,SAAS,KAAK,MAAM,EAAE;QACtB,SAAS;QACT,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAsB,CAAC;QAC1D,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC;QAC1B,OAAO,MAAM,CAAC;KACjB;IAED,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAsB,CAAC;IAClG,MAAM,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/compress",
|
|
3
|
-
"version": "0.3.0-
|
|
3
|
+
"version": "0.3.0-beta.2",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@bokuweb/zstd-wasm": "^0.0.14",
|
|
31
|
-
"@types/node": "^16.11.
|
|
31
|
+
"@types/node": "^16.11.11",
|
|
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.
|
|
35
|
+
"type-fest": "^2.8.0",
|
|
36
36
|
"wasm-flate": "1.0.2"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** 可用的压缩算法 */
|
|
2
|
-
export type
|
|
2
|
+
export type CompressionAlgorithm = 'zstd' | 'gzip' | 'none';
|
|
3
3
|
|
|
4
4
|
/** 压缩算法的魔数 */
|
|
5
5
|
export const MAGIC_NUMBERS = {
|
|
@@ -13,23 +13,28 @@ const modules = {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
/** 加载相关库 */
|
|
16
|
-
function loadLib(algorithm:
|
|
17
|
-
if (!(algorithm in modules)) throw new Error();
|
|
18
|
-
return modules[algorithm
|
|
16
|
+
function loadLib(algorithm: Exclude<CompressionAlgorithm, 'none'>): ReturnType<typeof modules[keyof typeof modules]> {
|
|
17
|
+
if (!(algorithm in modules)) throw new Error(`Unknown compression algorithm: ${algorithm}`);
|
|
18
|
+
return modules[algorithm]();
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/** 使用指定算法压缩 */
|
|
22
|
-
export
|
|
23
|
-
if (algorithm === 'none')
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
export function compress(data: Uint8Array, algorithm: CompressionAlgorithm = 'zstd'): CompressionResult {
|
|
23
|
+
if (algorithm === 'none') {
|
|
24
|
+
const ret = Promise.resolve(data) as CompressionResult;
|
|
25
|
+
ret.algorithm = 'none';
|
|
26
|
+
return ret;
|
|
27
|
+
}
|
|
28
|
+
const ret = loadLib(algorithm).then((lib) => lib.compress(data)) as CompressionResult;
|
|
29
|
+
ret.algorithm = algorithm;
|
|
30
|
+
return ret;
|
|
26
31
|
}
|
|
27
32
|
|
|
28
|
-
/**
|
|
29
|
-
type
|
|
33
|
+
/** 压缩/解压缩结果 */
|
|
34
|
+
export type CompressionResult = Promise<Uint8Array> & { algorithm: CompressionAlgorithm };
|
|
30
35
|
|
|
31
36
|
/** 探测压缩算法 */
|
|
32
|
-
export function detectCompression(data: Uint8Array):
|
|
37
|
+
export function detectCompression(data: Uint8Array): CompressionAlgorithm {
|
|
33
38
|
for (const key in MAGIC_NUMBERS) {
|
|
34
39
|
const k = key as keyof typeof MAGIC_NUMBERS;
|
|
35
40
|
const magicNumber = MAGIC_NUMBERS[k];
|
|
@@ -47,17 +52,17 @@ export function detectCompression(data: Uint8Array): Algorithm {
|
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
/** 探测压缩算法并解压缩 */
|
|
50
|
-
export function decompress(data: Uint8Array):
|
|
55
|
+
export function decompress(data: Uint8Array): CompressionResult {
|
|
51
56
|
const algorithm = detectCompression(data);
|
|
52
57
|
|
|
53
58
|
if (algorithm === 'none') {
|
|
54
59
|
// 不是压缩数据
|
|
55
|
-
const result = Promise.resolve(data) as
|
|
60
|
+
const result = Promise.resolve(data) as CompressionResult;
|
|
56
61
|
result.algorithm = 'none';
|
|
57
62
|
return result;
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
const result = loadLib(algorithm).then(({ decompress }) => decompress(data)) as
|
|
65
|
+
const result = loadLib(algorithm).then(({ decompress }) => decompress(data)) as CompressionResult;
|
|
61
66
|
result.algorithm = algorithm;
|
|
62
67
|
return result;
|
|
63
68
|
}
|