Skip to content

fix: fetch undici agent support, node 16+ #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions lib/rest.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* global fetch:false */

'use strict';

const h = require('highland'),
_ = require('lodash'),
nodeUrl = require('url'),
https = require('https'),
{ Agent } = require('undici'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node 20 already has undici for its native fetch, but I think this is a good idea to maintain compatibility with other Node versions.

b64 = require('base-64'),
pluralize = require('pluralize'),
agent = new https.Agent({ rejectUnauthorized: false }), // allow self-signed certs
CONTENT_TYPES = {
json: 'application/json; charset=UTF-8',
text: 'text/plain; charset=UTF-8'
};

// isormorphic-fetch sets a global
require('isomorphic-fetch');
},
agent = new Agent({
connect: {
rejectUnauthorized: false,
}
});

/**
* get protocol to determine if we need https agent
Expand All @@ -25,6 +25,15 @@ function isSSL(url) {
return nodeUrl.parse(url).protocol === 'https:';
}

/**
* returns the fetch dispatcher by url protocol
* @param {string} url
* @returns {Agent}
*/
function getDispatcherByUrl(url) {
return isSSL(url) ? agent : new Agent();
}

/**
* catch errors in api calls
* @param {Error} error
Expand Down Expand Up @@ -77,7 +86,7 @@ function get(url, options = {}) {
return h(send(url, {
method: 'GET',
headers: options.headers,
agent: isSSL(url) ? agent : null
dispatcher: getDispatcherByUrl(url)
}).then((res) => {
if (res instanceof Error) {
res.url = url; // capture urls that we error on
Expand Down Expand Up @@ -124,7 +133,7 @@ function put(url, data, options = {}) {
method: 'PUT',
body: body,
headers: headers,
agent: isSSL(url) ? agent : null
dispatcher: getDispatcherByUrl(url)
}).then((res) => {
if (res instanceof Error) {
return { type: 'error', details: url, message: res.message };
Expand Down Expand Up @@ -160,7 +169,7 @@ function query(url, query, options = {}) {
method: 'POST',
body: JSON.stringify(query),
headers: headers,
agent: isSSL(url) ? agent : null
dispatcher: getDispatcherByUrl(url)
}).then((res) => {
if (res instanceof Error) {
return { type: 'error', details: url, message: res.message };
Expand Down Expand Up @@ -217,7 +226,7 @@ function recursivelyCheckURI(currentURL, publicURI, options) {
return send(possibleUrl, {
method: 'GET',
headers: options.headers,
agent: isSSL(possibleUrl) ? agent : null
dispatcher: getDispatcherByUrl(possibleUrl)
}).then((res) => res.text())
.then((uri) => ({ uri, prefix: possiblePrefix })) // return page uri and the prefix we discovered
.catch(() => {
Expand Down Expand Up @@ -253,13 +262,9 @@ function findURI(url, options = {}) {
function isElasticPrefix(url) {
return h(send(`${url}/_components`, {
method: 'GET',
agent: isSSL(url) ? agent : null
dispatcher: getDispatcherByUrl(url)
}).then((res) => {
if (res instanceof Error) {
return false;
} else {
return true;
}
return !(res instanceof Error);
}));
}

Expand Down
Loading