@@ -78,6 +78,16 @@ class Request extends IncomingMessage {
78
78
this . httpVersion = '1.1'
79
79
this . method = options . method ? options . method . toUpperCase ( ) : 'GET'
80
80
81
+ // Use _lightMyRequest namespace to avoid collision with Node
82
+ this . _lightMyRequest = {
83
+ payload : options . payload || options . body || null ,
84
+ isDone : false ,
85
+ simulate : options . simulate || { } ,
86
+ authority : options . authority ,
87
+ cookies : options . cookies ,
88
+ hostHeader : hostHeaderFromURL ( parsedURL )
89
+ }
90
+
81
91
this . headers = { }
82
92
this . rawHeaders = [ ]
83
93
const headers = options . headers || { }
@@ -98,30 +108,42 @@ class Request extends IncomingMessage {
98
108
this . headers [ fieldLowerCase ] = '' + value
99
109
}
100
110
111
+ Object . defineProperty ( this , 'connection' , {
112
+ get ( ) {
113
+ warning . emit ( 'FST_LIGHTMYREQUEST_DEP01' )
114
+ return this . socket
115
+ } ,
116
+ configurable : true
117
+ } )
118
+ }
119
+
120
+ getLength ( payload ) {
121
+ if ( typeof payload === 'string' ) {
122
+ return Buffer . byteLength ( payload )
123
+ }
124
+
125
+ return payload . length
126
+ }
127
+
128
+ parseHeader ( ) {
101
129
if ( ( 'user-agent' in this . headers ) === false ) {
102
130
this . headers [ 'user-agent' ] = 'lightMyRequest'
103
131
}
104
- this . headers . host = this . headers . host || options . authority || hostHeaderFromURL ( parsedURL )
132
+ this . headers . host = this . headers . host || this . _lightMyRequest . authority || this . _lightMyRequest . hostHeader
105
133
106
- if ( options . cookies ) {
107
- const { cookies } = options
134
+ if ( this . _lightMyRequest . cookies ) {
135
+ const { cookies } = this . _lightMyRequest
108
136
const cookieValues = Object . keys ( cookies ) . map ( key => cookie . serialize ( key , cookies [ key ] ) )
109
137
if ( this . headers . cookie ) {
110
138
cookieValues . unshift ( this . headers . cookie )
111
139
}
112
140
this . headers . cookie = cookieValues . join ( '; ' )
113
141
}
142
+ }
114
143
115
- Object . defineProperty ( this , 'connection' , {
116
- get ( ) {
117
- warning . emit ( 'FST_LIGHTMYREQUEST_DEP01' )
118
- return this . socket
119
- } ,
120
- configurable : true
121
- } )
122
-
144
+ parsePayload ( ) {
123
145
// we keep both payload and body for compatibility reasons
124
- let payload = options . payload || options . body || null
146
+ let payload = this . _lightMyRequest . payload
125
147
const payloadResume = payload && typeof payload . resume === 'function'
126
148
127
149
if ( payload && typeof payload !== 'string' && ! payloadResume && ! Buffer . isBuffer ( payload ) ) {
@@ -132,11 +154,7 @@ class Request extends IncomingMessage {
132
154
}
133
155
}
134
156
135
- for ( const header of Object . keys ( this . headers ) ) {
136
- this . rawHeaders . push ( header , this . headers [ header ] )
137
- }
138
-
139
- if ( options . simulate ?. end === false ) {
157
+ if ( this . _lightMyRequest . simulate . end === false ) {
140
158
const prevPayload = payload
141
159
if ( payloadResume ) {
142
160
payload = new Readable ( {
@@ -157,23 +175,15 @@ class Request extends IncomingMessage {
157
175
}
158
176
}
159
177
160
- // Use _lightMyRequest namespace to avoid collision with Node
161
- this . _lightMyRequest = {
162
- payload,
163
- isDone : false ,
164
- simulate : options . simulate || { }
165
- }
166
- }
167
-
168
- getLength ( payload ) {
169
- if ( typeof payload === 'string' ) {
170
- return Buffer . byteLength ( payload )
171
- }
172
-
173
- return payload . length
178
+ this . _lightMyRequest . payload = payload
174
179
}
175
180
176
181
prepare ( next , onError ) {
182
+ this . parseHeader ( )
183
+ this . parsePayload ( )
184
+ for ( const header of Object . keys ( this . headers ) ) {
185
+ this . rawHeaders . push ( header , this . headers [ header ] )
186
+ }
177
187
let payload = this . _lightMyRequest . payload
178
188
this . complete = true
179
189
if ( payload ) {
0 commit comments