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

Hyperdrive dev remote fix #7783

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
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
31 changes: 30 additions & 1 deletion packages/wrangler/e2e/dev-with-resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { beforeEach, describe, expect, it } from "vitest";
import WebSocket from "ws";
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test";
import { fetchText } from "./helpers/fetch-text";

Check failure on line 9 in packages/wrangler/e2e/dev-with-resources.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'fetchText' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 9 in packages/wrangler/e2e/dev-with-resources.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'fetchText' is defined but never used
import { generateResourceName } from "./helpers/generate-resource-name";

const port = await getPort();
Expand Down Expand Up @@ -580,6 +581,35 @@
);
});

it("exposes Hyperdrive bindings", async () => {
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")
}
}`,
});

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 +722,6 @@
});

// 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 &&
!hyperdrive.localConnectionString
) {
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
Loading