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.
- package/README.md +159 -144
- package/THIRD_PARTY_NOTICES.txt +163 -0
- package/dist/compressor-core.d.ts +104 -0
- package/dist/compressor-slim.d.ts +17 -0
- package/dist/compressor-slim.js +1 -0
- package/dist/compressor.d.ts +1 -96
- package/dist/compressor.js +1 -190
- package/dist/compressor.wasm +0 -0
- package/dist/decompressor-core.d.ts +93 -0
- package/dist/decompressor-slim.d.ts +17 -0
- package/dist/decompressor-slim.js +1 -0
- package/dist/decompressor.d.ts +1 -84
- package/dist/decompressor.js +1 -183
- package/dist/decompressor.wasm +0 -0
- package/dist/wasm.d.ts +24 -0
- package/package.json +20 -7
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type WasmSource } from "./wasm.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for Brotli compression.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* compress(data, { quality: 11, lgwin: 22 }); // smallest output, slowest
|
|
8
|
+
* compress(data, { quality: 1 }); // fastest, larger output
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export interface BrotliCompressOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Brotli compression quality level (0–11).
|
|
14
|
+
*
|
|
15
|
+
* - **0** — fastest compression, largest output
|
|
16
|
+
* - **5–7** — good balance for web text (HTML/CSS/JS/JSON)
|
|
17
|
+
* - **11** — smallest output, slowest compression
|
|
18
|
+
*
|
|
19
|
+
* @default 6
|
|
20
|
+
*/
|
|
21
|
+
quality?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Sliding window size exponent (10–24).
|
|
24
|
+
*
|
|
25
|
+
* Larger windows let the encoder find longer matches at the cost of more
|
|
26
|
+
* memory. **22** is a strong default for typical web payloads.
|
|
27
|
+
*
|
|
28
|
+
* @default 22
|
|
29
|
+
*/
|
|
30
|
+
lgwin?: number;
|
|
31
|
+
}
|
|
32
|
+
/** Default compression quality. Good balance of speed and ratio for web text. */
|
|
33
|
+
export declare const DEFAULT_QUALITY = 6;
|
|
34
|
+
/** Default sliding window exponent. Covers most web-text repetition patterns. */
|
|
35
|
+
export declare const DEFAULT_LGWIN = 22;
|
|
36
|
+
/**
|
|
37
|
+
* Throws if `init()` has not been called yet.
|
|
38
|
+
* Used internally before every compress call.
|
|
39
|
+
*/
|
|
40
|
+
export declare function assertReady(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Validates and applies defaults for compression options.
|
|
43
|
+
*
|
|
44
|
+
* @throws If `quality` is not an integer in 0–11 or `lgwin` is not in 10–24.
|
|
45
|
+
*/
|
|
46
|
+
export declare function normalizeOptions(options?: BrotliCompressOptions): Required<BrotliCompressOptions>;
|
|
47
|
+
/**
|
|
48
|
+
* Instantiates the WASM module from `source()` on the first call; later calls return the same Promise
|
|
49
|
+
* until it settles. A rejected load is forgotten, so the next call tries again.
|
|
50
|
+
*
|
|
51
|
+
* @internal Called by the entry points' `init()`; not part of the public API.
|
|
52
|
+
*/
|
|
53
|
+
export declare function initFrom(source: () => WasmSource): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Non-throwing readiness check.
|
|
56
|
+
*
|
|
57
|
+
* @returns `true` after `init()` has resolved, `false` otherwise.
|
|
58
|
+
*/
|
|
59
|
+
export declare function isReady(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Compress raw bytes with Brotli.
|
|
62
|
+
*
|
|
63
|
+
* Use this when you already have a `Uint8Array` (binary data, pre-encoded
|
|
64
|
+
* text, HTTP response bodies, etc.). No encoding is performed — what you
|
|
65
|
+
* pass in is exactly what gets compressed.
|
|
66
|
+
*
|
|
67
|
+
* For compressing JS strings directly, see {@link compressText}.
|
|
68
|
+
*
|
|
69
|
+
* @param input Raw bytes to compress.
|
|
70
|
+
* @param options Compression tuning — see {@link BrotliCompressOptions}.
|
|
71
|
+
* @returns A new `Uint8Array` containing the Brotli-compressed output.
|
|
72
|
+
*
|
|
73
|
+
* @throws If `init()` was not called first.
|
|
74
|
+
* @throws If options are out of range.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* await init();
|
|
79
|
+
* const compressed = compress(myFileBytes, { quality: 11 });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function compress(input: Uint8Array, options?: BrotliCompressOptions): Uint8Array;
|
|
83
|
+
/**
|
|
84
|
+
* Compress a JS string with Brotli.
|
|
85
|
+
*
|
|
86
|
+
* Internally encodes the string to UTF-8 via `TextEncoder` and then
|
|
87
|
+
* delegates to {@link compress}. This is the right choice when your input is
|
|
88
|
+
* a plain string (JSON, HTML, Markdown, etc.) and you don't want to deal
|
|
89
|
+
* with encoding yourself.
|
|
90
|
+
*
|
|
91
|
+
* To decompress back to a string, use `decompressText()` from the
|
|
92
|
+
* decompressor module.
|
|
93
|
+
*
|
|
94
|
+
* @param text The string to compress.
|
|
95
|
+
* @param options Compression tuning — see {@link BrotliCompressOptions}.
|
|
96
|
+
* @returns A new `Uint8Array` containing the Brotli-compressed output.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* await init();
|
|
101
|
+
* const compressed = compressText('{"hello":"world"}', { quality: 6 });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export declare function compressText(text: string, options?: BrotliCompressOptions): Uint8Array;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { WasmSource } from "./wasm.js";
|
|
2
|
+
export { DEFAULT_LGWIN, DEFAULT_QUALITY, assertReady, compress, compressText, isReady, normalizeOptions, type BrotliCompressOptions, } from "./compressor-core.js";
|
|
3
|
+
export type { WasmSource } from "./wasm.js";
|
|
4
|
+
/**
|
|
5
|
+
* Load the Brotli compressor WASM module from a separate `.wasm` file. Must be called (and awaited) once before
|
|
6
|
+
* any `compress` / `compressText` call.
|
|
7
|
+
*
|
|
8
|
+
* Without `source`, it loads `compressor.wasm` from next to this module (`new URL("./compressor.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/compressor.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 P=Object.defineProperty;var k=(e)=>e;function C(e,t){this[e]=k.bind(null,t)}var j=(e,t)=>{for(var r in t)P(e,r,{get:t[r],enumerable:!0,configurable:!0,set:C.bind(t,r)})};var h={};j(h,{Encoder:()=>f,__wbg_Error_83742b46f01ce22d:()=>z,__wbg___wbindgen_throw_6ddd609b62940d55:()=>q,__wbg_set_wasm:()=>X});class f{__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,B.unregister(this),e}free(){let e=this.__destroy_into_raw();o.__wbg_encoder_free(e,0)}constructor(e,t,r){try{let a=o.__wbindgen_add_to_stack_pointer(-16);o.encoder_create(a,e,t,r);var i=l().getInt32(a+0,!0),n=l().getInt32(a+4,!0),s=l().getInt32(a+8,!0);if(s)throw R(n);return this.__wbg_ptr=i>>>0,B.register(this,this.__wbg_ptr,this),this}finally{o.__wbindgen_add_to_stack_pointer(16)}}encode(e,t){try{let s=o.__wbindgen_add_to_stack_pointer(-16);o.encoder_encode(s,this.__wbg_ptr,e,t);var r=l().getInt32(s+0,!0),i=l().getInt32(s+4,!0),n=l().getInt32(s+8,!0);if(n)throw R(i);return r>>>0}finally{o.__wbindgen_add_to_stack_pointer(16)}}finished(){return o.encoder_finished(this.__wbg_ptr)!==0}input_capacity(){return o.encoder_input_capacity(this.__wbg_ptr)>>>0}input_ptr(){return o.encoder_input_ptr(this.__wbg_ptr)>>>0}needs_input(){return o.encoder_needs_input(this.__wbg_ptr)!==0}output_ptr(){return o.encoder_output_ptr(this.__wbg_ptr)>>>0}}if(Symbol.dispose)f.prototype[Symbol.dispose]=f.prototype.free;function z(e,t){let r=Error(T(e,t));return N(r)}function q(e,t){throw Error(T(e,t))}var B=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry((e)=>o.__wbg_encoder_free(e>>>0,1));function N(e){if(m===u.length)u.push(u.length+1);let t=m;return m=u[t],u[t]=e,t}function G(e){if(e<1028)return;u[e]=m,m=e}var c=null;function l(){if(c===null||c.buffer.detached===!0||c.buffer.detached===void 0&&c.buffer!==o.memory.buffer)c=new DataView(o.memory.buffer);return c}function T(e,t){return e=e>>>0,Q(e,t)}var d=null;function V(){if(d===null||d.byteLength===0)d=new Uint8Array(o.memory.buffer);return d}function Y(e){return u[e]}var u=Array(1024).fill(void 0);u.push(void 0,null,!0,!1);var m=u.length;function R(e){let t=Y(e);return G(e),t}var y=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0});y.decode();var H=2146435072,g=0;function Q(e,t){if(g+=t,g>=H)y=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0}),y.decode(),g=t;return y.decode(V().subarray(e,e+t))}var o;function X(e){o=e}async function J(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 K(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 E(e,t,r){let i={[t]:r},n=await e;if(typeof n==="string"||n instanceof URL){let a=new URL(n,globalThis.location?.href);n=a.protocol==="file:"?await J(a):await fetch(a)}let s;if(n instanceof WebAssembly.Module)s=await WebAssembly.instantiate(n,i);else if(n instanceof Response)s=await K(n,i);else s=(await WebAssembly.instantiate(n,i)).instance;return r.__wbg_set_wasm(s.exports),s.exports.memory}function M(e,t){if(e.length===1)return e[0];let r=new Uint8Array(t),i=0;for(let n of e)r.set(n,i),i+=n.length;return r}var I=6,F=22,b=null,A=!1,x=null;function Z(e,t,r,i){let n=new f(t.length,r,i);try{let s=n.input_ptr(),a=n.output_ptr(),D=n.input_capacity(),U=[],_=0,W=0;do{let p=0;if(n.needs_input())p=Math.min(D,t.length-_),new Uint8Array(e.buffer,s,p).set(t.subarray(_,_+p)),_+=p;let w=n.encode(p,_===t.length);if(w>0)U.push(new Uint8Array(e.buffer,a,w).slice());W+=w}while(!n.finished());return M(U,W)}finally{n.free()}}function L(){if(!A||!x)throw Error("defuss-brotli/compressor is not initialized. Call await init() first.")}function O(e={}){let t=e.quality??I,r=e.lgwin??F;if(!Number.isInteger(t)||t<0||t>11)throw Error("quality must be an integer between 0 and 11");if(!Number.isInteger(r)||r<10||r>24)throw Error("lgwin must be an integer between 10 and 24");return{quality:t,lgwin:r}}function S(e){if(!b)b=Promise.resolve().then(()=>E(e(),"./defuss_brotli_compressor_bg.js",h)).then((t)=>{x=t,A=!0},(t)=>{throw b=null,t});return b}function ee(){return A}function v(e,t={}){L();let{quality:r,lgwin:i}=O(t);return Z(x,e,r,i)}function te(e,t={}){return v(new TextEncoder().encode(e),t)}function ae(e){return S(()=>e??new URL("./compressor.wasm",import.meta.url))}export{F as DEFAULT_LGWIN,I as DEFAULT_QUALITY,L as assertReady,v as compress,te as compressText,ae as init,ee as isReady,O as normalizeOptions};
|
package/dist/compressor.d.ts
CHANGED
|
@@ -1,48 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Options for Brotli compression.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* ```ts
|
|
6
|
-
* compress(data, { quality: 11, lgwin: 22 }); // smallest output, slowest
|
|
7
|
-
* compress(data, { quality: 1 }); // fastest, larger output
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
export interface BrotliCompressOptions {
|
|
11
|
-
/**
|
|
12
|
-
* Brotli compression quality level (0–11).
|
|
13
|
-
*
|
|
14
|
-
* - **0** — fastest compression, largest output
|
|
15
|
-
* - **5–7** — good balance for web text (HTML/CSS/JS/JSON)
|
|
16
|
-
* - **11** — smallest output, slowest compression
|
|
17
|
-
*
|
|
18
|
-
* @default 6
|
|
19
|
-
*/
|
|
20
|
-
quality?: number;
|
|
21
|
-
/**
|
|
22
|
-
* Sliding window size exponent (10–24).
|
|
23
|
-
*
|
|
24
|
-
* Larger windows let the encoder find longer matches at the cost of more
|
|
25
|
-
* memory. **22** is a strong default for typical web payloads.
|
|
26
|
-
*
|
|
27
|
-
* @default 22
|
|
28
|
-
*/
|
|
29
|
-
lgwin?: number;
|
|
30
|
-
}
|
|
31
|
-
/** Default compression quality. Good balance of speed and ratio for web text. */
|
|
32
|
-
export declare const DEFAULT_QUALITY = 6;
|
|
33
|
-
/** Default sliding window exponent. Covers most web-text repetition patterns. */
|
|
34
|
-
export declare const DEFAULT_LGWIN = 22;
|
|
35
|
-
/**
|
|
36
|
-
* Throws if `init()` has not been called yet.
|
|
37
|
-
* Used internally before every compress call.
|
|
38
|
-
*/
|
|
39
|
-
export declare function assertReady(): void;
|
|
40
|
-
/**
|
|
41
|
-
* Validates and applies defaults for compression options.
|
|
42
|
-
*
|
|
43
|
-
* @throws If `quality` is not an integer in 0–11 or `lgwin` is not in 10–24.
|
|
44
|
-
*/
|
|
45
|
-
export declare function normalizeOptions(options?: BrotliCompressOptions): Required<BrotliCompressOptions>;
|
|
1
|
+
export { DEFAULT_LGWIN, DEFAULT_QUALITY, assertReady, compress, compressText, isReady, normalizeOptions, type BrotliCompressOptions, } from "./compressor-core.js";
|
|
46
2
|
/**
|
|
47
3
|
* Load the Brotli compressor WASM module. Must be called (and awaited) once
|
|
48
4
|
* before any `compress` / `compressText` call.
|
|
@@ -53,54 +9,3 @@ export declare function normalizeOptions(options?: BrotliCompressOptions): Requi
|
|
|
53
9
|
* Works identically in Node.js and browsers.
|
|
54
10
|
*/
|
|
55
11
|
export declare function init(): Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Non-throwing readiness check.
|
|
58
|
-
*
|
|
59
|
-
* @returns `true` after `init()` has resolved, `false` otherwise.
|
|
60
|
-
*/
|
|
61
|
-
export declare function isReady(): boolean;
|
|
62
|
-
/**
|
|
63
|
-
* Compress raw bytes with Brotli.
|
|
64
|
-
*
|
|
65
|
-
* Use this when you already have a `Uint8Array` (binary data, pre-encoded
|
|
66
|
-
* text, HTTP response bodies, etc.). No encoding is performed — what you
|
|
67
|
-
* pass in is exactly what gets compressed.
|
|
68
|
-
*
|
|
69
|
-
* For compressing JS strings directly, see {@link compressText}.
|
|
70
|
-
*
|
|
71
|
-
* @param input Raw bytes to compress.
|
|
72
|
-
* @param options Compression tuning — see {@link BrotliCompressOptions}.
|
|
73
|
-
* @returns A new `Uint8Array` containing the Brotli-compressed output.
|
|
74
|
-
*
|
|
75
|
-
* @throws If `init()` was not called first.
|
|
76
|
-
* @throws If options are out of range.
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```ts
|
|
80
|
-
* await init();
|
|
81
|
-
* const compressed = compress(myFileBytes, { quality: 11 });
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
export declare function compress(input: Uint8Array, options?: BrotliCompressOptions): Uint8Array;
|
|
85
|
-
/**
|
|
86
|
-
* Compress a JS string with Brotli.
|
|
87
|
-
*
|
|
88
|
-
* Internally encodes the string to UTF-8 via `TextEncoder` and then
|
|
89
|
-
* delegates to {@link compress}. This is the right choice when your input is
|
|
90
|
-
* a plain string (JSON, HTML, Markdown, etc.) and you don't want to deal
|
|
91
|
-
* with encoding yourself.
|
|
92
|
-
*
|
|
93
|
-
* To decompress back to a string, use `decompressText()` from the
|
|
94
|
-
* decompressor module.
|
|
95
|
-
*
|
|
96
|
-
* @param text The string to compress.
|
|
97
|
-
* @param options Compression tuning — see {@link BrotliCompressOptions}.
|
|
98
|
-
* @returns A new `Uint8Array` containing the Brotli-compressed output.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* await init();
|
|
103
|
-
* const compressed = compressText('{"hello":"world"}', { quality: 6 });
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
export declare function compressText(text: string, options?: BrotliCompressOptions): Uint8Array;
|