@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 +216 -284
- package/lib/esm/transform-streams.js +88 -98
- package/package.json +5 -5
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
140
|
+
if (hasWebCompression) {
|
|
141
|
+
return compressWithWeb(data, format);
|
|
142
|
+
}
|
|
143
|
+
return compressWithGio(data, format);
|
|
156
144
|
}
|
|
157
145
|
async function decompress(data, format) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
146
|
+
if (hasWebCompression) {
|
|
147
|
+
return decompressWithWeb(data, format);
|
|
148
|
+
}
|
|
149
|
+
return decompressWithGio(data, format);
|
|
162
150
|
}
|
|
163
151
|
function toUint8Array(data) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
174
|
-
|
|
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
|
-
|
|
181
|
-
|
|
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
|
-
|
|
188
|
-
|
|
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
|
-
|
|
195
|
-
|
|
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
|
-
|
|
202
|
-
|
|
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
|
-
|
|
209
|
-
|
|
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
|
-
|
|
185
|
+
return compressWithGio(toUint8Array(data), "gzip");
|
|
216
186
|
}
|
|
217
187
|
function gunzipSync(data, _options) {
|
|
218
|
-
|
|
188
|
+
return decompressWithGio(toUint8Array(data), "gzip");
|
|
219
189
|
}
|
|
220
190
|
function deflateSync(data, _options) {
|
|
221
|
-
|
|
191
|
+
return compressWithGio(toUint8Array(data), "deflate");
|
|
222
192
|
}
|
|
223
193
|
function inflateSync(data, _options) {
|
|
224
|
-
|
|
194
|
+
return decompressWithGio(toUint8Array(data), "deflate");
|
|
225
195
|
}
|
|
226
196
|
function deflateRawSync(data, _options) {
|
|
227
|
-
|
|
197
|
+
return compressWithGio(toUint8Array(data), "deflate-raw");
|
|
228
198
|
}
|
|
229
199
|
function inflateRawSync(data, _options) {
|
|
230
|
-
|
|
200
|
+
return decompressWithGio(toUint8Array(data), "deflate-raw");
|
|
231
201
|
}
|
|
232
202
|
function brotliCompress(data, optionsOrCallback, callback) {
|
|
233
|
-
|
|
234
|
-
|
|
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
|
-
|
|
238
|
-
|
|
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
|
-
|
|
211
|
+
throw new Error("brotliCompressSync: Brotli is not supported in this environment");
|
|
242
212
|
}
|
|
243
213
|
function brotliDecompressSync(_data, _options) {
|
|
244
|
-
|
|
214
|
+
throw new Error("brotliDecompressSync: Brotli is not supported in this environment");
|
|
245
215
|
}
|
|
246
216
|
const constants = {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
16
|
-
|
|
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
|
-
|
|
85
|
+
return new ZlibTransform("gzip", "compress", options);
|
|
87
86
|
}
|
|
88
87
|
function createGunzip(options) {
|
|
89
|
-
|
|
88
|
+
return new ZlibTransform("gzip", "decompress", options);
|
|
90
89
|
}
|
|
91
90
|
function createDeflate(options) {
|
|
92
|
-
|
|
91
|
+
return new ZlibTransform("deflate", "compress", options);
|
|
93
92
|
}
|
|
94
93
|
function createInflate(options) {
|
|
95
|
-
|
|
94
|
+
return new ZlibTransform("deflate", "decompress", options);
|
|
96
95
|
}
|
|
97
96
|
function createDeflateRaw(options) {
|
|
98
|
-
|
|
97
|
+
return new ZlibTransform("deflate-raw", "compress", options);
|
|
99
98
|
}
|
|
100
99
|
function createInflateRaw(options) {
|
|
101
|
-
|
|
100
|
+
return new ZlibTransform("deflate-raw", "decompress", options);
|
|
102
101
|
}
|
|
103
102
|
function createUnzip(options) {
|
|
104
|
-
|
|
103
|
+
return new ZlibTransform("gzip", "decompress", options);
|
|
105
104
|
}
|
|
106
105
|
function createBrotliCompress(_options) {
|
|
107
|
-
|
|
106
|
+
throw new Error("createBrotliCompress is not supported on GJS (no Brotli in GLib)");
|
|
108
107
|
}
|
|
109
108
|
function createBrotliDecompress(_options) {
|
|
110
|
-
|
|
109
|
+
throw new Error("createBrotliDecompress is not supported on GJS (no Brotli in GLib)");
|
|
111
110
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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.
|
|
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": "
|
|
34
|
-
"@girs/glib-2.0": "
|
|
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.
|
|
38
|
-
"@gjsify/unit": "^0.3.
|
|
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
|
}
|