diff --git a/lib/internal/streams/iter/utils.js b/lib/internal/streams/iter/utils.js index f52255fc445..ad71de638ab 100644 --- a/lib/internal/streams/iter/utils.js +++ b/lib/internal/streams/iter/utils.js @@ -376,6 +376,17 @@ function ByteViewSnapshot(value, buffer, sharedBufferView) { } ByteViewSnapshot.prototype = ObjectFreeze({ __proto__: null }); +// The snapshot of a non-empty view of a fixed-length, non-shared ArrayBuffer, +// the common case. Such a view can only change by the buffer being detached, +// which makes its byteLength 0, so its byteLength is all that needs to be +// recorded. `buffer` is null to tell it from a ByteViewSnapshot. +function FixedByteView(value, byteLength) { + this.value = value; + this.byteLength = byteLength; + this.buffer = null; +} +FixedByteView.prototype = ObjectFreeze({ __proto__: null }); + function BatchEntry(views, byteLength) { this.views = views; this.byteLength = byteLength; @@ -399,12 +410,25 @@ PendingWrite.prototype = ObjectFreeze({ __proto__: null }); function snapshotByteView(value) { const buffer = TypedArrayPrototypeGetBuffer(value); - const sharedBufferView = isSharedArrayBuffer(buffer) ? - new Uint8Array(buffer) : undefined; - return new ByteViewSnapshot(value, buffer, sharedBufferView); + if (isSharedArrayBuffer(buffer)) { + return new ByteViewSnapshot(value, buffer, new Uint8Array(buffer)); + } + const byteLength = TypedArrayPrototypeGetByteLength(value); + if (byteLength !== 0 && !ArrayBufferPrototypeGetResizable(buffer)) { + return new FixedByteView(value, byteLength); + } + return new ByteViewSnapshot(value, buffer, undefined); } function validateByteView(snapshot) { + if (snapshot.buffer === null) { + const { value } = snapshot; + if (TypedArrayPrototypeGetByteLength(value) !== snapshot.byteLength) { + throw new ERR_INVALID_STATE.TypeError( + 'Byte view was resized or detached after being accepted'); + } + return value; + } const { value, buffer,