@gjsify/zlib 0.3.12 → 0.3.14

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/lib/esm/index.js CHANGED
@@ -1,342 +1,274 @@
1
- import {
2
- ZlibTransform,
3
- createGzip,
4
- createGunzip,
5
- createDeflate,
6
- createInflate,
7
- createDeflateRaw,
8
- createInflateRaw,
9
- createUnzip,
10
- createBrotliCompress,
11
- createBrotliDecompress
12
- } from "./transform-streams.js";
1
+ import { ZlibTransform, createBrotliCompress, createBrotliDecompress, createDeflate, createDeflateRaw, createGunzip, createGzip, createInflate, createInflateRaw, createUnzip } from "./transform-streams.js";
13
2
  import Gio from "@girs/gio-2.0";
14
3
  import GLib from "@girs/glib-2.0";
4
+
5
+ //#region src/index.ts
15
6
  const hasWebCompression = typeof globalThis.CompressionStream !== "undefined";
16
7
  function getGioFormat(format) {
17
- switch (format) {
18
- case "gzip":
19
- return Gio.ZlibCompressorFormat.GZIP;
20
- case "deflate":
21
- return Gio.ZlibCompressorFormat.ZLIB;
22
- case "deflate-raw":
23
- return Gio.ZlibCompressorFormat.RAW;
24
- }
8
+ switch (format) {
9
+ case "gzip": return Gio.ZlibCompressorFormat.GZIP;
10
+ case "deflate": return Gio.ZlibCompressorFormat.ZLIB;
11
+ case "deflate-raw": return Gio.ZlibCompressorFormat.RAW;
12
+ }
25
13
  }
26
14
  function compressWithGio(data, format) {
27
- const compressor = new Gio.ZlibCompressor({ format: getGioFormat(format) });
28
- const converter = new Gio.ConverterOutputStream({
29
- base_stream: Gio.MemoryOutputStream.new_resizable(),
30
- converter: compressor
31
- });
32
- converter.write_bytes(new GLib.Bytes(data), null);
33
- converter.close(null);
34
- const memStream = converter.get_base_stream();
35
- const bytes = memStream.steal_as_bytes();
36
- return new Uint8Array(bytes.get_data() ?? []);
15
+ const compressor = new Gio.ZlibCompressor({ format: getGioFormat(format) });
16
+ const converter = new Gio.ConverterOutputStream({
17
+ base_stream: Gio.MemoryOutputStream.new_resizable(),
18
+ converter: compressor
19
+ });
20
+ converter.write_bytes(new GLib.Bytes(data), null);
21
+ converter.close(null);
22
+ const memStream = converter.get_base_stream();
23
+ const bytes = memStream.steal_as_bytes();
24
+ return new Uint8Array(bytes.get_data() ?? []);
37
25
  }
38
26
  function decompressStreamWithGio(data, format) {
39
- const decompressor = new Gio.ZlibDecompressor({ format: getGioFormat(format) });
40
- const memInput = Gio.MemoryInputStream.new_from_bytes(new GLib.Bytes(data));
41
- const converter = new Gio.ConverterInputStream({
42
- base_stream: memInput,
43
- converter: decompressor
44
- });
45
- const chunks = [];
46
- const bufSize = 4096;
47
- while (true) {
48
- const bytes = converter.read_bytes(bufSize, null);
49
- const size = bytes.get_size();
50
- if (size === 0) break;
51
- chunks.push(new Uint8Array(bytes.get_data()));
52
- }
53
- converter.close(null);
54
- const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
55
- const result = new Uint8Array(totalLength);
56
- let offset = 0;
57
- for (const chunk of chunks) {
58
- result.set(chunk, offset);
59
- offset += chunk.length;
60
- }
61
- return result;
27
+ const decompressor = new Gio.ZlibDecompressor({ format: getGioFormat(format) });
28
+ const memInput = Gio.MemoryInputStream.new_from_bytes(new GLib.Bytes(data));
29
+ const converter = new Gio.ConverterInputStream({
30
+ base_stream: memInput,
31
+ converter: decompressor
32
+ });
33
+ const chunks = [];
34
+ const bufSize = 4096;
35
+ while (true) {
36
+ const bytes = converter.read_bytes(bufSize, null);
37
+ const size = bytes.get_size();
38
+ if (size === 0) break;
39
+ chunks.push(new Uint8Array(bytes.get_data()));
40
+ }
41
+ converter.close(null);
42
+ const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
43
+ const result = new Uint8Array(totalLength);
44
+ let offset = 0;
45
+ for (const chunk of chunks) {
46
+ result.set(chunk, offset);
47
+ offset += chunk.length;
48
+ }
49
+ return result;
62
50
  }
