Skip to content

Commit

Permalink
Revert "Revert "Hyperdrive dev remote fix"" (#7879)
Browse files Browse the repository at this point in the history
* Revert "Revert "Hyperdrive dev remote fix" (#7868)"

This reverts commit 78a9a2d.

* chore: fix E2E
  • Loading branch information
andyjessop authored Jan 27, 2025
1 parent 135dd85 commit 5c02e46
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-jeans-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Fix to not require local connection string when using Hyperdrive and wrangler dev --remote
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
HYPERDRIVE_DATABASE_URL: ${{ secrets.TEST_HYPERDRIVE_DATABASE_URL}}
WRANGLER: node --no-warnings ${{ github.workspace}}/packages/wrangler/bin/wrangler.js
WRANGLER_IMPORT: ${{ github.workspace}}/packages/wrangler/wrangler-dist/cli.js
NODE_OPTIONS: "--max_old_space_size=8192"
Expand Down
30 changes: 29 additions & 1 deletion packages/wrangler/e2e/dev-with-resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,35 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => {
);
});

it.skipIf(isLocal)("exposes Hyperdrive bindings", async () => {
const { id } = await helper.hyperdrive(isLocal);

await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-10-25"
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "${id}"
`,
"src/index.ts": dedent`
export default {
async fetch(request, env) {
if (request.url.includes("connect")) {
const conn = env.HYPERDRIVE.connect();
}
return new Response(env.HYPERDRIVE?.connectionString ?? "no")
}
}`,
});

const worker = helper.runLongLived(`wrangler dev ${flags}`);
const { url } = await worker.waitForReady();
await fetch(`${url}/connect`);
});

it.skipIf(!isLocal).fails("exposes Pipelines bindings", async () => {
await helper.seed({
"wrangler.toml": dedent`
Expand Down Expand Up @@ -692,7 +721,6 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => {
});

// TODO(soon): implement E2E tests for other bindings
it.todo("exposes hyperdrive bindings");
it.skipIf(isLocal).todo("exposes send email bindings");
it.skipIf(isLocal).todo("exposes browser bindings");
it.skipIf(isLocal).todo("exposes Workers AI bindings");
Expand Down
38 changes: 38 additions & 0 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,44 @@ describe("hyperdrive dev tests", () => {
await socketMsgPromise;
});

it("does not require local connection string when running `wrangler dev --remote`", async () => {
const helper = new WranglerE2ETestHelper();
const { id } = await helper.hyperdrive(false);

await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-10-25"
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "${id}"
`,
"src/index.ts": dedent`
export default {
async fetch(request, env) {
if (request.url.includes("connect")) {
const conn = env.HYPERDRIVE.connect();
}
return new Response(env.HYPERDRIVE?.connectionString ?? "no")
}
}`,
"package.json": dedent`
{
"name": "worker",
"version": "0.0.0",
"private": true
}
`,
});

const worker = helper.runLongLived("wrangler dev --remote");

const { url } = await worker.waitForReady();
await fetch(`${url}/connect`);
});

afterEach(() => {
if (server.listening) {
server.close();
Expand Down
23 changes: 23 additions & 0 deletions packages/wrangler/e2e/helpers/e2e-wrangler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,27 @@ export class WranglerE2ETestHelper {

return name;
}

async hyperdrive(isLocal: boolean): Promise<{ id: string; name: string }> {
const name = generateResourceName("hyperdrive");

if (isLocal) {
return { id: crypto.randomUUID(), name };
}

const result = await this.run(
`wrangler hyperdrive create ${name} --connection-string="${process.env.HYPERDRIVE_DATABASE_URL}"`
);
const tomlMatch = /id = "([0-9a-f]{32})"/.exec(result.stdout);
const jsonMatch = /"id": "([0-9a-f]{32})"/.exec(result.stdout);
const match = jsonMatch ?? tomlMatch;
assert(match !== null, `Cannot find ID in ${JSON.stringify(result)}`);
const id = match[1];

onTestFinished(async () => {
await this.run(`wrangler hyperdrive delete ${name}`);
});

return { id, name };
}
}
7 changes: 6 additions & 1 deletion packages/wrangler/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,12 @@ export function getBindings(
process.env[
`WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_${hyperdrive.binding}`
];
if (!connectionStringFromEnv && !hyperdrive.localConnectionString) {
// only require a local connection string in the wrangler file or the env if not using dev --remote
if (
local &&
connectionStringFromEnv === undefined &&
hyperdrive.localConnectionString === undefined
) {
throw new UserError(
`When developing locally, you should use a local Postgres connection string to emulate Hyperdrive functionality. Please setup Postgres locally and set the value of the 'WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_${hyperdrive.binding}' variable or "${hyperdrive.binding}"'s "localConnectionString" to the Postgres connection string.`
);
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"WRANGLER_DISABLE_EXPERIMENTAL_WARNING",
"WRANGLER_DISABLE_REQUEST_BODY_DRAINING",
"WRANGLER_WORKER_REGISTRY_PORT",
"WRANGLER_API_ENVIRONMENT"
"WRANGLER_API_ENVIRONMENT",
"HYPERDRIVE_DATABASE_URL"
]
},
"test:ci": {
Expand Down

0 comments on commit 5c02e46

Please sign in to comment.