@valbuild/server 0.154.1 → 0.154.3
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 +43 -0
- package/browser/dist/valbuild-server-browser.cjs.dev.js +6 -3
- package/browser/dist/valbuild-server-browser.cjs.prod.js +6 -3
- package/browser/dist/valbuild-server-browser.esm.js +6 -3
- package/dist/{ValRouterCore-cfb35067.cjs.prod.js → ValOpsMemory-18337353.cjs.prod.js} +23 -2013
- package/dist/{ValRouterCore-361673dc.cjs.dev.js → ValOpsMemory-45ba05ad.cjs.dev.js} +23 -2013
- package/dist/{ValRouterCore-be314bc7.esm.js → ValOpsMemory-972616ae.esm.js} +14 -1992
- package/dist/ValPatchStore-2f92eced.cjs.prod.js +1128 -0
- package/dist/ValPatchStore-df8cdbe7.cjs.dev.js +1128 -0
- package/dist/ValPatchStore-e23070df.esm.js +1119 -0
- package/dist/ValRouterCore-5b747abb.cjs.prod.js +612 -0
- package/dist/ValRouterCore-73eb843d.esm.js +608 -0
- package/dist/ValRouterCore-86757d56.cjs.dev.js +612 -0
- package/dist/createPrettierFormatter-05ad7480.cjs.prod.js +692 -0
- package/dist/createPrettierFormatter-1edca494.esm.js +673 -0
- package/dist/createPrettierFormatter-851fcf99.cjs.dev.js +692 -0
- package/dist/createValRuntime-472df6e8.cjs.prod.js +47 -0
- package/dist/createValRuntime-5cebf459.cjs.dev.js +47 -0
- package/dist/createValRuntime-c77ee010.esm.js +45 -0
- package/dist/declarations/src/ValOpsMemory.d.ts +3 -73
- package/dist/declarations/src/ValPatchStore.d.ts +92 -0
- package/dist/declarations/src/ValRouter.d.ts +1 -11
- package/dist/declarations/src/createPrettierFormatter.d.ts +18 -3
- package/dist/declarations/src/createValRuntime.d.ts +12 -0
- package/dist/declarations/src/draftReads.d.ts +11 -3
- package/dist/declarations/src/index.d.ts +4 -2
- package/dist/declarations/src/internal/index.d.ts +28 -6
- package/dist/declarations/src/proposal.d.ts +13 -25
- package/dist/declarations/src/proposalJsonEntries.d.ts +30 -0
- package/dist/declarations/src/serverMode.d.ts +31 -0
- package/dist/{draftReads-0dcddf72.cjs.prod.js → draftReads-3f7dcda5.cjs.prod.js} +10 -2
- package/dist/{draftReads-df44e629.cjs.dev.js → draftReads-a300a02f.cjs.dev.js} +10 -2
- package/dist/{draftReads-1f29aedd.esm.js → draftReads-bca0d49c.esm.js} +10 -2
- package/dist/{inTabFetch-7c8a5628.esm.js → inTabFetch-0c997998.esm.js} +2 -1
- package/dist/{inTabFetch-56226d62.cjs.prod.js → inTabFetch-c2efa1bd.cjs.prod.js} +5 -4
- package/dist/{inTabFetch-5d0314a3.cjs.dev.js → inTabFetch-db6d6425.cjs.dev.js} +5 -4
- package/dist/jsonValuesPatch-13acc674.esm.js +276 -0
- package/dist/jsonValuesPatch-88bc661d.cjs.dev.js +281 -0
- package/dist/jsonValuesPatch-92798812.cjs.prod.js +281 -0
- package/dist/proposalJsonEntries-0669f027.esm.js +480 -0
- package/dist/proposalJsonEntries-26f397bd.cjs.dev.js +500 -0
- package/dist/proposalJsonEntries-9e537244.cjs.prod.js +500 -0
- package/dist/{publishKit-707230bc.cjs.prod.js → publishKit-38d46a51.cjs.dev.js} +20 -22
- package/dist/{publishKit-5a8c9a0d.cjs.dev.js → publishKit-7aca03bb.cjs.prod.js} +20 -22
- package/dist/{publishKit-b850045b.esm.js → publishKit-ac0af189.esm.js} +8 -9
- package/dist/{ValRouter-9505c550.cjs.dev.js → valRuntime-2a3cc887.cjs.prod.js} +158 -6038
- package/dist/{ValRouter-31f89baf.cjs.prod.js → valRuntime-70ee2dc2.cjs.dev.js} +158 -6038
- package/dist/{ValRouter-b7dc784d.esm.js → valRuntime-e4d26702.esm.js} +107 -5937
- package/dist/valbuild-server.cjs.dev.js +5146 -352
- package/dist/valbuild-server.cjs.prod.js +5146 -352
- package/dist/valbuild-server.esm.js +4902 -132
- package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +7 -4
- package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +7 -4
- package/internal/browser/dist/valbuild-server-internal-browser.esm.js +7 -4
- package/internal/dist/valbuild-server-internal.cjs.dev.js +47 -31
- package/internal/dist/valbuild-server-internal.cjs.prod.js +47 -31
- package/internal/dist/valbuild-server-internal.esm.js +38 -22
- package/package.json +4 -4
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fp = require('@valbuild/core/fp');
|
|
4
|
+
require('@valbuild/shared/internal');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `path.posix`, for the four functions the request handlers use.
|
|
8
|
+
*
|
|
9
|
+
* Node's `path` cannot be bundled for a browser, and the request handlers run
|
|
10
|
+
* in one for the demo (`@valbuild/server/browser`). These are Node's own posix
|
|
11
|
+
* algorithms, and `posixPath.test.ts` checks them against `path.posix` itself,
|
|
12
|
+
* so the two cannot differ quietly. Module paths are always posix -- `/app/
|
|
13
|
+
* page.val.ts` on Windows too -- so nothing here needs the platform's `path`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Resolves `.` and `..` segments, as Node's `normalizeString` does. */
|
|
17
|
+
function normalizeSegments(path, allowAboveRoot) {
|
|
18
|
+
let res = "";
|
|
19
|
+
let lastSegmentLength = 0;
|
|
20
|
+
let lastSlash = -1;
|
|
21
|
+
let dots = 0;
|
|
22
|
+
let code = 0;
|
|
23
|
+
for (let i = 0; i <= path.length; ++i) {
|
|
24
|
+
if (i < path.length) {
|
|
25
|
+
code = path.charCodeAt(i);
|
|
26
|
+
} else if (code === 47 /* / */) {
|
|
27
|
+
break;
|
|
28
|
+
} else {
|
|
29
|
+
code = 47;
|
|
30
|
+
}
|
|
31
|
+
if (code === 47) {
|
|
32
|
+
if (lastSlash === i - 1 || dots === 1) ; else if (dots === 2) {
|
|
33
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /* . */ || res.charCodeAt(res.length - 2) !== 46) {
|
|
34
|
+
if (res.length > 2) {
|
|
35
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
36
|
+
if (lastSlashIndex === -1) {
|
|
37
|
+
res = "";
|
|
38
|
+
lastSegmentLength = 0;
|
|
39
|
+
} else {
|
|
40
|
+
res = res.slice(0, lastSlashIndex);
|
|
41
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
42
|
+
}
|
|
43
|
+
lastSlash = i;
|
|
44
|
+
dots = 0;
|
|
45
|
+
continue;
|
|
46
|
+
} else if (res.length !== 0) {
|
|
47
|
+
res = "";
|
|
48
|
+
lastSegmentLength = 0;
|
|
49
|
+
lastSlash = i;
|
|
50
|
+
dots = 0;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (allowAboveRoot) {
|
|
55
|
+
res += res.length > 0 ? "/.." : "..";
|
|
56
|
+
lastSegmentLength = 2;
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
if (res.length > 0) {
|
|
60
|
+
res += "/" + path.slice(lastSlash + 1, i);
|
|
61
|
+
} else {
|
|
62
|
+
res = path.slice(lastSlash + 1, i);
|
|
63
|
+
}
|
|
64
|
+
lastSegmentLength = i - lastSlash - 1;
|
|
65
|
+
}
|
|
66
|
+
lastSlash = i;
|
|
67
|
+
dots = 0;
|
|
68
|
+
} else if (code === 46 && dots !== -1) {
|
|
69
|
+
++dots;
|
|
70
|
+
} else {
|
|
71
|
+
dots = -1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return res;
|
|
75
|
+
}
|
|
76
|
+
function normalize(path) {
|
|
77
|
+
if (path.length === 0) return ".";
|
|
78
|
+
const isAbsolute = path.charCodeAt(0) === 47;
|
|
79
|
+
const trailingSeparator = path.charCodeAt(path.length - 1) === 47;
|
|
80
|
+
let normalized = normalizeSegments(path, !isAbsolute);
|
|
81
|
+
if (normalized.length === 0) {
|
|
82
|
+
if (isAbsolute) return "/";
|
|
83
|
+
return trailingSeparator ? "./" : ".";
|
|
84
|
+
}
|
|
85
|
+
if (trailingSeparator) normalized += "/";
|
|
86
|
+
return isAbsolute ? `/${normalized}` : normalized;
|
|
87
|
+
}
|
|
88
|
+
function join(...paths) {
|
|
89
|
+
const joined = paths.filter(path => path.length > 0).join("/");
|
|
90
|
+
return joined.length === 0 ? "." : normalize(joined);
|
|
91
|
+
}
|
|
92
|
+
function dirname(path) {
|
|
93
|
+
if (path.length === 0) return ".";
|
|
94
|
+
const hasRoot = path.charCodeAt(0) === 47;
|
|
95
|
+
let end = -1;
|
|
96
|
+
let matchedSlash = true;
|
|
97
|
+
for (let i = path.length - 1; i >= 1; --i) {
|
|
98
|
+
if (path.charCodeAt(i) === 47) {
|
|
99
|
+
if (!matchedSlash) {
|
|
100
|
+
end = i;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
matchedSlash = false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (end === -1) return hasRoot ? "/" : ".";
|
|
108
|
+
if (hasRoot && end === 1) return "//";
|
|
109
|
+
return path.slice(0, end);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Node resolves both sides against `process.cwd()` first. When both are
|
|
114
|
+
* relative the cwd is a common prefix and drops out of the answer, so a root
|
|
115
|
+
* stands in for it; when only one is, the answer depends on a directory a
|
|
116
|
+
* browser does not have, and it is refused rather than guessed.
|
|
117
|
+
*/
|
|
118
|
+
function resolveBoth(from, to) {
|
|
119
|
+
const fromAbsolute = from.charCodeAt(0) === 47;
|
|
120
|
+
const toAbsolute = to.charCodeAt(0) === 47;
|
|
121
|
+
if (fromAbsolute !== toAbsolute) {
|
|
122
|
+
throw new Error(`posixPath.relative needs two absolute or two relative paths, got '${from}' and '${to}'.`);
|
|
123
|
+
}
|
|
124
|
+
const root = path => `/${normalizeSegments(fromAbsolute ? path : `/${path}`, false)}`;
|
|
125
|
+
return [root(from), root(to)];
|
|
126
|
+
}
|
|
127
|
+
function relative(from, to) {
|
|
128
|
+
if (from === to) return "";
|
|
129
|
+
[from, to] = resolveBoth(from, to);
|
|
130
|
+
if (from === to) return "";
|
|
131
|
+
const fromStart = 1;
|
|
132
|
+
const fromEnd = from.length;
|
|
133
|
+
const fromLen = fromEnd - fromStart;
|
|
134
|
+
const toStart = 1;
|
|
135
|
+
const toLen = to.length - toStart;
|
|
136
|
+
const length = fromLen < toLen ? fromLen : toLen;
|
|
137
|
+
let lastCommonSep = -1;
|
|
138
|
+
let i = 0;
|
|
139
|
+
for (; i < length; i++) {
|
|
140
|
+
const fromCode = from.charCodeAt(fromStart + i);
|
|
141
|
+
if (fromCode !== to.charCodeAt(toStart + i)) break;else if (fromCode === 47) lastCommonSep = i;
|
|
142
|
+
}
|
|
143
|
+
if (i === length) {
|
|
144
|
+
if (toLen > length) {
|
|
145
|
+
if (to.charCodeAt(toStart + i) === 47) {
|
|
146
|
+
return to.slice(toStart + i + 1);
|
|
147
|
+
}
|
|
148
|
+
if (i === 0) {
|
|
149
|
+
return to.slice(toStart + i);
|
|
150
|
+
}
|
|
151
|
+
} else if (fromLen > length) {
|
|
152
|
+
if (from.charCodeAt(fromStart + i) === 47) {
|
|
153
|
+
lastCommonSep = i;
|
|
154
|
+
} else if (i === 0) {
|
|
155
|
+
lastCommonSep = 0;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
let out = "";
|
|
160
|
+
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
|
161
|
+
if (i === fromEnd || from.charCodeAt(i) === 47) {
|
|
162
|
+
out += out.length === 0 ? ".." : "/..";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return `${out}${to.slice(toStart + lastCommonSep)}`;
|
|
166
|
+
}
|
|
167
|
+
const posixPath = {
|
|
168
|
+
normalize,
|
|
169
|
+
join,
|
|
170
|
+
dirname,
|
|
171
|
+
relative
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Finds every `.jsonValues()` record in a module's schema that is NOT the
|
|
176
|
+
* module's root, returning the path to each within the module source.
|
|
177
|
+
*
|
|
178
|
+
* `.jsonValues()` is only supported on a module's ROOT record/router: the
|
|
179
|
+
* `/json` endpoint keys entries by a single string, the Studio substitutes
|
|
180
|
+
* loaded content at the top level of the module source, and
|
|
181
|
+
* `validateJsonValuesEntries` only visits a root record. A nested one would
|
|
182
|
+
* silently skip content validation and hang the Studio on a 404, so we reject
|
|
183
|
+
* it up front instead (see {@link ValOps.initSources}).
|
|
184
|
+
*/
|
|
185
|
+
function findNestedJsonValuesRecords(schema, path = []) {
|
|
186
|
+
const found = [];
|
|
187
|
+
const rec = (current, currentPath) => {
|
|
188
|
+
if (current.type === "record" && current.jsonValues && currentPath.length > 0) {
|
|
189
|
+
found.push(currentPath);
|
|
190
|
+
// Do not descend: everything below lives in the entry's `*.val.json`.
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
switch (current.type) {
|
|
194
|
+
case "object":
|
|
195
|
+
for (const key of Object.keys(current.items)) {
|
|
196
|
+
rec(current.items[key], currentPath.concat(key));
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
case "record":
|
|
200
|
+
rec(current.item, currentPath.concat("*"));
|
|
201
|
+
return;
|
|
202
|
+
case "array":
|
|
203
|
+
rec(current.item, currentPath.concat("*"));
|
|
204
|
+
return;
|
|
205
|
+
case "discriminated-union":
|
|
206
|
+
for (let i = 0; i < current.items.length; i++) {
|
|
207
|
+
rec(current.items[i], currentPath.concat(`union[${i}]`));
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
default:
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
rec(schema, path);
|
|
215
|
+
return found;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The `.val.ts` suffix a module file path ends with. Stripping it yields the
|
|
220
|
+
* folder that a new entry's `*.val.json` files are nested under.
|
|
221
|
+
*/
|
|
222
|
+
const VAL_TS_SUFFIX = ".val.ts";
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Computes the `*.val.json` file path (relative to rootDir) and the `import(...)`
|
|
226
|
+
* path (relative to the module's directory) for a NEW `.jsonValues()` entry,
|
|
227
|
+
* following the locked filename convention: the file mirrors the entry key under
|
|
228
|
+
* a folder named after the `.val.ts` (its `.val.ts` suffix becomes the folder).
|
|
229
|
+
*
|
|
230
|
+
* For module `/app/support/[slug]/page.val.ts` and key `/support/faq`:
|
|
231
|
+
* - jsonPath: `/app/support/[slug]/page/support/faq.val.json`
|
|
232
|
+
* - importPath: `./page/support/faq.val.json`
|
|
233
|
+
*/
|
|
234
|
+
function getNewJsonEntryPaths(moduleFilePath, entryKey) {
|
|
235
|
+
const base = moduleFilePath.endsWith(VAL_TS_SUFFIX) ? moduleFilePath.slice(0, -VAL_TS_SUFFIX.length) : moduleFilePath;
|
|
236
|
+
const keyRel = entryKey.replace(/^\//, "");
|
|
237
|
+
const invalid = reason => fp.result.err({
|
|
238
|
+
message: `Invalid .jsonValues() entry key '${entryKey}' in ${moduleFilePath}: ${reason}`,
|
|
239
|
+
filePath: moduleFilePath
|
|
240
|
+
});
|
|
241
|
+
// An entry key is CLIENT-SUPPLIED — it arrives as a record key in a patch op —
|
|
242
|
+
// and this path is what the commit writes to disk. Left unchecked, a key with
|
|
243
|
+
// `..` segments (or a backslash, which is a separator once the path is handed
|
|
244
|
+
// to node's `path.join` on Windows) puts that write anywhere in the project or
|
|
245
|
+
// outside it entirely, and a `move` additionally deletes the source path.
|
|
246
|
+
if (keyRel === "") {
|
|
247
|
+
return invalid("it is empty");
|
|
248
|
+
}
|
|
249
|
+
if (keyRel.includes("\\") || keyRel.includes("\0")) {
|
|
250
|
+
return invalid("it contains a backslash or a NUL byte");
|
|
251
|
+
}
|
|
252
|
+
const jsonPath = posixPath.normalize(`${base}/${keyRel}.val.json`);
|
|
253
|
+
if (!jsonPath.startsWith(`${base}/`)) {
|
|
254
|
+
return invalid(`it resolves outside '${base}/'`);
|
|
255
|
+
}
|
|
256
|
+
const moduleDir = posixPath.dirname(moduleFilePath);
|
|
257
|
+
let importPath = posixPath.relative(moduleDir, jsonPath);
|
|
258
|
+
if (!importPath.startsWith(".")) {
|
|
259
|
+
importPath = `./${importPath}`;
|
|
260
|
+
}
|
|
261
|
+
return fp.result.ok({
|
|
262
|
+
jsonPath,
|
|
263
|
+
importPath
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Resolves an EXISTING entry's `*.val.json` path (relative to rootDir) from the
|
|
269
|
+
* `import(...)` path recorded in the `.val.ts` thunk (from
|
|
270
|
+
* {@link analyzeJsonValuesEntries}). Existing files may have been hand-placed,
|
|
271
|
+
* so the import path is authoritative (hybrid authoring).
|
|
272
|
+
*/
|
|
273
|
+
function resolveExistingJsonPath(moduleFilePath, importPath) {
|
|
274
|
+
const moduleDir = posixPath.dirname(moduleFilePath);
|
|
275
|
+
return posixPath.join(moduleDir, importPath);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
exports.findNestedJsonValuesRecords = findNestedJsonValuesRecords;
|
|
279
|
+
exports.getNewJsonEntryPaths = getNewJsonEntryPaths;
|
|
280
|
+
exports.posixPath = posixPath;
|
|
281
|
+
exports.resolveExistingJsonPath = resolveExistingJsonPath;
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fp = require('@valbuild/core/fp');
|
|
4
|
+
require('@valbuild/shared/internal');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `path.posix`, for the four functions the request handlers use.
|
|
8
|
+
*
|
|
9
|
+
* Node's `path` cannot be bundled for a browser, and the request handlers run
|
|
10
|
+
* in one for the demo (`@valbuild/server/browser`). These are Node's own posix
|
|
11
|
+
* algorithms, and `posixPath.test.ts` checks them against `path.posix` itself,
|
|
12
|
+
* so the two cannot differ quietly. Module paths are always posix -- `/app/
|
|
13
|
+
* page.val.ts` on Windows too -- so nothing here needs the platform's `path`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Resolves `.` and `..` segments, as Node's `normalizeString` does. */
|
|
17
|
+
function normalizeSegments(path, allowAboveRoot) {
|
|
18
|
+
let res = "";
|
|
19
|
+
let lastSegmentLength = 0;
|
|
20
|
+
let lastSlash = -1;
|
|
21
|
+
let dots = 0;
|
|
22
|
+
let code = 0;
|
|
23
|
+
for (let i = 0; i <= path.length; ++i) {
|
|
24
|
+
if (i < path.length) {
|
|
25
|
+
code = path.charCodeAt(i);
|
|
26
|
+
} else if (code === 47 /* / */) {
|
|
27
|
+
break;
|
|
28
|
+
} else {
|
|
29
|
+
code = 47;
|
|
30
|
+
}
|
|
31
|
+
if (code === 47) {
|
|
32
|
+
if (lastSlash === i - 1 || dots === 1) ; else if (dots === 2) {
|
|
33
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /* . */ || res.charCodeAt(res.length - 2) !== 46) {
|
|
34
|
+
if (res.length > 2) {
|
|
35
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
36
|
+
if (lastSlashIndex === -1) {
|
|
37
|
+
res = "";
|
|
38
|
+
lastSegmentLength = 0;
|
|
39
|
+
} else {
|
|
40
|
+
res = res.slice(0, lastSlashIndex);
|
|
41
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
42
|
+
}
|
|
43
|
+
lastSlash = i;
|
|
44
|
+
dots = 0;
|
|
45
|
+
continue;
|
|
46
|
+
} else if (res.length !== 0) {
|
|
47
|
+
res = "";
|
|
48
|
+
lastSegmentLength = 0;
|
|
49
|
+
lastSlash = i;
|
|
50
|
+
dots = 0;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (allowAboveRoot) {
|
|
55
|
+
res += res.length > 0 ? "/.." : "..";
|
|
56
|
+
lastSegmentLength = 2;
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
if (res.length > 0) {
|
|
60
|
+
res += "/" + path.slice(lastSlash + 1, i);
|
|
61
|
+
} else {
|
|
62
|
+
res = path.slice(lastSlash + 1, i);
|
|
63
|
+
}
|
|
64
|
+
lastSegmentLength = i - lastSlash - 1;
|
|
65
|
+
}
|
|
66
|
+
lastSlash = i;
|
|
67
|
+
dots = 0;
|
|
68
|
+
} else if (code === 46 && dots !== -1) {
|
|
69
|
+
++dots;
|
|
70
|
+
} else {
|
|
71
|
+
dots = -1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return res;
|
|
75
|
+
}
|
|
76
|
+
function normalize(path) {
|
|
77
|
+
if (path.length === 0) return ".";
|
|
78
|
+
const isAbsolute = path.charCodeAt(0) === 47;
|
|
79
|
+
const trailingSeparator = path.charCodeAt(path.length - 1) === 47;
|
|
80
|
+
let normalized = normalizeSegments(path, !isAbsolute);
|
|
81
|
+
if (normalized.length === 0) {
|
|
82
|
+
if (isAbsolute) return "/";
|
|
83
|
+
return trailingSeparator ? "./" : ".";
|
|
84
|
+
}
|
|
85
|
+
if (trailingSeparator) normalized += "/";
|
|
86
|
+
return isAbsolute ? `/${normalized}` : normalized;
|
|
87
|
+
}
|
|
88
|
+
function join(...paths) {
|
|
89
|
+
const joined = paths.filter(path => path.length > 0).join("/");
|
|
90
|
+
return joined.length === 0 ? "." : normalize(joined);
|
|
91
|
+
}
|
|
92
|
+
function dirname(path) {
|
|
93
|
+
if (path.length === 0) return ".";
|
|
94
|
+
const hasRoot = path.charCodeAt(0) === 47;
|
|
95
|
+
let end = -1;
|
|
96
|
+
let matchedSlash = true;
|
|
97
|
+
for (let i = path.length - 1; i >= 1; --i) {
|
|
98
|
+
if (path.charCodeAt(i) === 47) {
|
|
99
|
+
if (!matchedSlash) {
|
|
100
|
+
end = i;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
matchedSlash = false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (end === -1) return hasRoot ? "/" : ".";
|
|
108
|
+
if (hasRoot && end === 1) return "//";
|
|
109
|
+
return path.slice(0, end);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Node resolves both sides against `process.cwd()` first. When both are
|
|
114
|
+
* relative the cwd is a common prefix and drops out of the answer, so a root
|
|
115
|
+
* stands in for it; when only one is, the answer depends on a directory a
|
|
116
|
+
* browser does not have, and it is refused rather than guessed.
|
|
117
|
+
*/
|
|
118
|
+
function resolveBoth(from, to) {
|
|
119
|
+
const fromAbsolute = from.charCodeAt(0) === 47;
|
|
120
|
+
const toAbsolute = to.charCodeAt(0) === 47;
|
|
121
|
+
if (fromAbsolute !== toAbsolute) {
|
|
122
|
+
throw new Error(`posixPath.relative needs two absolute or two relative paths, got '${from}' and '${to}'.`);
|
|
123
|
+
}
|
|
124
|
+
const root = path => `/${normalizeSegments(fromAbsolute ? path : `/${path}`, false)}`;
|
|
125
|
+
return [root(from), root(to)];
|
|
126
|
+
}
|
|
127
|
+
function relative(from, to) {
|
|
128
|
+
if (from === to) return "";
|
|
129
|
+
[from, to] = resolveBoth(from, to);
|
|
130
|
+
if (from === to) return "";
|
|
131
|
+
const fromStart = 1;
|
|
132
|
+
const fromEnd = from.length;
|
|
133
|
+
const fromLen = fromEnd - fromStart;
|
|
134
|
+
const toStart = 1;
|
|
135
|
+
const toLen = to.length - toStart;
|
|
136
|
+
const length = fromLen < toLen ? fromLen : toLen;
|
|
137
|
+
let lastCommonSep = -1;
|
|
138
|
+
let i = 0;
|
|
139
|
+
for (; i < length; i++) {
|
|
140
|
+
const fromCode = from.charCodeAt(fromStart + i);
|
|
141
|
+
if (fromCode !== to.charCodeAt(toStart + i)) break;else if (fromCode === 47) lastCommonSep = i;
|
|
142
|
+
}
|
|
143
|
+
if (i === length) {
|
|
144
|
+
if (toLen > length) {
|
|
145
|
+
if (to.charCodeAt(toStart + i) === 47) {
|
|
146
|
+
return to.slice(toStart + i + 1);
|
|
147
|
+
}
|
|
148
|
+
if (i === 0) {
|
|
149
|
+
return to.slice(toStart + i);
|
|
150
|
+
}
|
|
151
|
+
} else if (fromLen > length) {
|
|
152
|
+
if (from.charCodeAt(fromStart + i) === 47) {
|
|
153
|
+
lastCommonSep = i;
|
|
154
|
+
} else if (i === 0) {
|
|
155
|
+
lastCommonSep = 0;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
let out = "";
|
|
160
|
+
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
|
161
|
+
if (i === fromEnd || from.charCodeAt(i) === 47) {
|
|
162
|
+
out += out.length === 0 ? ".." : "/..";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return `${out}${to.slice(toStart + lastCommonSep)}`;
|
|
166
|
+
}
|
|
167
|
+
const posixPath = {
|
|
168
|
+
normalize,
|
|
169
|
+
join,
|
|
170
|
+
dirname,
|
|
171
|
+
relative
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Finds every `.jsonValues()` record in a module's schema that is NOT the
|
|
176
|
+
* module's root, returning the path to each within the module source.
|
|
177
|
+
*
|
|
178
|
+
* `.jsonValues()` is only supported on a module's ROOT record/router: the
|
|
179
|
+
* `/json` endpoint keys entries by a single string, the Studio substitutes
|
|
180
|
+
* loaded content at the top level of the module source, and
|
|
181
|
+
* `validateJsonValuesEntries` only visits a root record. A nested one would
|
|
182
|
+
* silently skip content validation and hang the Studio on a 404, so we reject
|
|
183
|
+
* it up front instead (see {@link ValOps.initSources}).
|
|
184
|
+
*/
|
|
185
|
+
function findNestedJsonValuesRecords(schema, path = []) {
|
|
186
|
+
const found = [];
|
|
187
|
+
const rec = (current, currentPath) => {
|
|
188
|
+
if (current.type === "record" && current.jsonValues && currentPath.length > 0) {
|
|
189
|
+
found.push(currentPath);
|
|
190
|
+
// Do not descend: everything below lives in the entry's `*.val.json`.
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
switch (current.type) {
|
|
194
|
+
case "object":
|
|
195
|
+
for (const key of Object.keys(current.items)) {
|
|
196
|
+
rec(current.items[key], currentPath.concat(key));
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
case "record":
|
|
200
|
+
rec(current.item, currentPath.concat("*"));
|
|
201
|
+
return;
|
|
202
|
+
case "array":
|
|
203
|
+
rec(current.item, currentPath.concat("*"));
|
|
204
|
+
return;
|
|
205
|
+
case "discriminated-union":
|
|
206
|
+
for (let i = 0; i < current.items.length; i++) {
|
|
207
|
+
rec(current.items[i], currentPath.concat(`union[${i}]`));
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
default:
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
rec(schema, path);
|
|
215
|
+
return found;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The `.val.ts` suffix a module file path ends with. Stripping it yields the
|
|
220
|
+
* folder that a new entry's `*.val.json` files are nested under.
|
|
221
|
+
*/
|
|
222
|
+
const VAL_TS_SUFFIX = ".val.ts";
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Computes the `*.val.json` file path (relative to rootDir) and the `import(...)`
|
|
226
|
+
* path (relative to the module's directory) for a NEW `.jsonValues()` entry,
|
|
227
|
+
* following the locked filename convention: the file mirrors the entry key under
|
|
228
|
+
* a folder named after the `.val.ts` (its `.val.ts` suffix becomes the folder).
|
|
229
|
+
*
|
|
230
|
+
* For module `/app/support/[slug]/page.val.ts` and key `/support/faq`:
|
|
231
|
+
* - jsonPath: `/app/support/[slug]/page/support/faq.val.json`
|
|
232
|
+
* - importPath: `./page/support/faq.val.json`
|
|
233
|
+
*/
|
|
234
|
+
function getNewJsonEntryPaths(moduleFilePath, entryKey) {
|
|
235
|
+
const base = moduleFilePath.endsWith(VAL_TS_SUFFIX) ? moduleFilePath.slice(0, -VAL_TS_SUFFIX.length) : moduleFilePath;
|
|
236
|
+
const keyRel = entryKey.replace(/^\//, "");
|
|
237
|
+
const invalid = reason => fp.result.err({
|
|
238
|
+
message: `Invalid .jsonValues() entry key '${entryKey}' in ${moduleFilePath}: ${reason}`,
|
|
239
|
+
filePath: moduleFilePath
|
|
240
|
+
});
|
|
241
|
+
// An entry key is CLIENT-SUPPLIED — it arrives as a record key in a patch op —
|
|
242
|
+
// and this path is what the commit writes to disk. Left unchecked, a key with
|
|
243
|
+
// `..` segments (or a backslash, which is a separator once the path is handed
|
|
244
|
+
// to node's `path.join` on Windows) puts that write anywhere in the project or
|
|
245
|
+
// outside it entirely, and a `move` additionally deletes the source path.
|
|
246
|
+
if (keyRel === "") {
|
|
247
|
+
return invalid("it is empty");
|
|
248
|
+
}
|
|
249
|
+
if (keyRel.includes("\\") || keyRel.includes("\0")) {
|
|
250
|
+
return invalid("it contains a backslash or a NUL byte");
|
|
251
|
+
}
|
|
252
|
+
const jsonPath = posixPath.normalize(`${base}/${keyRel}.val.json`);
|
|
253
|
+
if (!jsonPath.startsWith(`${base}/`)) {
|
|
254
|
+
return invalid(`it resolves outside '${base}/'`);
|
|
255
|
+
}
|
|
256
|
+
const moduleDir = posixPath.dirname(moduleFilePath);
|
|
257
|
+
let importPath = posixPath.relative(moduleDir, jsonPath);
|
|
258
|
+
if (!importPath.startsWith(".")) {
|
|
259
|
+
importPath = `./${importPath}`;
|
|
260
|
+
}
|
|
261
|
+
return fp.result.ok({
|
|
262
|
+
jsonPath,
|
|
263
|
+
importPath
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Resolves an EXISTING entry's `*.val.json` path (relative to rootDir) from the
|
|
269
|
+
* `import(...)` path recorded in the `.val.ts` thunk (from
|
|
270
|
+
* {@link analyzeJsonValuesEntries}). Existing files may have been hand-placed,
|
|
271
|
+
* so the import path is authoritative (hybrid authoring).
|
|
272
|
+
*/
|
|
273
|
+
function resolveExistingJsonPath(moduleFilePath, importPath) {
|
|
274
|
+
const moduleDir = posixPath.dirname(moduleFilePath);
|
|
275
|
+
return posixPath.join(moduleDir, importPath);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
exports.findNestedJsonValuesRecords = findNestedJsonValuesRecords;
|
|
279
|
+
exports.getNewJsonEntryPaths = getNewJsonEntryPaths;
|
|
280
|
+
exports.posixPath = posixPath;
|
|
281
|
+
exports.resolveExistingJsonPath = resolveExistingJsonPath;
|