database reading

This commit is contained in:
micialware committed 2026-09-14 14:32:38 +03:00
1 parent 68c24dcec5
commit fc8455e353
418 files changed
+674410 -1561

No files matched your search

+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+148
View File
@@ -0,0 +1,148 @@
---
title: Zstd
description: WebAssembly wrapper for the Zstandard compression library
outline: deep
---
# @hpcc-js/wasm-zstd
<script setup>
import ZstdDemo from "../../.vitepress/components/ZstdDemo.vue";
</script>
This package provides a WebAssembly wrapper around the [Zstandard](https://facebook.github.io/zstd/) library. This provides efficient compression and decompression of data.
## Installation
::: code-group
```sh [npm]
npm install @hpcc-js/wasm-zstd
```
```sh [yarn]
yarn add @hpcc-js/wasm-zstd
```
```sh [pnpm]
pnpm add @hpcc-js/wasm-zstd
```
:::
## One-shot usage
```typescript
import { Zstd } from "@hpcc-js/wasm-zstd";
const zstd = await Zstd.load();
// Generate some "data"
const data = new Uint8Array(Array.from({ length: 100000 }, (_, i) => i % 256));
const compressed_data = zstd.compress(data);
const decompressed_data = zstd.decompress(compressed_data);
```
## Try it
<ZstdDemo />
`decompress()` uses a fast path when the frame records its content size. Frames with unknown content size (streaming compressors, stdin, many network streams) automatically fall back to the safe streaming decoder—no output-size guessing is required.
## Streaming compression
```typescript
import { Zstd } from "@hpcc-js/wasm-zstd";
const zstd = await Zstd.load();
zstd.resetCompression(3);
const parts: Uint8Array[] = [];
parts.push(zstd.compressChunk(chunk1)); // may be empty while Zstd buffers input
parts.push(zstd.compressChunk(chunk2));
parts.push(zstd.compressEnd());
```
## Streaming decompression
```typescript
import { Zstd } from "@hpcc-js/wasm-zstd";
const zstd = await Zstd.load();
zstd.resetDecompression();
const parts: Uint8Array[] = [];
parts.push(zstd.decompressChunk(compressedChunk1)); // may be empty
parts.push(zstd.decompressChunk(compressedChunk2));
zstd.decompressEnd(); // throws if the final frame is truncated
```
Call `decompressEnd()` after the last chunk. Intermediate empty output is valid and does **not** mean the frame is complete. Corrupt or truncated input throws an `Error` that includes the operation name and Zstandard error name when available.
## Workers
Use the default package entry from browser module workers or Node.js worker threads. The WASM payload is embedded in the bundled JavaScript, so there is no separate `.wasm` file to copy or URL to pass to `Zstd.load()`.
### Browser module worker
```typescript [worker.ts]
import { Zstd } from "@hpcc-js/wasm-zstd";
self.onmessage = async function (e: MessageEvent<Uint8Array>) {
const zstd = await Zstd.load();
const compressed = zstd.compress(e.data);
const decompressed = zstd.decompress(compressed);
self.postMessage(decompressed);
};
```
```typescript [main.ts]
const worker = new Worker(new URL("./worker.ts", import.meta.url), {
type: "module",
});
worker.onmessage = function (e: MessageEvent<Uint8Array>) {
console.log(e.data);
};
worker.postMessage(data);
```
### Node.js worker thread
```javascript [worker.js]
import { parentPort } from "node:worker_threads";
import { Zstd } from "@hpcc-js/wasm-zstd";
parentPort?.on("message", async function (data) {
const zstd = await Zstd.load();
const compressed = zstd.compress(data);
const decompressed = zstd.decompress(compressed);
parentPort?.postMessage(decompressed);
});
```
```typescript [main.ts]
import { Worker } from "node:worker_threads";
const worker = new Worker(new URL("./worker.js", import.meta.url));
worker.on("message", function (data: Uint8Array) {
console.log(data);
});
worker.postMessage(data);
```
## Singleton / serialization warning
`Zstd.load()` returns a singleton within the current JavaScript context. Compression and decompression contexts are stateful. Callers must serialize streaming operations on the shared instance—do not interleave concurrent streams on the same `Zstd` object. Prefer `resetCompression()` / `resetDecompression()` over the legacy `reset()` so one context is not cleared accidentally with the other.
<!--@include: ../../docs/zstd/src/zstd/README.md-->
## Reference
- [API Documentation](https://hpcc-systems.github.io/hpcc-js-wasm/docs/zstd/src/zstd/classes/Zstd.html)
+2
View File
@@ -0,0 +1,2 @@
var mi=ArrayBuffer,N=Uint8Array,De=Uint16Array,_i=Int16Array;var Ge=Int32Array,ur=function(n,i,s){if(N.prototype.slice)return N.prototype.slice.call(n,i,s);(i==null||i<0)&&(i=0),(s==null||s>n.length)&&(s=n.length);var c=new N(s-i);return c.set(n.subarray(i,s)),c},Re=function(n,i,s,c){if(N.prototype.fill)return N.prototype.fill.call(n,i,s,c);for((s==null||s<0)&&(s=0),(c==null||c>n.length)&&(c=n.length);s<c;++s)n[s]=i;return n},Ui=function(n,i,s,c){if(N.prototype.copyWithin)return N.prototype.copyWithin.call(n,i,s,c);for((s==null||s<0)&&(s=0),(c==null||c>n.length)&&(c=n.length);s<c;)n[i++]=n[s++]};var bi=["invalid zstd data","window size too large (>2046MB)","invalid block type","FSE accuracy too high","match distance too far back","unexpected EOF"],k=function(n,i,s){var c=new Error(i||bi[n]);if(c.code=n,Error.captureStackTrace&&Error.captureStackTrace(c,k),!s)throw c;return c},Lr=function(n,i,s){for(var c=0,l=0;c<s;++c)l|=n[i++]<<(c<<3);return l},Ci=function(n,i){return(n[i]|n[i+1]<<8|n[i+2]<<16|n[i+3]<<24)>>>0},Mi=function(n,i){var s=n[0]|n[1]<<8|n[2]<<16;if(s==3126568&&n[3]==253){var c=n[4],l=c>>5&1,p=c>>2&1,h=c&3,d=c>>6;c&8&&k(0);var U=6-l,C=h==3?4:h,M=Lr(n,U,C);U+=C;var Q=d?1<<d:l,x=Lr(n,U,Q)+(d==1&&256),R=x;if(!l){var Z=1<<10+(n[5]>>3);R=Z+(Z>>3)*(n[5]&7)}R>2145386496&&k(1);var b=new N((i==1?x||R:i?0:R)+12);return b[0]=1,b[4]=4,b[8]=8,{b:U+Q,y:0,l:0,d:M,w:i&&i!=1?i:b.subarray(12),e:R,o:new Ge(b.buffer,0,3),u:x,c:p,m:Math.min(131072,R)}}else if((s>>4|n[3]<<20)==25481893)return Ci(n,4)+8;k(0)},me=function(n){for(var i=0;1<<i<=n;++i);return i-1},$e=function(n,i,s){var c=(i<<3)+4,l=(n[i]&15)+5;l>s&&k(3);for(var p=1<<l,h=p,d=-1,U=-1,C=-1,M=p,Q=new mi(512+(p<<2)),x=new _i(Q,0,256),R=new De(Q,0,256),Z=new De(Q,512,p),b=512+(p<<1),q=new N(Q,b,p),te=new N(Q,b+p);d<255&&h>0;){var V=me(h+1),K=c>>3,ie=(1<<V+1)-1,$=(n[K]|n[K+1]<<8|n[K+2]<<16)>>(c&7)&ie,y=(1<<V)-1,L=ie-h-1,O=$&y;if(O<L?(c+=V,$=O):(c+=V+1,$>y&&($-=L)),x[++d]=--$,$==-1?(h+=$,q[--M]=d):h-=$,!$)do{var W=c>>3;U=(n[W]|n[W+1]<<8)>>(c&7)&3,c+=2,d+=U}while(U==3)}(d>255||h)&&k(0);for(var Y=0,A=(p>>1)+(p>>3)+3,ee=p-1,D=0;D<=d;++D){var P=x[D];if(P<1){R[D]=-P;continue}for(C=0;C<P;++C){q[Y]=D;do Y=Y+A&ee;while(Y>=M)}}for(Y&&k(0),C=0;C<p;++C){var m=R[q[C]]++,S=te[C]=l-me(m);Z[C]=(m<<S)-p}return[c+7>>3,{b:l,s:q,n:te,t:Z}]},Pi=function(n,i){var s=0,c=-1,l=new N(292),p=n[i],h=l.subarray(0,256),d=l.subarray(256,268),U=new De(l.buffer,268);if(p<128){var C=$e(n,i+1,6),M=C[0],Q=C[1];i+=p;var x=M<<3,R=n[i];R||k(0);for(var Z=0,b=0,q=Q.b,te=q,V=(++i<<3)-8+me(R);V-=q,!(V<x);){var K=V>>3;if(Z+=(n[K]|n[K+1]<<8)>>(V&7)&(1<<q)-1,h[++c]=Q.s[Z],V-=te,V<x)break;K=V>>3,b+=(n[K]|n[K+1]<<8)>>(V&7)&(1<<te)-1,h[++c]=Q.s[b],q=Q.n[Z],Z=Q.t[Z],te=Q.n[b],b=Q.t[b]}++c>255&&k(0)}else{for(c=p-127;s<c;s+=2){var ie=n[++i];h[s]=ie>>4,h[s+1]=ie&15}++i}var $=0;for(s=0;s<c;++s){var y=h[s];y>11&&k(0),$+=y&&1<<y-1}var L=me($)+1,O=1<<L,W=O-$;for(W&W-1&&k(0),h[c++]=me(W)+1,s=0;s<c;++s){var y=h[s];++d[h[s]=y&&L+1-y]}var Y=new N(O<<1),A=Y.subarray(0,O),ee=Y.subarray(O);for(U[L]=0,s=L;s>0;--s){var D=U[s];Re(ee,s,D,U[s-1]=D+d[s]*(1<<L-s))}for(U[0]!=O&&k(0),s=0;s<c;++s){var P=h[s];if(P){var m=U[P];Re(A,s,m,U[P]=m+(1<<L-P))}}return[i,{n:ee,b:L,s:A}]},Qi=$e(new N([81,16,99,140,49,198,24,99,12,33,196,24,99,102,102,134,70,146,4]),0,6)[1],Fi=$e(new N([33,20,196,24,99,140,33,132,16,66,8,33,132,16,66,8,33,68,68,68,68,68,68,68,68,36,9]),0,6)[1],Hi=$e(new N([32,132,16,66,102,70,68,68,68,68,36,73,2]),0,5)[1],Yr=function(n,i){for(var s=n.length,c=new Ge(s),l=0;l<s;++l)c[l]=i,i+=1<<n[l];return c},cr=new N(new Ge([0,0,0,0,16843009,50528770,134678020,202050057,269422093]).buffer,0,36),xi=Yr(cr,0),lr=new N(new Ge([0,0,0,0,0,0,0,0,16843009,50528770,117769220,185207048,252579084,16]).buffer,0,53),Ti=Yr(lr,3),qe=function(n,i,s){var c=n.length,l=i.length,p=n[c-1],h=(1<<s.b)-1,d=-s.b;p||k(0);for(var U=0,C=s.b,M=(c<<3)-8+me(p)-C,Q=-1;M>d&&Q<l;){var x=M>>3,R=(n[x]|n[x+1]<<8|n[x+2]<<16)>>(M&7);U=(U<<C|R)&h,i[++Q]=s.s[U],M-=C=s.n[U]}(M!=d||Q+1!=l)&&k(0)},Ei=function(n,i,s){var c=6,l=i.length,p=l+3>>2,h=p<<1,d=p+h;qe(n.subarray(c,c+=n[0]|n[1]<<8),i.subarray(0,p),s),qe(n.subarray(c,c+=n[2]|n[3]<<8),i.subarray(p,h),s),qe(n.subarray(c,c+=n[4]|n[5]<<8),i.subarray(h,d),s),qe(n.subarray(c),i.subarray(d),s)},qi=function(n,i,s){var c,l=i.b,p=n[l],h=p>>1&3;i.l=p&1;var d=p>>3|n[l+1]<<5|n[l+2]<<13,U=(l+=3)+d;if(h==1)return l>=n.length?void 0:(i.b=l+1,s?(Re(s,n[l],i.y,i.y+=d),s):Re(new N(d),n[l]));if(!(U>n.length)){if(h==0)return i.b=U,s?(s.set(n.subarray(l,U),i.y),i.y+=d,s):ur(n,l,U);if(h==2){var C=n[l],M=C&3,Q=C>>2&3,x=C>>4,R=0,Z=0;M<2?Q&1?x|=n[++l]<<4|(Q&2&&n[++l]<<12):x=C>>3:(Z=Q,Q<2?(x|=(n[++l]&63)<<4,R=n[l]>>6|n[++l]<<2):Q==2?(x|=n[++l]<<4|(n[++l]&3)<<12,R=n[l]>>2|n[++l]<<6):(x|=n[++l]<<4|(n[++l]&63)<<12,R=n[l]>>6|n[++l]<<2|n[++l]<<10)),++l;var b=s?s.subarray(i.y,i.y+i.m):new N(i.m),q=b.length-x;if(M==0)b.set(n.subarray(l,l+=x),q);else if(M==1)Re(b,n[l++],q);else{var te=i.h;if(M==2){var V=Pi(n,l);R+=l-(l=V[0]),i.h=te=V[1]}else te||k(0);(ZLine truncated
//# sourceMappingURL=index.js.map
+7
View File
@@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["../../../node_modules/fzstd/esm/index.mjs", "../../../build/packages/zstd/zstdlib.js", "../../../build/packages/zstd/zstdlib.wasm", "../../util/src/wasm-library.ts", "../src/zstd.ts"],
"sourcesContent": ["// Some numerical data is initialized as -1 even when it doesn't need initialization to help the JIT infer types\n// aliases for shorter compressed code (most minifers don't do this)\nvar ab = ArrayBuffer, u8 = Uint8Array, u16 = Uint16Array, i16 = Int16Array, u32 = Uint32Array, i32 = Int32Array;\nvar slc = function (v, s, e) {\n if (u8.prototype.slice)\n return u8.prototype.slice.call(v, s, e);\n if (s == null || s < 0)\n s = 0;\n if (e == null || e > v.length)\n e = v.length;\n var n = new u8(e - s);\n n.set(v.subarray(s, e));\n return n;\n};\nvar fill = function (v, n, s, e) {\n if (u8.prototype.fill)\n return u8.prototype.fill.call(v, n, s, e);\n if (s == null || s < 0)\n s = 0;\n if (e == null || e > v.length)\n e = v.length;\n for (; s < e; ++s)\n v[s] = n;\n return v;\n};\nvar cpw = function (v, t, s, e) {\n if (u8.prototype.copyWithin)\n return u8.prototype.copyWithin.call(v, t, s, e);\n if (s == null || s < 0)\n s = 0;\n if (e == null || e > v.length)\n e = v.length;\n while (s < e) {\n v[t++] = v[s++];\n }\n};\n/**\n * Codes for errors generated within this library\n */\nexport var ZstdErrorCode = {\n InvalidData: 0,\n WindowSizeTooLarge: 1,\n InvalidBlockType: 2,\n FSEAccuracyTooHigh: 3,\n DistanceTooFarBack: 4,\n UnexpectedEOF: 5\n};\n// error codes\nvar ec = [\n 'invalid zstd data',\n 'window size too large (>2046MB)',\n 'invalid block type',\n 'FSE accuracy too high',\n 'match distance too far back',\n 'unexpected EOF'\n];\nvar err = function (ind, msg, nt) {\n var e = new Error(msg || ec[ind]);\n e.code = ind;\n if (Error.captureStackTrace)\n Error.captureStackTrace(e, err);\n if (!nt)\n throw e;\n return e;\n};\nvar rb = function (d, b, n) {\n var i = 0, o = 0;\n for (; i < n; ++i)\n o |= d[b++] << (i << 3);\n return o;\n};\nvar b4 = function (d, b) { return (d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24)) >>> 0; };\n// read Zstandard frame header\nvar rzfh = function (dat, w) {\n var n3 = dat[0] | (dat[1] << 8) | (dat[2] << 16);\n if (n3 == 0x2FB528 && dat[3] == 253) {\n // Zstandard\n var flg = dat[4];\n // single segment checksum dict flag frame content flag\n var ss = (flg >> 5) & 1, cc = (flg >> 2) & 1, df = flg & 3, fcf = flg >> 6;\n if (flg & 8)\n err(0);\n // byte\n var bt = 6 - ss;\n // dict bytes\n var db = df == 3 ? 4 : df;\n // dictionary id\n var di = rb(dat, bt, db);\n bt += db;\n // frame size bytes\n var fsb = fcf ? (1 << fcf) : ss;\n // frame source size\n var fss = rb(dat, bt, fsb) + ((fcf == 1) && 256);\n // window size\n var ws = fss;\n if (!ss) {\n // window descriptor\n var wb = 1 << (10 + (dat[5] >> 3));\n ws = wb + (wb >> 3) * (dat[5] & 7);\n }\n if (ws > 2145386496)\n err(1);\n var buf = new u8((w == 1 ? (fss || ws) : w ? 0 : ws) + 12);\n buf[0] = 1, buf[4] = 4, buf[8] = 8;\n return {\n b: bt + fsb,\n y: 0,\n l: 0,\n d: di,\n w: (w && w != 1) ? w : buf.subarray(12),\n e: ws,\n o: new i32(buf.buffer, 0, 3),\n u: fss,\n c: cc,\n m: Math.min(131072, ws)\n };\n }\n else if (((n3 >> 4) | (dat[3] << 20)) == 0x184D2A5) {\n // skippable\n return b4(dat, 4) + 8;\n }\n err(0);\n};\n// most significant bit for nonzero\nvar msb = function (val) {\n var bits = 0;\n for (; (1 << bits) <= val; ++bits)\n ;\n return bits - 1;\n};\n// read finite state entropy\nvar rfse = function (dat, bt, mal) {\n // table pos\n var tpos = (bt << 3) + 4;\n // accuracy log\n var al = (dat[bt] & 15) + 5;\n if (al > mal)\n err(3);\n // size\n var sz = 1 << al;\n // probabilities symbols repeat index high threshold\n var probs = sz, sym = -1, re = -1, i = -1, ht = sz;\n // optimization: single allocation is much faster\n var buf = new ab(512 + (sz << 2));\n var freq = new i16(buf, 0, 256);\n // same view as freq\n var dstate = new u16(buf, 0, 256);\n var nstate = new u16(buf, 512, sz);\n var bb1 = 512 + (sz << 1);\n var syms = new u8(buf, bb1, sz);\n var nbits = new u8(buf, bb1 + sz);\n while (sym < 255 && probs > 0) {\n var bits = msb(probs + 1);\n var cbt = tpos >> 3;\n // mask\n var msk = (1 << (bits + 1)) - 1;\n var val = ((dat[cbt] | (dat[cbt + 1] << 8) | (dat[cbt + 2] << 16)) >> (tpos & 7)) & msk;\n // mask (1 fewer bit)\n var msk1fb = (1 << bits) - 1;\n // max small value\n var msv = msk - probs - 1;\n // small value\Line truncated
"mappings": "AAEA,IAAIA,GAAK,YAAaC,EAAK,WAAYC,GAAM,YAAaC,GAAM,WAAhE,IAA+FC,GAAM,WACjGC,GAAM,SAAUC,EAAGC,EAAGC,EAAG,CACzB,GAAIC,EAAG,UAAU,MACb,OAAOA,EAAG,UAAU,MAAM,KAAKH,EAAGC,EAAGC,CAAC,GACtCD,GAAK,MAAQA,EAAI,KACjBA,EAAI,IACJC,GAAK,MAAQA,EAAIF,EAAE,UACnBE,EAAIF,EAAE,QACV,IAAII,EAAI,IAAID,EAAGD,EAAID,CAAC,EACpB,OAAAG,EAAE,IAAIJ,EAAE,SAASC,EAAGC,CAAC,CAAC,EACfE,CACX,EACIC,GAAO,SAAUL,EAAGI,EAAG,EAAGF,EAAG,CAC7B,GAAIC,EAAG,UAAU,KACb,OAAOA,EAAG,UAAU,KAAK,KAAKH,EAAGI,EAAG,EAAGF,CAAC,EAK5C,KAJI,GAAK,MAAQ,EAAI,KACjB,EAAI,IACJA,GAAK,MAAQA,EAAIF,EAAE,UACnBE,EAAIF,EAAE,QACH,EAAIE,EAAG,EAAE,EACZF,EAAE,CAAC,EAAII,EACX,OAAOJ,CACX,EACIM,GAAM,SAAUN,EAAGO,EAAG,EAAGL,EAAG,CAC5B,GAAIC,EAAG,UAAU,WACb,OAAOA,EAAG,UAAU,WAAW,KAAKH,EAAGO,EAAG,EAAGL,CAAC,EAKlD,KAJI,GAAK,MAAQ,EAAI,KACjB,EAAI,IACJA,GAAK,MAAQA,EAAIF,EAAE,UACnBE,EAAIF,EAAE,QACH,EAAIE,GACPF,EAAEO,GAAG,EAAIP,EAAE,GAAG,CAEtB,EAaA,IAAIQ,GAAK,CACL,oBACA,kCACA,qBACA,wBACA,8BACA,gBACJ,EACIC,EAAM,SAAUC,EAAKC,EAAKC,EAAI,CAC9B,IAAIC,EAAI,IAAI,MAAMF,GAAOH,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,EAAKzC,CAAC,CAAC,IAEnBuD,EAAKb,GAAM1C,CAAC,EAAI4B,EAAKN,GAAIgC,CAAE,EAC/Bf,EAAOvC,CAAC,GAAKsD,GAAMC,GAAM1B,CAC7B,CACA,MAAO,CAAEF,EAAO,GAAM,EAAG,CACjB,EAAGC,EACH,EAAGa,EACH,EAAGC,GACH,EAAGH,CACP,CAAC,CACT,EAEIiB,GAAM,SAAUpD,EAAKQ,EAAI,CAEzB,IAAIZ,EAAI,EAAGyD,EAAK,GAEZtC,EAAM,IAAIC,EAAG,GAAG,EAAGsC,EAAKtD,EAAIQ,CAAE,EAE9B+C,EAAKxC,EAAI,SAAS,EAAG,GAAG,EAExByC,EAAKzC,EAAI,SAAS,IAAK,GAAG,EAE1B0C,EAAK,IAAIvB,GAAInB,EAAI,OAAQ,GAAG,EAEhC,GAAIuC,EAAK,IAAK,CAEV,IAAII,EAAKrC,GAAKrB,EAAKQ,EAAK,EAAG,CAAC,EAAGmD,EAAMD,EAAG,CAAC,EAAGE,EAAMF,EAAG,CAAC,EACtDlD,GAAM8C,EACN,IAAIO,EAAOF,GAAO,EAEdG,EAAK9D,EAAIQ,CAAE,EACVsD,GACD3E,EAAI,CAAC,EAMT,QAJI4E,EAAM,EAAGC,EAAM,EAAGC,EAAOL,EAAI,EAAGM,GAAOD,EAGvCE,GAAQ,EAAE3D,GAAM,GAAK,EAAIU,GAAI4C,CAAE,EAE/BK,GAAQF,EACJ,EAAAE,EAAON,IAFN,CAIL,IAAItB,EAAM4B,GAAQ,EAIlB,GAHAJ,IAAS/D,EAAIuC,CAAG,EAAKvC,EAAIuC,EAAM,CAAC,GAAK,KAAQ4BLine truncated
"names": ["ab", "u8", "u16", "i16", "i32", "slc", "v", "s", "e", "u8", "n", "fill", "cpw", "t", "ec", "err", "ind", "msg", "nt", "e", "rb", "d", "b", "n", "i", "o", "b4", "rzfh", "dat", "w", "n3", "flg", "ss", "cc", "df", "fcf", "bt", "db", "di", "fsb", "fss", "ws", "wb", "buf", "u8", "i32", "msb", "val", "bits", "rfse", "mal", "tpos", "al", "sz", "probs", "sym", "re", "ht", "ab", "freq", "i16", "dstate", "u16", "nstate", "bb1", "syms", "nbits", "cbt", "msk", "msk1fb", "msv", "sval", "rbt", "sympos", "sstep", "smask", "s", "sf", "ns", "nb", "rhu", "wc", "hb", "hw", "rc", "ri", "_a", "ebt", "fdt", "epos", "lb", "st1", "st2", "btr1", "btr2", "fpos", "byte", "wes", "wt", "mb", "ts", "rem", "hbuf", "pv", "fill", "code", "dllt", "dmlt", "doct", "b2bl", "len", "bl", "llb", "llbl", "mlb", "mlbl", "dhu", "out", "hu", "eb", "st", "btr", "pos", "dhu4", "sz1", "sz2", "sz3", "rzb", "b0", "btype", "slc", "b3", "lbt", "lss", "lcs", "s4", "spl", "hud", "scm", "dts", "md", "rbuf", "_b", "mlt", "oct", "llt", "spos", "oubt", "lst", "ost", "mst", "llc", "lbtr", "mlc", "mbtr", "ofc", "obtr", "ofp", "off", "ml", "ll", "idx", "stin", "bs", "cct", "bufs", "ol", "chk", "decompress", "blk", "cpw", "cpp", "moduleArg", "Module", "quit_", "status", "toThrow", "_scriptName", "scriptDirectory", "locateFile", "path", "readAsync", "readBinary", "out", "err", "wasmBinary", "ABORT", "EXITSTATUS", "isFileURI", "filename", "runtimeInitialized", "getMemoryBuffer", "wasmMemory", "updateMemoryViews", "HEAP8", "b", "HEAP16", "HEAPU8", "HEAPU16", "HEAP32", "HEAPU32", "HEAPF32", "HEAPF64", "HEAP64", "HEAPU64", "preRun", "initRuntime", "wasmExports", "postRun", "abort", "what", "___trap", "wasmBinaryFile", "findWasmBinary", "getBinarySync", "file", "getWasmBinary", "binaryFile", "response", "instantiateArrayBuffer", "imports", "binary", "instance", "reason", "instantiateAsync", "instantiationResult", "getWasmImports", "wasmImports", "createWasm", "receiveInstance", "assignWasmExports", "receiveInstantiationResult", "result", "info", "exports", "ExitStatus", "getCppExceptionTag", "___cpp_exception", "getCppExceptionThrownObjectFromWebAssemblyException", "ex", "unwind_header", "___thrown_object_from_unwind_exception", "stackSave", "_emscripten_stack_get_current", "stackRestore", "val", "__emscripten_stack_restore", "stackAlloc", "sz", "__emscripten_stack_alloc", "UTF8Decoder", "findStringEnd", "heapOrArray", "idx", "maxBytesToRead", "ignoreNul", "maxIdx", "UTF8ArrayToString", "endPtr", "str", "u0", "u1", "u2", "ch", "UTF8ToString", "ptr", "getExceptionMessageCommon", "sp", "type_addr_addr", "message_addr_addr", "___get_exception_message", "type_addr", "message_addr", "type", "_free", "message", "getExceptionMessage", "___throw_exception_with_stack_trace", "__abort_js", "structRegistrations", "runDestructors", "destructors", "del", "readPointer", "pointer", "awaitingDependencies", "registeredTypes", "typeDependencies", "InternalError", "throwInternalError", "whenDependentTypesAreResolved", "myTypes", "dependentTypes", "getTypeConverters", "onComplete", "typeConverters", "myTypeConverters", "i", "registerType", "unregisteredTypes", "registered", "dt", "__embind_finalize_value_object", "structType", "reg", "rawConstructor", "rawDestructor", "fieldRecords", "fieldTypes", "field", "fields", "getterReturnType", "getter", "getterContext", "setterArgumentType", "setter", "setterContext", "o", "rv", "fieldName", "AsciiToString", "BindingError", "throwBindingError", "sharedRegisterType", "rawType", "registeredInstance", "options", "name", "callbacks", "cb", "integerReadValueFromPointer", "width", "signed", "__embind_register_bigint", "primitiveType", "size", "minRange", "maxRange", "isUnsignedType", "fromWireType", "value", "bitSize", "__embind_register_bool", "trueValue", "falseValue", "wt", "shallowCopyInternalPointer", "throwInstanceAlreadyDeleted", "obj", "getInstanceTypeName", "handle", "finalizationRegistry", "detachFinalizer", "runDestructor", "$$", "releaseClassHandle", "toDelete", "attachFinalizer", "hasSmartPtr", "deletionQueue", "flushPendingDeletes", "delayFunction", "init_ClassHandle", "proto", "ClassHandle", "other", "leftClass", "left", "rightClass", "right", "clone", "symbolDispose", "createNamedFunction", "func", "registeredPointers", "ensureOverloadTable", "methodName", "humanName", "prevFunc", "args", "exposePublicSymbol", "numArguments", "char_0", "char_9", "makeLegalFunctionName", "f", "RegisteredClass", "constructor", "instancePrototype", "baseClass", "getActualType", "upcast", "downcast", "upcastPointer", "ptrClass", "desiredClass", "embindRepr", "v", "t", "constNoSmartPtrRawPointerToWireType", "handleClass", "genericPointerToWireType", "clonedHandle", "Emval", "nonConstNoSmartPtrRawPointerToWireType", "downcastPointer", "registeredInstances", "getBasestPointer", "class_", "getInheritedInstance", "makeClassHandle", "prototype", "record", "hasSmartPtrType", "RegisteredPointer_fromWireType", "rawPointer", "makeDefaultHandle", "actualTypeLine truncated
}
+65
View File
@@ -0,0 +1,65 @@
{
"name": "@hpcc-js/wasm-zstd",
"version": "1.16.0",
"description": "hpcc-js - WASM zstd",
"type": "module",
"exports": {
".": {
"types": "./types/index.d.ts",
"default": "./dist/index.js"
}
},
"main": "./dist/index.js",
"types": "./types/index.d.ts",
"publishConfig": {
"access": "public"
},
"files": [
"dist/**/*",
"src/**/*",
"types/**/*"
],
"scripts": {
"clean": "rimraf coverage dist dist-test types .nyc_output",
"build-cpp": "cmake --build ../../build --target zstdlib",
"build-cpp-watch": "chokidar 'src-cpp/**.*' -c 'npm run build-cpp'",
"gen-types": "tsc --project tsconfig.json --emitDeclarationOnly",
"gen-types-watch": "npm run gen-types -- --watch",
"bundle": "node esbuild.js",
"bundle-dev": "npm run bundle -- --development",
"bundle-watch": "npm run bundle-dev -- --watch",
"build-dev": "run-p gen-types bundle-dev",
"build": "run-p gen-types bundle",
"lint-skypack": "npx -y @skypack/package-check",
"lint-eslint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
"lint": "run-p lint-eslint",
"test-browser": "vitest run --project browser",
"test-node": "vitest run --project node",
"test": "vitest run",
"coverage": "vitest run --coverage",
"update": "npx -y npm-check-updates -u -t minor",
"update-major": "npx -y npm-check-updates -u"
},
"dependencies": {},
"devDependencies": {
"@hpcc-js/esbuild-plugins": "1.12.0",
"@hpcc-js/wasm-util": "1.0.0"
},
"keywords": [
"graphviz",
"typescript",
"webassembly",
"wasm",
"dot",
"neato",
"twopi"
],
"author": "hpcc-systems",
"repository": {
"type": "git",
"url": "git+https://github.com/hpcc-systems/hpcc-js-wasm.git"
},
"homepage": "https://hpcc-systems.github.io/hpcc-js-wasm/",
"license": "Apache-2.0",
"gitHead": "8f570c863d34cb7d08aa7bc54f48558f9bd3bcba"
}
+3
View File
@@ -0,0 +1,3 @@
export const PKG_NAME = "@hpcc-js/wasm-zstd";
export const PKG_VERSION = "1.7.0";
export const BUILD_VERSION = "4.0.0";
+1
View File
@@ -0,0 +1 @@
export * from "./zstd.ts";
+475
View File
@@ -0,0 +1,475 @@
// @ts-expect-error importing from a wasm file is resolved via a custom esbuild plugin
import load, { reset } from "../../../build/packages/zstd/zstdlib.wasm";
import type { MainModule, zstd, StreamResult, FrameContentSizeResult } from "../types/zstdlib.js";
import { MainModuleEx, type HeapU8 } from "@hpcc-js/wasm-util";
type ZstdExports = MainModule["zstd"];
// Ref: http://facebook.github.io/zstd/zstd_manual.html
// Ref: https://github.com/facebook/zstd
let g_zstd: Promise<Zstd> | undefined;
const WASM32_MAX = 0xffffffff;
function assertSafeLength(length: number, label: string): void {
if (!Number.isInteger(length) || length < 0 || length > WASM32_MAX) {
throw new Error(`${label} length ${length} is outside the WASM32-safe range`);
}
}
function concatUint8Arrays(chunks: Uint8Array[]): Uint8Array {
if (chunks.length === 0) {
return new Uint8Array(0);
}
if (chunks.length === 1) {
return chunks[0];
}
let total = 0;
for (const chunk of chunks) {
total += chunk.length;
}
const out = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
out.set(chunk, offset);
offset += chunk.length;
}
return out;
}
/**
* The Zstandard WASM library, provides a simplified wrapper around the Zstandard c++ library.
*
* See [Zstandard](https://facebook.github.io/zstd/) for more details.
*
* ```ts
* import { Zstd } from "@hpcc-js/wasm-zstd";
*
* const zstd = await Zstd.load();
*
* // Generate some "data"
* const data = new Uint8Array(Array.from({ length: 100000 }, (_, i) => i % 256));
*
* const compressed_data = zstd.compress(data);
* const decompressed_data = zstd.decompress(compressed_data);
* ```
*
* ::: warning
* `Zstd.load()` returns a process-wide singleton. Streaming compression and decompression
* mutate shared codec state and must be serialized by the caller. Do not interleave
* concurrent streaming operations on the same instance.
* :::
*/
export class Zstd {
private _mainModule: MainModuleEx<MainModule>;
private _zstdClass: ZstdExports;
private _zstd: zstd;
private _compressionLevel: number;
private _decompressFrameCompleted = false;
private _decompressRemaining = 0;
private _decompressSawInput = false;
private constructor(_module: MainModule) {
this._mainModule = new MainModuleEx(_module);
this._zstdClass = _module.zstd;
this._zstd = new this._zstdClass();
this._compressionLevel = this._zstdClass.defaultCLevel();
}
/**
* Compiles and instantiates the raw wasm.
*
* ::: info
* In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing `load` to be asynchronous;
* :::
*
* @returns A promise to an instance of the Zstd class.
*/
static load(): Promise<Zstd> {
if (!g_zstd) {
g_zstd = load().then((module: MainModule) => new Zstd(module));
}
return g_zstd!;
}
/**
* Unloades the compiled wasm instance.
*/
static unload() {
reset();
g_zstd?.then(zstd => {
zstd?._zstd?.delete();
});
g_zstd = undefined;
}
private copyHeap(data: HeapU8): Uint8Array {
return this._mainModule.heapView(data).slice();
}
private mallocChecked(size: number, label: string): HeapU8 {
assertSafeLength(size, label);
const heap = this._mainModule.malloc(size);
if (!heap.ptr) {
this._mainModule.free(heap);
throw new Error(`Failed to allocate ${size} bytes for ${label}`);
}
return heap;
}
private throwStreamError(operation: string, result: StreamResult): never {
throw new Error(`${operation} failed: ${result.errorName || "unknown error"}`);
}
private ensureStreamOk(operation: string, result: StreamResult): void {
if (result.error) {
this.throwStreamError(operation, result);
}
}
/**
* @returns The Zstd c++ version
*/
version(): string {
return this._zstdClass.version();
}
/**
* Resets both compression and decompression state (legacy API).
* Prefer {@link resetCompression} / {@link resetDecompression} so the two
* contexts are not reset accidentally together.
*/
reset(): void {
const result = this._zstd.reset();
this.ensureStreamOk("reset", result);
this._decompressFrameCompleted = false;
this._decompressRemaining = 0;
this._decompressSawInput = false;
}
/**
* Resets compression state and optionally sets the compression level.
* @param compressionLevel When omitted, the current level is kept.
*/
resetCompression(compressionLevel?: number): void {
if (compressionLevel !== undefined) {
this._compressionLevel = compressionLevel;
}
const result = this._zstd.resetCompression(this._compressionLevel);
this.ensureStreamOk("resetCompression", result);
}
/**
* Resets decompression state before a new streaming decode.
*/
resetDecompression(): void {
const result = this._zstd.resetDecompression();
this.ensureStreamOk("resetDecompression", result);
this._decompressFrameCompleted = false;
this._decompressRemaining = 0;
this._decompressSawInput = false;
}
/**
* Sets the compression level for streaming compression.
* @param level Compression level (use minCLevel() to maxCLevel())
*/
setCompressionLevel(level: number): void {
const result = this._zstd.setCompressionLevel(level);
this.ensureStreamOk("setCompressionLevel", result);
this._compressionLevel = level;
}
/**
* @param data Data to be compressed
* @param compressionLevel Compression v Speed tradeoff, when omitted it will default to `zstd.defaultCLevel()` which is currently 3.
* @returns Compressed data.
*
* :::tip
* 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).
* :::
*/
compress(data: Uint8Array, compressionLevel: number = this.defaultCLevel()): Uint8Array {
assertSafeLength(data.length, "compress input");
const uncompressed = this._mainModule.dataToHeap(data);
try {
if (!uncompressed.ptr && data.length > 0) {
throw new Error("Failed to allocate compress input buffer");
}
const compressedSize = this._zstdClass.compressBound(data.length);
const compressed = this.mallocChecked(compressedSize, "compress output");
try {
compressed.size = this._zstdClass.compress(compressed.ptr, compressedSize, uncompressed.ptr, uncompressed.size, compressionLevel);
if (this._zstdClass.isError(compressed.size)) {
throw new Error(`compress failed: ${this._zstdClass.getErrorName(compressed.size)}`);
}
return this.copyHeap(compressed);
} finally {
this._mainModule.free(compressed);
}
} finally {
this._mainModule.free(uncompressed);
}
}
/**
* Compresses a chunk of data in streaming mode.
* Call {@link resetCompression} before the first chunk, then {@link compressChunk} for each chunk, and finally {@link compressEnd}.
* Intermediate calls may return an empty array when Zstandard buffers input.
* @param data Chunk of data to be compressed
* @returns Compressed chunk data produced for this input (may be empty)
*/
compressChunk(data: Uint8Array): Uint8Array {
assertSafeLength(data.length, "compressChunk input");
if (data.length === 0) {
return new Uint8Array(0);
}
const uncompressed = this._mainModule.dataToHeap(data);
try {
if (!uncompressed.ptr) {
throw new Error("Failed to allocate compressChunk input buffer");
}
const outCapacity = this._zstdClass.CStreamOutSize();
const chunks: Uint8Array[] = [];
let offset = 0;
while (offset < data.length) {
const compressed = this.mallocChecked(outCapacity, "compressChunk output");
try {
const result = this._zstd.compressChunk(
compressed.ptr,
outCapacity,
uncompressed.ptr + offset,
data.length - offset
);
this.ensureStreamOk("compressChunk", result);
if (result.consumed === 0 && result.produced === 0) {
throw new Error("compressChunk failed: no progress while consuming input");
}
offset += result.consumed;
if (result.produced > 0) {
compressed.size = result.produced;
chunks.push(this.copyHeap(compressed));
}
} finally {
this._mainModule.free(compressed);
}
}
return concatUint8Arrays(chunks);
} finally {
this._mainModule.free(uncompressed);
}
}
/**
* Finishes the streaming compression and returns any remaining compressed data.
* @returns Final compressed data
*/
compressEnd(): Uint8Array {
const outCapacity = this._zstdClass.CStreamOutSize();
const chunks: Uint8Array[] = [];
let remaining = 1;
while (remaining !== 0) {
const compressed = this.mallocChecked(outCapacity, "compressEnd output");
try {
const result = this._zstd.compressEnd(compressed.ptr, outCapacity);
this.ensureStreamOk("compressEnd", result);
if (result.produced === 0 && result.remaining !== 0) {
throw new Error("compressEnd failed: no progress while finishing stream");
}
remaining = result.remaining;
if (result.produced > 0) {
compressed.size = result.produced;
chunks.push(this.copyHeap(compressed));
}
} finally {
this._mainModule.free(compressed);
}
}
return concatUint8Arrays(chunks);
}
private frameContentSize(compressed: HeapU8): FrameContentSizeResult {
return this._zstdClass.getFrameContentSize(compressed.ptr, compressed.size);
}
/**
* @param compressedData Data to be decompressed
* @returns Uncompressed data.
*/
decompress(compressedData: Uint8Array): Uint8Array {
assertSafeLength(compressedData.length, "decompress input");
const compressed = this._mainModule.dataToHeap(compressedData);
try {
if (!compressed.ptr && compressedData.length > 0) {
throw new Error("Failed to allocate decompress input buffer");
}
const sizeInfo = this.frameContentSize(compressed);
if (sizeInfo.error) {
throw new Error(`Failed to get frame content size: ${sizeInfo.errorName}`);
}
let useFastPath = sizeInfo.known;
if (useFastPath) {
// ZSTD_decompress only handles one frame. Known-size concatenated
// frames would allocate only for the first frame and overflow.
const frameCompressedSize = this._zstdClass.findFrameCompressedSize(compressed.ptr, compressed.size);
if (this._zstdClass.isError(frameCompressedSize)) {
throw new Error(`Failed to find frame compressed size: ${this._zstdClass.getErrorName(frameCompressedSize)}`);
}
if (frameCompressedSize < compressed.size) {
useFastPath = false;
}
}
if (useFastPath) {
const uncompressed = this.mallocChecked(sizeInfo.size, "decompress output");
try {
uncompressed.size = this._zstdClass.decompress(
uncompressed.ptr,
sizeInfo.size,
compressed.ptr,
compressed.size
);
if (this._zstdClass.isError(uncompressed.size)) {
throw new Error(`decompress failed: ${this._zstdClass.getErrorName(uncompressed.size)}`);
}
return this.copyHeap(uncompressed);
} finally {
this._mainModule.free(uncompressed);
}
}
} finally {
this._mainModule.free(compressed);
}
return this.decompressViaStreaming(compressedData);
}
private decompressViaStreaming(compressedData: Uint8Array): Uint8Array {
this.resetDecompression();
const produced = this.decompressChunk(compressedData);
this.decompressEnd();
return produced;
}
/**
* Decompresses a chunk of data in streaming mode.
* Call {@link resetDecompression} before the first chunk, then {@link decompressChunk} for each chunk, and finally {@link decompressEnd}.
* Intermediate calls may return an empty array. Callers must not guess an output size.
* @param compressedData Chunk of compressed data
* @returns Decompressed chunk data produced for this input (may be empty)
*/
decompressChunk(compressedData: Uint8Array): Uint8Array {
assertSafeLength(compressedData.length, "decompressChunk input");
if (compressedData.length === 0) {
return new Uint8Array(0);
}
this._decompressSawInput = true;
const compressed = this._mainModule.dataToHeap(compressedData);
try {
if (!compressed.ptr) {
throw new Error("Failed to allocate decompressChunk input buffer");
}
const outCapacity = this._zstdClass.DStreamOutSize();
const chunks: Uint8Array[] = [];
let offset = 0;
let outputFilled = false;
// Continue while input remains, or the previous step filled the output buffer
// (pending decoder output may still need an empty-input flush).
while (offset < compressedData.length || outputFilled) {
const uncompressed = this.mallocChecked(outCapacity, "decompressChunk output");
try {
const srcSize = offset < compressedData.length
? compressedData.length - offset
: 0;
const srcPtr = srcSize > 0
? compressed.ptr + offset
: 0;
const result = this._zstd.decompressChunk(
uncompressed.ptr,
outCapacity,
srcPtr,
srcSize
);
this.ensureStreamOk("decompressChunk", result);
if (result.consumed === 0 && result.produced === 0) {
if (srcSize === 0) {
if (result.remaining > 0) {
// Need more compressed input — wait for the next chunk.
this._decompressRemaining = result.remaining;
}
// remaining === 0: fully flushed, or waiting for input above.
break;
}
throw new Error("decompressChunk failed: no progress while consuming input");
}
offset += result.consumed;
// Only flush with empty input while the current frame still has pending output.
// When remaining === 0 the frame is complete (even if the buffer filled exactly);
// a follow-up empty call can return a non-zero hint and must not look like truncation.
outputFilled = result.produced === outCapacity && result.remaining !== 0;
if (result.produced > 0) {
uncompressed.size = result.produced;
chunks.push(this.copyHeap(uncompressed));
}
if (result.remaining === 0) {
this._decompressFrameCompleted = true;
this._decompressRemaining = 0;
} else {
this._decompressRemaining = result.remaining;
}
} finally {
this._mainModule.free(uncompressed);
}
}
return concatUint8Arrays(chunks);
} finally {
this._mainModule.free(compressed);
}
}
/**
* Verifies that streaming decompression completed a full frame and was not truncated.
* Empty intermediate output is not treated as completion; call this after the final chunk.
*/
decompressEnd(): void {
if (!this._decompressSawInput) {
throw new Error("decompressEnd failed: empty compressed input is not a completed frame");
}
if (!this._decompressFrameCompleted || this._decompressRemaining !== 0) {
throw new Error("decompressEnd failed: truncated Zstandard input");
}
}
/**
* @returns Default compression level (see notes above above).
*/
defaultCLevel(): number {
return this._zstdClass.defaultCLevel();
}
minCLevel(): number {
return this._zstdClass.minCLevel();
}
maxCLevel(): number {
return this._zstdClass.maxCLevel();
}
}
+3
View File
@@ -0,0 +1,3 @@
export declare const PKG_NAME = "@hpcc-js/wasm-zstd";
export declare const PKG_VERSION = "1.7.0";
export declare const BUILD_VERSION = "4.0.0";
+1
View File
@@ -0,0 +1 @@
export * from "./zstd.ts";
+124
View File
@@ -0,0 +1,124 @@
/**
* The Zstandard WASM library, provides a simplified wrapper around the Zstandard c++ library.
*
* See [Zstandard](https://facebook.github.io/zstd/) for more details.
*
* ```ts
* import { Zstd } from "@hpcc-js/wasm-zstd";
*
* const zstd = await Zstd.load();
*
* // Generate some "data"
* const data = new Uint8Array(Array.from({ length: 100000 }, (_, i) => i % 256));
*
* const compressed_data = zstd.compress(data);
* const decompressed_data = zstd.decompress(compressed_data);
* ```
*
* ::: warning
* `Zstd.load()` returns a process-wide singleton. Streaming compression and decompression
* mutate shared codec state and must be serialized by the caller. Do not interleave
* concurrent streaming operations on the same instance.
* :::
*/
export declare class Zstd {
private _mainModule;
private _zstdClass;
private _zstd;
private _compressionLevel;
private _decompressFrameCompleted;
private _decompressRemaining;
private _decompressSawInput;
private constructor();
/**
* Compiles and instantiates the raw wasm.
*
* ::: info
* In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing `load` to be asynchronous;
* :::
*
* @returns A promise to an instance of the Zstd class.
*/
static load(): Promise<Zstd>;
/**
* Unloades the compiled wasm instance.
*/
static unload(): void;
private copyHeap;
private mallocChecked;
private throwStreamError;
private ensureStreamOk;
/**
* @returns The Zstd c++ version
*/
version(): string;
/**
* Resets both compression and decompression state (legacy API).
* Prefer {@link resetCompression} / {@link resetDecompression} so the two
* contexts are not reset accidentally together.
*/
reset(): void;
/**
* Resets compression state and optionally sets the compression level.
* @param compressionLevel When omitted, the current level is kept.
*/
resetCompression(compressionLevel?: number): void;
/**
* Resets decompression state before a new streaming decode.
*/
resetDecompression(): void;
/**
* Sets the compression level for streaming compression.
* @param level Compression level (use minCLevel() to maxCLevel())
*/
setCompressionLevel(level: number): void;
/**
* @param data Data to be compressed
* @param compressionLevel Compression v Speed tradeoff, when omitted it will default to `zstd.defaultCLevel()` which is currently 3.
* @returns Compressed data.
*
* :::tip
* 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).
* :::
*/
compress(data: Uint8Array, compressionLevel?: number): Uint8Array;
/**
* Compresses a chunk of data in streaming mode.
* Call {@link resetCompression} before the first chunk, then {@link compressChunk} for each chunk, and finally {@link compressEnd}.
* Intermediate calls may return an empty array when Zstandard buffers input.
* @param data Chunk of data to be compressed
* @returns Compressed chunk data produced for this input (may be empty)
*/
compressChunk(data: Uint8Array): Uint8Array;
/**
* Finishes the streaming compression and returns any remaining compressed data.
* @returns Final compressed data
*/
compressEnd(): Uint8Array;
private frameContentSize;
/**
* @param compressedData Data to be decompressed
* @returns Uncompressed data.
*/
decompress(compressedData: Uint8Array): Uint8Array;
private decompressViaStreaming;
/**
* Decompresses a chunk of data in streaming mode.
* Call {@link resetDecompression} before the first chunk, then {@link decompressChunk} for each chunk, and finally {@link decompressEnd}.
* Intermediate calls may return an empty array. Callers must not guess an output size.
* @param compressedData Chunk of compressed data
* @returns Decompressed chunk data produced for this input (may be empty)
*/
decompressChunk(compressedData: Uint8Array): Uint8Array;
/**
* Verifies that streaming decompression completed a full frame and was not truncated.
* Empty intermediate output is not treated as completion; call this after the final chunk.
*/
decompressEnd(): void;
/**
* @returns Default compression level (see notes above above).
*/
defaultCLevel(): number;
minCLevel(): number;
maxCLevel(): number;
}
+84
View File
@@ -0,0 +1,84 @@
// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.
declare var RuntimeExports: {
/**
* Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the
* emscripten HEAP, returns a copy of that string as a Javascript String object.
*
* @param {number} ptr
* @param {number=} maxBytesToRead - An optional length that specifies the
* maximum number of bytes to read. You can omit this parameter to scan the
* string until the first 0 byte. If maxBytesToRead is passed, and the string
* at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the
* string will cut short at that byte index.
* @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
* @return {string}
*/
UTF8ToString: (ptr: number, maxBytesToRead?: number | undefined, ignoreNul?: boolean | undefined) => string;
lengthBytesUTF8: (str: any) => number;
stringToUTF8: (str: any, outPtr: any, maxBytesToWrite: any) => any;
/** @type {!Uint8Array} */
HEAPU8: Uint8Array;
};
interface WasmModule {
_malloc(_0: number): number;
_free(_0: number): void;
}
type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
export interface ClassHandle {
isAliasOf(other: ClassHandle): boolean;
delete(): void;
deleteLater(): this;
isDeleted(): boolean;
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
[Symbol.dispose](): void;
clone(): this;
}
export interface zstd extends ClassHandle {
reset(): StreamResult;
resetCompression(_0: number): StreamResult;
resetDecompression(): StreamResult;
setCompressionLevel(_0: number): StreamResult;
compressChunk(_0: number, _1: number, _2: number, _3: number): StreamResult;
compressEnd(_0: number, _1: number): StreamResult;
decompressChunk(_0: number, _1: number, _2: number, _3: number): StreamResult;
}
export type StreamResult = {
consumed: number,
produced: number,
remaining: number,
error: boolean,
errorName: EmbindString
};
export type FrameContentSizeResult = {
known: boolean,
error: boolean,
size: number,
errorName: EmbindString
};
interface EmbindModule {
zstd: {
new(): zstd;
minCLevel(): number;
maxCLevel(): number;
defaultCLevel(): number;
malloc(_0: number): number;
free(_0: number): void;
compress(_0: number, _1: number, _2: number, _3: number, _4: number): number;
decompress(_0: number, _1: number, _2: number, _3: number): number;
findFrameCompressedSize(_0: number, _1: number): number;
compressBound(_0: number): number;
CStreamOutSize(): number;
DStreamOutSize(): number;
isError(_0: number): number;
getFrameContentSize(_0: number, _1: number): FrameContentSizeResult;
version(): string;
getErrorName(_0: number): string;
};
}
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;
export default function MainModuleFactory (options?: unknown): Promise<MainModule>;