File
Blob: src/workerd/api/tests/byob-reader-resize-pending-read-test.js
| 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 | import { strictEqual, ok } from 'node:assert'; |
| 5 | |
| 6 | export const ByobReaderResizePendingRead = { |
| 7 | async test() { |
| 8 | const ts = new IdentityTransformStream(); |
| 9 | const reader = ts.readable.getReader({ mode: 'byob' }); |
| 10 | const writer = ts.writable.getWriter(); |
| 11 | |
| 12 | const buffer = new ArrayBuffer(8192, { maxByteLength: 16384 }); |
| 13 | const view = new Uint8Array(buffer, 4096, 1024); |
| 14 | |
| 15 | const readPromise = reader.read(view); |
| 16 | buffer.resize(2048); |
| 17 | |
| 18 | await writer.write(new Uint8Array(100).fill(0x41)); |
| 19 | const result = await readPromise; |
| 20 | |
| 21 | strictEqual(result.done, false); |
| 22 | strictEqual(result.value.byteLength, 0); |
| 23 | |
| 24 | reader.releaseLock(); |
| 25 | writer.releaseLock(); |
| 26 | }, |
| 27 | }; |
| 28 | |
| 29 | export const ByobReaderResizableBufferTempLifetime = { |
| 30 | async test() { |
| 31 | const data = 'A'.repeat(1024); |
| 32 | const response = await fetch('data:text/plain;base64,' + btoa(data)); |
| 33 | const reader = response.body.getReader({ mode: 'byob' }); |
| 34 | |
| 35 | const buffer = new ArrayBuffer(512, { maxByteLength: 1024 }); |
| 36 | const view = new Uint8Array(buffer); |
| 37 | |
| 38 | const result = await reader.read(view); |
| 39 | reader.releaseLock(); |
| 40 | |
| 41 | strictEqual(result.done, false); |
| 42 | ok(result.value.byteLength > 0); |
| 43 | const text = new TextDecoder().decode(result.value); |
| 44 | strictEqual(text, 'A'.repeat(result.value.byteLength)); |
| 45 | }, |
| 46 | }; |