@@ -13,19 +13,20 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
1313 public method : string ;
1414
1515 // public query: querystring.ParsedUrlQuery;
16+ public headers : http . IncomingHttpHeaders = { } ;
17+ public body : any ;
18+ public finished : boolean = false ;
1619
1720 // private _url: string;
1821 // private _path: string;
1922 private _baseUrl : string = "" ;
2023 private _rawquery : string ;
2124 private _query : querystring . ParsedUrlQuery ;
22- private _headers : http . IncomingHttpHeaders = { } ;
2325 private _params : { [ name : string ] : string } ;
24- private _bodydata : any ;
25- private _rawbody : any ;
2626 private _remoteAddress : ArrayBuffer ;
2727 private _readableState = { pipes : [ ] } ;
2828 private _readBodyMaxTime = 500 ;
29+ private _rawbody ?: Buffer ;
2930
3031 public aborted : boolean ;
3132
@@ -42,14 +43,11 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
4243 ) {
4344 super ( ) ;
4445
45- this . _headers = { } ;
4646 this . req . forEach ( ( key , value ) => {
47- this . _headers [ key ] = value ;
47+ this . headers [ key ] = value ;
4848
4949 // workaround: also consider 'referrer'
50- if ( key === "referer" ) {
51- this . _headers [ 'referrer' ] = value ;
52- }
50+ if ( key === "referer" ) { this . headers [ 'referrer' ] = value ; }
5351 } ) ;
5452
5553 this . url = this . req . getUrl ( ) ;
@@ -67,58 +65,66 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
6765 if ( this . app . opts ?. readBodyMaxTime ) {
6866 this . _readBodyMaxTime = this . app . opts . readBodyMaxTime ;
6967 }
70- }
71-
72- get ip ( ) {
73- return Buffer . from ( this . _remoteAddress ) . toString ( ) ;
74- }
7568
76- set body ( _body : any ) {
77- this . _bodydata = _body ;
78- }
69+ // Define getters as own properties to prevent Express from shadowing them
70+ // This ensures the getters are called even if Express tries to set them as properties
7971
80- get body ( ) {
81- return this . _bodydata || this . _rawbody ?. toString ( ) ;
82- }
83-
84- get headers ( ) : http . IncomingHttpHeaders {
85- return this . _headers ;
86- }
87-
88- set params ( value ) {
89- this . _params = value ;
90- }
91-
92- get params ( ) : { [ name : string ] : string } {
93- if ( ! this . _params ) {
94- this . _params = { } ;
95- for ( let i = 0 ; i < this . parameterNames . length ; i ++ ) {
96- const paramName = this . parameterNames [ i ] ;
97- this . _params [ paramName ] = this . req . getParameter ( i ) ;
98- }
99- }
100-
101- return this . _params ;
102- }
72+ Object . defineProperty ( this , 'ip' , {
73+ get : ( ) => {
74+ return Buffer . from ( this . _remoteAddress ) . toString ( ) ;
75+ } ,
76+ enumerable : true ,
77+ configurable : true
78+ } ) ;
10379
104- get query ( ) : querystring . ParsedUrlQuery {
105- if ( ! this . _query ) this . _query = querystring . parse ( this . _rawquery ) ;
106- return this . _query ;
107- }
80+ Object . defineProperty ( this , 'params' , {
81+ get : ( ) : { [ name : string ] : string } => {
82+ if ( ! this . _params ) {
83+ this . _params = { } ;
84+ for ( let i = 0 ; i < this . parameterNames . length ; i ++ ) {
85+ const paramName = this . parameterNames [ i ] ;
86+ this . _params [ paramName ] = this . req . getParameter ( i ) ;
87+ }
88+ }
89+ return this . _params ;
90+ } ,
91+ set : ( value ) => {
92+ this . _params = value ;
93+ } ,
94+ enumerable : true ,
95+ configurable : true
96+ } ) ;
10897
109- get baseUrl ( ) {
110- return this . _baseUrl ;
111- }
98+ Object . defineProperty ( this , 'query' , {
99+ get : ( ) : querystring . ParsedUrlQuery => {
100+ if ( ! this . _query ) this . _query = querystring . parse ( this . _rawquery ) ;
101+ return this . _query ;
102+ } ,
103+ enumerable : true ,
104+ configurable : true
105+ } ) ;
112106
113- set baseUrl ( baseUrl ) {
114- this . _baseUrl = baseUrl ;
115- }
107+ Object . defineProperty ( this , 'baseUrl' , {
108+ get : ( ) => {
109+ return this . _baseUrl ;
110+ } ,
111+ set : ( baseUrl ) => {
112+ this . _baseUrl = baseUrl ;
113+ } ,
114+ enumerable : true ,
115+ configurable : true
116+ } ) ;
116117
117- get path ( ) : string {
118- const path = this . #_originalUrlParsed. pathname . replace ( this . _baseUrl , "" ) ;
119- return ( ! path . startsWith ( "/" ) )
120- ? `/${ path } `
121- : path ;
118+ Object . defineProperty ( this , 'path' , {
119+ get : ( ) : string => {
120+ const path = this . #_originalUrlParsed. pathname . replace ( this . _baseUrl , "" ) ;
121+ return ( ! path . startsWith ( "/" ) )
122+ ? `/${ path } `
123+ : path ;
124+ } ,
125+ enumerable : true ,
126+ configurable : true
127+ } ) ;
122128 }
123129
124130 get ( name : string ) {
@@ -127,7 +133,7 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
127133
128134 header ( name : string ) {
129135 name = name . toLowerCase ( ) ;
130- return this . _headers [ name ] || undefined ;
136+ return this . headers [ name ] || undefined ;
131137 }
132138
133139 accepts ( ...args : any [ ] ) : string | false {
@@ -153,7 +159,7 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
153159 return this ;
154160 }
155161
156- protected readBody ( ) {
162+ public _readBody ( ) {
157163 return new Promise < boolean > ( ( resolve , reject ) => {
158164 let body : Buffer ;
159165
@@ -162,23 +168,25 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
162168 // https://github.com/endel/uWebSockets-express/issues/9
163169 //
164170 const rejectionTimeout = setTimeout ( ( ) => {
165- if ( body ) {
166- this . _rawbody = body ;
167- this . headers [ 'content-length' ] = String ( body . length ) ;
168- }
171+ this . emit ( 'error' ) ;
169172 reject ( ) ;
170173 } , this . _readBodyMaxTime ) ;
171174
172175 this . res . onData ( ( arrayBuffer , isLast ) => {
176+ this . emit ( 'data' , new Uint8Array ( arrayBuffer ) ) ;
177+
173178 const chunk = Buffer . from ( arrayBuffer ) ;
174179 body = ( body && body . length !== 0 ) ? Buffer . concat ( [ body , chunk ] ) : Buffer . concat ( [ chunk ] ) ;
175180
176181 if ( isLast ) {
177182 clearTimeout ( rejectionTimeout ) ;
178183 this . _rawbody = body ;
184+ this . body = body . toString ( 'utf8' ) ;
185+ this . emit ( 'end' ) ;
179186 resolve ( body . length > 0 ) ;
180187 }
181188 } ) ;
189+
182190 } )
183191 }
184192
0 commit comments