From fe757caa69aed02d92664dcfe4673a5accdfcfc3 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Wed, 15 Apr 2026 15:54:39 -0700 Subject: [PATCH] readable stream reject chunk --- src/streams/pipes.c | 13 +----- .../test_readable_stream_tee_object_chunk.cjs | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 tests/test_readable_stream_tee_object_chunk.cjs diff --git a/src/streams/pipes.c b/src/streams/pipes.c index 90ed936..62f09e5 100644 --- a/src/streams/pipes.c +++ b/src/streams/pipes.c @@ -12,7 +12,6 @@ #include "streams/pipes.h" #include "streams/readable.h" #include "streams/writable.h" -#include "modules/structured-clone.h" typedef struct { bool settled; @@ -635,18 +634,8 @@ static ant_value_t tee_read_resolve(ant_t *js, ant_value_t *args, int nargs) { } ant_value_t value = js_get(js, result, "value"); - ant_value_t clone = value; - - if (!st->canceled1 && !st->canceled2) { - ant_value_t clone_args[1] = { value }; - clone = js_structured_clone(js, clone_args, 1); - if (is_err(clone)) { - tee_read_reject(js, &clone, 1); - return js_mkundef(); - }} - if (!st->canceled1) tee_enqueue_branch(js, branch1, value); - if (!st->canceled2) tee_enqueue_branch(js, branch2, clone); + if (!st->canceled2) tee_enqueue_branch(js, branch2, value); return js_mkundef(); } diff --git a/tests/test_readable_stream_tee_object_chunk.cjs b/tests/test_readable_stream_tee_object_chunk.cjs new file mode 100644 index 0000000..d979a36 --- /dev/null +++ b/tests/test_readable_stream_tee_object_chunk.cjs @@ -0,0 +1,41 @@ +function assert(condition, message) { + if (!condition) throw new Error(message); +} + +async function main() { + const chunk = { + label: 'alpha', + fn() { + return 'ok'; + }, + }; + + const source = new ReadableStream({ + start(controller) { + controller.enqueue(chunk); + controller.close(); + }, + }); + + const [branch1, branch2] = source.tee(); + const reader1 = branch1.getReader(); + const reader2 = branch2.getReader(); + + const [{ done: done1, value: value1 }, { done: done2, value: value2 }] = + await Promise.all([reader1.read(), reader2.read()]); + + assert(done1 === false, 'branch1 should receive a chunk'); + assert(done2 === false, 'branch2 should receive a chunk'); + assert(value1 === chunk, 'branch1 should receive the original chunk object'); + assert(value2 === chunk, 'branch2 should receive the original chunk object'); + assert(value1 === value2, 'tee branches should share the same chunk reference'); + assert(typeof value2.fn === 'function', 'function-valued chunk properties should survive tee'); + assert(value2.fn() === 'ok', 'function-valued chunk should remain callable'); + + console.log('readable stream tee preserves object chunks without cloning'); +} + +main().catch((err) => { + console.error(err && err.stack ? err.stack : String(err)); + throw err; +}); -- 2.51.2