| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 81a2194 commit 2266a4b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -102,6 +102,7 @@ const { | |||
| 102 | 102 | extractHighWaterMark, | |
| 103 | 103 | extractSizeAlgorithm, | |
| 104 | 104 | lazyTransfer, | |
| 105 | + isViewedArrayBufferDetached, | ||
| 105 | 106 | isBrandCheck, | |
| 106 | 107 | resetQueue, | |
| 107 | 108 | setPromiseHandled, | |
@@ -681,6 +682,10 @@ class ReadableStreamBYOBRequest { | |||
| 681 | 682 | 'This BYOB request has been invalidated'); | |
| 682 | 683 | } | |
| 683 | 684 | ||
| 685 | + if (isViewedArrayBufferDetached(view)) { | ||
| 686 | + throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached'); | ||
| 687 | + } | ||
| 688 | + | ||
| 684 | 689 | readableByteStreamControllerRespondWithNewView(controller, view); | |
| 685 | 690 | } | |
| 686 | 691 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ const { | |||
| 14 | 14 | PromiseReject, | |
| 15 | 15 | ReflectGet, | |
| 16 | 16 | Symbol, | |
| 17 | + Uint8Array, | ||
| 17 | 18 | } = primordials; | |
| 18 | 19 | ||
| 19 | 20 | const { | |
@@ -128,6 +129,24 @@ function transferArrayBuffer(buffer) { | |||
| 128 | 129 | return res; | |
| 129 | 130 | } | |
| 130 | 131 | ||
| 132 | + function isArrayBufferDetached(buffer) { | ||
| 133 | + if (ArrayBufferGetByteLength(buffer) === 0) { | ||
| 134 | + try { | ||
| 135 | + new Uint8Array(buffer); | ||
| 136 | + } catch { | ||
| 137 | + return true; | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + return false; | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + function isViewedArrayBufferDetached(view) { | ||
| 144 | + return ( | ||
| 145 | + ArrayBufferViewGetByteLength(view) === 0 && | ||
| 146 | + isArrayBufferDetached(ArrayBufferViewGetBuffer(view)) | ||
| 147 | + ); | ||
| 148 | + } | ||
| 149 | + | ||
| 131 | 150 | function dequeueValue(controller) { | |
| 132 | 151 | assert(controller[kState].queue !== undefined); | |
| 133 | 152 | assert(controller[kState].queueTotalSize !== undefined); | |
@@ -225,6 +244,7 @@ module.exports = { | |||
| 225 | 244 | lazyTransfer, | |
| 226 | 245 | isBrandCheck, | |
| 227 | 246 | isPromisePending, | |
| 247 | + isViewedArrayBufferDetached, | ||
| 228 | 248 | peekQueueValue, | |
| 229 | 249 | resetQueue, | |
| 230 | 250 | setPromiseHandled, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,54 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('node:assert'); | ||
| 5 | + | ||
| 6 | + let pass = 0; | ||
| 7 | + | ||
| 8 | + { | ||
| 9 | + // ReadableStream with byte source: respondWithNewView() throws if the | ||
| 10 | + // supplied view's buffer has a different length (in the closed state) | ||
| 11 | + const stream = new ReadableStream({ | ||
| 12 | + pull: common.mustCall(async (c) => { | ||
| 13 | + const view = new Uint8Array(new ArrayBuffer(10), 0, 0); | ||
| 14 | + | ||
| 15 | + c.close(); | ||
| 16 | + | ||
| 17 | + assert.throws(() => c.byobRequest.respondWithNewView(view), { | ||
| 18 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 19 | + name: 'RangeError', | ||
| 20 | + }); | ||
| 21 | + pass++; | ||
| 22 | + }), | ||
| 23 | + type: 'bytes', | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + const reader = stream.getReader({ mode: 'byob' }); | ||
| 27 | + reader.read(new Uint8Array([4, 5, 6])); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + { | ||
| 31 | + // ReadableStream with byte source: respondWithNewView() throws if the | ||
| 32 | + // supplied view's buffer has been detached (in the closed state) | ||
| 33 | + const stream = new ReadableStream({ | ||
| 34 | + pull: common.mustCall((c) => { | ||
| 35 | + c.close(); | ||
| 36 | + | ||
| 37 | + // Detach it by reading into it | ||
| 38 | + const view = new Uint8Array([1, 2, 3]); | ||
| 39 | + reader.read(view); | ||
| 40 | + | ||
| 41 | + assert.throws(() => c.byobRequest.respondWithNewView(view), { | ||
| 42 | + code: 'ERR_INVALID_STATE', | ||
| 43 | + name: 'TypeError', | ||
| 44 | + }); | ||
| 45 | + pass++; | ||
| 46 | + }), | ||
| 47 | + type: 'bytes', | ||
| 48 | + }); | ||
| 49 | + | ||
| 50 | + const reader = stream.getReader({ mode: 'byob' }); | ||
| 51 | + reader.read(new Uint8Array([4, 5, 6])); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + process.on('exit', () => assert.strictEqual(pass, 2)); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments