defuss-brotli 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -0,0 +1,93 @@
1
+ import { type WasmSource } from "./wasm.js";
2
+ /**
3
+ * Options for Brotli decompression.
4
+ */
5
+ export interface BrotliDecompressOptions {
6
+ /**
7
+ * Hard limit for decompressed output size in bytes.
8
+ *
9
+ * Guards against **decompression bombs** — a tiny compressed payload that
10
+ * expands into gigabytes. If the decoded output would exceed this limit
11
+ * the WASM module throws immediately.
12
+ *
13
+ * @default 67_108_864 (64 MiB)
14
+ */
15
+ maxOutputSize?: number;
16
+ }
17
+ /**
18
+ * Default decompressed-output cap: 64 MiB.
19
+ * Raise explicitly if you legitimately expect larger payloads.
20
+ */
21
+ export declare const DEFAULT_MAX_OUTPUT_SIZE: number;
22
+ /** The raw decoder after `init()`, without option defaults or validation; `null` before. */
23
+ export declare let wasmDecompressBytes: ((input: Uint8Array, maxOutputSize: number) => Uint8Array) | null;
24
+ /**
25
+ * Throws if `init()` has not been called yet.
26
+ * Used internally before every decompress call.
27
+ */
28
+ export declare function assertReady(): void;
29
+ /**
30
+ * Validates and applies defaults for decompression options.
31
+ *
32
+ * @throws If `maxOutputSize` is not a positive integer.
33
+ */
34
+ export declare function normalizeOptions(options?: BrotliDecompressOptions): Required<BrotliDecompressOptions>;
35
+ /**
36
+ * Instantiates the WASM module from `source()` on the first call; later calls return the same Promise
37
+ * until it settles. A rejected load is forgotten, so the next call tries again.
38
+ *
39
+ * @internal Called by the entry points' `init()`; not part of the public API.
40
+ */
41
+ export declare function initFrom(source: () => WasmSource): Promise<void>;
42
+ /**
43
+ * Non-throwing readiness check.
44
+ *
45
+ * @returns `true` after `init()` has resolved, `false` otherwise.
46
+ */
47
+ export declare function isReady(): boolean;
48
+ /**
49
+ * Decompress a Brotli-compressed `Uint8Array` back to raw bytes.
50
+ *
51
+ * Use this when the decompressed content is **binary** (images, protobuf,
52
+ * WASM, etc.) or when you want to handle text decoding yourself.
53
+ *
54
+ * For UTF-8 text payloads, see {@link decompressText}.
55
+ *
56
+ * @param input Brotli-compressed bytes.
57
+ * @param options Decompression limits — see {@link BrotliDecompressOptions}.
58
+ * @returns A new `Uint8Array` containing the decompressed output.
59
+ *
60
+ * @throws If `init()` was not called first.
61
+ * @throws If the output would exceed `maxOutputSize`.
62
+ * @throws If the input is not valid Brotli data.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * await init();
67
+ * const raw = decompress(compressedBytes);
68
+ * ```
69
+ */
70
+ export declare function decompress(input: Uint8Array, options?: BrotliDecompressOptions): Uint8Array;
71
+ /**
72
+ * Decompress a Brotli-compressed `Uint8Array` and decode the result as a
73
+ * UTF-8 string.
74
+ *
75
+ * Internally calls {@link decompress} and then applies `TextDecoder` to
76
+ * convert the bytes back to a JS string. This is the counterpart to
77
+ * `compressText()` from the compressor module.
78
+ *
79
+ * **Note:** If the decompressed bytes are not valid UTF-8, `TextDecoder`
80
+ * will insert U+FFFD replacement characters rather than throwing.
81
+ *
82
+ * @param input Brotli-compressed bytes that decode to UTF-8 text.
83
+ * @param options Decompression limits — see {@link BrotliDecompressOptions}.
84
+ * @returns The decompressed UTF-8 string.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * await init();
89
+ * const json = decompressText(compressedJsonBytes);
90
+ * const data = JSON.parse(json);
91
+ * ```
92
+ */
93
+ export declare function decompressText(input: Uint8Array, options?: BrotliDecompressOptions): string;
@@ -0,0 +1,17 @@
1
+ import type { WasmSource } from "./wasm.js";
2
+ export { DEFAULT_MAX_OUTPUT_SIZE, assertReady, decompress, decompressText, isReady, normalizeOptions, wasmDecompressBytes, type BrotliDecompressOptions, } from "./decompressor-core.js";
3
+ export type { WasmSource } from "./wasm.js";
4
+ /**
5
+ * Load the Brotli decompressor WASM module from a separate `.wasm` file. Must be called (and awaited) once before
6
+ * any `decompress` / `decompressText` call.
7
+ *
8
+ * Without `source`, it loads `decompressor.wasm` from next to this module (`new URL("./decompressor.wasm",
9
+ * import.meta.url)`): browsers fetch it, Node.js 20.16+ / 22.3+ and Bun read it from disk (VERIFIED: `make e2e`,
10
+ * Chromium fetching it next to a Bun bundle and Node.js 26 reading it from node_modules). A bundler that does not
11
+ * carry that file along needs `source`: a URL where you serve `defuss-brotli/decompressor.wasm`, `fetch(...)`,
12
+ * the bytes, or a compiled `WebAssembly.Module`.
13
+ *
14
+ * Idempotent: later calls return the first call's Promise and ignore their `source`; after a failed load, the next
15
+ * call tries again.
16
+ */
17
+ export declare function init(source?: WasmSource): Promise<void>;
@@ -0,0 +1 @@
1
+ var v=Object.defineProperty;var F=(e)=>e;function z(e,t){this[e]=F.bind(null,t)}var k=(e,t)=>{for(var r in t)v(e,r,{get:t[r],enumerable:!0,configurable:!0,set:z.bind(t,r)})};var A={};k(A,{Decoder:()=>d,__wbg_Error_83742b46f01ce22d:()=>L,__wbg___wbindgen_throw_6ddd609b62940d55:()=>j,__wbg_set_wasm:()=>H});class d{__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,S.unregister(this),e}free(){let e=this.__destroy_into_raw();s.__wbg_decoder_free(e,0)}constructor(e,t){try{let i=s.__wbindgen_add_to_stack_pointer(-16);s.decoder_create(i,e,t);var r=c().getInt32(i+0,!0),n=c().getInt32(i+4,!0),o=c().getInt32(i+8,!0);if(o)throw O(n);return this.__wbg_ptr=r>>>0,S.register(this,this.__wbg_ptr,this),this}finally{s.__wbindgen_add_to_stack_pointer(16)}}decode(e,t){try{let i=s.__wbindgen_add_to_stack_pointer(-16);s.decoder_decode(i,this.__wbg_ptr,e,t);var r=c().getInt32(i+0,!0),n=c().getInt32(i+4,!0),o=c().getInt32(i+8,!0);if(o)throw O(n);return r>>>0}finally{s.__wbindgen_add_to_stack_pointer(16)}}finished(){return s.decoder_finished(this.__wbg_ptr)!==0}input_capacity(){return s.decoder_input_capacity(this.__wbg_ptr)>>>0}input_ptr(){return s.decoder_input_ptr(this.__wbg_ptr)>>>0}needs_input(){return s.decoder_needs_input(this.__wbg_ptr)!==0}output_ptr(){return s.decoder_output_ptr(this.__wbg_ptr)>>>0}}if(Symbol.dispose)d.prototype[Symbol.dispose]=d.prototype.free;function L(e,t){let r=Error(B(e,t));return C(r)}function j(e,t){throw Error(B(e,t))}var S=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry((e)=>s.__wbg_decoder_free(e>>>0,1));function C(e){if(l===a.length)a.push(a.length+1);let t=l;return l=a[t],a[t]=e,t}function V(e){if(e<1028)return;a[e]=l,l=e}var u=null;function c(){if(u===null||u.buffer.detached===!0||u.buffer.detached===void 0&&u.buffer!==s.memory.buffer)u=new DataView(s.memory.buffer);return u}function B(e,t){return e=e>>>0,G(e,t)}var m=null;function X(){if(m===null||m.byteLength===0)m=new Uint8Array(s.memory.buffer);return m}function N(e){return a[e]}var a=Array(1024).fill(void 0);a.push(void 0,null,!0,!1);var l=a.length;function O(e){let t=N(e);return V(e),t}var y=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0});y.decode();var Z=2146435072,h=0;function G(e,t){if(h+=t,h>=Z)y=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0}),y.decode(),h=t;return y.decode(X().subarray(e,e+t))}var s;function H(e){s=e}async function q(e){let r=globalThis.process?.getBuiltinModule?.("node:fs/promises");if(!r)throw Error(`cannot read ${e.href} in this runtime; pass the .wasm bytes or a Response to init() instead`);return r.readFile(e)}async function Y(e,t){if(!e.ok)throw Error(`failed to load WASM from ${e.url}: HTTP ${e.status}`);if(typeof WebAssembly.instantiateStreaming==="function"&&e.headers.get("Content-Type")==="application/wasm")return(await WebAssembly.instantiateStreaming(e,t)).instance;return(await WebAssembly.instantiate(await e.arrayBuffer(),t)).instance}async function M(e,t,r){let n={[t]:r},o=await e;if(typeof o==="string"||o instanceof URL){let p=new URL(o,globalThis.location?.href);o=p.protocol==="file:"?await q(p):await fetch(p)}let i;if(o instanceof WebAssembly.Module)i=await WebAssembly.instantiate(o,n);else if(o instanceof Response)i=await Y(o,n);else i=(await WebAssembly.instantiate(o,n)).instance;return r.__wbg_set_wasm(i.exports),i.exports.memory}function D(e,t){if(e.length===1)return e[0];let r=new Uint8Array(t),n=0;for(let o of e)r.set(o,n),n+=o.length;return r}var R=67108864,J=4294967295,b=null,x=!1;function K(e,t,r){let n=new d(t.length,r);try{let o=n.input_ptr(),i=n.output_ptr(),p=n.input_capacity(),U=[],f=0,W=0;do{let _=0;if(n.needs_input())_=Math.min(p,t.length-f),new Uint8Array(e.buffer,o,_).set(t.subarray(f,f+_)),f+=_;let g=n.decode(_,f===t.length);if(g>0)U.push(new Uint8Array(e.buffer,i,g).slice());W+=g}while(!n.finished());return D(U,W)}finally{n.free()}}var w=null;function T(){if(!x||!w)throw Error("defuss-brotli/decompressor is not initialized. Call await init() first.")}function E(e={}){let t=e.maxOutputSize??R;if(!Number.isInteger(t)||t<=0)throw Error("maxOutputSize must be a positive integer");return{maxOutputSize:t}}function I(e){if(!b)b=Promise.resolve().then(()=>M(e(),"./defuss_brotli_decompressor_bg.js",A)).then((t)=>{w=(r,n)=>K(t,r,n),x=!0},(t)=>{throw b=null,t});return b}function Q(){return x}function P(e,t={}){T();let{maxOutputSize:r}=E(t);return w(e,Math.min(r,J))}function ee(e,t={}){return new TextDecoder().decode(P(e,t))}function se(e){return I(()=>e??new URL("./decompressor.wasm",import.meta.url))}export{R as DEFAULT_MAX_OUTPUT_SIZE,T as assertReady,P as decompress,ee as decompressText,se as init,Q as isReady,E as normalizeOptions,w as wasmDecompressBytes};
@@ -1,35 +1,4 @@
1
- /**
2
- * Options for Brotli decompression.
3
- */
4
- export interface BrotliDecompressOptions {
5
- /**
6
- * Hard limit for decompressed output size in bytes.
7
- *
8
- * Guards against **decompression bombs** — a tiny compressed payload that
9
- * expands into gigabytes. If the decoded output would exceed this limit
10
- * the WASM module throws immediately.
11
- *
12
- * @default 67_108_864 (64 MiB)
13
- */
14
- maxOutputSize?: number;
15
- }
16
- /**
17
- * Default decompressed-output cap: 64 MiB.
18
- * Raise explicitly if you legitimately expect larger payloads.
19
- */
20
- export declare const DEFAULT_MAX_OUTPUT_SIZE: number;
21
- export declare let wasmDecompressBytes: ((input: Uint8Array, maxOutputSize: number) => Uint8Array) | null;
22
- /**
23
- * Throws if `init()` has not been called yet.
24
- * Used internally before every decompress call.
25
- */
26
- export declare function assertReady(): void;
27
- /**
28
- * Validates and applies defaults for decompression options.
29
- *
30
- * @throws If `maxOutputSize` is not a positive integer.
31
- */
32
- export declare function normalizeOptions(options?: BrotliDecompressOptions): Required<BrotliDecompressOptions>;
1
+ export { DEFAULT_MAX_OUTPUT_SIZE, assertReady, decompress, decompressText, isReady, normalizeOptions, wasmDecompressBytes, type BrotliDecompressOptions, } from "./decompressor-core.js";
33
2
  /**
34
3
  * Load the Brotli decompressor WASM module. Must be called (and awaited)
35
4
  * once before any `decompress` / `decompressText` call.
@@ -40,55 +9,3 @@ export declare function normalizeOptions(options?: BrotliDecompressOptions): Req
40
9
  * Works identically in Node.js and browsers.
41
10
  */