63
51
  function findGzipMemberEnd(data) {
64
- const decompressor = new Gio.ZlibDecompressor({ format: Gio.ZlibCompressorFormat.GZIP });
65
- const outBuf = new Uint8Array(65536);
66
- let totalRead = 0;
67
- let finished = false;
68
- while (!finished) {
69
- const input = data.subarray(totalRead);
70
- try {
71
- const [result, bytesRead] = decompressor.convert(input, outBuf, Gio.ConverterFlags.NONE);
72
- totalRead += bytesRead;
73
- if (result === Gio.ConverterResult.FINISHED) finished = true;
74
- } catch {
75
- finished = true;
76
- }
77
- }
78
- return totalRead;
52
+ const decompressor = new Gio.ZlibDecompressor({ format: Gio.ZlibCompressorFormat.GZIP });
53
+ const outBuf = new Uint8Array(65536);
54
+ let totalRead = 0;
55
+ let finished = false;
56
+ while (!finished) {
57
+ const input = data.subarray(totalRead);
58
+ try {
59
+ const [result, bytesRead] = decompressor.convert(input, outBuf, Gio.ConverterFlags.NONE);
60
+ totalRead += bytesRead;
61
+ if (result === Gio.ConverterResult.FINISHED) finished = true;
62
+ } catch {
63
+ finished = true;
64
+ }
65
+ }
66
+ return totalRead;
79
67
  }
80
68
  function decompressWithGio(data, format) {
81
- if (format !== "gzip") {
82
- return decompressStreamWithGio(data, format);
83
- }
84
- const allChunks = [];
85
- let inputOffset = 0;
86
- while (inputOffset < data.length) {
87
- if (data.length - inputOffset < 2 || data[inputOffset] !== 31 || data[inputOffset + 1] !== 139) {
88
- break;
89
- }
90
- const memberData = data.subarray(inputOffset);
91
- const consumed = findGzipMemberEnd(memberData);
92
- if (consumed <= 0) break;
93
- const decompressed = decompressStreamWithGio(memberData.subarray(0, consumed), "gzip");
94
- allChunks.push(decompressed);
95
- inputOffset += consumed;
96
- }
97
- if (allChunks.length === 0) {
98
- return decompressStreamWithGio(data, "gzip");
99
- }
100
- const totalLength = allChunks.reduce((acc, c) => acc + c.length, 0);
101
- const result = new Uint8Array(totalLength);
102
- let offset = 0;
103
- for (const chunk of allChunks) {
104
- result.set(chunk, offset);
105
- offset += chunk.length;
106
- }
107
- return result;
69
+ if (format !== "gzip") {
70
+ return decompressStreamWithGio(data, format);
71
+ }
72
+ const allChunks = [];
73
+ let inputOffset = 0;
74
+ while (inputOffset < data.length) {
75
+ if (data.length - inputOffset < 2 || data[inputOffset] !== 31 || data[inputOffset + 1] !== 139) {
76
+ break;
77
+ }
78
+ const memberData = data.subarray(inputOffset);
79
+ const consumed = findGzipMemberEnd(memberData);
80
+ if (consumed <= 0) break;
81
+ const decompressed = decompressStreamWithGio(memberData.subarray(0, consumed), "gzip");
82
+ allChunks.push(decompressed);
83
+ inputOffset += consumed;
84
+ }
85
+ if (allChunks.length === 0) {
86
+ return decompressStreamWithGio(data, "gzip");
87
+ }
88
+ const totalLength = allChunks.reduce((acc, c) => acc + c.length, 0);
89
+ const result = new Uint8Array(totalLength);
90
+ let offset = 0;
91
+ for (const chunk of allChunks) {
92
+ result.set(chunk, offset);
93
+ offset += chunk.length;
94
+ }
95
+ return result;
108
96
  }
