@scelar/nodepod 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/LICENSE +43 -0
  2. package/README.md +240 -0
  3. package/dist/child_process-BJOMsZje.js +8233 -0
  4. package/dist/child_process-BJOMsZje.js.map +1 -0
  5. package/dist/child_process-Cj8vOcuc.cjs +7434 -0
  6. package/dist/child_process-Cj8vOcuc.cjs.map +1 -0
  7. package/dist/index-Cb1Cgdnd.js +35308 -0
  8. package/dist/index-Cb1Cgdnd.js.map +1 -0
  9. package/dist/index-DsMGS-xc.cjs +37195 -0
  10. package/dist/index-DsMGS-xc.cjs.map +1 -0
  11. package/dist/index.cjs +65 -0
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.mjs +59 -0
  14. package/dist/index.mjs.map +1 -0
  15. package/package.json +95 -0
  16. package/src/__tests__/smoke.test.ts +11 -0
  17. package/src/constants/cdn-urls.ts +18 -0
  18. package/src/constants/config.ts +236 -0
  19. package/src/cross-origin.ts +26 -0
  20. package/src/engine-factory.ts +176 -0
  21. package/src/engine-types.ts +56 -0
  22. package/src/helpers/byte-encoding.ts +39 -0
  23. package/src/helpers/digest.ts +9 -0
  24. package/src/helpers/event-loop.ts +96 -0
  25. package/src/helpers/wasm-cache.ts +133 -0
  26. package/src/iframe-sandbox.ts +141 -0
  27. package/src/index.ts +192 -0
  28. package/src/isolation-helpers.ts +148 -0
  29. package/src/memory-volume.ts +941 -0
  30. package/src/module-transformer.ts +368 -0
  31. package/src/packages/archive-extractor.ts +248 -0
  32. package/src/packages/browser-bundler.ts +284 -0
  33. package/src/packages/installer.ts +396 -0
  34. package/src/packages/registry-client.ts +131 -0
  35. package/src/packages/version-resolver.ts +411 -0
  36. package/src/polyfills/assert.ts +384 -0
  37. package/src/polyfills/async_hooks.ts +144 -0
  38. package/src/polyfills/buffer.ts +628 -0
  39. package/src/polyfills/child_process.ts +2288 -0
  40. package/src/polyfills/chokidar.ts +336 -0
  41. package/src/polyfills/cluster.ts +106 -0
  42. package/src/polyfills/console.ts +136 -0
  43. package/src/polyfills/constants.ts +123 -0
  44. package/src/polyfills/crypto.ts +885 -0
  45. package/src/polyfills/dgram.ts +87 -0
  46. package/src/polyfills/diagnostics_channel.ts +76 -0
  47. package/src/polyfills/dns.ts +134 -0
  48. package/src/polyfills/domain.ts +68 -0
  49. package/src/polyfills/esbuild.ts +854 -0
  50. package/src/polyfills/events.ts +276 -0
  51. package/src/polyfills/fs.ts +2888 -0
  52. package/src/polyfills/fsevents.ts +79 -0
  53. package/src/polyfills/http.ts +1449 -0
  54. package/src/polyfills/http2.ts +199 -0
  55. package/src/polyfills/https.ts +76 -0
  56. package/src/polyfills/inspector.ts +62 -0
  57. package/src/polyfills/lightningcss.ts +105 -0
  58. package/src/polyfills/module.ts +191 -0
  59. package/src/polyfills/net.ts +353 -0
  60. package/src/polyfills/os.ts +238 -0
  61. package/src/polyfills/path.ts +206 -0
  62. package/src/polyfills/perf_hooks.ts +102 -0
  63. package/src/polyfills/process.ts +690 -0
  64. package/src/polyfills/punycode.ts +159 -0
  65. package/src/polyfills/querystring.ts +93 -0
  66. package/src/polyfills/quic.ts +118 -0
  67. package/src/polyfills/readdirp.ts +229 -0
  68. package/src/polyfills/readline.ts +692 -0
  69. package/src/polyfills/repl.ts +134 -0
  70. package/src/polyfills/rollup.ts +119 -0
  71. package/src/polyfills/sea.ts +33 -0
  72. package/src/polyfills/sqlite.ts +78 -0
  73. package/src/polyfills/stream.ts +1620 -0
  74. package/src/polyfills/string_decoder.ts +25 -0
  75. package/src/polyfills/tailwindcss-oxide.ts +309 -0
  76. package/src/polyfills/test.ts +197 -0
  77. package/src/polyfills/timers.ts +32 -0
  78. package/src/polyfills/tls.ts +105 -0
  79. package/src/polyfills/trace_events.ts +50 -0
  80. package/src/polyfills/tty.ts +71 -0
  81. package/src/polyfills/url.ts +174 -0
  82. package/src/polyfills/util.ts +559 -0
  83. package/src/polyfills/v8.ts +126 -0
  84. package/src/polyfills/vm.ts +132 -0
  85. package/src/polyfills/volume-registry.ts +15 -0
  86. package/src/polyfills/wasi.ts +44 -0
  87. package/src/polyfills/worker_threads.ts +326 -0
  88. package/src/polyfills/ws.ts +595 -0
  89. package/src/polyfills/zlib.ts +881 -0
  90. package/src/request-proxy.ts +716 -0
  91. package/src/script-engine.ts +3375 -0
  92. package/src/sdk/nodepod-fs.ts +93 -0
  93. package/src/sdk/nodepod-process.ts +86 -0
  94. package/src/sdk/nodepod-terminal.ts +350 -0
  95. package/src/sdk/nodepod.ts +509 -0
  96. package/src/sdk/types.ts +70 -0
  97. package/src/shell/commands/bun.ts +121 -0
  98. package/src/shell/commands/directory.ts +297 -0
  99. package/src/shell/commands/file-ops.ts +525 -0
  100. package/src/shell/commands/git.ts +2142 -0
  101. package/src/shell/commands/node.ts +80 -0
  102. package/src/shell/commands/npm.ts +198 -0
  103. package/src/shell/commands/pm-types.ts +45 -0
  104. package/src/shell/commands/pnpm.ts +82 -0
  105. package/src/shell/commands/search.ts +264 -0
  106. package/src/shell/commands/shell-env.ts +352 -0
  107. package/src/shell/commands/text-processing.ts +1152 -0
  108. package/src/shell/commands/yarn.ts +84 -0
  109. package/src/shell/shell-builtins.ts +19 -0
  110. package/src/shell/shell-helpers.ts +250 -0
  111. package/src/shell/shell-interpreter.ts +514 -0
  112. package/src/shell/shell-parser.ts +429 -0
  113. package/src/shell/shell-types.ts +85 -0
  114. package/src/syntax-transforms.ts +561 -0
  115. package/src/threading/engine-worker.ts +64 -0
  116. package/src/threading/inline-worker.ts +372 -0
  117. package/src/threading/offload-types.ts +112 -0
  118. package/src/threading/offload-worker.ts +383 -0
  119. package/src/threading/offload.ts +271 -0
  120. package/src/threading/process-context.ts +92 -0
  121. package/src/threading/process-handle.ts +275 -0
  122. package/src/threading/process-manager.ts +956 -0
  123. package/src/threading/process-worker-entry.ts +854 -0
  124. package/src/threading/shared-vfs.ts +352 -0
  125. package/src/threading/sync-channel.ts +135 -0
  126. package/src/threading/task-queue.ts +177 -0
  127. package/src/threading/vfs-bridge.ts +231 -0
  128. package/src/threading/worker-pool.ts +233 -0
  129. package/src/threading/worker-protocol.ts +358 -0
  130. package/src/threading/worker-vfs.ts +218 -0
  131. package/src/types/externals.d.ts +38 -0
  132. package/src/types/fs-streams.ts +142 -0
  133. package/src/types/manifest.ts +17 -0
  134. package/src/worker-sandbox.ts +90 -0
