Skip to content

Commit

Permalink
fix: return readable stream with async iterators properly (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored Mar 2, 2020
1 parent dde5a6f commit 76553cf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/eventstream-serde-node/src/EventStreamMarshaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
EventStreamMarshaller as IEventStreamMarshaller
} from "@aws-sdk/types";
import { Readable, pipeline } from "stream";
import { ReadabletoIterable } from "./utils";
import { readabletoIterable } from "./utils";
import { EventMessageChunkerStream } from "./EventMessageChunkerStream";
import { MessageUnmarshallerStream } from "./MessageUnmarshallerStream";
import { EventDeserializerStream } from "./EventDeserializerStream";
Expand Down Expand Up @@ -44,7 +44,11 @@ export class EventStreamMarshaller {
);
//should use stream[Symbol.asyncIterable] when the api is stable
//reference: https://nodejs.org/docs/latest-v11.x/api/stream.html#stream_readable_symbol_asynciterator
return ReadabletoIterable(eventDeserializerStream);
if (typeof eventDeserializerStream[Symbol.asyncIterator] === "function") {
// use the experimental feature if available.
return eventDeserializerStream;
}
return readabletoIterable(eventDeserializerStream);
}

serialize<T>(
Expand Down
6 changes: 1 addition & 5 deletions packages/eventstream-serde-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ export function getSignatureBinary(signature: string): Uint8Array {
* Reference: https://nodejs.org/docs/latest-v11.x/api/stream.html#stream_readable_symbol_asynciterator
*/

export async function* ReadabletoIterable<T>(
export async function* readabletoIterable<T>(
readStream: Readable
): AsyncIterable<T> {
if (typeof readStream[Symbol.asyncIterator] === "function") {
// use the experimental feature if available.
throw readStream;
}
let streamEnded = false;
let generationEnded = false;
const records = new Array<T>();
Expand Down

0 comments on commit 76553cf

Please sign in to comment.