summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFrank <[email protected]>2025-05-23 14:40:28 -0400
committerDax Raad <[email protected]>2025-05-26 12:40:17 -0400
commit34a2dcb80a28e208c986d39ce1e145f245691f62 (patch)
tree6f5e4450c05fc2e10d5dcd3e8458ba3aff981335
parent8cbd59296e8510cb590b162d7548872fabf16c10 (diff)
downloadopencode-34a2dcb80a28e208c986d39ce1e145f245691f62.tar.gz
opencode-34a2dcb80a28e208c986d39ce1e145f245691f62.zip
Share: sync
-rw-r--r--app/infra/app.ts22
-rw-r--r--app/packages/function/src/api.ts26
-rw-r--r--app/packages/function/sst-env.d.ts4
-rw-r--r--app/packages/web/src/routes/share/[id].tsx8
-rw-r--r--app/sst-env.d.ts4
5 files changed, 36 insertions, 28 deletions
diff --git a/app/infra/app.ts b/app/infra/app.ts
index a331f14eb..1a97740f4 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.ts
index fe8dc9ab5..efe0c5625 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_id
const shareID = body.share_id
- const filename = body.filename
+ const key = body.key
const 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 server
const 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.ts
index fdc298fd6..41727ee9d 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
+ }
}
}
// cloudflare
diff --git a/app/packages/web/src/routes/share/[id].tsx b/app/packages/web/src/routes/share/[id].tsx
index d7f80e06a..851197de7 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: string
content: 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.ts
index b1aa7ea9a..7ca38b723 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" />