|
@ -1,22 +1,21 @@ |
|
|
'use strict'; |
|
|
const EventEmitter = require('events').EventEmitter; |
|
|
var EventEmitter = require('events').EventEmitter; |
|
|
const http = require('http'); |
|
|
var http = require('http'); |
|
|
const https = require('https'); |
|
|
var https = require('https'); |
|
|
const urlLib = require('url'); |
|
|
var urlLib = require('url'); |
|
|
const querystring = require('querystring'); |
|
|
var querystring = require('querystring'); |
|
|
const objectAssign = require('object-assign'); |
|
|
var objectAssign = require('object-assign'); |
|
|
const duplexify = require('duplexify'); |
|
|
var duplexify = require('duplexify'); |
|
|
const isStream = require('is-stream'); |
|
|
var isStream = require('is-stream'); |
|
|
const readAllStream = require('read-all-stream'); |
|
|
var readAllStream = require('read-all-stream'); |
|
|
const timedOut = require('timed-out'); |
|
|
var timedOut = require('timed-out'); |
|
|
const urlParseLax = require('url-parse-lax'); |
|
|
var urlParseLax = require('url-parse-lax'); |
|
|
const lowercaseKeys = require('lowercase-keys'); |
|
|
var lowercaseKeys = require('lowercase-keys'); |
|
|
const isRedirect = require('is-redirect'); |
|
|
var isRedirect = require('is-redirect'); |
|
|
const unzipResponse = require('unzip-response'); |
|
|
var unzipResponse = require('unzip-response'); |
|
|
const createErrorClass = require('create-error-class'); |
|
|
var createErrorClass = require('create-error-class'); |
|
|
const nodeStatusCodes = require('node-status-codes'); |
|
|
var nodeStatusCodes = require('node-status-codes'); |
|
|
const isPlainObj = require('is-plain-obj'); |
|
|
var isPlainObj = require('is-plain-obj'); |
|
|
const parseJson = require('parse-json'); |
|
|
var parseJson = require('parse-json'); |
|
|
|
|
|
|
|
|
|
|
|
function requestAsEventEmitter(opts) { |
|
|
function requestAsEventEmitter(opts) { |
|
|
opts = opts || {}; |
|
|
opts = opts || {}; |
|
@ -25,10 +24,10 @@ function requestAsEventEmitter(opts) { |
|
|
var redirectCount = 0; |
|
|
var redirectCount = 0; |
|
|
var retryCount = 0; |
|
|
var retryCount = 0; |
|
|
|
|
|
|
|
|
var get = function (opts) { |
|
|
var get = opts => { |
|
|
var fn = opts.protocol === 'https:' ? https : http; |
|
|
var fn = opts.protocol === 'https:' ? https : http; |
|
|
|
|
|
|
|
|
var req = fn.request(opts, function (res) { |
|
|
var req = fn.request(opts, res => { |
|
|
var statusCode = res.statusCode; |
|
|
var statusCode = res.statusCode; |
|
|
|
|
|
|
|
|
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { |
|
|
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { |
|
@ -48,14 +47,14 @@ function requestAsEventEmitter(opts) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// do not write ee.bind(...) instead of function - it will break gzip in Node.js 0.10
|
|
|
|
|
|
setImmediate(function () { |
|
|
setImmediate(function () { |
|
|
ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res); |
|
|
ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
req.once('error', function (err) { |
|
|
req.once('error', err => { |
|
|
var backoff = opts.retries(++retryCount, err); |
|
|
const backoff = opts.retries(++retryCount, err); |
|
|
|
|
|
|
|
|
if (backoff) { |
|
|
if (backoff) { |
|
|
setTimeout(get, backoff, opts); |
|
|
setTimeout(get, backoff, opts); |
|
|
return; |
|
|
return; |
|
@ -68,7 +67,7 @@ function requestAsEventEmitter(opts) { |
|
|
timedOut(req, opts.timeout); |
|
|
timedOut(req, opts.timeout); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
setImmediate(ee.emit.bind(ee), 'request', req); |
|
|
setImmediate(() => ee.emit('request', req)); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
get(opts); |
|
|
get(opts); |
|
@ -78,7 +77,7 @@ function requestAsEventEmitter(opts) { |
|
|
function asCallback(opts, cb) { |
|
|
function asCallback(opts, cb) { |
|
|
var ee = requestAsEventEmitter(opts); |
|
|
var ee = requestAsEventEmitter(opts); |
|
|
|
|
|
|
|
|
ee.on('request', function (req) { |
|
|
ee.on('request', req => { |
|
|
if (isStream(opts.body)) { |
|
|
if (isStream(opts.body)) { |
|
|
opts.body.pipe(req); |
|
|
opts.body.pipe(req); |
|
|
opts.body = undefined; |
|
|
opts.body = undefined; |
|
@ -88,8 +87,8 @@ function asCallback(opts, cb) { |
|
|
req.end(opts.body); |
|
|
req.end(opts.body); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
ee.on('response', function (res) { |
|
|
ee.on('response', res => |
|
|
readAllStream(res, opts.encoding, function (err, data) { |
|
|
readAllStream(res, opts.encoding, (err, data) => { |
|
|
var statusCode = res.statusCode; |
|
|
var statusCode = res.statusCode; |
|
|
|
|
|
|
|
|
if (err) { |
|
|
if (err) { |
|
@ -111,31 +110,28 @@ function asCallback(opts, cb) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
cb(err, data, res); |
|
|
cb(err, data, res); |
|
|
}); |
|
|
}) |
|
|
}); |
|
|
); |
|
|
|
|
|
|
|
|
ee.on('error', cb); |
|
|
ee.on('error', cb); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function asPromise(opts) { |
|
|
function asPromise(opts) { |
|
|
return new Promise(function (resolve, reject) { |
|
|
return new Promise((resolve, reject) => |
|
|
asCallback(opts, function (err, data, response) { |
|
|
asCallback(opts, (err, data, response) => { |
|
|
if (response) { |
|
|
if (response) { |
|
|
response.body = data; |
|
|
response.body = data; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (err) { |
|
|
if (err) { |
|
|
Object.defineProperty(err, 'response', { |
|
|
Object.defineProperty(err, 'response', {value: response}); |
|
|
value: response, |
|
|
|
|
|
enumerable: false |
|
|
|
|
|
}); |
|
|
|
|
|
reject(err); |
|
|
reject(err); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
resolve(response); |
|
|
resolve(response); |
|
|
}); |
|
|
}) |
|
|
}); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function asStream(opts) { |
|
|
function asStream(opts) { |
|
@ -146,14 +142,14 @@ function asStream(opts) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (opts.body) { |
|
|
if (opts.body) { |
|
|
proxy.write = function () { |
|
|
proxy.write = () => { |
|
|
throw new Error('got\'s stream is not writable when options.body is used'); |
|
|
throw new Error('got\'s stream is not writable when options.body is used'); |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var ee = requestAsEventEmitter(opts); |
|
|
var ee = requestAsEventEmitter(opts); |
|
|
|
|
|
|
|
|
ee.on('request', function (req) { |
|
|
ee.on('request', req => { |
|
|
proxy.emit('request', req); |
|
|
proxy.emit('request', req); |
|
|
|
|
|
|
|
|
if (isStream.readable(opts.body)) { |
|
|
if (isStream.readable(opts.body)) { |
|
@ -174,7 +170,7 @@ function asStream(opts) { |
|
|
req.end(); |
|
|
req.end(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
ee.on('response', function (res) { |
|
|
ee.on('response', res => { |
|
|
var statusCode = res.statusCode; |
|
|
var statusCode = res.statusCode; |
|
|
|
|
|
|
|
|
proxy.setReadable(res); |
|
|
proxy.setReadable(res); |
|
@ -307,8 +303,8 @@ var helpers = [ |
|
|
'delete' |
|
|
'delete' |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
helpers.forEach(function (el) { |
|
|
helpers.forEach(el => { |
|
|
got[el] = function (url, opts, cb) { |
|
|
got[el] = (url, opts, cb) => { |
|
|
if (typeof opts === 'function') { |
|
|
if (typeof opts === 'function') { |
|
|
cb = opts; |
|
|
cb = opts; |
|
|
opts = {}; |
|
|
opts = {}; |
|
@ -326,7 +322,7 @@ got.stream = function (url, opts, cb) { |
|
|
return asStream(normalizeArguments(url, opts)); |
|
|
return asStream(normalizeArguments(url, opts)); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
helpers.forEach(function (el) { |
|
|
helpers.forEach(el => { |
|
|
got.stream[el] = function (url, opts) { |
|
|
got.stream[el] = function (url, opts) { |
|
|
return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()})); |
|
|
return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()})); |
|
|
}; |
|
|
}; |
|
|