diff options
| author | Dax Raad <[email protected]> | 2025-05-30 14:40:53 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-30 14:40:59 -0400 |
| commit | e9bad39a7ed224efefe2d21a35a7d27cd25e0879 (patch) | |
| tree | f1ae466b4b6bac86a11f56a2e0cca366a1192685 /app/packages/function/src/api.ts | |
| parent | 42c7880858ee9bec72f3fb71c7f19512a38b0b8e (diff) | |
| download | opencode-e9bad39a7ed224efefe2d21a35a7d27cd25e0879.tar.gz opencode-e9bad39a7ed224efefe2d21a35a7d27cd25e0879.zip | |
sync
Diffstat (limited to 'app/packages/function/src/api.ts')
| -rw-r--r-- | app/packages/function/src/api.ts | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/app/packages/function/src/api.ts b/app/packages/function/src/api.ts index 52a326727..8b6c52dd8 100644 --- a/app/packages/function/src/api.ts +++ b/app/packages/function/src/api.ts @@ -2,12 +2,13 @@ import { DurableObject } from "cloudflare:workers" import { randomUUID } from "node:crypto" import { Resource } from "sst" -type Bindings = { +type Env = { SYNC_SERVER: DurableObjectNamespace<SyncServer> + Bucket: R2Bucket } -export class SyncServer extends DurableObject { - constructor(ctx: DurableObjectState, env: Bindings) { +export class SyncServer extends DurableObject<Env> { + constructor(ctx: DurableObjectState, env: Env) { super(ctx, env) } async fetch() { @@ -18,14 +19,10 @@ export class SyncServer extends DurableObject { this.ctx.acceptWebSocket(server) - setTimeout(async () => { - const data = await this.ctx.storage.list({ - prefix: "data/", - }) - for (const [key, content] of Object.entries(data)) { - server.send(JSON.stringify({ key, content })) - } - }, 0) + const data = await this.ctx.storage.list() + for (const [key, content] of data.entries()) { + server.send(JSON.stringify({ key, content })) + } return new Response(null, { status: 101, @@ -49,11 +46,17 @@ export class SyncServer extends DurableObject { return new Response("Error: Invalid key", { status: 400 }) // store message - await Resource.Bucket.put(`${key}.json`, JSON.stringify(content)) - await this.ctx.storage.put("data/" + key, content) + await this.env.Bucket.put(`share/${key}.json`, JSON.stringify(content), { + httpMetadata: { + contentType: "application/json", + }, + }) + await this.ctx.storage.put(key, content) const clients = this.ctx.getWebSockets() console.log("SyncServer publish", key, "to", clients.length, "subscribers") - clients.forEach((client) => client.send(JSON.stringify({ key, content }))) + for (const client of clients) { + client.send(JSON.stringify({ key, content })) + } } public async share(sessionID: string) { @@ -90,7 +93,7 @@ export class SyncServer extends DurableObject { } export default { - async fetch(request: Request, env: Bindings, ctx: ExecutionContext) { + async fetch(request: Request, env: Env, ctx: ExecutionContext) { const url = new URL(request.url) const splits = url.pathname.split("/") const method = splits[1] @@ -155,6 +158,7 @@ export default { }) } const id = url.searchParams.get("id") + console.log("share_poll", id) if (!id) return new Response("Error: Share ID is required", { status: 400 }) const stub = env.SYNC_SERVER.get(env.SYNC_SERVER.idFromName(id)) |
