Skip to content

Commit

Permalink
grpc-js: add await/async on method that return promise
Browse files Browse the repository at this point in the history
add await/async on method that return promise to ensure that the order of message (and of the end of stream) are preserved
  • Loading branch information
phoenix741 authored and murgatroid99 committed Mar 6, 2023
1 parent 07288b3 commit c23c67c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/grpc-js/src/server-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,10 @@ export class Http2ServerCallStream<

let pushedEnd = false;

const maybePushEnd = () => {
const maybePushEnd = async () => {
if (!pushedEnd && readsDone && !pendingMessageProcessing) {
pushedEnd = true;
this.pushOrBufferMessage(readable, null);
await this.pushOrBufferMessage(readable, null);
}
};

Expand Down Expand Up @@ -848,16 +848,16 @@ export class Http2ServerCallStream<
// Just return early
if (!decompressedMessage) return;

this.pushOrBufferMessage(readable, decompressedMessage);
await this.pushOrBufferMessage(readable, decompressedMessage);
}
pendingMessageProcessing = false;
this.stream.resume();
maybePushEnd();
await maybePushEnd();
});

this.stream.once('end', () => {
this.stream.once('end', async () => {
readsDone = true;
maybePushEnd();
await maybePushEnd();
});
}

Expand All @@ -881,16 +881,16 @@ export class Http2ServerCallStream<
return this.canPush;
}

private pushOrBufferMessage(
private async pushOrBufferMessage(
readable:
| ServerReadableStream<RequestType, ResponseType>
| ServerDuplexStream<RequestType, ResponseType>,
messageBytes: Buffer | null
): void {
): Promise<void> {
if (this.isPushPending) {
this.bufferedMessages.push(messageBytes);
} else {
this.pushMessage(readable, messageBytes);
await this.pushMessage(readable, messageBytes);
}
}

Expand Down Expand Up @@ -943,7 +943,7 @@ export class Http2ServerCallStream<
this.isPushPending = false;

if (this.bufferedMessages.length > 0) {
this.pushMessage(
await this.pushMessage(
readable,
this.bufferedMessages.shift() as Buffer | null
);
Expand Down

0 comments on commit c23c67c

Please sign in to comment.