@valbuild/server 0.150.22 → 0.151.0

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +326 -0
  2. package/browser/dist/valbuild-server-browser.cjs.dev.js +5 -137
  3. package/browser/dist/valbuild-server-browser.cjs.prod.js +5 -137
  4. package/browser/dist/valbuild-server-browser.esm.js +4 -140
  5. package/dist/ValRouter-2827d620.esm.js +14489 -0
  6. package/dist/ValRouter-ca467c83.cjs.dev.js +14585 -0
  7. package/dist/ValRouter-d43e0a33.cjs.prod.js +14585 -0
  8. package/dist/{ValRouterCore-3431ce5f.cjs.dev.js → ValRouterCore-0921fe9c.cjs.dev.js} +3156 -1758
  9. package/dist/{ValRouterCore-c5f6885a.esm.js → ValRouterCore-6c29fb75.esm.js} +3147 -1753
  10. package/dist/{ValRouterCore-df3f99c6.cjs.prod.js → ValRouterCore-eb1f53f5.cjs.prod.js} +3156 -1758
  11. package/dist/declarations/src/ValOps.d.ts +314 -68
  12. package/dist/declarations/src/ValOpsFS.d.ts +32 -0
  13. package/dist/declarations/src/ValOpsHttp.d.ts +93 -3
  14. package/dist/declarations/src/ValOpsMemory.d.ts +22 -47
  15. package/dist/declarations/src/ValRouter.d.ts +10 -0
  16. package/dist/declarations/src/ValServer.d.ts +0 -10
  17. package/dist/declarations/src/browser/inTabFetch.d.ts +19 -2
  18. package/dist/declarations/src/draftOverlay.d.ts +51 -0
  19. package/dist/declarations/src/draftReads.d.ts +103 -0
  20. package/dist/declarations/src/head.d.ts +312 -0
  21. package/dist/declarations/src/history/getJsonEntryAtCommit.d.ts +52 -0
  22. package/dist/declarations/src/index.d.ts +1 -0
  23. package/dist/declarations/src/internal/browser/index.d.ts +11 -0
  24. package/dist/declarations/src/internal/index.d.ts +15 -0
  25. package/dist/declarations/src/patch/jsonValuesPatch.d.ts +2 -165
  26. package/dist/declarations/src/patch/ts/jsonValuesModule.d.ts +38 -0
  27. package/dist/declarations/src/patch/ts/loadTypeScriptKit.d.ts +14 -0
  28. package/dist/declarations/src/patch/ts/ops.d.ts +45 -0
  29. package/dist/declarations/src/patch/ts/publishKit.d.ts +26 -0
  30. package/dist/declarations/src/platform.d.ts +14 -0
  31. package/dist/declarations/src/proposal.d.ts +54 -3
  32. package/dist/declarations/src/valRuntime.d.ts +35 -0
  33. package/dist/draftReads-05415ff6.cjs.dev.js +366 -0
  34. package/dist/draftReads-5f99edd3.esm.js +361 -0
  35. package/dist/draftReads-ea3946e0.cjs.prod.js +366 -0
  36. package/dist/inTabFetch-0cd45721.esm.js +176 -0
  37. package/dist/inTabFetch-35cf5698.cjs.prod.js +179 -0
  38. package/dist/inTabFetch-49db354b.cjs.dev.js +179 -0
  39. package/dist/{getJsonEntryAtCommit-b52171b1.cjs.prod.js → publishKit-24e1406b.cjs.prod.js} +55 -6
  40. package/dist/{getJsonEntryAtCommit-21a4bbfa.cjs.dev.js → publishKit-3afe011d.cjs.dev.js} +55 -6
  41. package/dist/{getJsonEntryAtCommit-93f9aba9.esm.js → publishKit-d2e3e44d.esm.js} +48 -5
  42. package/dist/valbuild-server.cjs.dev.js +160 -13319
  43. package/dist/valbuild-server.cjs.prod.js +160 -13319
  44. package/dist/valbuild-server.esm.js +34 -13174
  45. package/internal/browser/dist/valbuild-server-internal-browser.cjs.d.ts +2 -0
  46. package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +19 -0
  47. package/internal/browser/dist/valbuild-server-internal-browser.cjs.js +7 -0
  48. package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +19 -0
  49. package/internal/browser/dist/valbuild-server-internal-browser.esm.js +10 -0
  50. package/internal/browser/package.json +4 -0
  51. package/internal/dist/valbuild-server-internal.cjs.d.ts +2 -0
  52. package/internal/dist/valbuild-server-internal.cjs.dev.js +43 -0
  53. package/internal/dist/valbuild-server-internal.cjs.js +7 -0
  54. package/internal/dist/valbuild-server-internal.cjs.prod.js +43 -0
  55. package/internal/dist/valbuild-server-internal.esm.js +24 -0
  56. package/internal/package.json +4 -0
  57. package/package.json +17 -6
  58. package/dist/jsonValuesModule-235bd751.cjs.dev.js +0 -326
  59. package/dist/jsonValuesModule-23eb6f53.esm.js +0 -310
  60. package/dist/jsonValuesModule-625ebae8.cjs.prod.js +0 -326
  61. package/dist/ops-0e54b388.cjs.prod.js +0 -361
  62. package/dist/ops-9ca2ef2a.cjs.dev.js +0 -361
  63. package/dist/ops-b904007e.esm.js +0 -353
  64. package/dist/publishKit-34f71e50.cjs.dev.js +0 -42
  65. package/dist/publishKit-65e9e564.cjs.prod.js +0 -42
  66. package/dist/publishKit-7441c0fd.esm.js +0 -31
