zstd-stream 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/LICENSE +21 -0
- package/README.md +285 -0
- package/dist/compressor.d.ts +27 -0
- package/dist/compressor.js +155 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +148 -0
- package/dist/lib.d.ts +1 -0
- package/dist/lib.js +2 -0
- package/dist/loader.d.ts +3 -0
- package/dist/loader.js +106 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.js +1 -0
- package/package.json +63 -0
package/dist/loader.d.ts
ADDED
package/dist/loader.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { lib } from "./lib.js";
|
|
2
|
+
let module = null;
|
|
3
|
+
let initPromise = null;
|
|
4
|
+
const isNode = typeof process !== "undefined" && process.versions?.node != null;
|
|
5
|
+
async function loadGzipped() {
|
|
6
|
+
if (isNode) {
|
|
7
|
+
// Node.js path
|
|
8
|
+
const { gunzip } = await import("node:zlib");
|
|
9
|
+
const { promisify } = await import("node:util");
|
|
10
|
+
const binaryData = Buffer.from(lib, "base64");
|
|
11
|
+
const decompressed = await promisify(gunzip)(binaryData);
|
|
12
|
+
return decompressed.toString("utf-8");
|
|
13
|
+
}
|
|
14
|
+
// Browser path
|
|
15
|
+
const binaryString = atob(lib);
|
|
16
|
+
const binaryData = Uint8Array.from(binaryString, (char) => char.charCodeAt(0));
|
|
17
|
+
const stream = new Blob([binaryData])
|
|
18
|
+
.stream()
|
|
19
|
+
.pipeThrough(new DecompressionStream("gzip"));
|
|
20
|
+
const buffer = await new Response(stream).arrayBuffer();
|
|
21
|
+
return new TextDecoder().decode(buffer);
|
|
22
|
+
}
|
|
23
|
+
async function evaluateCode(code) {
|
|
24
|
+
if (isNode) {
|
|
25
|
+
try {
|
|
26
|
+
// Try using data URL import (works in Node.js v22+)
|
|
27
|
+
const url = `data:text/javascript,${encodeURIComponent(code)}`;
|
|
28
|
+
const mod = await import(/* @vite-ignore */ url);
|
|
29
|
+
return mod.default || mod.ZstdWasm || mod;
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
if (e.code !== "ERR_UNKNOWN_FILE_EXTENSION") {
|
|
33
|
+
console.debug("Data URL import failed:", e?.message);
|
|
34
|
+
}
|
|
35
|
+
const vm = await import("node:vm");
|
|
36
|
+
// Create a context with required globals
|
|
37
|
+
const context = vm.createContext({
|
|
38
|
+
...globalThis, // Start with real globals
|
|
39
|
+
// Override only what's needed
|
|
40
|
+
console,
|
|
41
|
+
TextEncoder,
|
|
42
|
+
TextDecoder,
|
|
43
|
+
});
|
|
44
|
+
// Create and evaluate as ESM
|
|
45
|
+
const mod = new vm.SourceTextModule(code, {
|
|
46
|
+
context,
|
|
47
|
+
identifier: "zstd-bundle.js",
|
|
48
|
+
importModuleDynamically: async (specifier) => {
|
|
49
|
+
// Allow dynamic ESM imports (e.g., import("node:buffer"))
|
|
50
|
+
// Most embedded bundles won't use this, but safe to allow
|
|
51
|
+
try {
|
|
52
|
+
return await import(/* @vite-ignore */ specifier);
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
console.warn(`⚠️ Dynamic import failed: ${specifier}`, e.message);
|
|
56
|
+
throw new Error(`Dynamic import not supported: ${specifier}`);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
// Link — since it's a self-contained bundle, it likely has no static imports
|
|
61
|
+
// If it does, you'd need a resolver — but most don't
|
|
62
|
+
await mod.link((specifier) => {
|
|
63
|
+
throw new Error(`Static import not supported: ${specifier}`);
|
|
64
|
+
});
|
|
65
|
+
await mod.evaluate();
|
|
66
|
+
const ns = mod.namespace;
|
|
67
|
+
return ns.default || ns.ZstdWasm || ns;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Browser path — unchanged
|
|
71
|
+
const { URL } = globalThis;
|
|
72
|
+
const blob = new Blob([code], { type: "application/javascript" });
|
|
73
|
+
const url = URL.createObjectURL(blob);
|
|
74
|
+
try {
|
|
75
|
+
const mod = await import(/* @vite-ignore */ url);
|
|
76
|
+
return mod.default || mod.ZstdWasm || mod;
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
URL.revokeObjectURL(url);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export async function initZstd() {
|
|
83
|
+
if (initPromise)
|
|
84
|
+
return initPromise;
|
|
85
|
+
if (module)
|
|
86
|
+
return;
|
|
87
|
+
initPromise = (async () => {
|
|
88
|
+
try {
|
|
89
|
+
const code = await loadGzipped();
|
|
90
|
+
const loader = await evaluateCode(code);
|
|
91
|
+
module = await loader();
|
|
92
|
+
if (!module)
|
|
93
|
+
throw new Error("WASM module initialization returned null");
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
initPromise = null;
|
|
97
|
+
throw new Error(`Failed to initialize Zstandard: ${error instanceof Error ? error.message : String(error)}`);
|
|
98
|
+
}
|
|
99
|
+
})();
|
|
100
|
+
return initPromise;
|
|
101
|
+
}
|
|
102
|
+
export function getModule() {
|
|
103
|
+
if (!module)
|
|
104
|
+
throw new Error("Module not initialized. Call initZstd() first.");
|
|
105
|
+
return module;
|
|
106
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Module {
|
|
2
|
+
_createCCtx(): number;
|
|
3
|
+
_createDCtx(): number;
|
|
4
|
+
_freeCCtx(ctx: number): void;
|
|
5
|
+
_freeDCtx(ctx: number): void;
|
|
6
|
+
_initCStream(ctx: number, level: number): number;
|
|
7
|
+
_initDStream(ctx: number): number;
|
|
8
|
+
_compressStream(ctx: number, dst: number, dstSize: number, src: number, srcSize: number, end: number): number;
|
|
9
|
+
_decompressStream(ctx: number, dst: number, dstSize: number, src: number, srcSize: number): number;
|
|
10
|
+
_dStreamOutSize(): number;
|
|
11
|
+
_cStreamOutSize(): number;
|
|
12
|
+
_getErrorName(error: number): string;
|
|
13
|
+
_malloc(size: number): number;
|
|
14
|
+
_free(ptr: number): void;
|
|
15
|
+
HEAPU8: Uint8Array;
|
|
16
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zstd-stream",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple and efficient Zstandard compression/decompression for Node.js and browsers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"clean": "rimraf dist",
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"rebuild": "npm run clean && npm run build",
|
|
22
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
23
|
+
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
|
|
24
|
+
"test:coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
|
|
25
|
+
"test:unit": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathIgnorePatterns=integration",
|
|
26
|
+
"test:integration": "node --experimental-vm-modules node_modules/jest/bin/jest.js integration.test.ts",
|
|
27
|
+
"prepare": "npm run build",
|
|
28
|
+
"prepack": "npm run clean && npm run build",
|
|
29
|
+
"prepublishOnly": "npm test",
|
|
30
|
+
"pack:check": "npm run prepack && npm pack --dry-run",
|
|
31
|
+
"publish:dry": "npm publish --dry-run"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"zstd",
|
|
35
|
+
"zstandard",
|
|
36
|
+
"compression",
|
|
37
|
+
"decompression",
|
|
38
|
+
"wasm",
|
|
39
|
+
"stream",
|
|
40
|
+
"esm"
|
|
41
|
+
],
|
|
42
|
+
"author": "Crellin Creative",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/crellincreative/zstd-stream.git"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/crellincreative/zstd-stream/issues"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/jest": "^30.0.0",
|
|
53
|
+
"@types/node": "^24.10.3",
|
|
54
|
+
"jest": "^30.2.0",
|
|
55
|
+
"rimraf": "^6.1.2",
|
|
56
|
+
"ts-jest": "^29.4.6",
|
|
57
|
+
"ts-node": "^10.9.2",
|
|
58
|
+
"typescript": "^5.3.3"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=18.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|