| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8461d18 commit 3f8b446
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -484,15 +484,20 @@ function fromSync(input) { | |||
| 484 | 484 | return fromSync(input[toStreamable]()); | |
| 485 | 485 | } | |
| 486 | 486 | ||
| 487 | - // Reject explicit async inputs | ||
| 488 | - if (isAsyncIterable(input)) { | ||
| 487 | + const isIterable = isSyncIterable(input); | ||
| 488 | + | ||
| 489 | + // Reject explicit async-only inputs | ||
| 490 | + if (!isIterable && isAsyncIterable(input)) { | ||
| 489 | 491 | throw new ERR_INVALID_ARG_TYPE( | |
| 490 | 492 | 'input', | |
| 491 | 493 | 'a synchronous input (not AsyncIterable)', | |
| 492 | 494 | input, | |
| 493 | 495 | ); | |
| 494 | 496 | } | |
| 495 | - if (typeof input === 'object' && input !== null && typeof input.then === 'function') { | ||
| 497 | + if (!isIterable && | ||
| 498 | + typeof input === 'object' && | ||
| 499 | + input !== null && | ||
| 500 | + typeof input.then === 'function') { | ||
| 496 | 501 | throw new ERR_INVALID_ARG_TYPE( | |
| 497 | 502 | 'input', | |
| 498 | 503 | 'a synchronous input (not Promise)', | |
@@ -501,7 +506,7 @@ function fromSync(input) { | |||
| 501 | 506 | } | |
| 502 | 507 | ||
| 503 | 508 | // Must be a SyncStreamable | |
| 504 | - if (!isSyncIterable(input)) { | ||
| 509 | + if (!isIterable) { | ||
| 505 | 510 | throw new ERR_INVALID_ARG_TYPE( | |
| 506 | 511 | 'input', | |
| 507 | 512 | ['string', 'ArrayBuffer', 'ArrayBufferView', 'Iterable', 'toStreamable'], | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | 5 | const assert = require('assert'); | |
| 6 | - const { fromSync } = require('stream/iter'); | ||
| 6 | + const { fromSync, textSync } = require('stream/iter'); | ||
| 7 | 7 | ||
| 8 | 8 | function testFromSyncString() { | |
| 9 | 9 | // String input should be UTF-8 encoded | |
@@ -186,6 +186,30 @@ function testFromSyncRejectsAsyncIterable() { | |||
| 186 | 186 | assert.throws(() => fromSync(gen()), { code: 'ERR_INVALID_ARG_TYPE' }); | |
| 187 | 187 | } | |
| 188 | 188 | ||
| 189 | + function testFromSyncPrefersIteratorForDualIterable() { | ||
| 190 | + const input = { | ||
| 191 | + *[Symbol.iterator]() { | ||
| 192 | + yield new TextEncoder().encode('sync'); | ||
| 193 | + }, | ||
| 194 | + async *[Symbol.asyncIterator]() { | ||
| 195 | + yield new TextEncoder().encode('async'); | ||
| 196 | + }, | ||
| 197 | + }; | ||
| 198 | + | ||
| 199 | + assert.strictEqual(textSync(fromSync(input)), 'sync'); | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + function testFromSyncPrefersIteratorForThenableIterable() { | ||
| 203 | + const input = { | ||
| 204 | + then() {}, | ||
| 205 | + *[Symbol.iterator]() { | ||
| 206 | + yield new TextEncoder().encode('sync'); | ||
| 207 | + }, | ||
| 208 | + }; | ||
| 209 | + | ||
| 210 | + assert.strictEqual(textSync(fromSync(input)), 'sync'); | ||
| 211 | + } | ||
| 212 | + | ||
| 189 | 213 | // Promise rejected | |
| 190 | 214 | function testFromSyncRejectsPromise() { | |
| 191 | 215 | assert.throws(() => fromSync(Promise.resolve('hello')), | |
@@ -232,6 +256,8 @@ Promise.all([ | |||
| 232 | 256 | testFromSyncTopLevelProtocolOverIterator(), | |
| 233 | 257 | testFromSyncIgnoresAsyncStreamable(), | |
| 234 | 258 | testFromSyncRejectsAsyncIterable(), | |
| 259 | + testFromSyncPrefersIteratorForDualIterable(), | ||
| 260 | + testFromSyncPrefersIteratorForThenableIterable(), | ||
| 235 | 261 | testFromSyncRejectsPromise(), | |
| 236 | 262 | testFromSyncDataView(), | |
| 237 | 263 | ]).then(common.mustCall()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments