1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
273
274
|
const { Transform } = require("node:stream");
const { throwNotImplemented } = require("$shared");
const { zlib: constants } = $processBindingConstants;
const codes = {
__proto__: null,
"0": "Z_OK",
"1": "Z_STREAM_END",
"2": "Z_NEED_DICT",
Z_OK: 0,
Z_STREAM_END: 1,
Z_NEED_DICT: 2,
Z_ERRNO: -1,
Z_STREAM_ERROR: -2,
Z_DATA_ERROR: -3,
Z_MEM_ERROR: -4,
Z_BUF_ERROR: -5,
Z_VERSION_ERROR: -6,
"-1": "Z_ERRNO",
"-2": "Z_STREAM_ERROR",
"-3": "Z_DATA_ERROR",
"-4": "Z_MEM_ERROR",
"-5": "Z_BUF_ERROR",
"-6": "Z_VERSION_ERROR",
};
function callbackified(callback, fn, ...args) {
try {
callback(null, fn(...args));
} catch (err) {
callback(err);
}
}
let slow_zlib;
function slowFallback(name: string, args: any[]) {
return (slow_zlib ??= require("./zlib.browserify.js"))[name](...args);
}
function ZlibBase(this, opts, processor) {
Reflect.apply(Transform, this, [{ autoDestroy: true, ...opts }]);
this._cb = processor;
this._buffer = [];
}
ZlibBase.prototype = Object.create(Transform.prototype);
ZlibBase.prototype._transform = function (chunk, encoding, callback) {
console.log("ZlibBase.prototype._transform", chunk, encoding, callback);
this._buffer.push(chunk);
callback();
};
ZlibBase.prototype._flush = function (callback) {
const buffer = Buffer.concat(this._buffer);
this._buffer = [];
console.log("ZlibBase.prototype._flush", buffer, this._cb);
callbackified(callback, this._cb, buffer);
};
function Deflate(this, options) {
ZlibBase.call(this, options, Bun.deflateSync);
}
Deflate.prototype = Object.create(ZlibBase.prototype);
function Inflate(this, options) {
ZlibBase.call(this, options, Bun.inflateSync);
}
Inflate.prototype = Object.create(ZlibBase.prototype);
function Gzip(this, options) {
ZlibBase.call(this, options, Bun.gzipSync);
}
Gzip.prototype = Object.create(ZlibBase.prototype);
function Gunzip(this, options) {
ZlibBase.call(this, options, Bun.gunzipSync);
}
Gunzip.prototype = Object.create(ZlibBase.prototype);
// function DeflateRaw(options) {
// return new ZlibBase(options, DeflateRaw);
// }
// DeflateRaw.prototype = Object.create(ZlibBase.prototype);
// function InflateRaw(options) {
// return new ZlibBase(options, InflateRaw);
// }
// InflateRaw.prototype = Object.create(ZlibBase.prototype);
// function Unzip(this, options) {
// ZlibBase.call(this, options, Unzip);
// }
// Unzip.prototype = Object.create(ZlibBase.prototype);
function BrotliCompress() {
throw throwNotImplemented("Brotli compression", 267);
}
BrotliCompress.prototype = Object.create(ZlibBase.prototype);
function BrotliDecompress() {
throw throwNotImplemented("Brotli compression", 267);
}
BrotliDecompress.prototype = Object.create(ZlibBase.prototype);
function deflate(buffer, options, callback) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
if (typeof callback !== "function") {
throw new TypeError("Callback must be a function");
}
// TODO: async
process.nextTick(callbackified, callback, deflateSync, buffer, options);
}
function deflateSync(buffer, options) {
// TODO: options
return Buffer.from(Bun.deflateSync(buffer));
}
function gzip(buffer, options, callback) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
if (typeof callback !== "function") {
throw new TypeError("Callback must be a function");
}
// TODO: async
process.nextTick(callbackified, callback, gzipSync, buffer, options);
}
function gzipSync(buffer, options) {
// TODO: options
return Buffer.from(Bun.gzipSync(buffer));
}
function deflateRaw(...args) {
return slowFallback("deflateRaw", args);
}
function deflateRawSync(...args) {
return slowFallback("deflateRawSync", args);
}
/// "Decompress either a Gzip- or Deflate-compressed stream by auto-detecting the header."
function unzip(...args) {
return slowFallback("unzip", args);
}
function unzipSync(...args) {
return slowFallback("unzipSync", args);
}
function inflate(buffer, options, callback) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
if (typeof callback !== "function") {
throw new TypeError("Callback must be a function");
}
// TODO: async
process.nextTick(callbackified, callback, gzipSync, buffer, options);
}
function inflateSync(buffer, options) {
// TODO: options
return Buffer.from(Bun.inflateSync(buffer));
}
function gunzip(callback, buffer, options) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
if (typeof callback !== "function") {
throw new TypeError("Callback must be a function");
}
// TODO: async
process.nextTick(callbackified, callback, gzipSync, buffer, options);
}
function gunzipSync(buffer, options) {
// TODO: options
return Buffer.from(Bun.gunzipSync(buffer));
}
function inflateRaw(...args) {
return slowFallback("inflateRaw", args);
}
function inflateRawSync(...args) {
return slowFallback("inflateRawSync", args);
}
function brotliCompress() {
throw throwNotImplemented("Brotli compression", 267);
}
function brotliCompressSync() {
throw throwNotImplemented("Brotli compression", 267);
}
function brotliDecompress() {
throw throwNotImplemented("Brotli compression", 267);
}
function brotliDecompressSync() {
throw throwNotImplemented("Brotli compression", 267);
}
const createDeflate = opts => new Deflate(opts);
const createInflate = opts => new Inflate(opts);
// const createDeflateRaw = opts => new DeflateRaw(opts);
// const createInflateRaw = opts => new InflateRaw(opts);
const createGzip = opts => new Gzip(opts);
const createGunzip = opts => new Gunzip(opts);
// const createUnzip = opts => new Unzip(opts);
function createBrotliCompress() {
throw throwNotImplemented("Brotli compression", 267);
}
function createBrotliDecompress() {
throw throwNotImplemented("Brotli compression", 267);
}
export default {
Deflate,
Inflate,
Gzip,
Gunzip,
// DeflateRaw,
// InflateRaw,
// Unzip,
BrotliCompress,
BrotliDecompress,
deflate,
deflateSync,
gzip,
gzipSync,
deflateRaw,
deflateRawSync,
unzip,
unzipSync,
inflate,
inflateSync,
gunzip,
gunzipSync,
inflateRaw,
inflateRawSync,
brotliCompress,
brotliCompressSync,
brotliDecompress,
brotliDecompressSync,
createDeflate,
createInflate,
// createDeflateRaw,
// createInflateRaw,
createGzip,
createGunzip,
// createUnzip,
createBrotliCompress,
createBrotliDecompress,
constants,
codes,
};
|