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

Add Hyperdrive Bindings #718

Merged
merged 2 commits into from
Oct 24, 2023
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
109 changes: 109 additions & 0 deletions packages/miniflare/src/plugins/hyperdrive/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { z } from "zod";
import { Service, Worker_Binding } from "../../runtime";
import { Plugin } from "../shared";

export const HYPERDRIVE_PLUGIN_NAME = "hyperdrive";

export const HyperdriveSchema = z
.string()
.url()
.transform((urlString, ctx) => {
const url = new URL(urlString);
if (url.protocol === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "You must specify the database protocol - e.g. 'postgresql'.",
});
} else if (
url.protocol !== "postgresql:" &&
url.protocol !== "postgres:" &&
url.protocol !== ""
mrbbot marked this conversation as resolved.
Show resolved Hide resolved
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"Only PostgreSQL or PostgreSQL compatible databases are currently supported.",
});
}
if (url.host === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"You must provide a hostname or IP address in your connection string - e.g. 'user:password@database-hostname.example.com:5432/databasename",
});
}
if (url.port === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"You must provide a port number - e.g. 'user:password@database.example.com:port/databasename",
});
}
if (url.pathname === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"You must provide a database name as the path component - e.g. /postgres",
});
}
if (url.username === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"You must provide a username - e.g. 'user:password@database.example.com:port/databasename'",
});
}
if (url.password === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"You must provide a password - e.g. 'user:password@database.example.com:port/databasename' ",
});
}
return {
database: url.pathname.replace("/", ""),
mrbbot marked this conversation as resolved.
Show resolved Hide resolved
user: url.username,
password: url.password,
scheme: url.protocol.replace(":", ""),
host: url.host,
port: url.port,
};
});

export const HyperdriveInputOptionsSchema = z.object({
hyperdrives: z.record(z.string(), HyperdriveSchema).optional(),
});

export const HYPERDRIVE_PLUGIN: Plugin<typeof HyperdriveInputOptionsSchema> = {
options: HyperdriveInputOptionsSchema,
getBindings(options) {
return Object.entries(options.hyperdrives ?? {}).map<Worker_Binding>(
([name, config]) => ({
name,
hyperdrive: {
designator: {
name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`,
},
database: config.database,
user: config.user,
password: config.password,
scheme: config.scheme,
},
})
);
},
getNodeBindings() {
return {};
},
async getServices({ options }) {
return Object.entries(options.hyperdrives ?? {}).map<Service>(
([name, config]) => ({
name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`,
external: {
address: `${config.host}:${config.port}`,
tcp: {},
},
})
);
},
};
6 changes: 5 additions & 1 deletion packages/miniflare/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CACHE_PLUGIN, CACHE_PLUGIN_NAME } from "./cache";
import { CORE_PLUGIN, CORE_PLUGIN_NAME } from "./core";
import { D1_PLUGIN, D1_PLUGIN_NAME } from "./d1";
import { DURABLE_OBJECTS_PLUGIN, DURABLE_OBJECTS_PLUGIN_NAME } from "./do";
import { HYPERDRIVE_PLUGIN, HYPERDRIVE_PLUGIN_NAME } from "./hyperdrive";
import { KV_PLUGIN, KV_PLUGIN_NAME } from "./kv";
import { QUEUES_PLUGIN, QUEUES_PLUGIN_NAME } from "./queues";
import { R2_PLUGIN, R2_PLUGIN_NAME } from "./r2";
Expand All @@ -16,6 +17,7 @@ export const PLUGINS = {
[KV_PLUGIN_NAME]: KV_PLUGIN,
[QUEUES_PLUGIN_NAME]: QUEUES_PLUGIN,
[R2_PLUGIN_NAME]: R2_PLUGIN,
[HYPERDRIVE_PLUGIN_NAME]: HYPERDRIVE_PLUGIN,
};
export type Plugins = typeof PLUGINS;

Expand Down Expand Up @@ -60,7 +62,8 @@ export type WorkerOptions = z.infer<typeof CORE_PLUGIN.options> &
z.infer<typeof DURABLE_OBJECTS_PLUGIN.options> &
z.infer<typeof KV_PLUGIN.options> &
z.infer<typeof QUEUES_PLUGIN.options> &
z.infer<typeof R2_PLUGIN.options>;
z.infer<typeof R2_PLUGIN.options> &
z.input<typeof HYPERDRIVE_PLUGIN.options>;
export type SharedOptions = z.infer<typeof CORE_PLUGIN.sharedOptions> &
z.infer<typeof CACHE_PLUGIN.sharedOptions> &
z.infer<typeof D1_PLUGIN.sharedOptions> &
Expand Down Expand Up @@ -104,3 +107,4 @@ export * from "./do";
export * from "./kv";
export * from "./queues";
export * from "./r2";
export * from "./hyperdrive";
36 changes: 36 additions & 0 deletions packages/miniflare/test/plugins/hyperdrive/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Hyperdrive } from "@cloudflare/workers-types/experimental";
import { MiniflareOptions } from "miniflare";
import { MiniflareTestContext, miniflareTest } from "../../test-shared";

const TEST_CONN_STRING = `postgresql://user:password@localhost:5432/database`;

const opts: Partial<MiniflareOptions> = {
hyperdrives: {
hyperdrive: TEST_CONN_STRING,
},
};

const test = miniflareTest<{ hyperdrive: Hyperdrive }, MiniflareTestContext>(
opts,
async (global, _, env) => {
return global.Response.json({
connectionString: env.hyperdrive.connectionString,
user: env.hyperdrive.user,
password: env.hyperdrive.password,
database: env.hyperdrive.database,
host: env.hyperdrive.host,
});
}
);

test("configuration: fields match expected", async (t) => {
const hyperdriveResp = await t.context.mf.dispatchFetch("http://localhost/");
const hyperdrive: any = await hyperdriveResp.json();
// Since the host is random, this connectionString should be different
t.not(hyperdrive.connectionString, TEST_CONN_STRING);
t.is(hyperdrive.user, "user");
t.is(hyperdrive.password, "password");
t.is(hyperdrive.database, "database");
// Random host should not be the same as the original
t.not(hyperdrive.host, "localhost");
});