Skip to content

Commit e657aa3

Browse files
committed
Improve Agent example, move to end
Move the `Agent` part of the docs to the end since it's only a stub impl and really isn't all that useful. Also improve the example to show how one might actually use it.
1 parent c76c5bd commit e657aa3

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

src/content/docs/workers/runtime-apis/nodejs/http.mdx

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,6 @@ import { Render } from "~/components"
77

88
<Render file="nodejs-compat-howto" />
99

10-
## Agent
11-
12-
An implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class.
13-
14-
An [Agent](https://nodejs.org/docs/latest/api/http.html#class-httpagent) manages HTTP connection reuse by maintaining request queues per host/port. In the
15-
workers environment, however, such low-level management of the network connection, ports,
16-
etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the
17-
implementation of `Agent` in Workers is a stub implementation that does not support connection
18-
pooling or keep-alive.
19-
20-
```js
21-
import { Agent } from 'node:http';
22-
import { strictEqual } from 'node:assert';
23-
24-
const agent = new Agent();
25-
strictEqual(agent.protocol, 'http:');
26-
```
27-
2810
## get
2911

3012
An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api/http.html#httpgetoptions-callback) method.
@@ -123,3 +105,31 @@ get('http://docs.cloudflare.com', (res) => {
123105
ok(res instanceof IncomingMessage);
124106
});
125107
```
108+
109+
## Agent
110+
111+
A partial implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class.
112+
113+
An `Agent` manages HTTP connection reuse by maintaining request queues per host/port. In the workers environment, however, such low-level management of the network connection, ports, etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the implementation of `Agent` in Workers is a stub implementation that does not support connection pooling or keep-alive.
114+
115+
```js
116+
import { get, Agent } from 'node:http';
117+
import { strictEqual } from 'node:assert';
118+
119+
export default {
120+
async fetch() {
121+
122+
const agent = new Agent();
123+
get({
124+
hostname: 'example.org',
125+
port: 80,
126+
path: '/',
127+
agent, // possible but not all that useful.
128+
}, (res) => {
129+
// ...
130+
});
131+
132+
return new Response('ok');
133+
}
134+
}
135+
```

src/content/docs/workers/runtime-apis/nodejs/https.mdx

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,6 @@ import { Render } from "~/components";
77

88
<Render file="nodejs-compat-howto" />
99

10-
## Agent
11-
12-
An implementation of the Node.js [`https.Agent'](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) class.
13-
14-
An [Agent](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) manages HTTPS connection reuse by maintaining request queues per host/port. In the
15-
workers environment, however, such low-level management of the network connection, ports,
16-
etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the
17-
implementation of `Agent` in Workers is a stub implementation that does not support connection
18-
pooling or keep-alive.
19-
20-
```js
21-
import { Agent } from "node:https";
22-
import { strictEqual } from "node:assert";
23-
24-
const agent = new Agent();
25-
strictEqual(agent.protocol, "https:");
26-
```
27-
2810
## get
2911

3012
An implementation of the Node.js [`https.get'](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method.
@@ -90,3 +72,31 @@ req.end();
9072
```
9173

9274
The following additional options are not supported: `ca`, `cert`, `ciphers`, `clientCertEngine` (deprecated), `crl`, `dhparam`, `ecdhCurve`, `honorCipherOrder`, `key`, `passphrase`, `pfx`, `rejectUnauthorized`, `secureOptions`, `secureProtocol`, `servername`, `sessionIdContext`, `highWaterMark`.
75+
76+
## Agent
77+
78+
An implementation of the Node.js [`https.Agent'](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) class.
79+
80+
An `Agent` manages HTTPS connection reuse by maintaining request queues per host/port. In the workers environment, however, such low-level management of the network connection, ports, etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the implementation of `Agent` in Workers is a stub implementation that does not support connection pooling or keep-alive.
81+
82+
```js
83+
import { get, Agent } from 'node:https';
84+
import { strictEqual } from 'node:assert';
85+
86+
export default {
87+
async fetch() {
88+
89+
const agent = new Agent();
90+
get({
91+
hostname: 'example.org',
92+
port: 443,
93+
path: '/',
94+
agent, // possible but not all that useful.
95+
}, (res) => {
96+
// ...
97+
});
98+
99+
return new Response('ok');
100+
}
101+
}
102+
```

0 commit comments

Comments
 (0)