MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

readable stream reject chunk

+42 -12
+1 -12
src/streams/pipes.c
··· 12 12 #include "streams/pipes.h" 13 13 #include "streams/readable.h" 14 14 #include "streams/writable.h" 15 - #include "modules/structured-clone.h" 16 15 17 16 typedef struct { 18 17 bool settled; ··· 635 634 } 636 635 637 636 ant_value_t value = js_get(js, result, "value"); 638 - ant_value_t clone = value; 639 - 640 - if (!st->canceled1 && !st->canceled2) { 641 - ant_value_t clone_args[1] = { value }; 642 - clone = js_structured_clone(js, clone_args, 1); 643 - if (is_err(clone)) { 644 - tee_read_reject(js, &clone, 1); 645 - return js_mkundef(); 646 - }} 647 - 648 637 if (!st->canceled1) tee_enqueue_branch(js, branch1, value); 649 - if (!st->canceled2) tee_enqueue_branch(js, branch2, clone); 638 + if (!st->canceled2) tee_enqueue_branch(js, branch2, value); 650 639 651 640 return js_mkundef(); 652 641 }
+41
tests/test_readable_stream_tee_object_chunk.cjs
··· 1 + function assert(condition, message) { 2 + if (!condition) throw new Error(message); 3 + } 4 + 5 + async function main() { 6 + const chunk = { 7 + label: 'alpha', 8 + fn() { 9 + return 'ok'; 10 + }, 11 + }; 12 + 13 + const source = new ReadableStream({ 14 + start(controller) { 15 + controller.enqueue(chunk); 16 + controller.close(); 17 + }, 18 + }); 19 + 20 + const [branch1, branch2] = source.tee(); 21 + const reader1 = branch1.getReader(); 22 + const reader2 = branch2.getReader(); 23 + 24 + const [{ done: done1, value: value1 }, { done: done2, value: value2 }] = 25 + await Promise.all([reader1.read(), reader2.read()]); 26 + 27 + assert(done1 === false, 'branch1 should receive a chunk'); 28 + assert(done2 === false, 'branch2 should receive a chunk'); 29 + assert(value1 === chunk, 'branch1 should receive the original chunk object'); 30 + assert(value2 === chunk, 'branch2 should receive the original chunk object'); 31 + assert(value1 === value2, 'tee branches should share the same chunk reference'); 32 + assert(typeof value2.fn === 'function', 'function-valued chunk properties should survive tee'); 33 + assert(value2.fn() === 'ok', 'function-valued chunk should remain callable'); 34 + 35 + console.log('readable stream tee preserves object chunks without cloning'); 36 + } 37 + 38 + main().catch((err) => { 39 + console.error(err && err.stack ? err.stack : String(err)); 40 + throw err; 41 + });