aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/js/node/zlib.browserify.js (renamed from src/js/node/zlib.js)6
-rw-r--r--src/js/node/zlib.ts274
2 files changed, 280 insertions, 0 deletions
diff --git a/src/js/node/zlib.js b/src/js/node/zlib.browserify.js
index 06c523b85..c1ea260a1 100644
--- a/src/js/node/zlib.js
+++ b/src/js/node/zlib.browserify.js
@@ -4111,14 +4111,17 @@ var require_lib = __commonJS({
}
function Deflate(opts) {
if (!(this instanceof Deflate)) return new Deflate(opts);
+ console.log("new Deflate");
Zlib.call(this, opts, binding.DEFLATE);
}
function Inflate(opts) {
if (!(this instanceof Inflate)) return new Inflate(opts);
+ console.log("new Inflate");
Zlib.call(this, opts, binding.INFLATE);
}
function Gzip(opts) {
if (!(this instanceof Gzip)) return new Gzip(opts);
+ console.log("new Gzip");
Zlib.call(this, opts, binding.GZIP);
}
function Gunzip(opts) {
@@ -4127,14 +4130,17 @@ var require_lib = __commonJS({
}
function DeflateRaw(opts) {
if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
+ console.log("new DeflateRaw");
Zlib.call(this, opts, binding.DEFLATERAW);
}
function InflateRaw(opts) {
if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
+ console.log("new InflateRaw");
Zlib.call(this, opts, binding.INFLATERAW);
}
function Unzip(opts) {
if (!(this instanceof Unzip)) return new Unzip(opts);
+ console.log("new Unzip");
Zlib.call(this, opts, binding.UNZIP);
}
function isValidFlushFlag(flag) {
diff --git a/src/js/node/zlib.ts b/src/js/node/zlib.ts
new file mode 100644
index 000000000..2db20da1c
--- /dev/null
+++ b/src/js/node/zlib.ts
@@ -0,0 +1,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,
+};