import { ERR_METHOD_NOT_IMPLEMENTED } from "__wasm_rquickjs_builtin/internal/errors";
import Duplex from "__wasm_rquickjs_builtin/internal/streams/duplex";
import { nextTick } from "node:process";
const kCallback = Symbol("kCallback");
function Transform(options) {
if (!(this instanceof Transform)) {
return new Transform(options);
}
Duplex.call(this, options);
this._readableState.sync = false;
this[kCallback] = null;
if (options) {
if (typeof options.transform === "function") {
this._transform = options.transform;
}
if (typeof options.flush === "function") {
this._flush = options.flush;
}
}
this.on("prefinish", prefinish);
}
Object.setPrototypeOf(Transform.prototype, Duplex.prototype);
Object.setPrototypeOf(Transform, Duplex);
function final(cb) {
let called = false;
if (typeof this._flush === "function" && !this.destroyed) {
const result = this._flush((er, data) => {
called = true;
if (er) {
if (cb) {
cb(er);
} else {
this.destroy(er);
}
return;
}
if (data != null) {
this.push(data);
}
this.push(null);
if (cb) {
cb();
}
});
if (result !== undefined && result !== null) {
try {
const then = result.then;
if (typeof then === "function") {
then.call(
result,
(data) => {
if (called) {
return;
}
if (data != null) {
this.push(data);
}
this.push(null);
if (cb) {
nextTick(cb);
}
},
(err) => {
if (cb) {
nextTick(cb, err);
} else {
nextTick(() => this.destroy(err));
}
},
);
}
} catch (err) {
nextTick(() => this.destroy(err));
}
}
} else {
this.push(null);
if (cb) {
cb();
}
}
}
function prefinish() {
if (this._final !== final) {
final.call(this);
}
}
Transform.prototype._final = final;
Transform.prototype._transform = function (chunk, encoding, callback) {
throw new ERR_METHOD_NOT_IMPLEMENTED("_transform()");
};
Transform.prototype._write = function (chunk, encoding, callback) {
const rState = this._readableState;
const wState = this._writableState;
const length = rState.length;
let called = false;
const result = this._transform(chunk, encoding, (err, val) => {
called = true;
if (err) {
callback(err);
return;
}
if (val != null) {
this.push(val);
}
if (
wState.ended || length === rState.length || rState.length < rState.highWaterMark ||
rState.length === 0
) {
callback();
} else {
this[kCallback] = callback;
}
});
if (result !== undefined && result != null) {
try {
const then = result.then;
if (typeof then === "function") {
then.call(
result,
(val) => {
if (called) {
return;
}
if (val != null) {
this.push(val);
}
if (
wState.ended ||
length === rState.length ||
rState.length < rState.highWaterMark ||
rState.length === 0
) {
nextTick(callback);
} else {
this[kCallback] = callback;
}
},
(err) => {
nextTick(callback, err);
},
);
}
} catch (err) {
nextTick(callback, err);
}
}
};
Transform.prototype._read = function () {
if (this[kCallback]) {
const callback = this[kCallback];
this[kCallback] = null;
callback();
}
};
export default Transform;