Skip to content

Commit

Permalink
Fix loop guard on streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Nov 18, 2023
1 parent 1ba379e commit 6e99a5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/seroval/src/core/context/parser/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
createAsyncIteratorFactoryInstanceNode(
this.parseAsyncIteratorFactory(),
this.parse(
asyncIteratorToReadableStream(properties as unknown as AsyncIterable<unknown>),
asyncIteratorToReadableStream(
properties as unknown as AsyncIterable<unknown>,
this,
),
),
),
);
Expand Down
6 changes: 5 additions & 1 deletion packages/seroval/src/core/utils/iterator-to-sequence.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type BaseStreamParserContext from '../context/parser/stream';

/* eslint-disable no-await-in-loop */
export interface Sequence {
v: unknown[];
Expand Down Expand Up @@ -124,11 +126,12 @@ export function sequenceToAsyncIterator<T>(

export function asyncIteratorToReadableStream<T>(
source: AsyncIterable<T>,
parser: BaseStreamParserContext,
): ReadableStream<unknown> {
return new ReadableStream({
async start(controller): Promise<void> {
const iterator = source[Symbol.asyncIterator]();
while (true) {
while (parser.isAlive()) {
try {
const result = await iterator.next();
controller.enqueue([result.done ? 2 : 0, result.value]);
Expand All @@ -140,6 +143,7 @@ export function asyncIteratorToReadableStream<T>(
controller.enqueue([1, error]);
}
}
controller.close();
},
});
}
Expand Down

0 comments on commit 6e99a5b

Please sign in to comment.