2016-09-15 13:59:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const url = require('url')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
const { Readable } = require('stream')
|
|
|
|
const { app } = require('electron')
|
2019-03-18 19:37:06 +00:00
|
|
|
const { Session } = process.electronBinding('session')
|
|
|
|
const { net, Net } = process.electronBinding('net')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { URLRequest } = net
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2017-11-02 04:05:27 +00:00
|
|
|
// Net is an EventEmitter.
|
2016-09-19 09:21:09 +00:00
|
|
|
Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
|
2017-11-02 04:05:27 +00:00
|
|
|
EventEmitter.call(net)
|
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
|
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
const kSupportedProtocols = new Set(['http:', 'https:'])
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-04-02 14:41:19 +00:00
|
|
|
// set of headers that Node.js discards duplicates for
|
|
|
|
// see https://nodejs.org/api/http.html#http_message_headers
|
|
|
|
const discardableDuplicateHeaders = new Set([
|
|
|
|
'content-type',
|
|
|
|
'content-length',
|
|
|
|
'user-agent',
|
|
|
|
'referer',
|
|
|
|
'host',
|
|
|
|
'authorization',
|
|
|
|
'proxy-authorization',
|
|
|
|
'if-modified-since',
|
|
|
|
'if-unmodified-since',
|
|
|
|
'from',
|
|
|
|
'location',
|
|
|
|
'max-forwards',
|
|
|
|
'retry-after',
|
|
|
|
'etag',
|
|
|
|
'last-modified',
|
|
|
|
'server',
|
|
|
|
'age',
|
|
|
|
'expires'
|
|
|
|
])
|
|
|
|
|
2016-10-06 12:14:05 +00:00
|
|
|
class IncomingMessage extends Readable {
|
2016-09-29 13:24:28 +00:00
|
|
|
constructor (urlRequest) {
|
|
|
|
super()
|
2016-10-13 15:14:23 +00:00
|
|
|
this.urlRequest = urlRequest
|
|
|
|
this.shouldPush = false
|
|
|
|
this.data = []
|
|
|
|
this.urlRequest.on('data', (event, chunk) => {
|
2016-10-06 12:14:05 +00:00
|
|
|
this._storeInternalData(chunk)
|
|
|
|
this._pushInternalData()
|
|
|
|
})
|
2016-10-13 15:14:23 +00:00
|
|
|
this.urlRequest.on('end', () => {
|
2016-10-06 12:14:05 +00:00
|
|
|
this._storeInternalData(null)
|
|
|
|
this._pushInternalData()
|
|
|
|
})
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get statusCode () {
|
2016-10-13 15:14:23 +00:00
|
|
|
return this.urlRequest.statusCode
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get statusMessage () {
|
2016-10-13 15:14:23 +00:00
|
|
|
return this.urlRequest.statusMessage
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get headers () {
|
2019-04-02 14:41:19 +00:00
|
|
|
const filteredHeaders = {}
|
|
|
|
const rawHeaders = this.urlRequest.rawResponseHeaders
|
|
|
|
Object.keys(rawHeaders).forEach(header => {
|
|
|
|
if (header in filteredHeaders && discardableDuplicateHeaders.has(header)) {
|
|
|
|
// do nothing with discardable duplicate headers
|
|
|
|
} else {
|
|
|
|
if (header === 'set-cookie') {
|
|
|
|
// keep set-cookie as an array per Node.js rules
|
|
|
|
// see https://nodejs.org/api/http.html#http_message_headers
|
|
|
|
filteredHeaders[header] = rawHeaders[header]
|
|
|
|
} else {
|
|
|
|
// for non-cookie headers, the values are joined together with ', '
|
|
|
|
filteredHeaders[header] = rawHeaders[header].join(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return filteredHeaders
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersion () {
|
|
|
|
return `${this.httpVersionMajor}.${this.httpVersionMinor}`
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersionMajor () {
|
2016-10-13 15:14:23 +00:00
|
|
|
return this.urlRequest.httpVersionMajor
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersionMinor () {
|
2016-10-13 15:14:23 +00:00
|
|
|
return this.urlRequest.httpVersionMinor
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
get rawTrailers () {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('HTTP trailers are not supported')
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
get trailers () {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('HTTP trailers are not supported')
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_storeInternalData (chunk) {
|
2016-10-13 15:14:23 +00:00
|
|
|
this.data.push(chunk)
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_pushInternalData () {
|
2016-10-13 15:14:23 +00:00
|
|
|
while (this.shouldPush && this.data.length > 0) {
|
|
|
|
const chunk = this.data.shift()
|
|
|
|
this.shouldPush = this.push(chunk)
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_read () {
|
2016-10-13 15:14:23 +00:00
|
|
|
this.shouldPush = true
|
2016-10-06 12:14:05 +00:00
|
|
|
this._pushInternalData()
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
URLRequest.prototype._emitRequestEvent = function (isAsync, ...rest) {
|
|
|
|
if (isAsync) {
|
2016-10-04 15:12:17 +00:00
|
|
|
process.nextTick(() => {
|
2016-12-01 22:37:03 +00:00
|
|
|
this.clientRequest.emit(...rest)
|
2016-10-04 15:12:17 +00:00
|
|
|
})
|
|
|
|
} else {
|
2016-12-01 22:37:03 +00:00
|
|
|
this.clientRequest.emit(...rest)
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
URLRequest.prototype._emitResponseEvent = function (isAsync, ...rest) {
|
|
|
|
if (isAsync) {
|
2016-10-04 15:12:17 +00:00
|
|
|
process.nextTick(() => {
|
2016-12-01 22:37:03 +00:00
|
|
|
this._response.emit(...rest)
|
2016-10-04 15:12:17 +00:00
|
|
|
})
|
|
|
|
} else {
|
2016-12-01 22:37:03 +00:00
|
|
|
this._response.emit(...rest)
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
class ClientRequest extends EventEmitter {
|
2016-09-29 13:24:28 +00:00
|
|
|
constructor (options, callback) {
|
|
|
|
super()
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-14 15:57:37 +00:00
|
|
|
if (!app.isReady()) {
|
|
|
|
throw new Error('net module can only be used after app is ready')
|
|
|
|
}
|
|
|
|
|
2016-09-27 10:33:51 +00:00
|
|
|
if (typeof options === 'string') {
|
2016-09-29 13:24:28 +00:00
|
|
|
options = url.parse(options)
|
2016-09-27 10:33:51 +00:00
|
|
|
} else {
|
2017-11-20 17:48:33 +00:00
|
|
|
options = Object.assign({}, options)
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const method = (options.method || 'GET').toUpperCase()
|
|
|
|
let urlStr = options.url
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
if (!urlStr) {
|
2018-10-02 01:56:31 +00:00
|
|
|
const urlObj = {}
|
2016-10-07 09:07:49 +00:00
|
|
|
const protocol = options.protocol || 'http:'
|
2016-09-27 10:33:51 +00:00
|
|
|
if (!kSupportedProtocols.has(protocol)) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('Protocol "' + protocol + '" not supported')
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2016-09-29 13:24:28 +00:00
|
|
|
urlObj.protocol = protocol
|
2016-09-27 10:33:51 +00:00
|
|
|
|
|
|
|
if (options.host) {
|
2016-09-29 13:24:28 +00:00
|
|
|
urlObj.host = options.host
|
2016-09-27 10:33:51 +00:00
|
|
|
} else {
|
|
|
|
if (options.hostname) {
|
2016-09-29 13:24:28 +00:00
|
|
|
urlObj.hostname = options.hostname
|
2016-09-27 10:33:51 +00:00
|
|
|
} else {
|
2016-09-29 13:24:28 +00:00
|
|
|
urlObj.hostname = 'localhost'
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.port) {
|
2016-09-29 13:24:28 +00:00
|
|
|
urlObj.port = options.port
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.path && / /.test(options.path)) {
|
|
|
|
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
|
|
|
// with an additional rule for ignoring percentage-escaped characters
|
|
|
|
// but that's a) hard to capture in a regular expression that performs
|
|
|
|
// well, and b) possibly too restrictive for real-world usage. That's
|
|
|
|
// why it only scans for spaces because those are guaranteed to create
|
|
|
|
// an invalid request.
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new TypeError('Request path contains unescaped characters')
|
2016-10-12 10:29:25 +00:00
|
|
|
}
|
2018-10-02 01:56:31 +00:00
|
|
|
const pathObj = url.parse(options.path || '/')
|
2016-10-12 10:29:25 +00:00
|
|
|
urlObj.pathname = pathObj.pathname
|
2016-10-10 14:21:12 +00:00
|
|
|
urlObj.search = pathObj.search
|
|
|
|
urlObj.hash = pathObj.hash
|
2016-09-29 13:24:28 +00:00
|
|
|
urlStr = url.format(urlObj)
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
const redirectPolicy = options.redirect || 'follow'
|
|
|
|
if (!['follow', 'error', 'manual'].includes(redirectPolicy)) {
|
|
|
|
throw new Error('redirect mode should be one of follow, error or manual')
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const urlRequestOptions = {
|
2016-09-27 10:33:51 +00:00
|
|
|
method: method,
|
2017-03-23 19:37:54 +00:00
|
|
|
url: urlStr,
|
|
|
|
redirect: redirectPolicy
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
|
|
|
if (options.session) {
|
2016-10-25 14:19:26 +00:00
|
|
|
if (options.session instanceof Session) {
|
2016-10-25 13:47:54 +00:00
|
|
|
urlRequestOptions.session = options.session
|
|
|
|
} else {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new TypeError('`session` should be an instance of the Session class')
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
|
|
|
} else if (options.partition) {
|
|
|
|
if (typeof options.partition === 'string') {
|
|
|
|
urlRequestOptions.partition = options.partition
|
|
|
|
} else {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new TypeError('`partition` should be a string')
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const urlRequest = new URLRequest(urlRequestOptions)
|
2016-09-27 10:33:51 +00:00
|
|
|
|
|
|
|
// Set back and forward links.
|
2016-10-13 15:14:23 +00:00
|
|
|
this.urlRequest = urlRequest
|
|
|
|
urlRequest.clientRequest = this
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2016-10-10 14:21:12 +00:00
|
|
|
// 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.
|
2016-10-13 15:14:23 +00:00
|
|
|
this.extraHeaders = {}
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2016-09-27 10:33:51 +00:00
|
|
|
if (options.headers) {
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const key in options.headers) {
|
2016-09-29 13:24:28 +00:00
|
|
|
this.setHeader(key, options.headers[key])
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
// Set when the request uses chunked encoding. Can be switched
|
2016-09-27 08:21:11 +00:00
|
|
|
// to true only once and never set back to false.
|
2016-10-13 15:14:23 +00:00
|
|
|
this.chunkedEncodingEnabled = false
|
2016-09-26 12:03:49 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
urlRequest.on('response', () => {
|
|
|
|
const response = new IncomingMessage(urlRequest)
|
|
|
|
urlRequest._response = response
|
|
|
|
this.emit('response', response)
|
|
|
|
})
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-10-20 09:43:26 +00:00
|
|
|
urlRequest.on('login', (event, authInfo, callback) => {
|
|
|
|
this.emit('login', authInfo, (username, password) => {
|
2016-10-31 15:59:26 +00:00
|
|
|
// If null or undefined username/password, force to empty string.
|
2016-10-20 09:43:26 +00:00
|
|
|
if (username === null || username === undefined) {
|
|
|
|
username = ''
|
|
|
|
}
|
|
|
|
if (typeof username !== 'string') {
|
|
|
|
throw new Error('username must be a string')
|
|
|
|
}
|
|
|
|
if (password === null || password === undefined) {
|
|
|
|
password = ''
|
|
|
|
}
|
|
|
|
if (typeof password !== 'string') {
|
|
|
|
throw new Error('password must be a string')
|
|
|
|
}
|
|
|
|
callback(username, password)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
if (callback) {
|
|
|
|
this.once('response', callback)
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get chunkedEncoding () {
|
2016-10-13 15:14:23 +00:00
|
|
|
return this.chunkedEncodingEnabled
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
set chunkedEncoding (value) {
|
2016-10-13 15:14:23 +00:00
|
|
|
if (!this.urlRequest.notStarted) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('Can\'t set the transfer encoding, headers have been sent')
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2016-10-13 15:14:23 +00:00
|
|
|
this.chunkedEncodingEnabled = value
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
setHeader (name, value) {
|
|
|
|
if (typeof name !== 'string') {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new TypeError('`name` should be a string in setHeader(name, value)')
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2017-03-30 16:24:40 +00:00
|
|
|
if (value == null) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('`value` required in setHeader("' + name + '", value)')
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2016-10-13 15:14:23 +00:00
|
|
|
if (!this.urlRequest.notStarted) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('Can\'t set headers after they are sent')
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const key = name.toLowerCase()
|
2016-10-13 15:14:23 +00:00
|
|
|
this.extraHeaders[key] = value
|
2017-03-30 16:24:40 +00:00
|
|
|
this.urlRequest.setExtraHeader(name, value.toString())
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
getHeader (name) {
|
2017-03-30 19:37:40 +00:00
|
|
|
if (name == null) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('`name` is required for getHeader(name)')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
if (!this.extraHeaders) {
|
2016-09-29 13:24:28 +00:00
|
|
|
return
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const key = name.toLowerCase()
|
2016-10-13 15:14:23 +00:00
|
|
|
return this.extraHeaders[key]
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
removeHeader (name) {
|
2017-03-30 19:37:40 +00:00
|
|
|
if (name == null) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('`name` is required for removeHeader(name)')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
if (!this.urlRequest.notStarted) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('Can\'t remove headers after they are sent')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const key = name.toLowerCase()
|
2016-10-13 15:14:23 +00:00
|
|
|
delete this.extraHeaders[key]
|
|
|
|
this.urlRequest.removeExtraHeader(name)
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
_write (chunk, encoding, callback, isLast) {
|
2018-10-02 01:56:31 +00:00
|
|
|
const chunkIsString = typeof chunk === 'string'
|
|
|
|
const chunkIsBuffer = chunk instanceof Buffer
|
2016-09-29 13:24:28 +00:00
|
|
|
if (!chunkIsString && !chunkIsBuffer) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new TypeError('First argument must be a string or Buffer')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
if (chunkIsString) {
|
2016-09-27 08:21:11 +00:00
|
|
|
// We convert all strings into binary buffers.
|
2016-09-29 13:24:28 +00:00
|
|
|
chunk = Buffer.from(chunk, encoding)
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
// Since writing to the network is asynchronous, we conservatively
|
2016-09-27 08:21:11 +00:00
|
|
|
// assume that request headers are written after delivering the first
|
|
|
|
// buffer to the network IO thread.
|
2016-10-13 15:14:23 +00:00
|
|
|
if (this.urlRequest.notStarted) {
|
|
|
|
this.urlRequest.setChunkedUpload(this.chunkedEncoding)
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 09:07:49 +00:00
|
|
|
// Headers are assumed to be sent on first call to _writeBuffer,
|
|
|
|
// i.e. after the first call to write or end.
|
2018-10-02 01:56:31 +00:00
|
|
|
const result = this.urlRequest.write(chunk, isLast)
|
2016-10-07 09:07:49 +00:00
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
// The write callback is fired asynchronously to mimic Node.js.
|
|
|
|
if (callback) {
|
2016-09-29 13:24:28 +00:00
|
|
|
process.nextTick(callback)
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
return result
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
write (data, encoding, callback) {
|
2016-10-13 15:14:23 +00:00
|
|
|
if (this.urlRequest.finished) {
|
2019-06-14 23:26:07 +00:00
|
|
|
const error = new Error('Write after end')
|
2016-09-29 13:24:28 +00:00
|
|
|
process.nextTick(writeAfterEndNT, this, error, callback)
|
|
|
|
return true
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-26 12:03:49 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
return this._write(data, encoding, callback, false)
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
end (data, encoding, callback) {
|
2016-10-13 15:14:23 +00:00
|
|
|
if (this.urlRequest.finished) {
|
2016-09-29 13:24:28 +00:00
|
|
|
return false
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-29 13:24:28 +00:00
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
if (typeof data === 'function') {
|
2016-09-29 13:24:28 +00:00
|
|
|
callback = data
|
|
|
|
encoding = null
|
|
|
|
data = null
|
2016-09-27 08:21:11 +00:00
|
|
|
} else if (typeof encoding === 'function') {
|
2016-09-29 13:24:28 +00:00
|
|
|
callback = encoding
|
|
|
|
encoding = null
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
data = data || ''
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
return this._write(data, encoding, callback, true)
|
2016-09-26 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
followRedirect () {
|
|
|
|
this.urlRequest.followRedirect()
|
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
abort () {
|
2016-10-13 15:14:23 +00:00
|
|
|
this.urlRequest.cancel()
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
2018-07-27 14:15:48 +00:00
|
|
|
|
|
|
|
getUploadProgress () {
|
|
|
|
return this.urlRequest.getUploadProgress()
|
|
|
|
}
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
function writeAfterEndNT (self, error, callback) {
|
|
|
|
self.emit('error', error)
|
|
|
|
if (callback) callback(error)
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
Net.prototype.request = function (options, callback) {
|
|
|
|
return new ClientRequest(options, callback)
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
net.ClientRequest = ClientRequest
|
2016-09-27 08:21:11 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
module.exports = net
|