Skip to content

Commit

Permalink
Allow string[] for kvNamespaces/r2Buckets like Miniflare 2
Browse files Browse the repository at this point in the history
In this case, the binding name is assumed to be the same as the
namespace ID/bucket name.
  • Loading branch information
mrbbot committed Nov 15, 2022
1 parent f7675c3 commit d3449df
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 46 deletions.
44 changes: 21 additions & 23 deletions packages/tre/src/plugins/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Plugin,
SCRIPT_PLUGIN_NAMESPACE_PERSIST,
encodePersist,
namespaceEntries,
} from "../shared";
import { KV_PLUGIN_NAME } from "./constants";
import { KVGateway } from "./gateway";
Expand All @@ -17,8 +18,7 @@ import { KVRouter } from "./router";
import { SitesOptions, getSitesBindings, getSitesService } from "./sites";

export const KVOptionsSchema = z.object({
// TODO: also allow array like Miniflare 2
kvNamespaces: z.record(z.string()).optional(),
kvNamespaces: z.union([z.record(z.string()), z.string().array()]).optional(),

// Workers Sites
sitePath: z.string().optional(),
Expand Down Expand Up @@ -48,9 +48,8 @@ export const KV_PLUGIN: Plugin<
options: KVOptionsSchema,
sharedOptions: KVSharedOptionsSchema,
async getBindings(options) {
const bindings = Object.entries(
options.kvNamespaces ?? {}
).map<Worker_Binding>(([name, id]) => ({
const namespaces = namespaceEntries(options.kvNamespaces);
const bindings = namespaces.map<Worker_Binding>(([name, id]) => ({
name,
kvNamespace: { name: `${SERVICE_NAMESPACE_PREFIX}:${id}` },
}));
Expand All @@ -63,24 +62,23 @@ export const KV_PLUGIN: Plugin<
},
getServices({ options, sharedOptions }) {
const persistBinding = encodePersist(sharedOptions.kvPersist);
const services = Object.entries(options.kvNamespaces ?? []).map<Service>(
([_, id]) => ({
name: `${SERVICE_NAMESPACE_PREFIX}:${id}`,
worker: {
serviceWorkerScript: SCRIPT_PLUGIN_NAMESPACE_PERSIST,
compatibilityDate: "2022-09-01",
bindings: [
...persistBinding,
{ name: BINDING_TEXT_PLUGIN, text: KV_PLUGIN_NAME },
{ name: BINDING_TEXT_NAMESPACE, text: id },
{
name: BINDING_SERVICE_LOOPBACK,
service: { name: SERVICE_LOOPBACK },
},
],
},
})
);
const namespaces = namespaceEntries(options.kvNamespaces);
const services = namespaces.map<Service>(([_, id]) => ({
name: `${SERVICE_NAMESPACE_PREFIX}:${id}`,
worker: {
serviceWorkerScript: SCRIPT_PLUGIN_NAMESPACE_PERSIST,
compatibilityDate: "2022-09-01",
bindings: [
...persistBinding,
{ name: BINDING_TEXT_PLUGIN, text: KV_PLUGIN_NAME },
{ name: BINDING_TEXT_NAMESPACE, text: id },
{
name: BINDING_SERVICE_LOOPBACK,
service: { name: SERVICE_LOOPBACK },
},
],
},
}));

if (isWorkersSitesEnabled(options)) {
services.push(getSitesService(options));
Expand Down
41 changes: 18 additions & 23 deletions packages/tre/src/plugins/r2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {
Plugin,
SCRIPT_PLUGIN_NAMESPACE_PERSIST,
encodePersist,
namespaceEntries,
} from "../shared";
import { R2Gateway } from "./gateway";
import { R2Router } from "./router";

export const R2OptionsSchema = z.object({
r2Buckets: z.record(z.string()).optional(),
r2Buckets: z.union([z.record(z.string()), z.string().array()]).optional(),
});
export const R2SharedOptionsSchema = z.object({
r2Persist: PersistenceSchema,
Expand All @@ -31,38 +32,32 @@ export const R2_PLUGIN: Plugin<
options: R2OptionsSchema,
sharedOptions: R2SharedOptionsSchema,
getBindings(options) {
const bindings = Object.entries(
options.r2Buckets ?? []
).map<Worker_Binding>(([name, id]) => ({
const buckets = namespaceEntries(options.r2Buckets);
return buckets.map<Worker_Binding>(([name, id]) => ({
name,
r2Bucket: { name: `${R2_PLUGIN_NAME}:${id}` },
}));

return bindings;
},
getServices({ options, sharedOptions }) {
const persistBinding = encodePersist(sharedOptions.r2Persist);
const loopbackBinding: Worker_Binding = {
name: BINDING_SERVICE_LOOPBACK,
service: { name: SERVICE_LOOPBACK },
};
const services = Object.entries(options.r2Buckets ?? []).map<Service>(
([_, id]) => ({
name: `${R2_PLUGIN_NAME}:${id}`,
worker: {
serviceWorkerScript: SCRIPT_PLUGIN_NAMESPACE_PERSIST,
bindings: [
...persistBinding,
{ name: BINDING_TEXT_PLUGIN, text: R2_PLUGIN_NAME },
{ name: BINDING_TEXT_NAMESPACE, text: id },
loopbackBinding,
],
compatibilityDate: "2022-09-01",
},
})
);

return services;
const buckets = namespaceEntries(options.r2Buckets);
return buckets.map<Service>(([_, id]) => ({
name: `${R2_PLUGIN_NAME}:${id}`,
worker: {
serviceWorkerScript: SCRIPT_PLUGIN_NAMESPACE_PERSIST,
bindings: [
...persistBinding,
{ name: BINDING_TEXT_PLUGIN, text: R2_PLUGIN_NAME },
{ name: BINDING_TEXT_NAMESPACE, text: id },
loopbackBinding,
],
compatibilityDate: "2022-09-01",
},
}));
},
};

Expand Down
12 changes: 12 additions & 0 deletions packages/tre/src/plugins/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export type Plugin<
remoteStorage?: RemoteStorageConstructor;
});

export function namespaceEntries(
namespaces?: Record<string, string> | string[]
): [bindingName: string, id: string][] {
if (Array.isArray(namespaces)) {
return namespaces.map(([bindingName]) => [bindingName, bindingName]);
} else if (namespaces !== undefined) {
return Object.entries(namespaces);
} else {
return [];
}
}

export * from "./constants";
export * from "./gateway";
export * from "./router";

0 comments on commit d3449df

Please sign in to comment.