1
1
import type * as http from 'node:http' ;
2
+ import type * as http2 from 'node:http2' ;
2
3
3
- import { type ClientAddress , type ErrorHandler , type FetchHandler } from './fetch-handler.ts' ;
4
+ import type { ClientAddress , ErrorHandler , FetchHandler } from './fetch-handler.ts' ;
4
5
import { readStream } from './read-stream.ts' ;
5
6
6
7
export interface RequestListenerOptions {
@@ -30,9 +31,12 @@ export interface RequestListenerOptions {
30
31
}
31
32
32
33
/**
33
- * Wraps a fetch handler in a Node.js `http.RequestListener` that can be used with
34
- * [`http.createServer()`](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener) or
35
- * [`https.createServer()`](https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener).
34
+ * Wraps a fetch handler in a Node.js request listener that can be used with:
35
+ *
36
+ * - [`http.createServer()`](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener)
37
+ * - [`https.createServer()`](https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener)
38
+ * - [`http2.createServer()`](https://nodejs.org/api/http2.html#http2createserveroptions-onrequesthandler)
39
+ * - [`http2.createSecureServer()`](https://nodejs.org/api/http2.html#http2createsecureserveroptions-onrequesthandler)
36
40
*
37
41
* Example:
38
42
*
@@ -109,18 +113,19 @@ function internalServerError(): Response {
109
113
export type RequestOptions = Omit < RequestListenerOptions , 'onError' > ;
110
114
111
115
/**
112
- * Creates a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object from a Node.js
113
- * [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage) and
114
- * [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) pair.
116
+ * Creates a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object from
117
+ *
118
+ * - a [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)/[`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) pair
119
+ * - a [`http2.Http2ServerRequest`](https://nodejs.org/api/http2.html#class-http2http2serverrequest)/[`http2.Http2ServerResponse`](https://nodejs.org/api/http2.html#class-http2http2serverresponse) pair
115
120
*
116
121
* @param req The incoming request object.
117
122
* @param res The server response object.
118
123
* @param options
119
124
* @returns A request object.
120
125
*/
121
126
export function createRequest (
122
- req : http . IncomingMessage ,
123
- res : http . ServerResponse ,
127
+ req : http . IncomingMessage | http2 . Http2ServerRequest ,
128
+ res : http . ServerResponse | http2 . Http2ServerResponse ,
124
129
options ?: RequestOptions ,
125
130
) : Request {
126
131
let controller = new AbortController ( ) ;
@@ -161,32 +166,36 @@ export function createRequest(
161
166
}
162
167
163
168
/**
164
- * Creates a [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object from the headers
165
- * in a Node.js [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage).
169
+ * Creates a [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object from the headers in a Node.js
170
+ * [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)/[`http2.Http2ServerRequest`](https://nodejs.org/api/http2.html#class-http2http2serverrequest ).
166
171
*
167
172
* @param req The incoming request object.
168
173
* @returns A headers object.
169
174
*/
170
- export function createHeaders ( req : http . IncomingMessage ) : Headers {
175
+ export function createHeaders ( req : http . IncomingMessage | http2 . Http2ServerRequest ) : Headers {
171
176
let headers = new Headers ( ) ;
172
177
173
178
let rawHeaders = req . rawHeaders ;
174
179
for ( let i = 0 ; i < rawHeaders . length ; i += 2 ) {
175
- if ( rawHeaders [ i ] . startsWith ( ":" ) ) continue ;
180
+ if ( rawHeaders [ i ] . startsWith ( ':' ) ) continue ;
176
181
headers . append ( rawHeaders [ i ] , rawHeaders [ i + 1 ] ) ;
177
182
}
178
183
179
184
return headers ;
180
185
}
181
186
182
187
/**
183
- * Sends a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the client using the
184
- * Node.js [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) object.
188
+ * Sends a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the client using a Node.js
189
+ * [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse)/[`http2.Http2ServerResponse`](https://nodejs.org/api/http2.html#class-http2http2serverresponse)
190
+ * object.
185
191
*
186
192
* @param res The server response object.
187
193
* @param response The response to send.
188
194
*/
189
- export async function sendResponse ( res : http . ServerResponse , response : Response ) : Promise < void > {
195
+ export async function sendResponse (
196
+ res : http . ServerResponse | http2 . Http2ServerResponse ,
197
+ response : Response ,
198
+ ) : Promise < void > {
190
199
// Iterate over response.headers so we are sure to send multiple Set-Cookie headers correctly.
191
200
// These would incorrectly be merged into a single header if we tried to use
192
201
// `Object.fromEntries(response.headers.entries())`.
@@ -207,6 +216,7 @@ export async function sendResponse(res: http.ServerResponse, response: Response)
207
216
208
217
if ( response . body != null && res . req . method !== 'HEAD' ) {
209
218
for await ( let chunk of readStream ( response . body ) ) {
219
+ // @ts -expect-error - Node typings for http2 require a 2nd parameter to write but it's optional
210
220
res . write ( chunk ) ;
211
221
}
212
222
}
0 commit comments