Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ declare module "bun" {
unref(): void;
}

interface TransformerCancelCallback {
(reason: any): void | PromiseLike<void>;
}

interface TransformerFlushCallback<O> {
(controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
}
Expand Down
1 change: 1 addition & 0 deletions packages/bun-types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ interface QueuingStrategySize<T = any> {
}

interface Transformer<I = any, O = any> {
cancel?: Bun.TransformerCancelCallback;
flush?: Bun.TransformerFlushCallback<O>;
readableType?: undefined;
start?: Bun.TransformerStartCallback<O>;
Expand Down
2 changes: 2 additions & 0 deletions src/js/builtins/BunBuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ using namespace JSC;
#define BUN_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(macro) \
macro(AbortSignal) \
macro(Buffer) \
macro(CompressionStreamTransformer) \
macro(Loader) \
macro(ReadableByteStreamController) \
macro(ReadableStream) \
Expand Down Expand Up @@ -99,6 +100,7 @@ using namespace JSC;
macro(fatal) \
macro(fd) \
macro(filename) \
macro(finishPromise) \
macro(flushAlgorithm) \
macro(format) \
macro(fulfillModuleSync) \
Expand Down
31 changes: 17 additions & 14 deletions src/js/builtins/CompressionStream.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
export function initializeCompressionStream(this, format) {
const zlib = require("node:zlib");
const { newBufferSourceTransformPairFromDuplex } = require("internal/webstreams_adapters");

const builders = {
"deflate": zlib.createDeflate,
"deflate-raw": zlib.createDeflateRaw,
"gzip": zlib.createGzip,
"brotli": zlib.createBrotliCompress,
"zstd": zlib.createZstdCompress,
// node:zlib NodeMode values (DEFLATE, GZIP, DEFLATERAW, BROTLI_ENCODE,
// ZSTD_COMPRESS) — the native transformer initializes the matching engine
// with node:zlib's defaults, so output bytes match the node-backed
// implementation this replaced.
Comment thread
robobun marked this conversation as resolved.
const modes = {
__proto__: null,
"deflate": 1,
"deflate-raw": 5,
"gzip": 3,
"brotli": 9,
"zstd": 10,
};

if (!(format in builders))
throw $ERR_INVALID_ARG_VALUE("format", format, "must be one of: " + Object.keys(builders).join(", "));
if (!(format in modes))
Comment thread
robobun marked this conversation as resolved.
throw $ERR_INVALID_ARG_VALUE("format", format, "must be one of: " + Object.keys(modes).join(", "));

const transform = $createCompressionTransform(modes[format]);

const transform = newBufferSourceTransformPairFromDuplex(builders[format]());
$putByIdDirectPrivate(this, "readable", transform.readable);
$putByIdDirectPrivate(this, "writable", transform.writable);
$putByIdDirectPrivate(this, "readable", $getByIdDirectPrivate(transform, "readable"));
$putByIdDirectPrivate(this, "writable", $getByIdDirectPrivate(transform, "writable"));

return this;
}
Expand Down
29 changes: 15 additions & 14 deletions src/js/builtins/DecompressionStream.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
export function initializeDecompressionStream(this, format) {
const zlib = require("node:zlib");
const { newBufferSourceTransformPairFromDuplex } = require("internal/webstreams_adapters");

const builders = {
"deflate": zlib.createInflate,
"deflate-raw": zlib.createInflateRaw,
"gzip": zlib.createGunzip,
"brotli": zlib.createBrotliDecompress,
"zstd": zlib.createZstdDecompress,
// node:zlib NodeMode values (INFLATE, GUNZIP, INFLATERAW, BROTLI_DECODE,
// ZSTD_DECOMPRESS) — see CompressionStream for the encode-side table.
const modes = {
__proto__: null,
"deflate": 2,
"deflate-raw": 6,
"gzip": 4,
"brotli": 8,
"zstd": 11,
};

if (!(format in builders))
throw $ERR_INVALID_ARG_VALUE("format", format, "must be one of: " + Object.keys(builders).join(", "));
if (!(format in modes))
throw $ERR_INVALID_ARG_VALUE("format", format, "must be one of: " + Object.keys(modes).join(", "));

const transform = $createCompressionTransform(modes[format]);

const transform = newBufferSourceTransformPairFromDuplex(builders[format]());
$putByIdDirectPrivate(this, "readable", transform.readable);
$putByIdDirectPrivate(this, "writable", transform.writable);
$putByIdDirectPrivate(this, "readable", $getByIdDirectPrivate(transform, "readable"));
$putByIdDirectPrivate(this, "writable", $getByIdDirectPrivate(transform, "writable"));

return this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/js/builtins/TransformStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export function initializeTransformStream(this) {
transformerDict["flush"] = transformer["flush"];
if (typeof transformerDict["flush"] !== "function") $throwTypeError("transformer.flush should be a function");
}
if ("cancel" in transformer) {
transformerDict["cancel"] = transformer["cancel"];
if (typeof transformerDict["cancel"] !== "function") $throwTypeError("transformer.cancel should be a function");
}

if ("readableType" in transformer) throw new RangeError("TransformStream transformer has a readableType");
if ("writableType" in transformer) throw new RangeError("TransformStream transformer has a writableType");
Expand Down
Loading
Loading