okno 1.0.0-beta.38 → 1.0.0-beta.39
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/dist/astro/index.d.ts +0 -8
- package/dist/astro/index.d.ts.map +1 -1
- package/dist/astro/index.js +6 -18
- package/dist/codegen/modules.d.ts +0 -16
- package/dist/codegen/modules.d.ts.map +1 -1
- package/dist/{dispatch-oBWbJ5Pl.js → dispatch-BifB_3GO.js} +181 -193
- package/dist/editor/index.js +14 -3
- package/dist/index.js +1 -1
- package/dist/next/index.js +1 -1
- package/dist/plugin-BdiBIiAv.js +489 -0
- package/dist/types/schema.d.ts +0 -5
- package/dist/types/schema.d.ts.map +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/vite/plugin.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/codegen/modules-instant.d.ts +0 -21
- package/dist/codegen/modules-instant.d.ts.map +0 -1
- package/dist/plugin-Bgk7L4b7.js +0 -575
- package/dist/runtime/instant.d.ts +0 -55
- package/dist/runtime/instant.d.ts.map +0 -1
- package/dist/runtime/instant.js +0 -146
- package/src/runtime/instant.test.ts +0 -113
- package/src/runtime/instant.ts +0 -287
package/dist/runtime/instant.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage as p } from "node:async_hooks";
|
|
2
|
-
const _ = new p();
|
|
3
|
-
function j(t, e) {
|
|
4
|
-
return _.run(t, e);
|
|
5
|
-
}
|
|
6
|
-
const g = () => _.getStore() ?? null;
|
|
7
|
-
let a = null;
|
|
8
|
-
const z = 3e3;
|
|
9
|
-
async function D(t) {
|
|
10
|
-
if (a?.url === t && Date.now() - a.at < z) return a.snap;
|
|
11
|
-
try {
|
|
12
|
-
const e = {};
|
|
13
|
-
a?.url === t && a.etag && (e["If-None-Match"] = a.etag);
|
|
14
|
-
const o = await fetch(t, { headers: e });
|
|
15
|
-
if (o.status === 304 && a?.url === t)
|
|
16
|
-
return a.at = Date.now(), a.snap;
|
|
17
|
-
if (!o.ok) throw new Error(`snapshot fetch: ${o.status}`);
|
|
18
|
-
const n = await o.json();
|
|
19
|
-
return a = { url: t, etag: o.headers.get("etag"), snap: n, at: Date.now() }, n;
|
|
20
|
-
} catch (e) {
|
|
21
|
-
return console.error("[okno] instant:", e instanceof Error ? e.message : e), a?.url === t ? a.snap : null;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
const y = /* @__PURE__ */ new WeakMap();
|
|
25
|
-
function f(t, e, o) {
|
|
26
|
-
let n = y.get(t);
|
|
27
|
-
return n || y.set(t, n = /* @__PURE__ */ new Map()), n.has(e) || n.set(e, o()), n.get(e);
|
|
28
|
-
}
|
|
29
|
-
const O = /^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|[0-9]{3}))?$/i;
|
|
30
|
-
function d(t) {
|
|
31
|
-
const e = t.schema?.locales?.supported;
|
|
32
|
-
if (!Array.isArray(e) || e.length === 0) return null;
|
|
33
|
-
const o = e.map((n) => typeof n == "string" ? n : n?.id).filter((n) => typeof n == "string");
|
|
34
|
-
return o.length ? new Set(o) : null;
|
|
35
|
-
}
|
|
36
|
-
const $ = (t, e) => e ? e.has(t) : O.test(t);
|
|
37
|
-
function L(t, e) {
|
|
38
|
-
return f(t, `locales:${e}`, () => {
|
|
39
|
-
const o = d(t), n = /* @__PURE__ */ new Set();
|
|
40
|
-
for (const r of Object.keys(t.entries)) {
|
|
41
|
-
const s = r.split("/");
|
|
42
|
-
s[0] === e && s.length === 3 && $(s[1], o) && n.add(s[1]);
|
|
43
|
-
}
|
|
44
|
-
return [...n];
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
function R(t) {
|
|
48
|
-
return f(t, "locales:collections", () => {
|
|
49
|
-
const e = /* @__PURE__ */ new Set();
|
|
50
|
-
for (const o of Object.values(t.collections))
|
|
51
|
-
for (const n of o) for (const r of Object.keys(n.locales ?? {})) e.add(r);
|
|
52
|
-
return [...e];
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function b(t, e) {
|
|
56
|
-
return f(t, "ns", () => {
|
|
57
|
-
const o = {};
|
|
58
|
-
for (const [n, r] of Object.entries(t.collections))
|
|
59
|
-
o[n] = r.map((s) => s.locales ? e.localize(s.record, s.locales) : s.record);
|
|
60
|
-
return o;
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
function E(t, e, o, n) {
|
|
64
|
-
const r = t.entries[`${e}/${o}`], s = L(t, e);
|
|
65
|
-
if (r === void 0) return { data: null, locales: s };
|
|
66
|
-
if (s.length === 0) return { data: r, locales: s };
|
|
67
|
-
const c = {};
|
|
68
|
-
for (const u of s) {
|
|
69
|
-
const i = t.entries[`${e}/${u}/${o}`];
|
|
70
|
-
i !== void 0 && (c[u] = i);
|
|
71
|
-
}
|
|
72
|
-
return { data: n.localize(r, c), locales: s };
|
|
73
|
-
}
|
|
74
|
-
function v(t, e, o, n, r) {
|
|
75
|
-
return f(t, `${e}:${o}`, () => {
|
|
76
|
-
const { data: s, locales: c } = E(t, e, o, r);
|
|
77
|
-
if (s === null) return r.wrap(null, o);
|
|
78
|
-
const u = n ? r.resolveEnums(r.resolveRefs(s, n, b(t, r), c), n, c) : s;
|
|
79
|
-
return r.wrap(u, o);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
function I(t, e, o, n, r) {
|
|
83
|
-
return f(t, `collection:${e}`, () => {
|
|
84
|
-
const s = t.collections[e] ?? [], c = R(t), u = b(t, r);
|
|
85
|
-
return s.filter(
|
|
86
|
-
(l) => (!n.drafts || l.record.status !== "draft") && (!n.scheduling || r.visible(l.record))
|
|
87
|
-
).map((l, m) => {
|
|
88
|
-
const h = l.locales ? r.localize(l.record, l.locales) : l.record, k = o ? r.resolveEnums(r.resolveRefs(h, o, u, c), o, c) : h;
|
|
89
|
-
return r.wrap(k, `${e}.${l.slug}`, `${e}.${m}`);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
function w(t, e) {
|
|
94
|
-
return new Proxy(e, {
|
|
95
|
-
get(o, n) {
|
|
96
|
-
const r = t(), s = Reflect.get(r, n);
|
|
97
|
-
return typeof s == "function" ? s.bind(r) : s;
|
|
98
|
-
},
|
|
99
|
-
has: (o, n) => Reflect.has(t(), n),
|
|
100
|
-
ownKeys: () => Reflect.ownKeys(t()),
|
|
101
|
-
getOwnPropertyDescriptor(o, n) {
|
|
102
|
-
const r = Reflect.getOwnPropertyDescriptor(t(), n);
|
|
103
|
-
return r && { ...r, configurable: !0 };
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
function x(t, e, o) {
|
|
108
|
-
return w(() => {
|
|
109
|
-
const n = g();
|
|
110
|
-
return n ? v(n, "pages", t, e, o) : o.wrap(null, t);
|
|
111
|
-
}, {});
|
|
112
|
-
}
|
|
113
|
-
function A(t, e, o) {
|
|
114
|
-
return w(() => {
|
|
115
|
-
const n = g();
|
|
116
|
-
return n ? v(n, "globals", t, e, o) : o.wrap(null, t);
|
|
117
|
-
}, {});
|
|
118
|
-
}
|
|
119
|
-
function M(t, e, o, n) {
|
|
120
|
-
return w(() => {
|
|
121
|
-
const r = g();
|
|
122
|
-
return r ? I(r, t, e, o, n) : [];
|
|
123
|
-
}, []);
|
|
124
|
-
}
|
|
125
|
-
function P(t, e, o) {
|
|
126
|
-
return w(() => {
|
|
127
|
-
const n = g();
|
|
128
|
-
return n ? f(n, "strings", () => {
|
|
129
|
-
const r = o.expand(n.entries.strings ?? {}), s = d(n), c = {};
|
|
130
|
-
for (const i of Object.keys(n.entries)) {
|
|
131
|
-
const l = i.match(/^strings\/(.+)$/);
|
|
132
|
-
l && $(l[1], s) && (c[l[1]] = o.expand(n.entries[i]));
|
|
133
|
-
}
|
|
134
|
-
const u = Object.keys(c).length ? o.localize(r, c) : r;
|
|
135
|
-
return o.make(u, t, e);
|
|
136
|
-
}) : o.make({}, t, e);
|
|
137
|
-
}, {});
|
|
138
|
-
}
|
|
139
|
-
export {
|
|
140
|
-
M as __oknoInstantCollection,
|
|
141
|
-
A as __oknoInstantGlobal,
|
|
142
|
-
D as __oknoInstantLoad,
|
|
143
|
-
x as __oknoInstantPage,
|
|
144
|
-
j as __oknoInstantRun,
|
|
145
|
-
P as __oknoInstantStrings
|
|
146
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test"
|
|
2
|
-
import {
|
|
3
|
-
__oknoInstantRun,
|
|
4
|
-
__oknoInstantPage,
|
|
5
|
-
__oknoInstantCollection,
|
|
6
|
-
__oknoInstantGlobal,
|
|
7
|
-
type InstantSnapshot,
|
|
8
|
-
type InstantDeps,
|
|
9
|
-
} from "./instant"
|
|
10
|
-
|
|
11
|
-
// Identity-ish deps: enough to verify the materialization pipeline + proxy
|
|
12
|
-
// behavior without dragging the full wrap runtime in. `wrap` tags its inputs
|
|
13
|
-
// so tests can see prefix/drag wiring.
|
|
14
|
-
const deps: InstantDeps = {
|
|
15
|
-
wrap: (data, prefix, drag) => (data === null ? { __empty: true, prefix } : { ...(data as object), prefix, drag }),
|
|
16
|
-
localize: (base, overrides) => ({ ...(base as object), __locales: Object.keys(overrides) }),
|
|
17
|
-
resolveRefs: (data) => data,
|
|
18
|
-
resolveEnums: (data) => data,
|
|
19
|
-
visible: (raw) => {
|
|
20
|
-
const p = (raw as { publishAt?: string }).publishAt
|
|
21
|
-
return !p || Date.parse(p) <= Date.now()
|
|
22
|
-
},
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function snap(partial: Partial<InstantSnapshot>): InstantSnapshot {
|
|
26
|
-
return { v: 1, version: "sha1", schema: null, entries: {}, collections: {}, ...partial }
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
describe("instant runtime", () => {
|
|
30
|
-
test("page proxy reads the request's snapshot, not module init state", () => {
|
|
31
|
-
const home = __oknoInstantPage("home", undefined, deps)
|
|
32
|
-
const a = snap({ entries: { "pages/home": { title: "A" } } })
|
|
33
|
-
const b = snap({ entries: { "pages/home": { title: "B" } } })
|
|
34
|
-
expect(__oknoInstantRun(a, () => (home as any).title)).toBe("A")
|
|
35
|
-
expect(__oknoInstantRun(b, () => (home as any).title)).toBe("B")
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test("outside a request context the page renders as empty placeholder", () => {
|
|
39
|
-
const home = __oknoInstantPage("home", undefined, deps)
|
|
40
|
-
expect((home as any).__empty).toBe(true)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
test("spread and JSON.stringify delegate through the proxy", () => {
|
|
44
|
-
const home = __oknoInstantPage("home", undefined, deps)
|
|
45
|
-
const s = snap({ entries: { "pages/home": { title: "Hi", sub: "There" } } })
|
|
46
|
-
__oknoInstantRun(s, () => {
|
|
47
|
-
expect({ ...(home as any) }).toEqual({ title: "Hi", sub: "There", prefix: "home", drag: undefined })
|
|
48
|
-
expect(JSON.parse(JSON.stringify(home)).title).toBe("Hi")
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test("collections iterate, map and carry slug/drag prefixes", () => {
|
|
53
|
-
const articles = __oknoInstantCollection("articles", undefined, { drafts: true, scheduling: true }, deps)
|
|
54
|
-
const s = snap({
|
|
55
|
-
collections: {
|
|
56
|
-
articles: [
|
|
57
|
-
{ slug: "one", record: { name: "One" } },
|
|
58
|
-
{ slug: "two", record: { name: "Two" } },
|
|
59
|
-
],
|
|
60
|
-
},
|
|
61
|
-
})
|
|
62
|
-
__oknoInstantRun(s, () => {
|
|
63
|
-
expect(articles.length).toBe(2)
|
|
64
|
-
expect(articles.map((a: any) => a.name)).toEqual(["One", "Two"])
|
|
65
|
-
expect([...articles][1].prefix).toBe("articles.two")
|
|
66
|
-
expect([...articles][1].drag).toBe("articles.1")
|
|
67
|
-
})
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
test("runtime re-filters drafts and scheduling per request", () => {
|
|
71
|
-
const articles = __oknoInstantCollection("articles", undefined, { drafts: true, scheduling: true }, deps)
|
|
72
|
-
const s = snap({
|
|
73
|
-
collections: {
|
|
74
|
-
articles: [
|
|
75
|
-
{ slug: "live", record: { name: "Live" } },
|
|
76
|
-
{ slug: "wip", record: { name: "WIP", status: "draft" } },
|
|
77
|
-
{ slug: "future", record: { name: "Future", publishAt: "2999-01-01T00:00:00Z" } },
|
|
78
|
-
],
|
|
79
|
-
},
|
|
80
|
-
})
|
|
81
|
-
__oknoInstantRun(s, () => {
|
|
82
|
-
expect(articles.map((a: any) => a.name)).toEqual(["Live"])
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test("locale folders derive from snapshot entries and merge overrides", () => {
|
|
87
|
-
const home = __oknoInstantPage("home", undefined, deps)
|
|
88
|
-
const s = snap({
|
|
89
|
-
schema: { locales: { supported: [{ id: "en" }, { id: "cs" }] } },
|
|
90
|
-
entries: {
|
|
91
|
-
"pages/home": { title: "Hi" },
|
|
92
|
-
"pages/cs/home": { title: "Ahoj" },
|
|
93
|
-
},
|
|
94
|
-
})
|
|
95
|
-
__oknoInstantRun(s, () => {
|
|
96
|
-
expect((home as any).__locales).toEqual(["cs"])
|
|
97
|
-
})
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
test("globals resolve from their own prefix", () => {
|
|
101
|
-
const theme = __oknoInstantGlobal("theme", undefined, deps)
|
|
102
|
-
const s = snap({ entries: { "globals/theme": { hue: 200 } } })
|
|
103
|
-
expect(__oknoInstantRun(s, () => (theme as any).hue)).toBe(200)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
test("same snapshot object materializes once (memoized identity)", () => {
|
|
107
|
-
const home = __oknoInstantPage("home", undefined, deps)
|
|
108
|
-
const s = snap({ entries: { "pages/home": { title: "Hi" } } })
|
|
109
|
-
const first = __oknoInstantRun(s, () => ({ ...(home as any) }))
|
|
110
|
-
const second = __oknoInstantRun(s, () => ({ ...(home as any) }))
|
|
111
|
-
expect(first).toEqual(second)
|
|
112
|
-
})
|
|
113
|
-
})
|
package/src/runtime/instant.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Instant-mode runtime — the body of the `okno:instant` virtual module.
|
|
3
|
-
*
|
|
4
|
-
* In instant mode content is NOT baked into the build: the generated
|
|
5
|
-
* `okno:pages` / `okno:globals` / `okno:collections` / `okno:strings` modules
|
|
6
|
-
* export lazy proxies from this file that read the CURRENT request's snapshot
|
|
7
|
-
* (put in AsyncLocalStorage by the injected middleware) and run the exact
|
|
8
|
-
* same hydration pipeline the baked modules would — localize → resolve refs +
|
|
9
|
-
* enums → wrap. Same public API, different content source. See docs/instant.md.
|
|
10
|
-
*
|
|
11
|
-
* Module-state discipline: `okno:runtime` (wrap.js) holds shared state (image
|
|
12
|
-
* map, routes), so this module never imports it — the generated modules import
|
|
13
|
-
* both and pass the wrap functions in as `deps`. One runtime instance, no
|
|
14
|
-
* duplicated state.
|
|
15
|
-
*
|
|
16
|
-
* Fail-soft: no snapshot (first request ever + fetch down) → accessors render
|
|
17
|
-
* as empty placeholders (`wrap(null)`), never a crash. Last-good snapshot is
|
|
18
|
-
* kept across fetch failures.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import { AsyncLocalStorage } from "node:async_hooks"
|
|
22
|
-
|
|
23
|
-
type Rec = Record<string, unknown>
|
|
24
|
-
export type InstantSnapshot = {
|
|
25
|
-
v: 1
|
|
26
|
-
version: string
|
|
27
|
-
schema: Rec | null
|
|
28
|
-
entries: Record<string, Rec>
|
|
29
|
-
collections: Record<string, { slug: string; record: Rec; locales?: Record<string, Rec> }[]>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type InstantDeps = {
|
|
33
|
-
wrap: (data: unknown, prefix: string, drag?: string) => any
|
|
34
|
-
localize: (base: unknown, overrides: Record<string, unknown>) => any
|
|
35
|
-
resolveRefs: (data: unknown, fields: unknown, ns: unknown, locales: string[]) => any
|
|
36
|
-
resolveEnums: (data: unknown, fields: unknown, locales: string[]) => any
|
|
37
|
-
visible: (raw: unknown, now?: number) => boolean
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// ----------------------------------------------------------------------------
|
|
41
|
-
// Request context + snapshot fetching
|
|
42
|
-
// ----------------------------------------------------------------------------
|
|
43
|
-
|
|
44
|
-
const storage = new AsyncLocalStorage<InstantSnapshot>()
|
|
45
|
-
|
|
46
|
-
/** Middleware: run `fn` (the rest of the request) with `snap` as the content. */
|
|
47
|
-
export function __oknoInstantRun<T>(snap: InstantSnapshot, fn: () => T): T {
|
|
48
|
-
return storage.run(snap, fn)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const current = (): InstantSnapshot | null => storage.getStore() ?? null
|
|
52
|
-
|
|
53
|
-
// One in-isolate cache: last parsed snapshot + its etag. The micro-TTL keeps a
|
|
54
|
-
// hot page from refetching per request; If-None-Match keeps revalidation cheap
|
|
55
|
-
// (304 → reuse). A fetch failure serves the last-good snapshot indefinitely —
|
|
56
|
-
// freshness degrades, availability doesn't.
|
|
57
|
-
let cache: { url: string; etag: string | null; snap: InstantSnapshot; at: number } | null = null
|
|
58
|
-
const TTL_MS = 3000
|
|
59
|
-
|
|
60
|
-
export async function __oknoInstantLoad(url: string): Promise<InstantSnapshot | null> {
|
|
61
|
-
if (cache?.url === url && Date.now() - cache.at < TTL_MS) return cache.snap
|
|
62
|
-
try {
|
|
63
|
-
const headers: Record<string, string> = {}
|
|
64
|
-
if (cache?.url === url && cache.etag) headers["If-None-Match"] = cache.etag
|
|
65
|
-
const res = await fetch(url, { headers })
|
|
66
|
-
if (res.status === 304 && cache?.url === url) {
|
|
67
|
-
cache.at = Date.now()
|
|
68
|
-
return cache.snap
|
|
69
|
-
}
|
|
70
|
-
if (!res.ok) throw new Error(`snapshot fetch: ${res.status}`)
|
|
71
|
-
const snap = (await res.json()) as InstantSnapshot
|
|
72
|
-
cache = { url, etag: res.headers.get("etag"), snap, at: Date.now() }
|
|
73
|
-
return snap
|
|
74
|
-
} catch (err) {
|
|
75
|
-
console.error("[okno] instant:", err instanceof Error ? err.message : err)
|
|
76
|
-
return cache?.url === url ? cache.snap : null
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ----------------------------------------------------------------------------
|
|
81
|
-
// Per-snapshot memoization — materialized values are cached per snapshot
|
|
82
|
-
// object, so a request re-renders nothing the previous request (on the same
|
|
83
|
-
// snapshot version) already built.
|
|
84
|
-
// ----------------------------------------------------------------------------
|
|
85
|
-
|
|
86
|
-
const memoStore = new WeakMap<InstantSnapshot, Map<string, unknown>>()
|
|
87
|
-
|
|
88
|
-
function memo<T>(snap: InstantSnapshot, key: string, make: () => T): T {
|
|
89
|
-
let m = memoStore.get(snap)
|
|
90
|
-
if (!m) memoStore.set(snap, (m = new Map()))
|
|
91
|
-
if (!m.has(key)) m.set(key, make())
|
|
92
|
-
return m.get(key) as T
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// ----------------------------------------------------------------------------
|
|
96
|
-
// Locale layout, derived from the snapshot (PARITY: mirrors the locale-folder
|
|
97
|
-
// rules in codegen/modules.ts — configured ids, else BCP-47 shape).
|
|
98
|
-
// ----------------------------------------------------------------------------
|
|
99
|
-
|
|
100
|
-
const LOCALE_FOLDER_RE = /^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|[0-9]{3}))?$/i
|
|
101
|
-
|
|
102
|
-
function localeIds(snap: InstantSnapshot): Set<string> | null {
|
|
103
|
-
const sup = (snap.schema as { locales?: { supported?: Array<string | { id?: string }> } } | null)?.locales
|
|
104
|
-
?.supported
|
|
105
|
-
if (!Array.isArray(sup) || sup.length === 0) return null
|
|
106
|
-
const ids = sup.map((e) => (typeof e === "string" ? e : e?.id)).filter((x): x is string => typeof x === "string")
|
|
107
|
-
return ids.length ? new Set(ids) : null
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const isLocaleFolder = (name: string, ids: Set<string> | null) => (ids ? ids.has(name) : LOCALE_FOLDER_RE.test(name))
|
|
111
|
-
|
|
112
|
-
/** Locale folders present under `pages/` or `globals/` in this snapshot. */
|
|
113
|
-
function kindLocales(snap: InstantSnapshot, kind: "pages" | "globals"): string[] {
|
|
114
|
-
return memo(snap, `locales:${kind}`, () => {
|
|
115
|
-
const ids = localeIds(snap)
|
|
116
|
-
const found = new Set<string>()
|
|
117
|
-
for (const key of Object.keys(snap.entries)) {
|
|
118
|
-
const segs = key.split("/")
|
|
119
|
-
if (segs[0] === kind && segs.length === 3 && isLocaleFolder(segs[1], ids)) found.add(segs[1])
|
|
120
|
-
}
|
|
121
|
-
return [...found]
|
|
122
|
-
})
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** Locale folders that appear on any collection item. */
|
|
126
|
-
function collectionLocales(snap: InstantSnapshot): string[] {
|
|
127
|
-
return memo(snap, "locales:collections", () => {
|
|
128
|
-
const found = new Set<string>()
|
|
129
|
-
for (const items of Object.values(snap.collections)) {
|
|
130
|
-
for (const it of items) for (const l of Object.keys(it.locales ?? {})) found.add(l)
|
|
131
|
-
}
|
|
132
|
-
return [...found]
|
|
133
|
-
})
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// ----------------------------------------------------------------------------
|
|
137
|
-
// Materializers — each mirrors what the baked module's generated code does.
|
|
138
|
-
// ----------------------------------------------------------------------------
|
|
139
|
-
|
|
140
|
-
/** Raw localized records per collection — what reference fields resolve
|
|
141
|
-
* against (the baked module's `__oknoNs`). */
|
|
142
|
-
function namespace(snap: InstantSnapshot, deps: InstantDeps): Record<string, Rec[]> {
|
|
143
|
-
return memo(snap, "ns", () => {
|
|
144
|
-
const ns: Record<string, Rec[]> = {}
|
|
145
|
-
for (const [name, items] of Object.entries(snap.collections)) {
|
|
146
|
-
ns[name] = items.map((it) => (it.locales ? deps.localize(it.record, it.locales) : it.record))
|
|
147
|
-
}
|
|
148
|
-
return ns
|
|
149
|
-
})
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function entryData(
|
|
153
|
-
snap: InstantSnapshot,
|
|
154
|
-
kind: "pages" | "globals",
|
|
155
|
-
name: string,
|
|
156
|
-
deps: InstantDeps,
|
|
157
|
-
): { data: unknown; locales: string[] } {
|
|
158
|
-
const base = snap.entries[`${kind}/${name}`]
|
|
159
|
-
const locales = kindLocales(snap, kind)
|
|
160
|
-
if (base === undefined) return { data: null, locales }
|
|
161
|
-
if (locales.length === 0) return { data: base, locales }
|
|
162
|
-
const overrides: Record<string, unknown> = {}
|
|
163
|
-
for (const l of locales) {
|
|
164
|
-
const o = snap.entries[`${kind}/${l}/${name}`]
|
|
165
|
-
if (o !== undefined) overrides[l] = o
|
|
166
|
-
}
|
|
167
|
-
return { data: deps.localize(base, overrides), locales }
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function materializeEntry(
|
|
171
|
-
snap: InstantSnapshot,
|
|
172
|
-
kind: "pages" | "globals",
|
|
173
|
-
name: string,
|
|
174
|
-
fields: unknown,
|
|
175
|
-
deps: InstantDeps,
|
|
176
|
-
): any {
|
|
177
|
-
return memo(snap, `${kind}:${name}`, () => {
|
|
178
|
-
const { data, locales } = entryData(snap, kind, name, deps)
|
|
179
|
-
if (data === null) return deps.wrap(null, name)
|
|
180
|
-
const resolved = fields
|
|
181
|
-
? deps.resolveEnums(deps.resolveRefs(data, fields, namespace(snap, deps), locales), fields, locales)
|
|
182
|
-
: data
|
|
183
|
-
return deps.wrap(resolved, name)
|
|
184
|
-
})
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function materializeCollection(
|
|
188
|
-
snap: InstantSnapshot,
|
|
189
|
-
name: string,
|
|
190
|
-
fields: unknown,
|
|
191
|
-
flags: { drafts: boolean; scheduling: boolean },
|
|
192
|
-
deps: InstantDeps,
|
|
193
|
-
): any[] {
|
|
194
|
-
return memo(snap, `collection:${name}`, () => {
|
|
195
|
-
const items = snap.collections[name] ?? []
|
|
196
|
-
const locales = collectionLocales(snap)
|
|
197
|
-
const ns = namespace(snap, deps)
|
|
198
|
-
// The backend already filtered at generation time; re-checking here makes
|
|
199
|
-
// an `unpublishAt` that passed since the last publish retire on the exact
|
|
200
|
-
// second (and is free). Predicates read the RAW base record, like the
|
|
201
|
-
// baked module's filter.
|
|
202
|
-
const kept = items.filter(
|
|
203
|
-
(it) =>
|
|
204
|
-
(!flags.drafts || (it.record as { status?: unknown }).status !== "draft") &&
|
|
205
|
-
(!flags.scheduling || deps.visible(it.record)),
|
|
206
|
-
)
|
|
207
|
-
return kept.map((it, i) => {
|
|
208
|
-
const data = it.locales ? deps.localize(it.record, it.locales) : it.record
|
|
209
|
-
const resolved = fields
|
|
210
|
-
? deps.resolveEnums(deps.resolveRefs(data, fields, ns, locales), fields, locales)
|
|
211
|
-
: data
|
|
212
|
-
return deps.wrap(resolved, `${name}.${it.slug}`, `${name}.${i}`)
|
|
213
|
-
})
|
|
214
|
-
})
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// ----------------------------------------------------------------------------
|
|
218
|
-
// Lazy proxies — module-scope exports that re-read the current request's
|
|
219
|
-
// snapshot on every property access. Spread, iteration and JSON.stringify all
|
|
220
|
-
// delegate to the materialized value.
|
|
221
|
-
// ----------------------------------------------------------------------------
|
|
222
|
-
|
|
223
|
-
function lazy(make: () => any, target: object): any {
|
|
224
|
-
return new Proxy(target, {
|
|
225
|
-
get(_t, prop) {
|
|
226
|
-
const v = make()
|
|
227
|
-
const r = Reflect.get(v, prop)
|
|
228
|
-
return typeof r === "function" ? r.bind(v) : r
|
|
229
|
-
},
|
|
230
|
-
has: (_t, prop) => Reflect.has(make(), prop),
|
|
231
|
-
ownKeys: () => Reflect.ownKeys(make()),
|
|
232
|
-
getOwnPropertyDescriptor(_t, prop) {
|
|
233
|
-
const d = Reflect.getOwnPropertyDescriptor(make(), prop)
|
|
234
|
-
return d ? { ...d, configurable: true } : d
|
|
235
|
-
},
|
|
236
|
-
})
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export function __oknoInstantPage(name: string, fields: unknown, deps: InstantDeps): any {
|
|
240
|
-
return lazy(() => {
|
|
241
|
-
const snap = current()
|
|
242
|
-
return snap ? materializeEntry(snap, "pages", name, fields, deps) : deps.wrap(null, name)
|
|
243
|
-
}, {})
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function __oknoInstantGlobal(name: string, fields: unknown, deps: InstantDeps): any {
|
|
247
|
-
return lazy(() => {
|
|
248
|
-
const snap = current()
|
|
249
|
-
return snap ? materializeEntry(snap, "globals", name, fields, deps) : deps.wrap(null, name)
|
|
250
|
-
}, {})
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
export function __oknoInstantCollection(
|
|
254
|
-
name: string,
|
|
255
|
-
fields: unknown,
|
|
256
|
-
flags: { drafts: boolean; scheduling: boolean },
|
|
257
|
-
deps: InstantDeps,
|
|
258
|
-
): any[] {
|
|
259
|
-
return lazy(() => {
|
|
260
|
-
const snap = current()
|
|
261
|
-
return snap ? materializeCollection(snap, name, fields, flags, deps) : []
|
|
262
|
-
}, []) as any[]
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export type InstantStringsDeps = {
|
|
266
|
-
expand: (flat: unknown) => Rec
|
|
267
|
-
localize: (base: unknown, overrides: Record<string, unknown>) => any
|
|
268
|
-
make: (data: unknown, slots: unknown, def: unknown) => any
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export function __oknoInstantStrings(slots: unknown, def: unknown, deps: InstantStringsDeps): any {
|
|
272
|
-
return lazy(() => {
|
|
273
|
-
const snap = current()
|
|
274
|
-
if (!snap) return deps.make({}, slots, def)
|
|
275
|
-
return memo(snap, "strings", () => {
|
|
276
|
-
const base = deps.expand(snap.entries["strings"] ?? {})
|
|
277
|
-
const ids = localeIds(snap)
|
|
278
|
-
const overrides: Record<string, unknown> = {}
|
|
279
|
-
for (const key of Object.keys(snap.entries)) {
|
|
280
|
-
const m = key.match(/^strings\/(.+)$/)
|
|
281
|
-
if (m && isLocaleFolder(m[1], ids)) overrides[m[1]] = deps.expand(snap.entries[key])
|
|
282
|
-
}
|
|
283
|
-
const data = Object.keys(overrides).length ? deps.localize(base, overrides) : base
|
|
284
|
-
return deps.make(data, slots, def)
|
|
285
|
-
})
|
|
286
|
-
}, {})
|
|
287
|
-
}
|