@structured-world/structured-zstd 0.0.30
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 +202 -0
- package/README.md +93 -0
- package/index.d.ts +68 -0
- package/index.d.ts.map +1 -0
- package/index.js +109 -0
- package/index.js.map +1 -0
- package/index.ts +159 -0
- package/package.json +74 -0
- package/scalar/structured_zstd_wasm.d.ts +100 -0
- package/scalar/structured_zstd_wasm.js +315 -0
- package/scalar/structured_zstd_wasm_bg.wasm +0 -0
- package/scalar/structured_zstd_wasm_bg.wasm.d.ts +16 -0
- package/simd/structured_zstd_wasm.d.ts +100 -0
- package/simd/structured_zstd_wasm.js +315 -0
- package/simd/structured_zstd_wasm_bg.wasm +0 -0
- package/simd/structured_zstd_wasm_bg.wasm.d.ts +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Structured World Foundation
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# @structured-world/structured-zstd
|
|
2
|
+
|
|
3
|
+
Pure-Rust [Zstandard](https://facebook.github.io/zstd/) (zstd) codec compiled
|
|
4
|
+
to WebAssembly. Compress and decompress in the **browser, Node.js and Deno** —
|
|
5
|
+
no native addons, no FFI, no build step for consumers.
|
|
6
|
+
|
|
7
|
+
- **Automatic SIMD.** Ships two payloads — one built with the WebAssembly
|
|
8
|
+
`simd128` SIMD tier, one scalar — and picks the fast one at runtime via
|
|
9
|
+
engine feature detection. You import one package; the right `.wasm` loads.
|
|
10
|
+
- **Pure ESM, strict TypeScript.** First-class types, tree-shakeable, `type:
|
|
11
|
+
module`.
|
|
12
|
+
- **Interoperable.** Frames produced here decode in the native C `zstd`, and
|
|
13
|
+
frames from the C library decode here.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @structured-world/structured-zstd
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { compress, decompress } from "@structured-world/structured-zstd";
|
|
25
|
+
|
|
26
|
+
const data = new TextEncoder().encode("hello, zstd ".repeat(1000));
|
|
27
|
+
|
|
28
|
+
// `level` follows the zstd scale: 1..=22 (higher = smaller/slower),
|
|
29
|
+
// negatives (-7..=-1) for the ultra-fast tier. Defaults to 3.
|
|
30
|
+
const framed = await compress(data, 19);
|
|
31
|
+
const plain = await decompress(framed);
|
|
32
|
+
|
|
33
|
+
console.assert(plain.length === data.length);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The codec initialises lazily on first call. To pre-warm it (e.g. before a
|
|
37
|
+
latency-sensitive path), call `init()`:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { init, compress } from "@structured-world/structured-zstd";
|
|
41
|
+
|
|
42
|
+
await init(); // selects + instantiates the best payload up front
|
|
43
|
+
const out = await compress(payload);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
| Export | Signature | Notes |
|
|
49
|
+
|--------|-----------|-------|
|
|
50
|
+
| `compress` | `(data: Uint8Array, level?: number) => Promise<Uint8Array>` | `level` defaults to `3`. |
|
|
51
|
+
| `decompress` | `(data: Uint8Array) => Promise<Uint8Array>` | Rejects on a malformed/incomplete frame. |
|
|
52
|
+
| `compressUsingDict` | `(data: Uint8Array, dict: Uint8Array, level?: number) => Promise<Uint8Array>` | Dictionary compression (raw zstd dictionary, e.g. `zstd --train`). |
|
|
53
|
+
| `decompressUsingDict` | `(data: Uint8Array, dict: Uint8Array) => Promise<Uint8Array>` | `dict` must match the one used to compress. |
|
|
54
|
+
| `createDecompressStream` | `() => Promise<DecompressStream>` | Incremental streaming decoder (see below). |
|
|
55
|
+
| `init` | `() => Promise<void>` | Optional pre-warm; idempotent. |
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { compressUsingDict, decompressUsingDict } from "@structured-world/structured-zstd";
|
|
59
|
+
const framed = await compressUsingDict(record, dictionary, 19);
|
|
60
|
+
const back = await decompressUsingDict(framed, dictionary);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Streaming decompression
|
|
64
|
+
|
|
65
|
+
Decode a frame incrementally without buffering all of it — feed compressed
|
|
66
|
+
chunks (e.g. from a `fetch` body) and read output as it arrives:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { createDecompressStream } from "@structured-world/structured-zstd";
|
|
70
|
+
|
|
71
|
+
const stream = await createDecompressStream();
|
|
72
|
+
const out: Uint8Array[] = [];
|
|
73
|
+
for await (const chunk of response.body) out.push(stream.push(chunk));
|
|
74
|
+
out.push(stream.finish()); // throws if the stream ended mid-frame
|
|
75
|
+
stream.free(); // release the wasm handle
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`push(chunk)` returns whatever output is available so far (possibly empty
|
|
79
|
+
while a block is still incomplete); the decoder window is held wasm-side
|
|
80
|
+
across chunks.
|
|
81
|
+
|
|
82
|
+
## How the payload is selected
|
|
83
|
+
|
|
84
|
+
WebAssembly has no runtime CPU detection, so SIMD selection is an engine
|
|
85
|
+
**capability** probe ([`wasm-feature-detect`](https://www.npmjs.com/package/wasm-feature-detect)
|
|
86
|
+
`simd()`): when the host supports wasm SIMD the `simd128` payload loads,
|
|
87
|
+
otherwise the scalar one. In Node.js the `.wasm` bytes are read from disk; in
|
|
88
|
+
the browser/Deno they are fetched relative to the module.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
[Apache-2.0](./LICENSE) © Structured World Foundation. Built from
|
|
93
|
+
[`structured-zstd`](https://github.com/structured-world/structured-zstd).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@structured-world/structured-zstd` — pure-Rust Zstandard codec compiled to
|
|
3
|
+
* WebAssembly, with automatic selection of the SIMD (`simd128`) or scalar
|
|
4
|
+
* payload based on the host engine's capabilities.
|
|
5
|
+
*
|
|
6
|
+
* Pure ESM, strict TypeScript. One async API for browser, Node.js and Deno:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { compress, decompress } from "@structured-world/structured-zstd";
|
|
10
|
+
* const framed = await compress(new TextEncoder().encode("hello"), 19);
|
|
11
|
+
* const plain = await decompress(framed);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The codec is initialised lazily on first use; call {@link init} explicitly
|
|
15
|
+
* to pre-warm it (e.g. before a latency-sensitive path).
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Incremental streaming decompressor handle. Feed compressed chunks with
|
|
19
|
+
* {@link DecompressStream.push} and read decompressed output as it becomes
|
|
20
|
+
* available; call {@link DecompressStream.finish} at end of input. The decoder
|
|
21
|
+
* window is held on the wasm side across chunks, so a large frame never needs
|
|
22
|
+
* to be fully buffered. Call {@link DecompressStream.free} when done.
|
|
23
|
+
*/
|
|
24
|
+
export interface DecompressStream {
|
|
25
|
+
/** Feed compressed bytes; returns decompressed output available so far. */
|
|
26
|
+
push(chunk: Uint8Array): Uint8Array;
|
|
27
|
+
/** Signal end of input; returns the final bytes. Throws if incomplete. */
|
|
28
|
+
finish(): Uint8Array;
|
|
29
|
+
/** Release the underlying wasm handle. */
|
|
30
|
+
free(): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Initialise the codec (select + instantiate the best payload) ahead of time.
|
|
34
|
+
* Idempotent and concurrency-safe; resolves once the module is ready.
|
|
35
|
+
*/
|
|
36
|
+
export declare function init(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Compress `data` into a Zstandard frame at `level` (zstd scale: `1..=22`,
|
|
39
|
+
* negatives for the ultra-fast tier; defaults to {@link DEFAULT_LEVEL}). The
|
|
40
|
+
* frame decodes in any compliant zstd decoder, including the native C library.
|
|
41
|
+
*/
|
|
42
|
+
export declare function compress(data: Uint8Array, level?: number): Promise<Uint8Array>;
|
|
43
|
+
/**
|
|
44
|
+
* Decompress a complete Zstandard frame. Rejects if the input is not a valid,
|
|
45
|
+
* complete frame.
|
|
46
|
+
*/
|
|
47
|
+
export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
|
|
48
|
+
/**
|
|
49
|
+
* Compress `data` against a raw Zstandard `dict` (e.g. from `zstd --train`) at
|
|
50
|
+
* `level` (defaults to {@link DEFAULT_LEVEL}). Mirrors C
|
|
51
|
+
* `ZSTD_compress_usingDict` — small, similar payloads compress far better.
|
|
52
|
+
* Rejects if the dictionary is invalid.
|
|
53
|
+
*/
|
|
54
|
+
export declare function compressUsingDict(data: Uint8Array, dict: Uint8Array, level?: number): Promise<Uint8Array>;
|
|
55
|
+
/**
|
|
56
|
+
* Decompress a dictionary-encoded frame. `dict` must be the same raw
|
|
57
|
+
* dictionary used to compress it. Mirrors C `ZSTD_decompress_usingDict`.
|
|
58
|
+
* Rejects on a malformed frame or dictionary mismatch.
|
|
59
|
+
*/
|
|
60
|
+
export declare function decompressUsingDict(data: Uint8Array, dict: Uint8Array): Promise<Uint8Array>;
|
|
61
|
+
/**
|
|
62
|
+
* Create an incremental streaming decompressor. Push compressed chunks and
|
|
63
|
+
* read decompressed output as it arrives, then `finish()`; `free()` when done.
|
|
64
|
+
* Unlike the common npm wasm zstd packages, the frame need not be fully
|
|
65
|
+
* buffered — the decoder window lives on the wasm side across chunks.
|
|
66
|
+
*/
|
|
67
|
+
export declare function createDecompressStream(): Promise<DecompressStream>;
|
|
68
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAkBH;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,2EAA2E;IAC3E,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAAC;IACpC,0EAA0E;IAC1E,MAAM,IAAI,UAAU,CAAC;IACrB,0CAA0C;IAC1C,IAAI,IAAI,IAAI,CAAC;CACd;AA2CD;;;GAGG;AACH,wBAAsB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAG1C;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAC5B,IAAI,EAAE,UAAU,EAChB,KAAK,GAAE,MAAsB,GAC5B,OAAO,CAAC,UAAU,CAAC,CAGrB;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAGtE;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,UAAU,EAChB,KAAK,GAAE,MAAsB,GAC5B,OAAO,CAAC,UAAU,CAAC,CAGrB;AAED;;;;GAIG;AACH,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,UAAU,CAAC,CAGrB;AAED;;;;;GAKG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAGxE"}
|
package/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@structured-world/structured-zstd` — pure-Rust Zstandard codec compiled to
|
|
3
|
+
* WebAssembly, with automatic selection of the SIMD (`simd128`) or scalar
|
|
4
|
+
* payload based on the host engine's capabilities.
|
|
5
|
+
*
|
|
6
|
+
* Pure ESM, strict TypeScript. One async API for browser, Node.js and Deno:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { compress, decompress } from "@structured-world/structured-zstd";
|
|
10
|
+
* const framed = await compress(new TextEncoder().encode("hello"), 19);
|
|
11
|
+
* const plain = await decompress(framed);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The codec is initialised lazily on first use; call {@link init} explicitly
|
|
15
|
+
* to pre-warm it (e.g. before a latency-sensitive path).
|
|
16
|
+
*/
|
|
17
|
+
import { simd } from "wasm-feature-detect";
|
|
18
|
+
/** Default compression level when the caller does not pass one. */
|
|
19
|
+
const DEFAULT_LEVEL = 3;
|
|
20
|
+
let loaded;
|
|
21
|
+
let loading;
|
|
22
|
+
/** True when running under Node.js (no `fetch` of `file://`, so load bytes). */
|
|
23
|
+
function isNode() {
|
|
24
|
+
const g = globalThis;
|
|
25
|
+
return g.process?.versions?.node != null;
|
|
26
|
+
}
|
|
27
|
+
/** Read a payload's `.wasm` from disk (Node only), relative to this module. */
|
|
28
|
+
async function readWasmBytes(dir) {
|
|
29
|
+
const url = new URL(`./${dir}/structured_zstd_wasm_bg.wasm`, import.meta.url);
|
|
30
|
+
const fs = await import(/* @vite-ignore */ "node:fs/promises");
|
|
31
|
+
return fs.readFile(url);
|
|
32
|
+
}
|
|
33
|
+
async function load() {
|
|
34
|
+
if (loaded !== undefined) {
|
|
35
|
+
return loaded;
|
|
36
|
+
}
|
|
37
|
+
const useSimd = await simd();
|
|
38
|
+
const dir = useSimd ? "simd" : "scalar";
|
|
39
|
+
const mod = (useSimd
|
|
40
|
+
? await import("./simd/structured_zstd_wasm.js")
|
|
41
|
+
: await import("./scalar/structured_zstd_wasm.js"));
|
|
42
|
+
if (isNode()) {
|
|
43
|
+
// Node's global fetch rejects `file://`, so hand the init the raw bytes.
|
|
44
|
+
// wasm-bindgen's current init wants `{ module_or_path }` (passing the
|
|
45
|
+
// bytes positionally is deprecated and warns).
|
|
46
|
+
await mod.default({ module_or_path: await readWasmBytes(dir) });
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Browser / Deno: the glue resolves its `.wasm` from its own module URL.
|
|
50
|
+
await mod.default();
|
|
51
|
+
}
|
|
52
|
+
loaded = mod;
|
|
53
|
+
return mod;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Initialise the codec (select + instantiate the best payload) ahead of time.
|
|
57
|
+
* Idempotent and concurrency-safe; resolves once the module is ready.
|
|
58
|
+
*/
|
|
59
|
+
export async function init() {
|
|
60
|
+
loading ??= load();
|
|
61
|
+
await loading;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Compress `data` into a Zstandard frame at `level` (zstd scale: `1..=22`,
|
|
65
|
+
* negatives for the ultra-fast tier; defaults to {@link DEFAULT_LEVEL}). The
|
|
66
|
+
* frame decodes in any compliant zstd decoder, including the native C library.
|
|
67
|
+
*/
|
|
68
|
+
export async function compress(data, level = DEFAULT_LEVEL) {
|
|
69
|
+
loading ??= load();
|
|
70
|
+
return (await loading).compress(data, level);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Decompress a complete Zstandard frame. Rejects if the input is not a valid,
|
|
74
|
+
* complete frame.
|
|
75
|
+
*/
|
|
76
|
+
export async function decompress(data) {
|
|
77
|
+
loading ??= load();
|
|
78
|
+
return (await loading).decompress(data);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Compress `data` against a raw Zstandard `dict` (e.g. from `zstd --train`) at
|
|
82
|
+
* `level` (defaults to {@link DEFAULT_LEVEL}). Mirrors C
|
|
83
|
+
* `ZSTD_compress_usingDict` — small, similar payloads compress far better.
|
|
84
|
+
* Rejects if the dictionary is invalid.
|
|
85
|
+
*/
|
|
86
|
+
export async function compressUsingDict(data, dict, level = DEFAULT_LEVEL) {
|
|
87
|
+
loading ??= load();
|
|
88
|
+
return (await loading).compressUsingDict(data, dict, level);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Decompress a dictionary-encoded frame. `dict` must be the same raw
|
|
92
|
+
* dictionary used to compress it. Mirrors C `ZSTD_decompress_usingDict`.
|
|
93
|
+
* Rejects on a malformed frame or dictionary mismatch.
|
|
94
|
+
*/
|
|
95
|
+
export async function decompressUsingDict(data, dict) {
|
|
96
|
+
loading ??= load();
|
|
97
|
+
return (await loading).decompressUsingDict(data, dict);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create an incremental streaming decompressor. Push compressed chunks and
|
|
101
|
+
* read decompressed output as it arrives, then `finish()`; `free()` when done.
|
|
102
|
+
* Unlike the common npm wasm zstd packages, the frame need not be fully
|
|
103
|
+
* buffered — the decoder window lives on the wasm side across chunks.
|
|
104
|
+
*/
|
|
105
|
+
export async function createDecompressStream() {
|
|
106
|
+
loading ??= load();
|
|
107
|
+
return new (await loading).ZstdDecompressStream();
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAgC3C,mEAAmE;AACnE,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB,IAAI,MAA2B,CAAC;AAChC,IAAI,OAAqC,CAAC;AAE1C,gFAAgF;AAChF,SAAS,MAAM;IACb,MAAM,CAAC,GAAG,UAA4D,CAAC;IACvE,OAAO,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,IAAI,IAAI,CAAC;AAC3C,CAAC;AAED,+EAA+E;AAC/E,KAAK,UAAU,aAAa,CAAC,GAAsB;IACjD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,GAAG,+BAA+B,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC9E,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;IAC/D,OAAO,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AAC1B,CAAC;AAED,KAAK,UAAU,IAAI;IACjB,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,IAAI,EAAE,CAAC;IAC7B,MAAM,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC;IACxC,MAAM,GAAG,GAAG,CAAC,OAAO;QAClB,CAAC,CAAC,MAAM,MAAM,CAAC,gCAAgC,CAAC;QAChD,CAAC,CAAC,MAAM,MAAM,CAAC,kCAAkC,CAAC,CAAuB,CAAC;IAC5E,IAAI,MAAM,EAAE,EAAE,CAAC;QACb,yEAAyE;QACzE,sEAAsE;QACtE,+CAA+C;QAC/C,MAAM,GAAG,CAAC,OAAO,CAAC,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAClE,CAAC;SAAM,CAAC;QACN,yEAAyE;QACzE,MAAM,GAAG,CAAC,OAAO,EAAE,CAAC;IACtB,CAAC;IACD,MAAM,GAAG,GAAG,CAAC;IACb,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI;IACxB,OAAO,KAAK,IAAI,EAAE,CAAC;IACnB,MAAM,OAAO,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAgB,EAChB,QAAgB,aAAa;IAE7B,OAAO,KAAK,IAAI,EAAE,CAAC;IACnB,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC/C,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAgB;IAC/C,OAAO,KAAK,IAAI,EAAE,CAAC;IACnB,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAgB,EAChB,IAAgB,EAChB,QAAgB,aAAa;IAE7B,OAAO,KAAK,IAAI,EAAE,CAAC;IACnB,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AAC9D,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,IAAgB,EAChB,IAAgB;IAEhB,OAAO,KAAK,IAAI,EAAE,CAAC;IACnB,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,mBAAmB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AACzD,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB;IAC1C,OAAO,KAAK,IAAI,EAAE,CAAC;IACnB,OAAO,IAAI,CAAC,MAAM,OAAO,CAAC,CAAC,oBAAoB,EAAE,CAAC;AACpD,CAAC"}
|
package/index.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@structured-world/structured-zstd` — pure-Rust Zstandard codec compiled to
|
|
3
|
+
* WebAssembly, with automatic selection of the SIMD (`simd128`) or scalar
|
|
4
|
+
* payload based on the host engine's capabilities.
|
|
5
|
+
*
|
|
6
|
+
* Pure ESM, strict TypeScript. One async API for browser, Node.js and Deno:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { compress, decompress } from "@structured-world/structured-zstd";
|
|
10
|
+
* const framed = await compress(new TextEncoder().encode("hello"), 19);
|
|
11
|
+
* const plain = await decompress(framed);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The codec is initialised lazily on first use; call {@link init} explicitly
|
|
15
|
+
* to pre-warm it (e.g. before a latency-sensitive path).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { simd } from "wasm-feature-detect";
|
|
19
|
+
|
|
20
|
+
/** Shape of a wasm-pack `web`-target payload glue module (simd or scalar). */
|
|
21
|
+
interface Payload {
|
|
22
|
+
/** Async wasm initialiser. Accepts wasm bytes / a URL, or nothing (browser). */
|
|
23
|
+
// Typed `unknown`: wasm-bindgen's `web` init accepts the broad `InitInput`
|
|
24
|
+
// (bytes / URL / Response / Module), and Node's `fs.readFile` returns
|
|
25
|
+
// `Buffer<ArrayBufferLike>`, wider than the DOM `BufferSource` in the .d.ts.
|
|
26
|
+
default: (moduleOrPath?: unknown) => Promise<unknown>;
|
|
27
|
+
compress: (data: Uint8Array, level: number) => Uint8Array;
|
|
28
|
+
decompress: (data: Uint8Array) => Uint8Array;
|
|
29
|
+
compressUsingDict: (data: Uint8Array, dict: Uint8Array, level: number) => Uint8Array;
|
|
30
|
+
decompressUsingDict: (data: Uint8Array, dict: Uint8Array) => Uint8Array;
|
|
31
|
+
ZstdDecompressStream: new () => DecompressStream;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Incremental streaming decompressor handle. Feed compressed chunks with
|
|
36
|
+
* {@link DecompressStream.push} and read decompressed output as it becomes
|
|
37
|
+
* available; call {@link DecompressStream.finish} at end of input. The decoder
|
|
38
|
+
* window is held on the wasm side across chunks, so a large frame never needs
|
|
39
|
+
* to be fully buffered. Call {@link DecompressStream.free} when done.
|
|
40
|
+
*/
|
|
41
|
+
export interface DecompressStream {
|
|
42
|
+
/** Feed compressed bytes; returns decompressed output available so far. */
|
|
43
|
+
push(chunk: Uint8Array): Uint8Array;
|
|
44
|
+
/** Signal end of input; returns the final bytes. Throws if incomplete. */
|
|
45
|
+
finish(): Uint8Array;
|
|
46
|
+
/** Release the underlying wasm handle. */
|
|
47
|
+
free(): void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Default compression level when the caller does not pass one. */
|
|
51
|
+
const DEFAULT_LEVEL = 3;
|
|
52
|
+
|
|
53
|
+
let loaded: Payload | undefined;
|
|
54
|
+
let loading: Promise<Payload> | undefined;
|
|
55
|
+
|
|
56
|
+
/** True when running under Node.js (no `fetch` of `file://`, so load bytes). */
|
|
57
|
+
function isNode(): boolean {
|
|
58
|
+
const g = globalThis as { process?: { versions?: { node?: string } } };
|
|
59
|
+
return g.process?.versions?.node != null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Read a payload's `.wasm` from disk (Node only), relative to this module. */
|
|
63
|
+
async function readWasmBytes(dir: "simd" | "scalar"): Promise<Uint8Array> {
|
|
64
|
+
const url = new URL(`./${dir}/structured_zstd_wasm_bg.wasm`, import.meta.url);
|
|
65
|
+
const fs = await import(/* @vite-ignore */ "node:fs/promises");
|
|
66
|
+
return fs.readFile(url);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function load(): Promise<Payload> {
|
|
70
|
+
if (loaded !== undefined) {
|
|
71
|
+
return loaded;
|
|
72
|
+
}
|
|
73
|
+
const useSimd = await simd();
|
|
74
|
+
const dir = useSimd ? "simd" : "scalar";
|
|
75
|
+
const mod = (useSimd
|
|
76
|
+
? await import("./simd/structured_zstd_wasm.js")
|
|
77
|
+
: await import("./scalar/structured_zstd_wasm.js")) as unknown as Payload;
|
|
78
|
+
if (isNode()) {
|
|
79
|
+
// Node's global fetch rejects `file://`, so hand the init the raw bytes.
|
|
80
|
+
// wasm-bindgen's current init wants `{ module_or_path }` (passing the
|
|
81
|
+
// bytes positionally is deprecated and warns).
|
|
82
|
+
await mod.default({ module_or_path: await readWasmBytes(dir) });
|
|
83
|
+
} else {
|
|
84
|
+
// Browser / Deno: the glue resolves its `.wasm` from its own module URL.
|
|
85
|
+
await mod.default();
|
|
86
|
+
}
|
|
87
|
+
loaded = mod;
|
|
88
|
+
return mod;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Initialise the codec (select + instantiate the best payload) ahead of time.
|
|
93
|
+
* Idempotent and concurrency-safe; resolves once the module is ready.
|
|
94
|
+
*/
|
|
95
|
+
export async function init(): Promise<void> {
|
|
96
|
+
loading ??= load();
|
|
97
|
+
await loading;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Compress `data` into a Zstandard frame at `level` (zstd scale: `1..=22`,
|
|
102
|
+
* negatives for the ultra-fast tier; defaults to {@link DEFAULT_LEVEL}). The
|
|
103
|
+
* frame decodes in any compliant zstd decoder, including the native C library.
|
|
104
|
+
*/
|
|
105
|
+
export async function compress(
|
|
106
|
+
data: Uint8Array,
|
|
107
|
+
level: number = DEFAULT_LEVEL,
|
|
108
|
+
): Promise<Uint8Array> {
|
|
109
|
+
loading ??= load();
|
|
110
|
+
return (await loading).compress(data, level);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Decompress a complete Zstandard frame. Rejects if the input is not a valid,
|
|
115
|
+
* complete frame.
|
|
116
|
+
*/
|
|
117
|
+
export async function decompress(data: Uint8Array): Promise<Uint8Array> {
|
|
118
|
+
loading ??= load();
|
|
119
|
+
return (await loading).decompress(data);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Compress `data` against a raw Zstandard `dict` (e.g. from `zstd --train`) at
|
|
124
|
+
* `level` (defaults to {@link DEFAULT_LEVEL}). Mirrors C
|
|
125
|
+
* `ZSTD_compress_usingDict` — small, similar payloads compress far better.
|
|
126
|
+
* Rejects if the dictionary is invalid.
|
|
127
|
+
*/
|
|
128
|
+
export async function compressUsingDict(
|
|
129
|
+
data: Uint8Array,
|
|
130
|
+
dict: Uint8Array,
|
|
131
|
+
level: number = DEFAULT_LEVEL,
|
|
132
|
+
): Promise<Uint8Array> {
|
|
133
|
+
loading ??= load();
|
|
134
|
+
return (await loading).compressUsingDict(data, dict, level);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Decompress a dictionary-encoded frame. `dict` must be the same raw
|
|
139
|
+
* dictionary used to compress it. Mirrors C `ZSTD_decompress_usingDict`.
|
|
140
|
+
* Rejects on a malformed frame or dictionary mismatch.
|
|
141
|
+
*/
|
|
142
|
+
export async function decompressUsingDict(
|
|
143
|
+
data: Uint8Array,
|
|
144
|
+
dict: Uint8Array,
|
|
145
|
+
): Promise<Uint8Array> {
|
|
146
|
+
loading ??= load();
|
|
147
|
+
return (await loading).decompressUsingDict(data, dict);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Create an incremental streaming decompressor. Push compressed chunks and
|
|
152
|
+
* read decompressed output as it arrives, then `finish()`; `free()` when done.
|
|
153
|
+
* Unlike the common npm wasm zstd packages, the frame need not be fully
|
|
154
|
+
* buffered — the decoder window lives on the wasm side across chunks.
|
|
155
|
+
*/
|
|
156
|
+
export async function createDecompressStream(): Promise<DecompressStream> {
|
|
157
|
+
loading ??= load();
|
|
158
|
+
return new (await loading).ZstdDecompressStream();
|
|
159
|
+
}
|