@@ -0,0 +1,881 @@
1
+ // Zlib polyfill using pako for deflate/inflate/gzip + optional brotli-wasm
2
+
3
+ import { Buffer } from "./buffer";
4
+ import { Transform } from "./stream";
5
+ import { CDN_BROTLI_WASM, cdnImport } from "../constants/cdn-urls";
6
+ import pako from "pako";
7
+
8
+
9
+ type BrotliEngine = {
10
+ compress: (input: Uint8Array) => Uint8Array;
11
+ decompress: (input: Uint8Array) => Uint8Array;
12
+ };
13
+
14
+ let brotliInstance: BrotliEngine | null = null;
15
+ let brotliLoading: Promise<BrotliEngine | null> | null = null;
16
+
17
+ async function ensureBrotli(): Promise<BrotliEngine | null> {
18
+ if (brotliInstance) return brotliInstance;
19
+ if (!brotliLoading) {
20
+ brotliLoading = (async () => {
21
+ try {
22
+ const mod = await cdnImport(CDN_BROTLI_WASM);
23
+ brotliInstance = await mod.default;
24
+ return brotliInstance;
25
+ } catch {
26
+ return null;
27
+ }
28
+ })();
29
+ }
30
+ return brotliLoading;
31
+ }
32
+
33
+
34
+ type CompressCallback = (err: Error | null, result: Buffer) => void;
35
+
36
+ export function gzip(input: Buffer | string, cb: CompressCallback): void {
37
+ try {
38
+ const data = typeof input === "string" ? Buffer.from(input) : input;
39
+ const compressed = pako.gzip(data);
40
+ cb(null, Buffer.from(compressed));
41
+ } catch (e) {
42
+ cb(e as Error, Buffer.alloc(0));
43
+ }
44
+ }
45
+
46
+ export function gunzip(input: Buffer, cb: CompressCallback): void {
47
+ try {
48
+ const decompressed = pako.ungzip(input);
49
+ cb(null, Buffer.from(decompressed));
50
+ } catch (e) {
51
+ cb(e as Error, Buffer.alloc(0));
52
+ }
53
+ }
54
+
55
+ export function deflate(input: Buffer | string, cb: CompressCallback): void {
56
+ try {
57
+ const data = typeof input === "string" ? Buffer.from(input) : input;
58
+ const compressed = pako.deflate(data);
59
+ cb(null, Buffer.from(compressed));
60
+ } catch (e) {
61
+ cb(e as Error, Buffer.alloc(0));
62
+ }
63
+ }
64
+
65
+ export function inflate(input: Buffer, cb: CompressCallback): void {
66
+ try {
67
+ const decompressed = pako.inflate(input);
68
+ cb(null, Buffer.from(decompressed));
69
+ } catch (e) {
70
+ cb(e as Error, Buffer.alloc(0));
71
+ }
72
+ }
73
+
74
+ export function deflateRaw(input: Buffer | string, cb: CompressCallback): void {
75
+ try {
76
+ const data = typeof input === "string" ? Buffer.from(input) : input;
77
+ const compressed = pako.deflateRaw(data);
78
+ cb(null, Buffer.from(compressed));
79
+ } catch (e) {
80
+ cb(e as Error, Buffer.alloc(0));
81
+ }
82
+ }
83
+
84
+ export function inflateRaw(input: Buffer, cb: CompressCallback): void {
85
+ try {
86
+ const decompressed = pako.inflateRaw(input);
87
+ cb(null, Buffer.from(decompressed));
88
+ } catch (e) {
89
+ cb(e as Error, Buffer.alloc(0));
90
+ }
91
+ }
92
+
93
+
94
+ export function brotliCompress(
95
+ input: Buffer | string,
96
+ optsOrCb: unknown,
97
+ cb?: CompressCallback,
98
+ ): void {
99
+ const callback =
100
+ typeof optsOrCb === "function" ? (optsOrCb as CompressCallback) : cb!;
101
+ ensureBrotli()
102
+ .then((engine) => {
103
+ if (!engine) {
104
+ callback(new Error("Brotli WASM is not available"), Buffer.alloc(0));
105
+ return;
106
+ }
107
+ try {
108
+ const data = typeof input === "string" ? Buffer.from(input) : input;
109
+ const out = engine.compress(new Uint8Array(data));
110
+ callback(null, Buffer.from(out));
111
+ } catch (e) {
112
+ callback(e as Error, Buffer.alloc(0));
113
+ }
114
+ })
115
+ .catch((e) => callback(e as Error, Buffer.alloc(0)));
116
+ }
117
+
118
+ export function brotliDecompress(
119
+ input: Buffer,
120
+ optsOrCb: unknown,
121
+ cb?: CompressCallback,
122
+ ): void {
123
+ const callback =
124
+ typeof optsOrCb === "function" ? (optsOrCb as CompressCallback) : cb!;
125
+ ensureBrotli()
126
+ .then((engine) => {
127
+ if (!engine) {
128
+ callback(new Error("Brotli WASM is not available"), Buffer.alloc(0));
129
+ return;
130
+ }
131
+ try {
132
+ const out = engine.decompress(new Uint8Array(input));
133
+ callback(null, Buffer.from(out));
134
+ } catch (e) {
135
+ callback(e as Error, Buffer.alloc(0));
136
+ }
137
+ })
138
+ .catch((e) => callback(e as Error, Buffer.alloc(0)));
139
+ }
140
+
141
+
142
+ export function gzipSync(input: Buffer | string): Buffer {
143
+ const data = typeof input === "string" ? Buffer.from(input) : input;
144
+ return Buffer.from(pako.gzip(data));
145
+ }
146
+
147
+ export function gunzipSync(input: Buffer): Buffer {
148
+ return Buffer.from(pako.ungzip(input));
149
+ }
150
+
151
+ export function deflateSync(input: Buffer | string): Buffer {
152
+ const data = typeof input === "string" ? Buffer.from(input) : input;
153
+ return Buffer.from(pako.deflate(data));
154
+ }
155
+
156
+ export function inflateSync(input: Buffer): Buffer {
157
+ return Buffer.from(pako.inflate(input));
158
+ }
159
+
160
+ export function deflateRawSync(input: Buffer | string): Buffer {
161
+ const data = typeof input === "string" ? Buffer.from(input) : input;
162
+ return Buffer.from(pako.deflateRaw(data));
163
+ }
164
+
165
+ export function inflateRawSync(input: Buffer): Buffer {
166
+ return Buffer.from(pako.inflateRaw(input));
167
+ }
168
+
169
+ export function brotliCompressSync(
170
+ input: Buffer | string,
171
+ _opts?: unknown,
172
+ ): Buffer {
173
+ if (!brotliInstance) {
174
+ throw new Error(
175
+ "Brotli WASM not loaded yet. Use async brotliCompress first.",
176
+ );
177
+ }
178
+ const data = typeof input === "string" ? Buffer.from(input) : input;
179
+ return Buffer.from(brotliInstance.compress(new Uint8Array(data)));
180
+ }
181
+
182
+ export function brotliDecompressSync(input: Buffer, _opts?: unknown): Buffer {
183
+ if (!brotliInstance) {
184
+ throw new Error(
185
+ "Brotli WASM not loaded yet. Use async brotliDecompress first.",
186
+ );
187
+ }
188
+ return Buffer.from(brotliInstance.decompress(new Uint8Array(input)));
189
+ }
190
+
191
+ // pako v2 may call onEnd synchronously during push(), moving chunks->result
192
+ function drainEngine(engine: { chunks: Uint8Array[]; result?: Uint8Array | null }): Buffer | null {
193
+ if (engine.chunks && engine.chunks.length > 0) {
194
+ const chunks = engine.chunks.splice(0);
195
+ if (chunks.length === 1) return Buffer.from(chunks[0]);
196
+ let total = 0;
197
+ for (const c of chunks) total += c.length;
198
+ const merged = new Uint8Array(total);
199
+ let off = 0;
200
+ for (const c of chunks) {
201
+ merged.set(c, off);
202
+ off += c.length;
203
+ }
204
+ return Buffer.from(merged);
205
+ }
206
+ if (engine.result && engine.result.length > 0) {
207
+ const buf = Buffer.from(engine.result);
208
+ engine.result = null;
209
+ return buf;
210
+ }
211
+ return null;
212
+ }
213
+
214
+ /* ------------------------------------------------------------------ */
215
+ /* ZlibTransform (base) */
216
+ /* ------------------------------------------------------------------ */
217
+
218
+ export interface ZlibTransform extends Transform {
219
+ _opts: any;
220
+ bytesWritten: number;
221
+ _handle: { close: () => void };
222
+ close(cb?: () => void): void;
223
+ flush(cb?: (err: Error | null) => void): void;
224
+ reset(): void;
225
+ params(_level: number, _strategy: number, cb?: (err: Error | null) => void): void;
226
+ _processChunk(chunk: Buffer | Uint8Array, flushFlag: number): Buffer | null;
227
+ }
228
+
229
+ interface ZlibTransformConstructor {
230
+ new (opts?: any): ZlibTransform;
231
+ (this: any, opts?: any): void;
232
+ prototype: any;
233
+ }
234
+
235
+ const ZlibTransform = function ZlibTransform(this: any, opts?: any) {
236
+ if (!this) return;
237
+ Transform.call(this);
238
+ this._opts = opts || {};
239
+ this.bytesWritten = 0;
240
+ this._handle = { close: () => {} };
241
+ } as unknown as ZlibTransformConstructor;
242
+
243
+ Object.setPrototypeOf(ZlibTransform.prototype, Transform.prototype);
244
+
245
+ ZlibTransform.prototype.close = function close(cb?: () => void): void {
246
+ this.destroy();
247
+ if (cb) queueMicrotask(cb);
248
+ };
249
+
250
+ ZlibTransform.prototype.flush = function flush(cb?: (err: Error | null) => void): void {
251
+ if (cb) queueMicrotask(() => cb(null));
252
+ };
253
+
254
+ ZlibTransform.prototype.reset = function reset(): void {
255
+ };
256
+
257
+ ZlibTransform.prototype.params = function params(
258
+ _level: number,
259
+ _strategy: number,
260
+ cb?: (err: Error | null) => void,
261
+ ): void {
262
+ if (cb) queueMicrotask(() => cb(null));
263
+ };
264
+
265
+ // used by next/dist/compiled/tar's ZlibBase.write() for sync decompression
266
+ ZlibTransform.prototype._processChunk = function _processChunk(
267
+ chunk: Buffer | Uint8Array,
268
+ flushFlag: number,
269
+ ): Buffer | null {
270
+ let result: Buffer | null = null;
271
+ this._transform(
272
+ chunk,
273
+ "buffer",
274
+ ((err?: Error | null, data?: any) => {
275
+ if (err) throw err;
276
+ if (data) result = Buffer.from(data);
277
+ }) as any,
278
+ );
279
+ // If Z_FINISH (4), also flush
280
+ if (flushFlag === 4) {
281
+ this._flush(((err?: Error | null, data?: any) => {
282
+ if (err) throw err;
283
+ if (data) {
284
+ result = result
285
+ ? Buffer.concat([result, Buffer.from(data)])
286
+ : Buffer.from(data);
287
+ }
288
+ }) as any);
289
+ }
290
+ return result;
291
+ };
292
+
293
+ /* ------------------------------------------------------------------ */
294
+ /* Gzip */
295
+ /* ------------------------------------------------------------------ */
296
+
297
+ export interface Gzip extends ZlibTransform {
298
+ _engine: pako.Deflate;
299
+ }
300
+
301
+ interface GzipConstructor {
302
+ new (opts?: any): Gzip;
303
+ (this: any, opts?: any): void;
304
+ prototype: any;
305
+ }
306
+
307
+ export const Gzip = function Gzip(this: any, opts?: any) {
308
+ if (!this) return;
309
+ ZlibTransform.call(this, opts);
310
+ this._engine = new pako.Deflate({ ...(opts || {}), gzip: true });
311
+ } as unknown as GzipConstructor;
312
+
313
+ Object.setPrototypeOf(Gzip.prototype, ZlibTransform.prototype);
314
+
315
+ Gzip.prototype._transform = function _transform(
316
+ chunk: any,
317
+ _enc: string,
318
+ done: (err: Error | null, data?: any) => void,
319
+ ): void {
320
+ try {
321
+ this._engine.push(chunk, false);
322
+ if (this._engine.err) {
323
+ done(new Error(this._engine.msg || "Gzip error"));
324
+ return;
325
+ }
326
+ const out = drainEngine(this._engine as any);
327
+ done(null, out ?? undefined);
328
+ } catch (e) {
329
+ done(e as Error);
330
+ }
331
+ };
332
+
333
+ Gzip.prototype._flush = function _flush(
334
+ done: (err: Error | null, data?: any) => void,
335
+ ): void {
336
+ try {
337
+ this._engine.push(new Uint8Array(0), true);
338
+ const out = drainEngine(this._engine as any);
339
+ done(null, out ?? undefined);
340
+ } catch (e) {
341
+ done(e as Error);
342
+ }
343
+ };
344
+
345
+ /* ------------------------------------------------------------------ */
346
+ /* Gunzip */
347
+ /* ------------------------------------------------------------------ */
348
+
349
+ export interface Gunzip extends ZlibTransform {
350
+ _engine: pako.Inflate;
351
+ }
352
+
353
+ interface GunzipConstructor {
354
+ new (opts?: any): Gunzip;
355
+ (this: any, opts?: any): void;
356
+ prototype: any;
357
+ }
358
+
359
+ export const Gunzip = function Gunzip(this: any, opts?: any) {
360
+ if (!this) return;
361
+ ZlibTransform.call(this, opts);
362
+ this._engine = new pako.Inflate({ ...(opts || {}) });
363
+ } as unknown as GunzipConstructor;
364
+
365
+ Object.setPrototypeOf(Gunzip.prototype, ZlibTransform.prototype);
366
+
367
+ Gunzip.prototype._transform = function _transform(
368
+ chunk: any,
369
+ _enc: string,
370
+ done: (err: Error | null, data?: any) => void,
371
+ ): void {
372
+ try {
373
+ this._engine.push(chunk, false);
374
+ if (this._engine.err) {
375
+ done(new Error(this._engine.msg || "Gunzip error"));
376
+ return;
377
+ }
378
+ const out = drainEngine(this._engine as any);
379
+ done(null, out ?? undefined);
380
+ } catch (e) {
381
+ done(e as Error);
382
+ }
383
+ };
384
+
385
+ Gunzip.prototype._flush = function _flush(
386
+ done: (err: Error | null, data?: any) => void,
387
+ ): void {
388
+ try {
389
+ this._engine.push(new Uint8Array(0), true);
390
+ const out = drainEngine(this._engine as any);
391
+ done(null, out ?? undefined);
392
+ } catch (e) {
393
+ done(e as Error);
394
+ }
395
+ };
396
+
397
+ /* ------------------------------------------------------------------ */
398
+ /* Deflate */
399
+ /* ------------------------------------------------------------------ */
400
+
401
+ export interface Deflate extends ZlibTransform {
402
+ _engine: pako.Deflate;
403
+ }
404
+
405
+ interface DeflateConstructor {
406
+ new (opts?: any): Deflate;
407
+ (this: any, opts?: any): void;
408
+ prototype: any;
409
+ }
410
+
411
+ export const Deflate = function Deflate(this: any, opts?: any) {
412
+ if (!this) return;
413
+ ZlibTransform.call(this, opts);
414
+ this._engine = new pako.Deflate({ ...(opts || {}) });
415
+ } as unknown as DeflateConstructor;
416
+
417
+ Object.setPrototypeOf(Deflate.prototype, ZlibTransform.prototype);
418
+
419
+ Deflate.prototype._transform = function _transform(
420
+ chunk: any,
421
+ _enc: string,
422
+ done: (err: Error | null, data?: any) => void,
423
+ ): void {
424
+ try {
425
+ this._engine.push(chunk, false);
426
+ if (this._engine.err) {
427
+ done(new Error(this._engine.msg || "Deflate error"));
428
+ return;
429
+ }
430
+ const out = drainEngine(this._engine as any);
431
+ done(null, out ?? undefined);
432
+ } catch (e) {
433
+ done(e as Error);
434
+ }
435
+ };
436
+
437
+ Deflate.prototype._flush = function _flush(
438
+ done: (err: Error | null, data?: any) => void,
439
+ ): void {
440
+ try {
441
+ this._engine.push(new Uint8Array(0), true);
442
+ const out = drainEngine(this._engine as any);
443
+ done(null, out ?? undefined);
444
+ } catch (e) {
445
+ done(e as Error);
446
+ }
447
+ };
448
+
449
+ /* ------------------------------------------------------------------ */
450
+ /* Inflate */
451
+ /* ------------------------------------------------------------------ */
452
+
453
+ export interface Inflate extends ZlibTransform {
454
+ _engine: pako.Inflate;
455
+ }
456
+
457
+ interface InflateConstructor {
458
+ new (opts?: any): Inflate;
459
+ (this: any, opts?: any): void;
460
+ prototype: any;
461
+ }
462
+
463
+ export const Inflate = function Inflate(this: any, opts?: any) {
464
+ if (!this) return;
465
+ ZlibTransform.call(this, opts);
466
+ this._engine = new pako.Inflate({ ...(opts || {}) });
467
+ } as unknown as InflateConstructor;
468
+
469
+ Object.setPrototypeOf(Inflate.prototype, ZlibTransform.prototype);
470
+
471
+ Inflate.prototype._transform = function _transform(
472
+ chunk: any,
473
+ _enc: string,
474
+ done: (err: Error | null, data?: any) => void,
475
+ ): void {
476
+ try {
477
+ this._engine.push(chunk, false);
478
+ if (this._engine.err) {
479
+ done(new Error(this._engine.msg || "Inflate error"));
480
+ return;
481
+ }
482
+ const out = drainEngine(this._engine as any);
483
+ done(null, out ?? undefined);
484
+ } catch (e) {
485
+ done(e as Error);
486
+ }
487
+ };
488
+
489
+ Inflate.prototype._flush = function _flush(
490
+ done: (err: Error | null, data?: any) => void,
491
+ ): void {
492
+ try {
493
+ this._engine.push(new Uint8Array(0), true);
494
+ const out = drainEngine(this._engine as any);
495
+ done(null, out ?? undefined);
496
+ } catch (e) {
497
+ done(e as Error);
498
+ }
499
+ };
500
+
501
+ /* ------------------------------------------------------------------ */
502
+ /* DeflateRaw */
503
+ /* ------------------------------------------------------------------ */
504
+
505
+ export interface DeflateRaw extends ZlibTransform {
506
+ _engine: pako.Deflate;
507
+ }
508
+
509
+ interface DeflateRawConstructor {
510
+ new (opts?: any): DeflateRaw;
511
+ (this: any, opts?: any): void;
512
+ prototype: any;
513
+ }
514
+
515
+ export const DeflateRaw = function DeflateRaw(this: any, opts?: any) {
516
+ if (!this) return;
517
+ ZlibTransform.call(this, opts);
518
+ this._engine = new pako.Deflate({ ...(opts || {}), raw: true });
519
+ } as unknown as DeflateRawConstructor;
520
+
521
+ Object.setPrototypeOf(DeflateRaw.prototype, ZlibTransform.prototype);
522
+
523
+ DeflateRaw.prototype._transform = function _transform(
524
+ chunk: any,
525
+ _enc: string,
526
+ done: (err: Error | null, data?: any) => void,
527
+ ): void {
528
+ try {
529
+ this._engine.push(chunk, false);
530
+ if (this._engine.err) {
531
+ done(new Error(this._engine.msg || "DeflateRaw error"));
532
+ return;
533
+ }
534
+ const out = drainEngine(this._engine as any);
535
+ done(null, out ?? undefined);
536
+ } catch (e) {
537
+ done(e as Error);
538
+ }
539
+ };
540
+
541
+ DeflateRaw.prototype._flush = function _flush(
542
+ done: (err: Error | null, data?: any) => void,
543
+ ): void {
544
+ try {
545
+ this._engine.push(new Uint8Array(0), true);
546
+ const out = drainEngine(this._engine as any);
547
+ done(null, out ?? undefined);
548
+ } catch (e) {
549
+ done(e as Error);
550
+ }
551
+ };
552
+
553
+ /* ------------------------------------------------------------------ */
554
+ /* InflateRaw */
555
+ /* ------------------------------------------------------------------ */
556
+
557
+ export interface InflateRaw extends ZlibTransform {
558
+ _engine: pako.Inflate;
559
+ }
560
+
561
+ interface InflateRawConstructor {
562
+ new (opts?: any): InflateRaw;
563
+ (this: any, opts?: any): void;
564
+ prototype: any;
565
+ }
566
+
567
+ export const InflateRaw = function InflateRaw(this: any, opts?: any) {
568
+ if (!this) return;
569
+ ZlibTransform.call(this, opts);
570
+ this._engine = new pako.Inflate({ ...(opts || {}), raw: true });
571
+ } as unknown as InflateRawConstructor;
572
+
573
+ Object.setPrototypeOf(InflateRaw.prototype, ZlibTransform.prototype);
574
+
575
+ InflateRaw.prototype._transform = function _transform(
576
+ chunk: any,
577
+ _enc: string,
578
+ done: (err: Error | null, data?: any) => void,
579
+ ): void {
580
+ try {
581
+ this._engine.push(chunk, false);
582
+ if (this._engine.err) {
583
+ done(new Error(this._engine.msg || "InflateRaw error"));
584
+ return;
585
+ }
586
+ const out = drainEngine(this._engine as any);
587
+ done(null, out ?? undefined);
588
+ } catch (e) {
589
+ done(e as Error);
590
+ }
591
+ };
592
+
593
+ InflateRaw.prototype._flush = function _flush(
594
+ done: (err: Error | null, data?: any) => void,
595
+ ): void {
596
+ try {
597
+ this._engine.push(new Uint8Array(0), true);
598
+ const out = drainEngine(this._engine as any);
599
+ done(null, out ?? undefined);
600
+ } catch (e) {
601
+ done(e as Error);
602
+ }
603
+ };
604
+
605
+ /* ------------------------------------------------------------------ */
606
+ /* Unzip */
607
+ /* ------------------------------------------------------------------ */
608
+
609
+ export interface Unzip extends ZlibTransform {
610
+ _engine: pako.Inflate | null;
611
+ }
612
+
613
+ interface UnzipConstructor {
614
+ new (opts?: any): Unzip;
615
+ (this: any, opts?: any): void;
616
+ prototype: any;
617
+ }
618
+
619
+ export const Unzip = function Unzip(this: any, opts?: any) {
620
+ if (!this) return;
621
+ ZlibTransform.call(this, opts);
622
+ this._engine = null;
623
+ } as unknown as UnzipConstructor;
624
+
625
+ Object.setPrototypeOf(Unzip.prototype, ZlibTransform.prototype);
626
+
627
+ Unzip.prototype._transform = function _transform(
628
+ chunk: any,
629
+ _enc: string,
630
+ done: (err: Error | null, data?: any) => void,
631
+ ): void {
632
+ try {
633
+ if (!this._engine) {
634
+ const buf = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
635
+ // gzip magic: 1f 8b -> windowBits 15+16, otherwise auto-detect with 15+32
636
+ const isGzip = buf.length >= 2 && buf[0] === 0x1f && buf[1] === 0x8b;
637
+ this._engine = new pako.Inflate({
638
+ windowBits: isGzip ? 15 + 16 : 15 + 32,
639
+ });
640
+ }
641
+ this._engine.push(chunk, false);
642
+ if (this._engine.err) {
643
+ done(new Error(this._engine.msg || "Unzip error"));
644
+ return;
645
+ }
646
+ const out = drainEngine(this._engine as any);
647
+ done(null, out ?? undefined);
648
+ } catch (e) {
649
+ done(e as Error);
650
+ }
651
+ };
652
+
653
+ Unzip.prototype._flush = function _flush(
654
+ done: (err: Error | null, data?: any) => void,
655
+ ): void {
656
+ try {
657
+ if (this._engine) {
658
+ this._engine.push(new Uint8Array(0), true);
659
+ const out = drainEngine(this._engine as any);
660
+ done(null, out ?? undefined);
661
+ } else {
662
+ done(null);
663
+ }
664
+ } catch (e) {
665
+ done(e as Error);
666
+ }
667
+ };
668
+
669
+ /* ------------------------------------------------------------------ */
670
+ /* BrotliCompressStream */
671
+ /* ------------------------------------------------------------------ */
672
+
673
+ export interface BrotliCompressStream extends ZlibTransform {}
674
+
675
+ interface BrotliCompressStreamConstructor {
676
+ new (opts?: any): BrotliCompressStream;
677
+ (this: any, opts?: any): void;
678
+ prototype: any;
679
+ }
680
+
681
+ export const BrotliCompressStream = function BrotliCompressStream(this: any, opts?: any) {
682
+ if (!this) return;
683
+ ZlibTransform.call(this, opts);
684
+ } as unknown as BrotliCompressStreamConstructor;
685
+
686
+ Object.setPrototypeOf(BrotliCompressStream.prototype, ZlibTransform.prototype);
687
+
688
+ BrotliCompressStream.prototype._transform = function _transform(
689
+ chunk: any,
690
+ _enc: string,
691
+ done: (err: Error | null, data?: any) => void,
692
+ ): void {
693
+ if (!brotliInstance) {
694
+ done(new Error("Brotli WASM not loaded"));
695
+ return;
696
+ }
697
+ try {
698
+ const data = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
699
+ done(null, Buffer.from(brotliInstance.compress(data)));
700
+ } catch (e) {
701
+ done(e as Error);
702
+ }
703
+ };
704
+
705
+ /* ------------------------------------------------------------------ */
706
+ /* BrotliDecompressStream */
707
+ /* ------------------------------------------------------------------ */
708
+
709
+ export interface BrotliDecompressStream extends ZlibTransform {}
710
+
711
+ interface BrotliDecompressStreamConstructor {
712
+ new (opts?: any): BrotliDecompressStream;
713
+ (this: any, opts?: any): void;
714
+ prototype: any;
715
+ }
716
+
717
+ export const BrotliDecompressStream = function BrotliDecompressStream(this: any, opts?: any) {
718
+ if (!this) return;
719
+ ZlibTransform.call(this, opts);
720
+ } as unknown as BrotliDecompressStreamConstructor;
721
+
722
+ Object.setPrototypeOf(BrotliDecompressStream.prototype, ZlibTransform.prototype);
723
+
724
+ BrotliDecompressStream.prototype._transform = function _transform(
725
+ chunk: any,
726
+ _enc: string,
727
+ done: (err: Error | null, data?: any) => void,
728
+ ): void {
729
+ if (!brotliInstance) {
730
+ done(new Error("Brotli WASM not loaded"));
731
+ return;
732
+ }
733
+ try {
734
+ const data = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
735
+ done(null, Buffer.from(brotliInstance.decompress(data)));
736
+ } catch (e) {
737
+ done(e as Error);
738
+ }
739
+ };
740
+
741
+ export const BrotliCompress = BrotliCompressStream;
742
+ export const BrotliDecompress = BrotliDecompressStream;
743
+
744
+
745
+ export function createGzip(opts?: any): Gzip {
746
+ return new Gzip(opts);
747
+ }
748
+
749
+ export function createGunzip(opts?: any): Gunzip {
750
+ return new Gunzip(opts);
751
+ }
752
+
753
+ export function createDeflate(opts?: any): Deflate {
754
+ return new Deflate(opts);
755
+ }
756
+
757
+ export function createInflate(opts?: any): Inflate {
758
+ return new Inflate(opts);
759
+ }
760
+
761
+ export function createDeflateRaw(opts?: any): DeflateRaw {
762
+ return new DeflateRaw(opts);
763
+ }
764
+
765
+ export function createInflateRaw(opts?: any): InflateRaw {
766
+ return new InflateRaw(opts);
767
+ }
768
+
769
+ export function createUnzip(opts?: any): Unzip {
770
+ return new Unzip(opts);
771
+ }
772
+
773
+ export function createBrotliCompress(opts?: any): BrotliCompressStream {
774
+ return new BrotliCompressStream(opts);
775
+ }
776
+
777
+ export function createBrotliDecompress(opts?: any): BrotliDecompressStream {
778
+ return new BrotliDecompressStream(opts);
779
+ }
780
+
781
+
782
+ export const constants = {
783
+ Z_NO_FLUSH: 0,
784
+ Z_PARTIAL_FLUSH: 1,
785
+ Z_SYNC_FLUSH: 2,
786
+ Z_FULL_FLUSH: 3,
787
+ Z_FINISH: 4,
788
+ Z_BLOCK: 5,
789
+ Z_OK: 0,
790
+ Z_STREAM_END: 1,
791
+ Z_NEED_DICT: 2,
792
+ Z_ERRNO: -1,
793
+ Z_STREAM_ERROR: -2,
794
+ Z_DATA_ERROR: -3,
795
+ Z_MEM_ERROR: -4,
796
+ Z_BUF_ERROR: -5,
797
+ Z_VERSION_ERROR: -6,
798
+ Z_NO_COMPRESSION: 0,
799
+ Z_BEST_SPEED: 1,
800
+ Z_BEST_COMPRESSION: 9,
801
+ Z_DEFAULT_COMPRESSION: -1,
802
+ Z_FILTERED: 1,
803
+ Z_HUFFMAN_ONLY: 2,
804
+ Z_RLE: 3,
805
+ Z_FIXED: 4,
806
+ Z_DEFAULT_STRATEGY: 0,
807
+ ZLIB_VERNUM: 4784,
808
+ Z_MIN_WINDOWBITS: 8,
809
+ Z_MAX_WINDOWBITS: 15,
810
+ Z_DEFAULT_WINDOWBITS: 15,
811
+ Z_MIN_CHUNK: 64,
812
+ Z_MAX_CHUNK: Infinity,
813
+ Z_DEFAULT_CHUNK: 16384,
814
+ Z_MIN_MEMLEVEL: 1,
815
+ Z_MAX_MEMLEVEL: 9,
816
+ Z_DEFAULT_MEMLEVEL: 8,
817
+ Z_MIN_LEVEL: -1,
818
+ Z_MAX_LEVEL: 9,
819
+ Z_DEFAULT_LEVEL: -1,
820
+ BROTLI_DECODE: 0,
821
+ BROTLI_ENCODE: 1,
822
+ BROTLI_OPERATION_PROCESS: 0,
823
+ BROTLI_OPERATION_FLUSH: 1,
824
+ BROTLI_OPERATION_FINISH: 2,
825
+ BROTLI_OPERATION_EMIT_METADATA: 3,
826
+ BROTLI_PARAM_MODE: 0,
827
+ BROTLI_MODE_GENERIC: 0,
828
+ BROTLI_MODE_TEXT: 1,
829
+ BROTLI_MODE_FONT: 2,
830
+ BROTLI_PARAM_QUALITY: 1,
831
+ BROTLI_MIN_QUALITY: 0,
832
+ BROTLI_MAX_QUALITY: 11,
833
+ BROTLI_DEFAULT_QUALITY: 11,
834
+ BROTLI_PARAM_LGWIN: 2,
835
+ BROTLI_MIN_WINDOW_BITS: 10,
836
+ BROTLI_MAX_WINDOW_BITS: 24,
837
+ BROTLI_DEFAULT_WINDOW: 22,
838
+ BROTLI_PARAM_LGBLOCK: 3,
839
+ BROTLI_MIN_INPUT_BLOCK_BITS: 16,
840
+ BROTLI_MAX_INPUT_BLOCK_BITS: 24,
841
+ };
842
+
843
+ export default {
844
+ gzip,
845
+ gunzip,
846
+ deflate,
847
+ inflate,
848
+ deflateRaw,
849
+ inflateRaw,
850
+ brotliCompress,
851
+ brotliDecompress,
852
+ gzipSync,
853
+ gunzipSync,
854
+ deflateSync,
855
+ inflateSync,
856
+ deflateRawSync,
857
+ inflateRawSync,
858
+ brotliCompressSync,
859
+ brotliDecompressSync,
860
+ Gzip,
861
+ Gunzip,
862
+ Deflate,
863
+ Inflate,
864
+ DeflateRaw,
865
+ InflateRaw,
866
+ Unzip,
867
+ BrotliCompress: BrotliCompressStream,
868
+ BrotliDecompress: BrotliDecompressStream,
869
+ // Stream creators
870
+ createGzip,
871
+ createGunzip,
872
+ createDeflate,
873
+ createInflate,
874
+ createDeflateRaw,
875
+ createInflateRaw,
876
+ createUnzip,
877
+ createBrotliCompress,
878
+ createBrotliDecompress,
879
+ // Constants
880
+ constants,
881
+ };