Skip to content

Commit 4ad78ea

Browse files
authored
Use the native implementation of node:tls (#8514)
* Use the native implementation of `node:tls` * fixup! lock file * sync with `[email protected]` * fixup! lockfile
1 parent 3ff1a00 commit 4ad78ea

File tree

8 files changed

+155
-48
lines changed

8 files changed

+155
-48
lines changed

.changeset/loud-crabs-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/unenv-preset": patch
3+
---
4+
5+
sync with `[email protected]`

.changeset/small-pigs-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/unenv-preset": minor
3+
---
4+
5+
Use the native implementation for `connect` and `TLSSocket` from `node:tls`

fixtures/nodejs-hybrid-app/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export default {
3030
return testRequireUnenvAliasedPackages();
3131
case "/test-immediate":
3232
return await testImmediate();
33+
case "/test-tls":
34+
return await testTls();
3335
}
3436

3537
return new Response(
@@ -39,6 +41,7 @@ export default {
3941
<a href="test-x509-certificate">Test X509Certificate</a>
4042
<a href="test-require-alias">Test require unenv aliased packages</a>
4143
<a href="test-immediate">Test setImmediate</a>
44+
<a href="test-tls">node:tls</a>
4245
`,
4346
{ headers: { "Content-Type": "text/html; charset=utf-8" } }
4447
);
@@ -195,3 +198,12 @@ async function testPostgresLibrary(env: Env, ctx: Context) {
195198
ctx.waitUntil(client.end());
196199
return resp;
197200
}
201+
202+
async function testTls(env: Env, ctx: Context) {
203+
const tls = await import("node:tls");
204+
205+
assert.strictEqual(typeof tls.connect, "function");
206+
assert.strictEqual(typeof tls.TLSSocket, "function");
207+
208+
return new Response("OK");
209+
}

fixtures/nodejs-hybrid-app/tests/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@ describe("nodejs compat", () => {
6464
const response = await fetch(`http://${ip}:${port}/test-immediate`);
6565
await expect(response.text()).resolves.toBe("OK");
6666
});
67+
68+
test("node:tls", async ({ expect }) => {
69+
const { ip, port } = wrangler;
70+
const response = await fetch(`http://${ip}:${port}/test-tls`);
71+
await expect(response.text()).resolves.toBe("OK");
72+
});
6773
});

packages/unenv-preset/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"wrangler": "workspace:*"
5454
},
5555
"peerDependencies": {
56-
"unenv": "2.0.0-rc.14",
57-
"workerd": "^1.20250124.0"
56+
"unenv": "2.0.0-rc.15",
57+
"workerd": "^1.20250310.0"
5858
},
5959
"peerDependenciesMeta": {
6060
"workerd": {

packages/unenv-preset/src/preset.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const hybridNodeCompatModules = [
4646
"crypto",
4747
"module",
4848
"process",
49+
"tls",
4950
"util",
5051
];
5152

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
checkServerIdentity,
3+
CLIENT_RENEG_LIMIT,
4+
CLIENT_RENEG_WINDOW,
5+
convertALPNProtocols,
6+
createSecureContext,
7+
createSecurePair,
8+
createServer,
9+
DEFAULT_CIPHERS,
10+
DEFAULT_ECDH_CURVE,
11+
DEFAULT_MAX_VERSION,
12+
DEFAULT_MIN_VERSION,
13+
getCiphers,
14+
rootCertificates,
15+
SecureContext,
16+
Server,
17+
} from "unenv/node/tls";
18+
import type nodeTls from "node:tls";
19+
20+
export {
21+
checkServerIdentity,
22+
CLIENT_RENEG_LIMIT,
23+
CLIENT_RENEG_WINDOW,
24+
convertALPNProtocols,
25+
createSecureContext,
26+
createSecurePair,
27+
createServer,
28+
DEFAULT_CIPHERS,
29+
DEFAULT_ECDH_CURVE,
30+
DEFAULT_MAX_VERSION,
31+
DEFAULT_MIN_VERSION,
32+
getCiphers,
33+
rootCertificates,
34+
SecureContext,
35+
Server,
36+
} from "unenv/node/tls";
37+
38+
const workerdTls = process.getBuiltinModule("node:tls");
39+
40+
// Natively implemented in workerd
41+
export const { connect, TLSSocket } = workerdTls;
42+
43+
export default {
44+
CLIENT_RENEG_LIMIT,
45+
CLIENT_RENEG_WINDOW,
46+
DEFAULT_CIPHERS,
47+
DEFAULT_ECDH_CURVE,
48+
DEFAULT_MAX_VERSION,
49+
DEFAULT_MIN_VERSION,
50+
// @ts-expect-error
51+
SecureContext,
52+
Server,
53+
TLSSocket,
54+
checkServerIdentity,
55+
connect,
56+
convertALPNProtocols,
57+
createSecureContext,
58+
createSecurePair,
59+
createServer,
60+
getCiphers,
61+
rootCertificates,
62+
} satisfies typeof nodeTls;

0 commit comments

Comments
 (0)