Skip to content

Commit

Permalink
Merge pull request #2305 from murgatroid99/grpc-js_trailers_only_defa…
Browse files Browse the repository at this point in the history
…ult_headers

grpc-js: Add HTTP status and content type headers to trailers-only responses
  • Loading branch information
murgatroid99 committed Jan 3, 2023
2 parents 7f5cb7d + a1b9464 commit fbfa73c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/grpc-js/src/server-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ const deadlineUnitsToMs: DeadlineUnitIndexSignature = {
u: 0.001,
n: 0.000001,
};
const defaultResponseHeaders = {
const defaultCompressionHeaders = {
// TODO(cjihrig): Remove these encoding headers from the default response
// once compression is integrated.
[GRPC_ACCEPT_ENCODING_HEADER]: 'identity,deflate,gzip',
[GRPC_ENCODING_HEADER]: 'identity',
}
const defaultResponseHeaders = {
[http2.constants.HTTP2_HEADER_STATUS]: http2.constants.HTTP_STATUS_OK,
[http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'application/grpc+proto',
};
Expand Down Expand Up @@ -500,7 +502,7 @@ export class Http2ServerCallStream<
this.metadataSent = true;
const custom = customMetadata ? customMetadata.toHttp2Headers() : null;
// TODO(cjihrig): Include compression headers.
const headers = { ...defaultResponseHeaders, ...custom };
const headers = { ...defaultResponseHeaders, ...defaultCompressionHeaders, ...custom };
this.stream.respond(headers, defaultResponseOptions);
}

Expand Down Expand Up @@ -725,9 +727,11 @@ export class Http2ServerCallStream<
this.stream.end();
}
} else {
// Trailers-only response
const trailersToSend = {
[GRPC_STATUS_HEADER]: statusObj.code,
[GRPC_MESSAGE_HEADER]: encodeURI(statusObj.details),
...defaultResponseHeaders,
...statusObj.metadata?.toHttp2Headers(),
};
this.stream.respond(trailersToSend, {endStream: true});
Expand Down

0 comments on commit fbfa73c

Please sign in to comment.