Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce pseudoHeaders #2428

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion test/types/header.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<IncomingHttpHeaders>(headers);

expectAssignable<IncomingH2PseudoHeaders>(pseudoHeaders);

// It is why we do not need to add ` | null` to `IncomingHttpHeaders`:
expectNotAssignable<CoreIncomingHttpHeaders>({
authorization: null,
Expand Down
7 changes: 5 additions & 2 deletions types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<string, string>;
opaque: unknown;
Expand All @@ -183,6 +185,7 @@ declare namespace Dispatcher {
export interface PipelineHandlerData {
statusCode: number;
headers: IncomingHttpHeaders;
pseudoHeaders: IncomingH2PseudoHeaders;
opaque: unknown;
body: BodyReadable;
context: object;
Expand Down Expand Up @@ -211,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. */
Expand Down
5 changes: 5 additions & 0 deletions types/header.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
* The header type declaration of `undici`.
*/
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;

/**
* The h2 pseudo-header type declaration of `undici`.
*/
export type IncomingH2PseudoHeaders = Record<':status', number | undefined> | Record<`:${string}`, string | number | string[] | undefined>;