-
-
Notifications
You must be signed in to change notification settings - Fork 206
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
Replace node-fetch
with Node.js' native fetch
#777
Replace node-fetch
with Node.js' native fetch
#777
Conversation
✅ Deploy Preview for testcontainers-node ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
node-fetch
in favor of Node.js' native fetchnode-fetch
in with Node.js' native fetch
node-fetch
in with Node.js' native fetch
node-fetch
with Node.js' native fetch
Hi @Kurre, thanks for raising. I see that the |
Yes, the reason is that the AFAIK there isn't other ways to properly handle this situation with |
b8a76f9
to
c289d84
Compare
fixed lint issues and added the |
packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts
Show resolved
Hide resolved
I used the afaik dropping the Node.js v16 support was the only breaking change of v6.x, https://github.com/nodejs/undici/releases/tag/v6.0.0 and it seems that v6.x has extended some interfaces (like We could also dynamically import the right undici version (5.x to v18 and 6.x to v20/v22) which would probably also work just fine, but would probably require some tweaking with the typings 🤔 Any thoughts on this @cristianrgreco ? Personally i'm open for all possible routes forward, since replacing the v2.x |
128e713
to
8ac3eac
Compare
|
Thanks @Kurre. I'm not thrilled with the addition of import https from 'https';
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
const response = await fetch(url, {
...
agent: httpsAgent,
}); Also was there a reason to add ES2022 to the libs? |
- Remove `node-fetch` and `@types/node-fetch` packages - Replace all `fetch()` calls to use native `fetch` - Replace `node-fetch`'s timeout with `AbortSignal` - Replace `https.Agent` with `Agent` from `undici` - Add `ES2022` as `lib` to `tsconfig.base.json` to have correct types for `fetch`
I'd like to avoid new deps if possible, but so far I haven't found a proper solution for it when using const undici = require("undici");
const https = require("node:https");
const url = "https://self-signed.badssl.com";
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
const undiciAgent = new undici.Agent({ connect: { rejectUnauthorized: false } });
fetch(url)
.then((res) => console.log("\n\nres", res))
.catch((err) => console.error("\n\nerror", err));
fetch(url, { agent: httpsAgent })
.then((res) => console.log("\n\nres with httpsAgent", res))
.catch((err) => console.error("\n\nerror with fetchWithAgent", err));
fetch(url, { dispatcher: undiciAgent })
.then((res) => console.log("\n\nres with dispatcher", res))
.catch((err) => console.error("\n\nerror with dispatcher", err)); and output is as follows: ❯ node node-fetch-test.js
error TypeError: fetch failed
at node:internal/deps/undici/undici:12502:13
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[cause]: Error: self-signed certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
at TLSSocket.emit (node:events:519:28)
at TLSSocket._finishInit (node:_tls_wrap:1085:8)
at ssl.onhandshakedone (node:_tls_wrap:871:12) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}
}
error with fetchWithAgent TypeError: fetch failed
at node:internal/deps/undici/undici:12502:13
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[cause]: Error: self-signed certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
at TLSSocket.emit (node:events:519:28)
at TLSSocket._finishInit (node:_tls_wrap:1085:8)
at ssl.onhandshakedone (node:_tls_wrap:871:12) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}
}
res with dispatcher Response {
status: 200,
statusText: 'OK',
headers: Headers {
server: 'nginx/1.10.3 (Ubuntu)',
date: 'Sun, 14 Jul 2024 09:08:13 GMT',
'content-type': 'text/html',
'last-modified': 'Fri, 17 May 2024 17:59:55 GMT',
'transfer-encoding': 'chunked',
connection: 'keep-alive',
etag: 'W/"66479b1b-1f6"',
'cache-control': 'no-store',
'content-encoding': 'gzip'
},
body: ReadableStream { locked: false, state: 'readable', supportsBYOB: true },
bodyUsed: false,
ok: true,
redirected: false,
type: 'basic',
url: 'https://self-signed.badssl.com/'
} and the edit: |
92ff8c8
to
5058c5a
Compare
node-fetch
and@types/node-fetch
packagesfetch()
calls to use nativefetch
node-fetch
's timeout withAbortSignal
https.Agent
withAgent
fromundici
ES2022
aslib
totsconfig.base.json
to have correct types forfetch
closes #765