@@ -0,0 +1,361 @@
1
+ import { Internal } from '@valbuild/core';
2
+ import { VAL_SESSION_COOKIE, schemaVersionOf } from '@valbuild/shared/internal';
3
+
4
+ /*
5
+ * The draft a server-side reader reads (`fetchVal` and friends, in
6
+ * `@valbuild/next/rsc` and `@valbuild/tanstack/server`): the HEAD of this
7
+ * process's runtime (`valRuntime`, `ValHead`), read directly with the
8
+ * request's session. One copy, for both frameworks.
9
+ *
10
+ * Before phase 6 of `docs/plans/one-content-model.md` each framework built a
11
+ * private Val server and asked it `PUT /sources/~` -- the whole tree,
12
+ * previewed and validated -- once per request, then `/patches` and `/json`
13
+ * for the entries a draft edits, and `/json` again per key read. Now a read
14
+ * asks the head "has the chain moved?" (`seq`) and is answered from modules
15
+ * made once per version: an unpatched module is the reader's own bundle, and
16
+ * nothing is read for it at all.
17
+ */
18
+
19
+ /** One module {@link readDraftChanges} read: what a reader's `reads` record. */
20
+
21
+ const SERVER_DRAFT = "server-draft";
22
+ const PASS = {
23
+ status: "pass"
24
+ };
25
+
26
+ /**
27
+ * The modules the editor's draft CHANGES, for this request: what
28
+ * `fetchValDraft` hands to `<ValProvider draft>`, in `@valbuild/tanstack/server`
29
+ * and `@valbuild/next/rsc` alike -- this is the whole of the read, and the
30
+ * frameworks add only where draft mode and the cookies come from (and, in
31
+ * TanStack, a proposal's snapshot).
32
+ *
33
+ * `null` -- and no read at all -- unless draft mode is on: a visitor pays one
34
+ * cookie lookup. Otherwise the head's modules a patch touches or a publish
35
+ * wrote and the host has not rebuilt since, with the `.jsonValues()` entries
36
+ * the draft changes in place of their markers, and the version of each (so
37
+ * the page compares versions with what the Studio relays, not content).
38
+ *
39
+ * Also `null` when the session is not one the server accepts, and when the
40
+ * draft could not be read at all (logged): published content, as before this
41
+ * existed -- a draft page that renders the site is a degraded preview, one
42
+ * that fails to render is an outage.
43
+ */
44
+ async function readDraftChanges(opts) {
45
+ try {
46
+ var _await$opts$getCookie;
47
+ if (!(await opts.isEnabled())) {
48
+ return null;
49
+ }
50
+ // Possibly absent: a local `fs` server answers without one. The runtime
51
+ // decides, as the API does.
52
+ const session = (_await$opts$getCookie = (await opts.getCookies()).get(VAL_SESSION_COOKIE)) === null || _await$opts$getCookie === void 0 ? void 0 : _await$opts$getCookie.value;
53
+ const runtime = await opts.runtime;
54
+ if (!runtime.authorize(session)) {
55
+ return null;
56
+ }
57
+ // One chain for the whole answer: asked "has it moved?" once.
58
+ const synced = await runtime.ops.head.sync();
59
+ if ("error" in synced) {
60
+ Internal.log.error("could not read the draft", synced.error.message);
61
+ return null;
62
+ }
63
+ const chain = synced.ok;
64
+ const changed = await runtime.ops.head.changed(chain);
65
+ if ("error" in changed) {
66
+ Internal.log.error("could not read the draft", changed.error.message);
67
+ return null;
68
+ }
69
+ const sources = {};
70
+ const versions = {};
71
+ const reads = [];
72
+ for (const path of changed.ok) {
73
+ var _draft$ok;
74
+ const draft = await runtime.ops.head.draft(path, chain);
75
+ if ("error" in draft) {
76
+ Internal.log.error("could not read the draft", draft.error.message);
77
+ return null;
78
+ }
79
+ // Left out when its entries cannot all be read: the published render
80
+ // that existed before the draft did is worse than this, and never
81
+ // wrong for long.
82
+ if (((_draft$ok = draft.ok) === null || _draft$ok === void 0 ? void 0 : _draft$ok.kind) !== "draft" || draft.ok.entriesFailed) continue;
83
+ sources[path] = draft.ok.source;
84
+ versions[path] = draft.ok.version;
85
+ reads.push({
86
+ path,
87
+ from: SERVER_DRAFT,
88
+ version: draft.ok.version
89
+ });
90
+ }
91
+ return {
92
+ sources,
93
+ versions,
94
+ reads
95
+ };
96
+ } catch (err) {
97
+ Internal.log.error("could not read the draft", err);
98
+ return null;
99
+ }
100
+ }
101
+
102
+ /** A request's runtime, and the one chain it reads at. */
103
+
104
+ /** One thing this source has started to read, or has read. */
105
+
106
+ /**
107
+ * The draft, as a source: this process's head, for this request's session.
108
+ * Created per request. A module whose newest version is the bundle's own
109
+ * passes, so the reader's bundle answers it and nothing is read; any other is
110
+ * the head's (with the `.jsonValues()` entries the draft changes filled in).
111
+ * An entry is the head's, resolved once per version.
112
+ *
113
+ * Every error is a `pass`, logged: a draft that cannot be read is the
114
+ * published content, which a page can render.
115
+ *
116
+ * The request reads ONE chain: the head is asked "has the chain moved?" once,
117
+ * at the first read, and every module and entry after it is read at that
118
+ * chain. In http mode that question is a request to the content service, and
119
+ * a page reads tens of modules and entries; and a page is one answer, never
120
+ * half of one chain and half of the next.
121
+ *
122
+ * `modules: false` (the single-entry readers): every module passes.
123
+ */
124
+ function serverDraftSource(opts) {
125
+ const drafts = new Map();
126
+ const entries = new Map();
127
+ /**
128
+ * The runtime and the chain this request reads, once this session may read
129
+ * the draft (asked once per request); `null` when it may not, or the
130
+ * chain could not be read, which reads as published content.
131
+ */
132
+ let allowed = null;
133
+ const runtime = () => {
134
+ allowed ??= opts.runtime.then(async runtime => {
135
+ if (!runtime.authorize(opts.session)) return null;
136
+ const synced = await runtime.ops.head.sync();
137
+ if ("error" in synced) {
138
+ Internal.log.error("could not read the draft", synced.error);
139
+ return null;
140
+ }
141
+ return {
142
+ runtime,
143
+ chain: synced.ok
144
+ };
145
+ }, err => {
146
+ Internal.log.error("could not start Val's runtime", err);
147
+ return null;
148
+ });
149
+ return allowed;
150
+ };
151
+ return {
152
+ name: SERVER_DRAFT,
153
+ module(path) {
154
+ if (!opts.modules) return PASS;
155
+ let state = drafts.get(path);
156
+ if (state === undefined) {
157
+ const ready = runtime().then(pinned => pinned === null ? null : pinned.runtime.ops.head.draft(path, pinned.chain)).then(read => {
158
+ if (read !== null && "error" in read) {
159
+ Internal.log.error("could not read the draft", read.error);
160
+ }
161
+ drafts.set(path, {
162
+ done: true,
163
+ value: read === null || "error" in read ? null : read.ok
164
+ });
165
+ }, err => {
166
+ Internal.log.error("could not read the draft", err);
167
+ drafts.set(path, {
168
+ done: true,
169
+ value: null
170
+ });
171
+ });
172
+ state = {
173
+ done: false,
174
+ ready
175
+ };
176
+ drafts.set(path, state);
177
+ }
178
+ if (!state.done) return {
179
+ status: "pending",
180
+ ready: state.ready
181
+ };
182
+ const draft = state.value;
183
+ if (draft === null || draft.kind === "bundle") return PASS;
184
+ return {
185
+ status: "ready",
186
+ source: draft.source,
187
+ from: SERVER_DRAFT,
188
+ version: draft.version
189
+ };
190
+ },
191
+ entry(path, key) {
192
+ const id = `${path}\u0000${key}`;
193
+ let state = entries.get(id);
194
+ if (state === undefined) {
195
+ const ready = runtime().then(pinned => pinned === null ? PASS : readEntry(pinned, path, key)).then(read => {
196
+ entries.set(id, {
197
+ done: true,
198
+ value: read
199
+ });
200
+ }, err => {
201
+ Internal.log.error("could not load draft JSON entry: ", err);
202
+ entries.set(id, {
203
+ done: true,
204
+ value: PASS
205
+ });
206
+ });
207
+ state = {
208
+ done: false,
209
+ ready
210
+ };
211
+ entries.set(id, state);
212
+ }
213
+ return state.done ? state.value : {
214
+ status: "pending",
215
+ ready: state.ready
216
+ };
217
+ }
218
+ };
219
+ }
220
+
221
+ /**
222
+ * ONE `.jsonValues()` entry's DRAFT content, from the head. `gone` is
223
+ * authoritative -- the draft has no such entry, removed by a pending patch or
224
+ * never there -- and is not a reason to fall back to the published entry;
225
+ * `pass` means the bundle's entry is the draft's (the record is the bundle's
226
+ * own), or it could not be read.
227
+ */
228
+ async function readEntry({
229
+ runtime,
230
+ chain
231
+ }, path, key) {
232
+ const draft = await runtime.ops.head.draft(path, chain);
233
+ if ("error" in draft) {
234
+ Internal.log.error("could not load draft JSON entry: ", draft.error);
235
+ return PASS;
236
+ }
237
+ if (draft.ok === null) return {
238
+ status: "gone",
239
+ from: SERVER_DRAFT
240
+ };
241
+ if (draft.ok.kind === "bundle") return PASS;
242
+ const read = await runtime.ops.head.entries(path, [key], chain);
243
+ if (read.status === "not-found") return {
244
+ status: "gone",
245
+ from: SERVER_DRAFT
246
+ };
247
+ if (read.status !== "success") {
248
+ Internal.log.error("could not load draft JSON entry: ", read.message);
249
+ return PASS;
250
+ }
251
+ const entry = read.value.entries[0];
252
+ if (entry !== undefined) {
253
+ return {
254
+ status: "ready",
255
+ content: entry.content,
256
+ from: SERVER_DRAFT,
257
+ version: read.value.entryVersions[key] ?? null
258
+ };
259
+ }
260
+ const error = read.value.errors[0];
261
+ if (error !== undefined) {
262
+ Internal.log.error("could not load draft JSON entry: ", error.message);
263
+ return PASS;
264
+ }
265
+ return {
266
+ status: "gone",
267
+ from: SERVER_DRAFT
268
+ };
269
+ }
270
+
271
+ /**
272
+ * Whether this request reads the draft, and the source to read it from.
273
+ *
274
+ * `enabled` is draft mode; it decides edit tags whether or not the draft can
275
+ * be read. The draft is `null` when draft mode is off, or the request's
276
+ * cookies cannot be read (outside a request: published content).
277
+ *
278
+ * `where` names where these readers may be called, for the errors a misuse
279
+ * gets; without it (the single-entry readers) they stay quiet, as they were.
280
+ */
281
+ async function draftForRequest(opts) {
282
+ var _cookies$get;
283
+ const fail = (what, err) => {
284
+ if (opts.where !== undefined) {
285
+ Internal.log.error(`${what} ${opts.where}`, err);
286
+ }
287
+ };
288
+ let enabled = false;
289
+ try {
290
+ enabled = await opts.isEnabled();
291
+ } catch (err) {
292
+ fail("could not check if Val is enabled! This might be due to an error reading draft mode.", err);
293
+ }
294
+ if (!enabled) {
295
+ return {
296
+ enabled,
297
+ draft: null
298
+ };
299
+ }
300
+ let host = null;
301
+ if (opts.modules !== undefined) {
302
+ try {
303
+ const headers = await opts.modules.getHeaders();
304
+ if (typeof headers.get !== "function") {
305
+ throw new Error("Invalid headers");
306
+ }
307
+ host = getHost(headers);
308
+ } catch (err) {
309
+ fail("could not read headers!", err);
310
+ }
311
+ }
312
+ let cookies;
313
+ try {
314
+ cookies = await opts.getCookies();
315
+ } catch (err) {
316
+ fail("could not read cookies!", err);
317
+ return {
318
+ enabled,
319
+ draft: null
320
+ };
321
+ }
322
+ return {
323
+ enabled,
324
+ draft: serverDraftSource({
325
+ runtime: opts.runtime,
326
+ session: (_cookies$get = cookies.get(VAL_SESSION_COOKIE)) === null || _cookies$get === void 0 ? void 0 : _cookies$get.value,
327
+ modules: opts.modules !== undefined && host !== null
328
+ })
329
+ };
330
+ }
331
+ function getHost(headers) {
332
+ var _headers$get;
333
+ const host = headers.get("host");
334
+ let proto = "https";
335
+ if (headers.get("x-forwarded-proto") === "http") {
336
+ proto = "http";
337
+ } else if ((_headers$get = headers.get("referer")) !== null && _headers$get !== void 0 && _headers$get.startsWith("http://")) {
338
+ proto = "http";
339
+ } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
340
+ proto = "http";
341
+ }
342
+ return host ? `${proto}://${host}` : null;
343
+ }
344
+ const schemaVersions = new WeakMap();
345
+
346
+ /**
347
+ * The version of the schema a reader's own code has for a module: what the
348
+ * readers hold a draft's version against (`layered`'s schema rule), so
349
+ * content is never read with a schema it was not made for. Hashed once per
350
+ * module object, which a build makes once: a load cost, not a read cost.
351
+ */
352
+ function readerSchemaVersion(module) {
353
+ const cached = schemaVersions.get(module);
354
+ if (cached !== undefined) return cached;
355
+ const schema = Internal.getSchema(module);
356
+ const version = schema === undefined ? null : schemaVersionOf(schema["executeSerialize"]());
357
+ schemaVersions.set(module, version);
358
+ return version;
359
+ }
360
+
361
+ export { readerSchemaVersion as a, draftForRequest as d, readDraftChanges as r, serverDraftSource as s };