diff --git a/src/index.ts b/src/index.ts index b797c84..9dbe2d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ export interface Env { AUTHORIZATION_TOKEN: string; DATABASE_DURABLE_OBJECT: DurableObjectNamespace; REGION: string; + JURISDICTION?: Jurisdiction; STUDIO_USER?: string; STUDIO_PASS?: string; // ## DO NOT REMOVE: TEMPLATE INTERFACE ## @@ -35,6 +36,11 @@ enum RegionLocationHint { ME = 'me', // Middle East } +enum Jurisdiction { + EU = 'eu', + FEDRAMP = 'fedramp' +} + export class DatabaseDurableObject extends DurableObject { // Durable storage for the SQL database public sql: SqlStorage; @@ -310,19 +316,25 @@ export default { } /** - * Retrieve the Durable Object identifier from the environment bindings and instantiate a - * Durable Object stub to interact with the Durable Object. + * Handle Durable Object creation with jurisdiction or region preferences */ - const region = env.REGION ?? RegionLocationHint.AUTO; - const id: DurableObjectId = env.DATABASE_DURABLE_OBJECT.idFromName(DURABLE_OBJECT_ID); - const stub = region !== RegionLocationHint.AUTO ? env.DATABASE_DURABLE_OBJECT.get(id, { locationHint: region as DurableObjectLocationHint }) : env.DATABASE_DURABLE_OBJECT.get(id); - - // ## DO NOT REMOVE: TEMPLATE ROUTING ## + let id: DurableObjectId; + let stub: DurableObjectStub; + + if (env.JURISDICTION) { + // If jurisdiction is specified, it takes precedence over region + const namespace = env.DATABASE_DURABLE_OBJECT.jurisdiction(env.JURISDICTION as Jurisdiction); + id = namespace.idFromName(DURABLE_OBJECT_ID); + stub = namespace.get(id); + } else { + // Fall back to region-based routing if no jurisdiction is specified + id = env.DATABASE_DURABLE_OBJECT.idFromName(DURABLE_OBJECT_ID); + const region = env.REGION ?? RegionLocationHint.AUTO; + stub = region !== RegionLocationHint.AUTO + ? env.DATABASE_DURABLE_OBJECT.get(id, { locationHint: region as DurableObjectLocationHint }) + : env.DATABASE_DURABLE_OBJECT.get(id); + } - /** - * Pass the fetch request directly to the Durable Object, which will handle the request - * and return a response to be sent back to the client. - */ return await stub.fetch(request); }, } satisfies ExportedHandler; diff --git a/wrangler.toml b/wrangler.toml index 6db1c29..d2be3d0 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -28,6 +28,7 @@ new_sqlite_classes = ["DatabaseDurableObject"] [vars] AUTHORIZATION_TOKEN = "ABC123" +JURISDICTION = "eu" # "eu" or "fedramp" REGION = "auto" # Uncomment the section below to create a user for logging into your database UI.