Skip to content

Commit 5f0b4b2

Browse files
committed
optimization request
1 parent a67e09d commit 5f0b4b2

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const http = require('http')
55
const Benchmark = require('benchmark')
66
const suite = new Benchmark.Suite()
77
const Request = require('../lib/request')
8-
const parseURL = require('../lib/parseURL')
8+
const parseURL = require('../lib/parse-url')
99

1010
const mockReq = {
1111
url: 'http://localhost',

lib/request.js

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ class Request extends IncomingMessage {
7878
this.httpVersion = '1.1'
7979
this.method = options.method ? options.method.toUpperCase() : 'GET'
8080

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+
8191
this.headers = {}
8292
this.rawHeaders = []
8393
const headers = options.headers || {}
@@ -98,30 +108,42 @@ class Request extends IncomingMessage {
98108
this.headers[fieldLowerCase] = '' + value
99109
}
100110

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 () {
101129
if (('user-agent' in this.headers) === false) {
102130
this.headers['user-agent'] = 'lightMyRequest'
103131
}
104-
this.headers.host = this.headers.host || options.authority || hostHeaderFromURL(parsedURL)
132+
this.headers.host = this.headers.host || this._lightMyRequest.authority || this._lightMyRequest.hostHeader
105133

106-
if (options.cookies) {
107-
const { cookies } = options
134+
if (this._lightMyRequest.cookies) {
135+
const { cookies } = this._lightMyRequest
108136
const cookieValues = Object.keys(cookies).map(key => cookie.serialize(key, cookies[key]))
109137
if (this.headers.cookie) {
110138
cookieValues.unshift(this.headers.cookie)
111139
}
112140
this.headers.cookie = cookieValues.join('; ')
113141
}
142+
}
114143

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 () {
123145
// we keep both payload and body for compatibility reasons
124-
let payload = options.payload || options.body || null
146+
let payload = this._lightMyRequest.payload
125147
const payloadResume = payload && typeof payload.resume === 'function'
126148

127149
if (payload && typeof payload !== 'string' && !payloadResume && !Buffer.isBuffer(payload)) {
@@ -132,11 +154,7 @@ class Request extends IncomingMessage {
132154
}
133155
}
134156

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) {
140158
const prevPayload = payload
141159
if (payloadResume) {
142160
payload = new Readable({
@@ -157,23 +175,15 @@ class Request extends IncomingMessage {
157175
}
158176
}
159177

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
174179
}
175180

176181
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+
}
177187
let payload = this._lightMyRequest.payload
178188
this.complete = true
179189
if (payload) {

0 commit comments

Comments
 (0)