Skip to content

Commit 4c8361d

Browse files
committed
fix: add handlerProtocol key explicitly in handler metadata
1 parent 2664c85 commit 4c8361d

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

packages/middleware-host-header/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export const hostHeaderMiddleware = <
3232
): BuildMiddleware<Input, Output> => next => async args => {
3333
if (!HttpRequest.isInstance(args.request)) return next(args);
3434
const { request } = args;
35-
const { metadata = [] } = options.requestHandler;
35+
const { handlerProtocol = "" } = options.requestHandler.metadata || {};
3636
//For H2 request, remove 'host' header and use ':authority' header instead
3737
//reference: https://nodejs.org/dist/latest-v13.x/docs/api/errors.html#ERR_HTTP2_INVALID_CONNECTION_HEADERS
38-
if (metadata.includes("h2") && !request.headers[":authority"]) {
38+
if (handlerProtocol.indexOf("h2") >= 0 && !request.headers[":authority"]) {
3939
delete request.headers["host"];
4040
request.headers[":authority"] = "";
4141
//non-H2 request and 'host' header is not set, set the 'host' header to request's hostname.

packages/node-http-handler/src/node-http-handler.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("NodeHttpHandler", () => {
4343
});
4444
it("has metadata", () => {
4545
const nodeHttpHandler = new NodeHttpHandler();
46-
expect(nodeHttpHandler.metadata).toContain("h1");
46+
expect(nodeHttpHandler.metadata.handlerProtocol).toContain("http/1.1");
4747
});
4848
it("can send http requests", async () => {
4949
const mockResponse = {

packages/node-http-handler/src/node-http-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export class NodeHttpHandler implements HttpHandler {
3333
private readonly httpsAgent: https.Agent;
3434
private readonly connectionTimeout?: number;
3535
private readonly socketTimeout?: number;
36-
public readonly metadata = ["h1"];
36+
// Node http handler is hard-coded to http/1.1: https://github.com/nodejs/node/blob/ff5664b83b89c55e4ab5d5f60068fb457f1f5872/lib/_http_server.js#L286
37+
public readonly metadata = { handlerProtocol: "http/1.1" };
3738

3839
constructor({
3940
connectionTimeout,

packages/node-http-handler/src/node-http2-handler.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("NodeHttp2Handler", () => {
4545
});
4646

4747
it("has metadata", () => {
48-
expect(nodeH2Handler.metadata).toContain("h2");
48+
expect(nodeH2Handler.metadata.handlerProtocol).toContain("h2");
4949
});
5050

5151
describe("connectionPool", () => {

packages/node-http-handler/src/node-http2-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface NodeHttp2Options {
2727

2828
export class NodeHttp2Handler implements HttpHandler {
2929
private readonly connectionPool: Map<string, ClientHttp2Session>;
30-
public readonly metadata = ["h2"];
30+
public readonly metadata = { handlerProtocol: "h2" };
3131

3232
constructor(private readonly http2Options: NodeHttp2Options = {}) {
3333
this.connectionPool = new Map<string, ClientHttp2Session>();

packages/types/src/transfer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ export interface RequestHandler<
1010
* 'h2' refers this handler is for handling HTTP/2 requests,
1111
* whereas 'h1' refers handling HTTP1 requests
1212
*/
13-
metadata?: Array<string>;
13+
metadata?: RequestHandlerMetadata;
1414
destroy?: () => void;
1515
handle: (
1616
request: RequestType,
1717
handlerOptions: HandlerOptions
1818
) => Promise<RequestHandlerOutput<ResponseType>>;
1919
}
20+
21+
export interface RequestHandlerMetadata {
22+
// This infers request handler's protocol
23+
// valid values are stated: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
24+
handlerProtocol: string;
25+
}

0 commit comments

Comments
 (0)