Skip to content

Commit fae7c6f

Browse files
authored
Add node:http and node:https modules (#23481)
1 parent 595d2f8 commit fae7c6f

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
pcx_content_type: configuration
3+
title: http
4+
---
5+
6+
import { Render } from "~/components"
7+
8+
<Render file="nodejs-compat-howto" />
9+
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+
28+
## get
29+
30+
An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api/http.html#httpgetoptions-callback) method.
31+
32+
The `get` method performs a GET request to the specified URL and invokes the callback with the response. It's a convenience method that simplifies making HTTP GET requests without manually configuring request options.
33+
34+
```js
35+
import { get } from 'node:http';
36+
import { strictEqual, ok } from 'node:assert';
37+
38+
get('http://docs.cloudflare.com/robots.txt', (res) => {
39+
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
40+
strictEqual(res.statusCode, 301);
41+
let data = '';
42+
res.setEncoding('utf8');
43+
res.on('data', (chunk) => {
44+
data += chunk;
45+
});
46+
res.on('end', () => {
47+
ok(data.includes('301 Moved Permanently'));
48+
});
49+
});
50+
```
51+
52+
## request
53+
54+
An implementation of the Node.js [`http.request'](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method.
55+
56+
The `request` method creates an HTTP request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a [writable stream](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data.
57+
58+
```js
59+
import { request } from 'node:http';
60+
import { strictEqual } from 'node:assert';
61+
62+
const req = request({
63+
method: 'GET',
64+
protocol: 'http:',
65+
hostname: 'docs.cloudflare.com',
66+
path: '/'
67+
}, (res) => {
68+
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
69+
strictEqual(res.statusCode, 301);
70+
71+
let data = '';
72+
res.setEncoding('utf8');
73+
res.on('data', (chunk) => {
74+
data += chunk;
75+
});
76+
res.on('end', () => {
77+
ok(data.includes('301 Moved Permanently'));
78+
});
79+
});
80+
req.end();
81+
```
82+
83+
```js
84+
const req = request(new URL('http://docs.cloudflare.com'),{
85+
method: 'GET',
86+
}, (res) => {
87+
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
88+
strictEqual(res.statusCode, 301);
89+
});
90+
91+
req.end();
92+
```
93+
94+
The following options passed to the `request` method are not supported due to the differences in the Cloudflare Workers and the implementation of the `node:http` module:
95+
- `maxHeaderSize`
96+
- `insecureHTTPParser`
97+
- `createConnection`
98+
- `lookup`
99+
- `socketPath`
100+
101+
## OutgoingMessage
102+
103+
The [`OutgoingMessage`](https://nodejs.org/docs/latest/api/http.html#class-httpoutgoingmessage) class represents an HTTP response that is sent to the client. It provides methods for writing response headers and body, as well as for ending the response. `OutgoingMessage` extends from the [`Writable` stream class](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable).
104+
105+
```js
106+
import { OutgoingMessage } from 'node:http';
107+
108+
const res = new OutgoingMessage();
109+
res.writeHead(200, { 'Content-Type': 'text/plain' });
110+
res.write('Hello, World!');
111+
res.end();
112+
```
113+
## IncomingMessage
114+
115+
The `IncomingMessage` class represents an HTTP request that is received from the client. It provides methods for reading request headers and body, as well as for ending the request. `IncomingMessage` extends from the `Readable` stream class.
116+
117+
```js
118+
import { get, IncomingMessage } from 'node:http';
119+
import { ok, strictEqual } from 'node:assert';
120+
121+
get('http://docs.cloudflare.com', (res) => {
122+
strictEqual(res.statusCode, 301);
123+
ok(res instanceof IncomingMessage);
124+
});
125+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
pcx_content_type: configuration
3+
title: https
4+
---
5+
6+
import { Render } from "~/components";
7+
8+
<Render file="nodejs-compat-howto" />
9+
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+
28+
## get
29+
30+
An implementation of the Node.js [`https.get'](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method.
31+
32+
The [`get`](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method performs a GET request to the specified URL and invokes the callback with the response. This is a convenience method that simplifies making HTTPS GET requests without manually configuring request options.
33+
34+
```js
35+
import { get } from "node:https";
36+
37+
export default {
38+
async fetch() {
39+
const { promise, resolve, reject } = Promise.withResolvers();
40+
get("http://example.com", (res) => {
41+
let data = "";
42+
res.setEncoding("utf8");
43+
res.on("data", (chunk) => {
44+
data += chunk;
45+
});
46+
res.on("error", reject);
47+
res.on("end", () => {
48+
resolve(new Response(data));
49+
});
50+
}).on("error", reject);
51+
return promise;
52+
},
53+
};
54+
```
55+
56+
## request
57+
58+
An implementation of the Node.js [`https.request'](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback) method.
59+
60+
The [`request`](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback) method creates an HTTPS request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a [writable stream](https://developers.cloudflare.com/workers/runtime-apis/streams/writablestream/) for sending request data.
61+
62+
Request method accepts all options from `http.request` with some differences in default values:
63+
64+
- `protocol`: default `https:`
65+
- `port`: default `443`
66+
- `agent`: default `https.globalAgent`
67+
68+
```js
69+
import { request } from "node:https";
70+
import { strictEqual, ok } from "node:assert";
71+
72+
const req = request(
73+
"https://developers.cloudflare.com/robots.txt",
74+
{
75+
method: "GET",
76+
},
77+
(res) => {
78+
strictEqual(res.statusCode, 200);
79+
let data = "";
80+
res.setEncoding("utf8");
81+
res.on("data", (chunk) => {
82+
data += chunk;
83+
});
84+
res.on("end", () => {
85+
ok(data.includes("User-agent"));
86+
});
87+
},
88+
);
89+
req.end();
90+
```
91+
92+
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`.

0 commit comments

Comments
 (0)