Skip to content

Commit f40d35a

Browse files
committed
feat: support undici v7
1 parent c15b908 commit f40d35a

17 files changed

+1244
-44
lines changed

MIGRATION.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
11
# MIGRATION GUIDES
22

3+
## Migration guide from v6.x.x to v7.0.0
4+
5+
- [Node.js v18 is no longer supported.](#nodejs-v18-is-no-longer-supported)
6+
- [When using global fetch with Node.js v22, v23, v24, undici v6 should be installed.](#when-using-global-fetch-with-nodejs-v22-v23-v24-undici-v6-should-be-installed)
7+
- [undici v7 supports](#undici-v7-supports)
8+
9+
### Node.js v18 is no longer supported.
10+
11+
Node.js v18 is no longer supported.
12+
13+
### When using global fetch with Node.js v22, v23, v24, undici v6 should be installed.
14+
15+
The global fetch built-in to Node.js v20, v22, and v23 is undici v6.
16+
17+
So, you should install undici v6 and then load `CookieAgent` from `http-cookie-agent/undici/v6`.
18+
19+
```diff
20+
- import { CookieAgent } from 'http-cookie-agent/undici';
21+
+ import { CookieAgent } from 'http-cookie-agent/undici/v6';
22+
```
23+
24+
### undici v7 supports
25+
26+
When using `CookieClient` or `createCookieClient`, you should replace it with the compose method.
27+
28+
```diff
29+
- import { CookieClient } from 'http-cookie-agent/undici';
30+
+ import { cookie } from 'http-cookie-agent/undici';
31+
+ import { Client } from 'undici';
32+
33+
- const client = new CookieClient('https://example.com', { cookie: { jar } });
34+
+ const client = new Client('https://example.com').compose(cookie({ jar }));
35+
```
36+
37+
When using `CookieAgent`, you can use the code as is.
38+
If you want to redirect like undici v6, you can add a "redirect" feature with the compose method.
39+
40+
```typescript
41+
import { CookieAgent } from 'http-cookie-agent/undici';
42+
import { CookieJar } from 'tough-cookie';
43+
import { fetch, interceptors } from 'undici';
44+
45+
const jar = new CookieJar();
46+
const agent = new CookieAgent({ cookies: { jar } }).compose(interceptors.redirect());
47+
48+
await fetch('https://example.com', { dispatcher: agent, maxRedirections: 3 });
49+
```
50+
351
## Migration guide from v2.x.x to v4.0.0
452

553
- [The import path has been changed.](#the-import-path-has-been-changed)
6-
- [The property name for passing cookiejar to agent has been changed.](#the-property-name-for-passing-cookiejar-to-agent-has-been-changed)
54+
- [The property name for passing CookieJar to Agent has been changed.](#the-property-name-for-passing-cookiejar-to-agent-has-been-changed)
755
- [Using synchronous CookieJar functions by default.](#using-synchronous-cookiejar-functions-by-default)
856

957
### The import path has been changed.

README.md

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Allows cookies with every Node.js HTTP clients (e.g. Node.js global fetch, undic
1414
- [Install](#install)
1515
- [Usage](#usage)
1616
- [Supported libraries](#supported-libraries)
17-
- [Using with an asynchronous Cookie store](#using-with-an-asynchronous-cookie-store)
1817
- [Using with another Agent library](#using-with-another-agent-library)
1918
- [Contributing](#contributing)
2019
- [License](#license)
@@ -86,6 +85,23 @@ const agent = new CookieAgent({ cookies: { jar } });
8685
await fetch('https://example.com', { dispatcher: agent });
8786
```
8887

88+
Alternatively, `http-cookie-agent` can be used as [interceptors](https://github.com/nodejs/undici/blob/v7.0.0/docs/docs/api/Dispatcher.md#dispatchercomposeinterceptors-interceptor).
89+
In this case, the `cookie()` must be placed at the beginning of the interceptors.
90+
91+
```js
92+
import { fetch, interceptors } from 'undici';
93+
import { CookieJar } from 'tough-cookie';
94+
import { cookie } from 'http-cookie-agent/undici';
95+
96+
const jar = new CookieJar();
97+
const agent = new Agent()
98+
.compose(cookie({ jar }))
99+
.compose(interceptors.retry())
100+
.compose(interceptors.redirect({ maxRedirections: 3 }));
101+
102+
await fetch('https://example.com', { dispatcher: agent });
103+
```
104+
89105
#### `node:http` / `node:https`
90106

91107
```js
@@ -258,7 +274,7 @@ await client.get('https://example.com');
258274
```js
259275
import { request, setGlobalDispatcher } from 'urllib';
260276
import { CookieJar } from 'tough-cookie';
261-
import { CookieClient } from 'http-cookie-agent/undici';
277+
import { CookieAgent } from 'http-cookie-agent/undici';
262278

263279
const jar = new CookieJar();
264280
const agent = new CookieAgent({ cookies: { jar } });
@@ -290,44 +306,17 @@ https.get('https://example.com', { agent }, (res) => {
290306

291307
#### `undici`
292308

293-
If you want to use another undici Agent library, use `CookieClient` via factory function.
309+
If you want to use another undici Agent library, use `cookie` with the compose method.
294310

295311
```js
296312
import { fetch, ProxyAgent } from 'undici';
297313
import { CookieJar } from 'tough-cookie';
298-
import { CookieClient } from 'http-cookie-agent/undici';
314+
import { cookie } from 'http-cookie-agent/undici';
299315

300316
const jar = new CookieJar();
301317
const agent = new ProxyAgent({
302-
factory: (origin, opts) => {
303-
return new CookieClient(origin, {
304-
...opts,
305-
cookies: { jar },
306-
});
307-
},
308-
});
309-
310-
await fetch('https://example.com', { dispatcher: agent });
311-
```
312-
313-
If you want to use another undici Client library, wrap the client in `createCookieClient`.
314-
315-
```js
316-
import { fetch, Agent, MockClient } from 'undici';
317-
import { CookieJar } from 'tough-cookie';
318-
import { createCookieClient } from 'http-cookie-agent/undici';
319-
320-
const CookieClient = createCookieClient(MockClient);
321-
322-
const jar = new CookieJar();
323-
const agent = new Agent({
324-
factory: (origin, opts) => {
325-
return new CookieClient(origin, {
326-
...opts,
327-
cookies: { jar },
328-
});
329-
},
330-
});
318+
/* ... */
319+
}).compose(cookie({ jar }));
331320

332321
await fetch('https://example.com', { dispatcher: agent });
333322
```

examples/undici/proxy.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import http from 'node:http';
22

3-
import { CookieClient } from 'http-cookie-agent/undici';
3+
import { cookie } from 'http-cookie-agent/undici';
44
import { createProxy } from 'proxy';
55
import { CookieJar } from 'tough-cookie';
66
import { fetch, ProxyAgent } from 'undici';
@@ -11,9 +11,8 @@ proxyServer.listen(9000);
1111

1212
const jar = new CookieJar();
1313
const agent = new ProxyAgent({
14-
factory: (origin, /** @type {object} */ opts) => new CookieClient(origin, { ...opts, cookies: { jar } }),
1514
uri: 'http://127.0.0.1:9000',
16-
});
15+
}).compose(cookie({ jar }));
1716

1817
await fetch('https://httpbin.org/cookies/set/session/userid', { dispatcher: agent });
1918

examples/undici/with_interceptor.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CookieAgent } from 'http-cookie-agent/undici';
2+
import { CookieJar } from 'tough-cookie';
3+
import { fetch, interceptors } from 'undici';
4+
5+
const jar = new CookieJar();
6+
const agent = new CookieAgent({ cookies: { jar } }).compose(
7+
interceptors.retry(),
8+
interceptors.redirect({ maxRedirections: 3 }),
9+
);
10+
11+
await fetch('https://httpbin.org/cookies/set/session/userid', { dispatcher: agent });
12+
13+
const cookies = await jar.getCookies('https://httpbin.org');
14+
console.log(cookies);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@
9797
"typescript": "5.8.3",
9898
"undici": "7.8.0",
9999
"undici@v6": "npm:[email protected]",
100-
"urllib": "4.4.0"
100+
"urllib": "4.6.11"
101101
},
102102
"peerDependencies": {
103103
"tough-cookie": "^4.0.0 || ^5.0.0",
104-
"undici": "^5.11.0 || ^6.0.0"
104+
"undici": "^7.0.0"
105105
},
106106
"peerDependenciesMeta": {
107107
"undici": {

pnpm-lock.yaml

Lines changed: 37 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)