109
97
  async function compressWithWeb(data, format) {
110
- const cs = new CompressionStream(format);
111
- const writer = cs.writable.getWriter();
112
- writer.write(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
113
- writer.close();
114
- const chunks = [];
115
- const reader = cs.readable.getReader();
116
- while (true) {
117
- const { done, value } = await reader.read();
118
- if (done) break;
119
- chunks.push(value);
120
- }
121
- const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
122
- const result = new Uint8Array(totalLength);
123
- let offset = 0;
124
- for (const chunk of chunks) {
125
- result.set(chunk, offset);
126
- offset += chunk.length;
127
- }
128
- return result;
98
+ const cs = new CompressionStream(format);
99
+ const writer = cs.writable.getWriter();
100
+ writer.write(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
101
+ writer.close();
102
+ const chunks = [];
103
+ const reader = cs.readable.getReader();
104
+ while (true) {
105
+ const { done, value } = await reader.read();
106
+ if (done) break;
107
+ chunks.push(value);
108
+ }
109
+ const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
110
+ const result = new Uint8Array(totalLength);
111
+ let offset = 0;
112
+ for (const chunk of chunks) {
113
+ result.set(chunk, offset);
114
+ offset += chunk.length;
115
+ }
116
+ return result;
129
117
  }
130
118
  async function decompressWithWeb(data, format) {
131
- const ds = new DecompressionStream(format);
132
- const writer = ds.writable.getWriter();
133
- writer.write(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
134
- writer.close();
135
- const chunks = [];
136
- const reader = ds.readable.getReader();
137
- while (true) {
138
- const { done, value } = await reader.read();
139
- if (done) break;
140
- chunks.push(value);
141
- }
142
- const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
143
- const result = new Uint8Array(totalLength);
144
- let offset = 0;
145
- for (const chunk of chunks) {
146
- result.set(chunk, offset);
147
- offset += chunk.length;
148
- }
149
- return result;
119
+ const ds = new DecompressionStream(format);
120
+ const writer = ds.writable.getWriter();
121
+ writer.write(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
122
+ writer.close();
123
+ const chunks = [];
124
+ const reader = ds.readable.getReader();
125
+ while (true) {
126
+ const { done, value } = await reader.read();
127
+ if (done) break;
128
+ chunks.push(value);
129
+ }
130
+ const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
131
+ const result = new Uint8Array(totalLength);
132
+ let offset = 0;
133
+ for (const chunk of chunks) {
134
+ result.set(chunk, offset);
135
+ offset += chunk.length;
136
+ }
137
+ return result;
150
138
  }
151
139
  async function compress(data, format) {
152
- if (hasWebCompression) {
153
- return compressWithWeb(data, format);
154
- }
155
- return compressWithGio(data, format);
140
+ if (hasWebCompression) {
141
+ return compressWithWeb(data, format);
142
+ }
143
+ return compressWithGio(data, format);
156
144
  }
157
145
  async function decompress(data, format) {
158
- if (hasWebCompression) {
159
- return decompressWithWeb(data, format);
160
- }
161
- return decompressWithGio(data, format);
146
+ if (hasWebCompression) {
147
+ return decompressWithWeb(data, format);
148
+ }
149
+ return decompressWithGio(data, format);
162
150
  }
163
151
  function toUint8Array(data) {
164
- if (typeof data === "string") {
165
- return new TextEncoder().encode(data);
166
- }
167
- if (data instanceof ArrayBuffer) {
168
- return new Uint8Array(data);
169
- }
170
- return data;
152
+ if (typeof data === "string") {
153
+ return new TextEncoder().encode(data);
154
+ }
155
+ if (data instanceof ArrayBuffer) {
156
+ return new Uint8Array(data);
157
+ }
158
+ return data;
171
159
  }
172
160
  function gzip(data, optionsOrCallback, callback) {
173
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
174
- compress(toUint8Array(data), "gzip").then(
175
- (result) => cb(null, result),
176
- (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0))
177
- );
161
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
162
+ compress(toUint8Array(data), "gzip").then((result) => cb(null, result), (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0)));
178
163
  }
179
164
  function gunzip(data, optionsOrCallback, callback) {
180
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
181
- decompress(toUint8Array(data), "gzip").then(
182
- (result) => cb(null, result),
183
- (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0))
184
- );
165
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
166
+ decompress(toUint8Array(data), "gzip").then((result) => cb(null, result), (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0)));
185
167
  }
