|
| 1 | +// Copyright (c) 2026 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | +// https://opensource.org/licenses/Apache-2.0 |
| 4 | + |
| 5 | +// Pipes carrying views over SPECIAL buffers: SharedArrayBuffer-backed |
| 6 | +// views (non-transferable by definition) and resizable-ArrayBuffer |
| 7 | +// views, written through native and JS-backed endpoints (migrated from |
| 8 | +// pipe-write-special-buffer-test.js, strengthened from length checks |
| 9 | +// to content verification). |
| 10 | + |
| 11 | +import { strictEqual, ok, rejects } from 'node:assert'; |
| 12 | +import { usingTsImpl } from 'which-impl'; |
| 13 | + |
| 14 | +function filledSabView(length, byte) { |
| 15 | + const sab = new SharedArrayBuffer(length); |
| 16 | + const view = new Uint8Array(sab); |
| 17 | + view.fill(byte); |
| 18 | + return view; |
| 19 | +} |
| 20 | + |
| 21 | +function streamOf(view) { |
| 22 | + return new ReadableStream({ |
| 23 | + start(controller) { |
| 24 | + controller.enqueue(view); |
| 25 | + controller.close(); |
| 26 | + }, |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +async function drainToBytes(readable) { |
| 31 | + const reader = readable.getReader(); |
| 32 | + const parts = []; |
| 33 | + let total = 0; |
| 34 | + for (;;) { |
| 35 | + const { value, done } = await reader.read(); |
| 36 | + if (done) break; |
| 37 | + parts.push(value); |
| 38 | + total += value.byteLength; |
| 39 | + } |
| 40 | + reader.releaseLock(); |
| 41 | + const out = new Uint8Array(total); |
| 42 | + let offset = 0; |
| 43 | + for (const part of parts) { |
| 44 | + out.set(part, offset); |
| 45 | + offset += part.byteLength; |
| 46 | + } |
| 47 | + return out; |
| 48 | +} |
| 49 | + |
| 50 | +function assertAllBytes(bytes, expectedLength, expectedByte) { |
| 51 | + strictEqual(bytes.byteLength, expectedLength); |
| 52 | + for (let i = 0; i < bytes.byteLength; i++) { |
| 53 | + if (bytes[i] !== expectedByte) { |
| 54 | + strictEqual(bytes[i], expectedByte, `byte ${i} corrupted`); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// A SAB-backed view piped through CompressionStream. DIVERGENCE: C++ |
| 60 | +// copies the shared bytes and round-trips them; the TypeScript |
| 61 | +// CompressionStream write path REJECTS SharedArrayBuffer-backed views |
| 62 | +// ('The provided value is not of type (ArrayBuffer or |
| 63 | +// ArrayBufferView)') even though its identity stream accepts them (see |
| 64 | +// the next test). The shared buffer is untouched either way. |
| 65 | +export const sabViewThroughCompressionRoundTrip = { |
| 66 | + async test() { |
| 67 | + const view = filledSabView(100, 0x41); |
| 68 | + const compressed = streamOf(view).pipeThrough( |
| 69 | + new CompressionStream('gzip') |
| 70 | + ); |
| 71 | + if (usingTsImpl) { |
| 72 | + await rejects( |
| 73 | + drainToBytes(compressed.pipeThrough(new DecompressionStream('gzip'))), |
| 74 | + { |
| 75 | + name: 'TypeError', |
| 76 | + message: |
| 77 | + 'The provided value is not of type (ArrayBuffer or ArrayBufferView)', |
| 78 | + } |
| 79 | + ); |
| 80 | + } else { |
| 81 | + const restored = await drainToBytes( |
| 82 | + compressed.pipeThrough(new DecompressionStream('gzip')) |
| 83 | + ); |
| 84 | + assertAllBytes(restored, 100, 0x41); |
| 85 | + } |
| 86 | + // The shared buffer itself must be untouched (it cannot be |
| 87 | + // detached). |
| 88 | + assertAllBytes(view, 100, 0x41); |
| 89 | + }, |
| 90 | +}; |
| 91 | + |
| 92 | +// A SAB-backed view piped into a native identity stream. |
| 93 | +export const sabViewThroughIdentityTransform = { |
| 94 | + async test() { |
| 95 | + const view = filledSabView(1024, 0x42); |
| 96 | + const its = new IdentityTransformStream(); |
| 97 | + const [bytes] = await Promise.all([ |
| 98 | + drainToBytes(its.readable), |
| 99 | + streamOf(view).pipeTo(its.writable), |
| 100 | + ]); |
| 101 | + assertAllBytes(bytes, 1024, 0x42); |
| 102 | + assertAllBytes(view, 1024, 0x42); |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +// A SAB-backed view through a JS-backed TransformStream into a JS |
| 107 | +// sink: the JS pipe path must carry the view without detaching its |
| 108 | +// (non-detachable) buffer. |
| 109 | +export const sabViewThroughJsPipeChain = { |
| 110 | + async test() { |
| 111 | + const view = filledSabView(512, 0x44); |
| 112 | + const received = []; |
| 113 | + await streamOf(view) |
| 114 | + .pipeThrough(new TransformStream()) |
| 115 | + .pipeTo( |
| 116 | + new WritableStream({ |
| 117 | + write(chunk) { |
| 118 | + received.push(chunk); |
| 119 | + }, |
| 120 | + }) |
| 121 | + ); |
| 122 | + strictEqual(received.length, 1); |
| 123 | + assertAllBytes(received[0], 512, 0x44); |
| 124 | + assertAllBytes(view, 512, 0x44); |
| 125 | + // The delivered chunk is the very view (no copy on the JS path). |
| 126 | + strictEqual(received[0], view); |
| 127 | + }, |
| 128 | +}; |
| 129 | + |
| 130 | +// A resizable-ArrayBuffer view piped into a native identity stream. |
| 131 | +export const resizableViewThroughIdentityTransform = { |
| 132 | + async test() { |
| 133 | + const buffer = new ArrayBuffer(1024, { maxByteLength: 2048 }); |
| 134 | + const view = new Uint8Array(buffer); |
| 135 | + view.fill(0x43); |
| 136 | + const its = new IdentityTransformStream(); |
| 137 | + const [bytes] = await Promise.all([ |
| 138 | + drainToBytes(its.readable), |
| 139 | + streamOf(view).pipeTo(its.writable), |
| 140 | + ]); |
| 141 | + assertAllBytes(bytes, 1024, 0x43); |
| 142 | + }, |
| 143 | +}; |
| 144 | + |
| 145 | +// A resizable-ArrayBuffer view through the JS pipe path: delivered |
| 146 | +// intact, and the buffer remains resizable afterwards. |
| 147 | +export const resizableViewThroughJsPipeChain = { |
| 148 | + async test() { |
| 149 | + const buffer = new ArrayBuffer(256, { maxByteLength: 512 }); |
| 150 | + const view = new Uint8Array(buffer); |
| 151 | + view.fill(0x45); |
| 152 | + const received = []; |
| 153 | + await streamOf(view) |
| 154 | + .pipeThrough(new TransformStream()) |
| 155 | + .pipeTo( |
| 156 | + new WritableStream({ |
| 157 | + write(chunk) { |
| 158 | + received.push(chunk); |
| 159 | + }, |
| 160 | + }) |
| 161 | + ); |
| 162 | + strictEqual(received.length, 1); |
| 163 | + assertAllBytes(received[0], 256, 0x45); |
| 164 | + ok(!buffer.detached); |
| 165 | + buffer.resize(512); // still resizable |
| 166 | + strictEqual(buffer.byteLength, 512); |
| 167 | + }, |
| 168 | +}; |
0 commit comments