Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update unenv preset #7581

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/chilled-mugs-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": patch
---

chore(wrangler): update unenv dependency version

unenv now uses the workerd implementation on node:dns
See the [unjs/unenv#376](https://github.com/unjs/unenv/pull/376)
15 changes: 10 additions & 5 deletions fixtures/nodejs-hybrid-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Stream } from "node:stream";
import { Context } from "vm";
import { Client } from "pg";
import { s } from "./dep.cjs";
import { testUnenvPreset } from "./unenv-preset";

testBasicNodejsProperties();

Expand All @@ -28,14 +29,18 @@ export default {
return testX509Certificate();
case "/test-require-alias":
return testRequireUenvAliasedPackages();
case "/test-unenv-preset":
return await testUnenvPreset();
}

return new Response(
'<a href="query">Postgres query</a> | ' +
'<a href="test-process">Test process global</a> | ' +
'<a href="test-random">Test getRandomValues()</a> | ' +
'<a href="test-x509-certificate">Test X509Certificate</a>' +
'<a href="test-require-alias">Test require unenv aliased packages</a>',
`<a href="query">Postgres query</a>
<a href="test-process">Test process global</a>
<a href="test-random">Test getRandomValues()</a>
<a href="test-x509-certificate">Test X509Certificate</a>
<a href="test-require-alias">Test require unenv aliased packages</a>
<a href="test-unenv-preset">Test unenv preset</a>
`,
{ headers: { "Content-Type": "text/html; charset=utf-8" } }
);
},
Expand Down
123 changes: 123 additions & 0 deletions fixtures/nodejs-hybrid-app/src/unenv-preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import assert from "node:assert";

// TODO: move to `@cloudflare/unenv-preset`
// See: https://github.com/cloudflare/workers-sdk/issues/7579
export async function testUnenvPreset() {
try {
await testCryptoGetRandomValues();
await testWorkerdImplementsBuffer();
await testWorkerdModules();
await testUtilImplements();
await testWorkerdPath();
await testWorkerdDns();
} catch (e) {
return new Response(String(e));
}

return new Response("OK!");
}

async function testCryptoGetRandomValues() {
const crypto = await import("node:crypto");

const array = new Uint32Array(10);
crypto.getRandomValues(array);
assert.strictEqual(array.length, 10);
assert(array.every((v) => v >= 0 && v <= 0xff_ff_ff_ff));
}

async function testWorkerdImplementsBuffer() {
const encoder = new TextEncoder();
const buffer = await import("node:buffer");
const Buffer = buffer.Buffer;
assert.strictEqual(buffer.isAscii(encoder.encode("hello world")), true);
assert.strictEqual(buffer.isUtf8(encoder.encode("Yağız")), true);
assert.strictEqual(buffer.btoa("hello"), "aGVsbG8=");
assert.strictEqual(buffer.atob("aGVsbG8="), "hello");
{
const dest = buffer.transcode(
Buffer.from([
0x74, 0x00, 0x1b, 0x01, 0x73, 0x00, 0x74, 0x00, 0x20, 0x00, 0x15, 0x26,
]),
"ucs2",
"utf8"
);
assert.strictEqual(
dest.toString(),
Buffer.from("těst ☕", "utf8").toString()
);
}
assert.ok(new buffer.File([], "file"));
assert.ok(new buffer.Blob([]));
assert.strictEqual(typeof buffer.INSPECT_MAX_BYTES, "number");
assert.strictEqual(typeof buffer.resolveObjectURL, "function");
}

async function testWorkerdModules() {
const module = await import("node:module");
// @ts-expect-error exposed by workerd
const require = module.createRequire("/");
const modules = [
"assert",
"assert/strict",
"buffer",
"diagnostics_channel",
"dns",
"dns/promises",
"events",
"path",
"path/posix",
"path/win32",
"querystring",
"stream",
"stream/consumers",
"stream/promises",
"stream/web",
"string_decoder",
"url",
"util/types",
"zlib",
];
for (const m of modules) {
assert.strictEqual(await import(m), require(m));
}
}

async function testUtilImplements() {
const { types } = await import("node:util");
assert.strictEqual(types.isExternal("hello world"), false);
assert.strictEqual(types.isAnyArrayBuffer(new ArrayBuffer(0)), true);
}

async function testWorkerdPath() {
const pathWin32 = await import("node:path/win32");
assert.strictEqual(pathWin32.sep, "\\");
assert.strictEqual(pathWin32.delimiter, ";");
const pathPosix = await import("node:path/posix");
assert.strictEqual(pathPosix.sep, "/");
assert.strictEqual(pathPosix.delimiter, ":");
}

async function testWorkerdDns() {
const dns = await import("node:dns");
await new Promise((resolve, reject) => {
dns.resolveTxt("nodejs.org", (error, results) => {
if (error) {
reject(error);
return;
}
assert.ok(Array.isArray(results[0]));
assert.strictEqual(results.length, 1);
assert.ok(results[0][0].startsWith("v=spf1"));
resolve(null);
});
});

const dnsPromises = await import("node:dns/promises");
const results = await dnsPromises.resolveCaa("google.com");
assert.ok(Array.isArray(results));
assert.strictEqual(results.length, 1);
assert.strictEqual(typeof results[0].critical, "number");
assert.strictEqual(results[0].critical, 0);
assert.strictEqual(results[0].issue, "pki.goog");
}
13 changes: 13 additions & 0 deletions fixtures/nodejs-hybrid-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@ describe("nodejs compat", () => {
await stop();
}
});

test("unenv preset", async ({ expect }) => {
const { ip, port, stop } = await runWranglerDev(
resolve(__dirname, "../src"),
["--port=0", "--inspector-port=0"]
);
try {
const response = await fetch(`http://${ip}:${port}/test-unenv-preset`);
await expect(response.text()).resolves.toBe("OK!");
} finally {
await stop();
}
});
});
3 changes: 1 addition & 2 deletions packages/unenv-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
"devDependencies": {
"@types/node": "*",
"typescript": "catalog:default",
"unbuild": "^2.0.0",
"wrangler": "3.95.0"
"unbuild": "^2.0.0"
},
"peerDependencies": {
"unenv": "npm:unenv-nightly@*",
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"resolve": "^1.22.8",
"selfsigned": "^2.0.1",
"source-map": "^0.6.1",
"unenv": "npm:[email protected]20241204-140205-a5d5190",
"unenv": "npm:[email protected]20241216-144314-7e05819",
"workerd": "1.20241218.0",
"xxhash-wasm": "^1.0.1"
},
Expand Down
Loading
Loading