186
168
  function deflate(data, optionsOrCallback, callback) {
187
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
188
- compress(toUint8Array(data), "deflate").then(
189
- (result) => cb(null, result),
190
- (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0))
191
- );
169
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
170
+ compress(toUint8Array(data), "deflate").then((result) => cb(null, result), (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0)));
192
171
  }
193
172
  function inflate(data, optionsOrCallback, callback) {
194
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
195
- decompress(toUint8Array(data), "deflate").then(
196
- (result) => cb(null, result),
197
- (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0))
198
- );
173
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
174
+ decompress(toUint8Array(data), "deflate").then((result) => cb(null, result), (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0)));
199
175
  }
200
176
  function deflateRaw(data, optionsOrCallback, callback) {
201
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
202
- compress(toUint8Array(data), "deflate-raw").then(
203
- (result) => cb(null, result),
204
- (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0))
205
- );
177
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
178
+ compress(toUint8Array(data), "deflate-raw").then((result) => cb(null, result), (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0)));
206
179
  }
207
180
  function inflateRaw(data, optionsOrCallback, callback) {
208
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
209
- decompress(toUint8Array(data), "deflate-raw").then(
210
- (result) => cb(null, result),
211
- (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0))
212
- );
181
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
182
+ decompress(toUint8Array(data), "deflate-raw").then((result) => cb(null, result), (err) => cb(err instanceof Error ? err : new Error(String(err)), new Uint8Array(0)));
213
183
  }
214
184
  function gzipSync(data, _options) {
215
- return compressWithGio(toUint8Array(data), "gzip");
185
+ return compressWithGio(toUint8Array(data), "gzip");
216
186
  }
217
187
  function gunzipSync(data, _options) {
218
- return decompressWithGio(toUint8Array(data), "gzip");
188
+ return decompressWithGio(toUint8Array(data), "gzip");
219
189
  }
220
190
  function deflateSync(data, _options) {
221
- return compressWithGio(toUint8Array(data), "deflate");
191
+ return compressWithGio(toUint8Array(data), "deflate");
222
192
  }
223
193
  function inflateSync(data, _options) {
224
- return decompressWithGio(toUint8Array(data), "deflate");
194
+ return decompressWithGio(toUint8Array(data), "deflate");
225
195
  }
226
196
  function deflateRawSync(data, _options) {
227
- return compressWithGio(toUint8Array(data), "deflate-raw");
197
+ return compressWithGio(toUint8Array(data), "deflate-raw");
228
198
  }
229
199
  function inflateRawSync(data, _options) {
230
- return decompressWithGio(toUint8Array(data), "deflate-raw");
200
+ return decompressWithGio(toUint8Array(data), "deflate-raw");
231
201
  }
232
202
  function brotliCompress(data, optionsOrCallback, callback) {
233
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
234
- cb(new Error("brotliCompress: Brotli is not supported in this environment"), null);
203
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
204
+ cb(new Error("brotliCompress: Brotli is not supported in this environment"), null);
235
205
  }
236
206
  function brotliDecompress(data, optionsOrCallback, callback) {
237
- const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
238
- cb(new Error("brotliDecompress: Brotli is not supported in this environment"), null);
207
+ const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
208
+ cb(new Error("brotliDecompress: Brotli is not supported in this environment"), null);
239
209
  }
