Skip to content

Commit 45b5909

Browse files
authored
Merge pull request #30 from Brayden/bwilmoth/region-hints
Allow users to suggest DO region deployment location
2 parents fd32890 + 011f1b0 commit 45b5909

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ const DURABLE_OBJECT_ID = 'sql-durable-object';
1616
interface Env {
1717
AUTHORIZATION_TOKEN: string;
1818
DATABASE_DURABLE_OBJECT: DurableObjectNamespace;
19+
REGION: string;
1920
STUDIO_USER?: string;
2021
STUDIO_PASS?: string;
2122
// ## DO NOT REMOVE: TEMPLATE INTERFACE ##
2223
}
2324

25+
enum RegionLocationHint {
26+
AUTO = 'auto',
27+
WNAM = 'wnam', // Western North America
28+
ENAM = 'enam', // Eastern North America
29+
SAM = 'sam', // South America
30+
WEUR = 'weur', // Western Europe
31+
EEUR = 'eeur', // Eastern Europe
32+
APAC = 'apac', // Asia Pacific
33+
OC = 'oc', // Oceania
34+
AFR = 'afr', // Africa
35+
ME = 'me', // Middle East
36+
}
37+
2438
export class DatabaseDurableObject extends DurableObject {
2539
// Durable storage for the SQL database
2640
public sql: SqlStorage;
@@ -165,6 +179,11 @@ export class DatabaseDurableObject extends DurableObject {
165179
return this.clientConnected();
166180
} else if (request.method === 'GET' && url.pathname === '/status') {
167181
return this.statusRoute(request);
182+
} else if (request.method === 'GET' && url.pathname === '/status/trace') {
183+
const response = await fetch('https://cloudflare.com/cdn-cgi/trace');
184+
return new Response(response.body, {
185+
headers: response.headers
186+
});
168187
} else if (url.pathname.startsWith('/rest')) {
169188
return await this.liteREST.handleRequest(request);
170189
} else if (request.method === 'GET' && url.pathname === '/export/dump') {
@@ -294,8 +313,9 @@ export default {
294313
* Retrieve the Durable Object identifier from the environment bindings and instantiate a
295314
* Durable Object stub to interact with the Durable Object.
296315
*/
297-
let id: DurableObjectId = env.DATABASE_DURABLE_OBJECT.idFromName(DURABLE_OBJECT_ID);
298-
let stub = env.DATABASE_DURABLE_OBJECT.get(id);
316+
const region = env.REGION ?? RegionLocationHint.AUTO;
317+
const id: DurableObjectId = env.DATABASE_DURABLE_OBJECT.idFromName(DURABLE_OBJECT_ID);
318+
const stub = region !== RegionLocationHint.AUTO ? env.DATABASE_DURABLE_OBJECT.get(id, { locationHint: region as DurableObjectLocationHint }) : env.DATABASE_DURABLE_OBJECT.get(id);
299319

300320
// ## DO NOT REMOVE: TEMPLATE ROUTING ##
301321

wrangler.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ new_sqlite_classes = ["DatabaseDurableObject"] # Array of new classes
2828

2929
[vars]
3030
AUTHORIZATION_TOKEN = "ABC123"
31+
REGION = "auto"
3132

3233
# Uncomment the section below to create a user for logging into your database UI.
3334
# You can access the Studio UI at: https://your_endpoint/studio

0 commit comments

Comments
 (0)