Skip to content

Commit

Permalink
feat: add timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Feb 16, 2019
1 parent 70ece78 commit a2158e6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export function send(method, uri, opts={}) {
Object.assign(opts, typeof uri === 'string' ? parse(uri) : uri);
opts.agent = opts.protocol === 'http:' ? globalAgent : void 0;

let req = request(opts, r => {
if (opts.timeout) {
opts.timer = setTimeout(() => opts.req.abort(), opts.timeout);
delete opts.timeout;
}

let req = opts.req = request(opts, r => {
r.setEncoding('utf8');

r.on('data', d => {
Expand All @@ -24,6 +29,7 @@ export function send(method, uri, opts={}) {
}
r.data = out;
if (r.statusCode >= 400) {
clearTimeout(opts.timer);
let err = new Error(r.statusMessage);
err.statusMessage = r.statusMessage;
err.statusCode = r.statusCode;
Expand All @@ -32,14 +38,19 @@ export function send(method, uri, opts={}) {
rej(err);
} else if (r.statusCode > 300 && redirect && r.headers.location) {
opts.path = resolve(opts.path, r.headers.location);
return send(method, opts.path.startsWith('/') ? opts : opts.path, opts).then(res, rej);
send(method, opts.path.startsWith('/') ? opts : opts.path, opts).then(res, rej);
} else {
clearTimeout(opts.timer);
res(r);
}
});
});

req.on('error', rej);
req.on('error', err => {
// Node 11.x ~> boolean, else timestamp
err.aborted = req.aborted;
rej(err);
});

if (opts.body) {
let isObj = typeof opts.body === 'object' && !Buffer.isBuffer(opts.body);
Expand Down

0 comments on commit a2158e6

Please sign in to comment.