240
210
  function brotliCompressSync(_data, _options) {
241
- throw new Error("brotliCompressSync: Brotli is not supported in this environment");
211
+ throw new Error("brotliCompressSync: Brotli is not supported in this environment");
242
212
  }
243
213
  function brotliDecompressSync(_data, _options) {
244
- throw new Error("brotliDecompressSync: Brotli is not supported in this environment");
214
+ throw new Error("brotliDecompressSync: Brotli is not supported in this environment");
245
215
  }
246
216
  const constants = {
247
- Z_NO_FLUSH: 0,
248
- Z_PARTIAL_FLUSH: 1,
249
- Z_SYNC_FLUSH: 2,
250
- Z_FULL_FLUSH: 3,
251
- Z_FINISH: 4,
252
- Z_BLOCK: 5,
253
- Z_TREES: 6,
254
- Z_OK: 0,
255
- Z_STREAM_END: 1,
256
- Z_NEED_DICT: 2,
257
- Z_ERRNO: -1,
258
- Z_STREAM_ERROR: -2,
259
- Z_DATA_ERROR: -3,
260
- Z_MEM_ERROR: -4,
261
- Z_BUF_ERROR: -5,
262
- Z_VERSION_ERROR: -6,
263
- Z_NO_COMPRESSION: 0,
264
- Z_BEST_SPEED: 1,
265
- Z_BEST_COMPRESSION: 9,
266
- Z_DEFAULT_COMPRESSION: -1,
267
- Z_FILTERED: 1,
268
- Z_HUFFMAN_ONLY: 2,
269
- Z_RLE: 3,
270
- Z_FIXED: 4,
271
- Z_DEFAULT_STRATEGY: 0,
272
- Z_DEFLATED: 8
217
+ Z_NO_FLUSH: 0,
218
+ Z_PARTIAL_FLUSH: 1,
219
+ Z_SYNC_FLUSH: 2,
220
+ Z_FULL_FLUSH: 3,
221
+ Z_FINISH: 4,
222
+ Z_BLOCK: 5,
223
+ Z_TREES: 6,
224
+ Z_OK: 0,
225
+ Z_STREAM_END: 1,
226
+ Z_NEED_DICT: 2,
227
+ Z_ERRNO: -1,
228
+ Z_STREAM_ERROR: -2,
229
+ Z_DATA_ERROR: -3,
230
+ Z_MEM_ERROR: -4,
231
+ Z_BUF_ERROR: -5,
232
+ Z_VERSION_ERROR: -6,
233
+ Z_NO_COMPRESSION: 0,
234
+ Z_BEST_SPEED: 1,
235
+ Z_BEST_COMPRESSION: 9,
236
+ Z_DEFAULT_COMPRESSION: -1,
237
+ Z_FILTERED: 1,
238
+ Z_HUFFMAN_ONLY: 2,
239
+ Z_RLE: 3,
240
+ Z_FIXED: 4,
241
+ Z_DEFAULT_STRATEGY: 0,
242
+ Z_DEFLATED: 8
273
243
  };
