1+ import { Multipart } from "multipart-ts" ;
12import http , { OutgoingHttpHeader } from "node:http" ;
23import stream from "node:stream" ;
3- import { Multipart } from "multipart-ts" ;
44
55/**
66 * An incoming HTTP request from a connected client.
@@ -33,7 +33,12 @@ export class Request {
3333 * @param headers See {@link Request#headers}.
3434 * @param bodyStream See {@link Request#bodyStream}.
3535 */
36- protected constructor ( method : Request [ "method" ] , url : Request [ "url" ] , headers : Request [ "headers" ] , bodyStream : Request [ "bodyStream" ] ) {
36+ protected constructor (
37+ method : Request [ "method" ] ,
38+ url : Request [ "url" ] ,
39+ headers : Request [ "headers" ] ,
40+ bodyStream : Request [ "bodyStream" ] ,
41+ ) {
3742 this . method = method ;
3843 this . url = url ;
3944 this . headers = headers ;
@@ -45,8 +50,14 @@ export class Request {
4550 * @throws {@link Request.BadUrlError } If the request URL is invalid.
4651 */
4752 public static incomingMessage ( incomingMessage : http . IncomingMessage ) {
48- const auth = incomingMessage . headers . authorization ?. toLowerCase ( ) . startsWith ( "basic " )
49- ? Buffer . from ( incomingMessage . headers . authorization . substring ( "basic " . length ) , "base64" ) . toString ( )
53+ const auth =
54+ incomingMessage . headers . authorization
55+ ?. toLowerCase ( )
56+ . startsWith ( "basic " )
57+ ? Buffer . from (
58+ incomingMessage . headers . authorization
59+ . substring ( "basic " . length ) , "base64"
60+ ) . toString ( )
5061 : null ;
5162
5263 const url = `http://${ auth ? `${ auth } @` : "" } ${ process . env . HOST ?? "localhost" } ${ incomingMessage . url ?? "/" } ` ;
@@ -55,12 +66,21 @@ export class Request {
5566
5667 const headers = Request . headersFromNodeDict ( incomingMessage . headers ) ;
5768
58- return new Request (
59- incomingMessage . method as Request . Method ,
60- new URL ( url ) ,
61- headers ,
62- incomingMessage
63- )
69+ return new Request ( incomingMessage . method as Request . Method , new URL ( url ) , headers , incomingMessage ) ;
70+ }
71+
72+ /**
73+ * @internal
74+ */
75+ public static headersFromNodeDict ( headers : Record < string , OutgoingHttpHeader | undefined > ) : Headers {
76+ return new Headers ( Object . entries ( headers )
77+ . filter ( ( e ) => e [ 1 ] !== undefined )
78+ . flatMap < [ string , string ] > ( ( [ key , value ] ) =>
79+ value instanceof Array
80+ ? value . map < [ string , string ] > ( v => [ key , v ] )
81+ : [ [ key , String ( value ) ] ]
82+ )
83+ ) ;
6484 }
6585
6686 /**
@@ -135,19 +155,6 @@ export class Request {
135155 public async text ( ) : Promise < string > {
136156 return ( await this . blob ( ) ) . text ( ) ;
137157 }
138-
139- /**
140- * @internal
141- */
142- public static headersFromNodeDict ( headers : Record < string , OutgoingHttpHeader | undefined > ) : Headers {
143- return new Headers (
144- ( Object . entries ( headers )
145- . filter ( ( [ , v ] ) => v !== undefined ) as [ string , Exclude < typeof headers [ string ] , undefined > ] [ ] )
146- . flatMap < [ string , string ] > ( ( [ key , value ] ) => value instanceof Array
147- ? value . map < [ string , string ] > ( v => [ key , v ] )
148- : [ [ key , String ( value ) ] ] )
149- ) ;
150- }
151158}
152159
153160export namespace Request {
0 commit comments