Skip to content

Commit

Permalink
fix: Allow “undefined“ as value in headers (nodejs#1929)
Browse files Browse the repository at this point in the history
* fix: Allow “undefined“ as value in headers

nodejs#1896 (comment)
Signed-off-by: pan93412 <pan93412@gmail.com>

* test: Add test for our IncomingHttpHeaders

Co-Authored-By: Simen Bekkhus <sbekkhus91@gmail.com>
Signed-off-by: pan93412 <pan93412@gmail.com>

* chore: Upgrade TypeScript to 4.9.5

Signed-off-by: pan93412 <pan93412@gmail.com>

---------

Signed-off-by: pan93412 <pan93412@gmail.com>
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
  • Loading branch information
2 people authored and metcoder95 committed Jul 21, 2023
1 parent 71bd118 commit f6d86e0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
10 changes: 5 additions & 5 deletions docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Returns: `void | Promise<ConnectData>` - Only returns a `Promise` if no `callbac
#### Parameter: `ConnectData`

* **statusCode** `number`
* **headers** `Record<string, string | string[]>`
* **headers** `Record<string, string | string[] | undefined>`
* **socket** `stream.Duplex`
* **opaque** `unknown`

Expand Down Expand Up @@ -383,7 +383,7 @@ Extends: [`RequestOptions`](#parameter-requestoptions)
#### Parameter: PipelineHandlerData

* **statusCode** `number`
* **headers** `Record<string, string | string[]>`
* **headers** `Record<string, string | string[] | undefined>`
* **opaque** `unknown`
* **body** `stream.Readable`
* **context** `object`
Expand Down Expand Up @@ -644,7 +644,7 @@ Returns: `void | Promise<StreamData>` - Only returns a `Promise` if no `callback
#### Parameter: `StreamFactoryData`

* **statusCode** `number`
* **headers** `Record<string, string | string[]>`
* **headers** `Record<string, string | string[] | undefined>`
* **opaque** `unknown`
* **onInfo** `({statusCode: number, headers: Record<string, string | string[]>}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received.

Expand Down Expand Up @@ -853,9 +853,9 @@ Emitted when dispatcher is no longer busy.

## Parameter: `UndiciHeaders`

* `Record<string, string | string[]> | string[] | null`
* `Record<string, string | string[] | undefined> | string[] | null`

Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in two forms; either as an object specified by the `Record<string, string | string[]>` (`IncomingHttpHeaders`) type, or an array of strings. An array representation of a header list must have an even length or an `InvalidArgumentError` will be thrown.
Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in two forms; either as an object specified by the `Record<string, string | string[] | undefined>` (`IncomingHttpHeaders`) type, or an array of strings. An array representation of a header list must have an even length or an `InvalidArgumentError` will be thrown.

Keys are lowercase and values are not modified.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"table": "^6.8.0",
"tap": "^16.1.0",
"tsd": "^0.25.0",
"typescript": "^4.8.4",
"typescript": "^4.9.5",
"wait-on": "^7.0.1",
"ws": "^8.11.0"
},
Expand Down
8 changes: 8 additions & 0 deletions test/types/dispatcher.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingHttpHeaders } from 'http'
import { Duplex, Readable, Writable } from 'stream'
import { expectAssignable, expectType } from 'tsd'
import { Dispatcher } from '../..'
Expand All @@ -9,11 +10,18 @@ expectAssignable<Dispatcher>(new Dispatcher())
{
const dispatcher = new Dispatcher()

const nodeCoreHeaders = {
authorization: undefined,
['content-type']: 'application/json'
} satisfies IncomingHttpHeaders;

// dispatch
expectAssignable<boolean>(dispatcher.dispatch({ path: '', method: 'GET' }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET' }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: { authorization: undefined } }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: [] }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: {} }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: nodeCoreHeaders }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: null, reset: true }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: new URL('http://localhost'), path: '', method: 'GET' }, {}))

Expand Down
16 changes: 16 additions & 0 deletions test/types/header.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IncomingHttpHeaders as CoreIncomingHttpHeaders } from "http";
import { expectAssignable, expectNotAssignable } from "tsd";
import { IncomingHttpHeaders } from "../../types/header";

const headers = {
authorization: undefined,
["content-type"]: "application/json",
} satisfies CoreIncomingHttpHeaders;

expectAssignable<IncomingHttpHeaders>(headers);

// It is why we do not need to add ` | null` to `IncomingHttpHeaders`:
expectNotAssignable<CoreIncomingHttpHeaders>({
authorization: null,
["content-type"]: "application/json",
});
2 changes: 1 addition & 1 deletion types/header.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* The header type declaration of `undici`.
*/
export type IncomingHttpHeaders = Record<string, string | string[]>;
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;

0 comments on commit f6d86e0

Please sign in to comment.