moi-computer 0.3.0-next.0 → 0.3.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/hooks/useMeiEvents.ts +2 -1
- package/client/lib/connection.ts +2 -1
- package/client/lib/ws-url.ts +10 -0
- package/dist/{chunk-c3zjp9ez.js → chunk-w18801vn.js} +126 -126
- package/dist/index.html +1 -1
- package/package.json +1 -1
- package/server/api.ts +8 -3
- package/server/applets.ts +84 -8
- package/server/views.ts +0 -0
- package/server/widgets.ts +3 -2
package/dist/index.html
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
</script>
|
|
23
23
|
|
|
24
|
-
<link rel="stylesheet" crossorigin href="/chunk-07qf2tv1.css"><script type="module" crossorigin src="/chunk-
|
|
24
|
+
<link rel="stylesheet" crossorigin href="/chunk-07qf2tv1.css"><script type="module" crossorigin src="/chunk-w18801vn.js"></script></head>
|
|
25
25
|
<body>
|
|
26
26
|
<div id="root"></div>
|
|
27
27
|
|
package/package.json
CHANGED
package/server/api.ts
CHANGED
|
@@ -104,7 +104,7 @@ one.get('/widgets/*', c => {
|
|
|
104
104
|
const id = c.req.param('id')
|
|
105
105
|
const { name, file } = parseAppletTail(c.req.url, id, 'widgets')
|
|
106
106
|
if (!name) return c.text('Not found', 404)
|
|
107
|
-
return serveWidget(name, file, c.get('ws').path, apiBaseFor(id))
|
|
107
|
+
return serveWidget(name, file, c.get('ws').path, apiBaseFor(id), c.req.header('if-none-match'))
|
|
108
108
|
})
|
|
109
109
|
|
|
110
110
|
// Views — full-screen agent apps. Mirrors the widget pair above: the exact path
|
|
@@ -115,7 +115,7 @@ one.get('/views/*', c => {
|
|
|
115
115
|
const id = c.req.param('id')
|
|
116
116
|
const { name, file } = parseAppletTail(c.req.url, id, 'views')
|
|
117
117
|
if (!name) return c.text('Not found', 404)
|
|
118
|
-
return serveView(name, file, c.get('ws').path, apiBaseFor(id))
|
|
118
|
+
return serveView(name, file, c.get('ws').path, apiBaseFor(id), c.req.header('if-none-match'))
|
|
119
119
|
})
|
|
120
120
|
|
|
121
121
|
// Workspace file stream — an applet's `fileUrl(path)` resolves here. Streams a
|
|
@@ -126,7 +126,12 @@ one.get('/views/*', c => {
|
|
|
126
126
|
one.get('/fs/*', c => {
|
|
127
127
|
const id = c.req.param('id')
|
|
128
128
|
const tail = new URL(c.req.url).pathname.split(`/api/workspaces/${id}/fs/`)[1] ?? ''
|
|
129
|
-
return serveWorkspaceFile(
|
|
129
|
+
return serveWorkspaceFile(
|
|
130
|
+
c.get('ws').path,
|
|
131
|
+
tail,
|
|
132
|
+
c.req.header('range'),
|
|
133
|
+
c.req.header('if-none-match')
|
|
134
|
+
)
|
|
130
135
|
})
|
|
131
136
|
|
|
132
137
|
// Applet RPC — the home for server-function calls from a bundle. The bundle's
|
package/server/applets.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// `%%MOI_APPLET_API_BASE%%` sentinel (for RPC + `fileUrl`), swapped to the real
|
|
13
13
|
// `/api/workspaces/<id>` base when served — so the on-disk bundle is
|
|
14
14
|
// workspace-agnostic.
|
|
15
|
-
import { realpathSync } from 'node:fs'
|
|
15
|
+
import { realpathSync, statSync } from 'node:fs'
|
|
16
16
|
import { mkdir, readdir, rm } from 'node:fs/promises'
|
|
17
17
|
import { dirname, join, resolve, sep } from 'path'
|
|
18
18
|
|
|
@@ -136,16 +136,44 @@ const CODE_FILE_RE = /\.js$/
|
|
|
136
136
|
// can be requested; `..` is rejected separately.
|
|
137
137
|
const ALLOWED_FILE_RE = /^[a-zA-Z0-9_-][a-zA-Z0-9._-]*$/
|
|
138
138
|
|
|
139
|
+
const JS_CONTENT_TYPE = 'text/javascript; charset=utf-8'
|
|
140
|
+
// A content-hashed url is a fingerprint of its bytes, so it's safe to cache
|
|
141
|
+
// without revalidation. But immutability here is INFERRED from the filename
|
|
142
|
+
// (HASHED_FILE_RE) rather than guaranteed by construction (unlike a sha256-named
|
|
143
|
+
// asset) — so we cap the TTL at a week instead of the usual year. If that
|
|
144
|
+
// inference ever misfires, a wrongly-pinned copy self-heals within a week at the
|
|
145
|
+
// edge and in the browser, rather than sticking around for a year.
|
|
146
|
+
const IMMUTABLE_CACHE = 'public, max-age=604800, immutable'
|
|
147
|
+
|
|
148
|
+
// Which build-dir files are safe to serve `immutable`. The build emits exactly
|
|
149
|
+
// two content-hashed shapes — Bun's `chunk-<hash>.js` and the runtime plugin's
|
|
150
|
+
// `<stem>-<hash>.<ext>` assets (hash = hex, see build-applet.ts) — alongside the
|
|
151
|
+
// ONE stable-named entry, `index.js`. We match those shapes POSITIVELY and
|
|
152
|
+
// default everything else (the entry, and anything unexpected) to revalidation:
|
|
153
|
+
// this fix exists to stop a shared cache pinning a stale copy at a stable url,
|
|
154
|
+
// so an unrecognized name must fail safe (revalidate), never fail stale
|
|
155
|
+
// (immutable). The hex-suffix arm rejects real words (`-sprite`, `-preview`) —
|
|
156
|
+
// they aren't hex — so only genuine hashes opt in.
|
|
157
|
+
const HASHED_FILE_RE = /^chunk-[0-9a-z]+\.\w+$|-[0-9a-f]{6,}\.\w+$/i
|
|
158
|
+
|
|
139
159
|
// Serve one file from a compiled applet directory: the `index.js` entry, a code
|
|
140
160
|
// chunk, or a bundled asset. `apiBase` is the workspace's `/api/workspaces/<id>`
|
|
141
161
|
// prefix — substituted for the build-time sentinel in every `.js` so RPC and
|
|
142
162
|
// `fileUrl` calls hit the right workspace. Assets stream untouched.
|
|
163
|
+
//
|
|
164
|
+
// Caching turns on the hashed-vs-stable split. A content-hashed chunk/asset gets
|
|
165
|
+
// a new url whenever its bytes change, so it's cached forever. The `index.js`
|
|
166
|
+
// entry (and anything not recognizably hashed) lives at ONE url across rebuilds,
|
|
167
|
+
// so a shared cache (e.g. Cloudflare) that stored it once would hand back a
|
|
168
|
+
// STALE copy after the next `moi bundle` — it gets an ETag (size+mtime) +
|
|
169
|
+
// `no-cache` instead, so an unchanged file costs a 304 and a rebuilt one busts.
|
|
143
170
|
export async function serveApplet(
|
|
144
171
|
kind: AppletKind,
|
|
145
172
|
name: string,
|
|
146
173
|
file: string,
|
|
147
174
|
workspacePath: string,
|
|
148
|
-
apiBase: string
|
|
175
|
+
apiBase: string,
|
|
176
|
+
ifNoneMatch?: string | null
|
|
149
177
|
): Promise<Response> {
|
|
150
178
|
if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
|
|
151
179
|
return new Response('Invalid name', { status: 400 })
|
|
@@ -160,11 +188,25 @@ export async function serveApplet(
|
|
|
160
188
|
if (!(await bunFile.exists())) {
|
|
161
189
|
return new Response(`"${name}" not built. Run: moi bundle`, { status: 404 })
|
|
162
190
|
}
|
|
191
|
+
|
|
192
|
+
// Content-hashed → immutable. Everything else → revalidate (ETag + no-cache),
|
|
193
|
+
// short-circuiting to a bodyless 304 when the client's ETag still matches.
|
|
194
|
+
let cache: Record<string, string>
|
|
195
|
+
if (HASHED_FILE_RE.test(file)) {
|
|
196
|
+
cache = { 'Cache-Control': IMMUTABLE_CACHE }
|
|
197
|
+
} else {
|
|
198
|
+
const etag = `"${bunFile.size}-${Math.trunc(bunFile.lastModified)}"`
|
|
199
|
+
cache = { ETag: etag, 'Cache-Control': 'no-cache' }
|
|
200
|
+
if (ifNoneMatch === etag) return new Response(null, { status: 304, headers: cache })
|
|
201
|
+
}
|
|
202
|
+
|
|
163
203
|
if (CODE_FILE_RE.test(file)) {
|
|
164
204
|
const swapped = (await bunFile.text()).replaceAll(APPLET_API_BASE_SENTINEL, apiBase)
|
|
165
|
-
return new Response(swapped, { headers: { 'Content-Type':
|
|
205
|
+
return new Response(swapped, { headers: { 'Content-Type': JS_CONTENT_TYPE, ...cache } })
|
|
166
206
|
}
|
|
167
|
-
return new Response(bunFile
|
|
207
|
+
return new Response(bunFile, {
|
|
208
|
+
headers: { 'Content-Type': bunFile.type || 'application/octet-stream', ...cache }
|
|
209
|
+
})
|
|
168
210
|
}
|
|
169
211
|
|
|
170
212
|
// ---- route helpers ----------------------------------------------------------
|
|
@@ -255,23 +297,51 @@ export function resolveWorkspaceMediaFile(workspaceRoot: string, tail: string):
|
|
|
255
297
|
// Range is handled explicitly (slice the BunFile, 206 + Content-Range): Bun's
|
|
256
298
|
// implicit range handling for `new Response(Bun.file())` doesn't fire once any
|
|
257
299
|
// headers object is attached, and <video>/<audio> seeking needs byte ranges.
|
|
300
|
+
//
|
|
301
|
+
// Caching: these are the user's own workspace files — private, and rewritten in
|
|
302
|
+
// place by the agent (a regenerated clip keeps its path). So they must NEVER be
|
|
303
|
+
// stored by a shared/edge cache sitting in front of moi, or one user's video
|
|
304
|
+
// could be served to the next, and an edited file could be served stale from a
|
|
305
|
+
// stable url (the applet-bundle incident, but with private bytes). `private`
|
|
306
|
+
// keeps them off the edge; a strong ETag (size + nanosecond mtime) + `no-cache`
|
|
307
|
+
// lets the browser revalidate cheaply (304) while re-fetching when it changes.
|
|
258
308
|
export async function serveWorkspaceFile(
|
|
259
309
|
workspaceRoot: string,
|
|
260
310
|
tail: string,
|
|
261
|
-
range?: string | null
|
|
311
|
+
range?: string | null,
|
|
312
|
+
ifNoneMatch?: string | null
|
|
262
313
|
): Promise<Response> {
|
|
263
314
|
const realTarget = resolveWorkspaceMediaFile(workspaceRoot, tail)
|
|
264
315
|
if (realTarget instanceof Response) return realTarget
|
|
265
316
|
const file = Bun.file(realTarget)
|
|
266
317
|
|
|
267
|
-
|
|
318
|
+
let stat: ReturnType<typeof statSync>
|
|
319
|
+
try {
|
|
320
|
+
stat = statSync(realTarget, { bigint: true })
|
|
321
|
+
} catch {
|
|
322
|
+
return new Response('Not found', { status: 404 })
|
|
323
|
+
}
|
|
324
|
+
const size = Number(stat.size)
|
|
268
325
|
const type = file.type || 'application/octet-stream'
|
|
326
|
+
|
|
327
|
+
// Validator = size + NANOSECOND mtime. This route serves agent-regenerated
|
|
328
|
+
// files at stable paths, so a weak validator that returns a false 304 pins
|
|
329
|
+
// stale media in the browser. Truncating mtime to whole milliseconds (or
|
|
330
|
+
// seconds) can collide when a same-length rewrite lands inside one tick; ns
|
|
331
|
+
// resolution closes that window without hashing the (possibly huge) file on
|
|
332
|
+
// every request. A tool that deliberately preserves mtime with same-size but
|
|
333
|
+
// different bytes could still collide — only a content hash defeats that, and
|
|
334
|
+
// it's not worth re-reading a video per request when agents rewrite normally.
|
|
335
|
+
const etag = `"${size}-${stat.mtimeNs}"`
|
|
336
|
+
const cache = { ETag: etag, 'Cache-Control': 'private, no-cache' }
|
|
337
|
+
if (ifNoneMatch === etag) return new Response(null, { status: 304, headers: cache })
|
|
338
|
+
|
|
269
339
|
const parsed = range ? parseByteRange(range, size) : null
|
|
270
340
|
|
|
271
341
|
if (parsed === 'invalid') {
|
|
272
342
|
return new Response('Range Not Satisfiable', {
|
|
273
343
|
status: 416,
|
|
274
|
-
headers: { 'Content-Range': `bytes */${size}`, 'Accept-Ranges': 'bytes' }
|
|
344
|
+
headers: { ...cache, 'Content-Range': `bytes */${size}`, 'Accept-Ranges': 'bytes' }
|
|
275
345
|
})
|
|
276
346
|
}
|
|
277
347
|
if (parsed) {
|
|
@@ -279,6 +349,7 @@ export async function serveWorkspaceFile(
|
|
|
279
349
|
return new Response(file.slice(start, end + 1), {
|
|
280
350
|
status: 206,
|
|
281
351
|
headers: {
|
|
352
|
+
...cache,
|
|
282
353
|
'Content-Type': type,
|
|
283
354
|
'Content-Length': String(end - start + 1),
|
|
284
355
|
'Content-Range': `bytes ${start}-${end}/${size}`,
|
|
@@ -287,7 +358,12 @@ export async function serveWorkspaceFile(
|
|
|
287
358
|
})
|
|
288
359
|
}
|
|
289
360
|
return new Response(file, {
|
|
290
|
-
headers: {
|
|
361
|
+
headers: {
|
|
362
|
+
...cache,
|
|
363
|
+
'Content-Type': type,
|
|
364
|
+
'Content-Length': String(size),
|
|
365
|
+
'Accept-Ranges': 'bytes'
|
|
366
|
+
}
|
|
291
367
|
})
|
|
292
368
|
}
|
|
293
369
|
|
package/server/views.ts
CHANGED
|
Binary file
|
package/server/widgets.ts
CHANGED
|
@@ -158,7 +158,8 @@ export function serveWidget(
|
|
|
158
158
|
name: string,
|
|
159
159
|
file: string,
|
|
160
160
|
workspacePath: string,
|
|
161
|
-
apiBase: string
|
|
161
|
+
apiBase: string,
|
|
162
|
+
ifNoneMatch?: string | null
|
|
162
163
|
): Promise<Response> {
|
|
163
|
-
return serveApplet('widget', name, file, workspacePath, apiBase)
|
|
164
|
+
return serveApplet('widget', name, file, workspacePath, apiBase, ifNoneMatch)
|
|
164
165
|
}
|