format: prettify entire project
This commit is contained in:
155
node_modules/simple-get/index.js
generated
vendored
155
node_modules/simple-get/index.js
generated
vendored
@ -1,108 +1,129 @@
|
||||
/*! simple-get. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
||||
module.exports = simpleGet
|
||||
module.exports = simpleGet;
|
||||
|
||||
const concat = require('simple-concat')
|
||||
const decompressResponse = require('decompress-response') // excluded from browser build
|
||||
const http = require('http')
|
||||
const https = require('https')
|
||||
const once = require('once')
|
||||
const querystring = require('querystring')
|
||||
const url = require('url')
|
||||
const concat = require('simple-concat');
|
||||
const decompressResponse = require('decompress-response'); // excluded from browser build
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const once = require('once');
|
||||
const querystring = require('querystring');
|
||||
const url = require('url');
|
||||
|
||||
const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'
|
||||
const isStream = (o) =>
|
||||
o !== null && typeof o === 'object' && typeof o.pipe === 'function';
|
||||
|
||||
function simpleGet (opts, cb) {
|
||||
opts = Object.assign({ maxRedirects: 10 }, typeof opts === 'string' ? { url: opts } : opts)
|
||||
cb = once(cb)
|
||||
function simpleGet(opts, cb) {
|
||||
opts = Object.assign(
|
||||
{ maxRedirects: 10 },
|
||||
typeof opts === 'string' ? { url: opts } : opts
|
||||
);
|
||||
cb = once(cb);
|
||||
|
||||
if (opts.url) {
|
||||
const { hostname, port, protocol, auth, path } = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
|
||||
delete opts.url
|
||||
if (!hostname && !port && !protocol && !auth) opts.path = path // Relative redirect
|
||||
else Object.assign(opts, { hostname, port, protocol, auth, path }) // Absolute redirect
|
||||
const { hostname, port, protocol, auth, path } = url.parse(opts.url); // eslint-disable-line node/no-deprecated-api
|
||||
delete opts.url;
|
||||
if (!hostname && !port && !protocol && !auth)
|
||||
opts.path = path; // Relative redirect
|
||||
else Object.assign(opts, { hostname, port, protocol, auth, path }); // Absolute redirect
|
||||
}
|
||||
|
||||
const headers = { 'accept-encoding': 'gzip, deflate' }
|
||||
if (opts.headers) Object.keys(opts.headers).forEach(k => (headers[k.toLowerCase()] = opts.headers[k]))
|
||||
opts.headers = headers
|
||||
const headers = { 'accept-encoding': 'gzip, deflate' };
|
||||
if (opts.headers)
|
||||
Object.keys(opts.headers).forEach(
|
||||
(k) => (headers[k.toLowerCase()] = opts.headers[k])
|
||||
);
|
||||
opts.headers = headers;
|
||||
|
||||
let body
|
||||
let body;
|
||||
if (opts.body) {
|
||||
body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body
|
||||
body =
|
||||
opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body;
|
||||
} else if (opts.form) {
|
||||
body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form)
|
||||
opts.headers['content-type'] = 'application/x-www-form-urlencoded'
|
||||
body =
|
||||
typeof opts.form === 'string' ?
|
||||
opts.form
|
||||
: querystring.stringify(opts.form);
|
||||
opts.headers['content-type'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
|
||||
if (body) {
|
||||
if (!opts.method) opts.method = 'POST'
|
||||
if (!isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body)
|
||||
if (opts.json && !opts.form) opts.headers['content-type'] = 'application/json'
|
||||
if (!opts.method) opts.method = 'POST';
|
||||
if (!isStream(body))
|
||||
opts.headers['content-length'] = Buffer.byteLength(body);
|
||||
if (opts.json && !opts.form)
|
||||
opts.headers['content-type'] = 'application/json';
|
||||
}
|
||||
delete opts.body; delete opts.form
|
||||
delete opts.body;
|
||||
delete opts.form;
|
||||
|
||||
if (opts.json) opts.headers.accept = 'application/json'
|
||||
if (opts.method) opts.method = opts.method.toUpperCase()
|
||||
if (opts.json) opts.headers.accept = 'application/json';
|
||||
if (opts.method) opts.method = opts.method.toUpperCase();
|
||||
|
||||
const originalHost = opts.hostname // hostname before potential redirect
|
||||
const protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
|
||||
const req = protocol.request(opts, res => {
|
||||
if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
opts.url = res.headers.location // Follow 3xx redirects
|
||||
delete opts.headers.host // Discard `host` header on redirect (see #32)
|
||||
res.resume() // Discard response
|
||||
const originalHost = opts.hostname; // hostname before potential redirect
|
||||
const protocol = opts.protocol === 'https:' ? https : http; // Support http/https urls
|
||||
const req = protocol.request(opts, (res) => {
|
||||
if (
|
||||
opts.followRedirects !== false &&
|
||||
res.statusCode >= 300 &&
|
||||
res.statusCode < 400 &&
|
||||
res.headers.location
|
||||
) {
|
||||
opts.url = res.headers.location; // Follow 3xx redirects
|
||||
delete opts.headers.host; // Discard `host` header on redirect (see #32)
|
||||
res.resume(); // Discard response
|
||||
|
||||
const redirectHost = url.parse(opts.url).hostname // eslint-disable-line node/no-deprecated-api
|
||||
const redirectHost = url.parse(opts.url).hostname; // eslint-disable-line node/no-deprecated-api
|
||||
// If redirected host is different than original host, drop headers to prevent cookie leak (#73)
|
||||
if (redirectHost !== null && redirectHost !== originalHost) {
|
||||
delete opts.headers.cookie
|
||||
delete opts.headers.authorization
|
||||
delete opts.headers.cookie;
|
||||
delete opts.headers.authorization;
|
||||
}
|
||||
|
||||
if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
|
||||
opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
|
||||
delete opts.headers['content-length']; delete opts.headers['content-type']
|
||||
opts.method = 'GET'; // On 301/302 redirect, change POST to GET (see #35)
|
||||
delete opts.headers['content-length'];
|
||||
delete opts.headers['content-type'];
|
||||
}
|
||||
|
||||
if (opts.maxRedirects-- === 0) return cb(new Error('too many redirects'))
|
||||
else return simpleGet(opts, cb)
|
||||
if (opts.maxRedirects-- === 0) return cb(new Error('too many redirects'));
|
||||
else return simpleGet(opts, cb);
|
||||
}
|
||||
|
||||
const tryUnzip = typeof decompressResponse === 'function' && opts.method !== 'HEAD'
|
||||
cb(null, tryUnzip ? decompressResponse(res) : res)
|
||||
})
|
||||
const tryUnzip =
|
||||
typeof decompressResponse === 'function' && opts.method !== 'HEAD';
|
||||
cb(null, tryUnzip ? decompressResponse(res) : res);
|
||||
});
|
||||
req.on('timeout', () => {
|
||||
req.abort()
|
||||
cb(new Error('Request timed out'))
|
||||
})
|
||||
req.on('error', cb)
|
||||
req.abort();
|
||||
cb(new Error('Request timed out'));
|
||||
});
|
||||
req.on('error', cb);
|
||||
|
||||
if (isStream(body)) body.on('error', cb).pipe(req)
|
||||
else req.end(body)
|
||||
if (isStream(body)) body.on('error', cb).pipe(req);
|
||||
else req.end(body);
|
||||
|
||||
return req
|
||||
return req;
|
||||
}
|
||||
|
||||
simpleGet.concat = (opts, cb) => {
|
||||
return simpleGet(opts, (err, res) => {
|
||||
if (err) return cb(err)
|
||||
if (err) return cb(err);
|
||||
concat(res, (err, data) => {
|
||||
if (err) return cb(err)
|
||||
if (err) return cb(err);
|
||||
if (opts.json) {
|
||||
try {
|
||||
data = JSON.parse(data.toString())
|
||||
data = JSON.parse(data.toString());
|
||||
} catch (err) {
|
||||
return cb(err, res, data)
|
||||
return cb(err, res, data);
|
||||
}
|
||||
}
|
||||
cb(null, res, data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
|
||||
cb(null, res, data);
|
||||
});
|
||||
});
|
||||
};
|
||||
['get', 'post', 'put', 'patch', 'head', 'delete'].forEach((method) => {
|
||||
simpleGet[method] = (opts, cb) => {
|
||||
if (typeof opts === 'string') opts = { url: opts }
|
||||
return simpleGet(Object.assign({ method: method.toUpperCase() }, opts), cb)
|
||||
}
|
||||
})
|
||||
if (typeof opts === 'string') opts = { url: opts };
|
||||
return simpleGet(Object.assign({ method: method.toUpperCase() }, opts), cb);
|
||||
};
|
||||
});
|
||||
|
Reference in New Issue
Block a user