okno 1.0.0-beta.39 → 1.0.0-beta.41

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.
@@ -1,489 +0,0 @@
1
- import { extname as z, join as y, dirname as T, relative as N, sep as D, resolve as g } from "node:path";
2
- import { existsSync as O, readFileSync as U, mkdirSync as M, writeFileSync as I, cpSync as Y } from "node:fs";
3
- import { fileURLToPath as Q } from "node:url";
4
- import { k as Z, g as x, s as E, b as q, a as tt, c as et, e as ot, d as nt, f as it, j as P, l as $, m as rt, h as st, i as at } from "./dispatch-BifB_3GO.js";
5
- import { simpleGit as R } from "simple-git";
6
- import "node:child_process";
7
- import "./parse-tlTHgSSs.js";
8
- function F(n, r, o) {
9
- n.statusCode = r, n.setHeader("Content-Type", "application/json"), n.setHeader("Cache-Control", "no-store"), n.end(JSON.stringify(o));
10
- }
11
- function lt(n) {
12
- return new Promise((r) => {
13
- let o = "";
14
- n.on("data", (t) => o += t), n.on("end", () => {
15
- try {
16
- r(o ? JSON.parse(o) : {});
17
- } catch {
18
- r({});
19
- }
20
- }), n.on("error", () => r({}));
21
- });
22
- }
23
- function ct(n) {
24
- return new Promise((r, o) => {
25
- const t = [];
26
- n.on("data", (c) => t.push(c)), n.on("end", () => r(Buffer.concat(t))), n.on("error", o);
27
- });
28
- }
29
- function ut(n, r) {
30
- if (r) {
31
- for (const o of new Set(r.modules)) {
32
- const t = n.moduleGraph.getModuleById("\0okno:" + o);
33
- t && n.moduleGraph.invalidateModule(t);
34
- }
35
- for (const o of new Set(r.files))
36
- n.moduleGraph.getModulesByFile(o)?.forEach((t) => n.moduleGraph.invalidateModule(t));
37
- }
38
- }
39
- function dt(n, r) {
40
- n.middlewares.use(async (o, t, c) => {
41
- if (!o.url?.startsWith("/__okno/api/")) return c();
42
- const m = o.url.slice(12).split("?")[0], e = {
43
- method: o.method || "GET",
44
- route: m,
45
- json: () => lt(o),
46
- binary: () => ct(o),
47
- header: (i) => {
48
- const s = o.headers[i];
49
- return Array.isArray(s) ? s[0] : s;
50
- }
51
- };
52
- try {
53
- const i = await Z(r, e);
54
- if (!i) return c();
55
- ut(n, i.invalidation), F(t, i.status, i.body);
56
- } catch (i) {
57
- F(t, 500, { error: i instanceof Error ? i.message : String(i) });
58
- }
59
- });
60
- }
61
- function ft(n, r) {
62
- let o;
63
- try {
64
- o = decodeURIComponent(r);
65
- } catch {
66
- return null;
67
- }
68
- if (!o || o.includes("\0")) return null;
69
- const t = g(n, "files"), c = g(t, o);
70
- return c === t || !c.startsWith(t + D) ? null : c;
71
- }
72
- const H = "okno", pt = {
73
- ".webp": "image/webp",
74
- ".png": "image/png",
75
- ".jpg": "image/jpeg",
76
- ".jpeg": "image/jpeg",
77
- ".gif": "image/gif",
78
- ".svg": "image/svg+xml",
79
- ".avif": "image/avif",
80
- ".ico": "image/x-icon",
81
- ".mp4": "video/mp4",
82
- ".webm": "video/webm",
83
- ".ogg": "video/ogg",
84
- ".mp3": "audio/mpeg",
85
- ".wav": "audio/wav",
86
- ".flac": "audio/flac",
87
- ".pdf": "application/pdf",
88
- ".woff2": "font/woff2",
89
- ".woff": "font/woff",
90
- ".ttf": "font/ttf",
91
- ".otf": "font/otf"
92
- };
93
- function gt(n, r) {
94
- dt(n, r), n.middlewares.use((o, t, c) => {
95
- if (o.method !== "GET" || !o.url?.startsWith("/okno/files/")) return c();
96
- const m = o.url.slice(12).split("?")[0];
97
- if (!m) return c();
98
- const e = ft(r, m);
99
- if (!e)
100
- return t.statusCode = 403, t.end("Forbidden");
101
- if (!O(e))
102
- return t.statusCode = 404, t.end("Not found");
103
- const i = z(e).toLowerCase();
104
- t.setHeader("Content-Type", pt[i] || "application/octet-stream"), t.setHeader("Cache-Control", "public, max-age=3600"), t.end(U(e));
105
- }), n.middlewares.use(async (o, t, c) => {
106
- if (!o.url?.startsWith("/__okno/")) return c();
107
- const m = o.url.slice(8);
108
- t.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate"), t.setHeader("Pragma", "no-cache"), t.setHeader("Expires", "0");
109
- try {
110
- if (o.method === "GET" && m === "repo") {
111
- t.setHeader("Content-Type", "application/json");
112
- try {
113
- const e = R(n.config.root), i = (await e.remote(["get-url", "origin"]))?.toString().trim(), s = (await e.revparse(["--abbrev-ref", "HEAD"])).trim(), { owner: f, repo: a } = mt(i);
114
- return t.end(
115
- JSON.stringify({ ok: !0, owner: f, repo: a, branch: s || null, remote: i || null })
116
- );
117
- } catch (e) {
118
- return t.end(JSON.stringify({ ok: !1, error: String(e) }));
119
- }
120
- }
121
- if (o.method === "POST" && m.startsWith("mirror")) {
122
- const i = new URL(o.url, "http://localhost").searchParams.get("path");
123
- if (!i || i.includes("..") || !i.startsWith("files/"))
124
- return t.statusCode = 400, t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ error: "Invalid path" }));
125
- const s = y(r, i);
126
- if (O(s))
127
- return t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ ok: !0, skipped: !0 }));
128
- const f = await ht(o);
129
- return M(T(s), { recursive: !0 }), I(s, f), t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ ok: !0, written: i, bytes: f.length }));
130
- }
131
- if (o.method === "POST" && m === "sync") {
132
- const i = (await yt(o))?.path;
133
- if (i) {
134
- if (i.includes("..") || i.includes("\0"))
135
- return t.statusCode = 400, t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ error: "Invalid path" }));
136
- const u = y(r, i);
137
- try {
138
- const l = R(r);
139
- await l.fetch("origin", H);
140
- const h = (await l.revparse(["--show-toplevel"])).trim(), p = N(h, u).split(D).join("/"), w = await l.show([`origin/${H}:${p}`]);
141
- M(T(u), { recursive: !0 }), I(u, w);
142
- } catch (l) {
143
- const h = l instanceof Error ? l.message : String(l);
144
- console.warn(
145
- `[okno] sync: could not materialize ${i} from origin/${H} — ${h.split(`
146
- `)[0]}`
147
- );
148
- }
149
- return A(n, i, u), t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ ok: !0, invalidated: i }));
150
- }
151
- const s = R(r), f = await s.revparse(["--show-toplevel"]), a = N(f.trim(), r);
152
- try {
153
- const u = await s.pull();
154
- for (const l of u.files)
155
- if (l.startsWith(`${a}/`)) {
156
- const h = l.slice(`${a}/`.length), p = y(r, h);
157
- A(n, h, p);
158
- }
159
- return t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ ok: !0, files: u.files }));
160
- } catch (u) {
161
- const l = u instanceof Error ? u.message : String(u);
162
- return console.warn(`[okno] sync: git pull skipped — ${l.split(`
163
- `)[0]}`), t.setHeader("Content-Type", "application/json"), t.end(JSON.stringify({ ok: !0, pullSkipped: !0, reason: l }));
164
- }
165
- }
166
- c();
167
- } catch (e) {
168
- t.statusCode = 500, t.end(JSON.stringify({ error: String(e) }));
169
- }
170
- });
171
- }
172
- function mt(n) {
173
- if (!n) return { owner: null, repo: null };
174
- const r = n.match(/github\.com[:/]([^/]+)\/([^/]+?)(?:\.git)?\/?$/);
175
- return r ? { owner: r[1], repo: r[2] } : { owner: null, repo: null };
176
- }
177
- function ht(n) {
178
- return new Promise((r, o) => {
179
- const t = [];
180
- n.on("data", (c) => t.push(c)), n.on("end", () => r(Buffer.concat(t))), n.on("error", o);
181
- });
182
- }
183
- function yt(n) {
184
- return new Promise((r) => {
185
- let o = "";
186
- n.on("data", (t) => {
187
- o += t;
188
- }), n.on("end", () => {
189
- try {
190
- r(o ? JSON.parse(o) : null);
191
- } catch {
192
- r(null);
193
- }
194
- }), n.on("error", () => r(null));
195
- });
196
- }
197
- function A(n, r, o) {
198
- const t = "\0okno:";
199
- let c;
200
- if (r.startsWith("pages/") ? c = t + "pages" : r.startsWith("collections/") ? c = t + "collections" : r.startsWith("globals/") && (c = t + "globals"), c) {
201
- const m = n.moduleGraph.getModuleById(c);
202
- m && n.moduleGraph.invalidateModule(m);
203
- const e = n.moduleGraph.getModulesByFile(o);
204
- e && e.forEach((i) => {
205
- n.moduleGraph.invalidateModule(i);
206
- });
207
- }
208
- }
209
- function wt(n, r) {
210
- const o = (n.split("?")[0] ?? "").replace(/\\/g, "/"), t = r.replace(/\\/g, "/");
211
- return o === t || o.startsWith(t + "/");
212
- }
213
- function bt(n, r) {
214
- for (const [o, t] of Object.entries(r)) {
215
- if (n === o) return t;
216
- if (n.startsWith(o + "/")) return t + n.slice(o.length);
217
- }
218
- return null;
219
- }
220
- const St = "okno:", _ = "\0okno:", _t = /* @__PURE__ */ new Set(["pages", "collections", "globals", "locales", "strings", "content"]), K = T(Q(import.meta.url)), Ot = JSON.parse(U(g(K, "../package.json"), "utf-8")).version, L = x(), b = g(K, "../editor"), j = g(b, "index.ts"), kt = O(j), vt = {
221
- "@modules": g(b, "modules"),
222
- "@lib": g(b, "lib"),
223
- "@logger": g(b, "lib/logger.svelte.ts"),
224
- "@utils": g(b, "lib/utils.ts"),
225
- "@/types": g(b, "lib/types.ts"),
226
- "@styles": g(b, "styles"),
227
- "@components": g(b, "components"),
228
- "@assets": g(b, "assets"),
229
- "@pages": g(b, "pages"),
230
- "@controllers": g(b, "controllers")
231
- };
232
- function Tt(n = {}) {
233
- const r = n.dir ?? "okno";
234
- let o, t, c = !1, m = !1;
235
- return n.isEditor, {
236
- name: "okno",
237
- enforce: "pre",
238
- config(e, i) {
239
- const s = i.command === "serve";
240
- return m = i.command === "build", c = s && kt && !n.builtEditor, {
241
- define: {
242
- __OKNO_API_URL__: JSON.stringify(n.apiUrl ?? "https://api.okno.build"),
243
- __OKNO_VERSION__: JSON.stringify(Ot),
244
- // True when the consumer is running `vite dev` / `astro dev` /
245
- // `sveltekit dev` — anything that serves locally. Replaced at
246
- // consumer build time so the prebuilt editor bundle can honor
247
- // the consumer's actual environment.
248
- __OKNO_IS_DEV__: JSON.stringify(s),
249
- // Public demo build: editor boots login-less, reads a static
250
- // `okno-demo.json` snapshot, no-ops writes. See docs/demo-mode.md.
251
- __OKNO_DEMO__: JSON.stringify(n.demo ?? !1),
252
- // Repo identity (`owner/repo@branch`) baked from the app's git
253
- // origin at the consumer's build time, so the deployed editor
254
- // auto-targets its repo with no picker. null outside a git
255
- // checkout → editor falls back to the picker. Dev ignores it
256
- // (runtime /__okno/repo derivation is more current).
257
- __OKNO_REPO__: JSON.stringify(at()),
258
- // Monorepo root path: the build dir relative to the git root (e.g.
259
- // "apps/site", or "" at the repo root). Sent as X-Okno-Path so the
260
- // hosted backend reads/writes content under `<root>/okno` instead of
261
- // the repo-root `okno` — one repo, many okno projects keyed (repo, path).
262
- __OKNO_ROOT__: JSON.stringify(st())
263
- },
264
- // Source mode (monorepo dev only): skip prebundling so saves to
265
- // `package/editor/**` propagate via HMR instead of needing
266
- // `bun run build:pkg`. The editor's internal @-aliases are NOT exposed
267
- // as a global `resolve.alias` here — that shadowed a consumer's own
268
- // identically-named tsconfig aliases (`@components`, `@lib`, …). They
269
- // are instead resolved per-importer in `resolveId`, scoped to imports
270
- // originating inside the editor source tree. See editor-aliases.ts.
271
- ...c && {
272
- optimizeDeps: { exclude: ["okno", "okno/editor"] }
273
- },
274
- server: {
275
- watch: {
276
- // Only ignore binary assets and editor-only metadata. Page/
277
- // collection/global .ts files SHOULD trigger Vite HMR — that's
278
- // how users see updates when they `git pull` content from the
279
- // drafts branch into their working tree. (Previously this list
280
- // also blocked pages/collections/globals because in DEV_MODE
281
- // the backend mirrored saves to disk, causing spurious full
282
- // reloads. In production-mode the backend never touches local
283
- // FS so blocking those is pure harm — pulls go unseen.)
284
- ignored: [`**/${r}/files/**`, `**/${r}/.okno-tree.json`]
285
- },
286
- // Source mode: the editor loads via a dynamic `import("okno/editor")`
287
- // from a separate package over `@fs`, so Vite's startup dep scan
288
- // never crawls it — its transitive deps (bits-ui, tiptap, runed…)
289
- // are discovered lazily on first import, triggering a mid-session
290
- // re-optimize that 404s the now-stale prebundled chunks. Warming the
291
- // editor entry forces that crawl at startup, so those deps land in
292
- // the first optimize pass and never re-bundle under the user.
293
- ...c && {
294
- warmup: { clientFiles: [j] }
295
- }
296
- }
297
- };
298
- },
299
- configResolved(e) {
300
- o = e.root, t = g(o, r);
301
- },
302
- async buildStart() {
303
- if (O(t)) {
304
- if (await P(t, o), m) {
305
- const e = y(t, "files");
306
- if (O(e)) {
307
- const i = O(y(o, "static")) ? y(o, "static") : y(o, "public"), s = y(i, "okno", "files");
308
- g(s) !== g(e) && (M(s, { recursive: !0 }), Y(e, s, {
309
- recursive: !0,
310
- filter: (f) => !f.endsWith(".okno-tree.json")
311
- }));
312
- }
313
- }
314
- if (n.demo) {
315
- const e = y(o, "public");
316
- O(e) || M(e, { recursive: !0 }), I(
317
- y(e, "okno-demo.json"),
318
- JSON.stringify(rt(t))
319
- );
320
- }
321
- }
322
- },
323
- configureServer(e) {
324
- gt(e, t);
325
- const i = (f) => {
326
- const a = N(t, f);
327
- if (a.startsWith("..") || !a.endsWith(".ts")) return;
328
- const u = a.split(/[\\/]/)[0];
329
- if (u !== "pages" && u !== "collections" && u !== "globals") return;
330
- const l = e.moduleGraph.getModuleById(_ + u);
331
- l && e.moduleGraph.invalidateModule(l), e.moduleGraph.getModulesByFile(f)?.forEach((h) => e.moduleGraph.invalidateModule(h)), !$(f) && e.ws.send({ type: "full-reload" });
332
- };
333
- e.watcher.on("add", i), e.watcher.on("unlink", i), e.middlewares.use("/__okno/imports", async (f, a, u) => {
334
- if (f.method !== "GET") return u();
335
- try {
336
- const [l, h, p] = await Promise.all([
337
- E(o, "pages"),
338
- E(o, "globals"),
339
- E(o, "collections")
340
- ]);
341
- a.setHeader("Content-Type", "application/json"), a.setHeader("Cache-Control", "no-store"), a.end(JSON.stringify({
342
- pages: Array.from(l),
343
- globals: Array.from(h),
344
- collections: Array.from(p)
345
- }));
346
- } catch (l) {
347
- u(l);
348
- }
349
- });
350
- const s = "/__okno_bootstrap.js";
351
- e.middlewares.use(s, async (f, a, u) => {
352
- try {
353
- const l = await e.transformRequest("\0okno:bootstrap");
354
- if (!l) {
355
- a.statusCode = 500, a.end("okno bootstrap transform failed");
356
- return;
357
- }
358
- a.setHeader("content-type", "text/javascript"), a.end(l.code);
359
- } catch (l) {
360
- u(l);
361
- }
362
- }), e.middlewares.use((f, a, u) => {
363
- const l = "<script>window.__oknoDev=true<\/script>", h = n.demo ? "<script>window.__oknoDemo=true<\/script>" : "", p = `${l}${h}<script type="module" src="${s}"><\/script>`, w = [];
364
- let k = null;
365
- const V = a.write.bind(a), B = a.end.bind(a), X = a.setHeader.bind(a), G = () => {
366
- const d = a.getHeader("content-type");
367
- return typeof d == "string" && d.includes("text/html");
368
- }, W = (d, S) => Buffer.isBuffer(d) ? d : d instanceof Uint8Array ? Buffer.from(d.buffer, d.byteOffset, d.byteLength) : typeof d == "string" ? Buffer.from(d, S || "utf-8") : Buffer.from(String(d));
369
- a.write = (d, S, ...C) => (k === null && (k = G()), k ? (d && w.push(W(d, typeof S == "string" ? S : void 0)), !0) : V(d, S, ...C)), a.end = (d, S, ...C) => {
370
- if (k === null && (k = G()), !k) return B(d, S, ...C);
371
- d && w.push(W(d, typeof S == "string" ? S : void 0));
372
- let v = Buffer.concat(w).toString("utf-8");
373
- v.includes(p) || (v = v.includes("</body>") ? v.replace("</body>", `${p}</body>`) : v + p);
374
- const J = Buffer.from(v, "utf-8");
375
- if (!a.headersSent)
376
- try {
377
- X("content-length", String(J.length));
378
- } catch {
379
- }
380
- return B(J, ...C);
381
- }, u();
382
- });
383
- },
384
- async handleHotUpdate(e) {
385
- const i = y(t, "schema.ts"), s = y(t, "users.json"), f = y(t, "permissions.ts"), a = y(t, "modules.d.ts");
386
- if (e.file === i) {
387
- await P(t, o);
388
- for (const p of ["pages", "collections", "globals", "locales", "content", "strings"]) {
389
- const w = e.server.moduleGraph.getModuleById(_ + p);
390
- w && e.server.moduleGraph.invalidateModule(w);
391
- }
392
- return [];
393
- }
394
- if (e.file === s || e.file === f || e.file === a)
395
- return [];
396
- const u = y(t, "strings.ts"), l = N(y(t, "strings"), e.file);
397
- if (e.file === u || !l.startsWith("..") && l.endsWith(".ts")) {
398
- await P(t, o);
399
- for (const p of ["strings", "content"]) {
400
- const w = e.server.moduleGraph.getModuleById(_ + p);
401
- w && e.server.moduleGraph.invalidateModule(w);
402
- }
403
- }
404
- const h = N(t, e.file);
405
- if (!h.startsWith("..") && h.endsWith(".ts")) {
406
- const p = h.split(/[\\/]/)[0];
407
- if ((p === "pages" || p === "collections" || p === "globals") && $(e.file)) {
408
- const w = e.server.moduleGraph.getModuleById(_ + p);
409
- return w && e.server.moduleGraph.invalidateModule(w), [];
410
- }
411
- }
412
- },
413
- // PRODUCTION boot for index.html frameworks (plain Vite, Solid, …). In dev
414
- // the `configureServer` middleware injects the editor; that middleware never
415
- // runs in a build, so a deployed page would ship no editor. `transformIndexHtml`
416
- // fires only for frameworks whose HTML goes through Vite's index.html pipeline
417
- // — Astro/SvelteKit/Nuxt own their HTML and bypass this hook, so they wire boot
418
- // their own way (an Astro integration, a +layout import, a Nuxt client plugin).
419
- // The inline `import "okno/boot"` is a real module script, so Vite bundles it +
420
- // code-splits the editor behind boot's press-`e`/`?okno` gate.
421
- transformIndexHtml: {
422
- // `pre`, so the inline module script is present BEFORE Vite's core HTML
423
- // plugin collects + bundles page scripts — otherwise the bare
424
- // `import "okno/boot"` survives unbundled and 404s in the browser.
425
- order: "pre",
426
- handler(e) {
427
- if (m)
428
- return {
429
- html: e,
430
- tags: [
431
- {
432
- tag: "script",
433
- attrs: { type: "module" },
434
- children: 'import "okno/boot"',
435
- injectTo: "body"
436
- }
437
- ]
438
- };
439
- }
440
- },
441
- async resolveId(e, i) {
442
- if (e.startsWith(St))
443
- return "\0" + e;
444
- if (e.startsWith("okno/")) {
445
- const s = e.slice(5);
446
- if (_t.has(s)) return _ + s;
447
- }
448
- if (c && e === "okno/editor")
449
- return j;
450
- if (c && i && wt(i, b)) {
451
- const s = bt(e, vt);
452
- if (s)
453
- return (await this.resolve(s, i, { skipSelf: !0 }))?.id;
454
- }
455
- },
456
- async load(e) {
457
- if (!e.startsWith(_)) return;
458
- const i = e.slice(_.length);
459
- if (i === "runtime") {
460
- if (!L)
461
- throw new Error("okno: runtime/wrap.js missing — run `bun run build:pkg`");
462
- return L;
463
- }
464
- if (i === "bootstrap")
465
- return 'import("okno/editor")';
466
- if (i === "pages") {
467
- const s = await E(o, "pages");
468
- return await q(t, s);
469
- }
470
- if (i === "collections") {
471
- const s = await E(o, "collections");
472
- return await tt(t, s, m);
473
- }
474
- if (i === "globals") {
475
- const s = await E(o, "globals");
476
- return await et(t, s);
477
- }
478
- if (i === "locales")
479
- return ot(t);
480
- if (i === "strings")
481
- return await nt(t);
482
- if (i === "content")
483
- return it();
484
- }
485
- };
486
- }
487
- export {
488
- Tt as o
489
- };