Skip to content

Commit

Permalink
chore(eventstream-serde-node): use Readable.from for serializing (#4359)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jan 24, 2023
1 parent 4035e36 commit 2bc3759
Showing 1 changed file with 1 addition and 33 deletions.
34 changes: 1 addition & 33 deletions packages/eventstream-serde-node/src/EventStreamMarshaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,6 @@ export class EventStreamMarshaller {
}

serialize<T>(input: AsyncIterable<T>, serializer: (event: T) => Message): Readable {
const serializedIterable = this.universalMarshaller.serialize(input, serializer);
if (typeof Readable.from === "function") {
//reference: https://nodejs.org/dist/latest-v13.x/docs/api/stream.html#stream_new_stream_readable_options
return Readable.from(serializedIterable);
} else {
const iterator = serializedIterable[Symbol.asyncIterator]();
const serializedStream = new Readable({
autoDestroy: true,
objectMode: true,
async read() {
iterator
.next()
.then(({ done, value }) => {
if (done) {
this.push(null);
} else {
this.push(value);
}
})
.catch((err) => {
this.destroy(err);
});
},
});
//TODO: use 'autoDestroy' when targeting Node 11
serializedStream.on("error", () => {
serializedStream.destroy();
});
serializedStream.on("end", () => {
serializedStream.destroy();
});
return serializedStream;
}
return Readable.from(this.universalMarshaller.serialize(input, serializer));
}
}

0 comments on commit 2bc3759

Please sign in to comment.