Fixing code review issues: function call formatting, renaming JS member variables, refactoring response headers conversion.

This commit is contained in:
ali.ibrahim 2016-10-13 17:14:23 +02:00
parent b290415bbd
commit 6f5b0a28c5
9 changed files with 272 additions and 185 deletions

View file

@ -18,29 +18,29 @@ kSupportedProtocols.add('https:')
class IncomingMessage extends Readable {
constructor (urlRequest) {
super()
this._url_request = urlRequest
this._shouldPush = false
this._data = []
this._url_request.on('data', (event, chunk) => {
this.urlRequest = urlRequest
this.shouldPush = false
this.data = []
this.urlRequest.on('data', (event, chunk) => {
this._storeInternalData(chunk)
this._pushInternalData()
})
this._url_request.on('end', () => {
this.urlRequest.on('end', () => {
this._storeInternalData(null)
this._pushInternalData()
})
}
get statusCode () {
return this._url_request.statusCode
return this.urlRequest.statusCode
}
get statusMessage () {
return this._url_request.statusMessage
return this.urlRequest.statusMessage
}
get headers () {
return this._url_request.rawResponseHeaders
return this.urlRequest.rawResponseHeaders
}
get httpVersion () {
@ -48,15 +48,11 @@ class IncomingMessage extends Readable {
}
get httpVersionMajor () {
return this._url_request.httpVersionMajor
return this.urlRequest.httpVersionMajor
}
get httpVersionMinor () {
return this._url_request.httpVersionMinor
}
get rawHeaders () {
return this._url_request.rawResponseHeaders
return this.urlRequest.httpVersionMinor
}
get rawTrailers () {
@ -68,18 +64,18 @@ class IncomingMessage extends Readable {
}
_storeInternalData (chunk) {
this._data.push(chunk)
this.data.push(chunk)
}
_pushInternalData () {
while (this._shouldPush && this._data.length > 0) {
const chunk = this._data.shift()
this._shouldPush = this.push(chunk)
while (this.shouldPush && this.data.length > 0) {
const chunk = this.data.shift()
this.shouldPush = this.push(chunk)
}
}
_read () {
this._shouldPush = true
this.shouldPush = true
this._pushInternalData()
}
@ -88,17 +84,17 @@ class IncomingMessage extends Readable {
URLRequest.prototype._emitRequestEvent = function (async, ...rest) {
if (async) {
process.nextTick(() => {
this._request.emit.apply(this._request, rest)
this.clientRequest.emit.apply(this.clientRequest, rest)
})
} else {
this._request.emit.apply(this._request, rest)
this.clientRequest.emit.apply(this.clientRequest, rest)
}
}
URLRequest.prototype._emitResponseEvent = function (async, ...rest) {
if (async) {
process.nextTick(() => {
this._request.emit.apply(this._response, rest)
this.clientRequest.emit.apply(this._response, rest)
})
} else {
this._response.emit.apply(this._response, rest)
@ -165,13 +161,13 @@ class ClientRequest extends EventEmitter {
})
// Set back and forward links.
this._url_request = urlRequest
urlRequest._request = this
this.urlRequest = urlRequest
urlRequest.clientRequest = this
// This is a copy of the extra headers structure held by the native
// net::URLRequest. The main reason is to keep the getHeader API synchronous
// after the request starts.
this._extra_headers = {}
this.extraHeaders = {}
if (options.headers) {
const keys = Object.keys(options.headers)
@ -183,7 +179,7 @@ class ClientRequest extends EventEmitter {
// Set when the request uses chunked encoding. Can be switched
// to true only once and never set back to false.
this._chunkedEncoding = false
this.chunkedEncodingEnabled = false
urlRequest.on('response', () => {
const response = new IncomingMessage(urlRequest)
@ -197,14 +193,14 @@ class ClientRequest extends EventEmitter {
}
get chunkedEncoding () {
return this._chunkedEncoding
return this.chunkedEncodingEnabled
}
set chunkedEncoding (value) {
if (!this._url_request.notStarted) {
if (!this.urlRequest.notStarted) {
throw new Error('Can\'t set the transfer encoding, headers have been sent.')
}
this._chunkedEncoding = value
this.chunkedEncodingEnabled = value
}
setHeader (name, value) {
@ -214,13 +210,13 @@ class ClientRequest extends EventEmitter {
if (value === undefined) {
throw new Error('`value` required in setHeader("' + name + '", value).')
}
if (!this._url_request.notStarted) {
if (!this.urlRequest.notStarted) {
throw new Error('Can\'t set headers after they are sent.')
}
const key = name.toLowerCase()
this._extra_headers[key] = value
this._url_request.setExtraHeader(name, value)
this.extraHeaders[key] = value
this.urlRequest.setExtraHeader(name, value)
}
getHeader (name) {
@ -228,12 +224,12 @@ class ClientRequest extends EventEmitter {
throw new Error('`name` is required for getHeader(name).')
}
if (!this._extra_headers) {
if (!this.extraHeaders) {
return
}
const key = name.toLowerCase()
return this._extra_headers[key]
return this.extraHeaders[key]
}
removeHeader (name) {
@ -241,13 +237,13 @@ class ClientRequest extends EventEmitter {
throw new Error('`name` is required for removeHeader(name).')
}
if (!this._url_request.notStarted) {
if (!this.urlRequest.notStarted) {
throw new Error('Can\'t remove headers after they are sent.')
}
const key = name.toLowerCase()
delete this._extra_headers[key]
this._url_request.removeExtraHeader(name)
delete this.extraHeaders[key]
this.urlRequest.removeExtraHeader(name)
}
_write (chunk, encoding, callback, isLast) {
@ -265,13 +261,13 @@ class ClientRequest extends EventEmitter {
// Since writing to the network is asynchronous, we conservatively
// assume that request headers are written after delivering the first
// buffer to the network IO thread.
if (this._url_request.notStarted) {
this._url_request.setChunkedUpload(this.chunkedEncoding)
if (this.urlRequest.notStarted) {
this.urlRequest.setChunkedUpload(this.chunkedEncoding)
}
// Headers are assumed to be sent on first call to _writeBuffer,
// i.e. after the first call to write or end.
let result = this._url_request.write(chunk, isLast)
let result = this.urlRequest.write(chunk, isLast)
// The write callback is fired asynchronously to mimic Node.js.
if (callback) {
@ -282,7 +278,7 @@ class ClientRequest extends EventEmitter {
}
write (data, encoding, callback) {
if (this._url_request.finished) {
if (this.urlRequest.finished) {
let error = new Error('Write after end.')
process.nextTick(writeAfterEndNT, this, error, callback)
return true
@ -292,7 +288,7 @@ class ClientRequest extends EventEmitter {
}
end (data, encoding, callback) {
if (this._url_request.finished) {
if (this.urlRequest.finished) {
return false
}
@ -311,7 +307,7 @@ class ClientRequest extends EventEmitter {
}
abort () {
this._url_request.cancel()
this.urlRequest.cancel()
}
}