Skip to content

Commit

Permalink
refactor!: update http request options
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `httpOptions` return property `lookup` was renamed to `dnsLookup`.
BREAKING CHANGE: `httpOptions` return property `timeout` was removed, return an `AbortSignal` instance as `signal` property instead.
  • Loading branch information
panva committed Dec 1, 2022
1 parent 846a81d commit 2fd5eda
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
17 changes: 11 additions & 6 deletions certification/runner/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createWriteStream } from 'node:fs';
import stream from 'node:stream';
import { promisify } from 'node:util';

import Got from 'got';
import got from 'got'; // eslint-disable-line import/no-unresolved
import ms from 'ms';

import debug from './debug.js';
Expand All @@ -18,7 +18,7 @@ class API {
constructor({ baseUrl, bearerToken } = {}) {
assert(baseUrl, 'argument property "baseUrl" missing');

const { get, post } = Got.extend({
const { get, post } = got.extend({
prefixUrl: baseUrl,
throwHttpErrors: false,
followRedirect: false,
Expand All @@ -27,22 +27,27 @@ class API {
'content-type': 'application/json',
},
responseType: 'json',
retry: 0,
timeout: 10000,
retry: { limit: 0 },
https: {
rejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0',
},
});

this.get = get;
this.post = post;

this.stream = Got.extend({
this.stream = got.extend({
prefixUrl: baseUrl,
throwHttpErrors: false,
followRedirect: false,
headers: {
...(bearerToken ? { authorization: `bearer ${bearerToken}` } : undefined),
'content-type': 'application/json',
},
retry: 0,
retry: { limit: 0 },
https: {
rejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0',
},
}).stream;
}

Expand Down
8 changes: 4 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2346,17 +2346,17 @@ _**default value**_:

### httpOptions

Function called whenever calls to an external HTTP(S) resource are being made. You can change the request `timeout` duration, the `agent` used as well as the `lookup` resolver function.
Function called whenever calls to an external HTTP(S) resource are being made. You can change the request timeout through the `signal` option, the `agent` used as well as the `dnsLookup` resolver function.



_**default value**_:
```js
function httpOptions(url) {
return {
timeout: 2500,
signal: undefined, // defaults to AbortSignal.timeout(2500)
agent: undefined, // defaults to node's global agents (https.globalAgent or http.globalAgent)
lookup: undefined, // defaults to `dns.lookup()` (https://nodejs.org/api/dns.html#dnslookuphostname-options-callback)
dnsLookup: undefined, // defaults to `dns.lookup()` (https://nodejs.org/api/dns.html#dnslookuphostname-options-callback)
};
}
```
Expand All @@ -2369,7 +2369,7 @@ To change all request's timeout configure the httpOptions as a function like so:
```js
{
httpOptions(url) {
return { timeout: 5000 };
return { signal: AbortSignal.timeout(5000) };
}
}
```
Expand Down
10 changes: 5 additions & 5 deletions lib/helpers/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ async function extraTokenClaims(ctx, token) {

function httpOptions(url) {
return {
timeout: 2500,
signal: undefined, // defaults to AbortSignal.timeout(2500)
agent: undefined, // defaults to node's global agents (https.globalAgent or http.globalAgent)
lookup: undefined, // defaults to `dns.lookup()` (https://nodejs.org/api/dns.html#dnslookuphostname-options-callback)
dnsLookup: undefined, // defaults to `dns.lookup()` (https://nodejs.org/api/dns.html#dnslookuphostname-options-callback)
};
}

Expand Down Expand Up @@ -1914,8 +1914,8 @@ function makeDefaults() {
* httpOptions
*
* description: Function called whenever calls to an external HTTP(S) resource are being made.
* You can change the request `timeout` duration, the `agent` used as well as the `lookup`
* resolver function.
* You can change the request timeout through the `signal` option, the `agent` used as well as
* the `dnsLookup` resolver function.
*
* example: To change the request's timeout
*
Expand All @@ -1924,7 +1924,7 @@ function makeDefaults() {
* ```js
* {
* httpOptions(url) {
* return { timeout: 5000 };
* return { signal: AbortSignal.timeout(5000) };
* }
* }
* ```
Expand Down
30 changes: 20 additions & 10 deletions lib/helpers/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import got from 'got';
import dns from 'node:dns';
import http from 'node:http';
import https from 'node:https';

import got from 'got'; // eslint-disable-line import/no-unresolved

import pickBy from './_/pick_by.js';
import instance from './weak_cache.js';
Expand All @@ -7,30 +11,36 @@ export default async function request(options) {
Object.assign(options, {
url: new URL(options.url),
headers: options.headers || {},
https: {
rejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0',
},
});
// eslint-disable-next-line no-param-reassign
options.headers['user-agent'] = undefined;
const { timeout, agent, lookup } = instance(this).configuration('httpOptions')(new URL(options.url));
const helperOptions = pickBy({ timeout, agent, lookup }, Boolean);
const {
signal = AbortSignal.timeout(2500),
agent = options.url.protocol === 'http:' ? http.globalAgent : https.globalAgent,
dnsLookup = dns.lookup,
} = instance(this).configuration('httpOptions')(new URL(options.url));
const helperOptions = pickBy({ signal, agent, dnsLookup }, Boolean);

if (helperOptions.timeout !== undefined && typeof helperOptions.timeout !== 'number') {
throw new TypeError('"timeout" http request option must be a number');
if (helperOptions.signal !== undefined && !(helperOptions.signal instanceof AbortSignal)) {
throw new TypeError('"signal" http request option must be an AbortSignal');
}

if (helperOptions.agent !== undefined && typeof helperOptions.agent !== 'number') {
if (helperOptions.agent !== undefined) {
helperOptions.agent = { [options.url.protocol.slice(0, -1)]: helperOptions.agent };
}

if (helperOptions.lookup !== undefined && typeof helperOptions.lookup !== 'function') {
throw new TypeError('"lookup" http request option must be a function');
if (helperOptions.dnsLookup !== undefined && typeof helperOptions.dnsLookup !== 'function') {
throw new TypeError('"dnsLookup" http request option must be a function');
}

return got({
...options,
followRedirect: false,
retry: 0,
retry: { limit: 0 },
throwHttpErrors: false,
timeout: 2500,
...helperOptions,
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@koa/cors": "^4.0.0",
"debug": "^4.3.4",
"ejs": "^3.1.8",
"got": "^11.8.5",
"got": "^12.5.3",
"jose": "^4.10.3",
"jsesc": "^3.0.2",
"koa": "^2.13.4",
Expand Down
2 changes: 1 addition & 1 deletion test/client_auth/client_auth.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'node:fs';

import got from 'got';
import got from 'got'; // eslint-disable-line import/no-unresolved
import nock from 'nock';
import jose from 'jose2';
import { importJWK } from 'jose';
Expand Down

0 comments on commit 2fd5eda

Please sign in to comment.