274
- import {
275
- createGzip as createGzip2,
276
- createGunzip as createGunzip2,
277
- createDeflate as createDeflate2,
278
- createInflate as createInflate2,
279
- createDeflateRaw as createDeflateRaw2,
280
- createInflateRaw as createInflateRaw2,
281
- createUnzip as createUnzip2,
282
- createBrotliCompress as createBrotliCompress2,
283
- createBrotliDecompress as createBrotliDecompress2
284
- } from "./transform-streams.js";
285
- var index_default = {
286
- gzip,
287
- gunzip,
288
- deflate,
289
- inflate,
290
- deflateRaw,
291
- inflateRaw,
292
- gzipSync,
293
- gunzipSync,
294
- deflateSync,
295
- inflateSync,
296
- deflateRawSync,
297
- inflateRawSync,
298
- brotliCompress,
299
- brotliDecompress,
300
- brotliCompressSync,
301
- brotliDecompressSync,
302
- createGzip: createGzip2,
303
- createGunzip: createGunzip2,
304
- createDeflate: createDeflate2,
305
- createInflate: createInflate2,
306
- createDeflateRaw: createDeflateRaw2,
307
- createInflateRaw: createInflateRaw2,
308
- createUnzip: createUnzip2,
309
- createBrotliCompress: createBrotliCompress2,
310
- createBrotliDecompress: createBrotliDecompress2,
311
- constants
312
- };
313
- export {
314
- ZlibTransform,
315
- brotliCompress,
316
- brotliCompressSync,
317
- brotliDecompress,
318
- brotliDecompressSync,
319
- constants,
320
- createBrotliCompress,
321
- createBrotliDecompress,
322
- createDeflate,
323
- createDeflateRaw,
324
- createGunzip,
325
- createGzip,
326
- createInflate,
327
- createInflateRaw,
328
- createUnzip,
329
- index_default as default,
330
- deflate,
331
- deflateRaw,
332
- deflateRawSync,
333
- deflateSync,
334
- gunzip,
335
- gunzipSync,
336
- gzip,
337
- gzipSync,
338
- inflate,
339
- inflateRaw,
340
- inflateRawSync,
341
- inflateSync
244
+ var src_default = {
245
+ gzip,
246
+ gunzip,
247
+ deflate,
248
+ inflate,
249
+ deflateRaw,
250
+ inflateRaw,
251
+ gzipSync,
252
+ gunzipSync,
253
+ deflateSync,
254
+ inflateSync,
255
+ deflateRawSync,
256
+ inflateRawSync,
257
+ brotliCompress,
258
+ brotliDecompress,
259
+ brotliCompressSync,
260
+ brotliDecompressSync,
261
+ createGzip,
262
+ createGunzip,
263
+ createDeflate,
264
+ createInflate,
265
+ createDeflateRaw,
266
+ createInflateRaw,
267
+ createUnzip,
268
+ createBrotliCompress,
269
+ createBrotliDecompress,
270
+ constants
342
271
  };
272
+
273
+ //#endregion
274
+ export { ZlibTransform, brotliCompress, brotliCompressSync, brotliDecompress, brotliDecompressSync, constants, createBrotliCompress, createBrotliDecompress, createDeflate, createDeflateRaw, createGunzip, createGzip, createInflate, createInflateRaw, createUnzip, src_default as default, deflate, deflateRaw, deflateRawSync, deflateSync, gunzip, gunzipSync, gzip, gzipSync, inflate, inflateRaw, inflateRawSync, inflateSync };
@@ -1,123 +1,113 @@
1
1
  import Gio from "@girs/gio-2.0";
2
2
  import GLib from "@girs/glib-2.0";
3
3
  import { Transform } from "node:stream";
4
+
5
+ //#region src/transform-streams.ts
4
6
  function getGioCompressorFormat(format) {
5
- switch (format) {
6
- case "gzip":
7
- return Gio.ZlibCompressorFormat.GZIP;
8
- case "deflate":
9
- return Gio.ZlibCompressorFormat.ZLIB;
10
- case "deflate-raw":
11
- return Gio.ZlibCompressorFormat.RAW;
12
- }
7
+ switch (format) {
8
+ case "gzip": return Gio.ZlibCompressorFormat.GZIP;
9
+ case "deflate": return Gio.ZlibCompressorFormat.ZLIB;
10
+ case "deflate-raw": return Gio.ZlibCompressorFormat.RAW;
11
+ }
13
12
  }
14
13
  function toUint8Array(chunk) {
15
- if (typeof chunk === "string") return new TextEncoder().encode(chunk);
16
- return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
17
- }
18
- class ZlibTransform extends Transform {
19
- _format;
20
- _mode;
21
- _chunks = [];
22
- constructor(format, mode, options) {
23
- super(options);
24
- this._format = format;
25
- this._mode = mode;
26
- }
27
- _transform(chunk, _encoding, callback) {
28
- this._chunks.push(toUint8Array(chunk));
29
- callback();
30
- }
31
- _flush(callback) {
32
- const totalLength = this._chunks.reduce((acc, c) => acc + c.length, 0);
33
- const input = new Uint8Array(totalLength);
34
- let offset = 0;
35
- for (const c of this._chunks) {
36
- input.set(c, offset);
37
- offset += c.length;
38
- }
39
- this._chunks = [];
40
- try {
41
- const result = this._mode === "compress" ? this._compress(input) : this._decompress(input);
42
- this.push(Buffer.from(result));
43
- callback();
44
- } catch (err) {
45
- callback(err instanceof Error ? err : new Error(String(err)));
46
- }
47
- }
48
- _compress(data) {
49
- const compressor = new Gio.ZlibCompressor({ format: getGioCompressorFormat(this._format) });
50
- const converter = new Gio.ConverterOutputStream({
51
- base_stream: Gio.MemoryOutputStream.new_resizable(),
52
- converter: compressor
53
- });
54
- converter.write_bytes(new GLib.Bytes(data), null);
55
- converter.close(null);
56
- const memStream = converter.get_base_stream();
57
- const bytes = memStream.steal_as_bytes();
58
- return new Uint8Array(bytes.get_data() ?? []);
59
- }
60
- _decompress(data) {
61
- const decompressor = new Gio.ZlibDecompressor({ format: getGioCompressorFormat(this._format) });
62
- const memInput = Gio.MemoryInputStream.new_from_bytes(new GLib.Bytes(data));
63
- const converter = new Gio.ConverterInputStream({
64
- base_stream: memInput,
65
- converter: decompressor
66
- });
67
- const chunks = [];
68
- const bufSize = 4096;
69
- while (true) {
70
- const bytes = converter.read_bytes(bufSize, null);
71
- if (bytes.get_size() === 0) break;
72
- chunks.push(new Uint8Array(bytes.get_data()));
73
- }
74
- converter.close(null);
75
- const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
76
- const result = new Uint8Array(totalLength);
77
- let off = 0;
78
- for (const c of chunks) {
79
- result.set(c, off);
80
- off += c.length;
81
- }
82
- return result;
83
- }
14
+ if (typeof chunk === "string") return new TextEncoder().encode(chunk);
15
+ return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
84
16
  }
17
+ var ZlibTransform = class extends Transform {
18
+ _format;
19
+ _mode;
20
+ _chunks = [];
21
+ constructor(format, mode, options) {
22
+ super(options);
23
+ this._format = format;
24
+ this._mode = mode;
25
+ }
26
+ _transform(chunk, _encoding, callback) {
27
+ this._chunks.push(toUint8Array(chunk));
28
+ callback();
29
+ }
30
+ _flush(callback) {
31
+ const totalLength = this._chunks.reduce((acc, c) => acc + c.length, 0);
32
+ const input = new Uint8Array(totalLength);
33
+ let offset = 0;
34
+ for (const c of this._chunks) {
35
+ input.set(c, offset);
36
+ offset += c.length;
37
+ }
38
+ this._chunks = [];
39
+ try {
40
+ const result = this._mode === "compress" ? this._compress(input) : this._decompress(input);
41
+ this.push(Buffer.from(result));
42
+ callback();
43
+ } catch (err) {
44
+ callback(err instanceof Error ? err : new Error(String(err)));
45
+ }
46
+ }
47
+ _compress(data) {
48
+ const compressor = new Gio.ZlibCompressor({ format: getGioCompressorFormat(this._format) });
49
+ const converter = new Gio.ConverterOutputStream({
50
+ base_stream: Gio.MemoryOutputStream.new_resizable(),
51
+ converter: compressor
52
+ });
53
+ converter.write_bytes(new GLib.Bytes(data), null);
54
+ converter.close(null);
55
+ const memStream = converter.get_base_stream();
56
+ const bytes = memStream.steal_as_bytes();
57
+ return new Uint8Array(bytes.get_data() ?? []);
58
+ }
59
+ _decompress(data) {
60
+ const decompressor = new Gio.ZlibDecompressor({ format: getGioCompressorFormat(this._format) });
61
+ const memInput = Gio.MemoryInputStream.new_from_bytes(new GLib.Bytes(data));
62
+ const converter = new Gio.ConverterInputStream({
63
+ base_stream: memInput,
64
+ converter: decompressor
65
+ });
66
+ const chunks = [];
67
+ const bufSize = 4096;
68
+ while (true) {
69
+ const bytes = converter.read_bytes(bufSize, null);
70
+ if (bytes.get_size() === 0) break;
71
+ chunks.push(new Uint8Array(bytes.get_data()));
72
+ }
73
+ converter.close(null);
74
+ const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
75
+ const result = new Uint8Array(totalLength);
76
+ let off = 0;
77
+ for (const c of chunks) {
78
+ result.set(c, off);
79
+ off += c.length;
80
+ }
81
+ return result;
82
+ }
83
+ };
85
84
  function createGzip(options) {
86
- return new ZlibTransform("gzip", "compress", options);
85
+ return new ZlibTransform("gzip", "compress", options);
87
86
  }
88
87
  function createGunzip(options) {
89
- return new ZlibTransform("gzip", "decompress", options);
88
+ return new ZlibTransform("gzip", "decompress", options);
90
89
  }
91
90
  function createDeflate(options) {
92
- return new ZlibTransform("deflate", "compress", options);
91
+ return new ZlibTransform("deflate", "compress", options);
93
92
  }
94
93
  function createInflate(options) {
95
- return new ZlibTransform("deflate", "decompress", options);
94
+ return new ZlibTransform("deflate", "decompress", options);
96
95
  }
97
96
  function createDeflateRaw(options) {
98
- return new ZlibTransform("deflate-raw", "compress", options);
97
+ return new ZlibTransform("deflate-raw", "compress", options);
99
98
  }
100
99
  function createInflateRaw(options) {
101
- return new ZlibTransform("deflate-raw", "decompress", options);
100
+ return new ZlibTransform("deflate-raw", "decompress", options);
102
101
  }
103
102
  function createUnzip(options) {
104
- return new ZlibTransform("gzip", "decompress", options);
103
+ return new ZlibTransform("gzip", "decompress", options);
105
104
  }
106
105
  function createBrotliCompress(_options) {
107
- throw new Error("createBrotliCompress is not supported on GJS (no Brotli in GLib)");
106
+ throw new Error("createBrotliCompress is not supported on GJS (no Brotli in GLib)");
108
107
  }
109
108
  function createBrotliDecompress(_options) {
110
- throw new Error("createBrotliDecompress is not supported on GJS (no Brotli in GLib)");
109
+ throw new Error("createBrotliDecompress is not supported on GJS (no Brotli in GLib)");
111
110
  }
112
- export {
113
- ZlibTransform,
114
- createBrotliCompress,
115
- createBrotliDecompress,
116
- createDeflate,
117
- createDeflateRaw,
118
- createGunzip,
119
- createGzip,
120
- createInflate,
121
- createInflateRaw,
122
- createUnzip
123
- };
111
+
112
+ //#endregion
113
+ export { ZlibTransform, createBrotliCompress, createBrotliDecompress, createDeflate, createDeflateRaw, createGunzip, createGzip, createInflate, createInflateRaw, createUnzip };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/zlib",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "description": "Node.js zlib module for Gjs",
5
5
  "module": "lib/esm/index.js",
6
6
  "types": "lib/types/index.d.ts",
@@ -30,12 +30,12 @@
30
30
  "zlib"
31
31
  ],
32
32
  "dependencies": {
33
- "@girs/gio-2.0": "^2.88.0-4.0.0-rc.9",
34
- "@girs/glib-2.0": "^2.88.0-4.0.0-rc.9"
33
+ "@girs/gio-2.0": "2.88.0-4.0.0-rc.9",
34
+ "@girs/glib-2.0": "2.88.0-4.0.0-rc.9"
35
35
  },
36
36
  "devDependencies": {
37
- "@gjsify/cli": "^0.3.12",
38
- "@gjsify/unit": "^0.3.12",
37
+ "@gjsify/cli": "^0.3.14",
38
+ "@gjsify/unit": "^0.3.14",
39
39
  "@types/node": "^25.6.0",
40
40
  "typescript": "^6.0.3"
41
41
  }