From 7aad4f13189df115882f6b14033b79bc44ac2ca8 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:20:58 +0900 Subject: [PATCH 1/4] add: typescript type --- types/dispatcher.d.ts | 5 ++++- types/header.d.ts | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 816db19d20d..59f619f162d 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -2,7 +2,7 @@ import { URL } from 'url' import { Duplex, Readable, Writable } from 'stream' import { EventEmitter } from 'events' import { Blob } from 'buffer' -import { IncomingHttpHeaders } from './header' +import { IncomingH2PseudoHeaders, IncomingHttpHeaders } from './header' import BodyReadable from './readable' import { FormData } from './formdata' import Errors from './errors' @@ -169,12 +169,14 @@ declare namespace Dispatcher { export interface ConnectData { statusCode: number; headers: IncomingHttpHeaders; + pseudoHeaders: IncomingH2PseudoHeaders; socket: Duplex; opaque: unknown; } export interface ResponseData { statusCode: number; headers: IncomingHttpHeaders; + pseudoHeaders: IncomingH2PseudoHeaders; body: BodyReadable & BodyMixin; trailers: Record; opaque: unknown; @@ -183,6 +185,7 @@ declare namespace Dispatcher { export interface PipelineHandlerData { statusCode: number; headers: IncomingHttpHeaders; + pseudoHeaders: IncomingH2PseudoHeaders; opaque: unknown; body: BodyReadable; context: object; diff --git a/types/header.d.ts b/types/header.d.ts index bfdb3296d4d..f4294b2b53b 100644 --- a/types/header.d.ts +++ b/types/header.d.ts @@ -2,3 +2,8 @@ * The header type declaration of `undici`. */ export type IncomingHttpHeaders = Record; + +/** + * The h2 pseudo-header type declaration of `undici`. + */ +export type IncomingH2PseudoHeaders = Record<':status', number | undefined> & Record<`:${string}`, string | string[] | undefined>; \ No newline at end of file From 57e0eed4fb4dc54f713acf3d36bf12ffd3447563 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:33:58 +0900 Subject: [PATCH 2/4] fix: dispatcher onHeaders type --- types/dispatcher.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 59f619f162d..bc3a90e6015 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -214,7 +214,7 @@ declare namespace Dispatcher { /** Invoked when request is upgraded either due to a `Upgrade` header or `CONNECT` method. */ onUpgrade?(statusCode: number, headers: Buffer[] | string[] | null, socket: Duplex): void; /** Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. */ - onHeaders?(statusCode: number, headers: Buffer[] | string[] | null, resume: () => void): boolean; + onHeaders?(statusCode: number, headers: Buffer[] | string[] | null, resume: () => void, statusText: string): boolean; /** Invoked when response payload data is received. */ onData?(chunk: Buffer): boolean; /** Invoked when response payload and trailers have been received and the request has completed. */ From 46b98d3cf6395ebdf4408200599b3178aa652aab Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:39:13 +0900 Subject: [PATCH 3/4] fix: type --- types/header.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/header.d.ts b/types/header.d.ts index f4294b2b53b..30048ac1e5e 100644 --- a/types/header.d.ts +++ b/types/header.d.ts @@ -6,4 +6,4 @@ export type IncomingHttpHeaders = Record; /** * The h2 pseudo-header type declaration of `undici`. */ -export type IncomingH2PseudoHeaders = Record<':status', number | undefined> & Record<`:${string}`, string | string[] | undefined>; \ No newline at end of file +export type IncomingH2PseudoHeaders = Record<':status', number | undefined> | Record<`:${string}`, string | number | string[] | undefined>; \ No newline at end of file From 8bd96556d90b722de590acb401cd4b524de33f70 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:41:25 +0900 Subject: [PATCH 4/4] test: typescript type --- test/types/header.test-d.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/types/header.test-d.ts b/test/types/header.test-d.ts index 38ac9f6afdc..a16e7b7a8f4 100644 --- a/test/types/header.test-d.ts +++ b/test/types/header.test-d.ts @@ -1,14 +1,23 @@ import { IncomingHttpHeaders as CoreIncomingHttpHeaders } from "http"; import { expectAssignable, expectNotAssignable } from "tsd"; -import { IncomingHttpHeaders } from "../../types/header"; +import { + IncomingHttpHeaders, + IncomingH2PseudoHeaders, +} from "../../types/header"; const headers = { authorization: undefined, ["content-type"]: "application/json", } satisfies CoreIncomingHttpHeaders; +const pseudoHeaders = { + ":status": 200, +} satisfies IncomingH2PseudoHeaders; + expectAssignable(headers); +expectAssignable(pseudoHeaders); + // It is why we do not need to add ` | null` to `IncomingHttpHeaders`: expectNotAssignable({ authorization: null,