Skip to content

Commit c4c1076

Browse files
mtlemiliopenalosa
authored andcommitted
Don't require Hyperdrive local connection string when using wrangler dev --remote (#7485)
* Don't require Hyperdrive local connection string when using wrangler dev --remote * Add changeset * Fix style issues * fix type issue
1 parent 9709789 commit c4c1076

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

.changeset/nervous-jeans-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix to not require local connection string when using Hyperdrive and wrangler dev --remote

packages/wrangler/e2e/dev.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,49 @@ describe("hyperdrive dev tests", () => {
555555
await socketMsgPromise;
556556
});
557557

558+
it("does not require local connection string when running `wrangler dev --remote`", async () => {
559+
const helper = new WranglerE2ETestHelper();
560+
await helper.seed({
561+
"wrangler.toml": dedent`
562+
name = "${workerName}"
563+
main = "src/index.ts"
564+
compatibility_date = "2023-10-25"
565+
566+
[[hyperdrive]]
567+
binding = "HYPERDRIVE"
568+
id = "hyperdrive_id"
569+
`,
570+
"src/index.ts": dedent`
571+
export default {
572+
async fetch(request, env) {
573+
if (request.url.includes("connect")) {
574+
const conn = env.HYPERDRIVE.connect();
575+
await conn.writable.getWriter().write(new TextEncoder().encode("test string"));
576+
}
577+
return new Response(env.HYPERDRIVE?.connectionString ?? "no")
578+
}
579+
}`,
580+
"package.json": dedent`
581+
{
582+
"name": "worker",
583+
"version": "0.0.0",
584+
"private": true
585+
}
586+
`,
587+
});
588+
589+
const worker = helper.runLongLived("wrangler dev --remote");
590+
591+
const { url } = await worker.waitForReady();
592+
const text = await fetchText(url);
593+
594+
const hyperdrive = new URL(text);
595+
expect(hyperdrive.pathname).toBe("/some_db");
596+
expect(hyperdrive.username).toBe("user");
597+
expect(hyperdrive.password).toBe("!pass");
598+
expect(hyperdrive.host).not.toBe("localhost");
599+
});
600+
558601
afterEach(() => {
559602
if (server.listening) {
560603
server.close();

packages/wrangler/src/dev.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,12 @@ export function getBindings(
10421042
process.env[
10431043
`WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_${hyperdrive.binding}`
10441044
];
1045-
if (!connectionStringFromEnv && !hyperdrive.localConnectionString) {
1045+
// only require a local connection string in the wrangler file or the env if not using dev --remote
1046+
if (
1047+
local &&
1048+
!connectionStringFromEnv &&
1049+
!hyperdrive.localConnectionString
1050+
) {
10461051
throw new UserError(
10471052
`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.`
10481053
);

0 commit comments

Comments
 (0)