-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
stream: fix isDetachedBuffer validations in ReadableStream
#44114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e5e32c0
50c7728
6f6ba3e
782c71a
e44effa
6fd3ea1
0cc34f4
fe816cd
58da0d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ const { | |
| } = internalBinding('buffer'); | ||
|
|
||
| const { | ||
| isArrayBuffer, | ||
| isArrayBufferView, | ||
| isPromise, | ||
| } = require('internal/util/types'); | ||
|
|
||
|
|
@@ -129,21 +131,32 @@ function transferArrayBuffer(buffer) { | |
| return res; | ||
| } | ||
|
|
||
| function isArrayBufferDetached(buffer) { | ||
| function isDetachedBuffer(buffer) { | ||
| if (!isArrayBuffer(buffer)) | ||
| throw new ERR_INVALID_ARG_TYPE('buffer', 'ArrayBuffer', buffer); | ||
|
||
| if (ArrayBufferGetByteLength(buffer) === 0) { | ||
LiviaMedeiros marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // TODO(daeyeon): Consider using C++ builtin to improve performance. | ||
| try { | ||
| new Uint8Array(buffer); | ||
| } catch { | ||
| } catch (error) { | ||
| assert(error.name === 'TypeError'); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function isViewedArrayBufferDetached(view) { | ||
| if (!isArrayBufferView(view)) { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| 'view', | ||
| ['Buffer', 'TypedArray', 'DataView'], | ||
| view, | ||
| ); | ||
| } | ||
| return ( | ||
| ArrayBufferViewGetByteLength(view) === 0 && | ||
| isArrayBufferDetached(ArrayBufferViewGetBuffer(view)) | ||
| isDetachedBuffer(ArrayBufferViewGetBuffer(view)) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -243,6 +256,7 @@ module.exports = { | |
| extractSizeAlgorithm, | ||
| lazyTransfer, | ||
| isBrandCheck, | ||
| isDetachedBuffer, | ||
| isPromisePending, | ||
| isViewedArrayBufferDetached, | ||
| peekQueueValue, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,4 +51,48 @@ let pass = 0; | |||||||||||||||||||||||||||||||||||||
| reader.read(new Uint8Array([4, 5, 6])); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| const stream = new ReadableStream({ | ||||||||||||||||||||||||||||||||||||||
| start(c) { | ||||||||||||||||||||||||||||||||||||||
| c.enqueue(new Uint8Array([1, 2, 3])); | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| type: 'bytes', | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| const reader = stream.getReader({ mode: 'byob' }); | ||||||||||||||||||||||||||||||||||||||
| const view = new Uint8Array(); | ||||||||||||||||||||||||||||||||||||||
| reader | ||||||||||||||||||||||||||||||||||||||
| .read(view) | ||||||||||||||||||||||||||||||||||||||
| .then(common.mustNotCall()) | ||||||||||||||||||||||||||||||||||||||
| .catch( | ||||||||||||||||||||||||||||||||||||||
| common.mustCall( | ||||||||||||||||||||||||||||||||||||||
| common.expectsError({ | ||||||||||||||||||||||||||||||||||||||
| code: 'ERR_INVALID_STATE', | ||||||||||||||||||||||||||||||||||||||
| name: 'TypeError', | ||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| reader | |
| .read(view) | |
| .then(common.mustNotCall()) | |
| .catch( | |
| common.mustCall( | |
| common.expectsError({ | |
| code: 'ERR_INVALID_STATE', | |
| name: 'TypeError', | |
| }), | |
| ), | |
| ); | |
| assert.rejects( | |
| reader.read(view), | |
| { | |
| code: 'ERR_INVALID_STATE', | |
| name: 'TypeError', | |
| } | |
| ).then(common.mustCall()); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| reader | |
| .read(view) | |
| .then(common.mustNotCall()) | |
| .catch( | |
| common.mustCall( | |
| common.expectsError({ | |
| code: 'ERR_INVALID_STATE', | |
| name: 'TypeError', | |
| }), | |
| ), | |
| ); | |
| assert.rejects( | |
| reader.read(view), | |
| { | |
| code: 'ERR_INVALID_STATE', | |
| name: 'TypeError', | |
| } | |
| ).then(common.mustCall()); |
Uh oh!
There was an error while loading. Please reload this page.