@hpcc-js/wasm-zstd 1.0.0
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/README.md +20 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +7 -0
- package/package.json +57 -0
- package/src/index.ts +1 -0
- package/src/wasm-library.ts +59 -0
- package/src/zstd.ts +130 -0
- package/types/index.d.ts +1 -0
- package/types/wasm-library.d.ts +20 -0
- package/types/zstd.d.ts +60 -0
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hpcc-js/wasm-zstd",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "hpcc-js - WASM zstd",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./types/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/**/*",
|
|
15
|
+
"src/**/*",
|
|
16
|
+
"types/**/*"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"clean": "rimraf ./dist ./dist-test ./types",
|
|
20
|
+
"build-types": "tsc --project tsconfig.json --emitDeclarationOnly",
|
|
21
|
+
"build-types-watch": "npm run build-types -- --watch",
|
|
22
|
+
"build-ts": "node esbuild.mjs",
|
|
23
|
+
"build-ts-dev": "npm run build-ts -- --mode=development",
|
|
24
|
+
"build-ts-watch": "npm run compile-ts-dev -- --watch",
|
|
25
|
+
"build-dev": "run-p build-types build-ts-dev",
|
|
26
|
+
"build": "run-p build-types build-ts",
|
|
27
|
+
"lint-skypack": "npx -y @skypack/package-check",
|
|
28
|
+
"lint-eslint": "eslint src/**/*.ts",
|
|
29
|
+
"lint": "run-p lint-eslint lint-skypack",
|
|
30
|
+
"test-chrome": "karma start --single-run --browsers ChromiumHeadless karma.conf.cjs",
|
|
31
|
+
"test-firefox": "karma start --single-run --browsers Firefox karma.conf.cjs",
|
|
32
|
+
"test-node": "mocha ./dist-test/index.node.js --reporter spec",
|
|
33
|
+
"test": "run-s test-chrome test-node"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"yargs": "17.7.2"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@hpcc-js/esbuild-plugins": "1.0.0"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"graphviz",
|
|
43
|
+
"typescript",
|
|
44
|
+
"webassembly",
|
|
45
|
+
"wasm",
|
|
46
|
+
"dot",
|
|
47
|
+
"neato",
|
|
48
|
+
"twopi"
|
|
49
|
+
],
|
|
50
|
+
"author": "hpcc-systems",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/hpcc-systems/hpcc-js-wasm.git"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://hpcc-systems.github.io/hpcc-js-wasm/",
|
|
56
|
+
"license": "Apache-2.0"
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./zstd.ts";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type PTR = number;
|
|
2
|
+
export interface HeapU8 {
|
|
3
|
+
ptr: PTR;
|
|
4
|
+
size: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Base class to simplify moving data into and out of Wasm memory.
|
|
9
|
+
*/
|
|
10
|
+
export class WasmLibrary {
|
|
11
|
+
|
|
12
|
+
protected _module: any;
|
|
13
|
+
protected _exports: any;
|
|
14
|
+
|
|
15
|
+
protected constructor(_module: any, _export: any) {
|
|
16
|
+
this._module = _module;
|
|
17
|
+
this._exports = _export;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
protected malloc_heapu8(size: number): HeapU8 {
|
|
21
|
+
const ptr: PTR = this._exports.malloc(size);
|
|
22
|
+
return {
|
|
23
|
+
ptr,
|
|
24
|
+
size
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected free_heapu8(data: HeapU8) {
|
|
29
|
+
this._exports.free(data.ptr);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected uint8_heapu8(data: Uint8Array): HeapU8 {
|
|
33
|
+
const retVal = this.malloc_heapu8(data.byteLength);
|
|
34
|
+
this._module.HEAPU8.set(data, retVal.ptr);
|
|
35
|
+
return retVal;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected heapu8_view(data: HeapU8): Uint8Array {
|
|
39
|
+
return this._module.HEAPU8.subarray(data.ptr, data.ptr + data.size);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected heapu8_uint8(data: HeapU8): Uint8Array {
|
|
43
|
+
return new Uint8Array([...this.heapu8_view(data)]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected string_heapu8(str: string): HeapU8 {
|
|
47
|
+
const data = Uint8Array.from(str, x => x.charCodeAt(0));
|
|
48
|
+
return this.uint8_heapu8(data);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected heapu8_string(data: HeapU8): string {
|
|
52
|
+
const retVal = Array.from({ length: data.size });
|
|
53
|
+
const submodule = this._module.HEAPU8.subarray(data.ptr, data.ptr + data.size);
|
|
54
|
+
submodule.forEach((c: number, i: number) => {
|
|
55
|
+
retVal[i] = String.fromCharCode(c);
|
|
56
|
+
});
|
|
57
|
+
return retVal.join("");
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/zstd.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import load, { reset } from "../../../build/src-cpp/zstd/zstdlib.wasm";
|
|
3
|
+
import { WasmLibrary } from "./wasm-library.ts";
|
|
4
|
+
|
|
5
|
+
// Ref: http://facebook.github.io/zstd/zstd_manual.html
|
|
6
|
+
// Ref: https://github.com/facebook/zstd
|
|
7
|
+
|
|
8
|
+
let g_zstd: Promise<Zstd>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The Zstandard WASM library, provides a simplified wrapper around the Zstandard c++ library.
|
|
12
|
+
*
|
|
13
|
+
* See [Zstandard](https://facebook.github.io/zstd/) for more details.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Zstd } from "@hpcc-js/wasm/zstd";
|
|
17
|
+
*
|
|
18
|
+
* const zstd = await Zstd.load();
|
|
19
|
+
*
|
|
20
|
+
* // Generate some "data"
|
|
21
|
+
* const data = new Uint8Array(Array.from({ length: 100000 }, (_, i) => i % 256));
|
|
22
|
+
*
|
|
23
|
+
* const compressed_data = zstd.compress(data);
|
|
24
|
+
* const decompressed_data = zstd.decompress(compressed_data);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export class Zstd extends WasmLibrary {
|
|
28
|
+
|
|
29
|
+
private constructor(_module: any) {
|
|
30
|
+
super(_module, _module.zstd.prototype);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Compiles and instantiates the raw wasm.
|
|
35
|
+
*
|
|
36
|
+
* ::: info
|
|
37
|
+
* In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing `load` to be asynchronous;
|
|
38
|
+
* :::
|
|
39
|
+
*
|
|
40
|
+
* @returns A promise to an instance of the Zstd class.
|
|
41
|
+
*/
|
|
42
|
+
static load(): Promise<Zstd> {
|
|
43
|
+
if (!g_zstd) {
|
|
44
|
+
g_zstd = load().then((module: any) => {
|
|
45
|
+
return new Zstd(module)
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return g_zstd;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Unloades the compiled wasm instance.
|
|
53
|
+
*/
|
|
54
|
+
static unload() {
|
|
55
|
+
reset();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @returns The Zstd c++ version
|
|
60
|
+
*/
|
|
61
|
+
version(): string {
|
|
62
|
+
return this._exports.version();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param data Data to be compressed
|
|
67
|
+
* @param compressionLevel Compression v Speed tradeoff, when omitted it will default to `zstd.defaultCLevel()` which is currently 3.
|
|
68
|
+
* @returns Compressed data.
|
|
69
|
+
*
|
|
70
|
+
* :::tip
|
|
71
|
+
* A note on compressionLevel: The library supports regular compression levels from 1 up o 22. Levels >= 20, should be used with caution, as they require more memory. The library also offers negative compression levels, which extend the range of speed vs. ratio preferences. The lower the level, the faster the speed (at the cost of compression).
|
|
72
|
+
* :::
|
|
73
|
+
*/
|
|
74
|
+
compress(data: Uint8Array, compressionLevel: number = this.defaultCLevel()): Uint8Array {
|
|
75
|
+
const uncompressed = this.uint8_heapu8(data);
|
|
76
|
+
|
|
77
|
+
const compressedSize = this._exports.compressBound(data.length);
|
|
78
|
+
const compressed = this.malloc_heapu8(compressedSize);
|
|
79
|
+
compressed.size = this._exports.compress(compressed.ptr, compressedSize, uncompressed.ptr, uncompressed.size, compressionLevel);
|
|
80
|
+
/* istanbul ignore if */
|
|
81
|
+
if (this._exports.isError(compressed.size)) {
|
|
82
|
+
console.error(this._exports.getErrorName(compressed.size));
|
|
83
|
+
}
|
|
84
|
+
const retVal = this.heapu8_uint8(compressed);
|
|
85
|
+
|
|
86
|
+
this.free_heapu8(compressed);
|
|
87
|
+
this.free_heapu8(uncompressed);
|
|
88
|
+
return retVal;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param compressedData Data to be compressed
|
|
93
|
+
* @returns Uncompressed data.
|
|
94
|
+
*/
|
|
95
|
+
decompress(compressedData: Uint8Array): Uint8Array {
|
|
96
|
+
const compressed = this.uint8_heapu8(compressedData);
|
|
97
|
+
const uncompressedSize = this._exports.getFrameContentSize(compressed.ptr, compressed.size);
|
|
98
|
+
/* istanbul ignore if */
|
|
99
|
+
if (this._exports.isError(uncompressedSize)) {
|
|
100
|
+
console.error(this._exports.getErrorName(uncompressedSize));
|
|
101
|
+
}
|
|
102
|
+
const uncompressed = this.malloc_heapu8(uncompressedSize);
|
|
103
|
+
|
|
104
|
+
uncompressed.size = this._exports.decompress(uncompressed.ptr, uncompressedSize, compressed.ptr, compressed.size);
|
|
105
|
+
/* istanbul ignore if */
|
|
106
|
+
if (this._exports.isError(uncompressed.size)) {
|
|
107
|
+
console.error(this._exports.getErrorName(uncompressed.size));
|
|
108
|
+
}
|
|
109
|
+
const retVal = this.heapu8_uint8(uncompressed);
|
|
110
|
+
|
|
111
|
+
this.free_heapu8(uncompressed);
|
|
112
|
+
this.free_heapu8(compressed);
|
|
113
|
+
return retVal;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @returns Default compression level (see notes above above).
|
|
118
|
+
*/
|
|
119
|
+
defaultCLevel(): number {
|
|
120
|
+
return this._exports.defaultCLevel();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
minCLevel(): number {
|
|
124
|
+
return this._exports.minCLevel();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
maxCLevel(): number {
|
|
128
|
+
return this._exports.maxCLevel();
|
|
129
|
+
}
|
|
130
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./zstd.ts";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type PTR = number;
|
|
2
|
+
export interface HeapU8 {
|
|
3
|
+
ptr: PTR;
|
|
4
|
+
size: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Base class to simplify moving data into and out of Wasm memory.
|
|
8
|
+
*/
|
|
9
|
+
export declare class WasmLibrary {
|
|
10
|
+
protected _module: any;
|
|
11
|
+
protected _exports: any;
|
|
12
|
+
protected constructor(_module: any, _export: any);
|
|
13
|
+
protected malloc_heapu8(size: number): HeapU8;
|
|
14
|
+
protected free_heapu8(data: HeapU8): void;
|
|
15
|
+
protected uint8_heapu8(data: Uint8Array): HeapU8;
|
|
16
|
+
protected heapu8_view(data: HeapU8): Uint8Array;
|
|
17
|
+
protected heapu8_uint8(data: HeapU8): Uint8Array;
|
|
18
|
+
protected string_heapu8(str: string): HeapU8;
|
|
19
|
+
protected heapu8_string(data: HeapU8): string;
|
|
20
|
+
}
|
package/types/zstd.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { WasmLibrary } from "./wasm-library.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The Zstandard WASM library, provides a simplified wrapper around the Zstandard c++ library.
|
|
4
|
+
*
|
|
5
|
+
* See [Zstandard](https://facebook.github.io/zstd/) for more details.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { Zstd } from "@hpcc-js/wasm/zstd";
|
|
9
|
+
*
|
|
10
|
+
* const zstd = await Zstd.load();
|
|
11
|
+
*
|
|
12
|
+
* // Generate some "data"
|
|
13
|
+
* const data = new Uint8Array(Array.from({ length: 100000 }, (_, i) => i % 256));
|
|
14
|
+
*
|
|
15
|
+
* const compressed_data = zstd.compress(data);
|
|
16
|
+
* const decompressed_data = zstd.decompress(compressed_data);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare class Zstd extends WasmLibrary {
|
|
20
|
+
private constructor();
|
|
21
|
+
/**
|
|
22
|
+
* Compiles and instantiates the raw wasm.
|
|
23
|
+
*
|
|
24
|
+
* ::: info
|
|
25
|
+
* In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing `load` to be asynchronous;
|
|
26
|
+
* :::
|
|
27
|
+
*
|
|
28
|
+
* @returns A promise to an instance of the Zstd class.
|
|
29
|
+
*/
|
|
30
|
+
static load(): Promise<Zstd>;
|
|
31
|
+
/**
|
|
32
|
+
* Unloades the compiled wasm instance.
|
|
33
|
+
*/
|
|
34
|
+
static unload(): void;
|
|
35
|
+
/**
|
|
36
|
+
* @returns The Zstd c++ version
|
|
37
|
+
*/
|
|
38
|
+
version(): string;
|
|
39
|
+
/**
|
|
40
|
+
* @param data Data to be compressed
|
|
41
|
+
* @param compressionLevel Compression v Speed tradeoff, when omitted it will default to `zstd.defaultCLevel()` which is currently 3.
|
|
42
|
+
* @returns Compressed data.
|
|
43
|
+
*
|
|
44
|
+
* :::tip
|
|
45
|
+
* A note on compressionLevel: The library supports regular compression levels from 1 up o 22. Levels >= 20, should be used with caution, as they require more memory. The library also offers negative compression levels, which extend the range of speed vs. ratio preferences. The lower the level, the faster the speed (at the cost of compression).
|
|
46
|
+
* :::
|
|
47
|
+
*/
|
|
48
|
+
compress(data: Uint8Array, compressionLevel?: number): Uint8Array;
|
|
49
|
+
/**
|
|
50
|
+
* @param compressedData Data to be compressed
|
|
51
|
+
* @returns Uncompressed data.
|
|
52
|
+
*/
|
|
53
|
+
decompress(compressedData: Uint8Array): Uint8Array;
|
|
54
|
+
/**
|
|
55
|
+
* @returns Default compression level (see notes above above).
|
|
56
|
+
*/
|
|
57
|
+
defaultCLevel(): number;
|
|
58
|
+
minCLevel(): number;
|
|
59
|
+
maxCLevel(): number;
|
|
60
|
+
}
|