Fixing lint-js issues.

This commit is contained in:
ali.ibrahim 2016-09-29 15:24:28 +02:00
parent 0588435882
commit 42adb2afd4
2 changed files with 140 additions and 149 deletions

View file

@ -108,10 +108,10 @@ Object.defineProperties(exports, {
} }
}, },
net: { net: {
enumerable: true, enumerable: true,
get: function () { get: function () {
return require('../net') return require('../net')
} }
}, },
// The internal modules, invisible unless you know their names. // The internal modules, invisible unless you know their names.

View file

@ -1,5 +1,6 @@
'use strict' 'use strict'
const url = require('url')
const {EventEmitter} = require('events') const {EventEmitter} = require('events')
const util = require('util') const util = require('util')
const binding = process.atomBinding('net') const binding = process.atomBinding('net')
@ -9,94 +10,91 @@ const {URLRequest} = net
Object.setPrototypeOf(Net.prototype, EventEmitter.prototype) Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype) Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
let kSupportedProtocols = new Set(); let kSupportedProtocols = new Set()
kSupportedProtocols.add('http:'); kSupportedProtocols.add('http:')
kSupportedProtocols.add('https:'); kSupportedProtocols.add('https:')
class IncomingMessage extends EventEmitter { class IncomingMessage extends EventEmitter {
constructor(url_request) { constructor (urlRequest) {
super(); super()
this._url_request = url_request; this._url_request = urlRequest
} }
get statusCode() { get statusCode () {
return this._url_request.statusCode; return this._url_request.statusCode
} }
get statusMessage() { get statusMessage () {
return this._url_request.statusMessage; return this._url_request.statusMessage
} }
get headers() { get headers () {
return this._url_request.rawResponseHeaders; return this._url_request.rawResponseHeaders
} }
get httpVersion() { get httpVersion () {
return `${this.httpVersionMajor}.${this.httpVersionMinor}`; return `${this.httpVersionMajor}.${this.httpVersionMinor}`
} }
get httpVersionMajor() { get httpVersionMajor () {
return this._url_request.httpVersionMajor; return this._url_request.httpVersionMajor
} }
get httpVersionMinor() { get httpVersionMinor () {
return this._url_request.httpVersionMinor; return this._url_request.httpVersionMinor
} }
get rawHeaders() { get rawHeaders () {
return this._url_request.rawResponseHeaders; return this._url_request.rawResponseHeaders
} }
} }
URLRequest.prototype._emitRequestEvent = function() { URLRequest.prototype._emitRequestEvent = function () {
this._request.emit.apply(this._request, arguments); this._request.emit.apply(this._request, arguments)
} }
URLRequest.prototype._emitResponseEvent = function() { URLRequest.prototype._emitResponseEvent = function () {
this._response.emit.apply(this._response, arguments); this._response.emit.apply(this._response, arguments)
} }
class ClientRequest extends EventEmitter { class ClientRequest extends EventEmitter {
constructor(options, callback) { constructor (options, callback) {
super(); super()
if (typeof options === 'string') { if (typeof options === 'string') {
options = url.parse(options); options = url.parse(options)
} else { } else {
options = util._extend({}, options); options = util._extend({}, options)
} }
const method = (options.method || 'GET').toUpperCase(); const method = (options.method || 'GET').toUpperCase()
let url_str = options.url; let urlStr = options.url
if (!url_str) { if (!urlStr) {
let url_obj = {}; let urlObj = {}
const protocol = options.protocol || 'http'
const protocol = options.protocol || 'http';
if (!kSupportedProtocols.has(protocol)) { if (!kSupportedProtocols.has(protocol)) {
throw new Error('Protocol "' + protocol + '" not supported. '); throw new Error('Protocol "' + protocol + '" not supported. ')
} }
url_obj.protocol = protocol; urlObj.protocol = protocol
if (options.host) { if (options.host) {
url_obj.host = options.host; urlObj.host = options.host
} else { } else {
if (options.hostname) { if (options.hostname) {
url_obj.hostname = options.hostname; urlObj.hostname = options.hostname
} else { } else {
url_obj.hostname = 'localhost'; urlObj.hostname = 'localhost'
} }
if (options.port) { if (options.port) {
url_obj.port = options.port; urlObj.port = options.port
} }
} }
const path = options.path || '/'; const path = options.path || '/'
if (options.path && / /.test(options.path)) { if (options.path && / /.test(options.path)) {
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/ // The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
// with an additional rule for ignoring percentage-escaped characters // with an additional rule for ignoring percentage-escaped characters
@ -104,210 +102,203 @@ class ClientRequest extends EventEmitter {
// well, and b) possibly too restrictive for real-world usage. That's // well, and b) possibly too restrictive for real-world usage. That's
// why it only scans for spaces because those are guaranteed to create // why it only scans for spaces because those are guaranteed to create
// an invalid request. // an invalid request.
throw new TypeError('Request path contains unescaped characters.'); throw new TypeError('Request path contains unescaped characters.')
} }
url_obj.path = path; urlObj.path = path
urlStr = url.format(urlObj)
url_str = url.format(url_obj);
} }
const session_name = options.session || ''; const sessionName = options.session || ''
let url_request = new URLRequest({ let urlRequest = new URLRequest({
method: method, method: method,
url: url_str, url: urlStr,
session: session_name session: sessionName
}); })
// Set back and forward links. // Set back and forward links.
this._url_request = url_request; this._url_request = urlRequest
url_request._request = this; urlRequest._request = this
if (options.headers) { if (options.headers) {
let keys = Object.keys(options.headers); const keys = Object.keys(options.headers)
for (let i = 0, l = keys.length; i < l; i++) { for (let i = 0, l = keys.length; i < l; i++) {
let key = keys[i]; const key = keys[i]
this.setHeader(key, options.headers[key]); this.setHeader(key, options.headers[key])
} }
} }
// Flag to prevent request's headers modifications after // Flag to prevent request's headers modifications after
// headers flush. // headers flush.
this._started = false; this._started = false
this._aborted = false; this._aborted = false
// Flag to prevent writings after end. // Flag to prevent writings after end.
this._finished = false; this._finished = false
// Set when the request uses chuned encoding. Can be switched // Set when the request uses chuned encoding. Can be switched
// to true only once and never set back to false. // to true only once and never set back to false.
this._chunkedEncoding = false; this._chunkedEncoding = false
// This is a copy of the extra headers structure held by the native // 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 // net::URLRequest. The main reason is to keep the getHeader API synchronous
// after the request starts. // after the request starts.
this._extra_headers = {}; this._extra_headers = {}
url_request.on('response', ()=> { urlRequest.on('response', () => {
let response = new IncomingMessage(url_request); const response = new IncomingMessage(urlRequest)
url_request._response = response; urlRequest._response = response
this.emit('response', response); this.emit('response', response)
}); })
if (callback) { if (callback) {
this.once('response', callback) this.once('response', callback)
} }
} }
get chunkedEncoding() { get chunkedEncoding () {
return this._chunkedEncoding; return this._chunkedEncoding
} }
set chunkedEncoding(value) { set chunkedEncoding (value) {
if (this._started) { if (this._started) {
throw new Error('Can\'t set the transfer encoding, headers have been sent.'); throw new Error('Can\'t set the transfer encoding, headers have been sent.')
} }
this._chunkedEncoding = value; this._chunkedEncoding = value
} }
setHeader (name, value) {
if (typeof name !== 'string') {
throw new TypeError('`name` should be a string in setHeader(name, value).')
}
if (value === undefined) {
throw new Error('`value` required in setHeader("' + name + '", value).')
}
if (this._started) {
throw new Error('Can\'t set headers after they are sent.')
}
setHeader(name, value) { const key = name.toLowerCase()
if (typeof name !== 'string') this._extra_headers[key] = value
throw new TypeError('`name` should be a string in setHeader(name, value).');
if (value === undefined)
throw new Error('`value` required in setHeader("' + name + '", value).');
if (this._started)
throw new Error('Can\'t set headers after they are sent.');
let key = name.toLowerCase();
this._extra_headers[key] = value;
this._url_request.setExtraHeader(name, value) this._url_request.setExtraHeader(name, value)
} }
getHeader (name) {
getHeader(name) {
if (arguments.length < 1) { if (arguments.length < 1) {
throw new Error('`name` is required for getHeader(name).'); throw new Error('`name` is required for getHeader(name).')
} }
if (!this._extra_headers) { if (!this._extra_headers) {
return; return
} }
let key = name.toLowerCase(); const key = name.toLowerCase()
return this._extra_headers[key]; return this._extra_headers[key]
} }
removeHeader (name) {
removeHeader(name) {
if (arguments.length < 1) { if (arguments.length < 1) {
throw new Error('`name` is required for removeHeader(name).'); throw new Error('`name` is required for removeHeader(name).')
} }
if (this._started) { if (this._started) {
throw new Error('Can\'t remove headers after they are sent.'); throw new Error('Can\'t remove headers after they are sent.')
} }
let key = name.toLowerCase(); const key = name.toLowerCase()
delete this._extra_headers[key]; delete this._extra_headers[key]
this._url_request.removeExtraHeader(name); this._url_request.removeExtraHeader(name)
} }
_write (chunk, encoding, callback, isLast) {
_write(chunk, encoding, callback, is_last) { let chunkIsString = typeof chunk === 'string'
let chunkIsBuffer = chunk instanceof Buffer
let chunk_is_string = typeof chunk === 'string'; if (!chunkIsString && !chunkIsBuffer) {
let chunk_is_buffer = chunk instanceof Buffer; throw new TypeError('First argument must be a string or Buffer.')
if (!chunk_is_string && !chunk_is_buffer) {
throw new TypeError('First argument must be a string or Buffer.');
} }
if (chunk_is_string) { if (chunkIsString) {
// We convert all strings into binary buffers. // We convert all strings into binary buffers.
chunk = Buffer.from(chunk, encoding); chunk = Buffer.from(chunk, encoding)
} }
// Headers are assumed to be sent on first call to _writeBuffer, // Headers are assumed to be sent on first call to _writeBuffer,
// i.e. after the first call to write or end. // i.e. after the first call to write or end.
let result = this._url_request.write(chunk, is_last); let result = this._url_request.write(chunk, isLast)
// Since writing to the network is asynchronous, we conservatively // Since writing to the network is asynchronous, we conservatively
// assume that request headers are written after delivering the first // assume that request headers are written after delivering the first
// buffer to the network IO thread. // buffer to the network IO thread.
if (!this._started) { if (!this._started) {
this._url_request.setChunkedUpload(this.chunkedEncoding); this._url_request.setChunkedUpload(this.chunkedEncoding)
this._started = true; this._started = true
} }
// The write callback is fired asynchronously to mimic Node.js. // The write callback is fired asynchronously to mimic Node.js.
if (callback) { if (callback) {
process.nextTick(callback); process.nextTick(callback)
} }
return result; return result
} }
write(data, encoding, callback) { write (data, encoding, callback) {
if (this._finished) { if (this._finished) {
let error = new Error('Write after end.'); let error = new Error('Write after end.')
process.nextTick(writeAfterEndNT, this, error, callback); process.nextTick(writeAfterEndNT, this, error, callback)
return true; return true
} }
return this._write(data, encoding, callback, false); return this._write(data, encoding, callback, false)
} }
end (data, encoding, callback) {
end(data, encoding, callback) {
if (this._finished) { if (this._finished) {
return false; return false
} }
this._finished = true; this._finished = true
if (typeof data === 'function') { if (typeof data === 'function') {
callback = data; callback = data
encoding = null; encoding = null
data = null; data = null
} else if (typeof encoding === 'function') { } else if (typeof encoding === 'function') {
callback = encoding; callback = encoding
encoding = null; encoding = null
} }
data = data || ''; data = data || ''
return this._write(data, encoding, callback, true); return this._write(data, encoding, callback, true)
} }
abort() { abort () {
if (!this._started) { if (!this._started) {
// Does nothing if stream // Does nothing if stream
return; return
} }
if (!this._aborted) { if (!this._aborted) {
this._url_request.abort(); this._url_request.abort()
this._aborted = true; this._aborted = true
process.nextTick( ()=>{ process.nextTick(() => {
this.emit('abort'); this.emit('abort')
} ); })
} }
} }
} }
function writeAfterEndNT(self, error, callback) { function writeAfterEndNT (self, error, callback) {
self.emit('error', error); self.emit('error', error)
if (callback) callback(error); if (callback) callback(error)
} }
Net.prototype.request = function (options, callback) {
Net.prototype.request = function(options, callback) { return new ClientRequest(options, callback)
return new ClientRequest(options, callback);
} }
net.ClientRequest = ClientRequest; net.ClientRequest = ClientRequest
module.exports = net module.exports = net