From de187ffa1cb33f474e052334e8b435cce4d3e574 Mon Sep 17 00:00:00 2001 From: viktormarinho Date: Sat, 6 Sep 2025 20:09:08 -0300 Subject: [PATCH 1/2] add api for enforcing id and stop serving homepage --- dist/index.d.ts | 2 ++ dist/index.js | 8 ++++++-- package.json | 4 ++-- src/index.ts | 12 +++++++++--- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 99744eb..3d7eaac 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -104,6 +104,8 @@ interface StudioTransactionRequest { } type StudioRequest = StudioQueryRequest | StudioTransactionRequest; interface StudioOptions { + enforceId?: string; + serveHomepage?: boolean; basicAuth?: { username: string; password: string; diff --git a/dist/index.js b/dist/index.js index cd07f07..d293c1e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -331,8 +331,11 @@ export async function studio(request, doNamespace, options) { if (request.method === 'GET') { // This is where we render the interface const url = new URL(request.url); - const stubId = url.searchParams.get('id'); + const stubId = options?.enforceId ?? url.searchParams.get('id'); if (!stubId) { + if (options?.serveHomepage === false) { + return new Response('Not found', { status: 404 }); + } return new Response(createHomepageInterface(), { headers: { 'Content-Type': 'text/html' } }); } return new Response(createStudioInterface(stubId), { headers: { 'Content-Type': 'text/html' } }); @@ -340,7 +343,8 @@ export async function studio(request, doNamespace, options) { else if (request.method === 'POST') { const body = (await request.json()); if (body.type === 'query' || body.type === 'transaction') { - const stubId = doNamespace.idFromName(body.id); + const id = options?.enforceId ?? body.id; + const stubId = doNamespace.idFromName(id); const stub = doNamespace.get(stubId); try { // @ts-ignore - accessing __studio method that we know exists diff --git a/package.json b/package.json index 8671a03..c3479af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@outerbase/browsable-durable-object", - "version": "0.1.1", + "version": "0.2.0", "type": "module", "module": "./dist/index.js", "main": "./dist/index.js", @@ -34,4 +34,4 @@ "typescript": "^5.5.2", "wrangler": "^3.114.0" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index ee864b8..8357206 100644 --- a/src/index.ts +++ b/src/index.ts @@ -396,6 +396,8 @@ function createStudioInterface(stubId: string) { } interface StudioOptions { + enforceId?: string; + serveHomepage?: boolean; basicAuth?: { username: string; password: string; @@ -433,9 +435,12 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa if (request.method === 'GET') { // This is where we render the interface const url = new URL(request.url); - const stubId = url.searchParams.get('id'); + const stubId = options?.enforceId ?? url.searchParams.get('id'); if (!stubId) { + if (options?.serveHomepage === false) { + return new Response('Not found', { status: 404 }); + } return new Response(createHomepageInterface(), { headers: { 'Content-Type': 'text/html' } }); } @@ -444,7 +449,8 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa const body = (await request.json()) as StudioRequest; if (body.type === 'query' || body.type === 'transaction') { - const stubId = doNamespace.idFromName(body.id); + const id = options?.enforceId ?? body.id; + const stubId = doNamespace.idFromName(id); const stub = doNamespace.get(stubId); try { @@ -463,4 +469,4 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa } return new Response('Method not allowed'); -} \ No newline at end of file +} From 2d341b13a8794004aea896db7164d05f79f98f4b Mon Sep 17 00:00:00 2001 From: viktormarinho Date: Sat, 6 Sep 2025 20:10:42 -0300 Subject: [PATCH 2/2] nicer --- dist/index.d.ts | 2 +- dist/index.js | 2 +- src/index.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 3d7eaac..4f694df 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -105,7 +105,7 @@ interface StudioTransactionRequest { type StudioRequest = StudioQueryRequest | StudioTransactionRequest; interface StudioOptions { enforceId?: string; - serveHomepage?: boolean; + disableHomepage?: boolean; basicAuth?: { username: string; password: string; diff --git a/dist/index.js b/dist/index.js index d293c1e..cc19f86 100644 --- a/dist/index.js +++ b/dist/index.js @@ -333,7 +333,7 @@ export async function studio(request, doNamespace, options) { const url = new URL(request.url); const stubId = options?.enforceId ?? url.searchParams.get('id'); if (!stubId) { - if (options?.serveHomepage === false) { + if (options?.disableHomepage) { return new Response('Not found', { status: 404 }); } return new Response(createHomepageInterface(), { headers: { 'Content-Type': 'text/html' } }); diff --git a/src/index.ts b/src/index.ts index 8357206..9a29c89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -397,7 +397,7 @@ function createStudioInterface(stubId: string) { interface StudioOptions { enforceId?: string; - serveHomepage?: boolean; + disableHomepage?: boolean; basicAuth?: { username: string; password: string; @@ -438,7 +438,7 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa const stubId = options?.enforceId ?? url.searchParams.get('id'); if (!stubId) { - if (options?.serveHomepage === false) { + if (options?.disableHomepage) { return new Response('Not found', { status: 404 }); } return new Response(createHomepageInterface(), { headers: { 'Content-Type': 'text/html' } });