@valbuild/shared 0.121.0 → 0.123.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 +178 -0
- package/dist/declarations/src/internal/ApiRoutes.d.ts +304 -41
- package/dist/declarations/src/internal/index.d.ts +7 -0
- package/dist/declarations/src/internal/media/encodeImageDecisions.d.ts +89 -0
- package/dist/declarations/src/internal/schema/compatibility.d.ts +62 -0
- package/dist/declarations/src/internal/search/getFilenameFromRef.d.ts +17 -0
- package/dist/declarations/src/internal/search/searchIndex.d.ts +76 -0
- package/dist/declarations/src/internal/search/sourcePath.d.ts +30 -0
- package/dist/declarations/src/internal/search/traverseSchemaSource.d.ts +20 -0
- package/dist/declarations/src/internal/zod/History.d.ts +257 -0
- package/dist/declarations/src/internal/zod/Patch.d.ts +7 -0
- package/dist/declarations/src/internal/zod/SerializedSchema.d.ts +2 -1
- package/internal/dist/valbuild-shared-internal.cjs.dev.js +1461 -124
- package/internal/dist/valbuild-shared-internal.cjs.prod.js +1461 -124
- package/internal/dist/valbuild-shared-internal.esm.js +1429 -127
- package/package.json +2 -1
|
@@ -2,6 +2,7 @@ import { Internal, CODE_LANGUAGES, DEFAULT_COLOR_FORMAT } from '@valbuild/core';
|
|
|
2
2
|
import { b as base64DataUrlToUint8Array } from '../../dist/utils-00f3e1df.esm.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { fromError } from 'zod-validation-error';
|
|
5
|
+
import FlexSearch from 'flexsearch';
|
|
5
6
|
|
|
6
7
|
function _toPrimitive(t, r) {
|
|
7
8
|
if ("object" != typeof t || !t) return t;
|
|
@@ -897,6 +898,12 @@ function _asyncToGenerator(n) {
|
|
|
897
898
|
};
|
|
898
899
|
}
|
|
899
900
|
|
|
901
|
+
/**
|
|
902
|
+
* JSON, recursively — the shape of a Source over the wire.
|
|
903
|
+
*
|
|
904
|
+
* Exported because history reads stored Sources back and needs the same
|
|
905
|
+
* definition; a second copy is a second thing to keep in step.
|
|
906
|
+
*/
|
|
900
907
|
var JSONValue = z.lazy(function () {
|
|
901
908
|
return z.union([z.string(), z.number(), z["boolean"](), z["null"](), z.array(JSONValue), z.record(z.string(), JSONValue)]);
|
|
902
909
|
});
|
|
@@ -1275,7 +1282,26 @@ var SerializedRouteSchema = z.object({
|
|
|
1275
1282
|
readonly: z["boolean"]().optional(),
|
|
1276
1283
|
hidden: z["boolean"]().optional()
|
|
1277
1284
|
});
|
|
1278
|
-
|
|
1285
|
+
|
|
1286
|
+
// A settings module, and each section inside one, serialize as this - the
|
|
1287
|
+
// shape is recursive because `items` holds sections which are themselves
|
|
1288
|
+
// settings schemas. `s.settings()` never writes render/preview/customValidate
|
|
1289
|
+
// (see core/src/schema/settings.ts), but they are accepted here because they
|
|
1290
|
+
// are part of the declared type.
|
|
1291
|
+
var SerializedSettingsSchema = z.lazy(function () {
|
|
1292
|
+
return z.object({
|
|
1293
|
+
type: z.literal("settings"),
|
|
1294
|
+
render: FieldRender,
|
|
1295
|
+
preview: z.literal(true).optional(),
|
|
1296
|
+
items: z.record(z.string(), SerializedSchema),
|
|
1297
|
+
opt: z["boolean"](),
|
|
1298
|
+
customValidate: z["boolean"]().optional(),
|
|
1299
|
+
readonly: z["boolean"]().optional(),
|
|
1300
|
+
hidden: z["boolean"]().optional(),
|
|
1301
|
+
description: z.string().optional()
|
|
1302
|
+
});
|
|
1303
|
+
});
|
|
1304
|
+
var SerializedSchema = z.union([SerializedStringSchema, SerializedLiteralSchema, SerializedBooleanSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedArraySchema, SerializedUnionSchema, SerializedRichTextSchema, SerializedRecordSchema, SerializedKeyOfSchema, SerializedRouteSchema, SerializedFileSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedColorSchema, SerializedCodeSchema, SerializedSettingsSchema, SerializedImageSchema]);
|
|
1279
1305
|
|
|
1280
1306
|
var ValCommit = z.object({
|
|
1281
1307
|
commitSha: z.string(),
|
|
@@ -1287,6 +1313,125 @@ var ValCommit = z.object({
|
|
|
1287
1313
|
commitMessage: z.string().nullable()
|
|
1288
1314
|
});
|
|
1289
1315
|
|
|
1316
|
+
/**
|
|
1317
|
+
* The wire shapes for reading history.
|
|
1318
|
+
*
|
|
1319
|
+
* Here rather than in `@valbuild/server` because both sides of `/api/val` need
|
|
1320
|
+
* them: the server builds these and the Studio parses them, and a second
|
|
1321
|
+
* hand-written copy in the client is how the two drift.
|
|
1322
|
+
*
|
|
1323
|
+
* `HistoryError` is a closed union of everything that can go wrong
|
|
1324
|
+
* reconstructing a commit - see `packages/server/src/history/HistoryError.ts`
|
|
1325
|
+
* for what each one means. It travels as data rather than as a message because
|
|
1326
|
+
* the Studio decides what to OFFER from it: a schema mismatch means "cannot
|
|
1327
|
+
* restore this field", a missing source means "cannot restore this module at
|
|
1328
|
+
* all", and a rendered string cannot be told apart.
|
|
1329
|
+
*/
|
|
1330
|
+
var HistoryError = z.discriminatedUnion("kind", [z.object({
|
|
1331
|
+
kind: z.literal("commit-not-found"),
|
|
1332
|
+
commitSha: z.string()
|
|
1333
|
+
}), z.object({
|
|
1334
|
+
kind: z.literal("archive-unreadable"),
|
|
1335
|
+
commitSha: z.string(),
|
|
1336
|
+
message: z.string()
|
|
1337
|
+
}), z.object({
|
|
1338
|
+
kind: z.literal("source-unavailable"),
|
|
1339
|
+
moduleFilePath: z.string()
|
|
1340
|
+
}), z.object({
|
|
1341
|
+
kind: z.literal("schema-unreadable"),
|
|
1342
|
+
moduleFilePath: z.string(),
|
|
1343
|
+
message: z.string()
|
|
1344
|
+
}), z.object({
|
|
1345
|
+
kind: z.literal("file-unavailable"),
|
|
1346
|
+
gitPath: z.string(),
|
|
1347
|
+
message: z.string()
|
|
1348
|
+
}), z.object({
|
|
1349
|
+
kind: z.literal("not-supported-in-fs-mode")
|
|
1350
|
+
}), z.object({
|
|
1351
|
+
kind: z.literal("transport"),
|
|
1352
|
+
message: z.string()
|
|
1353
|
+
})]);
|
|
1354
|
+
var HistoricalCommit = z.object({
|
|
1355
|
+
commitSha: z.string(),
|
|
1356
|
+
parentCommitSha: z.string(),
|
|
1357
|
+
clientCommitSha: z.string(),
|
|
1358
|
+
branch: z.string(),
|
|
1359
|
+
createdBranch: z.string().nullable(),
|
|
1360
|
+
creator: z.string().nullable(),
|
|
1361
|
+
message: z.string().nullable(),
|
|
1362
|
+
createdAt: z.string(),
|
|
1363
|
+
seqNum: z.string(),
|
|
1364
|
+
patchCount: z.number(),
|
|
1365
|
+
/** False for commits made before history was recorded. */
|
|
1366
|
+
hasArchive: z["boolean"]()
|
|
1367
|
+
});
|
|
1368
|
+
var FileChange = z.union([z.literal("added"), z.literal("modified"), z.literal("deleted")]);
|
|
1369
|
+
|
|
1370
|
+
/**
|
|
1371
|
+
* A binary file the commit touched - named, with a URL, and nothing fetched.
|
|
1372
|
+
* Only an `<img src>` that mounts pays for the bytes.
|
|
1373
|
+
*/
|
|
1374
|
+
var BinaryFileRef = z.object({
|
|
1375
|
+
gitPath: z.string(),
|
|
1376
|
+
change: FileChange,
|
|
1377
|
+
remote: z["boolean"](),
|
|
1378
|
+
url: z.string()
|
|
1379
|
+
});
|
|
1380
|
+
var HistoricalModule = z.object({
|
|
1381
|
+
/** The module's data at this commit. Null if deleted, or unreadable. */
|
|
1382
|
+
source: JSONValue.nullable(),
|
|
1383
|
+
/**
|
|
1384
|
+
* The schema at this commit, already validated against THIS Val.
|
|
1385
|
+
*
|
|
1386
|
+
* Null with a `schema-unreadable` failure means the stored schema came from a
|
|
1387
|
+
* Val whose schema format this one does not know — expected, not damage.
|
|
1388
|
+
*/
|
|
1389
|
+
schema: SerializedSchema.nullable(),
|
|
1390
|
+
patchIds: z.array(PatchId),
|
|
1391
|
+
changedPaths: z.array(z.string()),
|
|
1392
|
+
failures: z.array(HistoryError)
|
|
1393
|
+
});
|
|
1394
|
+
var HistoricalPatchSet = z.object({
|
|
1395
|
+
commit: HistoricalCommit,
|
|
1396
|
+
modules: z.record(z.string(), HistoricalModule),
|
|
1397
|
+
patches: z.array(z.object({
|
|
1398
|
+
patchId: PatchId,
|
|
1399
|
+
moduleFilePath: z.string(),
|
|
1400
|
+
patch: z.unknown(),
|
|
1401
|
+
authorId: z.string().nullable(),
|
|
1402
|
+
createdAt: z.string(),
|
|
1403
|
+
baseSha: z.string(),
|
|
1404
|
+
coreVersion: z.string()
|
|
1405
|
+
})),
|
|
1406
|
+
jsonEntries: z.record(z.string(), z.unknown()),
|
|
1407
|
+
binaryFiles: z.array(BinaryFileRef),
|
|
1408
|
+
warnings: z.array(HistoryError)
|
|
1409
|
+
});
|
|
1410
|
+
/**
|
|
1411
|
+
* Whether the historical value can be written back as the project stands.
|
|
1412
|
+
*
|
|
1413
|
+
* Computed in the Studio, not on the server: the verdict depends on the current
|
|
1414
|
+
* source and the current schema, both of which the client already holds, and it
|
|
1415
|
+
* runs against the same worker-thread validator that validates ordinary edits.
|
|
1416
|
+
* The types stay here because they are what a restore preview hands to the UI.
|
|
1417
|
+
*
|
|
1418
|
+
* `blocked` carries its reasons so a refusal can name the field and say why,
|
|
1419
|
+
* rather than being an unexplained disabled button.
|
|
1420
|
+
*/
|
|
1421
|
+
var RestoreVerdict = z.union([z.object({
|
|
1422
|
+
status: z.literal("restorable")
|
|
1423
|
+
}), z.object({
|
|
1424
|
+
status: z.literal("blocked"),
|
|
1425
|
+
reasons: z.array(HistoryError)
|
|
1426
|
+
})]);
|
|
1427
|
+
var ModuleComparison = z.object({
|
|
1428
|
+
current: z.unknown().nullable(),
|
|
1429
|
+
/** Paths where the commit's result and the current source differ: what a
|
|
1430
|
+
* restore would undo. */
|
|
1431
|
+
changedVsCurrent: z.array(z.string()),
|
|
1432
|
+
verdict: RestoreVerdict
|
|
1433
|
+
});
|
|
1434
|
+
|
|
1290
1435
|
var ModuleFilePath = z.string().refine(function (_path) {
|
|
1291
1436
|
return true;
|
|
1292
1437
|
} // TODO: validation
|
|
@@ -1556,48 +1701,6 @@ var Api = {
|
|
|
1556
1701
|
})
|
|
1557
1702
|
}
|
|
1558
1703
|
},
|
|
1559
|
-
"/session": {
|
|
1560
|
-
GET: {
|
|
1561
|
-
req: {
|
|
1562
|
-
cookies: {
|
|
1563
|
-
val_session: z.string().optional()
|
|
1564
|
-
}
|
|
1565
|
-
},
|
|
1566
|
-
res: z.union([z.object({
|
|
1567
|
-
status: z.literal(200),
|
|
1568
|
-
json: z.object({
|
|
1569
|
-
mode: z.union([z.literal("local"), z.literal("proxy")]),
|
|
1570
|
-
enabled: z["boolean"]()
|
|
1571
|
-
})
|
|
1572
|
-
}), z.object({
|
|
1573
|
-
status: z.union([
|
|
1574
|
-
// TODO: Remove the ones we don't need.
|
|
1575
|
-
z.literal(400), z.literal(401), z.literal(403), z.literal(404), z.literal(500), z.literal(501)]),
|
|
1576
|
-
json: z.object({
|
|
1577
|
-
message: z.string()
|
|
1578
|
-
})
|
|
1579
|
-
}), z.object({
|
|
1580
|
-
status: z.literal(401),
|
|
1581
|
-
json: z.object({
|
|
1582
|
-
message: z.string(),
|
|
1583
|
-
details: z.union([z.string(), z.object({
|
|
1584
|
-
reason: z.string()
|
|
1585
|
-
}), z.object({
|
|
1586
|
-
sub: z.string(),
|
|
1587
|
-
exp: z.number(),
|
|
1588
|
-
token: z.string(),
|
|
1589
|
-
org: z.string(),
|
|
1590
|
-
project: z.string()
|
|
1591
|
-
})])
|
|
1592
|
-
})
|
|
1593
|
-
}), z.object({
|
|
1594
|
-
status: z.literal(500),
|
|
1595
|
-
json: z.object({
|
|
1596
|
-
message: z.string()
|
|
1597
|
-
})
|
|
1598
|
-
})])
|
|
1599
|
-
}
|
|
1600
|
-
},
|
|
1601
1704
|
"/logout": {
|
|
1602
1705
|
GET: {
|
|
1603
1706
|
req: {
|
|
@@ -2389,6 +2492,144 @@ var Api = {
|
|
|
2389
2492
|
})])
|
|
2390
2493
|
}
|
|
2391
2494
|
},
|
|
2495
|
+
// #region history
|
|
2496
|
+
//
|
|
2497
|
+
// Reading the past. Split across three routes on purpose, because they cache
|
|
2498
|
+
// differently: a reconstructed commit cannot change and is immutable
|
|
2499
|
+
// forever, the comparison against current source changes with every edit, and
|
|
2500
|
+
// a file at a fixed commit is immutable too. Folding them together would mean
|
|
2501
|
+
// the cheapest thing dragging the most expensive one's cache headers down to
|
|
2502
|
+
// no-store.
|
|
2503
|
+
"/history/commits": {
|
|
2504
|
+
GET: {
|
|
2505
|
+
req: {
|
|
2506
|
+
query: {
|
|
2507
|
+
branch: onlyOneStringQueryParam,
|
|
2508
|
+
limit: onlyOneIntQueryParam.optional(),
|
|
2509
|
+
/** The previous page's `nextCursor`. */
|
|
2510
|
+
cursor: onlyOneStringQueryParam.optional()
|
|
2511
|
+
},
|
|
2512
|
+
cookies: {
|
|
2513
|
+
val_session: z.string().optional()
|
|
2514
|
+
}
|
|
2515
|
+
},
|
|
2516
|
+
res: z.union([unauthorizedResponse, notFoundResponse, z.object({
|
|
2517
|
+
status: z.literal(400),
|
|
2518
|
+
json: GenericError.and(z.object({
|
|
2519
|
+
kind: z.string().optional()
|
|
2520
|
+
}))
|
|
2521
|
+
}), z.object({
|
|
2522
|
+
status: z.literal(500),
|
|
2523
|
+
json: GenericError.and(z.object({
|
|
2524
|
+
kind: z.string().optional()
|
|
2525
|
+
}))
|
|
2526
|
+
}), z.object({
|
|
2527
|
+
status: z.literal(200),
|
|
2528
|
+
json: z.object({
|
|
2529
|
+
commits: z.array(HistoricalCommit),
|
|
2530
|
+
nextCursor: z.string().nullable()
|
|
2531
|
+
})
|
|
2532
|
+
})])
|
|
2533
|
+
}
|
|
2534
|
+
},
|
|
2535
|
+
/**
|
|
2536
|
+
* One commit, reconstructed. IMMUTABLE for a given sha - it says nothing
|
|
2537
|
+
* about the current source or schema, which is what lets it be cached
|
|
2538
|
+
* forever and what makes comparing many commits cheap.
|
|
2539
|
+
*/
|
|
2540
|
+
"/history/commit": {
|
|
2541
|
+
GET: {
|
|
2542
|
+
req: {
|
|
2543
|
+
query: {
|
|
2544
|
+
commit_sha: onlyOneStringQueryParam
|
|
2545
|
+
},
|
|
2546
|
+
cookies: {
|
|
2547
|
+
val_session: z.string().optional()
|
|
2548
|
+
}
|
|
2549
|
+
},
|
|
2550
|
+
res: z.union([unauthorizedResponse, notFoundResponse, z.object({
|
|
2551
|
+
status: z.literal(400),
|
|
2552
|
+
json: GenericError
|
|
2553
|
+
}), z.object({
|
|
2554
|
+
status: z.literal(500),
|
|
2555
|
+
json: GenericError.and(z.object({
|
|
2556
|
+
kind: z.string().optional()
|
|
2557
|
+
}))
|
|
2558
|
+
}), z.object({
|
|
2559
|
+
status: z.literal(200),
|
|
2560
|
+
json: HistoricalPatchSet
|
|
2561
|
+
})])
|
|
2562
|
+
}
|
|
2563
|
+
},
|
|
2564
|
+
/**
|
|
2565
|
+
* ONE module, as of a commit — including a commit that did not change it.
|
|
2566
|
+
*
|
|
2567
|
+
* `/history/commit` covers what a commit changed, which is the narrow case:
|
|
2568
|
+
* navigate the history pane to anything else and there would be nothing to
|
|
2569
|
+
* show. This answers "how did this module look at that point", which is the
|
|
2570
|
+
* question someone comparing two panes is actually asking. Immutable for the
|
|
2571
|
+
* same reason: what a commit left behind cannot change.
|
|
2572
|
+
*/
|
|
2573
|
+
"/history/module": {
|
|
2574
|
+
GET: {
|
|
2575
|
+
req: {
|
|
2576
|
+
query: {
|
|
2577
|
+
commit_sha: onlyOneStringQueryParam,
|
|
2578
|
+
module_file_path: onlyOneStringQueryParam
|
|
2579
|
+
},
|
|
2580
|
+
cookies: {
|
|
2581
|
+
val_session: z.string().optional()
|
|
2582
|
+
}
|
|
2583
|
+
},
|
|
2584
|
+
res: z.union([unauthorizedResponse, notFoundResponse, z.object({
|
|
2585
|
+
status: z.literal(400),
|
|
2586
|
+
json: GenericError
|
|
2587
|
+
}), z.object({
|
|
2588
|
+
status: z.literal(500),
|
|
2589
|
+
json: GenericError.and(z.object({
|
|
2590
|
+
kind: z.string().optional()
|
|
2591
|
+
}))
|
|
2592
|
+
}), z.object({
|
|
2593
|
+
status: z.literal(200),
|
|
2594
|
+
json: z.object({
|
|
2595
|
+
moduleFilePath: z.string(),
|
|
2596
|
+
/**
|
|
2597
|
+
* Null when history has no record of this module at or before the
|
|
2598
|
+
* commit — it was last edited before recording started, or never.
|
|
2599
|
+
* Distinct from a module the commit deleted.
|
|
2600
|
+
*/
|
|
2601
|
+
module: HistoricalModule.nullable()
|
|
2602
|
+
})
|
|
2603
|
+
})])
|
|
2604
|
+
}
|
|
2605
|
+
},
|
|
2606
|
+
/**
|
|
2607
|
+
* One file as it was at one commit.
|
|
2608
|
+
*
|
|
2609
|
+
* The lazy half: `/history/commit` NAMES a commit's binary files and this is
|
|
2610
|
+
* where their bytes come from, so nothing is fetched until an `<img>`
|
|
2611
|
+
* actually mounts. A file at a fixed commit cannot change, so the response is
|
|
2612
|
+
* immutable and the browser's own cache handles flipping between commits.
|
|
2613
|
+
*/
|
|
2614
|
+
"/history/files": {
|
|
2615
|
+
GET: {
|
|
2616
|
+
req: {
|
|
2617
|
+
query: {
|
|
2618
|
+
commit_sha: onlyOneStringQueryParam,
|
|
2619
|
+
path: onlyOneStringQueryParam,
|
|
2620
|
+
remote: onlyOneStringQueryParam.optional()
|
|
2621
|
+
}
|
|
2622
|
+
},
|
|
2623
|
+
res: z.union([unauthorizedResponse, notFoundResponse, z.object({
|
|
2624
|
+
status: z.literal(400),
|
|
2625
|
+
json: GenericError
|
|
2626
|
+
}), z.object({
|
|
2627
|
+
status: z.literal(200),
|
|
2628
|
+
body: z["instanceof"](ReadableStream)
|
|
2629
|
+
})])
|
|
2630
|
+
}
|
|
2631
|
+
},
|
|
2632
|
+
// #endregion history
|
|
2392
2633
|
"/files": {
|
|
2393
2634
|
GET: {
|
|
2394
2635
|
req: {
|
|
@@ -3026,98 +3267,446 @@ var urlOf = function urlOf() {
|
|
|
3026
3267
|
};
|
|
3027
3268
|
|
|
3028
3269
|
/**
|
|
3029
|
-
*
|
|
3030
|
-
*
|
|
3031
|
-
* This is the **publish head**: unlike `baseSha`, which only moves once a
|
|
3032
|
-
* deployment lands, it moves the instant somebody publishes. That makes it the
|
|
3033
|
-
* one thing a client can carry to `/save` to say "this is the world I decided
|
|
3034
|
-
* against" — and the only way the server can tell that somebody else has
|
|
3035
|
-
* published since the review screen was read.
|
|
3270
|
+
* Can this value, taken from one schema, be written into another?
|
|
3036
3271
|
*
|
|
3037
|
-
*
|
|
3038
|
-
*
|
|
3039
|
-
*
|
|
3272
|
+
* The question restore asks, and it is NOT the question validation asks.
|
|
3273
|
+
* Validation checks a value against the schema it belongs to. This compares two
|
|
3274
|
+
* schemas — the one a value was stored under and the one it is being written
|
|
3275
|
+
* into — because a restore moves a value across a schema change, and possibly
|
|
3276
|
+
* across paths.
|
|
3040
3277
|
*
|
|
3041
|
-
*
|
|
3042
|
-
*
|
|
3043
|
-
*
|
|
3044
|
-
*
|
|
3278
|
+
* Three answers, not two. `unknown` is where the comparison genuinely cannot
|
|
3279
|
+
* decide, and it exists so that an editor is told "we are not sure" rather than
|
|
3280
|
+
* being given a confident answer we made up. Treating unknown as compatible
|
|
3281
|
+
* writes values that break; treating it as incompatible blocks restores that
|
|
3282
|
+
* would have been fine.
|
|
3045
3283
|
*/
|
|
3046
|
-
function newestCommitSha(commits) {
|
|
3047
|
-
var _newest$commitSha, _newest;
|
|
3048
|
-
var newest = null;
|
|
3049
|
-
var _iterator = _createForOfIteratorHelper(commits !== null && commits !== void 0 ? commits : []),
|
|
3050
|
-
_step;
|
|
3051
|
-
try {
|
|
3052
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
3053
|
-
var commit = _step.value;
|
|
3054
|
-
if (newest === null || commit.createdAt > newest.createdAt) {
|
|
3055
|
-
newest = commit;
|
|
3056
|
-
}
|
|
3057
|
-
}
|
|
3058
|
-
} catch (err) {
|
|
3059
|
-
_iterator.e(err);
|
|
3060
|
-
} finally {
|
|
3061
|
-
_iterator.f();
|
|
3062
|
-
}
|
|
3063
|
-
return (_newest$commitSha = (_newest = newest) === null || _newest === void 0 ? void 0 : _newest.commitSha) !== null && _newest$commitSha !== void 0 ? _newest$commitSha : null;
|
|
3064
|
-
}
|
|
3065
3284
|
|
|
3066
|
-
var
|
|
3067
|
-
|
|
3068
|
-
|
|
3285
|
+
var compatible = {
|
|
3286
|
+
status: "compatible"
|
|
3287
|
+
};
|
|
3069
3288
|
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
}
|
|
3083
|
-
}
|
|
3289
|
+
/**
|
|
3290
|
+
* `Array.isArray` cannot narrow `JsonArray` out of `Json`, because it is
|
|
3291
|
+
* `readonly Json[]` — so the check that reads naturally does not type. This
|
|
3292
|
+
* predicate is that check, said once.
|
|
3293
|
+
*/
|
|
3294
|
+
function isJsonObject(value) {
|
|
3295
|
+
return _typeof(value) === "object" && value !== null && !Array.isArray(value);
|
|
3296
|
+
}
|
|
3297
|
+
function incompatible(reason) {
|
|
3298
|
+
return {
|
|
3299
|
+
status: "incompatible",
|
|
3300
|
+
reason: reason
|
|
3301
|
+
};
|
|
3302
|
+
}
|
|
3084
3303
|
|
|
3085
3304
|
/**
|
|
3086
|
-
*
|
|
3305
|
+
* "an object", "a string".
|
|
3306
|
+
*
|
|
3307
|
+
* Schema type names are data — `object`, `array`, `image` — so an interpolated
|
|
3308
|
+
* "a" is wrong for three of them, in a sentence shown to editors the moment a
|
|
3309
|
+
* restore is refused.
|
|
3087
3310
|
*/
|
|
3311
|
+
function withArticle(type) {
|
|
3312
|
+
return /^[aeiou]/i.test(type) ? "an ".concat(type) : "a ".concat(type);
|
|
3313
|
+
}
|
|
3088
3314
|
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3315
|
+
/**
|
|
3316
|
+
* A sentence an editor can act on, for the popup that explains a refusal.
|
|
3317
|
+
*
|
|
3318
|
+
* Kept beside the reasons rather than in the UI so that adding a reason without
|
|
3319
|
+
* wording for it is a type error rather than a blank dialog.
|
|
3320
|
+
*/
|
|
3321
|
+
function explainIncompatible(reason) {
|
|
3322
|
+
switch (reason.kind) {
|
|
3323
|
+
case "type-changed":
|
|
3324
|
+
return "This was ".concat(withArticle(reason.from), " and the field here is ").concat(withArticle(reason.to), ".");
|
|
3325
|
+
case "no-matching-variant":
|
|
3326
|
+
return "This does not match any of the shapes allowed here: ".concat(reason.variants.join(", "), ".");
|
|
3327
|
+
case "unknown-field":
|
|
3328
|
+
return "The old value has ".concat(reason.keys.length === 1 ? "a field" : "fields", " that no longer exist here: ").concat(reason.keys.join(", "), ".");
|
|
3329
|
+
case "missing-required":
|
|
3330
|
+
return "The field here needs ".concat(reason.keys.join(", "), ", which the old value does not have.");
|
|
3331
|
+
case "not-optional":
|
|
3332
|
+
return "The old value was empty, and this field cannot be empty.";
|
|
3333
|
+
case "literal-changed":
|
|
3334
|
+
return "This has to be exactly \"".concat(reason.to, "\", and the old value was \"").concat(reason.from, "\".");
|
|
3335
|
+
case "different-module":
|
|
3336
|
+
return "This points into ".concat(reason.to, ", and the old value pointed into ").concat(reason.from, ".");
|
|
3337
|
+
}
|
|
3338
|
+
}
|
|
3096
3339
|
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3340
|
+
/**
|
|
3341
|
+
* The variant of a union that a value actually is.
|
|
3342
|
+
*
|
|
3343
|
+
* `undefined` when the value does not identify itself — which is a real answer,
|
|
3344
|
+
* not a failure: it is what makes the caller return `unknown` rather than guess.
|
|
3345
|
+
*/
|
|
3346
|
+
function variantOf(schema, value) {
|
|
3347
|
+
if (schema.type !== "union") {
|
|
3348
|
+
return undefined;
|
|
3349
|
+
}
|
|
3350
|
+
// String union: the value IS the discriminator.
|
|
3351
|
+
if (typeof schema.key !== "string") {
|
|
3352
|
+
if (typeof value !== "string") {
|
|
3353
|
+
return undefined;
|
|
3354
|
+
}
|
|
3355
|
+
return schema.items.find(function (item) {
|
|
3356
|
+
return item.type === "literal" && item.value === value;
|
|
3357
|
+
});
|
|
3358
|
+
}
|
|
3359
|
+
// Object union: the discriminator is a literal at `key`.
|
|
3360
|
+
var discriminator = schema.key;
|
|
3361
|
+
if (!isJsonObject(value)) {
|
|
3362
|
+
return undefined;
|
|
3363
|
+
}
|
|
3364
|
+
var tag = value[discriminator];
|
|
3365
|
+
if (typeof tag !== "string") {
|
|
3366
|
+
return undefined;
|
|
3367
|
+
}
|
|
3368
|
+
return schema.items.find(function (item) {
|
|
3369
|
+
if (item.type !== "object") {
|
|
3370
|
+
return false;
|
|
3371
|
+
}
|
|
3372
|
+
var keySchema = item.items[discriminator];
|
|
3373
|
+
return (keySchema === null || keySchema === void 0 ? void 0 : keySchema.type) === "literal" && keySchema.value === tag;
|
|
3374
|
+
});
|
|
3375
|
+
}
|
|
3376
|
+
function describeVariant(schema, key) {
|
|
3377
|
+
if (schema.type === "literal") {
|
|
3378
|
+
return schema.value;
|
|
3379
|
+
}
|
|
3380
|
+
if (schema.type === "object") {
|
|
3381
|
+
var tag = schema.items[key];
|
|
3382
|
+
if ((tag === null || tag === void 0 ? void 0 : tag.type) === "literal") {
|
|
3383
|
+
return tag.value;
|
|
3384
|
+
}
|
|
3385
|
+
}
|
|
3386
|
+
return schema.type;
|
|
3387
|
+
}
|
|
3388
|
+
function checkCompatibility(from, to) {
|
|
3389
|
+
// Emptiness first: a null that lands in a field which cannot be empty is a
|
|
3390
|
+
// break regardless of what the two schemas otherwise say.
|
|
3391
|
+
if (from.value === null) {
|
|
3392
|
+
return to.schema.opt ? compatible : incompatible({
|
|
3393
|
+
kind: "not-optional"
|
|
3394
|
+
});
|
|
3395
|
+
}
|
|
3396
|
+
|
|
3397
|
+
// A changed union is not itself disqualifying. What matters is whether the
|
|
3398
|
+
// variant being restored still has somewhere to go — comparing the unions
|
|
3399
|
+
// wholesale would refuse a restore that is perfectly safe.
|
|
3400
|
+
if (to.schema.type === "union") {
|
|
3401
|
+
var key = typeof to.schema.key === "string" ? to.schema.key : "";
|
|
3402
|
+
var _iterator = _createForOfIteratorHelper(to.schema.items),
|
|
3403
|
+
_step;
|
|
3404
|
+
try {
|
|
3405
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
3406
|
+
var variant = _step.value;
|
|
3407
|
+
if (checkCompatibility(from, {
|
|
3408
|
+
schema: variant
|
|
3409
|
+
}).status === "compatible") {
|
|
3410
|
+
return compatible;
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
} catch (err) {
|
|
3414
|
+
_iterator.e(err);
|
|
3415
|
+
} finally {
|
|
3416
|
+
_iterator.f();
|
|
3417
|
+
}
|
|
3418
|
+
return incompatible({
|
|
3419
|
+
kind: "no-matching-variant",
|
|
3420
|
+
variants: to.schema.items.map(function (item) {
|
|
3421
|
+
return describeVariant(item, key);
|
|
3422
|
+
})
|
|
3423
|
+
});
|
|
3424
|
+
}
|
|
3425
|
+
|
|
3426
|
+
// Coming FROM a union into something that is not one: only the variant this
|
|
3427
|
+
// value actually is matters, so narrow before comparing.
|
|
3428
|
+
if (from.schema.type === "union") {
|
|
3429
|
+
var _variant = variantOf(from.schema, from.value);
|
|
3430
|
+
if (_variant === undefined) {
|
|
3431
|
+
return {
|
|
3432
|
+
status: "unknown",
|
|
3433
|
+
message: "The old value does not say which shape it is, so we cannot tell whether it fits here."
|
|
3434
|
+
};
|
|
3435
|
+
}
|
|
3436
|
+
return checkCompatibility({
|
|
3437
|
+
schema: _variant,
|
|
3438
|
+
value: from.value
|
|
3439
|
+
}, to);
|
|
3440
|
+
}
|
|
3441
|
+
if (from.schema.type !== to.schema.type) {
|
|
3442
|
+
return incompatible({
|
|
3443
|
+
kind: "type-changed",
|
|
3444
|
+
from: from.schema.type,
|
|
3445
|
+
to: to.schema.type
|
|
3446
|
+
});
|
|
3447
|
+
}
|
|
3448
|
+
switch (to.schema.type) {
|
|
3449
|
+
case "literal":
|
|
3450
|
+
{
|
|
3451
|
+
var fromValue = from.schema.type === "literal" ? from.schema.value : String(from.value);
|
|
3452
|
+
return fromValue === to.schema.value ? compatible : incompatible({
|
|
3453
|
+
kind: "literal-changed",
|
|
3454
|
+
from: fromValue,
|
|
3455
|
+
to: to.schema.value
|
|
3456
|
+
});
|
|
3457
|
+
}
|
|
3458
|
+
case "object":
|
|
3459
|
+
{
|
|
3460
|
+
if (from.schema.type !== "object" || !isJsonObject(from.value)) {
|
|
3461
|
+
return {
|
|
3462
|
+
status: "unknown",
|
|
3463
|
+
message: "The old value is not shaped like an object."
|
|
3464
|
+
};
|
|
3465
|
+
}
|
|
3466
|
+
var _fromValue = from.value;
|
|
3467
|
+
var target = to.schema.items;
|
|
3468
|
+
var source = from.schema.items;
|
|
3469
|
+
|
|
3470
|
+
// Keys the old value carries that the field no longer has. Writing them
|
|
3471
|
+
// would put data into a schema with nowhere to hold it.
|
|
3472
|
+
var extra = Object.keys(source).filter(function (key) {
|
|
3473
|
+
return !(key in target);
|
|
3474
|
+
});
|
|
3475
|
+
if (extra.length > 0) {
|
|
3476
|
+
return incompatible({
|
|
3477
|
+
kind: "unknown-field",
|
|
3478
|
+
keys: extra
|
|
3479
|
+
});
|
|
3480
|
+
}
|
|
3481
|
+
|
|
3482
|
+
// Keys the field requires that the old value cannot supply.
|
|
3483
|
+
var missing = Object.keys(target).filter(function (key) {
|
|
3484
|
+
return !target[key].opt && !(key in source);
|
|
3485
|
+
});
|
|
3486
|
+
if (missing.length > 0) {
|
|
3487
|
+
return incompatible({
|
|
3488
|
+
kind: "missing-required",
|
|
3489
|
+
keys: missing
|
|
3490
|
+
});
|
|
3491
|
+
}
|
|
3492
|
+
var unknown = null;
|
|
3493
|
+
for (var _i = 0, _Object$entries = Object.entries(target); _i < _Object$entries.length; _i++) {
|
|
3494
|
+
var _fromValue$_key;
|
|
3495
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
3496
|
+
_key = _Object$entries$_i[0],
|
|
3497
|
+
targetItem = _Object$entries$_i[1];
|
|
3498
|
+
var sourceItem = source[_key];
|
|
3499
|
+
if (sourceItem === undefined) {
|
|
3500
|
+
continue; // optional and absent — already allowed above
|
|
3501
|
+
}
|
|
3502
|
+
var res = checkCompatibility({
|
|
3503
|
+
schema: sourceItem,
|
|
3504
|
+
value: (_fromValue$_key = _fromValue[_key]) !== null && _fromValue$_key !== void 0 ? _fromValue$_key : null
|
|
3505
|
+
}, {
|
|
3506
|
+
schema: targetItem
|
|
3507
|
+
});
|
|
3508
|
+
if (res.status === "incompatible") {
|
|
3509
|
+
return res;
|
|
3510
|
+
}
|
|
3511
|
+
if (res.status === "unknown" && unknown === null) {
|
|
3512
|
+
unknown = res;
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
return unknown !== null && unknown !== void 0 ? unknown : compatible;
|
|
3516
|
+
}
|
|
3517
|
+
case "array":
|
|
3518
|
+
{
|
|
3519
|
+
if (from.schema.type !== "array" || !Array.isArray(from.value)) {
|
|
3520
|
+
return {
|
|
3521
|
+
status: "unknown",
|
|
3522
|
+
message: "The old value is not a list."
|
|
3523
|
+
};
|
|
3524
|
+
}
|
|
3525
|
+
// An empty list has nothing to probe the item schema with. The item
|
|
3526
|
+
// TYPES still have to agree, and that much can be decided without a
|
|
3527
|
+
// value - so decide it rather than passing an empty list as proof.
|
|
3528
|
+
if (from.value.length === 0) {
|
|
3529
|
+
return from.schema.item.type === to.schema.item.type ? compatible : incompatible({
|
|
3530
|
+
kind: "type-changed",
|
|
3531
|
+
from: from.schema.item.type,
|
|
3532
|
+
to: to.schema.item.type
|
|
3533
|
+
});
|
|
3534
|
+
}
|
|
3535
|
+
var _unknown = null;
|
|
3536
|
+
var _iterator2 = _createForOfIteratorHelper(from.value),
|
|
3537
|
+
_step2;
|
|
3538
|
+
try {
|
|
3539
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
3540
|
+
var item = _step2.value;
|
|
3541
|
+
var _res = checkCompatibility({
|
|
3542
|
+
schema: from.schema.item,
|
|
3543
|
+
value: item
|
|
3544
|
+
}, {
|
|
3545
|
+
schema: to.schema.item
|
|
3546
|
+
});
|
|
3547
|
+
if (_res.status === "incompatible") {
|
|
3548
|
+
return _res;
|
|
3549
|
+
}
|
|
3550
|
+
if (_res.status === "unknown" && _unknown === null) {
|
|
3551
|
+
_unknown = _res;
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
} catch (err) {
|
|
3555
|
+
_iterator2.e(err);
|
|
3556
|
+
} finally {
|
|
3557
|
+
_iterator2.f();
|
|
3558
|
+
}
|
|
3559
|
+
return _unknown !== null && _unknown !== void 0 ? _unknown : compatible;
|
|
3560
|
+
}
|
|
3561
|
+
case "record":
|
|
3562
|
+
{
|
|
3563
|
+
if (from.schema.type !== "record" || !isJsonObject(from.value)) {
|
|
3564
|
+
return {
|
|
3565
|
+
status: "unknown",
|
|
3566
|
+
message: "The old value is not a record."
|
|
3567
|
+
};
|
|
3568
|
+
}
|
|
3569
|
+
var _unknown2 = null;
|
|
3570
|
+
for (var _i2 = 0, _Object$values = Object.values(from.value); _i2 < _Object$values.length; _i2++) {
|
|
3571
|
+
var entry = _Object$values[_i2];
|
|
3572
|
+
var _res2 = checkCompatibility({
|
|
3573
|
+
schema: from.schema.item,
|
|
3574
|
+
value: entry !== null && entry !== void 0 ? entry : null
|
|
3575
|
+
}, {
|
|
3576
|
+
schema: to.schema.item
|
|
3577
|
+
});
|
|
3578
|
+
if (_res2.status === "incompatible") {
|
|
3579
|
+
return _res2;
|
|
3580
|
+
}
|
|
3581
|
+
if (_res2.status === "unknown" && _unknown2 === null) {
|
|
3582
|
+
_unknown2 = _res2;
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3585
|
+
return _unknown2 !== null && _unknown2 !== void 0 ? _unknown2 : compatible;
|
|
3586
|
+
}
|
|
3587
|
+
case "keyOf":
|
|
3588
|
+
{
|
|
3589
|
+
// A key is only meaningful against the module it indexes.
|
|
3590
|
+
if (from.schema.type !== "keyOf") {
|
|
3591
|
+
return compatible;
|
|
3592
|
+
}
|
|
3593
|
+
return from.schema.path === to.schema.path ? compatible : incompatible({
|
|
3594
|
+
kind: "different-module",
|
|
3595
|
+
from: from.schema.path,
|
|
3596
|
+
to: to.schema.path
|
|
3597
|
+
});
|
|
3598
|
+
}
|
|
3599
|
+
case "richtext":
|
|
3600
|
+
{
|
|
3601
|
+
// The options decide which nodes are legal, and comparing them properly
|
|
3602
|
+
// means comparing node trees against them. Not yet done, and saying so is
|
|
3603
|
+
// better than a confident answer we cannot back.
|
|
3604
|
+
return {
|
|
3605
|
+
status: "unknown",
|
|
3606
|
+
message: "Rich text can be restored, but we cannot yet confirm every mark and block is still allowed here."
|
|
3607
|
+
};
|
|
3608
|
+
}
|
|
3609
|
+
default:
|
|
3610
|
+
// Primitives and media: same type is the whole test. Constraints
|
|
3611
|
+
// (maxLength, accept, …) are validated on write, which reports them
|
|
3612
|
+
// better than a schema comparison could.
|
|
3613
|
+
return compatible;
|
|
3614
|
+
}
|
|
3615
|
+
}
|
|
3616
|
+
|
|
3617
|
+
/**
|
|
3618
|
+
* The newest commit the client has been told about, or `null` if none.
|
|
3619
|
+
*
|
|
3620
|
+
* This is the **publish head**: unlike `baseSha`, which only moves once a
|
|
3621
|
+
* deployment lands, it moves the instant somebody publishes. That makes it the
|
|
3622
|
+
* one thing a client can carry to `/save` to say "this is the world I decided
|
|
3623
|
+
* against" — and the only way the server can tell that somebody else has
|
|
3624
|
+
* published since the review screen was read.
|
|
3625
|
+
*
|
|
3626
|
+
* In `shared` because both sides need the SAME answer: the client names a head
|
|
3627
|
+
* and the server compares against one, so two implementations of "newest" is two
|
|
3628
|
+
* ways to disagree about whether a publish is stale.
|
|
3629
|
+
*
|
|
3630
|
+
* By `createdAt` rather than by position, because the list's order is the
|
|
3631
|
+
* content API's business and nothing here should depend on it. Ties keep the
|
|
3632
|
+
* first seen, which is arbitrary and does not matter: two commits sharing a
|
|
3633
|
+
* timestamp are equally "since", and either answer refuses the same publishes.
|
|
3634
|
+
*/
|
|
3635
|
+
function newestCommitSha(commits) {
|
|
3636
|
+
var _newest$commitSha, _newest;
|
|
3637
|
+
var newest = null;
|
|
3638
|
+
var _iterator = _createForOfIteratorHelper(commits !== null && commits !== void 0 ? commits : []),
|
|
3639
|
+
_step;
|
|
3640
|
+
try {
|
|
3641
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
3642
|
+
var commit = _step.value;
|
|
3643
|
+
if (newest === null || commit.createdAt > newest.createdAt) {
|
|
3644
|
+
newest = commit;
|
|
3645
|
+
}
|
|
3646
|
+
}
|
|
3647
|
+
} catch (err) {
|
|
3648
|
+
_iterator.e(err);
|
|
3649
|
+
} finally {
|
|
3650
|
+
_iterator.f();
|
|
3651
|
+
}
|
|
3652
|
+
return (_newest$commitSha = (_newest = newest) === null || _newest === void 0 ? void 0 : _newest.commitSha) !== null && _newest$commitSha !== void 0 ? _newest$commitSha : null;
|
|
3653
|
+
}
|
|
3654
|
+
|
|
3655
|
+
var VAL_CONFIG_SESSION_STORAGE_KEY = "val-config";
|
|
3656
|
+
var VAL_THEME_SESSION_STORAGE_KEY = "val-theme";
|
|
3657
|
+
var VAL_AI_SESSION_STORAGE_KEY = "val-ai-session";
|
|
3658
|
+
|
|
3659
|
+
var SharedValConfig = z.object({
|
|
3660
|
+
contentHostUrl: z.string().optional(),
|
|
3661
|
+
appHostUrl: z.string().optional(),
|
|
3662
|
+
project: z.string().optional(),
|
|
3663
|
+
root: z.string().optional(),
|
|
3664
|
+
gitCommit: z.string().optional(),
|
|
3665
|
+
gitBranch: z.string().optional(),
|
|
3666
|
+
defaultTheme: z["enum"](["dark", "light"]).optional(),
|
|
3667
|
+
ai: z.object({
|
|
3668
|
+
commitMessages: z.object({
|
|
3669
|
+
disabled: z["boolean"]().optional()
|
|
3670
|
+
}).optional()
|
|
3671
|
+
}).optional()
|
|
3672
|
+
});
|
|
3673
|
+
|
|
3674
|
+
/**
|
|
3675
|
+
* Config that is shared between the client and server.
|
|
3676
|
+
*/
|
|
3677
|
+
|
|
3678
|
+
var ValDeployment = z.object({
|
|
3679
|
+
deploymentId: z.string(),
|
|
3680
|
+
deploymentState: z.string(),
|
|
3681
|
+
commitSha: z.string(),
|
|
3682
|
+
updatedAt: z.string(),
|
|
3683
|
+
createdAt: z.string()
|
|
3684
|
+
});
|
|
3685
|
+
|
|
3686
|
+
// Strictly speaking this should be in the next package but it's shared, because we want to use it in the ui package. We want to resolve that somehow
|
|
3687
|
+
function getNextAppRouterSitemapTree(srcFolder, paths) {
|
|
3688
|
+
var root = {
|
|
3689
|
+
type: "node",
|
|
3690
|
+
name: "/",
|
|
3691
|
+
children: []
|
|
3692
|
+
};
|
|
3693
|
+
var _iterator = _createForOfIteratorHelper(paths),
|
|
3694
|
+
_step;
|
|
3695
|
+
try {
|
|
3696
|
+
var _loop = function _loop() {
|
|
3697
|
+
var path = _step.value;
|
|
3698
|
+
var urlPath = path.urlPath,
|
|
3699
|
+
moduleFilePathS = path.moduleFilePath;
|
|
3700
|
+
var moduleFilePath = moduleFilePathS;
|
|
3701
|
+
if (!urlPath.startsWith("/")) {
|
|
3702
|
+
console.error("urlPath must start with /: ".concat(urlPath));
|
|
3703
|
+
return 0; // continue
|
|
3704
|
+
}
|
|
3705
|
+
var pattern = getPatternFromModuleFilePath(moduleFilePath, srcFolder);
|
|
3706
|
+
if (urlPath === "/") {
|
|
3707
|
+
var fullPath = "/";
|
|
3708
|
+
root.pattern = getPatternFromModuleFilePath(moduleFilePath, srcFolder);
|
|
3709
|
+
root.page = {
|
|
3121
3710
|
fullPath: fullPath
|
|
3122
3711
|
};
|
|
3123
3712
|
root.sourcePath = Internal.joinModuleFilePathAndModulePath(moduleFilePath, "\"".concat(fullPath, "\""));
|
|
@@ -4078,13 +4667,21 @@ function checkGallerySchema(moduleSchema) {
|
|
|
4078
4667
|
};
|
|
4079
4668
|
}
|
|
4080
4669
|
function buildImageGalleryPatch(args, moduleSchema) {
|
|
4670
|
+
var _args$metadata$alt;
|
|
4081
4671
|
var wrong = checkGallerySchema(moduleSchema);
|
|
4082
4672
|
if (wrong) return wrong;
|
|
4083
4673
|
var remote = isRemoteSchema(moduleSchema);
|
|
4084
4674
|
var patch = [{
|
|
4085
4675
|
op: "add",
|
|
4086
4676
|
path: [args.filePath],
|
|
4087
|
-
value: _objectSpread2({}, args.metadata)
|
|
4677
|
+
value: _objectSpread2(_objectSpread2({}, args.metadata), {}, {
|
|
4678
|
+
// `alt` is a REQUIRED field of a gallery entry (`s.images()` gives it a
|
|
4679
|
+
// nullable string schema), so an entry written without one is invalid
|
|
4680
|
+
// the moment it lands — and the caller that had no alt to give is the
|
|
4681
|
+
// common case, not the odd one. Null is what the Studio's own upload
|
|
4682
|
+
// writes there.
|
|
4683
|
+
alt: (_args$metadata$alt = args.metadata.alt) !== null && _args$metadata$alt !== void 0 ? _args$metadata$alt : null
|
|
4684
|
+
})
|
|
4088
4685
|
}, {
|
|
4089
4686
|
op: "file",
|
|
4090
4687
|
path: [args.filePath],
|
|
@@ -4530,6 +5127,13 @@ function isSkippableFixCode(fix) {
|
|
|
4530
5127
|
// rewriting the module) and saving does not repair it either, so surface
|
|
4531
5128
|
// it: the message names the command (`val validate --fix`) that does.
|
|
4532
5129
|
return false;
|
|
5130
|
+
case "external:upload":
|
|
5131
|
+
// Same shape as `jsonValues:extract-entry` — an entry inlined in a
|
|
5132
|
+
// `.val.ts` that belongs elsewhere — and more emphatically not skippable.
|
|
5133
|
+
// Fixing it writes to a live store, which is why even the CLI does it only
|
|
5134
|
+
// under `val external upload` and never under a blanket `--fix`. Surfacing
|
|
5135
|
+
// it is the whole point: someone has to decide.
|
|
5136
|
+
return false;
|
|
4533
5137
|
default:
|
|
4534
5138
|
{
|
|
4535
5139
|
return false;
|
|
@@ -4569,4 +5173,702 @@ function filterBlockingValidationErrors(validationErrors, schemas, sources) {
|
|
|
4569
5173
|
return partitionValidationErrors(resolved).surfaced;
|
|
4570
5174
|
}
|
|
4571
5175
|
|
|
4572
|
-
|
|
5176
|
+
/**
|
|
5177
|
+
* Every decision `encode` makes, with none of the pixels.
|
|
5178
|
+
*
|
|
5179
|
+
* The re-encode itself happens in two places that share nothing: the Studio
|
|
5180
|
+
* runs it on a `<canvas>` before the upload leaves the browser
|
|
5181
|
+
* (`packages/ui/spa/utils/encodeImage.ts`), and the MCP image tool runs it on
|
|
5182
|
+
* `sharp` in the server process (`@valbuild/mcp`). What must NOT differ between
|
|
5183
|
+
* them is what `s.image({ encode })` means — which images are converted, how
|
|
5184
|
+
* far they are scaled, and when the original wins anyway. So the decisions live
|
|
5185
|
+
* here, once, and each side supplies only its own encoder.
|
|
5186
|
+
*
|
|
5187
|
+
* See `architecture/media.md`.
|
|
5188
|
+
*/
|
|
5189
|
+
|
|
5190
|
+
var ENCODE_DEFAULTS = {
|
|
5191
|
+
quality: 0.8,
|
|
5192
|
+
maxWidth: 2560,
|
|
5193
|
+
maxHeight: 2560
|
|
5194
|
+
};
|
|
5195
|
+
var ENCODE_MIME_TYPE_OF = {
|
|
5196
|
+
webp: "image/webp"
|
|
5197
|
+
};
|
|
5198
|
+
|
|
5199
|
+
/**
|
|
5200
|
+
* Formats an encoder cannot round-trip without losing something: SVG is vector,
|
|
5201
|
+
* an animated GIF keeps only its first frame, and AVIF is already smaller than
|
|
5202
|
+
* anything we would produce.
|
|
5203
|
+
*/
|
|
5204
|
+
var NEVER_ENCODED = ["image/svg+xml", "image/gif", "image/avif"];
|
|
5205
|
+
|
|
5206
|
+
/** A dimension bound the encoder can act on, else the default. */
|
|
5207
|
+
function usableBound(value, fallback) {
|
|
5208
|
+
return value !== undefined && Number.isFinite(value) && value > 0 ? value : fallback;
|
|
5209
|
+
}
|
|
5210
|
+
|
|
5211
|
+
/**
|
|
5212
|
+
* The schema's own option first, then the gallery it references, then off.
|
|
5213
|
+
*
|
|
5214
|
+
* Mirrors how `ImageField` resolves `accept` and `directory`. A gallery-backed
|
|
5215
|
+
* field (`s.image(galleryVal)`) serializes with EMPTY options, so without the
|
|
5216
|
+
* gallery fallback it could never inherit what the gallery asked for — and
|
|
5217
|
+
* `s.image(galleryVal, { encode: false })` is how such a field opts back out.
|
|
5218
|
+
*
|
|
5219
|
+
* Nonsense numbers fall back to the defaults rather than through. `fitWithin`
|
|
5220
|
+
* reads a bound of 0 (or NaN, or a negative) as "everything already fits", so
|
|
5221
|
+
* an unusable `maxWidth` would otherwise SILENTLY disable the downscale it was
|
|
5222
|
+
* asking for, and a `quality` outside 0–1 is ignored by `canvas.toBlob` just as
|
|
5223
|
+
* quietly. Falling back is deliberately not throwing: a schema is evaluated by
|
|
5224
|
+
* the CLI's `node:vm` sandbox and by the Next server, so a typo'd quality would
|
|
5225
|
+
* take a whole project down over an upload-time optimisation.
|
|
5226
|
+
*/
|
|
5227
|
+
function resolveEncodeSettings(fieldOption, galleryOption) {
|
|
5228
|
+
var option = fieldOption !== undefined ? fieldOption : galleryOption;
|
|
5229
|
+
if (!option) {
|
|
5230
|
+
return null;
|
|
5231
|
+
}
|
|
5232
|
+
var quality = option.quality;
|
|
5233
|
+
return {
|
|
5234
|
+
type: option.type,
|
|
5235
|
+
quality: quality !== undefined && Number.isFinite(quality) && quality > 0 ? Math.min(quality, 1) : ENCODE_DEFAULTS.quality,
|
|
5236
|
+
maxWidth: usableBound(option.maxWidth, ENCODE_DEFAULTS.maxWidth),
|
|
5237
|
+
maxHeight: usableBound(option.maxHeight, ENCODE_DEFAULTS.maxHeight)
|
|
5238
|
+
};
|
|
5239
|
+
}
|
|
5240
|
+
|
|
5241
|
+
/**
|
|
5242
|
+
* Dimensions scaled to fit inside the box, or null when the image already does.
|
|
5243
|
+
*
|
|
5244
|
+
* Null is the signal for "no downscale was needed", which the size comparison
|
|
5245
|
+
* in {@link chooseEncoded} turns on.
|
|
5246
|
+
*/
|
|
5247
|
+
function fitWithin(width, height, maxWidth, maxHeight) {
|
|
5248
|
+
if (!(width > 0) || !(height > 0) || !(maxWidth > 0) || !(maxHeight > 0) || width <= maxWidth && height <= maxHeight) {
|
|
5249
|
+
return null;
|
|
5250
|
+
}
|
|
5251
|
+
var scale = Math.min(maxWidth / width, maxHeight / height);
|
|
5252
|
+
return {
|
|
5253
|
+
// At least one pixel: a very wide, very short image would otherwise round
|
|
5254
|
+
// its height to zero, and a zero-sized canvas cannot be encoded.
|
|
5255
|
+
width: Math.max(1, Math.round(width * scale)),
|
|
5256
|
+
height: Math.max(1, Math.round(height * scale))
|
|
5257
|
+
};
|
|
5258
|
+
}
|
|
5259
|
+
|
|
5260
|
+
/**
|
|
5261
|
+
* Whether a schema that declares `accept` would take this mime type.
|
|
5262
|
+
*
|
|
5263
|
+
* The same rules as `ImageSchema.executeValidate`, which validates the STORED
|
|
5264
|
+
* mimeType against `accept` — so converting to a type `accept` forbids would
|
|
5265
|
+
* upload a file the schema reports as invalid the moment it lands. Delegated to
|
|
5266
|
+
* `@valbuild/core` rather than re-spelled, because a second reading of one
|
|
5267
|
+
* `accept` string is how a converted image passes here and fails there.
|
|
5268
|
+
*/
|
|
5269
|
+
function isMimeTypeAccepted(mimeType, accept) {
|
|
5270
|
+
if (!accept) {
|
|
5271
|
+
return true;
|
|
5272
|
+
}
|
|
5273
|
+
return Internal.mimeTypeMatchesAccept(mimeType, accept);
|
|
5274
|
+
}
|
|
5275
|
+
|
|
5276
|
+
/** Formats left alone: the lossy list, plus the target type when it already fits. */
|
|
5277
|
+
function isSkippedSource(sourceMimeType, targetMimeType, needsDownscale) {
|
|
5278
|
+
if (NEVER_ENCODED.includes(sourceMimeType)) {
|
|
5279
|
+
return true;
|
|
5280
|
+
}
|
|
5281
|
+
return sourceMimeType === targetMimeType && !needsDownscale;
|
|
5282
|
+
}
|
|
5283
|
+
|
|
5284
|
+
/**
|
|
5285
|
+
* Whether to upload what the encoder produced, or the file that was picked.
|
|
5286
|
+
*
|
|
5287
|
+
* Two ways the encoded bytes lose. They are not actually the type we asked for
|
|
5288
|
+
* — `canvas.toBlob` answers an unsupported type with a PNG rather than with
|
|
5289
|
+
* null, so "not null" is not the same question as "is a webp". Or they are
|
|
5290
|
+
* simply bigger, which happens for small and flat images: measured, an 8x8
|
|
5291
|
+
* solid PNG of 74 bytes becomes a 548 byte webp. A downscale overrides that,
|
|
5292
|
+
* since the original is then the wrong size whatever it weighs.
|
|
5293
|
+
*/
|
|
5294
|
+
function chooseEncoded(input) {
|
|
5295
|
+
if (input.encodedSize === null || input.encodedType !== input.targetMimeType) {
|
|
5296
|
+
return false;
|
|
5297
|
+
}
|
|
5298
|
+
if (input.needsDownscale) {
|
|
5299
|
+
return true;
|
|
5300
|
+
}
|
|
5301
|
+
return input.encodedSize < input.originalSize;
|
|
5302
|
+
}
|
|
5303
|
+
|
|
5304
|
+
/**
|
|
5305
|
+
* `photo.png` -> `photo.webp`. The extension is re-derived downstream too, but
|
|
5306
|
+
* the name should not lie.
|
|
5307
|
+
*/
|
|
5308
|
+
function withExtension(filename, ext) {
|
|
5309
|
+
var base = filename.split(".").slice(0, -1).join(".") || filename;
|
|
5310
|
+
return "".concat(base, ".").concat(ext);
|
|
5311
|
+
}
|
|
5312
|
+
|
|
5313
|
+
/**
|
|
5314
|
+
* The one way to build a child's source path.
|
|
5315
|
+
*
|
|
5316
|
+
* There were five hand-rolled versions of this, three of them subtly different
|
|
5317
|
+
* and two of them wrong, and the wrong ones were wrong in a way that hid: a
|
|
5318
|
+
* module path segment has to be JSON-quoted if it is a string and bare if it is
|
|
5319
|
+
* an array index, and both broken copies appended the raw key at the module
|
|
5320
|
+
* root. An unquoted key parses fine as long as it contains no `.` — so every
|
|
5321
|
+
* object field and every ordinary record key still resolved, and only a key with
|
|
5322
|
+
* a dot in it broke. That is exactly what a media gallery is keyed by, so
|
|
5323
|
+
* clicking a file in the Media panel navigated to
|
|
5324
|
+
* `?p=/public/val/red-8x8_bfbd0.png`, which `splitModulePath` reads as the two
|
|
5325
|
+
* segments `/public/val/red-8x8_bfbd0` and `png`, and the studio said the module
|
|
5326
|
+
* was not found.
|
|
5327
|
+
*
|
|
5328
|
+
* So: one function, and never a template string. `Internal.createValPathOfItem`
|
|
5329
|
+
* is the rule itself — `JSON.stringify` quotes a string and leaves a number
|
|
5330
|
+
* bare, which is precisely the module-path grammar — and everything here defers
|
|
5331
|
+
* to it rather than restating it.
|
|
5332
|
+
*/
|
|
5333
|
+
function sourcePathOfChild(parent, key) {
|
|
5334
|
+
/**
|
|
5335
|
+
* A parent that is already `…?p=` gets the key with no separator.
|
|
5336
|
+
*
|
|
5337
|
+
* The search index walks from `moduleFilePath + "?p="` rather than from the
|
|
5338
|
+
* module file path, so that every path it produces has a module path — and
|
|
5339
|
+
* `createValPathOfItem` would see the separator and join with a `.`, giving
|
|
5340
|
+
* `?p=."title"`.
|
|
5341
|
+
*/
|
|
5342
|
+
if (parent.endsWith(Internal.ModuleFilePathSep)) {
|
|
5343
|
+
return "".concat(parent).concat(JSON.stringify(key));
|
|
5344
|
+
}
|
|
5345
|
+
var path = Internal.createValPathOfItem(parent, key);
|
|
5346
|
+
if (path === undefined) {
|
|
5347
|
+
// Only when the parent is empty, which callers cannot produce: every walk
|
|
5348
|
+
// starts from a module file path. Thrown rather than skipped so a future
|
|
5349
|
+
// change that breaks that assumption is loud.
|
|
5350
|
+
throw new Error("Could not build a source path for '".concat(key, "' under ").concat(parent));
|
|
5351
|
+
}
|
|
5352
|
+
return path;
|
|
5353
|
+
}
|
|
5354
|
+
|
|
5355
|
+
/**
|
|
5356
|
+
* The same thing, for callers holding the module file path and module path apart.
|
|
5357
|
+
*
|
|
5358
|
+
* `ModulePath` is empty at the module root, which is the case both broken copies
|
|
5359
|
+
* got wrong: there is no separator in the path yet, so the key is the first
|
|
5360
|
+
* segment and still has to be quoted.
|
|
5361
|
+
*/
|
|
5362
|
+
function concatModulePath(moduleFilePath, modulePath, key) {
|
|
5363
|
+
return sourcePathOfChild(modulePath ? Internal.joinModuleFilePathAndModulePath(moduleFilePath, modulePath) : moduleFilePath, key);
|
|
5364
|
+
}
|
|
5365
|
+
|
|
5366
|
+
/**
|
|
5367
|
+
* Traverses a schema and source pair, calling a callback for each leaf node.
|
|
5368
|
+
* Handles all schema types including primitives, objects, arrays, records, unions, richtext, files, images, etc.
|
|
5369
|
+
*
|
|
5370
|
+
* @param source - The JSON source data
|
|
5371
|
+
* @param schema - The serialized schema
|
|
5372
|
+
* @param path - The current source path
|
|
5373
|
+
* @param callback - Function called for each traversable node with { source, schema, path }
|
|
5374
|
+
*/
|
|
5375
|
+
function traverseSchemaSource(source, schema, path, callback) {
|
|
5376
|
+
if (!(schema !== null && schema !== void 0 && schema.type)) {
|
|
5377
|
+
throw new Error("Schema not found for " + path);
|
|
5378
|
+
}
|
|
5379
|
+
if (source === null) {
|
|
5380
|
+
return;
|
|
5381
|
+
}
|
|
5382
|
+
|
|
5383
|
+
// An un-loaded `.jsonValues()` entry is an opaque `{_type:"json"}` marker, not
|
|
5384
|
+
// the value its schema describes. Skip it — its content is traversed once
|
|
5385
|
+
// loaded, since the sync engine substitutes it in place of the marker.
|
|
5386
|
+
//
|
|
5387
|
+
// Without this the marker falls through to whichever branch its ITEM schema
|
|
5388
|
+
// selects: an object/record item walks the marker's OWN keys, so an entry
|
|
5389
|
+
// schema with a field named `_type` or `patch_id` would index the marker's
|
|
5390
|
+
// internals as if they were content.
|
|
5391
|
+
if (Internal.isJson(source)) {
|
|
5392
|
+
return;
|
|
5393
|
+
}
|
|
5394
|
+
|
|
5395
|
+
// Handle primitives
|
|
5396
|
+
if (schema.type === "string") {
|
|
5397
|
+
if (typeof source === "string") {
|
|
5398
|
+
callback({
|
|
5399
|
+
source: source,
|
|
5400
|
+
schema: schema,
|
|
5401
|
+
path: path
|
|
5402
|
+
});
|
|
5403
|
+
}
|
|
5404
|
+
return;
|
|
5405
|
+
}
|
|
5406
|
+
if (schema.type === "number") {
|
|
5407
|
+
if (typeof source === "number") {
|
|
5408
|
+
callback({
|
|
5409
|
+
source: source,
|
|
5410
|
+
schema: schema,
|
|
5411
|
+
path: path
|
|
5412
|
+
});
|
|
5413
|
+
}
|
|
5414
|
+
return;
|
|
5415
|
+
}
|
|
5416
|
+
if (schema.type === "boolean") {
|
|
5417
|
+
if (typeof source === "boolean") {
|
|
5418
|
+
callback({
|
|
5419
|
+
source: source,
|
|
5420
|
+
schema: schema,
|
|
5421
|
+
path: path
|
|
5422
|
+
});
|
|
5423
|
+
}
|
|
5424
|
+
return;
|
|
5425
|
+
}
|
|
5426
|
+
|
|
5427
|
+
// Handle richtext - flatten and call callback
|
|
5428
|
+
if (schema.type === "richtext") {
|
|
5429
|
+
callback({
|
|
5430
|
+
source: source,
|
|
5431
|
+
schema: schema,
|
|
5432
|
+
path: path
|
|
5433
|
+
});
|
|
5434
|
+
return;
|
|
5435
|
+
}
|
|
5436
|
+
|
|
5437
|
+
// Handle file/image
|
|
5438
|
+
if (schema.type === "file" || schema.type === "image") {
|
|
5439
|
+
if (source && _typeof(source) === "object" && "path" in source && typeof source.path === "string") {
|
|
5440
|
+
callback({
|
|
5441
|
+
source: source,
|
|
5442
|
+
schema: schema,
|
|
5443
|
+
path: path
|
|
5444
|
+
});
|
|
5445
|
+
}
|
|
5446
|
+
return;
|
|
5447
|
+
}
|
|
5448
|
+
|
|
5449
|
+
// Handle date
|
|
5450
|
+
if (schema.type === "date") {
|
|
5451
|
+
if (typeof source === "string") {
|
|
5452
|
+
callback({
|
|
5453
|
+
source: source,
|
|
5454
|
+
schema: schema,
|
|
5455
|
+
path: path
|
|
5456
|
+
});
|
|
5457
|
+
}
|
|
5458
|
+
return;
|
|
5459
|
+
}
|
|
5460
|
+
|
|
5461
|
+
// Handle dateTime
|
|
5462
|
+
if (schema.type === "dateTime") {
|
|
5463
|
+
if (typeof source === "string") {
|
|
5464
|
+
callback({
|
|
5465
|
+
source: source,
|
|
5466
|
+
schema: schema,
|
|
5467
|
+
path: path
|
|
5468
|
+
});
|
|
5469
|
+
}
|
|
5470
|
+
return;
|
|
5471
|
+
}
|
|
5472
|
+
|
|
5473
|
+
// Handle color
|
|
5474
|
+
if (schema.type === "color") {
|
|
5475
|
+
if (typeof source === "string") {
|
|
5476
|
+
callback({
|
|
5477
|
+
source: source,
|
|
5478
|
+
schema: schema,
|
|
5479
|
+
path: path
|
|
5480
|
+
});
|
|
5481
|
+
}
|
|
5482
|
+
return;
|
|
5483
|
+
}
|
|
5484
|
+
|
|
5485
|
+
// Handle code
|
|
5486
|
+
if (schema.type === "code") {
|
|
5487
|
+
if (typeof source === "string") {
|
|
5488
|
+
callback({
|
|
5489
|
+
source: source,
|
|
5490
|
+
schema: schema,
|
|
5491
|
+
path: path
|
|
5492
|
+
});
|
|
5493
|
+
}
|
|
5494
|
+
return;
|
|
5495
|
+
}
|
|
5496
|
+
|
|
5497
|
+
// Handle keyOf
|
|
5498
|
+
if (schema.type === "keyOf") {
|
|
5499
|
+
if (typeof source === "string" || typeof source === "number") {
|
|
5500
|
+
callback({
|
|
5501
|
+
source: source,
|
|
5502
|
+
schema: schema,
|
|
5503
|
+
path: path
|
|
5504
|
+
});
|
|
5505
|
+
}
|
|
5506
|
+
return;
|
|
5507
|
+
}
|
|
5508
|
+
|
|
5509
|
+
// Handle literal
|
|
5510
|
+
if (schema.type === "literal") {
|
|
5511
|
+
callback({
|
|
5512
|
+
source: source,
|
|
5513
|
+
schema: schema,
|
|
5514
|
+
path: path
|
|
5515
|
+
});
|
|
5516
|
+
return;
|
|
5517
|
+
}
|
|
5518
|
+
|
|
5519
|
+
// Handle route
|
|
5520
|
+
if (schema.type === "route") {
|
|
5521
|
+
if (typeof source === "string") {
|
|
5522
|
+
callback({
|
|
5523
|
+
source: source,
|
|
5524
|
+
schema: schema,
|
|
5525
|
+
path: path
|
|
5526
|
+
});
|
|
5527
|
+
}
|
|
5528
|
+
return;
|
|
5529
|
+
}
|
|
5530
|
+
|
|
5531
|
+
// Handle array
|
|
5532
|
+
if (schema.type === "array") {
|
|
5533
|
+
if (!Array.isArray(source)) {
|
|
5534
|
+
return;
|
|
5535
|
+
}
|
|
5536
|
+
if (!schema.item) {
|
|
5537
|
+
throw new Error("Schema (" + schema.type + ") item not found for " + path);
|
|
5538
|
+
}
|
|
5539
|
+
for (var i = 0; i < source.length; i++) {
|
|
5540
|
+
var subPath = sourcePathOfChild(path, i);
|
|
5541
|
+
traverseSchemaSource(source[i], schema.item, subPath, callback);
|
|
5542
|
+
}
|
|
5543
|
+
return;
|
|
5544
|
+
}
|
|
5545
|
+
|
|
5546
|
+
// Handle object, record and settings
|
|
5547
|
+
if (schema.type === "object" || schema.type === "record" ||
|
|
5548
|
+
// Settings walks like an object, and starting from the SOURCE is what makes
|
|
5549
|
+
// its optional keys work: an absent section has nothing to traverse.
|
|
5550
|
+
schema.type === "settings") {
|
|
5551
|
+
if (_typeof(source) !== "object" || source === null || Array.isArray(source)) {
|
|
5552
|
+
return;
|
|
5553
|
+
}
|
|
5554
|
+
for (var key in source) {
|
|
5555
|
+
var _schema$items;
|
|
5556
|
+
var subSchema = schema.type === "record" ? schema.item : (_schema$items = schema.items) === null || _schema$items === void 0 ? void 0 : _schema$items[key];
|
|
5557
|
+
if (!subSchema) {
|
|
5558
|
+
continue;
|
|
5559
|
+
}
|
|
5560
|
+
var _subPath = sourcePathOfChild(path, key);
|
|
5561
|
+
traverseSchemaSource(source[key], subSchema, _subPath, callback);
|
|
5562
|
+
}
|
|
5563
|
+
return;
|
|
5564
|
+
}
|
|
5565
|
+
|
|
5566
|
+
// Handle union
|
|
5567
|
+
if (schema.type === "union") {
|
|
5568
|
+
var schemaKey = schema.key;
|
|
5569
|
+
if (typeof schemaKey === "string") {
|
|
5570
|
+
// Tagged union - find matching sub-schema
|
|
5571
|
+
if (source && _typeof(source) === "object" && !Array.isArray(source) && schemaKey in source) {
|
|
5572
|
+
var itemKey = source[schemaKey];
|
|
5573
|
+
if (typeof itemKey === "string") {
|
|
5574
|
+
var schemaOfItem = schema.items.filter(function (item) {
|
|
5575
|
+
return item.type === "object";
|
|
5576
|
+
}).find(function (item) {
|
|
5577
|
+
var itemKeySchema = item.items[schemaKey];
|
|
5578
|
+
if ((itemKeySchema === null || itemKeySchema === void 0 ? void 0 : itemKeySchema.type) === "literal") {
|
|
5579
|
+
return itemKeySchema.value === itemKey;
|
|
5580
|
+
}
|
|
5581
|
+
return false;
|
|
5582
|
+
});
|
|
5583
|
+
if (schemaOfItem) {
|
|
5584
|
+
traverseSchemaSource(source, schemaOfItem, path, callback);
|
|
5585
|
+
}
|
|
5586
|
+
}
|
|
5587
|
+
}
|
|
5588
|
+
} else {
|
|
5589
|
+
// Literal union - treat as primitive
|
|
5590
|
+
if (typeof source === "string") {
|
|
5591
|
+
callback({
|
|
5592
|
+
source: source,
|
|
5593
|
+
schema: schema,
|
|
5594
|
+
path: path
|
|
5595
|
+
});
|
|
5596
|
+
}
|
|
5597
|
+
}
|
|
5598
|
+
return;
|
|
5599
|
+
}
|
|
5600
|
+
|
|
5601
|
+
// Exhaustive check
|
|
5602
|
+
var exhaustiveCheck = schema;
|
|
5603
|
+
throw new Error("Unsupported schema type: " + JSON.stringify(exhaustiveCheck));
|
|
5604
|
+
}
|
|
5605
|
+
|
|
5606
|
+
/**
|
|
5607
|
+
* Flattens richtext source to a plain string, extracting only text content.
|
|
5608
|
+
* Used for indexing richtext content in search.
|
|
5609
|
+
*/
|
|
5610
|
+
function flattenRichText(source) {
|
|
5611
|
+
var res = "";
|
|
5612
|
+
function rec(child) {
|
|
5613
|
+
if (typeof child === "string") {
|
|
5614
|
+
res += child;
|
|
5615
|
+
} else {
|
|
5616
|
+
if (child && _typeof(child) === "object" && "children" in child && Array.isArray(child.children)) {
|
|
5617
|
+
var _iterator = _createForOfIteratorHelper(child.children),
|
|
5618
|
+
_step;
|
|
5619
|
+
try {
|
|
5620
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
5621
|
+
var c = _step.value;
|
|
5622
|
+
rec(c);
|
|
5623
|
+
}
|
|
5624
|
+
} catch (err) {
|
|
5625
|
+
_iterator.e(err);
|
|
5626
|
+
} finally {
|
|
5627
|
+
_iterator.f();
|
|
5628
|
+
}
|
|
5629
|
+
}
|
|
5630
|
+
}
|
|
5631
|
+
}
|
|
5632
|
+
rec({
|
|
5633
|
+
children: source
|
|
5634
|
+
});
|
|
5635
|
+
return res;
|
|
5636
|
+
}
|
|
5637
|
+
|
|
5638
|
+
/**
|
|
5639
|
+
* Resolves a file ref (local path or remote URL) to a clean local-style path.
|
|
5640
|
+
*/
|
|
5641
|
+
function cleanRefPath(ref) {
|
|
5642
|
+
var remoteRefRes = Internal.remote.splitRemoteRef(ref);
|
|
5643
|
+
return remoteRefRes.status === "success" ? "/".concat(remoteRefRes.filePath) : ref;
|
|
5644
|
+
}
|
|
5645
|
+
|
|
5646
|
+
/**
|
|
5647
|
+
* Extract a human-readable filename from a file ref (local path or remote URL).
|
|
5648
|
+
* Handles both remote refs (via splitRemoteRef) and plain `/public/...` paths.
|
|
5649
|
+
*/
|
|
5650
|
+
function getFilenameFromRef(ref) {
|
|
5651
|
+
var cleanPath = cleanRefPath(ref);
|
|
5652
|
+
return cleanPath.split("/").pop() || cleanPath;
|
|
5653
|
+
}
|
|
5654
|
+
|
|
5655
|
+
/**
|
|
5656
|
+
* Parse a file ref into its constituent parts: a clean path, filename, and
|
|
5657
|
+
* folder (the path relative to `/public`).
|
|
5658
|
+
*
|
|
5659
|
+
* The `folder` strips the standard `/public` prefix so it shows a
|
|
5660
|
+
* concise location like `/images` instead of `/public/images`.
|
|
5661
|
+
*/
|
|
5662
|
+
function getRefParts(ref) {
|
|
5663
|
+
var cleanPath = cleanRefPath(ref);
|
|
5664
|
+
var filename = cleanPath.split("/").pop() || cleanPath;
|
|
5665
|
+
// Only strip a leading "/public" path segment: "/publicity/a.png" must keep its folder
|
|
5666
|
+
var folder = cleanPath.replace(/^\/public(?=\/|$)/, "").replace(/\/[^/]+$/, "") || "/";
|
|
5667
|
+
return {
|
|
5668
|
+
cleanPath: cleanPath,
|
|
5669
|
+
filename: filename,
|
|
5670
|
+
folder: folder
|
|
5671
|
+
};
|
|
5672
|
+
}
|
|
5673
|
+
|
|
5674
|
+
/**
|
|
5675
|
+
* The search index and the labels to render for its hits.
|
|
5676
|
+
*
|
|
5677
|
+
* Kept as a plain module rather than living inside a worker so it can be tested:
|
|
5678
|
+
* a worker entry runs `self.onmessage` on import, which no test environment here
|
|
5679
|
+
* provides.
|
|
5680
|
+
*
|
|
5681
|
+
* In `@valbuild/shared` rather than the Studio because there are now two
|
|
5682
|
+
* realms searching the same content: the Studio's `SearchStore`, which keeps an
|
|
5683
|
+
* index alive in a worker and re-indexes a module at a time, and the MCP
|
|
5684
|
+
* `search_content` tool, which builds one per call and throws it away. What
|
|
5685
|
+
* counts as a document, and what a hit is labelled, must not differ between
|
|
5686
|
+
* them — an agent and an editor searching the same project should find the same
|
|
5687
|
+
* things.
|
|
5688
|
+
*/
|
|
5689
|
+
|
|
5690
|
+
/** An index with nothing in it. Fill it with {@link indexModule}. */
|
|
5691
|
+
function createSearchIndex() {
|
|
5692
|
+
return {
|
|
5693
|
+
index: new FlexSearch.Index({
|
|
5694
|
+
tokenize: "forward"
|
|
5695
|
+
}),
|
|
5696
|
+
pathToLabel: new Map(),
|
|
5697
|
+
docsByModule: new Map()
|
|
5698
|
+
};
|
|
5699
|
+
}
|
|
5700
|
+
|
|
5701
|
+
/**
|
|
5702
|
+
* Index one module, replacing whatever was previously indexed for it.
|
|
5703
|
+
*
|
|
5704
|
+
* Remove-then-add rather than `update`, because the set of paths in a module is
|
|
5705
|
+
* not stable: deleting an array item or a record entry means documents that must
|
|
5706
|
+
* disappear, and `update` only revises ids it is given.
|
|
5707
|
+
*
|
|
5708
|
+
* `.jsonValues()` entries are indexed only once their content is loaded — an
|
|
5709
|
+
* un-loaded entry is an opaque marker that {@link traverseSchemaSource} skips, so
|
|
5710
|
+
* the index is naturally PARTIAL and grows as batches land. That is why the search
|
|
5711
|
+
* UI loads the entries on the first query and re-indexes (debounced) as they
|
|
5712
|
+
* arrive, rather than pretending an empty result set is an answer.
|
|
5713
|
+
*/
|
|
5714
|
+
function indexModule(searchIndex, moduleFilePath, source, schema) {
|
|
5715
|
+
var index = searchIndex.index,
|
|
5716
|
+
pathToLabel = searchIndex.pathToLabel,
|
|
5717
|
+
docsByModule = searchIndex.docsByModule;
|
|
5718
|
+
removeModule(searchIndex, moduleFilePath);
|
|
5719
|
+
var docs = new Set();
|
|
5720
|
+
docsByModule.set(moduleFilePath, docs);
|
|
5721
|
+
var path = moduleFilePath + "?p=";
|
|
5722
|
+
traverseSchemaSource(source, schema, path, function (_ref) {
|
|
5723
|
+
var source = _ref.source,
|
|
5724
|
+
schema = _ref.schema,
|
|
5725
|
+
path = _ref.path;
|
|
5726
|
+
if (source === null) {
|
|
5727
|
+
return;
|
|
5728
|
+
}
|
|
5729
|
+
var searchText = "";
|
|
5730
|
+
var label = "";
|
|
5731
|
+
|
|
5732
|
+
// Handle primitives
|
|
5733
|
+
if (schema.type === "string" || schema.type === "number" || schema.type === "boolean" || schema.type === "date" || schema.type === "dateTime" || schema.type === "color" || schema.type === "code" || schema.type === "keyOf" || schema.type === "route") {
|
|
5734
|
+
var _source$toString, _source$toString2;
|
|
5735
|
+
searchText = (_source$toString = source === null || source === void 0 ? void 0 : source.toString()) !== null && _source$toString !== void 0 ? _source$toString : "";
|
|
5736
|
+
label = (_source$toString2 = source === null || source === void 0 ? void 0 : source.toString()) !== null && _source$toString2 !== void 0 ? _source$toString2 : "";
|
|
5737
|
+
} else if (schema.type === "literal") {
|
|
5738
|
+
searchText = schema.value.toString();
|
|
5739
|
+
label = schema.value.toString();
|
|
5740
|
+
}
|
|
5741
|
+
// Handle richtext - flatten to get text content
|
|
5742
|
+
else if (schema.type === "richtext") {
|
|
5743
|
+
searchText = flattenRichText(source);
|
|
5744
|
+
// Use first 50 chars as label
|
|
5745
|
+
label = searchText.substring(0, 50) || "richtext";
|
|
5746
|
+
}
|
|
5747
|
+
// Handle file/image
|
|
5748
|
+
else if (schema.type === "file" || schema.type === "image") {
|
|
5749
|
+
if (source !== null && _typeof(source) === "object" && "path" in source && typeof source.path === "string") {
|
|
5750
|
+
// The label is the bare filename - the folder is shown separately in
|
|
5751
|
+
// the UI - but both are searchable.
|
|
5752
|
+
var _getRefParts = getRefParts(source.path),
|
|
5753
|
+
filename = _getRefParts.filename,
|
|
5754
|
+
folder = _getRefParts.folder;
|
|
5755
|
+
var alt = "alt" in source ? source.alt : "";
|
|
5756
|
+
searchText = filename + " " + folder + " " + alt;
|
|
5757
|
+
label = filename;
|
|
5758
|
+
}
|
|
5759
|
+
}
|
|
5760
|
+
|
|
5761
|
+
// Add to index if we have search text
|
|
5762
|
+
if (searchText) {
|
|
5763
|
+
var _Internal$splitModule = Internal.splitModuleFilePathAndModulePath(path),
|
|
5764
|
+
_Internal$splitModule2 = _slicedToArray(_Internal$splitModule, 2),
|
|
5765
|
+
modulePath = _Internal$splitModule2[1];
|
|
5766
|
+
var cleanPath = fastRemoveNonWordChars(modulePath) + " ";
|
|
5767
|
+
index.add(path, cleanPath + " " + path + " " + searchText);
|
|
5768
|
+
pathToLabel.set(path, label);
|
|
5769
|
+
docs.add(path);
|
|
5770
|
+
}
|
|
5771
|
+
});
|
|
5772
|
+
}
|
|
5773
|
+
|
|
5774
|
+
/**
|
|
5775
|
+
* Drop everything indexed for one module.
|
|
5776
|
+
*
|
|
5777
|
+
* Also used for a module that has gone away entirely — otherwise its documents
|
|
5778
|
+
* stay searchable and a hit navigates to a path that no longer exists.
|
|
5779
|
+
*/
|
|
5780
|
+
function removeModule(searchIndex, moduleFilePath) {
|
|
5781
|
+
var index = searchIndex.index,
|
|
5782
|
+
pathToLabel = searchIndex.pathToLabel,
|
|
5783
|
+
docsByModule = searchIndex.docsByModule;
|
|
5784
|
+
var existing = docsByModule.get(moduleFilePath);
|
|
5785
|
+
if (existing === undefined) return;
|
|
5786
|
+
var _iterator = _createForOfIteratorHelper(existing),
|
|
5787
|
+
_step;
|
|
5788
|
+
try {
|
|
5789
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
5790
|
+
var id = _step.value;
|
|
5791
|
+
index.remove(id);
|
|
5792
|
+
pathToLabel["delete"](id);
|
|
5793
|
+
}
|
|
5794
|
+
} catch (err) {
|
|
5795
|
+
_iterator.e(err);
|
|
5796
|
+
} finally {
|
|
5797
|
+
_iterator.f();
|
|
5798
|
+
}
|
|
5799
|
+
docsByModule["delete"](moduleFilePath);
|
|
5800
|
+
}
|
|
5801
|
+
|
|
5802
|
+
/**
|
|
5803
|
+
* Index every module, from scratch.
|
|
5804
|
+
*
|
|
5805
|
+
* Now a loop over {@link indexModule} rather than its own walk, so the full
|
|
5806
|
+
* build and the incremental one cannot drift in what they consider a document.
|
|
5807
|
+
*/
|
|
5808
|
+
function buildSearchIndex(modules) {
|
|
5809
|
+
var searchIndex = createSearchIndex();
|
|
5810
|
+
for (var moduleFilePathS in modules) {
|
|
5811
|
+
var moduleFilePath = moduleFilePathS;
|
|
5812
|
+
var _modules$moduleFilePa = modules[moduleFilePath],
|
|
5813
|
+
source = _modules$moduleFilePa.source,
|
|
5814
|
+
schema = _modules$moduleFilePa.schema;
|
|
5815
|
+
indexModule(searchIndex, moduleFilePath, source, schema);
|
|
5816
|
+
}
|
|
5817
|
+
return searchIndex;
|
|
5818
|
+
}
|
|
5819
|
+
var NON_CHARS = new Set(["/", "-", "_", ".", ":", "?", "&", "=", "@", '"', "'"]);
|
|
5820
|
+
function fastRemoveNonWordChars(str) {
|
|
5821
|
+
var result = "";
|
|
5822
|
+
for (var i = 0; i < str.length; i++) {
|
|
5823
|
+
var _char = str[i];
|
|
5824
|
+
if (!NON_CHARS.has(_char)) {
|
|
5825
|
+
result += _char;
|
|
5826
|
+
} else {
|
|
5827
|
+
result += " ";
|
|
5828
|
+
}
|
|
5829
|
+
}
|
|
5830
|
+
return result;
|
|
5831
|
+
}
|
|
5832
|
+
|
|
5833
|
+
/**
|
|
5834
|
+
* How many hits are counted before `total` stops being a count.
|
|
5835
|
+
*
|
|
5836
|
+
* FlexSearch stops as soon as it has the number of ids it was asked for, so a
|
|
5837
|
+
* `total` taken from a page-sized search is just the page size again — which
|
|
5838
|
+
* reads as "that is all there is" to anyone who did not write it. Counting is
|
|
5839
|
+
* asking for more ids than the page needs, so it costs an array of ids and no
|
|
5840
|
+
* more; this bound is where that stops being free, and `totalIsLowerBound` says
|
|
5841
|
+
* it was reached rather than letting the number quietly lie.
|
|
5842
|
+
*/
|
|
5843
|
+
var MAX_COUNTED_RESULTS = 10000;
|
|
5844
|
+
function performSearch(searchIndex, query) {
|
|
5845
|
+
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 50;
|
|
5846
|
+
var offset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
|
5847
|
+
if (searchIndex === null || !query.trim()) {
|
|
5848
|
+
return {
|
|
5849
|
+
results: [],
|
|
5850
|
+
total: 0,
|
|
5851
|
+
totalIsLowerBound: false
|
|
5852
|
+
};
|
|
5853
|
+
}
|
|
5854
|
+
var index = searchIndex.index,
|
|
5855
|
+
pathToLabel = searchIndex.pathToLabel;
|
|
5856
|
+
var counted = Math.max(offset + limit, MAX_COUNTED_RESULTS);
|
|
5857
|
+
var searchResults = index.search(query, {
|
|
5858
|
+
limit: counted
|
|
5859
|
+
});
|
|
5860
|
+
var total = searchResults.length;
|
|
5861
|
+
var paged = searchResults.slice(offset, offset + limit);
|
|
5862
|
+
return {
|
|
5863
|
+
results: paged.map(function (id) {
|
|
5864
|
+
return {
|
|
5865
|
+
path: id,
|
|
5866
|
+
label: pathToLabel.get(id) || id
|
|
5867
|
+
};
|
|
5868
|
+
}),
|
|
5869
|
+
total: total,
|
|
5870
|
+
totalIsLowerBound: total >= counted
|
|
5871
|
+
};
|
|
5872
|
+
}
|
|
5873
|
+
|
|
5874
|
+
export { Api, BinaryFileRef, CodeOptions, ColorOptions, DEFAULT_ERROR_MESSAGE, DateOptions, ENCODE_DEFAULTS, ENCODE_MIME_TYPE_OF, FileOptions, HistoricalCommit, HistoricalModule, HistoricalPatchSet, HistoryError, ImageEncodeOption, ImageOptions, JSONValue, JSON_ENTRIES_BATCH_MAX, ModuleComparison, NEVER_ENCODED, ParentRef$1 as ParentRef, Patch, PatchBlock, PatchGroup, PatchId, PatchJSON, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, RestoreVerdict, RichTextOptions, SerializedArraySchema, SerializedBooleanSchema, SerializedCodeSchema, SerializedColorSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedFileSchema, SerializedImageSchema, SerializedKeyOfSchema, SerializedLiteralSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedRecordSchema, SerializedRichTextSchema, SerializedRouteSchema, SerializedSchema, SerializedSettingsSchema, SerializedStringSchema, SerializedUnionSchema, SharedValConfig, VAL_AI_SESSION_STORAGE_KEY, VAL_CANVAS_MESSAGE, VAL_CANVAS_PARAM, VAL_CONFIG_SESSION_STORAGE_KEY, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, VAL_THEME_SESSION_STORAGE_KEY, ValCommit, ValDeployment, buildDuplicatePatch, buildEmptyAtPathPatch, buildFileRefValue, buildImageGalleryPatch, buildRemoveImageGalleryEntryPatch, buildSearchIndex, checkCompatibility, chooseEncoded, concatModulePath, createRegExpFromPattern, createSearchIndex, createValClient, describeContainerAtPath, emptyOf, explainIncompatible, filterBlockingValidationErrors, filterRoutesByPatterns, findSimilar, fitWithin, flattenRichText, getErrorMessageFromUnknownJson, getFilenameFromRef, getInlineImgInfo, getNextAppRouterSitemapTree, getNextAppRouterSourceFolder, getPatternFromModuleFilePath, getRefParts, getRoutesOf, getRoutesWithModulePaths, getSourceAt, indexModule, isMimeTypeAccepted, isRemoteSchema, isSkippedSource, isValCanvasFrame, isValCanvasPageMessage, isValCanvasStudioMessage, levenshtein, newestCommitSha, parseRoutePattern, partitionValidationErrors, performSearch, remirrorToRichTextSource, removeModule, resolveEncodeSettings, resolveSchemaSourceFixForError, resolveSchemaSourceFixes, resolveSerializedSchemaAtPath, richTextToRemirror, safeParsePatch, sourcePathOfChild, toolNames, traverseSchemaSource, urlOf, validateRoutePatterns, withExtension, withValCanvasParam };
|