Skip to content

Commit

Permalink
[wrangler] Expose URL from url & node:url in node_compat mode (#5480)
Browse files Browse the repository at this point in the history
* Add e2e tests & support node:url

* Add changeset

* Address comments
  • Loading branch information
penalosa authored Apr 2, 2024
1 parent 80b8fcb commit 0cce21f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chatty-rats-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: Ensure url & node:url export URL (aliased to globalThis.URL) in node_compat mode
33 changes: 33 additions & 0 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,39 @@ describe("basic dev tests", () => {
`,
}));
});
it("can import URL from 'url' in node_compat mode", async () => {
await worker.seed((workerName) => ({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-01-01"
node_compat = true
`,
"src/index.ts": dedent`
const { URL } = require('url');
const { URL: nURL } = require('node:url');
export default {
fetch(request) {
const url = new URL('postgresql://user:[email protected]:12345/dbname?sslmode=disable')
const nUrl = new nURL('postgresql://user:[email protected]:12345/dbname?sslmode=disable')
return new Response(url + nUrl)
}
}`,
}));
await worker.runDevSession("", async (session) => {
const { text } = await retry(
(s) => s.status !== 200,
async () => {
const r = await fetch(`http://127.0.0.1:${session.port}`);
return { text: await r.text(), status: r.status };
}
);
expect(text).toMatchInlineSnapshot(
`"postgresql://user:[email protected]:12345/dbname?sslmode=disablepostgresql://user:[email protected]:12345/dbname?sslmode=disable"`
);
});
});

it("can modify worker during dev session (local)", async () => {
await worker.runDevSession("", async (session) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Plugin } from "esbuild";
export const standardURLPlugin: () => Plugin = () => ({
name: "standard URL plugin",
setup(pluginBuild) {
pluginBuild.onResolve({ filter: /^url$/ }, ({ importer }) => {
pluginBuild.onResolve({ filter: /^node:url$|^url$/ }, ({ importer }) => {
if (importer === "standard-url-plugin") return;
return {
path: "wrangler-url-polyfill",
Expand Down

0 comments on commit 0cce21f

Please sign in to comment.