42
11
  export declare function init(): Promise<void>;
43
- /**
44
- * Non-throwing readiness check.
45
- *
46
- * @returns `true` after `init()` has resolved, `false` otherwise.
47
- */
48
- export declare function isReady(): boolean;
49
- /**
50
- * Decompress a Brotli-compressed `Uint8Array` back to raw bytes.
51
- *
52
- * Use this when the decompressed content is **binary** (images, protobuf,
53
- * WASM, etc.) or when you want to handle text decoding yourself.
54
- *
55
- * For UTF-8 text payloads, see {@link decompressText}.
56
- *
57
- * @param input Brotli-compressed bytes.
58
- * @param options Decompression limits — see {@link BrotliDecompressOptions}.
59
- * @returns A new `Uint8Array` containing the decompressed output.
60
- *
61
- * @throws If `init()` was not called first.
62
- * @throws If the output would exceed `maxOutputSize`.
63
- * @throws If the input is not valid Brotli data.
64
- *
65
- * @example
66
- * ```ts
67
- * await init();
68
- * const raw = decompress(compressedBytes);
69
- * ```
70
- */
71
- export declare function decompress(input: Uint8Array, options?: BrotliDecompressOptions): Uint8Array;
72
- /**
73
- * Decompress a Brotli-compressed `Uint8Array` and decode the result as a
74
- * UTF-8 string.
75
- *
76
- * Internally calls {@link decompress} and then applies `TextDecoder` to
77
- * convert the bytes back to a JS string. This is the counterpart to
78
- * `compressText()` from the compressor module.
79
- *
80
- * **Note:** If the decompressed bytes are not valid UTF-8, `TextDecoder`
81
- * will insert U+FFFD replacement characters rather than throwing.
82
- *
83
- * @param input Brotli-compressed bytes that decode to UTF-8 text.
84
- * @param options Decompression limits — see {@link BrotliDecompressOptions}.
85
- * @returns The decompressed UTF-8 string.
86
- *
87
- * @example
88
- * ```ts
89
- * await init();
90
- * const json = decompressText(compressedJsonBytes);
91
- * const data = JSON.parse(json);
92
- * ```
93
- */
94
- export declare function decompressText(input: Uint8Array, options?: BrotliDecompressOptions): string;