Skip to content

Commit 1f7da2c

Browse files
committed
Use separate DO per-user in validator example
1 parent aeba44f commit 1f7da2c

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

examples/typescript-validator/src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export interface ValidateResponse {
1313
sessionId: string;
1414
compiled: boolean;
1515
timings: {
16-
install?: number;
1716
bundle?: number;
1817
load: number;
1918
execute: number;

examples/typescript-validator/worker/compiler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { getSandbox } from '@cloudflare/sandbox';
2-
import type { ValidateRequest, ValidateResponse, ErrorResponse } from './types';
2+
import type {
3+
ValidateRequest,
4+
ValidateResponse,
5+
ErrorResponse,
6+
ZodParseResult
7+
} from './types';
38

49
/**
510
* Compiler Durable Object
@@ -216,11 +221,7 @@ export class CompilerDO implements DurableObject {
216221
});
217222

218223
const response = await worker.getEntrypoint().fetch(testRequest);
219-
const result = (await response.json()) as {
220-
success: boolean;
221-
data?: unknown;
222-
error?: { issues: unknown[] };
223-
};
224+
const result = (await response.json()) as ZodParseResult;
224225

225226
timings.execute = Date.now() - executeStart;
226227

examples/typescript-validator/worker/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ export default {
99

1010
// Route API requests to Compiler DO
1111
if (url.pathname === '/validate') {
12-
const id = env.Compiler.idFromName('singleton');
12+
// Use session ID from header to isolate per user
13+
const sessionId = request.headers.get('X-Session-ID');
14+
if (!sessionId) {
15+
return Response.json(
16+
{ error: 'Missing X-Session-ID header' },
17+
{ status: 400 }
18+
);
19+
}
20+
21+
const id = env.Compiler.idFromName(sessionId);
1322
const stub = env.Compiler.get(id);
1423
return stub.fetch(request);
1524
}

examples/typescript-validator/worker/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ export interface ErrorResponse {
3333
error: string;
3434
details?: string;
3535
}
36+
37+
/**
38+
* Result from Zod schema validation
39+
*/
40+
export interface ZodParseResult {
41+
success: boolean;
42+
data?: unknown;
43+
error?: {
44+
issues: unknown[];
45+
};
46+
}

0 commit comments

Comments
 (0)