The open source coding agent.
TypeScript
Share: sync
Frank34a2dcb80a28
diff --git a/app/infra/app.ts b/app/infra/app.tsindex a331f14ebb5..1a97740f456 100644--- a/app/infra/app.ts+++ b/app/infra/app.ts@@ -23,14 +23,14 @@ export const api = new sst.cloudflare.Worker("Api", {},})-//new sst.cloudflare.StaticSite("Web", {-// path: "packages/web",-// environment: {-// VITE_API_URL: api.url,-// },-// errorPage: "fallback.html",-// build: {-// command: "bun run build",-// output: "dist/client",-// },-//})+new sst.cloudflare.StaticSite("Web", {+ path: "packages/web",+ environment: {+ VITE_API_URL: api.url,+ },+ errorPage: "fallback.html",+ build: {+ command: "bun run build",+ output: "dist/client",+ },+})diff --git a/app/packages/function/src/api.ts b/app/packages/function/src/api.tsindex fe8dc9ab5dc..efe0c5625d8 100644--- a/app/packages/function/src/api.ts+++ b/app/packages/function/src/api.ts@@ -21,20 +21,20 @@ export class SyncServer extends DurableObject {})}- async publish(filename: string, content: string) {+ async publish(key: string, content: string) {console.log("SyncServer publish",- filename,+ key,content,"to",this.ctx.getWebSockets().length,"subscribers",)- this.files.set(filename, content)- await this.ctx.storage.put(filename, content)+ this.files.set(key, content)+ await this.ctx.storage.put(key, content)this.ctx.getWebSockets().forEach((client) => {- client.send(JSON.stringify({ filename, content }))+ client.send(JSON.stringify({ key, content }))})}@@ -57,8 +57,8 @@ export class SyncServer extends DurableObject {this.ctx.acceptWebSocket(server)setTimeout(() => {- this.files.forEach((content, filename) =>- server.send(JSON.stringify({ filename, content })),+ this.files.forEach((content, key) =>+ server.send(JSON.stringify({ key, content })),)}, 0)@@ -107,12 +107,12 @@ export default {const body = await request.json()const sessionID = body.session_idconst shareID = body.share_id- const filename = body.filename+ const key = body.keyconst content = body.content- // validate filename- if (!filename.startsWith("info/") && !filename.startsWith("message/"))- return new Response("Error: Invalid filename", { status: 400 })+ // validate key+ if (!key.startsWith("info/") && !key.startsWith("message/"))+ return new Response("Error: Invalid key", { status: 400 })const infoFile = `${shareID}/info/${sessionID}.json`const ret = await Resource.Bucket.get(infoFile)@@ -122,10 +122,10 @@ export default {// send message to serverconst id = env.SYNC_SERVER.idFromName(sessionID)const stub = env.SYNC_SERVER.get(id)- await stub.publish(filename, content)+ await stub.publish(key, content)// store message- await Resource.Bucket.put(`${shareID}/${filename}`, content)+ await Resource.Bucket.put(`${shareID}/${key}`, content)return new Response(JSON.stringify({}), {headers: { "Content-Type": "application/json" },diff --git a/app/packages/function/sst-env.d.ts b/app/packages/function/sst-env.d.tsindex fdc298fd601..41727ee9ddd 100644--- a/app/packages/function/sst-env.d.ts+++ b/app/packages/function/sst-env.d.ts@@ -6,6 +6,10 @@import "sst"declare module "sst" {export interface Resource {+ "Web": {+ "type": "sst.cloudflare.StaticSite"+ "url": string+ }}}// cloudflarediff --git a/app/packages/web/src/routes/share/[id].tsx b/app/packages/web/src/routes/share/[id].tsxindex d7f80e06a65..851197de788 100644--- a/app/packages/web/src/routes/share/[id].tsx+++ b/app/packages/web/src/routes/share/[id].tsx@@ -3,7 +3,7 @@ import { createSignal, onCleanup, onMount, Show, For } from "solid-js"import { useParams } from "@solidjs/router"type Message = {- filename: string+ key: stringcontent: string}@@ -39,7 +39,7 @@ export default function SharePage() {setConnectionStatus("Connecting...")// Always use secure WebSocket protocol (wss)- const wsBaseUrl = apiUrl.replace(/^https?:\/\//, 'wss://')+ const wsBaseUrl = apiUrl.replace(/^https?:\/\//, "wss://")const wsUrl = `${wsBaseUrl}/share_poll?share_id=${shareId}`console.log("Connecting to WebSocket URL:", wsUrl)@@ -78,7 +78,7 @@ export default function SharePage() {clearTimeout(reconnectTimer)reconnectTimer = window.setTimeout(setupWebSocket,- 2000+ 2000,) as unknown as number}}@@ -133,7 +133,7 @@ export default function SharePage() {}}><div>- <strong>Filename:</strong> {msg.filename}+ <strong>Key:</strong> {msg.key}</div><div><strong>Content:</strong> {msg.content}diff --git a/app/sst-env.d.ts b/app/sst-env.d.tsindex b1aa7ea9a3b..7ca38b723ba 100644--- a/app/sst-env.d.ts+++ b/app/sst-env.d.ts@@ -12,6 +12,10 @@ declare module "sst" {"Bucket": {"type": "sst.cloudflare.Bucket"}+ "Web": {+ "type": "sst.cloudflare.StaticSite"+ "url": string+ }}}/// <reference path="sst-env.d.ts" />