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