GeezerVison is showing github.com — stream: ensure from() observes returned rejecting promise correctly · nodejs/node@3ad1338
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/from.js +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ const { | 15 | 15 | DataViewPrototypeGetByteLength, | 16 | 16 | DataViewPrototypeGetByteOffset, | 17 | 17 | FunctionPrototypeCall, | | 18 | + PromisePrototypeThen, | 18 | 19 | SymbolAsyncIterator, | 19 | 20 | SymbolIterator, | 20 | 21 | TypedArrayPrototypeGetBuffer, | @@ -23,6 +24,8 @@ const { | 23 | 24 | Uint8Array, | 24 | 25 | } = primordials; | 25 | 26 |
| | 27 | +const { markPromiseAsHandled } = internalBinding('util'); | | 28 | + | 26 | 29 | const { | 27 | 30 | codes: { | 28 | 31 | ERR_INVALID_ARG_TYPE, | @@ -594,7 +597,11 @@ function from(input) { | 594 | 597 | // Check toAsyncStreamable protocol (takes precedence over toStreamable and | 595 | 598 | // iteration protocols) | 596 | 599 | if (typeof input[toAsyncStreamable] === 'function') { | 597 | | - const result = input[toAsyncStreamable](); | | 600 | + let result = input[toAsyncStreamable](); | | 601 | + if (isPromise(result)) { | | 602 | + result = PromisePrototypeThen(result, undefined, undefined); | | 603 | + markPromiseAsHandled(result); | | 604 | + } | 598 | 605 | // Synchronous validated source (e.g. Readable batched iterator) | 599 | 606 | if (result?.[kValidatedSource]) { | 600 | 607 | return result; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-from-async.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -235,6 +235,20 @@ async function testFromTopLevelProtocolOverIterator() { | 235 | 235 | assert.strictEqual(result, 'from-protocol'); | 236 | 236 | } | 237 | 237 |
| | 238 | +async function testFromHandlesProtocolRejectionUntilIteration() { | | 239 | + const reason = new Error('protocol failed'); | | 240 | + const iterable = from({ | | 241 | + [Symbol.for('Stream.toAsyncStreamable')]: common.mustCall( | | 242 | + () => Promise.reject(reason)), | | 243 | + }); | | 244 | + | | 245 | + await new Promise(setImmediate); | | 246 | + await assert.rejects( | | 247 | + iterable[Symbol.asyncIterator]().next(), | | 248 | + (error) => error === reason, | | 249 | + ); | | 250 | +} | | 251 | + | 238 | 252 | // DataView input should be converted to Uint8Array (zero-copy) | 239 | 253 | async function testFromDataView() { | 240 | 254 | const buf = new ArrayBuffer(5); | @@ -279,5 +293,6 @@ Promise.all([ | 279 | 293 | testFromTopLevelToStreamable(), | 280 | 294 | testFromTopLevelAsyncPrecedence(), | 281 | 295 | testFromTopLevelProtocolOverIterator(), | | 296 | + testFromHandlesProtocolRejectionUntilIteration(), | 282 | 297 | testFromDataView(), | 283 | 298 | ]).then(common.mustCall()); |
|