|
| 1 | +// Flags: --experimental-stream-iter |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { |
| 7 | + from, |
| 8 | + fromSync, |
| 9 | + bytes, |
| 10 | + bytesSync, |
| 11 | + text, |
| 12 | + textSync, |
| 13 | + arrayBuffer, |
| 14 | + arrayBufferSync, |
| 15 | + pipeTo, |
| 16 | + pipeToSync, |
| 17 | + pull, |
| 18 | + pullSync, |
| 19 | +} = require('stream/iter'); |
| 20 | + |
| 21 | +// ============================================================================= |
| 22 | +// from() / fromSync() with SharedArrayBuffer |
| 23 | +// ============================================================================= |
| 24 | + |
| 25 | +function testFromSyncSAB() { |
| 26 | + const sab = new SharedArrayBuffer(4); |
| 27 | + new Uint8Array(sab).set([10, 20, 30, 40]); |
| 28 | + const batches = []; |
| 29 | + for (const batch of fromSync(sab)) { |
| 30 | + batches.push(batch); |
| 31 | + } |
| 32 | + assert.strictEqual(batches.length, 1); |
| 33 | + assert.deepStrictEqual(batches[0][0], new Uint8Array([10, 20, 30, 40])); |
| 34 | +} |
| 35 | + |
| 36 | +async function testFromAsyncSAB() { |
| 37 | + const sab = new SharedArrayBuffer(4); |
| 38 | + new Uint8Array(sab).set([10, 20, 30, 40]); |
| 39 | + const batches = []; |
| 40 | + for await (const batch of from(sab)) { |
| 41 | + batches.push(batch); |
| 42 | + } |
| 43 | + assert.strictEqual(batches.length, 1); |
| 44 | + assert.deepStrictEqual(batches[0][0], new Uint8Array([10, 20, 30, 40])); |
| 45 | +} |
| 46 | + |
| 47 | +// ============================================================================= |
| 48 | +// Consumers with SAB-backed source |
| 49 | +// ============================================================================= |
| 50 | + |
| 51 | +function testBytesSyncSAB() { |
| 52 | + const sab = new SharedArrayBuffer(5); |
| 53 | + new Uint8Array(sab).set([104, 101, 108, 108, 111]); // 'hello' |
| 54 | + const data = bytesSync(fromSync(sab)); |
| 55 | + assert.deepStrictEqual(data, new Uint8Array([104, 101, 108, 108, 111])); |
| 56 | +} |
| 57 | + |
| 58 | +async function testBytesAsyncSAB() { |
| 59 | + const sab = new SharedArrayBuffer(5); |
| 60 | + new Uint8Array(sab).set([104, 101, 108, 108, 111]); // 'hello' |
| 61 | + const data = await bytes(from(sab)); |
| 62 | + assert.deepStrictEqual(data, new Uint8Array([104, 101, 108, 108, 111])); |
| 63 | +} |
| 64 | + |
| 65 | +function testTextSyncSAB() { |
| 66 | + const sab = new SharedArrayBuffer(5); |
| 67 | + new Uint8Array(sab).set([104, 101, 108, 108, 111]); // 'hello' |
| 68 | + const result = textSync(fromSync(sab)); |
| 69 | + assert.strictEqual(result, 'hello'); |
| 70 | +} |
| 71 | + |
| 72 | +async function testTextAsyncSAB() { |
| 73 | + const sab = new SharedArrayBuffer(5); |
| 74 | + new Uint8Array(sab).set([104, 101, 108, 108, 111]); // 'hello' |
| 75 | + const result = await text(from(sab)); |
| 76 | + assert.strictEqual(result, 'hello'); |
| 77 | +} |
| 78 | + |
| 79 | +function testArrayBufferSyncSAB() { |
| 80 | + const sab = new SharedArrayBuffer(4); |
| 81 | + new Uint8Array(sab).set([1, 2, 3, 4]); |
| 82 | + const result = arrayBufferSync(fromSync(sab)); |
| 83 | + assert.ok(result instanceof SharedArrayBuffer || result instanceof ArrayBuffer); |
| 84 | + assert.deepStrictEqual(new Uint8Array(result), new Uint8Array([1, 2, 3, 4])); |
| 85 | +} |
| 86 | + |
| 87 | +async function testArrayBufferAsyncSAB() { |
| 88 | + const sab = new SharedArrayBuffer(4); |
| 89 | + new Uint8Array(sab).set([1, 2, 3, 4]); |
| 90 | + const result = await arrayBuffer(from(sab)); |
| 91 | + assert.ok(result instanceof SharedArrayBuffer || result instanceof ArrayBuffer); |
| 92 | + assert.deepStrictEqual(new Uint8Array(result), new Uint8Array([1, 2, 3, 4])); |
| 93 | +} |
| 94 | + |
| 95 | +// ============================================================================= |
| 96 | +// pipeTo / pipeToSync with SAB source |
| 97 | +// ============================================================================= |
| 98 | + |
| 99 | +function testPipeToSyncSAB() { |
| 100 | + const sab = new SharedArrayBuffer(3); |
| 101 | + new Uint8Array(sab).set([65, 66, 67]); // 'ABC' |
| 102 | + const written = []; |
| 103 | + const writer = { |
| 104 | + writeSync(chunk) { written.push(chunk); return true; }, |
| 105 | + endSync() { return written.length; }, |
| 106 | + }; |
| 107 | + const totalBytes = pipeToSync(fromSync(sab), writer); |
| 108 | + assert.strictEqual(totalBytes, 3); |
| 109 | + assert.deepStrictEqual(written[0], new Uint8Array([65, 66, 67])); |
| 110 | +} |
| 111 | + |
| 112 | +async function testPipeToAsyncSAB() { |
| 113 | + const sab = new SharedArrayBuffer(3); |
| 114 | + new Uint8Array(sab).set([65, 66, 67]); // 'ABC' |
| 115 | + const written = []; |
| 116 | + const writer = { |
| 117 | + async write(chunk) { written.push(chunk); }, |
| 118 | + async end() { return written.length; }, |
| 119 | + }; |
| 120 | + await pipeTo(from(sab), writer); |
| 121 | + assert.deepStrictEqual(written[0], new Uint8Array([65, 66, 67])); |
| 122 | +} |
| 123 | + |
| 124 | +// ============================================================================= |
| 125 | +// pull / pullSync with SAB-yielding generator |
| 126 | +// ============================================================================= |
| 127 | + |
| 128 | +function testPullSyncSABChunks() { |
| 129 | + function* gen() { |
| 130 | + const sab = new SharedArrayBuffer(2); |
| 131 | + new Uint8Array(sab).set([1, 2]); |
| 132 | + yield [new Uint8Array(sab)]; |
| 133 | + } |
| 134 | + const batches = []; |
| 135 | + for (const batch of pullSync(gen())) { |
| 136 | + batches.push(batch); |
| 137 | + } |
| 138 | + assert.strictEqual(batches.length, 1); |
| 139 | + assert.deepStrictEqual(batches[0][0], new Uint8Array([1, 2])); |
| 140 | +} |
| 141 | + |
| 142 | +async function testPullAsyncSABChunks() { |
| 143 | + async function* gen() { |
| 144 | + const sab = new SharedArrayBuffer(2); |
| 145 | + new Uint8Array(sab).set([3, 4]); |
| 146 | + yield [new Uint8Array(sab)]; |
| 147 | + } |
| 148 | + const batches = []; |
| 149 | + for await (const batch of pull(gen())) { |
| 150 | + batches.push(batch); |
| 151 | + } |
| 152 | + assert.strictEqual(batches.length, 1); |
| 153 | + assert.deepStrictEqual(batches[0][0], new Uint8Array([3, 4])); |
| 154 | +} |
| 155 | + |
| 156 | +// ============================================================================= |
| 157 | +// Transform returning SAB |
| 158 | +// ============================================================================= |
| 159 | + |
| 160 | +async function testTransformReturningSAB() { |
| 161 | + function* source() { |
| 162 | + yield [new Uint8Array([1, 2, 3])]; |
| 163 | + } |
| 164 | + const transform = (chunks) => { |
| 165 | + if (chunks === null) return null; |
| 166 | + // Transform returns a Uint8Array backed by a SharedArrayBuffer |
| 167 | + const sab = new SharedArrayBuffer(chunks[0].length); |
| 168 | + new Uint8Array(sab).set(chunks[0]); |
| 169 | + return new Uint8Array(sab); |
| 170 | + }; |
| 171 | + const batches = []; |
| 172 | + for await (const batch of pull(source(), transform)) { |
| 173 | + batches.push(batch); |
| 174 | + } |
| 175 | + assert.strictEqual(batches.length, 1); |
| 176 | + assert.deepStrictEqual(batches[0][0], new Uint8Array([1, 2, 3])); |
| 177 | +} |
| 178 | + |
| 179 | +// ============================================================================= |
| 180 | + |
| 181 | +Promise.all([ |
| 182 | + testFromSyncSAB(), |
| 183 | + testFromAsyncSAB(), |
| 184 | + testBytesSyncSAB(), |
| 185 | + testBytesAsyncSAB(), |
| 186 | + testTextSyncSAB(), |
| 187 | + testTextAsyncSAB(), |
| 188 | + testArrayBufferSyncSAB(), |
| 189 | + testArrayBufferAsyncSAB(), |
| 190 | + testPipeToSyncSAB(), |
| 191 | + testPipeToAsyncSAB(), |
| 192 | + testPullSyncSABChunks(), |
| 193 | + testPullAsyncSABChunks(), |
| 194 | + testTransformReturningSAB(), |
| 195 | +]).then(common.mustCall()); |
0 commit comments