Skip to content

Commit 013a1e4

Browse files
committed
stream: simplify readableStreamFromIterable
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent e6ef477 commit 013a1e4

File tree

2 files changed

+23
-92
lines changed

2 files changed

+23
-92
lines changed

lib/internal/webstreams/readablestream.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
Symbol,
2525
SymbolAsyncIterator,
2626
SymbolDispose,
27+
SymbolIterator,
2728
SymbolToStringTag,
2829
TypedArrayPrototypeGetLength,
2930
Uint8Array,
@@ -32,6 +33,7 @@ const {
3233
const {
3334
AbortError,
3435
codes: {
36+
ERR_ARG_NOT_ITERABLE,
3537
ERR_ILLEGAL_CONSTRUCTOR,
3638
ERR_INVALID_ARG_TYPE,
3739
ERR_INVALID_ARG_VALUE,
@@ -110,8 +112,6 @@ const {
110112
nonOpCancel,
111113
nonOpPull,
112114
nonOpStart,
113-
getIterator,
114-
iteratorNext,
115115
kType,
116116
kState,
117117
} = require('internal/webstreams/util');
@@ -1341,41 +1341,36 @@ function createReadableStreamState() {
13411341

13421342
function readableStreamFromIterable(iterable) {
13431343
let stream;
1344-
const iteratorRecord = getIterator(iterable, 'async');
1345-
1344+
const iteratorGetter = iterable[SymbolAsyncIterator] ?? iterable[SymbolIterator];
1345+
if (iteratorGetter == null || typeof iteratorGetter !== 'function') {
1346+
throw new ERR_ARG_NOT_ITERABLE(iterable);
1347+
}
1348+
const iterator = FunctionPrototypeCall(iteratorGetter, iterable);
13461349
const startAlgorithm = nonOpStart;
13471350

13481351
async function pullAlgorithm() {
1349-
const nextResult = iteratorNext(iteratorRecord);
1350-
const nextPromise = PromiseResolve(nextResult);
1351-
return PromisePrototypeThen(nextPromise, (iterResult) => {
1352-
if (typeof iterResult !== 'object' || iterResult === null) {
1353-
throw new ERR_INVALID_STATE.TypeError(
1354-
'The promise returned by the iterator.next() method must fulfill with an object');
1355-
}
1356-
if (iterResult.done) {
1357-
readableStreamDefaultControllerClose(stream[kState].controller);
1358-
} else {
1359-
readableStreamDefaultControllerEnqueue(stream[kState].controller, iterResult.value);
1360-
}
1361-
});
1352+
const iterResult = await iterator.next();
1353+
if (typeof iterResult !== 'object' || iterResult === null) {
1354+
throw new ERR_INVALID_STATE.TypeError(
1355+
'The promise returned by the iterator.next() method must fulfill with an object');
1356+
}
1357+
if (iterResult.done) {
1358+
readableStreamDefaultControllerClose(stream[kState].controller);
1359+
} else {
1360+
readableStreamDefaultControllerEnqueue(stream[kState].controller, await iterResult.value);
1361+
}
13621362
}
13631363

13641364
async function cancelAlgorithm(reason) {
1365-
const iterator = iteratorRecord.iterator;
13661365
const returnMethod = iterator.return;
13671366
if (returnMethod === undefined) {
1368-
return PromiseResolve();
1367+
return;
1368+
}
1369+
const iterResult = await FunctionPrototypeCall(returnMethod, iterator, reason);
1370+
if (typeof iterResult !== 'object' || iterResult === null) {
1371+
throw new ERR_INVALID_STATE.TypeError(
1372+
'The promise returned by the iterator.return() method must fulfill with an object');
13691373
}
1370-
const returnResult = FunctionPrototypeCall(returnMethod, iterator, reason);
1371-
const returnPromise = PromiseResolve(returnResult);
1372-
return PromisePrototypeThen(returnPromise, (iterResult) => {
1373-
if (typeof iterResult !== 'object' || iterResult === null) {
1374-
throw new ERR_INVALID_STATE.TypeError(
1375-
'The promise returned by the iterator.return() method must fulfill with an object');
1376-
}
1377-
return undefined;
1378-
});
13791374
}
13801375

13811376
stream = createReadableStream(

lib/internal/webstreams/util.js

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ const {
1313
PromisePrototypeThen,
1414
ReflectGet,
1515
Symbol,
16-
SymbolAsyncIterator,
17-
SymbolIterator,
1816
Uint8Array,
1917
} = primordials;
2018

2119
const {
2220
codes: {
23-
ERR_ARG_NOT_ITERABLE,
2421
ERR_INVALID_ARG_VALUE,
25-
ERR_INVALID_STATE,
2622
},
2723
} = require('internal/errors');
2824

@@ -208,64 +204,6 @@ function lazyTransfer() {
208204
return transfer;
209205
}
210206

211-
function createAsyncFromSyncIterator(syncIteratorRecord) {
212-
const syncIterable = {
213-
[SymbolIterator]: () => syncIteratorRecord.iterator,
214-
};
215-
216-
const asyncIterator = (async function* () {
217-
return yield* syncIterable;
218-
}());
219-
220-
const nextMethod = asyncIterator.next;
221-
return { iterator: asyncIterator, nextMethod, done: false };
222-
}
223-
224-
// Refs: https://tc39.es/ecma262/#sec-getiterator
225-
function getIterator(obj, kind = 'sync', method) {
226-
if (method === undefined) {
227-
if (kind === 'async') {
228-
method = obj[SymbolAsyncIterator];
229-
if (method == null) {
230-
const syncMethod = obj[SymbolIterator];
231-
232-
if (syncMethod === undefined) {
233-
throw new ERR_ARG_NOT_ITERABLE(obj);
234-
}
235-
236-
const syncIteratorRecord = getIterator(obj, 'sync', syncMethod);
237-
return createAsyncFromSyncIterator(syncIteratorRecord);
238-
}
239-
} else {
240-
method = obj[SymbolIterator];
241-
}
242-
}
243-
244-
if (method === undefined) {
245-
throw new ERR_ARG_NOT_ITERABLE(obj);
246-
}
247-
248-
const iterator = FunctionPrototypeCall(method, obj);
249-
if (typeof iterator !== 'object' || iterator === null) {
250-
throw new ERR_INVALID_STATE.TypeError('The iterator method must return an object');
251-
}
252-
const nextMethod = iterator.next;
253-
return { iterator, nextMethod, done: false };
254-
}
255-
256-
function iteratorNext(iteratorRecord, value) {
257-
let result;
258-
if (value === undefined) {
259-
result = FunctionPrototypeCall(iteratorRecord.nextMethod, iteratorRecord.iterator);
260-
} else {
261-
result = FunctionPrototypeCall(iteratorRecord.nextMethod, iteratorRecord.iterator, [value]);
262-
}
263-
if (typeof result !== 'object' || result === null) {
264-
throw new ERR_INVALID_STATE.TypeError('The iterator.next() method must return an object');
265-
}
266-
return result;
267-
}
268-
269207
module.exports = {
270208
ArrayBufferViewGetBuffer,
271209
ArrayBufferViewGetByteLength,
@@ -292,8 +230,6 @@ module.exports = {
292230
nonOpPull,
293231
nonOpStart,
294232
nonOpWrite,
295-
getIterator,
296-
iteratorNext,
297233
kType,
298234
kState,
299235
};

0 commit comments

Comments
 (0)