From 6ddc2a06a7cd5e4b710ac01616b553483f69783f Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 7 Jul 2025 10:21:36 -0400 Subject: [PATCH 1/8] add node:http and node:https modules --- .../docs/workers/runtime-apis/nodejs/http.mdx | 121 ++++++++++++++++++ .../workers/runtime-apis/nodejs/https.mdx | 82 ++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 src/content/docs/workers/runtime-apis/nodejs/http.mdx create mode 100644 src/content/docs/workers/runtime-apis/nodejs/https.mdx diff --git a/src/content/docs/workers/runtime-apis/nodejs/http.mdx b/src/content/docs/workers/runtime-apis/nodejs/http.mdx new file mode 100644 index 00000000000000..6ae18a9a5f4782 --- /dev/null +++ b/src/content/docs/workers/runtime-apis/nodejs/http.mdx @@ -0,0 +1,121 @@ +--- +pcx_content_type: configuration +title: http +--- + +import { Render } from "~/components" + + + +## Agent + +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. + +```js +import { Agent } from 'node:http'; +import { strictEqual } from 'node:assert'; + +const agent = new Agent(); +strictEqual(agent.protocol, 'http:'); +``` + +## get + +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. + +```js +import { get } from 'node:http'; +import { strictEqual, ok } from 'node:assert'; + +get('http://docs.cloudflare.com/robots.txt', (res) => { + // http://docs.cloudflare.com redirects to https url. + strictEqual(res.statusCode, 301); + let data = ''; + res.setEncoding('utf8'); + res.on('data', (chunk) => { + data += chunk; + }); + res.on('end', () => { + // Returns the response body as a string. + console.log(data); + ok(data.includes('301 Moved Permanently')); + }); +}); +``` + +## request + +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. + +```js +import { request } from 'node:http'; +import { strictEqual } from 'node:assert'; + +const req = request({ + method: 'GET', + protocol: 'http:', + hostname: 'docs.cloudflare.com', + path: '/' +}, (res) => { + // http://docs.cloudflare.com redirects to https url. + strictEqual(res.statusCode, 301); + + let data = ''; + res.setEncoding('utf8'); + res.on('data', (chunk) => { + data += chunk; + }); + res.on('end', () => { + // Returns the response body as a string. + console.log(data); + ok(data.includes('301 Moved Permanently')); + }); +}); +req.end(); + +const req2 = request(new URL('http://docs.cloudflare.com'),{ + method: 'GET', +}, (res) => { + // http://docs.cloudflare.com redirects to https url. + strictEqual(res.statusCode, 301); +}); + +req2.end(); +``` + +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: +- `maxHeaderSize` +- `insecureHTTPParser` +- `createConnection` +- `lookup` +- `socketPath` + +## OutgoingMessage + +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). + +```js +import { OutgoingMessage } from 'node:http'; + +const res = new OutgoingMessage(); +res.writeHead(200, { 'Content-Type': 'text/plain' }); +res.write('Hello, World!'); +res.end(); +``` +## IncomingMessage + +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. + +```js +import { get, IncomingMessage } from 'node:http'; +import { ok, strictEqual } from 'node:assert'; + +get('http://docs.cloudflare.com', (res) => { + strictEqual(res.statusCode, 301); + ok(res instanceof IncomingMessage); +}); +``` diff --git a/src/content/docs/workers/runtime-apis/nodejs/https.mdx b/src/content/docs/workers/runtime-apis/nodejs/https.mdx new file mode 100644 index 00000000000000..175c94e69858e5 --- /dev/null +++ b/src/content/docs/workers/runtime-apis/nodejs/https.mdx @@ -0,0 +1,82 @@ +--- +pcx_content_type: configuration +title: https +--- + +import { Render } from "~/components" + + + +## Agent + +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. + +```js +import { Agent } from 'node:https'; +import { strictEqual } from 'node:assert'; + +const agent = new Agent(); +strictEqual(agent.protocol, 'https:'); +``` + +## get + +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. It's a convenience method that simplifies making HTTPS GET requests without manually configuring request options. + +`get` methods behaves the same as `node:http` get method. + +```js +import { get } from 'node:https'; +import { strictEqual, ok } from 'node:assert'; + +get('https://developers.cloudflare.com/robots.txt', (res) => { + strictEqual(res.statusCode, 200); + let data = ''; + res.setEncoding('utf8'); + res.on('data', (chunk) => { + data += chunk; + }); + res.on('end', () => { + // Returns the response body as a string. + console.log(data); + ok(data.includes('User-agent')); + }); +}); +``` + +## request + +The [`request`](https://nodejs.org/docs/latest/api/https.html#httpsrequesturl-options-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://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data. + +Request method accepts all options from `http.request` with some differences in default values: +- `protocol`: default `https:` +- `port`: default `443` +- `agent`: default `https.globalAgent` + +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. + +```js +import { request } from 'node:https'; +import { strictEqual, ok } from 'node:assert'; + +const req = request('https://developers.cloudflare.com/robots.txt', { + method: 'GET', +}, (res) => { + strictEqual(res.statusCode, 200); + let data = ''; + res.setEncoding('utf8'); + res.on('data', (chunk) => { + data += chunk; + }); + res.on('end', () => { + // Returns the response body as a string. + console.log(data); + ok(data.includes('User-agent')); + }); +}); +req.end(); +``` From 990cc940f31ab0b7de6b6a054c61b79d4e546a31 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 16 Jul 2025 13:57:35 -0400 Subject: [PATCH 2/8] Update src/content/docs/workers/runtime-apis/nodejs/https.mdx Co-authored-by: Mike Nomitch --- src/content/docs/workers/runtime-apis/nodejs/https.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/workers/runtime-apis/nodejs/https.mdx b/src/content/docs/workers/runtime-apis/nodejs/https.mdx index 175c94e69858e5..f75541e7de0dd8 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/https.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/https.mdx @@ -25,7 +25,7 @@ strictEqual(agent.protocol, 'https:'); ## get -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. It's a convenience method that simplifies making HTTPS GET requests without manually configuring request options. +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. `get` methods behaves the same as `node:http` get method. From be5db3cc205011efa569f007a04acc47a891edcf Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Jul 2025 09:21:47 -0400 Subject: [PATCH 3/8] Update src/content/docs/workers/runtime-apis/nodejs/http.mdx Co-authored-by: Dario Piotrowicz --- src/content/docs/workers/runtime-apis/nodejs/http.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/workers/runtime-apis/nodejs/http.mdx b/src/content/docs/workers/runtime-apis/nodejs/http.mdx index 6ae18a9a5f4782..4b4c95ac9b5627 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/http.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/http.mdx @@ -87,7 +87,7 @@ const req2 = request(new URL('http://docs.cloudflare.com'),{ req2.end(); ``` -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: +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: - `maxHeaderSize` - `insecureHTTPParser` - `createConnection` From 90cbe94fd38af9f540be42a537f38cfca984a5e3 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Jul 2025 09:22:06 -0400 Subject: [PATCH 4/8] Update src/content/docs/workers/runtime-apis/nodejs/https.mdx Co-authored-by: Dario Piotrowicz --- src/content/docs/workers/runtime-apis/nodejs/https.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/workers/runtime-apis/nodejs/https.mdx b/src/content/docs/workers/runtime-apis/nodejs/https.mdx index f75541e7de0dd8..477024dfab2ec0 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/https.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/https.mdx @@ -57,7 +57,7 @@ Request method accepts all options from `http.request` with some differences in - `port`: default `443` - `agent`: default `https.globalAgent` -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. +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`. ```js import { request } from 'node:https'; From de7fe07be4947ea706c4049de0b35b33f765a50c Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Jul 2025 09:22:34 -0400 Subject: [PATCH 5/8] Update src/content/docs/workers/runtime-apis/nodejs/http.mdx Co-authored-by: Dario Piotrowicz --- src/content/docs/workers/runtime-apis/nodejs/http.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/workers/runtime-apis/nodejs/http.mdx b/src/content/docs/workers/runtime-apis/nodejs/http.mdx index 4b4c95ac9b5627..3dbb9c737849f2 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/http.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/http.mdx @@ -32,7 +32,7 @@ import { get } from 'node:http'; import { strictEqual, ok } from 'node:assert'; get('http://docs.cloudflare.com/robots.txt', (res) => { - // http://docs.cloudflare.com redirects to https url. + // requests to http://docs.cloudflare.com get redirected to their https counterpart. strictEqual(res.statusCode, 301); let data = ''; res.setEncoding('utf8'); From dfba2b1f259a865de6dc389ea43f013cb62b223e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Jul 2025 15:09:30 -0400 Subject: [PATCH 6/8] Update src/content/docs/workers/runtime-apis/nodejs/http.mdx Co-authored-by: James M Snell --- src/content/docs/workers/runtime-apis/nodejs/http.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/content/docs/workers/runtime-apis/nodejs/http.mdx b/src/content/docs/workers/runtime-apis/nodejs/http.mdx index 3dbb9c737849f2..dd9fbb15c9b91f 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/http.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/http.mdx @@ -25,6 +25,8 @@ strictEqual(agent.protocol, 'http:'); ## get +An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api/http.html#httpgetoptions-callback) method. + 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. ```js From bc74bec5acd63875bb8d744f589e45c70b1f40f3 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Jul 2025 15:09:40 -0400 Subject: [PATCH 7/8] Update src/content/docs/workers/runtime-apis/nodejs/http.mdx Co-authored-by: James M Snell --- .../docs/workers/runtime-apis/nodejs/http.mdx | 20 ++++++++++--------- .../workers/runtime-apis/nodejs/https.mdx | 20 +++++++++---------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/content/docs/workers/runtime-apis/nodejs/http.mdx b/src/content/docs/workers/runtime-apis/nodejs/http.mdx index dd9fbb15c9b91f..ce3a9b9dc5e048 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/http.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/http.mdx @@ -9,7 +9,9 @@ import { Render } from "~/components" ## Agent -An Agent manages HTTP connection reuse by maintaining request queues per host/port. In the +An implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class. + +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 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 @@ -42,8 +44,6 @@ get('http://docs.cloudflare.com/robots.txt', (res) => { data += chunk; }); res.on('end', () => { - // Returns the response body as a string. - console.log(data); ok(data.includes('301 Moved Permanently')); }); }); @@ -51,6 +51,8 @@ get('http://docs.cloudflare.com/robots.txt', (res) => { ## request +An implementation of the Node.js [`http.request'](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method. + 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. ```js @@ -63,7 +65,7 @@ const req = request({ hostname: 'docs.cloudflare.com', path: '/' }, (res) => { - // http://docs.cloudflare.com redirects to https url. + // requests to http://docs.cloudflare.com get redirected to their https counterpart. strictEqual(res.statusCode, 301); let data = ''; @@ -72,21 +74,21 @@ const req = request({ data += chunk; }); res.on('end', () => { - // Returns the response body as a string. - console.log(data); ok(data.includes('301 Moved Permanently')); }); }); req.end(); +``` -const req2 = request(new URL('http://docs.cloudflare.com'),{ +```js +const req = request(new URL('http://docs.cloudflare.com'),{ method: 'GET', }, (res) => { - // http://docs.cloudflare.com redirects to https url. + // requests to http://docs.cloudflare.com get redirected to their https counterpart. strictEqual(res.statusCode, 301); }); -req2.end(); +req.end(); ``` 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: diff --git a/src/content/docs/workers/runtime-apis/nodejs/https.mdx b/src/content/docs/workers/runtime-apis/nodejs/https.mdx index 477024dfab2ec0..6d26138da8f04e 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/https.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/https.mdx @@ -9,7 +9,9 @@ import { Render } from "~/components" ## Agent -An Agent manages HTTPS connection reuse by maintaining request queues per host/port. In the +An implementation of the Node.js [`https.Agent'](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) class. + +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 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 @@ -25,9 +27,9 @@ strictEqual(agent.protocol, 'https:'); ## get -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. +An implementation of the Node.js [`https.get'](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method. -`get` methods behaves the same as `node:http` get method. +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. ```js import { get } from 'node:https'; @@ -41,8 +43,6 @@ get('https://developers.cloudflare.com/robots.txt', (res) => { data += chunk; }); res.on('end', () => { - // Returns the response body as a string. - console.log(data); ok(data.includes('User-agent')); }); }); @@ -50,15 +50,15 @@ get('https://developers.cloudflare.com/robots.txt', (res) => { ## request -The [`request`](https://nodejs.org/docs/latest/api/https.html#httpsrequesturl-options-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://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data. +An implementation of the Node.js [`https.request'](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback) method. + +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. Request method accepts all options from `http.request` with some differences in default values: - `protocol`: default `https:` - `port`: default `443` - `agent`: default `https.globalAgent` -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`. - ```js import { request } from 'node:https'; import { strictEqual, ok } from 'node:assert'; @@ -73,10 +73,10 @@ const req = request('https://developers.cloudflare.com/robots.txt', { data += chunk; }); res.on('end', () => { - // Returns the response body as a string. - console.log(data); ok(data.includes('User-agent')); }); }); req.end(); ``` + +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`. From 5198119452ec7b3559aa9f8becb712165301a8a7 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Jul 2025 15:21:16 -0400 Subject: [PATCH 8/8] update example to use fetch handler --- .../workers/runtime-apis/nodejs/https.mdx | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/src/content/docs/workers/runtime-apis/nodejs/https.mdx b/src/content/docs/workers/runtime-apis/nodejs/https.mdx index 6d26138da8f04e..5c761c7ee6a72e 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/https.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/https.mdx @@ -3,7 +3,7 @@ pcx_content_type: configuration title: https --- -import { Render } from "~/components" +import { Render } from "~/components"; @@ -18,11 +18,11 @@ implementation of `Agent` in Workers is a stub implementation that does not supp pooling or keep-alive. ```js -import { Agent } from 'node:https'; -import { strictEqual } from 'node:assert'; +import { Agent } from "node:https"; +import { strictEqual } from "node:assert"; const agent = new Agent(); -strictEqual(agent.protocol, 'https:'); +strictEqual(agent.protocol, "https:"); ``` ## get @@ -32,20 +32,25 @@ An implementation of the Node.js [`https.get'](https://nodejs.org/docs/latest/ap 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. ```js -import { get } from 'node:https'; -import { strictEqual, ok } from 'node:assert'; - -get('https://developers.cloudflare.com/robots.txt', (res) => { - strictEqual(res.statusCode, 200); - let data = ''; - res.setEncoding('utf8'); - res.on('data', (chunk) => { - data += chunk; - }); - res.on('end', () => { - ok(data.includes('User-agent')); - }); -}); +import { get } from "node:https"; + +export default { + async fetch() { + const { promise, resolve, reject } = Promise.withResolvers(); + get("http://example.com", (res) => { + let data = ""; + res.setEncoding("utf8"); + res.on("data", (chunk) => { + data += chunk; + }); + res.on("error", reject); + res.on("end", () => { + resolve(new Response(data)); + }); + }).on("error", reject); + return promise; + }, +}; ``` ## request @@ -55,27 +60,32 @@ An implementation of the Node.js [`https.request'](https://nodejs.org/docs/lates 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. Request method accepts all options from `http.request` with some differences in default values: + - `protocol`: default `https:` - `port`: default `443` - `agent`: default `https.globalAgent` ```js -import { request } from 'node:https'; -import { strictEqual, ok } from 'node:assert'; - -const req = request('https://developers.cloudflare.com/robots.txt', { - method: 'GET', -}, (res) => { - strictEqual(res.statusCode, 200); - let data = ''; - res.setEncoding('utf8'); - res.on('data', (chunk) => { - data += chunk; - }); - res.on('end', () => { - ok(data.includes('User-agent')); - }); -}); +import { request } from "node:https"; +import { strictEqual, ok } from "node:assert"; + +const req = request( + "https://developers.cloudflare.com/robots.txt", + { + method: "GET", + }, + (res) => { + strictEqual(res.statusCode, 200); + let data = ""; + res.setEncoding("utf8"); + res.on("data", (chunk) => { + data += chunk; + }); + res.on("end", () => { + ok(data.includes("User-agent")); + }); + }, +); req.end(); ```