|
| 1 | +// Flags: --experimental-stream-iter |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Coverage tests for from.js: sub-batching >128, DataView in generator, |
| 5 | +// non-Uint8Array TypedArray normalization. |
| 6 | + |
| 7 | +const common = require('../common'); |
| 8 | +const assert = require('assert'); |
| 9 | +const { |
| 10 | + from, |
| 11 | + fromSync, |
| 12 | + bytes, |
| 13 | + bytesSync, |
| 14 | +} = require('stream/iter'); |
| 15 | + |
| 16 | +// fromSync: Uint8Array[] with > 128 elements triggers sub-batching |
| 17 | +async function testFromSyncSubBatching() { |
| 18 | + const bigBatch = Array.from({ length: 200 }, |
| 19 | + (_, i) => new Uint8Array([i & 0xFF])); |
| 20 | + const batches = []; |
| 21 | + for (const batch of fromSync(bigBatch)) { |
| 22 | + batches.push(batch); |
| 23 | + } |
| 24 | + // Should be split into sub-batches: 128 + 72 |
| 25 | + assert.strictEqual(batches.length, 2); |
| 26 | + assert.strictEqual(batches[0].length, 128); |
| 27 | + assert.strictEqual(batches[1].length, 72); |
| 28 | + // Verify no data loss |
| 29 | + let totalChunks = 0; |
| 30 | + for (const batch of batches) totalChunks += batch.length; |
| 31 | + assert.strictEqual(totalChunks, 200); |
| 32 | +} |
| 33 | + |
| 34 | +// from: Uint8Array[] with > 128 elements triggers sub-batching (async) |
| 35 | +async function testFromAsyncSubBatching() { |
| 36 | + const bigBatch = Array.from({ length: 200 }, |
| 37 | + (_, i) => new Uint8Array([i & 0xFF])); |
| 38 | + const batches = []; |
| 39 | + for await (const batch of from(bigBatch)) { |
| 40 | + batches.push(batch); |
| 41 | + } |
| 42 | + assert.strictEqual(batches.length, 2); |
| 43 | + assert.strictEqual(batches[0].length, 128); |
| 44 | + assert.strictEqual(batches[1].length, 72); |
| 45 | +} |
| 46 | + |
| 47 | +// Exact boundary: 128 elements → single batch (no split) |
| 48 | +async function testFromSubBatchingBoundary() { |
| 49 | + const exactBatch = Array.from({ length: 128 }, |
| 50 | + (_, i) => new Uint8Array([i])); |
| 51 | + const batches = []; |
| 52 | + for (const batch of fromSync(exactBatch)) { |
| 53 | + batches.push(batch); |
| 54 | + } |
| 55 | + assert.strictEqual(batches.length, 1); |
| 56 | + assert.strictEqual(batches[0].length, 128); |
| 57 | +} |
| 58 | + |
| 59 | +// 129 elements → 2 batches (128 + 1) |
| 60 | +async function testFromSubBatchingBoundaryPlus1() { |
| 61 | + const batch129 = Array.from({ length: 129 }, |
| 62 | + (_, i) => new Uint8Array([i & 0xFF])); |
| 63 | + const batches = []; |
| 64 | + for await (const batch of from(batch129)) { |
| 65 | + batches.push(batch); |
| 66 | + } |
| 67 | + assert.strictEqual(batches.length, 2); |
| 68 | + assert.strictEqual(batches[0].length, 128); |
| 69 | + assert.strictEqual(batches[1].length, 1); |
| 70 | +} |
| 71 | + |
| 72 | +// DataView yielded from a sync generator → normalizeSyncValue path |
| 73 | +async function testFromSyncDataViewInGenerator() { |
| 74 | + function* gen() { |
| 75 | + const buf = new ArrayBuffer(3); |
| 76 | + const dv = new DataView(buf); |
| 77 | + dv.setUint8(0, 65); |
| 78 | + dv.setUint8(1, 66); |
| 79 | + dv.setUint8(2, 67); |
| 80 | + yield dv; |
| 81 | + } |
| 82 | + const data = bytesSync(fromSync(gen())); |
| 83 | + assert.deepStrictEqual(data, new Uint8Array([65, 66, 67])); |
| 84 | +} |
| 85 | + |
| 86 | +// DataView yielded from an async generator → normalizeAsyncValue path |
| 87 | +async function testFromAsyncDataViewInGenerator() { |
| 88 | + async function* gen() { |
| 89 | + const buf = new ArrayBuffer(3); |
| 90 | + const dv = new DataView(buf); |
| 91 | + dv.setUint8(0, 68); |
| 92 | + dv.setUint8(1, 69); |
| 93 | + dv.setUint8(2, 70); |
| 94 | + yield dv; |
| 95 | + } |
| 96 | + const data = await bytes(from(gen())); |
| 97 | + assert.deepStrictEqual(data, new Uint8Array([68, 69, 70])); |
| 98 | +} |
| 99 | + |
| 100 | +// Int16Array yielded from generator → primitiveToUint8Array fallback |
| 101 | +async function testFromSyncInt16ArrayInGenerator() { |
| 102 | + function* gen() { |
| 103 | + yield new Int16Array([0x0102, 0x0304]); |
| 104 | + } |
| 105 | + const data = bytesSync(fromSync(gen())); |
| 106 | + assert.strictEqual(data.byteLength, 4); // 2 int16 = 4 bytes |
| 107 | +} |
| 108 | + |
| 109 | +// Float64Array as top-level input to from() |
| 110 | +async function testFromFloat64Array() { |
| 111 | + const f64 = new Float64Array([1.0]); |
| 112 | + const batches = []; |
| 113 | + for await (const batch of from(f64)) { |
| 114 | + batches.push(batch); |
| 115 | + } |
| 116 | + assert.strictEqual(batches.length, 1); |
| 117 | + assert.strictEqual(batches[0][0].byteLength, 8); // 1 float64 = 8 bytes |
| 118 | +} |
| 119 | + |
| 120 | +// Sync generator yielding invalid type → ERR_INVALID_ARG_TYPE |
| 121 | +async function testFromSyncInvalidYield() { |
| 122 | + function* gen() { |
| 123 | + yield 42; // Not a valid stream value |
| 124 | + } |
| 125 | + assert.throws( |
| 126 | + () => { |
| 127 | + // eslint-disable-next-line no-unused-vars |
| 128 | + for (const batch of fromSync(gen())) { /* consume */ } |
| 129 | + }, |
| 130 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +Promise.all([ |
| 135 | + testFromSyncSubBatching(), |
| 136 | + testFromAsyncSubBatching(), |
| 137 | + testFromSubBatchingBoundary(), |
| 138 | + testFromSubBatchingBoundaryPlus1(), |
| 139 | + testFromSyncDataViewInGenerator(), |
| 140 | + testFromAsyncDataViewInGenerator(), |
| 141 | + testFromSyncInt16ArrayInGenerator(), |
| 142 | + testFromFloat64Array(), |
| 143 | + testFromSyncInvalidYield(), |
| 144 | +]).then(common.mustCall()); |
0 commit comments