@mulmoclaude/core 3.6.0 → 3.7.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/dist/collection/registry/server/index.cjs +19 -19
- package/dist/collection/registry/server/index.cjs.map +1 -1
- package/dist/collection/registry/server/index.js +2 -2
- package/dist/collection/server/host.d.ts +11 -0
- package/dist/collection/server/index.cjs +72 -59
- package/dist/collection/server/index.d.ts +4 -0
- package/dist/collection/server/index.js +3 -3
- package/dist/collection/server/manageTool.d.ts +4 -0
- package/dist/collection/server/publish.d.ts +56 -0
- package/dist/collection/server/publishChecks.d.ts +29 -0
- package/dist/collection/server/publishManifest.d.ts +181 -0
- package/dist/collection/server/publishProject.d.ts +86 -0
- package/dist/collection/server/validate.d.ts +12 -0
- package/dist/collection-watchers/index.cjs +15 -15
- package/dist/collection-watchers/index.cjs.map +1 -1
- package/dist/collection-watchers/index.js +2 -2
- package/dist/feeds/server/index.cjs +10 -10
- package/dist/feeds/server/index.cjs.map +1 -1
- package/dist/feeds/server/index.js +2 -2
- package/dist/google/index.cjs +12 -12
- package/dist/google/index.cjs.map +1 -1
- package/dist/google/index.js +1 -1
- package/dist/{server-DdHaX_Uu.js → server-B48Jyxcj.js} +1079 -228
- package/dist/server-B48Jyxcj.js.map +1 -0
- package/dist/{server-9mFaGLdq.cjs → server-CWZyg8fn.cjs} +1318 -389
- package/dist/server-CWZyg8fn.cjs.map +1 -0
- package/dist/{discovery-Cw5xWzZD.cjs → store-5_P_NsGa.cjs} +1947 -1947
- package/dist/store-5_P_NsGa.cjs.map +1 -0
- package/dist/{discovery-CdkaURVY.js → store-_61sO8K8.js} +1948 -1948
- package/dist/store-_61sO8K8.js.map +1 -0
- package/dist/whisper/index.cjs +1 -1
- package/dist/whisper/index.js +1 -1
- package/package.json +1 -1
- package/dist/discovery-CdkaURVY.js.map +0 -1
- package/dist/discovery-Cw5xWzZD.cjs.map +0 -1
- package/dist/server-9mFaGLdq.cjs.map +0 -1
- package/dist/server-DdHaX_Uu.js.map +0 -1
|
@@ -7,9 +7,9 @@ import { readFileSync, realpathSync, watch } from "node:fs";
|
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { createHash, randomBytes } from "node:crypto";
|
|
9
9
|
import { lstat, mkdir, open, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
10
|
+
import { z } from "zod";
|
|
10
11
|
import { tmpdir } from "node:os";
|
|
11
12
|
import iconv from "iconv-lite";
|
|
12
|
-
import { z } from "zod";
|
|
13
13
|
//#region src/host/hostSlot.ts
|
|
14
14
|
function createHostSlot(name) {
|
|
15
15
|
let current = null;
|
|
@@ -606,1541 +606,363 @@ function appManifestReason(failure, root) {
|
|
|
606
606
|
if (failure.kind === "unreadable") return `cannot read ${manifestPath}: ${failure.detail}`;
|
|
607
607
|
return `${manifestPath} ${failure.detail}`;
|
|
608
608
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
*
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
* failure so the caller's "missing" branch covers those cases too.
|
|
617
|
-
* Exported so `ontology.ts`'s record COUNT classifies entries with the
|
|
618
|
-
* SAME lstat logic — the two must agree on what a record file is. */
|
|
619
|
-
async function isRegularFile(filePath) {
|
|
620
|
-
try {
|
|
621
|
-
return (await lstat(filePath)).isFile();
|
|
622
|
-
} catch {
|
|
623
|
-
return false;
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
/** Read one JSON record file. Returns null when the file is missing,
|
|
627
|
-
* is a symlink (file-disclosure defense), parses to a non-object,
|
|
628
|
-
* or has a read/parse error. Caller logs the per-entry skip — this
|
|
629
|
-
* helper just classifies. Split out to keep `listItems` under the
|
|
630
|
-
* `sonarjs/cognitive-complexity` threshold. */
|
|
631
|
-
/** Parse a record file's text into a plain-object `CollectionItem`, or
|
|
632
|
-
* null when it isn't a JSON object (array / scalar / null). */
|
|
633
|
-
function parseRecordJson(raw) {
|
|
634
|
-
const parsed = JSON.parse(raw);
|
|
635
|
-
return isRecord(parsed) ? parsed : null;
|
|
636
|
-
}
|
|
637
|
-
async function tryReadRecord(filePath) {
|
|
638
|
-
if (!await isRegularFile(filePath)) return null;
|
|
639
|
-
try {
|
|
640
|
-
return parseRecordJson(await readFile(filePath, "utf-8"));
|
|
641
|
-
} catch {
|
|
642
|
-
return null;
|
|
643
|
-
}
|
|
609
|
+
/** The param name a `set` value references, or null when the value is a
|
|
610
|
+
* literal (non-strings can never be references). A bare/empty prefix
|
|
611
|
+
* (`"$params."`) returns the empty string — the schema refine rejects
|
|
612
|
+
* it as an undeclared param, never silently treats it as a literal. */
|
|
613
|
+
function paramRefName(value) {
|
|
614
|
+
if (typeof value !== "string" || !value.startsWith("$params.")) return null;
|
|
615
|
+
return value.slice(8);
|
|
644
616
|
}
|
|
645
|
-
/**
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
let entries;
|
|
657
|
-
try {
|
|
658
|
-
entries = await readdir(dataDir);
|
|
659
|
-
} catch (err) {
|
|
660
|
-
if (isErrorWithCode(err) && err.code === "ENOENT") return [];
|
|
661
|
-
throw err;
|
|
662
|
-
}
|
|
663
|
-
const results = [];
|
|
664
|
-
for (const name of entries) {
|
|
665
|
-
if (!name.endsWith(".json")) continue;
|
|
666
|
-
if (name.startsWith(".")) continue;
|
|
667
|
-
const filePath = path.join(dataDir, name);
|
|
668
|
-
const record = await tryReadRecord(filePath);
|
|
669
|
-
if (record === null) {
|
|
670
|
-
log.warn("collections", "skipping record (missing, symlink, or unreadable)", { path: filePath });
|
|
617
|
+
/** Resolve a mutate action's `set` map against the submitted params:
|
|
618
|
+
* literals pass through, `$params.<name>` reads the param value. An
|
|
619
|
+
* ABSENT referenced param omits the key entirely (merge semantics —
|
|
620
|
+
* the stored value survives), mirroring how the record form omits
|
|
621
|
+
* empty optionals rather than writing empty strings. */
|
|
622
|
+
function resolveMutateSet(set, params) {
|
|
623
|
+
const resolved = {};
|
|
624
|
+
for (const [key, value] of Object.entries(set)) {
|
|
625
|
+
const ref = paramRefName(value);
|
|
626
|
+
if (ref === null) {
|
|
627
|
+
resolved[key] = value;
|
|
671
628
|
continue;
|
|
672
629
|
}
|
|
673
|
-
|
|
630
|
+
const paramValue = params[ref];
|
|
631
|
+
if (paramValue !== void 0 && paramValue !== null && paramValue !== "") resolved[key] = paramValue;
|
|
674
632
|
}
|
|
675
|
-
return
|
|
633
|
+
return resolved;
|
|
676
634
|
}
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
635
|
+
//#endregion
|
|
636
|
+
//#region src/collection/core/schemaRules.ts
|
|
637
|
+
var declaredField = (fields, name) => Object.hasOwn(fields, name) ? fields[name] : void 0;
|
|
638
|
+
var isDateLike = (type) => type === "date" || type === "datetime";
|
|
639
|
+
var isTimeStringField = (type) => type === "string" || type === "text";
|
|
640
|
+
var CODE_FIELD_TYPES = /* @__PURE__ */ new Set([
|
|
641
|
+
"string",
|
|
642
|
+
"text",
|
|
643
|
+
"enum"
|
|
644
|
+
]);
|
|
645
|
+
var namesStoredField = (fields, name, primaryKey) => {
|
|
646
|
+
const target = declaredField(fields, name);
|
|
647
|
+
return target !== void 0 && !COMPUTED_TYPES.has(target.type) && name !== primaryKey;
|
|
648
|
+
};
|
|
649
|
+
var hasUniqueIds = (entries) => entries === void 0 || new Set(entries.map((entry) => entry.id)).size === entries.length;
|
|
650
|
+
/** Exactly one storage declaration: native records need `dataPath`, an external
|
|
651
|
+
* data file needs `dataSource`, an alternative backend needs `storage`. Zero
|
|
652
|
+
* (nowhere to read) and several (ambiguous which wins) are equally
|
|
653
|
+
* meaningless — fail loudly at load instead of picking silently. */
|
|
654
|
+
function declaresExactlyOneStore(schema) {
|
|
655
|
+
return [
|
|
656
|
+
schema.dataPath,
|
|
657
|
+
schema.dataSource,
|
|
658
|
+
schema.storage
|
|
659
|
+
].filter((declared) => declared !== void 0).length === 1;
|
|
693
660
|
}
|
|
694
|
-
/**
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
*
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
* a symlink between this call and the `mkdir` / `open` / `unlink` that
|
|
707
|
-
* follows. Closing that needs directory-handle I/O anchored at the workspace
|
|
708
|
-
* (`openat` + `O_NOFOLLOW`), which `node:fs` does not expose — it would mean
|
|
709
|
-
* a different I/O layer, not a tighter check here.
|
|
710
|
-
*
|
|
711
|
-
* That race is deliberately outside this app's threat model: the process is
|
|
712
|
-
* loopback-bound and bearer-authed, so anyone able to swap directories inside
|
|
713
|
-
* the workspace is already the workspace owner — the same trust principal the
|
|
714
|
-
* writes belong to. Revisit if collections ever serve a lower-trust caller. */
|
|
715
|
-
function escapesWorkspace(dataDir, workspaceRoot, itemId, stage) {
|
|
716
|
-
if (isContainedInRoot(dataDir, workspaceRoot)) return false;
|
|
717
|
-
log.warn("collections", `${stage} refused: dataDir escapes workspace via symlink`, {
|
|
718
|
-
dataDir,
|
|
719
|
-
itemId
|
|
720
|
-
});
|
|
661
|
+
/** A `dataSource` collection is read-only by definition, so schema-level write
|
|
662
|
+
* machinery can never fire: `singleton` pins CREATES, `ingest` REFILLS
|
|
663
|
+
* records, `spawn` WRITES successor records. Rejecting them at validation
|
|
664
|
+
* kills whole classes of writes before any runtime guard. */
|
|
665
|
+
function dataSourceDeclaresNoWriteMachinery(schema) {
|
|
666
|
+
if (schema.dataSource === void 0) return true;
|
|
667
|
+
return schema.singleton === void 0 && schema.ingest === void 0 && schema.spawn === void 0 && schema.googleCalendar === void 0;
|
|
668
|
+
}
|
|
669
|
+
/** Same rule for declarative host writes: a mutate action writes the record
|
|
670
|
+
* it's invoked on, which a read-only collection has no business doing. */
|
|
671
|
+
function dataSourceDeclaresNoMutateAction(schema) {
|
|
672
|
+
if (schema.dataSource !== void 0) return [...schema.actions ?? [], ...schema.collectionActions ?? []].every((action) => action.kind !== "mutate");
|
|
721
673
|
return true;
|
|
722
674
|
}
|
|
723
|
-
/**
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
* Create path (`refuseOverwrite: true`) uses an O_EXCL `wx` open
|
|
727
|
-
* rather than `stat` + `writeFileAtomic` to close a check-then-write
|
|
728
|
-
* race: two concurrent POSTs would otherwise both pass the existence
|
|
729
|
-
* check and one would silently overwrite the other. The trade-off
|
|
730
|
-
* is that the create path is not crash-atomic (a partial file could
|
|
731
|
-
* remain if the process dies mid-write); acceptable here because
|
|
732
|
-
* records are small JSON blobs and the next read either parses or
|
|
733
|
-
* is skipped via the "malformed JSON" branch in `listItems`.
|
|
734
|
-
*
|
|
735
|
-
* Update path (`refuseOverwrite: false`) uses `writeFileAtomic` so
|
|
736
|
-
* PUT remains crash-atomic. No race there — the URL pins the id. */
|
|
737
|
-
async function writeItem(dataDir, itemId, item, opts = {}) {
|
|
738
|
-
const safeId = safeRecordId(itemId);
|
|
739
|
-
if (safeId === null) return {
|
|
740
|
-
kind: "invalid-id",
|
|
741
|
-
itemId
|
|
742
|
-
};
|
|
743
|
-
const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();
|
|
744
|
-
if (escapesWorkspace(dataDir, workspaceRoot, safeId, "writeItem (pre-mkdir)")) return {
|
|
745
|
-
kind: "path-escape",
|
|
746
|
-
itemId: safeId
|
|
747
|
-
};
|
|
748
|
-
await mkdir(dataDir, { recursive: true });
|
|
749
|
-
if (escapesWorkspace(dataDir, workspaceRoot, safeId, "writeItem (post-mkdir)")) return {
|
|
750
|
-
kind: "path-escape",
|
|
751
|
-
itemId: safeId
|
|
752
|
-
};
|
|
753
|
-
const filePath = itemFilePath(dataDir, safeId);
|
|
754
|
-
const payload = `${JSON.stringify(item, null, 2)}\n`;
|
|
755
|
-
if (opts.refuseOverwrite) {
|
|
756
|
-
let handle;
|
|
757
|
-
try {
|
|
758
|
-
handle = await open(filePath, "wx");
|
|
759
|
-
} catch (err) {
|
|
760
|
-
if (isErrorWithCode(err) && err.code === "EEXIST") return {
|
|
761
|
-
kind: "conflict",
|
|
762
|
-
itemId: safeId
|
|
763
|
-
};
|
|
764
|
-
throw err;
|
|
765
|
-
}
|
|
766
|
-
try {
|
|
767
|
-
await handle.writeFile(payload);
|
|
768
|
-
} finally {
|
|
769
|
-
await handle.close();
|
|
770
|
-
}
|
|
771
|
-
} else await writeFileAtomic(filePath, payload);
|
|
772
|
-
if (opts.slug) publishCollectionChange(collectionChangePayload({
|
|
773
|
-
slug: opts.slug,
|
|
774
|
-
ids: [safeId],
|
|
775
|
-
op: "upsert"
|
|
776
|
-
}, opts.workspaceRoot));
|
|
777
|
-
return {
|
|
778
|
-
kind: "ok",
|
|
779
|
-
itemId: safeId,
|
|
780
|
-
item
|
|
781
|
-
};
|
|
675
|
+
/** Action ids must be unique so the dispatch route resolves unambiguously. */
|
|
676
|
+
function actionIdsAreUnique(schema) {
|
|
677
|
+
return hasUniqueIds(schema.actions);
|
|
782
678
|
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
kind: "invalid-id",
|
|
787
|
-
itemId
|
|
788
|
-
};
|
|
789
|
-
if (escapesWorkspace(dataDir, opts.workspaceRoot ?? getWorkspaceRoot(), safeId, "deleteItem")) return {
|
|
790
|
-
kind: "path-escape",
|
|
791
|
-
itemId: safeId
|
|
792
|
-
};
|
|
793
|
-
const filePath = itemFilePath(dataDir, safeId);
|
|
794
|
-
try {
|
|
795
|
-
await unlink(filePath);
|
|
796
|
-
if (opts.slug) publishCollectionChange(collectionChangePayload({
|
|
797
|
-
slug: opts.slug,
|
|
798
|
-
ids: [safeId],
|
|
799
|
-
op: "delete"
|
|
800
|
-
}, opts.workspaceRoot));
|
|
801
|
-
return {
|
|
802
|
-
kind: "ok",
|
|
803
|
-
itemId: safeId
|
|
804
|
-
};
|
|
805
|
-
} catch (err) {
|
|
806
|
-
if (isErrorWithCode(err) && err.code === "ENOENT") return {
|
|
807
|
-
kind: "not-found",
|
|
808
|
-
itemId: safeId
|
|
809
|
-
};
|
|
810
|
-
throw err;
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
/** Generate a short random hex id. Used by POST when the form doesn't
|
|
814
|
-
* carry a primary-key value (UI shortcut — Claude normally derives a
|
|
815
|
-
* semantic id from the record's name). */
|
|
816
|
-
function generateItemId() {
|
|
817
|
-
return randomBytes(4).toString("hex");
|
|
818
|
-
}
|
|
819
|
-
/** The item id a CREATE should use for `schema`, or null when the
|
|
820
|
-
* caller should generate one. A singleton collection pins every
|
|
821
|
-
* create to its fixed `schema.singleton` id, so the "at most one
|
|
822
|
-
* record" contract is enforced server-side (a second create targets
|
|
823
|
-
* the same file and hits `writeItem`'s refuseOverwrite conflict) —
|
|
824
|
-
* not only in the UI. Otherwise the record's own primaryKey value
|
|
825
|
-
* wins, falling back to a generated id (null = "generate"). */
|
|
826
|
-
function resolveCreateItemId(schema, record) {
|
|
827
|
-
if (schema.singleton) return schema.singleton;
|
|
828
|
-
const primaryRaw = record[schema.primaryKey];
|
|
829
|
-
return typeof primaryRaw === "string" && primaryRaw.length > 0 ? primaryRaw : null;
|
|
679
|
+
/** Collection-level action ids must likewise be unique. */
|
|
680
|
+
function collectionActionIdsAreUnique(schema) {
|
|
681
|
+
return hasUniqueIds(schema.collectionActions);
|
|
830
682
|
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
var SAFE_ALIAS_PATTERN = /^[A-Za-z_]\w{0,63}$/;
|
|
837
|
-
/** Hard ceiling on returned rows; `limit` clamps below it. A group-by on
|
|
838
|
-
* a near-unique column would otherwise return one row per source row —
|
|
839
|
-
* the exact materialization the aggregate path exists to avoid. */
|
|
840
|
-
var MAX_QUERY_ROWS = 1e4;
|
|
841
|
-
/** Default row cap when the query declares no `limit`. */
|
|
842
|
-
var DEFAULT_QUERY_ROWS = 1e3;
|
|
843
|
-
/** One aggregate column: `count` (rows; `column` optional to count
|
|
844
|
-
* non-null cells) or `sum`/`avg`/`min`/`max` over a named CSV column. */
|
|
845
|
-
var QueryAggregateZ = z.object({
|
|
846
|
-
op: z.enum([
|
|
847
|
-
"count",
|
|
848
|
-
"sum",
|
|
849
|
-
"avg",
|
|
850
|
-
"min",
|
|
851
|
-
"max"
|
|
852
|
-
]),
|
|
853
|
-
column: z.string().min(1).optional()
|
|
854
|
-
}).refine((aggregate) => aggregate.op === "count" || aggregate.column !== void 0, {
|
|
855
|
-
message: "`column` is required for every aggregate op except `count`",
|
|
856
|
-
path: ["column"]
|
|
857
|
-
});
|
|
858
|
-
/** One filter condition. Same op vocabulary as the schema-level `where`
|
|
859
|
-
* (`core/where.ts`) so authors learn one set; values may be typed
|
|
860
|
-
* (number / boolean) since CSV columns are. `in` requires an array
|
|
861
|
-
* value, every other op a scalar. */
|
|
862
|
-
var QueryWhereZ = z.object({
|
|
863
|
-
field: z.string().min(1),
|
|
864
|
-
op: z.enum([
|
|
865
|
-
"eq",
|
|
866
|
-
"ne",
|
|
867
|
-
"in",
|
|
868
|
-
"gt",
|
|
869
|
-
"gte",
|
|
870
|
-
"lt",
|
|
871
|
-
"lte",
|
|
872
|
-
"contains"
|
|
873
|
-
]),
|
|
874
|
-
value: z.union([
|
|
875
|
-
z.string(),
|
|
876
|
-
z.number(),
|
|
877
|
-
z.boolean(),
|
|
878
|
-
z.array(z.union([
|
|
879
|
-
z.string(),
|
|
880
|
-
z.number(),
|
|
881
|
-
z.boolean()
|
|
882
|
-
])).min(1).max(100)
|
|
883
|
-
])
|
|
884
|
-
}).refine((cond) => cond.op === "in" === Array.isArray(cond.value), {
|
|
885
|
-
message: "`in` requires an array value (the allowed set); every other op requires a scalar value",
|
|
886
|
-
path: ["value"]
|
|
887
|
-
});
|
|
888
|
-
var QueryOrderZ = z.object({
|
|
889
|
-
/** A `groupBy` column or an aggregate alias — membership enforced by
|
|
890
|
-
* the whole-query refine below. */
|
|
891
|
-
field: z.string().min(1),
|
|
892
|
-
dir: z.enum(["asc", "desc"]).optional()
|
|
893
|
-
});
|
|
894
|
-
/** The whole query. At least one of `groupBy` / `aggregates` must be
|
|
895
|
-
* present: bare `groupBy` is a DISTINCT listing, bare `aggregates` a
|
|
896
|
-
* whole-file scalar row, together a grouped aggregation. */
|
|
897
|
-
var CollectionQueryZ = z.object({
|
|
898
|
-
groupBy: z.array(z.string().min(1)).max(8).refine((columns) => new Set(columns.map((column) => column.toLowerCase())).size === columns.length, { message: "`groupBy` columns must be unique (case-insensitively — SQL identifiers ignore case)" }).optional(),
|
|
899
|
-
aggregates: z.record(z.string().regex(SAFE_ALIAS_PATTERN, "aggregate aliases must be simple identifiers (letters/digits/underscore)"), QueryAggregateZ).optional(),
|
|
900
|
-
where: z.array(QueryWhereZ).max(16).optional(),
|
|
901
|
-
orderBy: z.array(QueryOrderZ).max(4).optional(),
|
|
902
|
-
limit: z.number().int().min(1).max(MAX_QUERY_ROWS).optional()
|
|
903
|
-
}).refine((query) => (query.groupBy?.length ?? 0) > 0 || Object.keys(query.aggregates ?? {}).length > 0, {
|
|
904
|
-
message: "declare at least one of `groupBy` (columns to bucket by) or `aggregates` (values to compute)",
|
|
905
|
-
path: ["groupBy"]
|
|
906
|
-
}).refine((query) => Object.keys(query.aggregates ?? {}).length <= 32, {
|
|
907
|
-
message: `\`aggregates\` supports at most 32 entries`,
|
|
908
|
-
path: ["aggregates"]
|
|
909
|
-
}).refine((query) => {
|
|
910
|
-
const groupLower = new Set((query.groupBy ?? []).map((column) => column.toLowerCase()));
|
|
911
|
-
const seen = /* @__PURE__ */ new Set();
|
|
912
|
-
return Object.keys(query.aggregates ?? {}).every((alias) => {
|
|
913
|
-
const lower = alias.toLowerCase();
|
|
914
|
-
if (groupLower.has(lower) || seen.has(lower)) return false;
|
|
915
|
-
seen.add(lower);
|
|
916
|
-
return true;
|
|
917
|
-
});
|
|
918
|
-
}, {
|
|
919
|
-
message: "aggregate aliases must be unique and must not collide with `groupBy` column names (case-insensitively — SQL identifiers ignore case)",
|
|
920
|
-
path: ["aggregates"]
|
|
921
|
-
}).refine((query) => {
|
|
922
|
-
const sortable = /* @__PURE__ */ new Set([...query.groupBy ?? [], ...Object.keys(query.aggregates ?? {})]);
|
|
923
|
-
return (query.orderBy ?? []).every((order) => sortable.has(order.field));
|
|
924
|
-
}, {
|
|
925
|
-
message: "every `orderBy.field` must be a `groupBy` column or an aggregate alias",
|
|
926
|
-
path: ["orderBy"]
|
|
927
|
-
});
|
|
928
|
-
//#endregion
|
|
929
|
-
//#region src/collection/server/csvQuery.ts
|
|
930
|
-
/** Double-quote a SQL identifier (CSV column name / result alias). */
|
|
931
|
-
function quoteIdent(name) {
|
|
932
|
-
return `"${name.replaceAll("\"", "\"\"")}"`;
|
|
683
|
+
/** A mutate action's `set` writes real STORED fields: a typo'd key would write
|
|
684
|
+
* a stray value forever, a computed/projected field is never persisted, and
|
|
685
|
+
* the primaryKey is the filename (renaming is not a mutation). */
|
|
686
|
+
function mutateSetKeysNameStoredFields(schema) {
|
|
687
|
+
return (schema.actions ?? []).every((action) => action.kind !== "mutate" || Object.keys(action.set).every((key) => namesStoredField(schema.fields, key, schema.primaryKey)));
|
|
933
688
|
}
|
|
934
|
-
/**
|
|
935
|
-
|
|
936
|
-
|
|
689
|
+
/** Every `$params.<name>` reference in `set` must name a declared param — an
|
|
690
|
+
* undeclared one would silently no-op the assignment. */
|
|
691
|
+
function mutateParamRefsAreDeclared(schema) {
|
|
692
|
+
return (schema.actions ?? []).every((action) => action.kind !== "mutate" || Object.values(action.set).every((value) => {
|
|
693
|
+
const ref = paramRefName(value);
|
|
694
|
+
return ref === null || (action.params ?? {})[ref] !== void 0;
|
|
695
|
+
}));
|
|
937
696
|
}
|
|
938
|
-
/**
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
* and distinct keys collapse. */
|
|
942
|
-
function readCsvArgs(primaryKey) {
|
|
943
|
-
return `?, types={${quoteLiteral(primaryKey)}: 'VARCHAR'}`;
|
|
697
|
+
/** A collection-level action has no record to write. */
|
|
698
|
+
function collectionActionsAreNotMutate(schema) {
|
|
699
|
+
return (schema.collectionActions ?? []).every((action) => action.kind !== "mutate");
|
|
944
700
|
}
|
|
945
|
-
/**
|
|
946
|
-
*
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
const { op, column } = aggregate;
|
|
952
|
-
if (op === "count") return column === void 0 ? "count(*)" : `count(${quoteIdent(column)})`;
|
|
953
|
-
if (op === "sum" || op === "avg") return `${op}(TRY_CAST(${quoteIdent(column ?? "")} AS DOUBLE))`;
|
|
954
|
-
return `${op}(${quoteIdent(column ?? "")})`;
|
|
701
|
+
/** The singleton value becomes a record id (and thus a `<id>.json` filename),
|
|
702
|
+
* so it must satisfy the SAME record-id rule the write path enforces —
|
|
703
|
+
* otherwise the create form would lock the primary key to a value the POST
|
|
704
|
+
* route then rejects, making the collection impossible to initialize. */
|
|
705
|
+
function singletonIsAValidRecordId(schema) {
|
|
706
|
+
return schema.singleton === void 0 || isSafeRecordId(schema.singleton);
|
|
955
707
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
const column = quoteIdent(cond.field);
|
|
962
|
-
const asText = `CAST(${column} AS VARCHAR)`;
|
|
963
|
-
if (cond.op === "in") {
|
|
964
|
-
const values = arrayValue(cond);
|
|
965
|
-
return {
|
|
966
|
-
sql: `${values.every((value) => typeof value === "string") ? asText : column} IN (${values.map(() => "?").join(", ")})`,
|
|
967
|
-
params: values
|
|
968
|
-
};
|
|
708
|
+
function collectCurrencyFieldRefs(fields) {
|
|
709
|
+
const refs = [];
|
|
710
|
+
for (const field of Object.values(fields)) {
|
|
711
|
+
if (typeof field.currencyField === "string" && field.currencyField.length > 0) refs.push(field.currencyField);
|
|
712
|
+
for (const sub of Object.values(field.of ?? {})) if (typeof sub.currencyField === "string" && sub.currencyField.length > 0) refs.push(sub.currencyField);
|
|
969
713
|
}
|
|
970
|
-
|
|
971
|
-
sql: `contains(${asText}, ?)`,
|
|
972
|
-
params: [String(scalarValue(cond))]
|
|
973
|
-
};
|
|
974
|
-
const operator = {
|
|
975
|
-
eq: "=",
|
|
976
|
-
ne: "<>",
|
|
977
|
-
gt: ">",
|
|
978
|
-
gte: ">=",
|
|
979
|
-
lt: "<",
|
|
980
|
-
lte: "<="
|
|
981
|
-
}[cond.op];
|
|
982
|
-
return {
|
|
983
|
-
sql: `${typeof cond.value === "string" && (cond.op === "eq" || cond.op === "ne") ? asText : column} ${operator} ?`,
|
|
984
|
-
params: [scalarValue(cond)]
|
|
985
|
-
};
|
|
714
|
+
return refs;
|
|
986
715
|
}
|
|
987
|
-
/**
|
|
988
|
-
* `
|
|
989
|
-
*
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
if (!Array.isArray(cond.value)) throw new Error(`where condition on '${cond.field}' uses op 'in', which requires an array value, not a scalar`);
|
|
993
|
-
return cond.value;
|
|
716
|
+
/** A `currencyField` pointer must name a real top-level field that holds a code
|
|
717
|
+
* string — a typo (`curreny`) would otherwise pass the per-field check, then
|
|
718
|
+
* silently fall back to the literal / USD at render and mislabel amounts. */
|
|
719
|
+
function currencyFieldRefsNameCodeFields(schema) {
|
|
720
|
+
return collectCurrencyFieldRefs(schema.fields).every((name) => CODE_FIELD_TYPES.has(declaredField(schema.fields, name)?.type ?? ""));
|
|
994
721
|
}
|
|
995
|
-
/**
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
722
|
+
/** The pair must be declared together — one without the other is meaningless:
|
|
723
|
+
* the host would either never fire (no done values to compare against) or
|
|
724
|
+
* never clear (no field to read).
|
|
725
|
+
*
|
|
726
|
+
* EXCEPTION: when `completionField` names a `flag` field, done ⇔ the flag's
|
|
727
|
+
* `where` matches, so `completionDoneValues` carries no information and MUST
|
|
728
|
+
* be omitted (declaring it would invite a contradictory second source of
|
|
729
|
+
* truth). */
|
|
730
|
+
function completionPairIsCoherent(schema) {
|
|
731
|
+
if (schema.completionField !== void 0 && declaredField(schema.fields, schema.completionField)?.type === "flag") return schema.completionDoneValues === void 0;
|
|
732
|
+
return schema.completionField === void 0 === (schema.completionDoneValues === void 0);
|
|
1001
733
|
}
|
|
1002
|
-
/**
|
|
1003
|
-
*
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
* the shape (aliases already charset-checked, orderBy membership already
|
|
1007
|
-
* enforced). */
|
|
1008
|
-
function compileQuery(query, fromSql) {
|
|
1009
|
-
const groupBy = query.groupBy ?? [];
|
|
1010
|
-
const aggregates = Object.entries(query.aggregates ?? {});
|
|
1011
|
-
const selectList = [...groupBy.map(quoteIdent), ...aggregates.map(([alias, aggregate]) => `${aggregateExpr(aggregate)} AS ${quoteIdent(alias)}`)];
|
|
1012
|
-
const where = (query.where ?? []).map(whereFragment);
|
|
1013
|
-
const clauses = [`SELECT ${selectList.join(", ")}`, `FROM ${fromSql}`];
|
|
1014
|
-
if (where.length > 0) clauses.push(`WHERE ${where.map((fragment) => fragment.sql).join(" AND ")}`);
|
|
1015
|
-
if (groupBy.length > 0) clauses.push(`GROUP BY ${groupBy.map(quoteIdent).join(", ")}`);
|
|
1016
|
-
const orderBy = (query.orderBy ?? []).map((order) => quoteIdent(order.field) + (order.dir === "desc" ? " DESC" : " ASC"));
|
|
1017
|
-
if (orderBy.length > 0) clauses.push(`ORDER BY ${orderBy.join(", ")}`);
|
|
1018
|
-
clauses.push(`LIMIT ${query.limit ?? 1e3}`);
|
|
1019
|
-
return {
|
|
1020
|
-
sql: clauses.join(" "),
|
|
1021
|
-
params: where.flatMap((fragment) => fragment.params)
|
|
1022
|
-
};
|
|
734
|
+
/** `completionField` must name a real top-level field — a typo would silently
|
|
735
|
+
* disable the notification mechanism otherwise. */
|
|
736
|
+
function completionFieldIsDeclared(schema) {
|
|
737
|
+
return schema.completionField === void 0 || declaredField(schema.fields, schema.completionField) !== void 0;
|
|
1023
738
|
}
|
|
1024
|
-
/**
|
|
1025
|
-
|
|
1026
|
-
|
|
739
|
+
/** A flag named by `completionField` is evaluated against the RAW record — the
|
|
740
|
+
* reconciler (and spawn's fallback) read items straight off disk, BEFORE any
|
|
741
|
+
* `deriveAll` enrichment — so its `where` may only reference STORED fields. A
|
|
742
|
+
* condition over a computed sibling would see an absent key: `ne` matches
|
|
743
|
+
* vacuously, every other op reads false, and the bell would clear wrongly /
|
|
744
|
+
* never. General (non-completion) flags keep the full vocabulary — the UI
|
|
745
|
+
* evaluates them post-enrichment. */
|
|
746
|
+
function completionFlagReadsOnlyStoredFields(schema) {
|
|
747
|
+
const spec = schema.completionField === void 0 ? void 0 : declaredField(schema.fields, schema.completionField);
|
|
748
|
+
if (spec?.type !== "flag") return true;
|
|
749
|
+
return spec.where.every((cond) => [cond.field, ...cond.valueFrom ? [cond.valueFrom.field] : []].every((name) => {
|
|
750
|
+
const target = declaredField(schema.fields, name);
|
|
751
|
+
return target !== void 0 && !COMPUTED_TYPES.has(target.type);
|
|
752
|
+
}));
|
|
1027
753
|
}
|
|
1028
|
-
/**
|
|
1029
|
-
*
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
* optional/derived field first appearing past the sample would not be
|
|
1033
|
-
* inferred as a column and the query would binder-error on it (Codex P2
|
|
1034
|
-
* on #2165). The full scan costs nothing extra here: aggregation reads
|
|
1035
|
-
* the whole file anyway. */
|
|
1036
|
-
function compileJsonlQuery(query) {
|
|
1037
|
-
return compileQuery(query, `read_json(?, format='newline_delimited', sample_size=-1)`);
|
|
754
|
+
/** `displayField`, like `completionField`, must name a real top-level field —
|
|
755
|
+
* a typo would silently fall back to the primaryKey forever. */
|
|
756
|
+
function displayFieldIsDeclared(schema) {
|
|
757
|
+
return schema.displayField === void 0 || declaredField(schema.fields, schema.displayField) !== void 0;
|
|
1038
758
|
}
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
/** Record ids minted from non-safe key values: `id0x` + utf-8 hex. Raw key
|
|
1045
|
-
* values that themselves match this pattern are ALSO encoded, so the
|
|
1046
|
-
* encoded namespace never collides with a raw value (injective mapping). */
|
|
1047
|
-
var ENCODED_ID_PATTERN = /^id0x([0-9a-f]+)$/;
|
|
1048
|
-
/** A CSV key value → the record id it's addressed by. Safe values pass
|
|
1049
|
-
* through untouched; everything else (and anything shaped like an encoded
|
|
1050
|
-
* id) becomes `id0x<hex>`. Pure + exported for unit tests. */
|
|
1051
|
-
function encodeCsvRecordId(rawKey) {
|
|
1052
|
-
if (safeRecordId(rawKey) === rawKey && !ENCODED_ID_PATTERN.test(rawKey)) return rawKey;
|
|
1053
|
-
return `id0x${Buffer.from(rawKey, "utf-8").toString("hex")}`;
|
|
759
|
+
/** A field's `when.field` gates its visibility against a sibling's value, so it
|
|
760
|
+
* must name a real top-level field — a typo would silently keep the field
|
|
761
|
+
* hidden forever (the gate never matches). */
|
|
762
|
+
function fieldVisibilityGatesNameDeclaredFields(schema) {
|
|
763
|
+
return Object.values(schema.fields).every((field) => field.when === void 0 || declaredField(schema.fields, field.when.field) !== void 0);
|
|
1054
764
|
}
|
|
1055
|
-
/** A
|
|
1056
|
-
* `
|
|
1057
|
-
*
|
|
1058
|
-
function
|
|
1059
|
-
|
|
1060
|
-
if (hex === void 0) return itemId;
|
|
1061
|
-
return Buffer.from(hex, "hex").toString("utf-8");
|
|
765
|
+
/** A flag's `where` reads sibling fields (both `cond.field` and a same-record
|
|
766
|
+
* `valueFrom.field`), so each must name a real top-level field — a typo would
|
|
767
|
+
* silently pin the flag false forever (`ne`: true forever). */
|
|
768
|
+
function flagConditionsNameDeclaredFields(schema) {
|
|
769
|
+
return Object.values(schema.fields).every((field) => field.type !== "flag" || field.where.every((cond) => declaredField(schema.fields, cond.field) !== void 0 && (cond.valueFrom === void 0 || declaredField(schema.fields, cond.valueFrom.field) !== void 0)));
|
|
1062
770
|
}
|
|
1063
|
-
/**
|
|
1064
|
-
*
|
|
1065
|
-
*
|
|
1066
|
-
* field
|
|
1067
|
-
*
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
} catch {
|
|
1075
|
-
return String(value);
|
|
1076
|
-
}
|
|
771
|
+
/** An `embed`'s `idField` resolves the target record id from a sibling's value,
|
|
772
|
+
* so it must name a real top-level field — and one whose stored value is a
|
|
773
|
+
* plain id string. Only `ref` / `string` qualify: the editor writes the picked
|
|
774
|
+
* id into that field, so a non-persisted or composite type would either not
|
|
775
|
+
* round-trip on save or hold no usable id. */
|
|
776
|
+
function embedIdFieldsNameIdBearingFields(schema) {
|
|
777
|
+
return Object.values(schema.fields).every((field) => {
|
|
778
|
+
if (field.type !== "embed" || field.idField === void 0) return true;
|
|
779
|
+
const target = declaredField(schema.fields, field.idField);
|
|
780
|
+
return target !== void 0 && (target.type === "ref" || target.type === "string");
|
|
781
|
+
});
|
|
1077
782
|
}
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
783
|
+
/** The sync writes each mapped value into a declared field, and puts the Google
|
|
784
|
+
* event id in the primary field — so a map key that names no field (or names
|
|
785
|
+
* the primary) would silently drop data or fight the id. */
|
|
786
|
+
function googleCalendarMapNamesStoredFields(schema) {
|
|
787
|
+
if (schema.googleCalendar === void 0) return true;
|
|
788
|
+
return Object.keys(schema.googleCalendar.map).every((key) => namesStoredField(schema.fields, key, schema.primaryKey));
|
|
789
|
+
}
|
|
790
|
+
/** A `toggle` field projects an `enum` field: its `field` must name a real
|
|
791
|
+
* top-level enum, and `onValue` / `offValue` must be members of that enum's
|
|
792
|
+
* `values` — otherwise toggling would write a value outside the closed set
|
|
793
|
+
* (and never appear "checked"). */
|
|
794
|
+
function togglesProjectValidEnums(schema) {
|
|
795
|
+
const { fields } = schema;
|
|
796
|
+
for (const spec of Object.values(fields)) {
|
|
797
|
+
if (spec.type !== "toggle") continue;
|
|
798
|
+
const target = declaredField(fields, spec.field);
|
|
799
|
+
if (!target || target.type !== "enum") return false;
|
|
800
|
+
const allowed = new Set(target.values);
|
|
801
|
+
if (!allowed.has(spec.onValue) || !allowed.has(spec.offValue)) return false;
|
|
1083
802
|
}
|
|
1084
|
-
|
|
1085
|
-
return value;
|
|
803
|
+
return true;
|
|
1086
804
|
}
|
|
1087
|
-
/**
|
|
1088
|
-
*
|
|
1089
|
-
*
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
function csvRowToItem(row, primaryKey) {
|
|
1093
|
-
const normalized = Object.fromEntries(Object.entries(row).map(([key, value]) => [key, normalizeCsvValue(value)]));
|
|
1094
|
-
const rawKey = normalized[primaryKey];
|
|
1095
|
-
const keyText = fieldTextOrNull(rawKey);
|
|
1096
|
-
if (keyText === null || keyText === "") return null;
|
|
1097
|
-
return {
|
|
1098
|
-
...normalized,
|
|
1099
|
-
[primaryKey]: encodeCsvRecordId(keyText)
|
|
1100
|
-
};
|
|
805
|
+
/** `triggerField` requires the completion pair: the time gate only suppresses
|
|
806
|
+
* the *completion* bell until the date, and the bell still clears via
|
|
807
|
+
* `completionDoneValues`. Without completion there is no bell to gate. */
|
|
808
|
+
function triggerFieldRequiresCompletion(schema) {
|
|
809
|
+
return schema.triggerField === void 0 || schema.completionField !== void 0;
|
|
1101
810
|
}
|
|
1102
|
-
/**
|
|
1103
|
-
*
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
const byId = /* @__PURE__ */ new Map();
|
|
1107
|
-
for (const item of items) byId.set(String(item[primaryKey]), item);
|
|
1108
|
-
return {
|
|
1109
|
-
items: [...byId.values()],
|
|
1110
|
-
duplicates: items.length - byId.size
|
|
1111
|
-
};
|
|
811
|
+
/** `triggerField` must name a real `date` field — the gate parses its value as
|
|
812
|
+
* `YYYY-MM-DD`; any other type can't be compared to the clock. */
|
|
813
|
+
function triggerFieldIsADateField(schema) {
|
|
814
|
+
return schema.triggerField === void 0 || declaredField(schema.fields, schema.triggerField)?.type === "date";
|
|
1112
815
|
}
|
|
1113
|
-
/**
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
function isMissingKeyColumnError(err) {
|
|
1117
|
-
return String(err).includes("do not exist in the CSV");
|
|
816
|
+
/** `triggerLeadDays` only means something relative to a trigger date. */
|
|
817
|
+
function triggerLeadDaysRequiresTriggerField(schema) {
|
|
818
|
+
return schema.triggerLeadDays === void 0 || schema.triggerField !== void 0;
|
|
1118
819
|
}
|
|
1119
|
-
/**
|
|
1120
|
-
*
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
function isValidUtf8(buf) {
|
|
1124
|
-
try {
|
|
1125
|
-
new TextDecoder("utf-8", { fatal: true }).decode(buf);
|
|
1126
|
-
return true;
|
|
1127
|
-
} catch {
|
|
1128
|
-
return false;
|
|
1129
|
-
}
|
|
820
|
+
/** `spawn` advances `triggerField` to compute the successor's trigger date, so
|
|
821
|
+
* the schema must declare one. */
|
|
822
|
+
function spawnRequiresTriggerField(schema) {
|
|
823
|
+
return schema.spawn === void 0 || schema.triggerField !== void 0;
|
|
1130
824
|
}
|
|
1131
|
-
/**
|
|
1132
|
-
*
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
if (buf.length >= 2 && buf[0] === 255 && buf[1] === 254) return "utf-16le";
|
|
1136
|
-
if (buf.length >= 2 && buf[0] === 254 && buf[1] === 255) return "utf-16be";
|
|
1137
|
-
return "cp932";
|
|
825
|
+
/** `spawn.when.field` must name a real top-level field — a typo would silently
|
|
826
|
+
* never match. */
|
|
827
|
+
function spawnWhenFieldIsDeclared(schema) {
|
|
828
|
+
return schema.spawn?.when === void 0 || declaredField(schema.fields, schema.spawn.when.field) !== void 0;
|
|
1138
829
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
830
|
+
/** Every `spawn.carry` entry must name a real top-level field — a typo would
|
|
831
|
+
* silently never copy. */
|
|
832
|
+
function spawnCarryEntriesAreDeclared(schema) {
|
|
833
|
+
return (schema.spawn?.carry ?? []).every((name) => declaredField(schema.fields, name) !== void 0);
|
|
1141
834
|
}
|
|
1142
|
-
/**
|
|
1143
|
-
*
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
835
|
+
/** A successor must NOT be born already matching its own spawn predicate — it
|
|
836
|
+
* would re-spawn on its first reconcile, fanning out into an unbounded chain
|
|
837
|
+
* of records. The predicate field/values are `spawn.when` when given, else the
|
|
838
|
+
* completion-done pair. The successor's value for that field is `set[field]`
|
|
839
|
+
* if set, else the carried source value (which matched, by definition, when
|
|
840
|
+
* the spawn fired) if carried, else absent (safe). */
|
|
841
|
+
function spawnSuccessorStartsInert(schema) {
|
|
842
|
+
const { spawn } = schema;
|
|
843
|
+
if (!spawn) return true;
|
|
844
|
+
const field = spawn.when?.field ?? schema.completionField;
|
|
845
|
+
const values = spawn.when?.in ?? schema.completionDoneValues;
|
|
846
|
+
if (!field || !values) return true;
|
|
847
|
+
if (spawn.set && Object.prototype.hasOwnProperty.call(spawn.set, field)) return !values.includes(String(spawn.set[field]));
|
|
848
|
+
return !(spawn.carry ?? []).includes(field);
|
|
1154
849
|
}
|
|
1155
|
-
/**
|
|
1156
|
-
*
|
|
1157
|
-
*
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
return true;
|
|
1162
|
-
} catch {
|
|
1163
|
-
return false;
|
|
1164
|
-
}
|
|
850
|
+
/** `spawnSuccessorStartsInert` cannot see through a flag's `where` (the
|
|
851
|
+
* predicate would need full record evaluation against `set`/`carry`). So a
|
|
852
|
+
* schema whose completion is flag-form may only spawn with an explicit
|
|
853
|
+
* `spawn.when` — which that check CAN evaluate. */
|
|
854
|
+
function flagCompletionSpawnDeclaresWhen(schema) {
|
|
855
|
+
return schema.spawn === void 0 || schema.spawn.when !== void 0 || declaredField(schema.fields, schema.completionField ?? "")?.type !== "flag";
|
|
1165
856
|
}
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
* (unlink-while-open is safe on POSIX). */
|
|
1171
|
-
async function evictSupersededCache(key, keepBasename) {
|
|
1172
|
-
try {
|
|
1173
|
-
const entries = await readdir(cacheDir());
|
|
1174
|
-
await Promise.all(entries.filter((name) => name.startsWith(`${key}-`) && name !== keepBasename).map((name) => unlink(path.join(cacheDir(), name)).catch(() => void 0)));
|
|
1175
|
-
} catch {}
|
|
857
|
+
function fieldDrivenSpawnEvery(schema) {
|
|
858
|
+
const every = schema.spawn?.every;
|
|
859
|
+
if (!every || !("fromField" in every)) return null;
|
|
860
|
+
return every;
|
|
1176
861
|
}
|
|
1177
|
-
/**
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
const cached = path.join(cacheDir(), `${key}-${Math.trunc(info.mtimeMs)}-${info.size}.csv`);
|
|
1185
|
-
if (!await pathExists(cached)) {
|
|
1186
|
-
const whole = await readFile(absPath);
|
|
1187
|
-
const encoding = fallbackEncoding(whole);
|
|
1188
|
-
const text = iconv.decode(whole, encoding);
|
|
1189
|
-
await mkdir(cacheDir(), {
|
|
1190
|
-
recursive: true,
|
|
1191
|
-
mode: 448
|
|
1192
|
-
});
|
|
1193
|
-
const tmp = `${cached}.${randomBytes(4).toString("hex")}.tmp`;
|
|
1194
|
-
await writeFile(tmp, text, {
|
|
1195
|
-
encoding: "utf-8",
|
|
1196
|
-
mode: 384
|
|
1197
|
-
});
|
|
1198
|
-
await rename(tmp, cached);
|
|
1199
|
-
log.info("collections", "decoded non-UTF-8 dataSource file to cache", {
|
|
1200
|
-
path: absPath,
|
|
1201
|
-
encoding
|
|
1202
|
-
});
|
|
1203
|
-
await evictSupersededCache(key, path.basename(cached));
|
|
1204
|
-
}
|
|
1205
|
-
return cached;
|
|
862
|
+
/** §4.1 — `fromField` must name a real top-level `enum` field. The `map` keys
|
|
863
|
+
* are only meaningful against a closed value set, and the field renders as a
|
|
864
|
+
* form `<select>`; a non-enum target has no finite values to validate. */
|
|
865
|
+
function fieldDrivenFromFieldIsEnum(schema) {
|
|
866
|
+
const driven = fieldDrivenSpawnEvery(schema);
|
|
867
|
+
if (!driven) return true;
|
|
868
|
+
return declaredField(schema.fields, driven.fromField)?.type === "enum";
|
|
1206
869
|
}
|
|
1207
|
-
/**
|
|
1208
|
-
*
|
|
1209
|
-
*
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
870
|
+
/** §4.2 — `map` keys must EXACTLY cover the enum's `values` (no missing keys —
|
|
871
|
+
* a record could pick an unmapped frequency and silently stall; no extra keys
|
|
872
|
+
* — a stale map outliving an enum edit). */
|
|
873
|
+
function fieldDrivenMapCoversValues(schema) {
|
|
874
|
+
const driven = fieldDrivenSpawnEvery(schema);
|
|
875
|
+
if (!driven) return true;
|
|
876
|
+
const target = declaredField(schema.fields, driven.fromField);
|
|
877
|
+
if (target?.type !== "enum") return true;
|
|
878
|
+
const values = new Set(target.values);
|
|
879
|
+
const keys = Object.keys(driven.map);
|
|
880
|
+
return keys.length === values.size && keys.every((key) => values.has(key));
|
|
881
|
+
}
|
|
882
|
+
/** §4.5 — `fromField` must reach the successor (via `carry` or `set`);
|
|
883
|
+
* otherwise the successor loses its frequency and the NEXT spawn along the
|
|
884
|
+
* chain can't resolve an interval, silently halting the recurrence.
|
|
885
|
+
*
|
|
886
|
+
* `set` writes a FIXED value, so it must itself be a key of `map` (else the
|
|
887
|
+
* successor is born with an unresolvable driver and `resolveEvery` skips it —
|
|
888
|
+
* the exact silent-halt §4.5 exists to prevent). `carry` copies the source's
|
|
889
|
+
* own value, which — for a record that matched the spawn — is one of the
|
|
890
|
+
* enum's values, all of which `map` covers by §4.2; so a carried driver is
|
|
891
|
+
* always resolvable and needs no value check here. */
|
|
892
|
+
function fieldDrivenFromFieldCarried(schema) {
|
|
893
|
+
const driven = fieldDrivenSpawnEvery(schema);
|
|
894
|
+
if (!driven) return true;
|
|
895
|
+
const { carry, set } = schema.spawn ?? {};
|
|
896
|
+
if (set && Object.prototype.hasOwnProperty.call(set, driven.fromField)) {
|
|
897
|
+
const raw = set[driven.fromField];
|
|
898
|
+
if (raw === void 0 || raw === null || raw === "") return false;
|
|
899
|
+
const key = fieldTextOrNull(raw);
|
|
900
|
+
return key !== null && Object.prototype.hasOwnProperty.call(driven.map, key);
|
|
1229
901
|
}
|
|
1230
|
-
return
|
|
902
|
+
return (carry ?? []).includes(driven.fromField);
|
|
1231
903
|
}
|
|
1232
|
-
/**
|
|
1233
|
-
*
|
|
1234
|
-
*
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
async function ensureUtf8CsvPath(absPath, workspaceRoot) {
|
|
1238
|
-
const info = await safeCsvStat(absPath, workspaceRoot);
|
|
1239
|
-
if (info === null) return null;
|
|
1240
|
-
const head = await readHead(absPath, SNIFF_BYTES);
|
|
1241
|
-
const sample = head.length === SNIFF_BYTES ? head.subarray(0, 1048573) : head;
|
|
1242
|
-
if (!(head.length >= 2 && (head[0] === 255 && head[1] === 254 || head[0] === 254 && head[1] === 255)) && isValidUtf8(sample)) return absPath;
|
|
1243
|
-
return decodeToCache(absPath, info);
|
|
904
|
+
/** `calendarField` must name a real `date`/`datetime` field — the calendar view
|
|
905
|
+
* parses its value to place records on the month grid (a `datetime` anchor
|
|
906
|
+
* also carries the clock for the day view). */
|
|
907
|
+
function calendarFieldIsDateLike(schema) {
|
|
908
|
+
return schema.calendarField === void 0 || isDateLike(declaredField(schema.fields, schema.calendarField)?.type);
|
|
1244
909
|
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
* collections only, never a broken core. A failed init is retried on the
|
|
1250
|
-
* next call (the promise is reset). */
|
|
1251
|
-
async function duckDbInstance() {
|
|
1252
|
-
if (instancePromise === null) instancePromise = import("@duckdb/node-api").then((mod) => mod.DuckDBInstance.create(":memory:"));
|
|
1253
|
-
try {
|
|
1254
|
-
return await instancePromise;
|
|
1255
|
-
} catch (err) {
|
|
1256
|
-
instancePromise = null;
|
|
1257
|
-
throw new BackendUnavailableError(`DuckDB is unavailable on this host (@duckdb/node-api failed to load: ${String(err)}) — dataSource collections cannot be read`);
|
|
1258
|
-
}
|
|
910
|
+
/** `calendarEndField` marks the end of a multi-day span, so it only means
|
|
911
|
+
* something alongside a start anchor. */
|
|
912
|
+
function calendarEndFieldRequiresCalendarField(schema) {
|
|
913
|
+
return schema.calendarEndField === void 0 || schema.calendarField !== void 0;
|
|
1259
914
|
}
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
return (await connection.runAndReadAll(sql, params)).getRowObjectsJS();
|
|
1264
|
-
} finally {
|
|
1265
|
-
connection.disconnectSync();
|
|
1266
|
-
}
|
|
915
|
+
/** `calendarEndField` must also name a real `date`/`datetime` field — same parse. */
|
|
916
|
+
function calendarEndFieldIsDateLike(schema) {
|
|
917
|
+
return schema.calendarEndField === void 0 || isDateLike(declaredField(schema.fields, schema.calendarEndField)?.type);
|
|
1267
918
|
}
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
truncated: false
|
|
1273
|
-
};
|
|
1274
|
-
let rows;
|
|
1275
|
-
try {
|
|
1276
|
-
rows = await queryCsv(`SELECT * FROM read_csv(${readCsvArgs(primaryKey)}) LIMIT 5001`, [utf8Path]);
|
|
1277
|
-
} catch (err) {
|
|
1278
|
-
if (!isMissingKeyColumnError(err)) throw err;
|
|
1279
|
-
log.warn("collections", "dataSource CSV has no primaryKey column — every row is skipped", {
|
|
1280
|
-
path: absPath,
|
|
1281
|
-
primaryKey
|
|
1282
|
-
});
|
|
1283
|
-
return {
|
|
1284
|
-
items: [],
|
|
1285
|
-
truncated: false
|
|
1286
|
-
};
|
|
1287
|
-
}
|
|
1288
|
-
const truncated = rows.length > MAX_CSV_ROWS;
|
|
1289
|
-
if (truncated) {
|
|
1290
|
-
log.warn("collections", "dataSource CSV truncated to row cap", {
|
|
1291
|
-
path: absPath,
|
|
1292
|
-
cap: MAX_CSV_ROWS
|
|
1293
|
-
});
|
|
1294
|
-
rows.length = MAX_CSV_ROWS;
|
|
1295
|
-
}
|
|
1296
|
-
const items = rows.map((row) => csvRowToItem(row, primaryKey)).filter((item) => item !== null);
|
|
1297
|
-
const skipped = rows.length - items.length;
|
|
1298
|
-
if (skipped > 0) log.warn("collections", "dataSource CSV rows skipped (empty key cell)", {
|
|
1299
|
-
path: absPath,
|
|
1300
|
-
skipped
|
|
1301
|
-
});
|
|
1302
|
-
const deduped = dedupeByRecordId(items, primaryKey);
|
|
1303
|
-
if (deduped.duplicates > 0) log.warn("collections", "dataSource CSV has duplicate key values (last row wins)", {
|
|
1304
|
-
path: absPath,
|
|
1305
|
-
duplicates: deduped.duplicates
|
|
1306
|
-
});
|
|
1307
|
-
return {
|
|
1308
|
-
items: deduped.items,
|
|
1309
|
-
truncated
|
|
1310
|
-
};
|
|
919
|
+
/** `calendarTimeField` places records on the day view, so it only means
|
|
920
|
+
* something alongside a start anchor. */
|
|
921
|
+
function calendarTimeFieldRequiresCalendarField(schema) {
|
|
922
|
+
return schema.calendarTimeField === void 0 || schema.calendarField !== void 0;
|
|
1311
923
|
}
|
|
1312
|
-
/**
|
|
1313
|
-
*
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
/** One record by id. The comparison value rides as a prepared-statement
|
|
1317
|
-
* parameter, and the LAST matching row is selected IN DuckDB (scan-order
|
|
1318
|
-
* ordinal + LIMIT 1) — a CSV with thousands of duplicate keys must not
|
|
1319
|
-
* materialize them all for one detail read. Consistent with csvList's
|
|
1320
|
-
* last-wins dedupe. */
|
|
1321
|
-
async function csvRead(absPath, primaryKey, itemId, workspaceRoot) {
|
|
1322
|
-
const utf8Path = await ensureUtf8CsvPath(absPath, workspaceRoot ?? getWorkspaceRoot());
|
|
1323
|
-
if (utf8Path === null) return null;
|
|
1324
|
-
const rawKey = decodeCsvRecordId(itemId);
|
|
1325
|
-
const last = (await queryCsv(`SELECT * FROM (SELECT *, row_number() OVER () AS ${quoteIdent(ROW_ORDINAL)} FROM read_csv(${readCsvArgs(primaryKey)})) WHERE CAST(${quoteIdent(primaryKey)} AS VARCHAR) = ? ORDER BY ${quoteIdent(ROW_ORDINAL)} DESC LIMIT 1`, [utf8Path, rawKey])).at(0);
|
|
1326
|
-
if (last === void 0) return null;
|
|
1327
|
-
const { [ROW_ORDINAL]: __ordinal, ...record } = last;
|
|
1328
|
-
return csvRowToItem(record, primaryKey);
|
|
924
|
+
/** `calendarTimeField` must name a real top-level field (a free-form time
|
|
925
|
+
* string the day view parses). */
|
|
926
|
+
function calendarTimeFieldIsDeclared(schema) {
|
|
927
|
+
return schema.calendarTimeField === void 0 || declaredField(schema.fields, schema.calendarTimeField) !== void 0;
|
|
1329
928
|
}
|
|
1330
|
-
/**
|
|
1331
|
-
*
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
return
|
|
929
|
+
/** …and that field must be string-backed — the day view parses its value as a
|
|
930
|
+
* time string, so a number/enum/date column can't drive it. */
|
|
931
|
+
function calendarTimeFieldIsStringBacked(schema) {
|
|
932
|
+
return schema.calendarTimeField === void 0 || isTimeStringField(declaredField(schema.fields, schema.calendarTimeField)?.type);
|
|
933
|
+
}
|
|
934
|
+
/** `kanbanField` must name a real `enum` field — the board groups records into
|
|
935
|
+
* one column per declared enum value; any other type has no closed set of
|
|
936
|
+
* columns to group by. */
|
|
937
|
+
function kanbanFieldIsAnEnum(schema) {
|
|
938
|
+
return schema.kanbanField === void 0 || declaredField(schema.fields, schema.kanbanField)?.type === "enum";
|
|
939
|
+
}
|
|
940
|
+
/** `notifyWhen` narrows the completion bell, so it only means something with
|
|
941
|
+
* completion tracking. */
|
|
942
|
+
function notifyWhenRequiresCompletion(schema) {
|
|
943
|
+
return schema.notifyWhen === void 0 || schema.completionField !== void 0;
|
|
944
|
+
}
|
|
945
|
+
/** `notifyWhen.field` must name a real top-level field. */
|
|
946
|
+
function notifyWhenFieldIsDeclared(schema) {
|
|
947
|
+
return schema.notifyWhen === void 0 || declaredField(schema.fields, schema.notifyWhen.field) !== void 0;
|
|
948
|
+
}
|
|
949
|
+
/** Every custom view `id` must be a valid slug — it doubles as the view-mode
|
|
950
|
+
* selector key (`custom:<id>`) and the capability-token clamp key, both of
|
|
951
|
+
* which expect a path-safe token. */
|
|
952
|
+
function viewIdsAreSlugs(schema) {
|
|
953
|
+
return schema.views === void 0 || schema.views.every((view) => isSafeSlug(view.id));
|
|
954
|
+
}
|
|
955
|
+
/** Custom view ids must be unique so the selector + token clamp resolve
|
|
956
|
+
* unambiguously. */
|
|
957
|
+
function viewIdsAreUnique(schema) {
|
|
958
|
+
return hasUniqueIds(schema.views);
|
|
1340
959
|
}
|
|
1341
960
|
//#endregion
|
|
1342
|
-
//#region src/collection/
|
|
1343
|
-
/**
|
|
1344
|
-
*
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
*
|
|
1348
|
-
* ReadDirectoryChangesW reports filenames against the LONG path, but a watch
|
|
1349
|
-
* opened on a short path (`C:\Users\RUNNER~1\…` — what `os.tmpdir()` returns
|
|
1350
|
-
* on GitHub's Windows runners) keeps the short form. libuv's
|
|
1351
|
-
* `assert(!_wcsnicmp(filename, dir, dirlen))` in `src/win/fs-event.c` then
|
|
1352
|
-
* aborts the PROCESS on the first event — a native assert, so neither
|
|
1353
|
-
* `watcher.on("error")` nor a try/catch can contain it.
|
|
1354
|
-
*
|
|
1355
|
-
* POSIX is deliberately left alone: `realpath` there also collapses symlinks
|
|
1356
|
-
* (`/var` → `/private/var` on macOS), which we neither need nor want to
|
|
1357
|
-
* change. A failure falls back to the original path — worst case we are no
|
|
1358
|
-
* worse off than before. */
|
|
1359
|
-
function watchablePath(dir) {
|
|
1360
|
-
if (process.platform !== "win32") return dir;
|
|
1361
|
-
try {
|
|
1362
|
-
return realpathSync.native(dir);
|
|
1363
|
-
} catch {
|
|
1364
|
-
return dir;
|
|
1365
|
-
}
|
|
1366
|
-
}
|
|
1367
|
-
/** Watch `dir`, reporting each accepted filename. `accept` decides what is
|
|
1368
|
-
* noise; a null filename always passes (the platform didn't tell us which
|
|
1369
|
-
* file, so the caller must assume the worst). */
|
|
1370
|
-
async function watchDirectory(dir, accept, onHit) {
|
|
1371
|
-
try {
|
|
1372
|
-
await mkdir(dir, { recursive: true });
|
|
1373
|
-
const watcher = watch(watchablePath(dir), { persistent: false }, (_eventType, rawFilename) => {
|
|
1374
|
-
const filename = rawFilename === null ? null : String(rawFilename);
|
|
1375
|
-
if (filename !== null && !accept(filename)) return;
|
|
1376
|
-
onHit(filename);
|
|
1377
|
-
});
|
|
1378
|
-
watcher.on("error", (err) => {
|
|
1379
|
-
log.warn("collections", "fs watch error", {
|
|
1380
|
-
dir,
|
|
1381
|
-
error: String(err)
|
|
1382
|
-
});
|
|
1383
|
-
});
|
|
1384
|
-
return { close: () => watcher.close() };
|
|
1385
|
-
} catch (err) {
|
|
1386
|
-
log.warn("collections", "fs watch start failed", {
|
|
1387
|
-
dir,
|
|
1388
|
-
error: String(err)
|
|
1389
|
-
});
|
|
1390
|
-
return null;
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
/** Watch the single file `absPath` by watching its PARENT directory, so an
|
|
1394
|
-
* atomic replace can't strand the watch on a dead inode. `alsoAccept`
|
|
1395
|
-
* widens the filter beyond the exact basename (sqlite's `-wal`/`-journal`
|
|
1396
|
-
* sidecars). Reports are debounced: one replace, one call. */
|
|
1397
|
-
async function watchSingleFile(absPath, alsoAccept, onChange) {
|
|
1398
|
-
const dir = path.dirname(absPath);
|
|
1399
|
-
const base = path.basename(absPath);
|
|
1400
|
-
let timer = null;
|
|
1401
|
-
const fire = () => {
|
|
1402
|
-
if (timer) clearTimeout(timer);
|
|
1403
|
-
timer = setTimeout(() => {
|
|
1404
|
-
timer = null;
|
|
1405
|
-
onChange();
|
|
1406
|
-
}, REPLACE_DEBOUNCE_MS);
|
|
1407
|
-
timer.unref?.();
|
|
1408
|
-
};
|
|
1409
|
-
const handle = await watchDirectory(dir, (filename) => filename === base || alsoAccept(base, filename), fire);
|
|
1410
|
-
if (!handle) return null;
|
|
1411
|
-
return { close: () => {
|
|
1412
|
-
if (timer) clearTimeout(timer);
|
|
1413
|
-
timer = null;
|
|
1414
|
-
handle.close();
|
|
1415
|
-
} };
|
|
1416
|
-
}
|
|
1417
|
-
/** An `FsWatchHandle` as a bare unsubscribe — `null` straight through, so an
|
|
1418
|
-
* unarmed watch stays distinguishable from an armed one. Lives here rather
|
|
1419
|
-
* than beside the store contract so both `store.ts` and the backends it
|
|
1420
|
-
* registers can reach it without importing each other. */
|
|
1421
|
-
function closerFor(handle) {
|
|
1422
|
-
return handle === null ? null : () => handle.close();
|
|
1423
|
-
}
|
|
1424
|
-
//#endregion
|
|
1425
|
-
//#region src/collection/server/sqliteStore.ts
|
|
1426
|
-
/** A constructor's parameter and return types are not observable at runtime,
|
|
1427
|
-
* so the check stops at "DatabaseSync is constructible" — the only member of
|
|
1428
|
-
* the module this store ever touches. */
|
|
1429
|
-
function isSqliteModule(mod) {
|
|
1430
|
-
return isRecord(mod) && typeof mod.DatabaseSync === "function";
|
|
1431
|
-
}
|
|
1432
|
-
var sqliteModule = null;
|
|
1433
|
-
/** Drops the memo first so a later call can retry (e.g. tests stubbing the
|
|
1434
|
-
* runtime), then reports why the backend is unusable. */
|
|
1435
|
-
function sqliteUnavailable(reason) {
|
|
1436
|
-
sqliteModule = null;
|
|
1437
|
-
throw new BackendUnavailableError(`sqlite storage needs the node:sqlite module (Node.js >= 22.5) — this runtime cannot load it: ${reason}`);
|
|
1438
|
-
}
|
|
1439
|
-
/** Lazy-load node:sqlite once. A runtime without it (Node < 22.5) throws a
|
|
1440
|
-
* clearly-worded error the caller surfaces — never a bare MODULE_NOT_FOUND. */
|
|
1441
|
-
function loadSqlite() {
|
|
1442
|
-
sqliteModule ??= import("node:sqlite").then((mod) => isSqliteModule(mod) ? mod : sqliteUnavailable("the module exposes no DatabaseSync constructor"), (err) => sqliteUnavailable(String(err)));
|
|
1443
|
-
return sqliteModule;
|
|
1444
|
-
}
|
|
1445
|
-
/** The db file's on-disk state. A symlink or non-regular file is refused
|
|
1446
|
-
* (file-disclosure defense, same rule as io.ts record files); ENOENT is
|
|
1447
|
-
* just "no records yet". Any OTHER lstat failure (EACCES, EIO, …) is
|
|
1448
|
-
* rethrown so reads surface a real filesystem problem instead of
|
|
1449
|
-
* silently reporting an empty collection. */
|
|
1450
|
-
async function dbFileState(absPath) {
|
|
1451
|
-
try {
|
|
1452
|
-
return (await lstat(absPath)).isFile() ? "file" : "refused";
|
|
1453
|
-
} catch (err) {
|
|
1454
|
-
if (isErrorWithCode(err) && err.code === "ENOENT") return "missing";
|
|
1455
|
-
throw err;
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
1458
|
-
var CREATE_TABLE = "CREATE TABLE IF NOT EXISTS records (id TEXT PRIMARY KEY, record TEXT NOT NULL)";
|
|
1459
|
-
/** Open the database for one operation, classifying the two unavailable
|
|
1460
|
-
* states so callers can map them honestly (`refused` ⇒ path-escape,
|
|
1461
|
-
* `missing` ⇒ empty / not-found — conflating them would misreport a
|
|
1462
|
-
* containment escape as "item not found"). The containment pre-check runs
|
|
1463
|
-
* BEFORE mkdir even when the file is missing — `isContainedInRoot`
|
|
1464
|
-
* resolves through the closest existing ancestor, so a symlinked-away
|
|
1465
|
-
* parent can never make the recursive mkdir create directories outside
|
|
1466
|
-
* the workspace (same pre/post belt-and-suspenders as io.ts writes). */
|
|
1467
|
-
async function openDb(absPath, workspaceRoot, mode) {
|
|
1468
|
-
const state = await dbFileState(absPath);
|
|
1469
|
-
if (state === "refused") {
|
|
1470
|
-
log.warn("collections", "sqlite database refused: not a regular file", { path: absPath });
|
|
1471
|
-
return { kind: "refused" };
|
|
1472
|
-
}
|
|
1473
|
-
if (!isContainedInRoot(path.dirname(absPath), workspaceRoot)) {
|
|
1474
|
-
log.warn("collections", "sqlite refused: database dir escapes workspace via symlink", { path: absPath });
|
|
1475
|
-
return { kind: "refused" };
|
|
1476
|
-
}
|
|
1477
|
-
if (mode === "read" && state === "missing") return { kind: "missing" };
|
|
1478
|
-
if (mode === "write") {
|
|
1479
|
-
await mkdir(path.dirname(absPath), { recursive: true });
|
|
1480
|
-
if (!isContainedInRoot(path.dirname(absPath), workspaceRoot)) {
|
|
1481
|
-
log.warn("collections", "sqlite write refused: database dir escapes workspace via symlink (post-mkdir)", { path: absPath });
|
|
1482
|
-
return { kind: "refused" };
|
|
1483
|
-
}
|
|
1484
|
-
}
|
|
1485
|
-
const { DatabaseSync } = await loadSqlite();
|
|
1486
|
-
const database = new DatabaseSync(absPath);
|
|
1487
|
-
database.exec("PRAGMA busy_timeout = 5000");
|
|
1488
|
-
database.exec(CREATE_TABLE);
|
|
1489
|
-
return {
|
|
1490
|
-
kind: "ok",
|
|
1491
|
-
database
|
|
1492
|
-
};
|
|
1493
|
-
}
|
|
1494
|
-
/** Run `operation` against the database and always close it; unavailable
|
|
1495
|
-
* states resolve through `onUnavailable` so each caller maps `missing`
|
|
1496
|
-
* vs `refused` to its own result kind. */
|
|
1497
|
-
async function withDb(absPath, workspaceRoot, mode, onUnavailable, operation) {
|
|
1498
|
-
const handle = await openDb(absPath, workspaceRoot, mode);
|
|
1499
|
-
if (handle.kind !== "ok") return onUnavailable(handle.kind);
|
|
1500
|
-
try {
|
|
1501
|
-
return await operation(handle.database);
|
|
1502
|
-
} finally {
|
|
1503
|
-
handle.database.close();
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
var SQLITE_CONSTRAINT_PRIMARYKEY = 1555;
|
|
1507
|
-
var SQLITE_CONSTRAINT_UNIQUE = 2067;
|
|
1508
|
-
/** node:sqlite throws ERR_SQLITE_ERROR with the SQLite extended result
|
|
1509
|
-
* code on `errcode`. Checked structurally (message text kept only as a
|
|
1510
|
-
* fallback for runtimes that don't expose `errcode`). */
|
|
1511
|
-
function isUniqueConstraintError(err) {
|
|
1512
|
-
if (hasNumberProp(err, "errcode")) return err.errcode === SQLITE_CONSTRAINT_PRIMARYKEY || err.errcode === SQLITE_CONSTRAINT_UNIQUE;
|
|
1513
|
-
return String(err).includes("UNIQUE constraint");
|
|
1514
|
-
}
|
|
1515
|
-
function parseRow(raw) {
|
|
1516
|
-
if (typeof raw !== "string") return null;
|
|
1517
|
-
try {
|
|
1518
|
-
const parsed = JSON.parse(raw);
|
|
1519
|
-
return isRecord(parsed) ? parsed : null;
|
|
1520
|
-
} catch {
|
|
1521
|
-
return null;
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
/** One column of a result row. node:sqlite types rows as `unknown`, so a
|
|
1525
|
-
* value that is not a row object yields no column at all. */
|
|
1526
|
-
function readColumn(row, column) {
|
|
1527
|
-
return isRecord(row) ? row[column] : void 0;
|
|
1528
|
-
}
|
|
1529
|
-
function rowsToItems(rows) {
|
|
1530
|
-
return rows.map((row) => parseRow(readColumn(row, "record"))).filter((item) => item !== null);
|
|
1531
|
-
}
|
|
1532
|
-
/** node:sqlite hands back an integer column as `number`, or as `bigint` once
|
|
1533
|
-
* it leaves the safe-integer range — COUNT(*) can be either. */
|
|
1534
|
-
function countRecords(database) {
|
|
1535
|
-
const count = readColumn(database.prepare("SELECT COUNT(*) AS n FROM records").get(), "n");
|
|
1536
|
-
if (typeof count === "number") return count;
|
|
1537
|
-
if (typeof count === "bigint") return Number(count);
|
|
1538
|
-
throw new Error(`sqlite COUNT(*) returned no numeric row count (got ${typeof count})`);
|
|
1539
|
-
}
|
|
1540
|
-
async function sqliteList(absPath, workspaceRoot) {
|
|
1541
|
-
return withDb(absPath, workspaceRoot, "read", () => [], (database) => rowsToItems(database.prepare("SELECT record FROM records ORDER BY id").all()));
|
|
1542
|
-
}
|
|
1543
|
-
async function sqlitePage(absPath, primaryKey, opts, workspaceRoot) {
|
|
1544
|
-
const emptyPage = {
|
|
1545
|
-
items: [],
|
|
1546
|
-
total: 0,
|
|
1547
|
-
truncated: false
|
|
1548
|
-
};
|
|
1549
|
-
return withDb(absPath, workspaceRoot, "read", () => emptyPage, (database) => {
|
|
1550
|
-
const total = countRecords(database);
|
|
1551
|
-
const offset = Math.max(0, opts.offset ?? 0);
|
|
1552
|
-
const limit = opts.limit === void 0 ? -1 : Math.max(0, opts.limit);
|
|
1553
|
-
return {
|
|
1554
|
-
items: projectItemFields(rowsToItems(database.prepare("SELECT record FROM records ORDER BY id LIMIT ? OFFSET ?").all(limit, offset)), opts.fields, primaryKey),
|
|
1555
|
-
total,
|
|
1556
|
-
truncated: false
|
|
1557
|
-
};
|
|
1558
|
-
});
|
|
1559
|
-
}
|
|
1560
|
-
async function sqliteRead(absPath, itemId, workspaceRoot) {
|
|
1561
|
-
const safeId = safeRecordId(itemId);
|
|
1562
|
-
if (safeId === null) return null;
|
|
1563
|
-
return withDb(absPath, workspaceRoot, "read", () => null, (database) => {
|
|
1564
|
-
return parseRow(readColumn(database.prepare("SELECT record FROM records WHERE id = ?").get(safeId), "record"));
|
|
1565
|
-
});
|
|
1566
|
-
}
|
|
1567
|
-
async function sqliteWrite(absPath, itemId, item, opts) {
|
|
1568
|
-
const safeId = safeRecordId(itemId);
|
|
1569
|
-
if (safeId === null) return {
|
|
1570
|
-
kind: "invalid-id",
|
|
1571
|
-
itemId
|
|
1572
|
-
};
|
|
1573
|
-
const outcome = await withDb(absPath, opts.workspaceRoot, "write", () => ({
|
|
1574
|
-
kind: "path-escape",
|
|
1575
|
-
itemId: safeId
|
|
1576
|
-
}), (database) => {
|
|
1577
|
-
const payload = JSON.stringify(item);
|
|
1578
|
-
if (opts.refuseOverwrite) try {
|
|
1579
|
-
database.prepare("INSERT INTO records (id, record) VALUES (?, ?)").run(safeId, payload);
|
|
1580
|
-
} catch (err) {
|
|
1581
|
-
if (isUniqueConstraintError(err)) return {
|
|
1582
|
-
kind: "conflict",
|
|
1583
|
-
itemId: safeId
|
|
1584
|
-
};
|
|
1585
|
-
throw err;
|
|
1586
|
-
}
|
|
1587
|
-
else database.prepare("INSERT INTO records (id, record) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET record = excluded.record").run(safeId, payload);
|
|
1588
|
-
return {
|
|
1589
|
-
kind: "ok",
|
|
1590
|
-
itemId: safeId,
|
|
1591
|
-
item
|
|
1592
|
-
};
|
|
1593
|
-
});
|
|
1594
|
-
if (outcome.kind === "ok" && opts.slug) publishCollectionChange(collectionChangePayload({
|
|
1595
|
-
slug: opts.slug,
|
|
1596
|
-
ids: [safeId],
|
|
1597
|
-
op: "upsert"
|
|
1598
|
-
}, opts.publishRoot));
|
|
1599
|
-
return outcome;
|
|
1600
|
-
}
|
|
1601
|
-
async function sqliteDelete(absPath, itemId, opts) {
|
|
1602
|
-
const safeId = safeRecordId(itemId);
|
|
1603
|
-
if (safeId === null) return {
|
|
1604
|
-
kind: "invalid-id",
|
|
1605
|
-
itemId
|
|
1606
|
-
};
|
|
1607
|
-
const outcome = await withDb(absPath, opts.workspaceRoot, "read", (reason) => reason === "refused" ? {
|
|
1608
|
-
kind: "path-escape",
|
|
1609
|
-
itemId: safeId
|
|
1610
|
-
} : {
|
|
1611
|
-
kind: "not-found",
|
|
1612
|
-
itemId: safeId
|
|
1613
|
-
}, (database) => {
|
|
1614
|
-
const { changes } = database.prepare("DELETE FROM records WHERE id = ?").run(safeId);
|
|
1615
|
-
return Number(changes) === 0 ? {
|
|
1616
|
-
kind: "not-found",
|
|
1617
|
-
itemId: safeId
|
|
1618
|
-
} : {
|
|
1619
|
-
kind: "ok",
|
|
1620
|
-
itemId: safeId
|
|
1621
|
-
};
|
|
1622
|
-
});
|
|
1623
|
-
if (outcome.kind === "ok" && opts.slug) publishCollectionChange(collectionChangePayload({
|
|
1624
|
-
slug: opts.slug,
|
|
1625
|
-
ids: [safeId],
|
|
1626
|
-
op: "delete"
|
|
1627
|
-
}, opts.publishRoot));
|
|
1628
|
-
return outcome;
|
|
1629
|
-
}
|
|
1630
|
-
/** Best-effort full WAL checkpoint so the MAIN db file alone is a
|
|
1631
|
-
* complete snapshot (committed pages in `<db>-wal` are folded in and the
|
|
1632
|
-
* WAL truncated). Used by `deleteCollection` before archiving. Returns
|
|
1633
|
-
* false on any failure (runtime without node:sqlite, locked db, missing
|
|
1634
|
-
* file) — the caller then archives the sidecar files alongside the db so
|
|
1635
|
-
* no committed data is lost either way. */
|
|
1636
|
-
async function checkpointSqliteDatabase(absPath) {
|
|
1637
|
-
try {
|
|
1638
|
-
const { DatabaseSync } = await loadSqlite();
|
|
1639
|
-
const database = new DatabaseSync(absPath);
|
|
1640
|
-
try {
|
|
1641
|
-
database.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
1642
|
-
} finally {
|
|
1643
|
-
database.close();
|
|
1644
|
-
}
|
|
1645
|
-
return true;
|
|
1646
|
-
} catch {
|
|
1647
|
-
return false;
|
|
1648
|
-
}
|
|
1649
|
-
}
|
|
1650
|
-
/** A `storage: sqlite` store over `collection.storageFile`. A schema whose
|
|
1651
|
-
* `storageFile` failed to resolve yields a read-only EMPTY store rather
|
|
1652
|
-
* than a writable one — same fail-closed rule as the CSV store. */
|
|
1653
|
-
function sqliteStoreFor(collection, opts) {
|
|
1654
|
-
const file = collection.storageFile;
|
|
1655
|
-
const key = collection.schema.primaryKey;
|
|
1656
|
-
const slug = opts.slug ?? collection.slug;
|
|
1657
|
-
const root = () => opts.workspaceRoot ?? getWorkspaceRoot();
|
|
1658
|
-
const publishRoot = opts.workspaceRoot;
|
|
1659
|
-
if (file === void 0) return {
|
|
1660
|
-
capabilities: {
|
|
1661
|
-
writable: false,
|
|
1662
|
-
nativeQuery: false,
|
|
1663
|
-
nativePaging: false
|
|
1664
|
-
},
|
|
1665
|
-
list: () => Promise.resolve([]),
|
|
1666
|
-
page: () => Promise.resolve({
|
|
1667
|
-
items: [],
|
|
1668
|
-
total: 0,
|
|
1669
|
-
truncated: false
|
|
1670
|
-
}),
|
|
1671
|
-
read: () => Promise.resolve(null)
|
|
1672
|
-
};
|
|
1673
|
-
return {
|
|
1674
|
-
capabilities: {
|
|
1675
|
-
writable: true,
|
|
1676
|
-
nativeQuery: false,
|
|
1677
|
-
nativePaging: true
|
|
1678
|
-
},
|
|
1679
|
-
list: () => sqliteList(file, root()),
|
|
1680
|
-
page: (pageOpts = {}) => sqlitePage(file, key, pageOpts, root()),
|
|
1681
|
-
read: (itemId) => sqliteRead(file, itemId, root()),
|
|
1682
|
-
write: (itemId, item, writeOpts = {}) => sqliteWrite(file, itemId, item, {
|
|
1683
|
-
workspaceRoot: root(),
|
|
1684
|
-
publishRoot,
|
|
1685
|
-
slug,
|
|
1686
|
-
refuseOverwrite: writeOpts.refuseOverwrite
|
|
1687
|
-
}),
|
|
1688
|
-
delete: (itemId) => sqliteDelete(file, itemId, {
|
|
1689
|
-
workspaceRoot: root(),
|
|
1690
|
-
publishRoot,
|
|
1691
|
-
slug
|
|
1692
|
-
}),
|
|
1693
|
-
watch: async (onChange) => closerFor(await watchSingleFile(file, (base, name) => name.startsWith(base), () => onChange({ kind: "collection" })))
|
|
1694
|
-
};
|
|
1695
|
-
}
|
|
1696
|
-
//#endregion
|
|
1697
|
-
//#region src/collection/server/store.ts
|
|
1698
|
-
/** The file store's stable order: lexicographic by record id (codepoint
|
|
1699
|
-
* compare — locale-independent). `listItems` returns readdir order, which
|
|
1700
|
-
* is filesystem-dependent; paging needs determinism. */
|
|
1701
|
-
function sortByRecordId(items, primaryKey) {
|
|
1702
|
-
return [...items].sort((left, right) => {
|
|
1703
|
-
const leftId = fieldText(left[primaryKey]);
|
|
1704
|
-
const rightId = fieldText(right[primaryKey]);
|
|
1705
|
-
if (leftId < rightId) return -1;
|
|
1706
|
-
return leftId > rightId ? 1 : 0;
|
|
1707
|
-
});
|
|
1708
|
-
}
|
|
1709
|
-
/** True when the collection accepts UI/tool writes. A `dataSource`
|
|
1710
|
-
* collection is read-only: updates happen by editing/replacing the
|
|
1711
|
-
* data file itself. Every write entry point checks this BEFORE calling
|
|
1712
|
-
* `writeItem`/`deleteItem` — server-enforced, not just UI-hidden. */
|
|
1713
|
-
function collectionWritable(collection) {
|
|
1714
|
-
return !isReadOnlySchema(collection.schema);
|
|
1715
|
-
}
|
|
1716
|
-
/** The one-line refusal write paths surface (HTTP 405 / MCP error text). */
|
|
1717
|
-
function readOnlyRefusal(slug) {
|
|
1718
|
-
return `collection '${slug}' is read-only (backed by an external dataSource) — update the data file itself instead`;
|
|
1719
|
-
}
|
|
1720
|
-
/** A `dataSource` store over `file` (CSV row order; DuckDB-native query).
|
|
1721
|
-
* A schema whose `dataSourceFile` failed to resolve yields a read-only
|
|
1722
|
-
* EMPTY store rather than falling back to the (writable) file store — a
|
|
1723
|
-
* half-loaded read-only collection must never become writable. */
|
|
1724
|
-
function csvStoreFor(collection, opts) {
|
|
1725
|
-
const file = collection.dataSourceFile;
|
|
1726
|
-
const key = collection.schema.primaryKey;
|
|
1727
|
-
const listAll = () => file === void 0 ? Promise.resolve({
|
|
1728
|
-
items: [],
|
|
1729
|
-
truncated: false
|
|
1730
|
-
}) : csvList(file, key, opts.workspaceRoot);
|
|
1731
|
-
return {
|
|
1732
|
-
capabilities: {
|
|
1733
|
-
writable: false,
|
|
1734
|
-
nativeQuery: true,
|
|
1735
|
-
nativePaging: false
|
|
1736
|
-
},
|
|
1737
|
-
list: () => listAll().then((result) => result.items),
|
|
1738
|
-
page: (pageOpts = {}) => listAll().then((result) => pageFromFullRead(result.items, pageOpts, key, result.truncated)),
|
|
1739
|
-
read: (itemId) => file === void 0 ? Promise.resolve(null) : csvRead(file, key, itemId, opts.workspaceRoot),
|
|
1740
|
-
query: (query) => file === void 0 ? Promise.resolve([]) : csvRunQuery(file, key, query, opts.workspaceRoot),
|
|
1741
|
-
...file === void 0 ? {} : { watch: async (onChange) => closerFor(await watchSingleFile(file, () => false, () => onChange({ kind: "collection" }))) }
|
|
1742
|
-
};
|
|
1743
|
-
}
|
|
1744
|
-
/** The classic file store over `<dataDir>/<itemId>.json` records. */
|
|
1745
|
-
function fileStoreFor(collection, opts) {
|
|
1746
|
-
const key = collection.schema.primaryKey;
|
|
1747
|
-
const ioOpts = {
|
|
1748
|
-
...opts,
|
|
1749
|
-
slug: opts.slug ?? collection.slug
|
|
1750
|
-
};
|
|
1751
|
-
return {
|
|
1752
|
-
capabilities: {
|
|
1753
|
-
writable: true,
|
|
1754
|
-
nativeQuery: false,
|
|
1755
|
-
nativePaging: false
|
|
1756
|
-
},
|
|
1757
|
-
list: () => listItems(collection.dataDir, opts),
|
|
1758
|
-
page: async (pageOpts = {}) => pageFromFullRead(sortByRecordId(await listItems(collection.dataDir, opts), key), pageOpts, key, false),
|
|
1759
|
-
read: (itemId) => readItem(collection.dataDir, itemId, opts),
|
|
1760
|
-
write: (itemId, item, writeOpts = {}) => writeItem(collection.dataDir, itemId, item, {
|
|
1761
|
-
...ioOpts,
|
|
1762
|
-
refuseOverwrite: writeOpts.refuseOverwrite
|
|
1763
|
-
}),
|
|
1764
|
-
delete: (itemId) => deleteItem(collection.dataDir, itemId, ioOpts),
|
|
1765
|
-
watch: async (onChange) => closerFor(await watchDirectory(collection.dataDir, (name) => name.endsWith(".json") && !name.startsWith("."), (filename) => onChange(filename === null ? { kind: "collection" } : {
|
|
1766
|
-
kind: "item",
|
|
1767
|
-
itemId: filename.slice(0, -5)
|
|
1768
|
-
})))
|
|
1769
|
-
};
|
|
1770
|
-
}
|
|
1771
|
-
var storeFactories = /* @__PURE__ */ new Map([
|
|
1772
|
-
["file", fileStoreFor],
|
|
1773
|
-
["csv", csvStoreFor],
|
|
1774
|
-
["sqlite", sqliteStoreFor],
|
|
1775
|
-
["firestore", firestoreStoreFor]
|
|
1776
|
-
]);
|
|
1777
|
-
/** Pick the store implementation for a discovered collection via the
|
|
1778
|
-
* factory registry. An unknown kind cannot normally reach here (the
|
|
1779
|
-
* schema's `StorageZ` union gates it), so the throw is a loud invariant
|
|
1780
|
-
* breach, not a user-facing path. */
|
|
1781
|
-
function storeFor(collection, opts = {}) {
|
|
1782
|
-
const kind = storageKindFor(collection.schema);
|
|
1783
|
-
const factory = storeFactories.get(kind);
|
|
1784
|
-
if (!factory) throw new Error(`no store factory registered for storage kind '${kind}'`);
|
|
1785
|
-
return factory(collection, opts);
|
|
1786
|
-
}
|
|
1787
|
-
/** The param name a `set` value references, or null when the value is a
|
|
1788
|
-
* literal (non-strings can never be references). A bare/empty prefix
|
|
1789
|
-
* (`"$params."`) returns the empty string — the schema refine rejects
|
|
1790
|
-
* it as an undeclared param, never silently treats it as a literal. */
|
|
1791
|
-
function paramRefName(value) {
|
|
1792
|
-
if (typeof value !== "string" || !value.startsWith("$params.")) return null;
|
|
1793
|
-
return value.slice(8);
|
|
1794
|
-
}
|
|
1795
|
-
/** Resolve a mutate action's `set` map against the submitted params:
|
|
1796
|
-
* literals pass through, `$params.<name>` reads the param value. An
|
|
1797
|
-
* ABSENT referenced param omits the key entirely (merge semantics —
|
|
1798
|
-
* the stored value survives), mirroring how the record form omits
|
|
1799
|
-
* empty optionals rather than writing empty strings. */
|
|
1800
|
-
function resolveMutateSet(set, params) {
|
|
1801
|
-
const resolved = {};
|
|
1802
|
-
for (const [key, value] of Object.entries(set)) {
|
|
1803
|
-
const ref = paramRefName(value);
|
|
1804
|
-
if (ref === null) {
|
|
1805
|
-
resolved[key] = value;
|
|
1806
|
-
continue;
|
|
1807
|
-
}
|
|
1808
|
-
const paramValue = params[ref];
|
|
1809
|
-
if (paramValue !== void 0 && paramValue !== null && paramValue !== "") resolved[key] = paramValue;
|
|
1810
|
-
}
|
|
1811
|
-
return resolved;
|
|
1812
|
-
}
|
|
1813
|
-
//#endregion
|
|
1814
|
-
//#region src/collection/core/schemaRules.ts
|
|
1815
|
-
var declaredField = (fields, name) => Object.hasOwn(fields, name) ? fields[name] : void 0;
|
|
1816
|
-
var isDateLike = (type) => type === "date" || type === "datetime";
|
|
1817
|
-
var isTimeStringField = (type) => type === "string" || type === "text";
|
|
1818
|
-
var CODE_FIELD_TYPES = /* @__PURE__ */ new Set([
|
|
1819
|
-
"string",
|
|
1820
|
-
"text",
|
|
1821
|
-
"enum"
|
|
1822
|
-
]);
|
|
1823
|
-
var namesStoredField = (fields, name, primaryKey) => {
|
|
1824
|
-
const target = declaredField(fields, name);
|
|
1825
|
-
return target !== void 0 && !COMPUTED_TYPES.has(target.type) && name !== primaryKey;
|
|
1826
|
-
};
|
|
1827
|
-
var hasUniqueIds = (entries) => entries === void 0 || new Set(entries.map((entry) => entry.id)).size === entries.length;
|
|
1828
|
-
/** Exactly one storage declaration: native records need `dataPath`, an external
|
|
1829
|
-
* data file needs `dataSource`, an alternative backend needs `storage`. Zero
|
|
1830
|
-
* (nowhere to read) and several (ambiguous which wins) are equally
|
|
1831
|
-
* meaningless — fail loudly at load instead of picking silently. */
|
|
1832
|
-
function declaresExactlyOneStore(schema) {
|
|
1833
|
-
return [
|
|
1834
|
-
schema.dataPath,
|
|
1835
|
-
schema.dataSource,
|
|
1836
|
-
schema.storage
|
|
1837
|
-
].filter((declared) => declared !== void 0).length === 1;
|
|
1838
|
-
}
|
|
1839
|
-
/** A `dataSource` collection is read-only by definition, so schema-level write
|
|
1840
|
-
* machinery can never fire: `singleton` pins CREATES, `ingest` REFILLS
|
|
1841
|
-
* records, `spawn` WRITES successor records. Rejecting them at validation
|
|
1842
|
-
* kills whole classes of writes before any runtime guard. */
|
|
1843
|
-
function dataSourceDeclaresNoWriteMachinery(schema) {
|
|
1844
|
-
if (schema.dataSource === void 0) return true;
|
|
1845
|
-
return schema.singleton === void 0 && schema.ingest === void 0 && schema.spawn === void 0 && schema.googleCalendar === void 0;
|
|
1846
|
-
}
|
|
1847
|
-
/** Same rule for declarative host writes: a mutate action writes the record
|
|
1848
|
-
* it's invoked on, which a read-only collection has no business doing. */
|
|
1849
|
-
function dataSourceDeclaresNoMutateAction(schema) {
|
|
1850
|
-
if (schema.dataSource !== void 0) return [...schema.actions ?? [], ...schema.collectionActions ?? []].every((action) => action.kind !== "mutate");
|
|
1851
|
-
return true;
|
|
1852
|
-
}
|
|
1853
|
-
/** Action ids must be unique so the dispatch route resolves unambiguously. */
|
|
1854
|
-
function actionIdsAreUnique(schema) {
|
|
1855
|
-
return hasUniqueIds(schema.actions);
|
|
1856
|
-
}
|
|
1857
|
-
/** Collection-level action ids must likewise be unique. */
|
|
1858
|
-
function collectionActionIdsAreUnique(schema) {
|
|
1859
|
-
return hasUniqueIds(schema.collectionActions);
|
|
1860
|
-
}
|
|
1861
|
-
/** A mutate action's `set` writes real STORED fields: a typo'd key would write
|
|
1862
|
-
* a stray value forever, a computed/projected field is never persisted, and
|
|
1863
|
-
* the primaryKey is the filename (renaming is not a mutation). */
|
|
1864
|
-
function mutateSetKeysNameStoredFields(schema) {
|
|
1865
|
-
return (schema.actions ?? []).every((action) => action.kind !== "mutate" || Object.keys(action.set).every((key) => namesStoredField(schema.fields, key, schema.primaryKey)));
|
|
1866
|
-
}
|
|
1867
|
-
/** Every `$params.<name>` reference in `set` must name a declared param — an
|
|
1868
|
-
* undeclared one would silently no-op the assignment. */
|
|
1869
|
-
function mutateParamRefsAreDeclared(schema) {
|
|
1870
|
-
return (schema.actions ?? []).every((action) => action.kind !== "mutate" || Object.values(action.set).every((value) => {
|
|
1871
|
-
const ref = paramRefName(value);
|
|
1872
|
-
return ref === null || (action.params ?? {})[ref] !== void 0;
|
|
1873
|
-
}));
|
|
1874
|
-
}
|
|
1875
|
-
/** A collection-level action has no record to write. */
|
|
1876
|
-
function collectionActionsAreNotMutate(schema) {
|
|
1877
|
-
return (schema.collectionActions ?? []).every((action) => action.kind !== "mutate");
|
|
1878
|
-
}
|
|
1879
|
-
/** The singleton value becomes a record id (and thus a `<id>.json` filename),
|
|
1880
|
-
* so it must satisfy the SAME record-id rule the write path enforces —
|
|
1881
|
-
* otherwise the create form would lock the primary key to a value the POST
|
|
1882
|
-
* route then rejects, making the collection impossible to initialize. */
|
|
1883
|
-
function singletonIsAValidRecordId(schema) {
|
|
1884
|
-
return schema.singleton === void 0 || isSafeRecordId(schema.singleton);
|
|
1885
|
-
}
|
|
1886
|
-
function collectCurrencyFieldRefs(fields) {
|
|
1887
|
-
const refs = [];
|
|
1888
|
-
for (const field of Object.values(fields)) {
|
|
1889
|
-
if (typeof field.currencyField === "string" && field.currencyField.length > 0) refs.push(field.currencyField);
|
|
1890
|
-
for (const sub of Object.values(field.of ?? {})) if (typeof sub.currencyField === "string" && sub.currencyField.length > 0) refs.push(sub.currencyField);
|
|
1891
|
-
}
|
|
1892
|
-
return refs;
|
|
1893
|
-
}
|
|
1894
|
-
/** A `currencyField` pointer must name a real top-level field that holds a code
|
|
1895
|
-
* string — a typo (`curreny`) would otherwise pass the per-field check, then
|
|
1896
|
-
* silently fall back to the literal / USD at render and mislabel amounts. */
|
|
1897
|
-
function currencyFieldRefsNameCodeFields(schema) {
|
|
1898
|
-
return collectCurrencyFieldRefs(schema.fields).every((name) => CODE_FIELD_TYPES.has(declaredField(schema.fields, name)?.type ?? ""));
|
|
1899
|
-
}
|
|
1900
|
-
/** The pair must be declared together — one without the other is meaningless:
|
|
1901
|
-
* the host would either never fire (no done values to compare against) or
|
|
1902
|
-
* never clear (no field to read).
|
|
1903
|
-
*
|
|
1904
|
-
* EXCEPTION: when `completionField` names a `flag` field, done ⇔ the flag's
|
|
1905
|
-
* `where` matches, so `completionDoneValues` carries no information and MUST
|
|
1906
|
-
* be omitted (declaring it would invite a contradictory second source of
|
|
1907
|
-
* truth). */
|
|
1908
|
-
function completionPairIsCoherent(schema) {
|
|
1909
|
-
if (schema.completionField !== void 0 && declaredField(schema.fields, schema.completionField)?.type === "flag") return schema.completionDoneValues === void 0;
|
|
1910
|
-
return schema.completionField === void 0 === (schema.completionDoneValues === void 0);
|
|
1911
|
-
}
|
|
1912
|
-
/** `completionField` must name a real top-level field — a typo would silently
|
|
1913
|
-
* disable the notification mechanism otherwise. */
|
|
1914
|
-
function completionFieldIsDeclared(schema) {
|
|
1915
|
-
return schema.completionField === void 0 || declaredField(schema.fields, schema.completionField) !== void 0;
|
|
1916
|
-
}
|
|
1917
|
-
/** A flag named by `completionField` is evaluated against the RAW record — the
|
|
1918
|
-
* reconciler (and spawn's fallback) read items straight off disk, BEFORE any
|
|
1919
|
-
* `deriveAll` enrichment — so its `where` may only reference STORED fields. A
|
|
1920
|
-
* condition over a computed sibling would see an absent key: `ne` matches
|
|
1921
|
-
* vacuously, every other op reads false, and the bell would clear wrongly /
|
|
1922
|
-
* never. General (non-completion) flags keep the full vocabulary — the UI
|
|
1923
|
-
* evaluates them post-enrichment. */
|
|
1924
|
-
function completionFlagReadsOnlyStoredFields(schema) {
|
|
1925
|
-
const spec = schema.completionField === void 0 ? void 0 : declaredField(schema.fields, schema.completionField);
|
|
1926
|
-
if (spec?.type !== "flag") return true;
|
|
1927
|
-
return spec.where.every((cond) => [cond.field, ...cond.valueFrom ? [cond.valueFrom.field] : []].every((name) => {
|
|
1928
|
-
const target = declaredField(schema.fields, name);
|
|
1929
|
-
return target !== void 0 && !COMPUTED_TYPES.has(target.type);
|
|
1930
|
-
}));
|
|
1931
|
-
}
|
|
1932
|
-
/** `displayField`, like `completionField`, must name a real top-level field —
|
|
1933
|
-
* a typo would silently fall back to the primaryKey forever. */
|
|
1934
|
-
function displayFieldIsDeclared(schema) {
|
|
1935
|
-
return schema.displayField === void 0 || declaredField(schema.fields, schema.displayField) !== void 0;
|
|
1936
|
-
}
|
|
1937
|
-
/** A field's `when.field` gates its visibility against a sibling's value, so it
|
|
1938
|
-
* must name a real top-level field — a typo would silently keep the field
|
|
1939
|
-
* hidden forever (the gate never matches). */
|
|
1940
|
-
function fieldVisibilityGatesNameDeclaredFields(schema) {
|
|
1941
|
-
return Object.values(schema.fields).every((field) => field.when === void 0 || declaredField(schema.fields, field.when.field) !== void 0);
|
|
1942
|
-
}
|
|
1943
|
-
/** A flag's `where` reads sibling fields (both `cond.field` and a same-record
|
|
1944
|
-
* `valueFrom.field`), so each must name a real top-level field — a typo would
|
|
1945
|
-
* silently pin the flag false forever (`ne`: true forever). */
|
|
1946
|
-
function flagConditionsNameDeclaredFields(schema) {
|
|
1947
|
-
return Object.values(schema.fields).every((field) => field.type !== "flag" || field.where.every((cond) => declaredField(schema.fields, cond.field) !== void 0 && (cond.valueFrom === void 0 || declaredField(schema.fields, cond.valueFrom.field) !== void 0)));
|
|
1948
|
-
}
|
|
1949
|
-
/** An `embed`'s `idField` resolves the target record id from a sibling's value,
|
|
1950
|
-
* so it must name a real top-level field — and one whose stored value is a
|
|
1951
|
-
* plain id string. Only `ref` / `string` qualify: the editor writes the picked
|
|
1952
|
-
* id into that field, so a non-persisted or composite type would either not
|
|
1953
|
-
* round-trip on save or hold no usable id. */
|
|
1954
|
-
function embedIdFieldsNameIdBearingFields(schema) {
|
|
1955
|
-
return Object.values(schema.fields).every((field) => {
|
|
1956
|
-
if (field.type !== "embed" || field.idField === void 0) return true;
|
|
1957
|
-
const target = declaredField(schema.fields, field.idField);
|
|
1958
|
-
return target !== void 0 && (target.type === "ref" || target.type === "string");
|
|
1959
|
-
});
|
|
1960
|
-
}
|
|
1961
|
-
/** The sync writes each mapped value into a declared field, and puts the Google
|
|
1962
|
-
* event id in the primary field — so a map key that names no field (or names
|
|
1963
|
-
* the primary) would silently drop data or fight the id. */
|
|
1964
|
-
function googleCalendarMapNamesStoredFields(schema) {
|
|
1965
|
-
if (schema.googleCalendar === void 0) return true;
|
|
1966
|
-
return Object.keys(schema.googleCalendar.map).every((key) => namesStoredField(schema.fields, key, schema.primaryKey));
|
|
1967
|
-
}
|
|
1968
|
-
/** A `toggle` field projects an `enum` field: its `field` must name a real
|
|
1969
|
-
* top-level enum, and `onValue` / `offValue` must be members of that enum's
|
|
1970
|
-
* `values` — otherwise toggling would write a value outside the closed set
|
|
1971
|
-
* (and never appear "checked"). */
|
|
1972
|
-
function togglesProjectValidEnums(schema) {
|
|
1973
|
-
const { fields } = schema;
|
|
1974
|
-
for (const spec of Object.values(fields)) {
|
|
1975
|
-
if (spec.type !== "toggle") continue;
|
|
1976
|
-
const target = declaredField(fields, spec.field);
|
|
1977
|
-
if (!target || target.type !== "enum") return false;
|
|
1978
|
-
const allowed = new Set(target.values);
|
|
1979
|
-
if (!allowed.has(spec.onValue) || !allowed.has(spec.offValue)) return false;
|
|
1980
|
-
}
|
|
1981
|
-
return true;
|
|
1982
|
-
}
|
|
1983
|
-
/** `triggerField` requires the completion pair: the time gate only suppresses
|
|
1984
|
-
* the *completion* bell until the date, and the bell still clears via
|
|
1985
|
-
* `completionDoneValues`. Without completion there is no bell to gate. */
|
|
1986
|
-
function triggerFieldRequiresCompletion(schema) {
|
|
1987
|
-
return schema.triggerField === void 0 || schema.completionField !== void 0;
|
|
1988
|
-
}
|
|
1989
|
-
/** `triggerField` must name a real `date` field — the gate parses its value as
|
|
1990
|
-
* `YYYY-MM-DD`; any other type can't be compared to the clock. */
|
|
1991
|
-
function triggerFieldIsADateField(schema) {
|
|
1992
|
-
return schema.triggerField === void 0 || declaredField(schema.fields, schema.triggerField)?.type === "date";
|
|
1993
|
-
}
|
|
1994
|
-
/** `triggerLeadDays` only means something relative to a trigger date. */
|
|
1995
|
-
function triggerLeadDaysRequiresTriggerField(schema) {
|
|
1996
|
-
return schema.triggerLeadDays === void 0 || schema.triggerField !== void 0;
|
|
1997
|
-
}
|
|
1998
|
-
/** `spawn` advances `triggerField` to compute the successor's trigger date, so
|
|
1999
|
-
* the schema must declare one. */
|
|
2000
|
-
function spawnRequiresTriggerField(schema) {
|
|
2001
|
-
return schema.spawn === void 0 || schema.triggerField !== void 0;
|
|
2002
|
-
}
|
|
2003
|
-
/** `spawn.when.field` must name a real top-level field — a typo would silently
|
|
2004
|
-
* never match. */
|
|
2005
|
-
function spawnWhenFieldIsDeclared(schema) {
|
|
2006
|
-
return schema.spawn?.when === void 0 || declaredField(schema.fields, schema.spawn.when.field) !== void 0;
|
|
2007
|
-
}
|
|
2008
|
-
/** Every `spawn.carry` entry must name a real top-level field — a typo would
|
|
2009
|
-
* silently never copy. */
|
|
2010
|
-
function spawnCarryEntriesAreDeclared(schema) {
|
|
2011
|
-
return (schema.spawn?.carry ?? []).every((name) => declaredField(schema.fields, name) !== void 0);
|
|
2012
|
-
}
|
|
2013
|
-
/** A successor must NOT be born already matching its own spawn predicate — it
|
|
2014
|
-
* would re-spawn on its first reconcile, fanning out into an unbounded chain
|
|
2015
|
-
* of records. The predicate field/values are `spawn.when` when given, else the
|
|
2016
|
-
* completion-done pair. The successor's value for that field is `set[field]`
|
|
2017
|
-
* if set, else the carried source value (which matched, by definition, when
|
|
2018
|
-
* the spawn fired) if carried, else absent (safe). */
|
|
2019
|
-
function spawnSuccessorStartsInert(schema) {
|
|
2020
|
-
const { spawn } = schema;
|
|
2021
|
-
if (!spawn) return true;
|
|
2022
|
-
const field = spawn.when?.field ?? schema.completionField;
|
|
2023
|
-
const values = spawn.when?.in ?? schema.completionDoneValues;
|
|
2024
|
-
if (!field || !values) return true;
|
|
2025
|
-
if (spawn.set && Object.prototype.hasOwnProperty.call(spawn.set, field)) return !values.includes(String(spawn.set[field]));
|
|
2026
|
-
return !(spawn.carry ?? []).includes(field);
|
|
2027
|
-
}
|
|
2028
|
-
/** `spawnSuccessorStartsInert` cannot see through a flag's `where` (the
|
|
2029
|
-
* predicate would need full record evaluation against `set`/`carry`). So a
|
|
2030
|
-
* schema whose completion is flag-form may only spawn with an explicit
|
|
2031
|
-
* `spawn.when` — which that check CAN evaluate. */
|
|
2032
|
-
function flagCompletionSpawnDeclaresWhen(schema) {
|
|
2033
|
-
return schema.spawn === void 0 || schema.spawn.when !== void 0 || declaredField(schema.fields, schema.completionField ?? "")?.type !== "flag";
|
|
2034
|
-
}
|
|
2035
|
-
function fieldDrivenSpawnEvery(schema) {
|
|
2036
|
-
const every = schema.spawn?.every;
|
|
2037
|
-
if (!every || !("fromField" in every)) return null;
|
|
2038
|
-
return every;
|
|
2039
|
-
}
|
|
2040
|
-
/** §4.1 — `fromField` must name a real top-level `enum` field. The `map` keys
|
|
2041
|
-
* are only meaningful against a closed value set, and the field renders as a
|
|
2042
|
-
* form `<select>`; a non-enum target has no finite values to validate. */
|
|
2043
|
-
function fieldDrivenFromFieldIsEnum(schema) {
|
|
2044
|
-
const driven = fieldDrivenSpawnEvery(schema);
|
|
2045
|
-
if (!driven) return true;
|
|
2046
|
-
return declaredField(schema.fields, driven.fromField)?.type === "enum";
|
|
2047
|
-
}
|
|
2048
|
-
/** §4.2 — `map` keys must EXACTLY cover the enum's `values` (no missing keys —
|
|
2049
|
-
* a record could pick an unmapped frequency and silently stall; no extra keys
|
|
2050
|
-
* — a stale map outliving an enum edit). */
|
|
2051
|
-
function fieldDrivenMapCoversValues(schema) {
|
|
2052
|
-
const driven = fieldDrivenSpawnEvery(schema);
|
|
2053
|
-
if (!driven) return true;
|
|
2054
|
-
const target = declaredField(schema.fields, driven.fromField);
|
|
2055
|
-
if (target?.type !== "enum") return true;
|
|
2056
|
-
const values = new Set(target.values);
|
|
2057
|
-
const keys = Object.keys(driven.map);
|
|
2058
|
-
return keys.length === values.size && keys.every((key) => values.has(key));
|
|
2059
|
-
}
|
|
2060
|
-
/** §4.5 — `fromField` must reach the successor (via `carry` or `set`);
|
|
2061
|
-
* otherwise the successor loses its frequency and the NEXT spawn along the
|
|
2062
|
-
* chain can't resolve an interval, silently halting the recurrence.
|
|
2063
|
-
*
|
|
2064
|
-
* `set` writes a FIXED value, so it must itself be a key of `map` (else the
|
|
2065
|
-
* successor is born with an unresolvable driver and `resolveEvery` skips it —
|
|
2066
|
-
* the exact silent-halt §4.5 exists to prevent). `carry` copies the source's
|
|
2067
|
-
* own value, which — for a record that matched the spawn — is one of the
|
|
2068
|
-
* enum's values, all of which `map` covers by §4.2; so a carried driver is
|
|
2069
|
-
* always resolvable and needs no value check here. */
|
|
2070
|
-
function fieldDrivenFromFieldCarried(schema) {
|
|
2071
|
-
const driven = fieldDrivenSpawnEvery(schema);
|
|
2072
|
-
if (!driven) return true;
|
|
2073
|
-
const { carry, set } = schema.spawn ?? {};
|
|
2074
|
-
if (set && Object.prototype.hasOwnProperty.call(set, driven.fromField)) {
|
|
2075
|
-
const raw = set[driven.fromField];
|
|
2076
|
-
if (raw === void 0 || raw === null || raw === "") return false;
|
|
2077
|
-
const key = fieldTextOrNull(raw);
|
|
2078
|
-
return key !== null && Object.prototype.hasOwnProperty.call(driven.map, key);
|
|
2079
|
-
}
|
|
2080
|
-
return (carry ?? []).includes(driven.fromField);
|
|
2081
|
-
}
|
|
2082
|
-
/** `calendarField` must name a real `date`/`datetime` field — the calendar view
|
|
2083
|
-
* parses its value to place records on the month grid (a `datetime` anchor
|
|
2084
|
-
* also carries the clock for the day view). */
|
|
2085
|
-
function calendarFieldIsDateLike(schema) {
|
|
2086
|
-
return schema.calendarField === void 0 || isDateLike(declaredField(schema.fields, schema.calendarField)?.type);
|
|
2087
|
-
}
|
|
2088
|
-
/** `calendarEndField` marks the end of a multi-day span, so it only means
|
|
2089
|
-
* something alongside a start anchor. */
|
|
2090
|
-
function calendarEndFieldRequiresCalendarField(schema) {
|
|
2091
|
-
return schema.calendarEndField === void 0 || schema.calendarField !== void 0;
|
|
2092
|
-
}
|
|
2093
|
-
/** `calendarEndField` must also name a real `date`/`datetime` field — same parse. */
|
|
2094
|
-
function calendarEndFieldIsDateLike(schema) {
|
|
2095
|
-
return schema.calendarEndField === void 0 || isDateLike(declaredField(schema.fields, schema.calendarEndField)?.type);
|
|
2096
|
-
}
|
|
2097
|
-
/** `calendarTimeField` places records on the day view, so it only means
|
|
2098
|
-
* something alongside a start anchor. */
|
|
2099
|
-
function calendarTimeFieldRequiresCalendarField(schema) {
|
|
2100
|
-
return schema.calendarTimeField === void 0 || schema.calendarField !== void 0;
|
|
2101
|
-
}
|
|
2102
|
-
/** `calendarTimeField` must name a real top-level field (a free-form time
|
|
2103
|
-
* string the day view parses). */
|
|
2104
|
-
function calendarTimeFieldIsDeclared(schema) {
|
|
2105
|
-
return schema.calendarTimeField === void 0 || declaredField(schema.fields, schema.calendarTimeField) !== void 0;
|
|
2106
|
-
}
|
|
2107
|
-
/** …and that field must be string-backed — the day view parses its value as a
|
|
2108
|
-
* time string, so a number/enum/date column can't drive it. */
|
|
2109
|
-
function calendarTimeFieldIsStringBacked(schema) {
|
|
2110
|
-
return schema.calendarTimeField === void 0 || isTimeStringField(declaredField(schema.fields, schema.calendarTimeField)?.type);
|
|
2111
|
-
}
|
|
2112
|
-
/** `kanbanField` must name a real `enum` field — the board groups records into
|
|
2113
|
-
* one column per declared enum value; any other type has no closed set of
|
|
2114
|
-
* columns to group by. */
|
|
2115
|
-
function kanbanFieldIsAnEnum(schema) {
|
|
2116
|
-
return schema.kanbanField === void 0 || declaredField(schema.fields, schema.kanbanField)?.type === "enum";
|
|
2117
|
-
}
|
|
2118
|
-
/** `notifyWhen` narrows the completion bell, so it only means something with
|
|
2119
|
-
* completion tracking. */
|
|
2120
|
-
function notifyWhenRequiresCompletion(schema) {
|
|
2121
|
-
return schema.notifyWhen === void 0 || schema.completionField !== void 0;
|
|
2122
|
-
}
|
|
2123
|
-
/** `notifyWhen.field` must name a real top-level field. */
|
|
2124
|
-
function notifyWhenFieldIsDeclared(schema) {
|
|
2125
|
-
return schema.notifyWhen === void 0 || declaredField(schema.fields, schema.notifyWhen.field) !== void 0;
|
|
2126
|
-
}
|
|
2127
|
-
/** Every custom view `id` must be a valid slug — it doubles as the view-mode
|
|
2128
|
-
* selector key (`custom:<id>`) and the capability-token clamp key, both of
|
|
2129
|
-
* which expect a path-safe token. */
|
|
2130
|
-
function viewIdsAreSlugs(schema) {
|
|
2131
|
-
return schema.views === void 0 || schema.views.every((view) => isSafeSlug(view.id));
|
|
2132
|
-
}
|
|
2133
|
-
/** Custom view ids must be unique so the selector + token clamp resolve
|
|
2134
|
-
* unambiguously. */
|
|
2135
|
-
function viewIdsAreUnique(schema) {
|
|
2136
|
-
return hasUniqueIds(schema.views);
|
|
2137
|
-
}
|
|
2138
|
-
//#endregion
|
|
2139
|
-
//#region src/collection/core/schemaZ.ts
|
|
2140
|
-
/** Optional visibility predicate shared by actions and fields: the target
|
|
2141
|
-
* shows only when the open record's `field` (stringified) is one of `in`.
|
|
2142
|
-
* Domain-free — `field` is any non-empty key, `in` a non-empty array of
|
|
2143
|
-
* non-empty values; the host never interprets the meaning.
|
|
961
|
+
//#region src/collection/core/schemaZ.ts
|
|
962
|
+
/** Optional visibility predicate shared by actions and fields: the target
|
|
963
|
+
* shows only when the open record's `field` (stringified) is one of `in`.
|
|
964
|
+
* Domain-free — `field` is any non-empty key, `in` a non-empty array of
|
|
965
|
+
* non-empty values; the host never interprets the meaning.
|
|
2144
966
|
*
|
|
2145
967
|
* `trim().min(1)` rather than bare `min(1)` so a whitespace-only string
|
|
2146
968
|
* (" ") fails validation — otherwise the cell formatter / dropdown would
|
|
@@ -2625,511 +1447,1689 @@ var DataSourceZ = z.object({
|
|
|
2625
1447
|
type: z.literal("csv"),
|
|
2626
1448
|
path: z.string().min(1)
|
|
2627
1449
|
});
|
|
2628
|
-
/** Alternative WRITABLE storage backend for a collection's records —
|
|
2629
|
-
* unlike `dataSource` (external read-only file), a `storage` collection
|
|
2630
|
-
* behaves like a normal writable collection; only where the rows live
|
|
2631
|
-
* changes. The store factory registry (`server/store.ts`) picks the
|
|
2632
|
-
* implementation by `type` (plans/done/refactor-storage-virtualization.md).
|
|
1450
|
+
/** Alternative WRITABLE storage backend for a collection's records —
|
|
1451
|
+
* unlike `dataSource` (external read-only file), a `storage` collection
|
|
1452
|
+
* behaves like a normal writable collection; only where the rows live
|
|
1453
|
+
* changes. The store factory registry (`server/store.ts`) picks the
|
|
1454
|
+
* implementation by `type` (plans/done/refactor-storage-virtualization.md).
|
|
1455
|
+
*
|
|
1456
|
+
* A discriminated union rather than one shape with optional keys, because
|
|
1457
|
+
* only the sqlite variant is a workspace FILE: its `path` is
|
|
1458
|
+
* workspace-relative and containment-checked exactly like `dataPath`, while
|
|
1459
|
+
* the firestore variant has no path to check — its records are not on this
|
|
1460
|
+
* machine at all. Optional keys would let each arm accept the other's, and
|
|
1461
|
+
* the compiler would stop being the thing that tells you which. */
|
|
1462
|
+
var StorageZ = z.discriminatedUnion("type", [z.object({
|
|
1463
|
+
type: z.literal("sqlite"),
|
|
1464
|
+
path: z.string().min(1)
|
|
1465
|
+
}), z.object({ type: z.literal("firestore") }).strict()]);
|
|
1466
|
+
var BareCollectionSchemaZ = z.object({
|
|
1467
|
+
title: z.string().min(1),
|
|
1468
|
+
icon: z.string().min(1),
|
|
1469
|
+
dataPath: z.string().min(1).optional(),
|
|
1470
|
+
dataSource: DataSourceZ.optional(),
|
|
1471
|
+
storage: StorageZ.optional(),
|
|
1472
|
+
primaryKey: z.string().min(1),
|
|
1473
|
+
singleton: z.string().trim().min(1).optional(),
|
|
1474
|
+
fields: z.record(z.string(), FieldSpecZ),
|
|
1475
|
+
actions: z.array(ActionSpecZ).optional(),
|
|
1476
|
+
collectionActions: z.array(ActionSpecZ).optional(),
|
|
1477
|
+
completionField: z.string().trim().min(1).optional(),
|
|
1478
|
+
completionDoneValues: z.array(z.string().trim().min(1)).min(1).optional(),
|
|
1479
|
+
displayField: z.string().trim().min(1).optional(),
|
|
1480
|
+
triggerField: z.string().trim().min(1).optional(),
|
|
1481
|
+
triggerLeadDays: z.number().int().min(0).optional(),
|
|
1482
|
+
spawn: SpawnZ.optional(),
|
|
1483
|
+
calendarField: z.string().trim().min(1).optional(),
|
|
1484
|
+
calendarEndField: z.string().trim().min(1).optional(),
|
|
1485
|
+
calendarTimeField: z.string().trim().min(1).optional(),
|
|
1486
|
+
kanbanField: z.string().trim().min(1).optional(),
|
|
1487
|
+
views: z.array(CustomViewZ).optional(),
|
|
1488
|
+
notifyWhen: WhenZ.optional(),
|
|
1489
|
+
ingest: IngestZ.optional(),
|
|
1490
|
+
googleCalendar: GoogleCalendarSyncZ.optional(),
|
|
1491
|
+
dynamicIcon: DynamicIconSpecZ.optional()
|
|
1492
|
+
}).refine(declaresExactlyOneStore, {
|
|
1493
|
+
message: "declare exactly one of `dataPath` (native JSON records), `dataSource` (external read-only data file), or `storage` (alternative writable backend)",
|
|
1494
|
+
path: ["dataPath"]
|
|
1495
|
+
}).refine(dataSourceDeclaresNoWriteMachinery, {
|
|
1496
|
+
message: "a `dataSource` collection is read-only — it cannot declare `singleton`, `ingest`, `spawn`, or `googleCalendar` (all of them write records)",
|
|
1497
|
+
path: ["dataSource"]
|
|
1498
|
+
}).refine(googleCalendarMapNamesStoredFields, {
|
|
1499
|
+
message: "a `googleCalendar` map key must name a declared, non-computed field, and never the primaryKey (that always holds the Google event id)",
|
|
1500
|
+
path: ["googleCalendar"]
|
|
1501
|
+
}).refine(dataSourceDeclaresNoMutateAction, {
|
|
1502
|
+
message: "a `dataSource` collection is read-only — its actions cannot use `kind: \"mutate\"` (a host write); use `chat`/`agent` actions instead",
|
|
1503
|
+
path: ["dataSource"]
|
|
1504
|
+
}).refine(singletonIsAValidRecordId, {
|
|
1505
|
+
message: "schema `singleton` must be a valid item id (alphanumeric / hyphen / underscore / interior dot, no `..` or path separators)",
|
|
1506
|
+
path: ["singleton"]
|
|
1507
|
+
}).refine(actionIdsAreUnique, {
|
|
1508
|
+
message: "schema `actions` must have unique `id`s",
|
|
1509
|
+
path: ["actions"]
|
|
1510
|
+
}).refine(collectionActionIdsAreUnique, {
|
|
1511
|
+
message: "schema `collectionActions` must have unique `id`s",
|
|
1512
|
+
path: ["collectionActions"]
|
|
1513
|
+
}).refine(mutateSetKeysNameStoredFields, {
|
|
1514
|
+
message: "a mutate action's `set` keys must name declared, non-computed fields (and never the primaryKey)",
|
|
1515
|
+
path: ["actions"]
|
|
1516
|
+
}).refine(mutateParamRefsAreDeclared, {
|
|
1517
|
+
message: "a mutate action's `$params.<name>` references must name keys declared in its `params`",
|
|
1518
|
+
path: ["actions"]
|
|
1519
|
+
}).refine(collectionActionsAreNotMutate, {
|
|
1520
|
+
message: "`collectionActions` cannot contain `kind: \"mutate\"` — a collection-level action has no record to write",
|
|
1521
|
+
path: ["collectionActions"]
|
|
1522
|
+
}).refine(currencyFieldRefsNameCodeFields, {
|
|
1523
|
+
message: "a money field's `currencyField` must name a top-level `string`, `text`, or `enum` field that holds the currency code",
|
|
1524
|
+
path: ["fields"]
|
|
1525
|
+
}).refine(completionPairIsCoherent, {
|
|
1526
|
+
message: "schema `completionField` and `completionDoneValues` must be declared together (both set, or both omitted) — unless `completionField` names a `flag` field, in which case `completionDoneValues` must be omitted (done ⇔ the flag matches)",
|
|
1527
|
+
path: ["completionField"]
|
|
1528
|
+
}).refine(completionFieldIsDeclared, {
|
|
1529
|
+
message: "schema `completionField` must name a top-level field declared in `fields`",
|
|
1530
|
+
path: ["completionField"]
|
|
1531
|
+
}).refine(displayFieldIsDeclared, {
|
|
1532
|
+
message: "schema `displayField` must name a top-level field declared in `fields`",
|
|
1533
|
+
path: ["displayField"]
|
|
1534
|
+
}).refine(fieldVisibilityGatesNameDeclaredFields, {
|
|
1535
|
+
message: "a field's `when.field` must name a top-level field declared in `fields`",
|
|
1536
|
+
path: ["fields"]
|
|
1537
|
+
}).refine(flagConditionsNameDeclaredFields, {
|
|
1538
|
+
message: "a flag field's `where` conditions must name top-level fields declared in `fields` (both `field` and a same-record `valueFrom.field`)",
|
|
1539
|
+
path: ["fields"]
|
|
1540
|
+
}).refine(completionFlagReadsOnlyStoredFields, {
|
|
1541
|
+
message: "a `flag` named by `completionField` may only reference STORED fields in its `where` — completion is evaluated against the raw record (before deriveAll), where computed values (derived/rollup/toggle/flag/embed/backlinks) are absent",
|
|
1542
|
+
path: ["completionField"]
|
|
1543
|
+
}).refine(flagCompletionSpawnDeclaresWhen, {
|
|
1544
|
+
message: "a schema whose `completionField` names a `flag` field must declare an explicit `spawn.when` (the spawn-inert check cannot statically evaluate a flag's `where`)",
|
|
1545
|
+
path: ["spawn"]
|
|
1546
|
+
}).refine(embedIdFieldsNameIdBearingFields, {
|
|
1547
|
+
message: "an embed field's `idField` must name a top-level `ref` or `string` field declared in `fields`",
|
|
1548
|
+
path: ["fields"]
|
|
1549
|
+
}).refine(triggerFieldRequiresCompletion, {
|
|
1550
|
+
message: "schema `triggerField` requires `completionField` / `completionDoneValues` (the gated bell still clears via the done value)",
|
|
1551
|
+
path: ["triggerField"]
|
|
1552
|
+
}).refine(triggerFieldIsADateField, {
|
|
1553
|
+
message: "schema `triggerField` must name a top-level `date` field declared in `fields`",
|
|
1554
|
+
path: ["triggerField"]
|
|
1555
|
+
}).refine(triggerLeadDaysRequiresTriggerField, {
|
|
1556
|
+
message: "schema `triggerLeadDays` requires `triggerField` (it shifts when that field's bell fires)",
|
|
1557
|
+
path: ["triggerLeadDays"]
|
|
1558
|
+
}).refine(spawnRequiresTriggerField, {
|
|
1559
|
+
message: "schema `spawn` requires `triggerField` (the successor's trigger date is `triggerField` advanced by `spawn.every`)",
|
|
1560
|
+
path: ["spawn"]
|
|
1561
|
+
}).refine(spawnWhenFieldIsDeclared, {
|
|
1562
|
+
message: "schema `spawn.when.field` must name a top-level field declared in `fields`",
|
|
1563
|
+
path: ["spawn"]
|
|
1564
|
+
}).refine(spawnCarryEntriesAreDeclared, {
|
|
1565
|
+
message: "every `spawn.carry` entry must name a top-level field declared in `fields`",
|
|
1566
|
+
path: ["spawn"]
|
|
1567
|
+
}).refine(spawnSuccessorStartsInert, {
|
|
1568
|
+
message: "`spawn` must leave the successor in a non-matching state (e.g. `set` the status to a pending value); seeding the predicate field to a matching value via `set`/`carry` would respawn forever",
|
|
1569
|
+
path: ["spawn"]
|
|
1570
|
+
}).refine(fieldDrivenFromFieldIsEnum, {
|
|
1571
|
+
message: "`spawn.every.fromField` must name a top-level `enum` field declared in `fields`",
|
|
1572
|
+
path: ["spawn"]
|
|
1573
|
+
}).refine(fieldDrivenMapCoversValues, {
|
|
1574
|
+
message: "`spawn.every.map` keys must exactly cover the `values` of the `enum` named by `fromField` (no missing or extra keys)",
|
|
1575
|
+
path: ["spawn"]
|
|
1576
|
+
}).refine(fieldDrivenFromFieldCarried, {
|
|
1577
|
+
message: "`spawn.every.fromField` must appear in `spawn.carry`, or be written by `spawn.set` to a value present in `spawn.every.map`, so the successor keeps a resolvable recurrence interval",
|
|
1578
|
+
path: ["spawn"]
|
|
1579
|
+
}).refine(calendarFieldIsDateLike, {
|
|
1580
|
+
message: "schema `calendarField` must name a top-level `date` or `datetime` field declared in `fields`",
|
|
1581
|
+
path: ["calendarField"]
|
|
1582
|
+
}).refine(calendarEndFieldRequiresCalendarField, {
|
|
1583
|
+
message: "schema `calendarEndField` requires `calendarField` (it marks the end of the span that starts at `calendarField`)",
|
|
1584
|
+
path: ["calendarEndField"]
|
|
1585
|
+
}).refine(calendarEndFieldIsDateLike, {
|
|
1586
|
+
message: "schema `calendarEndField` must name a top-level `date` or `datetime` field declared in `fields`",
|
|
1587
|
+
path: ["calendarEndField"]
|
|
1588
|
+
}).refine(calendarTimeFieldRequiresCalendarField, {
|
|
1589
|
+
message: "schema `calendarTimeField` requires `calendarField` (it supplies the time-of-day for the calendar's day view)",
|
|
1590
|
+
path: ["calendarTimeField"]
|
|
1591
|
+
}).refine(calendarTimeFieldIsDeclared, {
|
|
1592
|
+
message: "schema `calendarTimeField` must name a top-level field declared in `fields`",
|
|
1593
|
+
path: ["calendarTimeField"]
|
|
1594
|
+
}).refine(calendarTimeFieldIsStringBacked, {
|
|
1595
|
+
message: "schema `calendarTimeField` must name a top-level `string` or `text` field declared in `fields`",
|
|
1596
|
+
path: ["calendarTimeField"]
|
|
1597
|
+
}).refine(kanbanFieldIsAnEnum, {
|
|
1598
|
+
message: "schema `kanbanField` must name a top-level `enum` field declared in `fields`",
|
|
1599
|
+
path: ["kanbanField"]
|
|
1600
|
+
}).refine(togglesProjectValidEnums, {
|
|
1601
|
+
message: "a `toggle` field's `field` must name a top-level `enum` field, and its `onValue`/`offValue` must be values of that enum",
|
|
1602
|
+
path: ["fields"]
|
|
1603
|
+
}).refine(notifyWhenRequiresCompletion, {
|
|
1604
|
+
message: "schema `notifyWhen` requires `completionField` (it narrows that bell)",
|
|
1605
|
+
path: ["notifyWhen"]
|
|
1606
|
+
}).refine(notifyWhenFieldIsDeclared, {
|
|
1607
|
+
message: "schema `notifyWhen.field` must name a top-level field declared in `fields`",
|
|
1608
|
+
path: ["notifyWhen"]
|
|
1609
|
+
}).refine(viewIdsAreSlugs, {
|
|
1610
|
+
message: "every `views[].id` must be a valid slug (alphanumeric / hyphen / underscore, no path separators)",
|
|
1611
|
+
path: ["views"]
|
|
1612
|
+
}).refine(viewIdsAreUnique, {
|
|
1613
|
+
message: "schema `views` must have unique `id`s",
|
|
1614
|
+
path: ["views"]
|
|
1615
|
+
});
|
|
1616
|
+
var PROTOTYPE_KEYS = [
|
|
1617
|
+
"__proto__",
|
|
1618
|
+
"constructor",
|
|
1619
|
+
"prototype"
|
|
1620
|
+
];
|
|
1621
|
+
/** The first own prototype-sensitive key of `value`, or null. */
|
|
1622
|
+
function ownPrototypeKey(value) {
|
|
1623
|
+
if (value === null || typeof value !== "object") return null;
|
|
1624
|
+
for (const key of PROTOTYPE_KEYS) if (Object.hasOwn(value, key)) return key;
|
|
1625
|
+
return null;
|
|
1626
|
+
}
|
|
1627
|
+
/** Own enumerable entries of an object (arrays keyed by index), none for
|
|
1628
|
+
* anything else — the raw input is unvalidated, so `fields` may be junk. */
|
|
1629
|
+
function ownEntries(value) {
|
|
1630
|
+
if (isUnknownArray(value)) return value.map((entry, index) => [String(index), entry]);
|
|
1631
|
+
return isRecord(value) ? Object.entries(value) : [];
|
|
1632
|
+
}
|
|
1633
|
+
/** The name-defining sub-record a raw field spec (`of`) or action (`params`)
|
|
1634
|
+
* carries, or undefined when the holder isn't an object at all. */
|
|
1635
|
+
function nameDefiningSubRecord(holder, key) {
|
|
1636
|
+
return isRecord(holder) ? holder[key] : void 0;
|
|
1637
|
+
}
|
|
1638
|
+
/** Dotted path of the first prototype-sensitive `params` name across both
|
|
1639
|
+
* action lists, or null. */
|
|
1640
|
+
function prototypeActionParamPath(input) {
|
|
1641
|
+
for (const [listName, list] of [["actions", input.actions], ["collectionActions", input.collectionActions]]) for (const action of isUnknownArray(list) ? list : []) {
|
|
1642
|
+
const badParam = ownPrototypeKey(nameDefiningSubRecord(action, "params"));
|
|
1643
|
+
if (badParam !== null) return `${listName}.params.${badParam}`;
|
|
1644
|
+
}
|
|
1645
|
+
return null;
|
|
1646
|
+
}
|
|
1647
|
+
/** Dotted path of the first prototype-sensitive field name in the raw
|
|
1648
|
+
* schema input — top-level `fields`, each table field's `of`, and each
|
|
1649
|
+
* action's `params` (the three records that DEFINE names) — or null. */
|
|
1650
|
+
function prototypeFieldKeyPath(input) {
|
|
1651
|
+
if (!isRecord(input)) return null;
|
|
1652
|
+
const bad = ownPrototypeKey(input.fields);
|
|
1653
|
+
if (bad !== null) return `fields.${bad}`;
|
|
1654
|
+
for (const [key, spec] of ownEntries(input.fields)) {
|
|
1655
|
+
const badSub = ownPrototypeKey(nameDefiningSubRecord(spec, "of"));
|
|
1656
|
+
if (badSub !== null) return `fields.${key}.of.${badSub}`;
|
|
1657
|
+
}
|
|
1658
|
+
return prototypeActionParamPath(input);
|
|
1659
|
+
}
|
|
1660
|
+
var CollectionSchemaZ = z.preprocess((input, ctx) => {
|
|
1661
|
+
const bad = prototypeFieldKeyPath(input);
|
|
1662
|
+
if (bad !== null) {
|
|
1663
|
+
ctx.addIssue({
|
|
1664
|
+
code: "custom",
|
|
1665
|
+
message: `'${bad}': field names must not be prototype-sensitive keys (\`__proto__\`, \`constructor\`, \`prototype\`)`
|
|
1666
|
+
});
|
|
1667
|
+
return z.NEVER;
|
|
1668
|
+
}
|
|
1669
|
+
return input;
|
|
1670
|
+
}, BareCollectionSchemaZ);
|
|
1671
|
+
//#endregion
|
|
1672
|
+
//#region src/collection/server/discovery.ts
|
|
1673
|
+
function applyFeedSchemaDefaults(parsed, slug) {
|
|
1674
|
+
if (!isRecord(parsed)) return parsed;
|
|
1675
|
+
const icon = typeof parsed.icon === "string" && parsed.icon.trim().length > 0 ? parsed.icon : "dynamic_feed";
|
|
1676
|
+
return {
|
|
1677
|
+
...parsed,
|
|
1678
|
+
icon,
|
|
1679
|
+
dataPath: `data/feeds/${slug}`
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
/** The conventional per-slug records dir a `dataSource` / `storage` collection
|
|
1683
|
+
* gets as its `dataDir` (records never live there, but archive/delete paths
|
|
1684
|
+
* stay well-defined — same shape the registry's R3 normalization uses).
|
|
1685
|
+
*
|
|
1686
|
+
* INVARIANT — this is NOT a default `dataPath`, and must not be used as one.
|
|
1687
|
+
* It applies only to the two backends whose records are not per-file JSON. A
|
|
1688
|
+
* normal collection declares its own location and exactly one of `dataPath` /
|
|
1689
|
+
* `dataSource` / `storage`; a schema with none of the three is REJECTED, not
|
|
1690
|
+
* quietly pointed here. Handing a per-file collection this path would silently
|
|
1691
|
+
* relocate its records away from the folder the user (and its SKILL.md) sees. */
|
|
1692
|
+
function conventionalDataPath(slug) {
|
|
1693
|
+
return `data/collections/${slug}/items`;
|
|
1694
|
+
}
|
|
1695
|
+
/** The declared field named by `primaryKey`, or `undefined` when the schema
|
|
1696
|
+
* declares no such field. Own-property guarded: a `primaryKey` of `toString`
|
|
1697
|
+
* / `constructor` / `__proto__` must miss here, not read an Object.prototype
|
|
1698
|
+
* member and slip past the "is it a declared field?" gate into the wrong
|
|
1699
|
+
* "add `primary: true`" advice. Shared with manageCollection's putSchema
|
|
1700
|
+
* gate so both report the SAME reason. */
|
|
1701
|
+
function resolvePrimaryField(fields, primaryKey) {
|
|
1702
|
+
return Object.hasOwn(fields, primaryKey) ? fields[primaryKey] : void 0;
|
|
1703
|
+
}
|
|
1704
|
+
/** The acceptance gates discovery applies AFTER `CollectionSchemaZ` parses,
|
|
1705
|
+
* before a schema becomes a live collection:
|
|
1706
|
+
*
|
|
1707
|
+
* - the `primaryKey` must be a declared field flagged `primary: true` —
|
|
1708
|
+
* without the flag CollectionView renders the field editable, and a
|
|
1709
|
+
* rename is silently pinned back to the URL itemId on save, so the user's
|
|
1710
|
+
* edit is dropped with no error;
|
|
1711
|
+
* - a `feed` schema must declare an `ingest` block (else it's a dead,
|
|
1712
|
+
* non-refreshable card);
|
|
1713
|
+
* - `dataPath` — or a `dataSource`'s `path` — must resolve INSIDE the
|
|
1714
|
+
* workspace (same realpath containment for both).
|
|
1715
|
+
*
|
|
1716
|
+
* Exported so `manageCollection`'s `putSchema` can run the SAME gates before
|
|
1717
|
+
* it reports success — a schema that passes `CollectionSchemaZ` but fails one
|
|
1718
|
+
* of these would otherwise write cleanly yet be skipped on the next discovery,
|
|
1719
|
+
* hiding the collection (the exact failure that tool exists to prevent). */
|
|
1720
|
+
function acceptParsedSchema(schema, opts) {
|
|
1721
|
+
const primaryField = resolvePrimaryField(schema.fields, schema.primaryKey);
|
|
1722
|
+
if (!primaryField) return {
|
|
1723
|
+
ok: false,
|
|
1724
|
+
reason: `primaryKey '${schema.primaryKey}' is not one of the declared fields`
|
|
1725
|
+
};
|
|
1726
|
+
if (primaryField.primary !== true) return {
|
|
1727
|
+
ok: false,
|
|
1728
|
+
reason: `the primaryKey field '${schema.primaryKey}' must be flagged \`primary: true\``
|
|
1729
|
+
};
|
|
1730
|
+
if (opts.source === "feed" && !schema.ingest) return {
|
|
1731
|
+
ok: false,
|
|
1732
|
+
reason: "a feed schema must declare an `ingest` block"
|
|
1733
|
+
};
|
|
1734
|
+
if (schema.dataSource !== void 0) {
|
|
1735
|
+
const dataSourceFile = resolveDataDir(schema.dataSource.path, opts.workspaceRoot);
|
|
1736
|
+
if (dataSourceFile === null) return {
|
|
1737
|
+
ok: false,
|
|
1738
|
+
reason: `dataSource.path '${schema.dataSource.path}' escapes the workspace`
|
|
1739
|
+
};
|
|
1740
|
+
const dataDir = resolveDataDir(conventionalDataPath(opts.slug), opts.workspaceRoot);
|
|
1741
|
+
if (dataDir === null) return {
|
|
1742
|
+
ok: false,
|
|
1743
|
+
reason: `slug '${opts.slug}' yields no workspace-contained data dir`
|
|
1744
|
+
};
|
|
1745
|
+
return {
|
|
1746
|
+
ok: true,
|
|
1747
|
+
dataDir,
|
|
1748
|
+
dataSourceFile
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
if (schema.storage !== void 0) return acceptStorageSchema(schema.storage, opts);
|
|
1752
|
+
const dataDir = resolveDataDir(schema.dataPath ?? "", opts.workspaceRoot);
|
|
1753
|
+
if (dataDir === null) return {
|
|
1754
|
+
ok: false,
|
|
1755
|
+
reason: `dataPath '${schema.dataPath}' escapes the workspace`
|
|
1756
|
+
};
|
|
1757
|
+
return {
|
|
1758
|
+
ok: true,
|
|
1759
|
+
dataDir
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
/** The `storage` arm of the acceptance gate. Every storage backend gets the
|
|
1763
|
+
* conventional phantom dataDir; what differs is what else has to resolve
|
|
1764
|
+
* before the collection can exist at all.
|
|
1765
|
+
*
|
|
1766
|
+
* A FILE-backed backend (sqlite) resolves and containment-checks a
|
|
1767
|
+
* `storageFile`. A SHARED one (firestore) has no path on this machine — it
|
|
1768
|
+
* resolves an IDENTITY instead: the `aid` from the repository's `app.json`,
|
|
1769
|
+
* which together with the slug as `cid` names `apps/{aid}/collections/{cid}`.
|
|
1770
|
+
*
|
|
1771
|
+
* Resolving it HERE, once, is the point. The store then receives a settled
|
|
1772
|
+
* `(aid, cid)` and never reads `app.json` itself — otherwise the questions of
|
|
1773
|
+
* caching, staleness and what to do when the file is missing would be decided
|
|
1774
|
+
* inside a read path, where the only cheap answer is to return nothing, and
|
|
1775
|
+
* "this collection is misconfigured" would reach the user as "this collection
|
|
1776
|
+
* is empty". A missing or malformed `app.json` is a CONFIGURATION error, so it
|
|
1777
|
+
* is reported the same way an escaping `storage.path` is: the schema is
|
|
1778
|
+
* refused, with a reason naming the file to create. */
|
|
1779
|
+
function acceptStorageSchema(storage, opts) {
|
|
1780
|
+
const dataDir = resolveDataDir(conventionalDataPath(opts.slug), opts.workspaceRoot);
|
|
1781
|
+
if (dataDir === null) return {
|
|
1782
|
+
ok: false,
|
|
1783
|
+
reason: `slug '${opts.slug}' yields no workspace-contained data dir`
|
|
1784
|
+
};
|
|
1785
|
+
if (storage.type === "sqlite") {
|
|
1786
|
+
const storageFile = resolveDataDir(storage.path, opts.workspaceRoot);
|
|
1787
|
+
if (storageFile === null) return {
|
|
1788
|
+
ok: false,
|
|
1789
|
+
reason: `storage.path '${storage.path}' escapes the workspace`
|
|
1790
|
+
};
|
|
1791
|
+
return {
|
|
1792
|
+
ok: true,
|
|
1793
|
+
dataDir,
|
|
1794
|
+
storageFile
|
|
1795
|
+
};
|
|
1796
|
+
}
|
|
1797
|
+
const manifest = loadAppManifest(opts.workspaceRoot);
|
|
1798
|
+
if (!manifest.ok) return {
|
|
1799
|
+
ok: false,
|
|
1800
|
+
reason: appManifestReason(manifest, opts.workspaceRoot)
|
|
1801
|
+
};
|
|
1802
|
+
return {
|
|
1803
|
+
ok: true,
|
|
1804
|
+
dataDir,
|
|
1805
|
+
appId: manifest.manifest.aid
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1808
|
+
async function loadOneCollection(skillsRoot, slug, source, workspaceRoot) {
|
|
1809
|
+
const safeName = safeSlugName(slug);
|
|
1810
|
+
if (safeName === null) return null;
|
|
1811
|
+
const schemaPath = path.join(skillsRoot, safeName, SCHEMA_FILE);
|
|
1812
|
+
let raw;
|
|
1813
|
+
try {
|
|
1814
|
+
if (!(await stat(schemaPath)).isFile()) return null;
|
|
1815
|
+
raw = await readFile(schemaPath, "utf-8");
|
|
1816
|
+
} catch (err) {
|
|
1817
|
+
if (!isErrorWithCode(err) || err.code !== "ENOENT") log.warn("collections", "failed to read schema.json, skipping", {
|
|
1818
|
+
slug: safeName,
|
|
1819
|
+
path: schemaPath,
|
|
1820
|
+
error: String(err)
|
|
1821
|
+
});
|
|
1822
|
+
return null;
|
|
1823
|
+
}
|
|
1824
|
+
let parsedJson;
|
|
1825
|
+
try {
|
|
1826
|
+
parsedJson = JSON.parse(raw);
|
|
1827
|
+
} catch (err) {
|
|
1828
|
+
log.warn("collections", "schema.json is not valid JSON, skipping", {
|
|
1829
|
+
slug: safeName,
|
|
1830
|
+
error: String(err)
|
|
1831
|
+
});
|
|
1832
|
+
return null;
|
|
1833
|
+
}
|
|
1834
|
+
const candidate = source === "feed" ? applyFeedSchemaDefaults(parsedJson, safeName) : parsedJson;
|
|
1835
|
+
const parsed = CollectionSchemaZ.safeParse(candidate);
|
|
1836
|
+
if (!parsed.success) {
|
|
1837
|
+
log.warn("collections", "schema.json failed validation, skipping", {
|
|
1838
|
+
slug: safeName,
|
|
1839
|
+
issues: parsed.error.issues
|
|
1840
|
+
});
|
|
1841
|
+
return null;
|
|
1842
|
+
}
|
|
1843
|
+
const schema = parsed.data;
|
|
1844
|
+
const acceptance = acceptParsedSchema(schema, {
|
|
1845
|
+
source,
|
|
1846
|
+
workspaceRoot,
|
|
1847
|
+
slug: safeName
|
|
1848
|
+
});
|
|
1849
|
+
if (!acceptance.ok) {
|
|
1850
|
+
log.warn("collections", "schema.json rejected after validation, skipping", {
|
|
1851
|
+
slug: safeName,
|
|
1852
|
+
reason: acceptance.reason
|
|
1853
|
+
});
|
|
1854
|
+
return null;
|
|
1855
|
+
}
|
|
1856
|
+
return {
|
|
1857
|
+
slug: safeName,
|
|
1858
|
+
source,
|
|
1859
|
+
schema,
|
|
1860
|
+
dataDir: acceptance.dataDir,
|
|
1861
|
+
...acceptance.dataSourceFile !== void 0 ? { dataSourceFile: acceptance.dataSourceFile } : {},
|
|
1862
|
+
...acceptance.storageFile !== void 0 ? { storageFile: acceptance.storageFile } : {},
|
|
1863
|
+
...acceptance.appId !== void 0 ? { appId: acceptance.appId } : {},
|
|
1864
|
+
skillDir: path.join(skillsRoot, safeName)
|
|
1865
|
+
};
|
|
1866
|
+
}
|
|
1867
|
+
async function collectFromDir(skillsRoot, source, workspaceRoot) {
|
|
1868
|
+
let entries;
|
|
1869
|
+
try {
|
|
1870
|
+
entries = await readdir(skillsRoot);
|
|
1871
|
+
} catch (err) {
|
|
1872
|
+
if (isErrorWithCode(err) && err.code === "ENOENT") return [];
|
|
1873
|
+
log.warn("collections", "failed to list skills dir, returning empty", {
|
|
1874
|
+
root: skillsRoot,
|
|
1875
|
+
error: String(err)
|
|
1876
|
+
});
|
|
1877
|
+
return [];
|
|
1878
|
+
}
|
|
1879
|
+
const results = [];
|
|
1880
|
+
for (const name of entries) {
|
|
1881
|
+
if (name.startsWith(".")) continue;
|
|
1882
|
+
const safeName = safeSlugName(name);
|
|
1883
|
+
if (safeName === null) continue;
|
|
1884
|
+
const dirPath = path.join(skillsRoot, safeName);
|
|
1885
|
+
let dirStat;
|
|
1886
|
+
try {
|
|
1887
|
+
dirStat = await stat(dirPath);
|
|
1888
|
+
} catch {
|
|
1889
|
+
continue;
|
|
1890
|
+
}
|
|
1891
|
+
if (!dirStat.isDirectory()) continue;
|
|
1892
|
+
const collection = await loadOneCollection(skillsRoot, safeName, source, workspaceRoot);
|
|
1893
|
+
if (collection) results.push(collection);
|
|
1894
|
+
}
|
|
1895
|
+
return results;
|
|
1896
|
+
}
|
|
1897
|
+
/** The user-scope dir this call should scan, or `null` for none. The single
|
|
1898
|
+
* place the "explicit override beats the host binding, and either may say
|
|
1899
|
+
* none" rule is spelled — `??` cannot express it, because `undefined` there
|
|
1900
|
+
* means "ask the host" and would silently re-enable a scope the caller
|
|
1901
|
+
* passed `null` to switch off. */
|
|
1902
|
+
function resolveUserDir(opts, workspaceRoot) {
|
|
1903
|
+
return opts.userSkillsDir !== void 0 ? opts.userSkillsDir : userSkillsDir(workspaceRoot);
|
|
1904
|
+
}
|
|
1905
|
+
/** Discover every schema-driven collection available to this
|
|
1906
|
+
* workspace. Project-scope collections override user-scope on slug
|
|
1907
|
+
* collision. The `workspaceRoot` override also flows into each
|
|
1908
|
+
* collection's dataDir resolution so a tmpdir-scoped test gets
|
|
1909
|
+
* dataDirs under the same tmpdir (Codex P1 review on PR #1489 —
|
|
1910
|
+
* previously dataDir was always rooted at the live workspacePath
|
|
1911
|
+
* regardless of override). */
|
|
1912
|
+
async function discoverCollections(opts = {}) {
|
|
1913
|
+
const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();
|
|
1914
|
+
const userDir = resolveUserDir(opts, workspaceRoot);
|
|
1915
|
+
const projectDir = projectSkillsDir(workspaceRoot);
|
|
1916
|
+
const feedCollections = await collectFromDir(feedsRoot(workspaceRoot), "feed", workspaceRoot);
|
|
1917
|
+
const userCollections = userDir === null ? [] : await collectFromDir(userDir, "user", workspaceRoot);
|
|
1918
|
+
const projectCollections = await collectFromDir(projectDir, "project", workspaceRoot);
|
|
1919
|
+
const merged = /* @__PURE__ */ new Map();
|
|
1920
|
+
for (const entry of feedCollections) merged.set(entry.slug, entry);
|
|
1921
|
+
for (const entry of userCollections) merged.set(entry.slug, entry);
|
|
1922
|
+
for (const entry of projectCollections) merged.set(entry.slug, entry);
|
|
1923
|
+
return [...merged.values()].sort((left, right) => left.slug.localeCompare(right.slug));
|
|
1924
|
+
}
|
|
1925
|
+
/** Load one collection by slug. Returns null if the slug is invalid,
|
|
1926
|
+
* no matching skill exists, or the schema is malformed. */
|
|
1927
|
+
async function loadCollection(slug, opts = {}) {
|
|
1928
|
+
const safeName = safeSlugName(slug);
|
|
1929
|
+
if (safeName === null) return null;
|
|
1930
|
+
const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();
|
|
1931
|
+
const userDir = resolveUserDir(opts, workspaceRoot);
|
|
1932
|
+
const projectCollection = await loadOneCollection(projectSkillsDir(workspaceRoot), safeName, "project", workspaceRoot);
|
|
1933
|
+
if (projectCollection) return projectCollection;
|
|
1934
|
+
const userCollection = userDir === null ? null : await loadOneCollection(userDir, safeName, "user", workspaceRoot);
|
|
1935
|
+
if (userCollection) return userCollection;
|
|
1936
|
+
return loadOneCollection(feedsRoot(workspaceRoot), safeName, "feed", workspaceRoot);
|
|
1937
|
+
}
|
|
1938
|
+
function toSummary(collection) {
|
|
1939
|
+
return {
|
|
1940
|
+
slug: collection.slug,
|
|
1941
|
+
title: collection.schema.title,
|
|
1942
|
+
icon: collection.schema.icon,
|
|
1943
|
+
source: collection.source,
|
|
1944
|
+
...collection.schema.dataSource !== void 0 ? { readonly: true } : {},
|
|
1945
|
+
...collection.appId !== void 0 ? { appId: collection.appId } : {}
|
|
1946
|
+
};
|
|
1947
|
+
}
|
|
1948
|
+
function toDetail(collection) {
|
|
1949
|
+
return {
|
|
1950
|
+
...toSummary(collection),
|
|
1951
|
+
schema: collection.schema
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
//#endregion
|
|
1955
|
+
//#region src/collection/server/io.ts
|
|
1956
|
+
/** True iff `filePath` exists and is a regular file (NOT a symlink).
|
|
1957
|
+
* Defends `listItems` / `readItem` against `*.json` symlinks placed
|
|
1958
|
+
* inside an otherwise-contained data dir — without this, a record
|
|
1959
|
+
* file could symlink to /etc/passwd and the detail endpoint would
|
|
1960
|
+
* happily serve it. Returns false on ENOENT and on any other lstat
|
|
1961
|
+
* failure so the caller's "missing" branch covers those cases too.
|
|
1962
|
+
* Exported so `ontology.ts`'s record COUNT classifies entries with the
|
|
1963
|
+
* SAME lstat logic — the two must agree on what a record file is. */
|
|
1964
|
+
async function isRegularFile(filePath) {
|
|
1965
|
+
try {
|
|
1966
|
+
return (await lstat(filePath)).isFile();
|
|
1967
|
+
} catch {
|
|
1968
|
+
return false;
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
/** Read one JSON record file. Returns null when the file is missing,
|
|
1972
|
+
* is a symlink (file-disclosure defense), parses to a non-object,
|
|
1973
|
+
* or has a read/parse error. Caller logs the per-entry skip — this
|
|
1974
|
+
* helper just classifies. Split out to keep `listItems` under the
|
|
1975
|
+
* `sonarjs/cognitive-complexity` threshold. */
|
|
1976
|
+
/** Parse a record file's text into a plain-object `CollectionItem`, or
|
|
1977
|
+
* null when it isn't a JSON object (array / scalar / null). */
|
|
1978
|
+
function parseRecordJson(raw) {
|
|
1979
|
+
const parsed = JSON.parse(raw);
|
|
1980
|
+
return isRecord(parsed) ? parsed : null;
|
|
1981
|
+
}
|
|
1982
|
+
async function tryReadRecord(filePath) {
|
|
1983
|
+
if (!await isRegularFile(filePath)) return null;
|
|
1984
|
+
try {
|
|
1985
|
+
return parseRecordJson(await readFile(filePath, "utf-8"));
|
|
1986
|
+
} catch {
|
|
1987
|
+
return null;
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
/** Read every record under `dataDir`. Returns [] if the dir doesn't
|
|
1991
|
+
* exist yet (legitimate first-use state). Malformed JSON files and
|
|
1992
|
+
* symlinked records are skipped (the latter is a file-disclosure
|
|
1993
|
+
* defense — see `isRegularFile`). Re-validates the realpath
|
|
1994
|
+
* containment to defend against a symlinked data dir appearing
|
|
1995
|
+
* between discovery and use. */
|
|
1996
|
+
async function listItems(dataDir, opts = {}) {
|
|
1997
|
+
if (!isContainedInRoot(dataDir, opts.workspaceRoot ?? getWorkspaceRoot())) {
|
|
1998
|
+
log.warn("collections", "listItems refused: dataDir escapes workspace via symlink", { dataDir });
|
|
1999
|
+
return [];
|
|
2000
|
+
}
|
|
2001
|
+
let entries;
|
|
2002
|
+
try {
|
|
2003
|
+
entries = await readdir(dataDir);
|
|
2004
|
+
} catch (err) {
|
|
2005
|
+
if (isErrorWithCode(err) && err.code === "ENOENT") return [];
|
|
2006
|
+
throw err;
|
|
2007
|
+
}
|
|
2008
|
+
const results = [];
|
|
2009
|
+
for (const name of entries) {
|
|
2010
|
+
if (!name.endsWith(".json")) continue;
|
|
2011
|
+
if (name.startsWith(".")) continue;
|
|
2012
|
+
const filePath = path.join(dataDir, name);
|
|
2013
|
+
const record = await tryReadRecord(filePath);
|
|
2014
|
+
if (record === null) {
|
|
2015
|
+
log.warn("collections", "skipping record (missing, symlink, or unreadable)", { path: filePath });
|
|
2016
|
+
continue;
|
|
2017
|
+
}
|
|
2018
|
+
results.push(record);
|
|
2019
|
+
}
|
|
2020
|
+
return results;
|
|
2021
|
+
}
|
|
2022
|
+
/** Read one record by id. Returns null when the file is missing,
|
|
2023
|
+
* when the resolved path escapes the workspace via a symlink, or
|
|
2024
|
+
* when the record file itself is a symlink (file-disclosure
|
|
2025
|
+
* defense — see `isRegularFile`). */
|
|
2026
|
+
async function readItem(dataDir, itemId, opts = {}) {
|
|
2027
|
+
const safeId = safeRecordId(itemId);
|
|
2028
|
+
if (safeId === null) return null;
|
|
2029
|
+
if (!isContainedInRoot(dataDir, opts.workspaceRoot ?? getWorkspaceRoot())) return null;
|
|
2030
|
+
const filePath = itemFilePath(dataDir, safeId);
|
|
2031
|
+
if (!await isRegularFile(filePath)) return null;
|
|
2032
|
+
try {
|
|
2033
|
+
return parseRecordJson(await readFile(filePath, "utf-8"));
|
|
2034
|
+
} catch (err) {
|
|
2035
|
+
if (isErrorWithCode(err) && err.code === "ENOENT") return null;
|
|
2036
|
+
throw err;
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
/** The symlink-containment refusal every record path shares: one check, one
|
|
2040
|
+
* warn, one answer. Extracted because this is a security RULE applied at
|
|
2041
|
+
* three sites (write pre-mkdir, write post-mkdir, delete) — a fix to the
|
|
2042
|
+
* check must not be able to land at only one of them.
|
|
2633
2043
|
*
|
|
2634
|
-
*
|
|
2635
|
-
*
|
|
2636
|
-
*
|
|
2637
|
-
*
|
|
2638
|
-
*
|
|
2639
|
-
*
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
}
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
})
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
}
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
}
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
}
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
}
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
}
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
path: ["views"]
|
|
2044
|
+
* `stage` names the call site so the warn stays as diagnosable as the three
|
|
2045
|
+
* hand-written copies were.
|
|
2046
|
+
*
|
|
2047
|
+
* Scope, stated explicitly because a reviewer asks every time: this catches
|
|
2048
|
+
* a symlink that EXISTS when we look — `isContainedInRoot` realpaths the
|
|
2049
|
+
* closest existing ancestor, so a pre-planted escape is refused. It does not
|
|
2050
|
+
* and cannot close the check-then-use race, where an ancestor is swapped for
|
|
2051
|
+
* a symlink between this call and the `mkdir` / `open` / `unlink` that
|
|
2052
|
+
* follows. Closing that needs directory-handle I/O anchored at the workspace
|
|
2053
|
+
* (`openat` + `O_NOFOLLOW`), which `node:fs` does not expose — it would mean
|
|
2054
|
+
* a different I/O layer, not a tighter check here.
|
|
2055
|
+
*
|
|
2056
|
+
* That race is deliberately outside this app's threat model: the process is
|
|
2057
|
+
* loopback-bound and bearer-authed, so anyone able to swap directories inside
|
|
2058
|
+
* the workspace is already the workspace owner — the same trust principal the
|
|
2059
|
+
* writes belong to. Revisit if collections ever serve a lower-trust caller. */
|
|
2060
|
+
function escapesWorkspace(dataDir, workspaceRoot, itemId, stage) {
|
|
2061
|
+
if (isContainedInRoot(dataDir, workspaceRoot)) return false;
|
|
2062
|
+
log.warn("collections", `${stage} refused: dataDir escapes workspace via symlink`, {
|
|
2063
|
+
dataDir,
|
|
2064
|
+
itemId
|
|
2065
|
+
});
|
|
2066
|
+
return true;
|
|
2067
|
+
}
|
|
2068
|
+
/** Write a record. Ensures the directory exists, validates the id,
|
|
2069
|
+
* re-checks symlink containment after mkdir, and writes atomically.
|
|
2070
|
+
*
|
|
2071
|
+
* Create path (`refuseOverwrite: true`) uses an O_EXCL `wx` open
|
|
2072
|
+
* rather than `stat` + `writeFileAtomic` to close a check-then-write
|
|
2073
|
+
* race: two concurrent POSTs would otherwise both pass the existence
|
|
2074
|
+
* check and one would silently overwrite the other. The trade-off
|
|
2075
|
+
* is that the create path is not crash-atomic (a partial file could
|
|
2076
|
+
* remain if the process dies mid-write); acceptable here because
|
|
2077
|
+
* records are small JSON blobs and the next read either parses or
|
|
2078
|
+
* is skipped via the "malformed JSON" branch in `listItems`.
|
|
2079
|
+
*
|
|
2080
|
+
* Update path (`refuseOverwrite: false`) uses `writeFileAtomic` so
|
|
2081
|
+
* PUT remains crash-atomic. No race there — the URL pins the id. */
|
|
2082
|
+
async function writeItem(dataDir, itemId, item, opts = {}) {
|
|
2083
|
+
const safeId = safeRecordId(itemId);
|
|
2084
|
+
if (safeId === null) return {
|
|
2085
|
+
kind: "invalid-id",
|
|
2086
|
+
itemId
|
|
2087
|
+
};
|
|
2088
|
+
const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();
|
|
2089
|
+
if (escapesWorkspace(dataDir, workspaceRoot, safeId, "writeItem (pre-mkdir)")) return {
|
|
2090
|
+
kind: "path-escape",
|
|
2091
|
+
itemId: safeId
|
|
2092
|
+
};
|
|
2093
|
+
await mkdir(dataDir, { recursive: true });
|
|
2094
|
+
if (escapesWorkspace(dataDir, workspaceRoot, safeId, "writeItem (post-mkdir)")) return {
|
|
2095
|
+
kind: "path-escape",
|
|
2096
|
+
itemId: safeId
|
|
2097
|
+
};
|
|
2098
|
+
const filePath = itemFilePath(dataDir, safeId);
|
|
2099
|
+
const payload = `${JSON.stringify(item, null, 2)}\n`;
|
|
2100
|
+
if (opts.refuseOverwrite) {
|
|
2101
|
+
let handle;
|
|
2102
|
+
try {
|
|
2103
|
+
handle = await open(filePath, "wx");
|
|
2104
|
+
} catch (err) {
|
|
2105
|
+
if (isErrorWithCode(err) && err.code === "EEXIST") return {
|
|
2106
|
+
kind: "conflict",
|
|
2107
|
+
itemId: safeId
|
|
2108
|
+
};
|
|
2109
|
+
throw err;
|
|
2110
|
+
}
|
|
2111
|
+
try {
|
|
2112
|
+
await handle.writeFile(payload);
|
|
2113
|
+
} finally {
|
|
2114
|
+
await handle.close();
|
|
2115
|
+
}
|
|
2116
|
+
} else await writeFileAtomic(filePath, payload);
|
|
2117
|
+
if (opts.slug) publishCollectionChange(collectionChangePayload({
|
|
2118
|
+
slug: opts.slug,
|
|
2119
|
+
ids: [safeId],
|
|
2120
|
+
op: "upsert"
|
|
2121
|
+
}, opts.workspaceRoot));
|
|
2122
|
+
return {
|
|
2123
|
+
kind: "ok",
|
|
2124
|
+
itemId: safeId,
|
|
2125
|
+
item
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
async function deleteItem(dataDir, itemId, opts = {}) {
|
|
2129
|
+
const safeId = safeRecordId(itemId);
|
|
2130
|
+
if (safeId === null) return {
|
|
2131
|
+
kind: "invalid-id",
|
|
2132
|
+
itemId
|
|
2133
|
+
};
|
|
2134
|
+
if (escapesWorkspace(dataDir, opts.workspaceRoot ?? getWorkspaceRoot(), safeId, "deleteItem")) return {
|
|
2135
|
+
kind: "path-escape",
|
|
2136
|
+
itemId: safeId
|
|
2137
|
+
};
|
|
2138
|
+
const filePath = itemFilePath(dataDir, safeId);
|
|
2139
|
+
try {
|
|
2140
|
+
await unlink(filePath);
|
|
2141
|
+
if (opts.slug) publishCollectionChange(collectionChangePayload({
|
|
2142
|
+
slug: opts.slug,
|
|
2143
|
+
ids: [safeId],
|
|
2144
|
+
op: "delete"
|
|
2145
|
+
}, opts.workspaceRoot));
|
|
2146
|
+
return {
|
|
2147
|
+
kind: "ok",
|
|
2148
|
+
itemId: safeId
|
|
2149
|
+
};
|
|
2150
|
+
} catch (err) {
|
|
2151
|
+
if (isErrorWithCode(err) && err.code === "ENOENT") return {
|
|
2152
|
+
kind: "not-found",
|
|
2153
|
+
itemId: safeId
|
|
2154
|
+
};
|
|
2155
|
+
throw err;
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
/** Generate a short random hex id. Used by POST when the form doesn't
|
|
2159
|
+
* carry a primary-key value (UI shortcut — Claude normally derives a
|
|
2160
|
+
* semantic id from the record's name). */
|
|
2161
|
+
function generateItemId() {
|
|
2162
|
+
return randomBytes(4).toString("hex");
|
|
2163
|
+
}
|
|
2164
|
+
/** The item id a CREATE should use for `schema`, or null when the
|
|
2165
|
+
* caller should generate one. A singleton collection pins every
|
|
2166
|
+
* create to its fixed `schema.singleton` id, so the "at most one
|
|
2167
|
+
* record" contract is enforced server-side (a second create targets
|
|
2168
|
+
* the same file and hits `writeItem`'s refuseOverwrite conflict) —
|
|
2169
|
+
* not only in the UI. Otherwise the record's own primaryKey value
|
|
2170
|
+
* wins, falling back to a generated id (null = "generate"). */
|
|
2171
|
+
function resolveCreateItemId(schema, record) {
|
|
2172
|
+
if (schema.singleton) return schema.singleton;
|
|
2173
|
+
const primaryRaw = record[schema.primaryKey];
|
|
2174
|
+
return typeof primaryRaw === "string" && primaryRaw.length > 0 ? primaryRaw : null;
|
|
2175
|
+
}
|
|
2176
|
+
//#endregion
|
|
2177
|
+
//#region src/collection/core/queryZ.ts
|
|
2178
|
+
/** Result-column aliases double as SQL identifiers and JSON keys — keep
|
|
2179
|
+
* them to a conservative identifier charset so neither side needs
|
|
2180
|
+
* escaping gymnastics. */
|
|
2181
|
+
var SAFE_ALIAS_PATTERN = /^[A-Za-z_]\w{0,63}$/;
|
|
2182
|
+
/** Hard ceiling on returned rows; `limit` clamps below it. A group-by on
|
|
2183
|
+
* a near-unique column would otherwise return one row per source row —
|
|
2184
|
+
* the exact materialization the aggregate path exists to avoid. */
|
|
2185
|
+
var MAX_QUERY_ROWS = 1e4;
|
|
2186
|
+
/** Default row cap when the query declares no `limit`. */
|
|
2187
|
+
var DEFAULT_QUERY_ROWS = 1e3;
|
|
2188
|
+
/** One aggregate column: `count` (rows; `column` optional to count
|
|
2189
|
+
* non-null cells) or `sum`/`avg`/`min`/`max` over a named CSV column. */
|
|
2190
|
+
var QueryAggregateZ = z.object({
|
|
2191
|
+
op: z.enum([
|
|
2192
|
+
"count",
|
|
2193
|
+
"sum",
|
|
2194
|
+
"avg",
|
|
2195
|
+
"min",
|
|
2196
|
+
"max"
|
|
2197
|
+
]),
|
|
2198
|
+
column: z.string().min(1).optional()
|
|
2199
|
+
}).refine((aggregate) => aggregate.op === "count" || aggregate.column !== void 0, {
|
|
2200
|
+
message: "`column` is required for every aggregate op except `count`",
|
|
2201
|
+
path: ["column"]
|
|
2793
2202
|
});
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2203
|
+
/** One filter condition. Same op vocabulary as the schema-level `where`
|
|
2204
|
+
* (`core/where.ts`) so authors learn one set; values may be typed
|
|
2205
|
+
* (number / boolean) since CSV columns are. `in` requires an array
|
|
2206
|
+
* value, every other op a scalar. */
|
|
2207
|
+
var QueryWhereZ = z.object({
|
|
2208
|
+
field: z.string().min(1),
|
|
2209
|
+
op: z.enum([
|
|
2210
|
+
"eq",
|
|
2211
|
+
"ne",
|
|
2212
|
+
"in",
|
|
2213
|
+
"gt",
|
|
2214
|
+
"gte",
|
|
2215
|
+
"lt",
|
|
2216
|
+
"lte",
|
|
2217
|
+
"contains"
|
|
2218
|
+
]),
|
|
2219
|
+
value: z.union([
|
|
2220
|
+
z.string(),
|
|
2221
|
+
z.number(),
|
|
2222
|
+
z.boolean(),
|
|
2223
|
+
z.array(z.union([
|
|
2224
|
+
z.string(),
|
|
2225
|
+
z.number(),
|
|
2226
|
+
z.boolean()
|
|
2227
|
+
])).min(1).max(100)
|
|
2228
|
+
])
|
|
2229
|
+
}).refine((cond) => cond.op === "in" === Array.isArray(cond.value), {
|
|
2230
|
+
message: "`in` requires an array value (the allowed set); every other op requires a scalar value",
|
|
2231
|
+
path: ["value"]
|
|
2232
|
+
});
|
|
2233
|
+
var QueryOrderZ = z.object({
|
|
2234
|
+
/** A `groupBy` column or an aggregate alias — membership enforced by
|
|
2235
|
+
* the whole-query refine below. */
|
|
2236
|
+
field: z.string().min(1),
|
|
2237
|
+
dir: z.enum(["asc", "desc"]).optional()
|
|
2238
|
+
});
|
|
2239
|
+
/** The whole query. At least one of `groupBy` / `aggregates` must be
|
|
2240
|
+
* present: bare `groupBy` is a DISTINCT listing, bare `aggregates` a
|
|
2241
|
+
* whole-file scalar row, together a grouped aggregation. */
|
|
2242
|
+
var CollectionQueryZ = z.object({
|
|
2243
|
+
groupBy: z.array(z.string().min(1)).max(8).refine((columns) => new Set(columns.map((column) => column.toLowerCase())).size === columns.length, { message: "`groupBy` columns must be unique (case-insensitively — SQL identifiers ignore case)" }).optional(),
|
|
2244
|
+
aggregates: z.record(z.string().regex(SAFE_ALIAS_PATTERN, "aggregate aliases must be simple identifiers (letters/digits/underscore)"), QueryAggregateZ).optional(),
|
|
2245
|
+
where: z.array(QueryWhereZ).max(16).optional(),
|
|
2246
|
+
orderBy: z.array(QueryOrderZ).max(4).optional(),
|
|
2247
|
+
limit: z.number().int().min(1).max(MAX_QUERY_ROWS).optional()
|
|
2248
|
+
}).refine((query) => (query.groupBy?.length ?? 0) > 0 || Object.keys(query.aggregates ?? {}).length > 0, {
|
|
2249
|
+
message: "declare at least one of `groupBy` (columns to bucket by) or `aggregates` (values to compute)",
|
|
2250
|
+
path: ["groupBy"]
|
|
2251
|
+
}).refine((query) => Object.keys(query.aggregates ?? {}).length <= 32, {
|
|
2252
|
+
message: `\`aggregates\` supports at most 32 entries`,
|
|
2253
|
+
path: ["aggregates"]
|
|
2254
|
+
}).refine((query) => {
|
|
2255
|
+
const groupLower = new Set((query.groupBy ?? []).map((column) => column.toLowerCase()));
|
|
2256
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2257
|
+
return Object.keys(query.aggregates ?? {}).every((alias) => {
|
|
2258
|
+
const lower = alias.toLowerCase();
|
|
2259
|
+
if (groupLower.has(lower) || seen.has(lower)) return false;
|
|
2260
|
+
seen.add(lower);
|
|
2261
|
+
return true;
|
|
2262
|
+
});
|
|
2263
|
+
}, {
|
|
2264
|
+
message: "aggregate aliases must be unique and must not collide with `groupBy` column names (case-insensitively — SQL identifiers ignore case)",
|
|
2265
|
+
path: ["aggregates"]
|
|
2266
|
+
}).refine((query) => {
|
|
2267
|
+
const sortable = /* @__PURE__ */ new Set([...query.groupBy ?? [], ...Object.keys(query.aggregates ?? {})]);
|
|
2268
|
+
return (query.orderBy ?? []).every((order) => sortable.has(order.field));
|
|
2269
|
+
}, {
|
|
2270
|
+
message: "every `orderBy.field` must be a `groupBy` column or an aggregate alias",
|
|
2271
|
+
path: ["orderBy"]
|
|
2272
|
+
});
|
|
2273
|
+
//#endregion
|
|
2274
|
+
//#region src/collection/server/csvQuery.ts
|
|
2275
|
+
/** Double-quote a SQL identifier (CSV column name / result alias). */
|
|
2276
|
+
function quoteIdent(name) {
|
|
2277
|
+
return `"${name.replaceAll("\"", "\"\"")}"`;
|
|
2278
|
+
}
|
|
2279
|
+
/** Single-quote a SQL string literal (a `types={...}` struct key). */
|
|
2280
|
+
function quoteLiteral(value) {
|
|
2281
|
+
return `'${value.replaceAll("'", "''")}'`;
|
|
2282
|
+
}
|
|
2283
|
+
/** The `read_csv` argument list shared by every CSV query: the (prepared)
|
|
2284
|
+
* path plus a `types` pin forcing the key column to VARCHAR — without it
|
|
2285
|
+
* DuckDB's sniffer turns `001` into BIGINT 1, so leading zeros vanish
|
|
2286
|
+
* and distinct keys collapse. */
|
|
2287
|
+
function readCsvArgs(primaryKey) {
|
|
2288
|
+
return `?, types={${quoteLiteral(primaryKey)}: 'VARCHAR'}`;
|
|
2289
|
+
}
|
|
2290
|
+
/** One aggregate's SQL expression. `sum`/`avg` TRY_CAST to DOUBLE so a
|
|
2291
|
+
* column the sniffer kept as VARCHAR (mixed values) aggregates over its
|
|
2292
|
+
* numeric cells instead of erroring; non-numeric cells become NULL and
|
|
2293
|
+
* are skipped — standard BI tolerance. `min`/`max` stay native (they are
|
|
2294
|
+
* meaningful on strings and dates too). */
|
|
2295
|
+
function aggregateExpr(aggregate) {
|
|
2296
|
+
const { op, column } = aggregate;
|
|
2297
|
+
if (op === "count") return column === void 0 ? "count(*)" : `count(${quoteIdent(column)})`;
|
|
2298
|
+
if (op === "sum" || op === "avg") return `${op}(TRY_CAST(${quoteIdent(column ?? "")} AS DOUBLE))`;
|
|
2299
|
+
return `${op}(${quoteIdent(column ?? "")})`;
|
|
2300
|
+
}
|
|
2301
|
+
/** One where condition → SQL fragment + its bound parameters. String
|
|
2302
|
+
* equality compares against `CAST(col AS VARCHAR)` so a sniffer-typed
|
|
2303
|
+
* column still matches its textual value; numeric/boolean values compare
|
|
2304
|
+
* natively (DuckDB coerces the column side). */
|
|
2305
|
+
function whereFragment(cond) {
|
|
2306
|
+
const column = quoteIdent(cond.field);
|
|
2307
|
+
const asText = `CAST(${column} AS VARCHAR)`;
|
|
2308
|
+
if (cond.op === "in") {
|
|
2309
|
+
const values = arrayValue(cond);
|
|
2310
|
+
return {
|
|
2311
|
+
sql: `${values.every((value) => typeof value === "string") ? asText : column} IN (${values.map(() => "?").join(", ")})`,
|
|
2312
|
+
params: values
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2315
|
+
if (cond.op === "contains") return {
|
|
2316
|
+
sql: `contains(${asText}, ?)`,
|
|
2317
|
+
params: [String(scalarValue(cond))]
|
|
2318
|
+
};
|
|
2319
|
+
const operator = {
|
|
2320
|
+
eq: "=",
|
|
2321
|
+
ne: "<>",
|
|
2322
|
+
gt: ">",
|
|
2323
|
+
gte: ">=",
|
|
2324
|
+
lt: "<",
|
|
2325
|
+
lte: "<="
|
|
2326
|
+
}[cond.op];
|
|
2327
|
+
return {
|
|
2328
|
+
sql: `${typeof cond.value === "string" && (cond.op === "eq" || cond.op === "ne") ? asText : column} ${operator} ?`,
|
|
2329
|
+
params: [scalarValue(cond)]
|
|
2330
|
+
};
|
|
2331
|
+
}
|
|
2332
|
+
/** Mirror of `scalarValue` for the one op that takes a set: a scalar under
|
|
2333
|
+
* `in` also means the query skipped `CollectionQueryZ`. Left unchecked it
|
|
2334
|
+
* failed as `values.every is not a function`, naming neither the field nor
|
|
2335
|
+
* the op. */
|
|
2336
|
+
function arrayValue(cond) {
|
|
2337
|
+
if (!Array.isArray(cond.value)) throw new Error(`where condition on '${cond.field}' uses op 'in', which requires an array value, not a scalar`);
|
|
2338
|
+
return cond.value;
|
|
2339
|
+
}
|
|
2340
|
+
/** `CollectionQueryZ` refines "`in` ⇔ array value", so an array reaching a
|
|
2341
|
+
* scalar op means the query was compiled without being validated first —
|
|
2342
|
+
* binding it would send an array to a single `?`. */
|
|
2343
|
+
function scalarValue(cond) {
|
|
2344
|
+
if (Array.isArray(cond.value)) throw new Error(`where condition on '${cond.field}' uses op '${cond.op}', which requires a scalar value, not an array`);
|
|
2345
|
+
return cond.value;
|
|
2804
2346
|
}
|
|
2805
|
-
/**
|
|
2806
|
-
*
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2347
|
+
/** Compile a validated query against `fromSql` (a table-function call
|
|
2348
|
+
* whose FIRST placeholder is the source path — the executor binds it).
|
|
2349
|
+
* Returns the SQL and the where-value parameters that follow the path.
|
|
2350
|
+
* Callers MUST have run `CollectionQueryZ` first; this function trusts
|
|
2351
|
+
* the shape (aliases already charset-checked, orderBy membership already
|
|
2352
|
+
* enforced). */
|
|
2353
|
+
function compileQuery(query, fromSql) {
|
|
2354
|
+
const groupBy = query.groupBy ?? [];
|
|
2355
|
+
const aggregates = Object.entries(query.aggregates ?? {});
|
|
2356
|
+
const selectList = [...groupBy.map(quoteIdent), ...aggregates.map(([alias, aggregate]) => `${aggregateExpr(aggregate)} AS ${quoteIdent(alias)}`)];
|
|
2357
|
+
const where = (query.where ?? []).map(whereFragment);
|
|
2358
|
+
const clauses = [`SELECT ${selectList.join(", ")}`, `FROM ${fromSql}`];
|
|
2359
|
+
if (where.length > 0) clauses.push(`WHERE ${where.map((fragment) => fragment.sql).join(" AND ")}`);
|
|
2360
|
+
if (groupBy.length > 0) clauses.push(`GROUP BY ${groupBy.map(quoteIdent).join(", ")}`);
|
|
2361
|
+
const orderBy = (query.orderBy ?? []).map((order) => quoteIdent(order.field) + (order.dir === "desc" ? " DESC" : " ASC"));
|
|
2362
|
+
if (orderBy.length > 0) clauses.push(`ORDER BY ${orderBy.join(", ")}`);
|
|
2363
|
+
clauses.push(`LIMIT ${query.limit ?? 1e3}`);
|
|
2364
|
+
return {
|
|
2365
|
+
sql: clauses.join(" "),
|
|
2366
|
+
params: where.flatMap((fragment) => fragment.params)
|
|
2367
|
+
};
|
|
2810
2368
|
}
|
|
2811
|
-
/**
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
return isRecord(holder) ? holder[key] : void 0;
|
|
2369
|
+
/** Compile against a CSV file (the dataSource store's engine). */
|
|
2370
|
+
function compileCsvQuery(query, primaryKey) {
|
|
2371
|
+
return compileQuery(query, `read_csv(${readCsvArgs(primaryKey)})`);
|
|
2815
2372
|
}
|
|
2816
|
-
/**
|
|
2817
|
-
*
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2373
|
+
/** Compile against a JSONL file of ENRICHED records — the file-backed
|
|
2374
|
+
* collections' engine (see `jsonlQuery.ts`). No VARCHAR key pin needed:
|
|
2375
|
+
* enriched record ids are already strings. `sample_size=-1` makes the
|
|
2376
|
+
* schema inference scan EVERY line — with the default sample, a sparse
|
|
2377
|
+
* optional/derived field first appearing past the sample would not be
|
|
2378
|
+
* inferred as a column and the query would binder-error on it (Codex P2
|
|
2379
|
+
* on #2165). The full scan costs nothing extra here: aggregation reads
|
|
2380
|
+
* the whole file anyway. */
|
|
2381
|
+
function compileJsonlQuery(query) {
|
|
2382
|
+
return compileQuery(query, `read_json(?, format='newline_delimited', sample_size=-1)`);
|
|
2383
|
+
}
|
|
2384
|
+
//#endregion
|
|
2385
|
+
//#region src/collection/server/csvStore.ts
|
|
2386
|
+
/** `list()` row cap. Over-cap files are truncated with a warn — the v1
|
|
2387
|
+
* contract is "browse + per-record views", not full-table analytics. */
|
|
2388
|
+
var MAX_CSV_ROWS = 5e3;
|
|
2389
|
+
/** Record ids minted from non-safe key values: `id0x` + utf-8 hex. Raw key
|
|
2390
|
+
* values that themselves match this pattern are ALSO encoded, so the
|
|
2391
|
+
* encoded namespace never collides with a raw value (injective mapping). */
|
|
2392
|
+
var ENCODED_ID_PATTERN = /^id0x([0-9a-f]+)$/;
|
|
2393
|
+
/** A CSV key value → the record id it's addressed by. Safe values pass
|
|
2394
|
+
* through untouched; everything else (and anything shaped like an encoded
|
|
2395
|
+
* id) becomes `id0x<hex>`. Pure + exported for unit tests. */
|
|
2396
|
+
function encodeCsvRecordId(rawKey) {
|
|
2397
|
+
if (safeRecordId(rawKey) === rawKey && !ENCODED_ID_PATTERN.test(rawKey)) return rawKey;
|
|
2398
|
+
return `id0x${Buffer.from(rawKey, "utf-8").toString("hex")}`;
|
|
2399
|
+
}
|
|
2400
|
+
/** A record id → the CSV key value to look up. Inverse of
|
|
2401
|
+
* `encodeCsvRecordId` for encoded ids; anything else is already the raw
|
|
2402
|
+
* value. Pure + exported for unit tests. */
|
|
2403
|
+
function decodeCsvRecordId(itemId) {
|
|
2404
|
+
const hex = ENCODED_ID_PATTERN.exec(itemId)?.[1];
|
|
2405
|
+
if (hex === void 0) return itemId;
|
|
2406
|
+
return Buffer.from(hex, "hex").toString("utf-8");
|
|
2407
|
+
}
|
|
2408
|
+
/** Normalize one DuckDB JS value into a JSON-safe record value: BigInt →
|
|
2409
|
+
* number (string beyond the safe range), DATE/TIMESTAMP → ISO string
|
|
2410
|
+
* (date-only when the clock is exactly UTC midnight, matching the `date`
|
|
2411
|
+
* field contract), exotic DuckDB values → their string form. Pure +
|
|
2412
|
+
* exported for unit tests. */
|
|
2413
|
+
/** `JSON.stringify` restricted to what a CSV cell can survive. Returns the
|
|
2414
|
+
* serialised value, or `String(value)` when serialisation is impossible —
|
|
2415
|
+
* losing the content of one cell is bad, failing the entire query is worse. */
|
|
2416
|
+
function safeJsonCell(value) {
|
|
2417
|
+
try {
|
|
2418
|
+
return JSON.stringify(value, (_key, entry) => typeof entry === "bigint" ? entry.toString() : entry) ?? String(value);
|
|
2419
|
+
} catch {
|
|
2420
|
+
return String(value);
|
|
2822
2421
|
}
|
|
2823
|
-
return null;
|
|
2824
2422
|
}
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
if (
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2423
|
+
function normalizeCsvValue(value) {
|
|
2424
|
+
if (typeof value === "bigint") return value <= BigInt(Number.MAX_SAFE_INTEGER) && value >= BigInt(-Number.MAX_SAFE_INTEGER) ? Number(value) : value.toString();
|
|
2425
|
+
if (value instanceof Date) {
|
|
2426
|
+
const iso = value.toISOString();
|
|
2427
|
+
return iso.endsWith("T00:00:00.000Z") ? iso.slice(0, 10) : iso;
|
|
2428
|
+
}
|
|
2429
|
+
if (value !== null && typeof value === "object") return safeJsonCell(value);
|
|
2430
|
+
return value;
|
|
2431
|
+
}
|
|
2432
|
+
/** One raw DuckDB row → a CollectionItem, or null when the key cell is
|
|
2433
|
+
* missing/empty (the row can't be addressed). The primaryKey field is
|
|
2434
|
+
* OVERWRITTEN with the (possibly encoded) record id so `item[primaryKey]`
|
|
2435
|
+
* and the record's address never drift — same invariant the file store's
|
|
2436
|
+
* write path enforces. Pure + exported for unit tests. */
|
|
2437
|
+
function csvRowToItem(row, primaryKey) {
|
|
2438
|
+
const normalized = Object.fromEntries(Object.entries(row).map(([key, value]) => [key, normalizeCsvValue(value)]));
|
|
2439
|
+
const rawKey = normalized[primaryKey];
|
|
2440
|
+
const keyText = fieldTextOrNull(rawKey);
|
|
2441
|
+
if (keyText === null || keyText === "") return null;
|
|
2442
|
+
return {
|
|
2443
|
+
...normalized,
|
|
2444
|
+
[primaryKey]: encodeCsvRecordId(keyText)
|
|
2445
|
+
};
|
|
2446
|
+
}
|
|
2447
|
+
/** Dedupe by record id, LAST row wins (matches `csvRead`'s last-match
|
|
2448
|
+
* pick). Returns the surviving items in first-seen order. Pure +
|
|
2449
|
+
* exported for unit tests. */
|
|
2450
|
+
function dedupeByRecordId(items, primaryKey) {
|
|
2451
|
+
const byId = /* @__PURE__ */ new Map();
|
|
2452
|
+
for (const item of items) byId.set(String(item[primaryKey]), item);
|
|
2453
|
+
return {
|
|
2454
|
+
items: [...byId.values()],
|
|
2455
|
+
duplicates: items.length - byId.size
|
|
2456
|
+
};
|
|
2457
|
+
}
|
|
2458
|
+
/** True when a thrown DuckDB error is the `types` pin naming a column the
|
|
2459
|
+
* CSV doesn't have — the schema/file-mismatch case the caller downgrades
|
|
2460
|
+
* to "empty collection + warn" instead of a 500. */
|
|
2461
|
+
function isMissingKeyColumnError(err) {
|
|
2462
|
+
return String(err).includes("do not exist in the CSV");
|
|
2463
|
+
}
|
|
2464
|
+
/** Bytes sniffed for UTF-8 validity. The trailing 3 bytes of the sample
|
|
2465
|
+
* are dropped so a multibyte char split at the boundary can't produce a
|
|
2466
|
+
* false negative on a valid file. */
|
|
2467
|
+
var SNIFF_BYTES = 1048576;
|
|
2468
|
+
function isValidUtf8(buf) {
|
|
2469
|
+
try {
|
|
2470
|
+
new TextDecoder("utf-8", { fatal: true }).decode(buf);
|
|
2471
|
+
return true;
|
|
2472
|
+
} catch {
|
|
2473
|
+
return false;
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
/** Detect the (best-effort) encoding of a non-UTF-8 buffer. BOMs decide
|
|
2477
|
+
* UTF-16; otherwise cp932 (the Shift_JIS superset — Excel-exported
|
|
2478
|
+
* Japanese CSVs are the primary non-UTF-8 case this feature serves). */
|
|
2479
|
+
function fallbackEncoding(buf) {
|
|
2480
|
+
if (buf.length >= 2 && buf[0] === 255 && buf[1] === 254) return "utf-16le";
|
|
2481
|
+
if (buf.length >= 2 && buf[0] === 254 && buf[1] === 255) return "utf-16be";
|
|
2482
|
+
return "cp932";
|
|
2483
|
+
}
|
|
2484
|
+
function cacheDir() {
|
|
2485
|
+
return path.join(tmpdir(), "mulmoclaude-csv-utf8");
|
|
2486
|
+
}
|
|
2487
|
+
/** Read only the first `bytes` of a file — the encoding sniff must not
|
|
2488
|
+
* pull a multi-hundred-MB CSV into memory on the (common) UTF-8 path. */
|
|
2489
|
+
async function readHead(absPath, bytes) {
|
|
2490
|
+
const handle = await open(absPath, "r");
|
|
2491
|
+
try {
|
|
2492
|
+
const { size } = await handle.stat();
|
|
2493
|
+
const buf = Buffer.alloc(Math.min(bytes, size));
|
|
2494
|
+
await handle.read(buf, 0, buf.length, 0);
|
|
2495
|
+
return buf;
|
|
2496
|
+
} finally {
|
|
2497
|
+
await handle.close();
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
/** Decode the whole file into a UTF-8 cache copy and return its path.
|
|
2501
|
+
* Cache key = (path, mtime, size), so a replaced CSV re-decodes and an
|
|
2502
|
+
* unchanged one never does. */
|
|
2503
|
+
async function pathExists(target) {
|
|
2504
|
+
try {
|
|
2505
|
+
await stat(target);
|
|
2506
|
+
return true;
|
|
2507
|
+
} catch {
|
|
2508
|
+
return false;
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
/** Best-effort removal of older decode-cache entries for the same source
|
|
2512
|
+
* path — a frequently-replaced large CSV would otherwise accumulate one
|
|
2513
|
+
* full copy per (mtime, size) forever. Runs AFTER the current copy is
|
|
2514
|
+
* published; a concurrent reader holding an old fd is unaffected
|
|
2515
|
+
* (unlink-while-open is safe on POSIX). */
|
|
2516
|
+
async function evictSupersededCache(key, keepBasename) {
|
|
2517
|
+
try {
|
|
2518
|
+
const entries = await readdir(cacheDir());
|
|
2519
|
+
await Promise.all(entries.filter((name) => name.startsWith(`${key}-`) && name !== keepBasename).map((name) => unlink(path.join(cacheDir(), name)).catch(() => void 0)));
|
|
2520
|
+
} catch {}
|
|
2521
|
+
}
|
|
2522
|
+
/** Decode the whole file into a UTF-8 cache copy and return its path.
|
|
2523
|
+
* Cache key = (path, mtime, size), so a replaced CSV re-decodes and an
|
|
2524
|
+
* unchanged one never does; superseded copies are evicted. The cache
|
|
2525
|
+
* lives in the SHARED OS tmpdir, so the dir is 0700 and files 0600 —
|
|
2526
|
+
* decoded rows must not be readable by other local users. */
|
|
2527
|
+
async function decodeToCache(absPath, info) {
|
|
2528
|
+
const key = createHash("sha256").update(absPath).digest("hex").slice(0, 16);
|
|
2529
|
+
const cached = path.join(cacheDir(), `${key}-${Math.trunc(info.mtimeMs)}-${info.size}.csv`);
|
|
2530
|
+
if (!await pathExists(cached)) {
|
|
2531
|
+
const whole = await readFile(absPath);
|
|
2532
|
+
const encoding = fallbackEncoding(whole);
|
|
2533
|
+
const text = iconv.decode(whole, encoding);
|
|
2534
|
+
await mkdir(cacheDir(), {
|
|
2535
|
+
recursive: true,
|
|
2536
|
+
mode: 448
|
|
2537
|
+
});
|
|
2538
|
+
const tmp = `${cached}.${randomBytes(4).toString("hex")}.tmp`;
|
|
2539
|
+
await writeFile(tmp, text, {
|
|
2540
|
+
encoding: "utf-8",
|
|
2541
|
+
mode: 384
|
|
2542
|
+
});
|
|
2543
|
+
await rename(tmp, cached);
|
|
2544
|
+
log.info("collections", "decoded non-UTF-8 dataSource file to cache", {
|
|
2545
|
+
path: absPath,
|
|
2546
|
+
encoding
|
|
2547
|
+
});
|
|
2548
|
+
await evictSupersededCache(key, path.basename(cached));
|
|
2549
|
+
}
|
|
2550
|
+
return cached;
|
|
2551
|
+
}
|
|
2552
|
+
/** Re-validate the dataSource file at READ time, mirroring the JSON
|
|
2553
|
+
* store's per-read defenses: realpath containment (a symlink swapped in
|
|
2554
|
+
* after discovery must not walk out of the workspace) and an lstat
|
|
2555
|
+
* regular-file check (a symlink leaf is refused outright, even one
|
|
2556
|
+
* pointing inside the workspace — same rule as `isRegularFile` on
|
|
2557
|
+
* record files). Returns the stat info, or null for "no readable file"
|
|
2558
|
+
* (ENOENT / refused), which callers render as an empty collection. */
|
|
2559
|
+
async function safeCsvStat(absPath, workspaceRoot) {
|
|
2560
|
+
if (!isContainedInRoot(absPath, workspaceRoot)) {
|
|
2561
|
+
log.warn("collections", "dataSource read refused: path escapes workspace", { path: absPath });
|
|
2562
|
+
return null;
|
|
2563
|
+
}
|
|
2564
|
+
let info;
|
|
2565
|
+
try {
|
|
2566
|
+
info = await lstat(absPath);
|
|
2567
|
+
} catch (err) {
|
|
2568
|
+
if (isErrorWithCode(err) && err.code === "ENOENT") return null;
|
|
2569
|
+
throw err;
|
|
2570
|
+
}
|
|
2571
|
+
if (!info.isFile()) {
|
|
2572
|
+
log.warn("collections", "dataSource read refused: not a regular file (symlink?)", { path: absPath });
|
|
2573
|
+
return null;
|
|
2574
|
+
}
|
|
2575
|
+
return info;
|
|
2576
|
+
}
|
|
2577
|
+
/** Return a path DuckDB can read as UTF-8: the original file when it
|
|
2578
|
+
* already is UTF-8 (the cheap, common case — only the head is sniffed),
|
|
2579
|
+
* else a decoded cache copy (see `decodeToCache`). Returns null when
|
|
2580
|
+
* there is no readable file (missing, symlink, or containment-refused —
|
|
2581
|
+
* see `safeCsvStat`), which callers render as an empty collection. */
|
|
2582
|
+
async function ensureUtf8CsvPath(absPath, workspaceRoot) {
|
|
2583
|
+
const info = await safeCsvStat(absPath, workspaceRoot);
|
|
2584
|
+
if (info === null) return null;
|
|
2585
|
+
const head = await readHead(absPath, SNIFF_BYTES);
|
|
2586
|
+
const sample = head.length === SNIFF_BYTES ? head.subarray(0, 1048573) : head;
|
|
2587
|
+
if (!(head.length >= 2 && (head[0] === 255 && head[1] === 254 || head[0] === 254 && head[1] === 255)) && isValidUtf8(sample)) return absPath;
|
|
2588
|
+
return decodeToCache(absPath, info);
|
|
2589
|
+
}
|
|
2590
|
+
var instancePromise = null;
|
|
2591
|
+
/** Lazily create one shared in-memory DuckDB instance. The dynamic import
|
|
2592
|
+
* keeps the native module OUT of core's load path — a platform where the
|
|
2593
|
+
* prebuilt binding is missing degrades to a per-query error on dataSource
|
|
2594
|
+
* collections only, never a broken core. A failed init is retried on the
|
|
2595
|
+
* next call (the promise is reset). */
|
|
2596
|
+
async function duckDbInstance() {
|
|
2597
|
+
if (instancePromise === null) instancePromise = import("@duckdb/node-api").then((mod) => mod.DuckDBInstance.create(":memory:"));
|
|
2598
|
+
try {
|
|
2599
|
+
return await instancePromise;
|
|
2600
|
+
} catch (err) {
|
|
2601
|
+
instancePromise = null;
|
|
2602
|
+
throw new BackendUnavailableError(`DuckDB is unavailable on this host (@duckdb/node-api failed to load: ${String(err)}) — dataSource collections cannot be read`);
|
|
2835
2603
|
}
|
|
2836
|
-
return prototypeActionParamPath(input);
|
|
2837
2604
|
}
|
|
2838
|
-
|
|
2839
|
-
const
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
});
|
|
2845
|
-
return z.NEVER;
|
|
2605
|
+
async function queryCsv(sql, params) {
|
|
2606
|
+
const connection = await (await duckDbInstance()).connect();
|
|
2607
|
+
try {
|
|
2608
|
+
return (await connection.runAndReadAll(sql, params)).getRowObjectsJS();
|
|
2609
|
+
} finally {
|
|
2610
|
+
connection.disconnectSync();
|
|
2846
2611
|
}
|
|
2847
|
-
return input;
|
|
2848
|
-
}, BareCollectionSchemaZ);
|
|
2849
|
-
//#endregion
|
|
2850
|
-
//#region src/collection/server/discovery.ts
|
|
2851
|
-
function applyFeedSchemaDefaults(parsed, slug) {
|
|
2852
|
-
if (!isRecord(parsed)) return parsed;
|
|
2853
|
-
const icon = typeof parsed.icon === "string" && parsed.icon.trim().length > 0 ? parsed.icon : "dynamic_feed";
|
|
2854
|
-
return {
|
|
2855
|
-
...parsed,
|
|
2856
|
-
icon,
|
|
2857
|
-
dataPath: `data/feeds/${slug}`
|
|
2858
|
-
};
|
|
2859
|
-
}
|
|
2860
|
-
/** The conventional per-slug records dir a `dataSource` / `storage` collection
|
|
2861
|
-
* gets as its `dataDir` (records never live there, but archive/delete paths
|
|
2862
|
-
* stay well-defined — same shape the registry's R3 normalization uses).
|
|
2863
|
-
*
|
|
2864
|
-
* INVARIANT — this is NOT a default `dataPath`, and must not be used as one.
|
|
2865
|
-
* It applies only to the two backends whose records are not per-file JSON. A
|
|
2866
|
-
* normal collection declares its own location and exactly one of `dataPath` /
|
|
2867
|
-
* `dataSource` / `storage`; a schema with none of the three is REJECTED, not
|
|
2868
|
-
* quietly pointed here. Handing a per-file collection this path would silently
|
|
2869
|
-
* relocate its records away from the folder the user (and its SKILL.md) sees. */
|
|
2870
|
-
function conventionalDataPath(slug) {
|
|
2871
|
-
return `data/collections/${slug}/items`;
|
|
2872
|
-
}
|
|
2873
|
-
/** The declared field named by `primaryKey`, or `undefined` when the schema
|
|
2874
|
-
* declares no such field. Own-property guarded: a `primaryKey` of `toString`
|
|
2875
|
-
* / `constructor` / `__proto__` must miss here, not read an Object.prototype
|
|
2876
|
-
* member and slip past the "is it a declared field?" gate into the wrong
|
|
2877
|
-
* "add `primary: true`" advice. Shared with manageCollection's putSchema
|
|
2878
|
-
* gate so both report the SAME reason. */
|
|
2879
|
-
function resolvePrimaryField(fields, primaryKey) {
|
|
2880
|
-
return Object.hasOwn(fields, primaryKey) ? fields[primaryKey] : void 0;
|
|
2881
2612
|
}
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
* rename is silently pinned back to the URL itemId on save, so the user's
|
|
2888
|
-
* edit is dropped with no error;
|
|
2889
|
-
* - a `feed` schema must declare an `ingest` block (else it's a dead,
|
|
2890
|
-
* non-refreshable card);
|
|
2891
|
-
* - `dataPath` — or a `dataSource`'s `path` — must resolve INSIDE the
|
|
2892
|
-
* workspace (same realpath containment for both).
|
|
2893
|
-
*
|
|
2894
|
-
* Exported so `manageCollection`'s `putSchema` can run the SAME gates before
|
|
2895
|
-
* it reports success — a schema that passes `CollectionSchemaZ` but fails one
|
|
2896
|
-
* of these would otherwise write cleanly yet be skipped on the next discovery,
|
|
2897
|
-
* hiding the collection (the exact failure that tool exists to prevent). */
|
|
2898
|
-
function acceptParsedSchema(schema, opts) {
|
|
2899
|
-
const primaryField = resolvePrimaryField(schema.fields, schema.primaryKey);
|
|
2900
|
-
if (!primaryField) return {
|
|
2901
|
-
ok: false,
|
|
2902
|
-
reason: `primaryKey '${schema.primaryKey}' is not one of the declared fields`
|
|
2903
|
-
};
|
|
2904
|
-
if (primaryField.primary !== true) return {
|
|
2905
|
-
ok: false,
|
|
2906
|
-
reason: `the primaryKey field '${schema.primaryKey}' must be flagged \`primary: true\``
|
|
2907
|
-
};
|
|
2908
|
-
if (opts.source === "feed" && !schema.ingest) return {
|
|
2909
|
-
ok: false,
|
|
2910
|
-
reason: "a feed schema must declare an `ingest` block"
|
|
2613
|
+
async function csvList(absPath, primaryKey, workspaceRoot) {
|
|
2614
|
+
const utf8Path = await ensureUtf8CsvPath(absPath, workspaceRoot ?? getWorkspaceRoot());
|
|
2615
|
+
if (utf8Path === null) return {
|
|
2616
|
+
items: [],
|
|
2617
|
+
truncated: false
|
|
2911
2618
|
};
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
reason: `slug '${opts.slug}' yields no workspace-contained data dir`
|
|
2922
|
-
};
|
|
2619
|
+
let rows;
|
|
2620
|
+
try {
|
|
2621
|
+
rows = await queryCsv(`SELECT * FROM read_csv(${readCsvArgs(primaryKey)}) LIMIT 5001`, [utf8Path]);
|
|
2622
|
+
} catch (err) {
|
|
2623
|
+
if (!isMissingKeyColumnError(err)) throw err;
|
|
2624
|
+
log.warn("collections", "dataSource CSV has no primaryKey column — every row is skipped", {
|
|
2625
|
+
path: absPath,
|
|
2626
|
+
primaryKey
|
|
2627
|
+
});
|
|
2923
2628
|
return {
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
dataSourceFile
|
|
2629
|
+
items: [],
|
|
2630
|
+
truncated: false
|
|
2927
2631
|
};
|
|
2928
2632
|
}
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2633
|
+
const truncated = rows.length > MAX_CSV_ROWS;
|
|
2634
|
+
if (truncated) {
|
|
2635
|
+
log.warn("collections", "dataSource CSV truncated to row cap", {
|
|
2636
|
+
path: absPath,
|
|
2637
|
+
cap: MAX_CSV_ROWS
|
|
2638
|
+
});
|
|
2639
|
+
rows.length = MAX_CSV_ROWS;
|
|
2640
|
+
}
|
|
2641
|
+
const items = rows.map((row) => csvRowToItem(row, primaryKey)).filter((item) => item !== null);
|
|
2642
|
+
const skipped = rows.length - items.length;
|
|
2643
|
+
if (skipped > 0) log.warn("collections", "dataSource CSV rows skipped (empty key cell)", {
|
|
2644
|
+
path: absPath,
|
|
2645
|
+
skipped
|
|
2646
|
+
});
|
|
2647
|
+
const deduped = dedupeByRecordId(items, primaryKey);
|
|
2648
|
+
if (deduped.duplicates > 0) log.warn("collections", "dataSource CSV has duplicate key values (last row wins)", {
|
|
2649
|
+
path: absPath,
|
|
2650
|
+
duplicates: deduped.duplicates
|
|
2651
|
+
});
|
|
2935
2652
|
return {
|
|
2936
|
-
|
|
2937
|
-
|
|
2653
|
+
items: deduped.items,
|
|
2654
|
+
truncated
|
|
2938
2655
|
};
|
|
2939
2656
|
}
|
|
2940
|
-
/** The
|
|
2941
|
-
*
|
|
2942
|
-
*
|
|
2657
|
+
/** The scan-order ordinal column the last-match read adds. Underscore
|
|
2658
|
+
* prefix keeps it out of any plausible CSV header namespace; it is
|
|
2659
|
+
* stripped from the returned record either way. */
|
|
2660
|
+
var ROW_ORDINAL = "__mc_row";
|
|
2661
|
+
/** One record by id. The comparison value rides as a prepared-statement
|
|
2662
|
+
* parameter, and the LAST matching row is selected IN DuckDB (scan-order
|
|
2663
|
+
* ordinal + LIMIT 1) — a CSV with thousands of duplicate keys must not
|
|
2664
|
+
* materialize them all for one detail read. Consistent with csvList's
|
|
2665
|
+
* last-wins dedupe. */
|
|
2666
|
+
async function csvRead(absPath, primaryKey, itemId, workspaceRoot) {
|
|
2667
|
+
const utf8Path = await ensureUtf8CsvPath(absPath, workspaceRoot ?? getWorkspaceRoot());
|
|
2668
|
+
if (utf8Path === null) return null;
|
|
2669
|
+
const rawKey = decodeCsvRecordId(itemId);
|
|
2670
|
+
const last = (await queryCsv(`SELECT * FROM (SELECT *, row_number() OVER () AS ${quoteIdent(ROW_ORDINAL)} FROM read_csv(${readCsvArgs(primaryKey)})) WHERE CAST(${quoteIdent(primaryKey)} AS VARCHAR) = ? ORDER BY ${quoteIdent(ROW_ORDINAL)} DESC LIMIT 1`, [utf8Path, rawKey])).at(0);
|
|
2671
|
+
if (last === void 0) return null;
|
|
2672
|
+
const { [ROW_ORDINAL]: __ordinal, ...record } = last;
|
|
2673
|
+
return csvRowToItem(record, primaryKey);
|
|
2674
|
+
}
|
|
2675
|
+
/** Run a validated aggregation query (the structured DSL — see
|
|
2676
|
+
* `core/queryZ.ts`) over the WHOLE file: no row cap on the scan (a
|
|
2677
|
+
* capped aggregate would be a wrong number), only the result-row LIMIT
|
|
2678
|
+
* the compiler emits. Values are normalized like list/read rows so a
|
|
2679
|
+
* chart consumer gets plain JSON scalars. */
|
|
2680
|
+
async function csvRunQuery(absPath, primaryKey, query, workspaceRoot) {
|
|
2681
|
+
const utf8Path = await ensureUtf8CsvPath(absPath, workspaceRoot ?? getWorkspaceRoot());
|
|
2682
|
+
if (utf8Path === null) return [];
|
|
2683
|
+
const { sql, params } = compileCsvQuery(query, primaryKey);
|
|
2684
|
+
return (await queryCsv(sql, [utf8Path, ...params])).map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [key, normalizeCsvValue(value)])));
|
|
2685
|
+
}
|
|
2686
|
+
//#endregion
|
|
2687
|
+
//#region src/collection/server/watchFs.ts
|
|
2688
|
+
/** An atomic file replace (editor save, `mv` over the target) surfaces as
|
|
2689
|
+
* 2-3 events. Collapse them so one user action reports one change. */
|
|
2690
|
+
var REPLACE_DEBOUNCE_MS = 300;
|
|
2691
|
+
/** The path to hand `watch()`, with Windows 8.3 short names resolved away.
|
|
2943
2692
|
*
|
|
2944
|
-
*
|
|
2945
|
-
*
|
|
2946
|
-
*
|
|
2947
|
-
*
|
|
2693
|
+
* ReadDirectoryChangesW reports filenames against the LONG path, but a watch
|
|
2694
|
+
* opened on a short path (`C:\Users\RUNNER~1\…` — what `os.tmpdir()` returns
|
|
2695
|
+
* on GitHub's Windows runners) keeps the short form. libuv's
|
|
2696
|
+
* `assert(!_wcsnicmp(filename, dir, dirlen))` in `src/win/fs-event.c` then
|
|
2697
|
+
* aborts the PROCESS on the first event — a native assert, so neither
|
|
2698
|
+
* `watcher.on("error")` nor a try/catch can contain it.
|
|
2948
2699
|
*
|
|
2949
|
-
*
|
|
2950
|
-
* `
|
|
2951
|
-
*
|
|
2952
|
-
*
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
ok: false,
|
|
2961
|
-
reason: `slug '${opts.slug}' yields no workspace-contained data dir`
|
|
2962
|
-
};
|
|
2963
|
-
if (storage.type === "sqlite") {
|
|
2964
|
-
const storageFile = resolveDataDir(storage.path, opts.workspaceRoot);
|
|
2965
|
-
if (storageFile === null) return {
|
|
2966
|
-
ok: false,
|
|
2967
|
-
reason: `storage.path '${storage.path}' escapes the workspace`
|
|
2968
|
-
};
|
|
2969
|
-
return {
|
|
2970
|
-
ok: true,
|
|
2971
|
-
dataDir,
|
|
2972
|
-
storageFile
|
|
2973
|
-
};
|
|
2974
|
-
}
|
|
2975
|
-
const manifest = loadAppManifest(opts.workspaceRoot);
|
|
2976
|
-
if (!manifest.ok) return {
|
|
2977
|
-
ok: false,
|
|
2978
|
-
reason: appManifestReason(manifest, opts.workspaceRoot)
|
|
2979
|
-
};
|
|
2980
|
-
return {
|
|
2981
|
-
ok: true,
|
|
2982
|
-
dataDir,
|
|
2983
|
-
appId: manifest.manifest.aid
|
|
2984
|
-
};
|
|
2700
|
+
* POSIX is deliberately left alone: `realpath` there also collapses symlinks
|
|
2701
|
+
* (`/var` → `/private/var` on macOS), which we neither need nor want to
|
|
2702
|
+
* change. A failure falls back to the original path — worst case we are no
|
|
2703
|
+
* worse off than before. */
|
|
2704
|
+
function watchablePath(dir) {
|
|
2705
|
+
if (process.platform !== "win32") return dir;
|
|
2706
|
+
try {
|
|
2707
|
+
return realpathSync.native(dir);
|
|
2708
|
+
} catch {
|
|
2709
|
+
return dir;
|
|
2710
|
+
}
|
|
2985
2711
|
}
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
let raw;
|
|
2712
|
+
/** Watch `dir`, reporting each accepted filename. `accept` decides what is
|
|
2713
|
+
* noise; a null filename always passes (the platform didn't tell us which
|
|
2714
|
+
* file, so the caller must assume the worst). */
|
|
2715
|
+
async function watchDirectory(dir, accept, onHit) {
|
|
2991
2716
|
try {
|
|
2992
|
-
|
|
2993
|
-
|
|
2717
|
+
await mkdir(dir, { recursive: true });
|
|
2718
|
+
const watcher = watch(watchablePath(dir), { persistent: false }, (_eventType, rawFilename) => {
|
|
2719
|
+
const filename = rawFilename === null ? null : String(rawFilename);
|
|
2720
|
+
if (filename !== null && !accept(filename)) return;
|
|
2721
|
+
onHit(filename);
|
|
2722
|
+
});
|
|
2723
|
+
watcher.on("error", (err) => {
|
|
2724
|
+
log.warn("collections", "fs watch error", {
|
|
2725
|
+
dir,
|
|
2726
|
+
error: String(err)
|
|
2727
|
+
});
|
|
2728
|
+
});
|
|
2729
|
+
return { close: () => watcher.close() };
|
|
2994
2730
|
} catch (err) {
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
path: schemaPath,
|
|
2731
|
+
log.warn("collections", "fs watch start failed", {
|
|
2732
|
+
dir,
|
|
2998
2733
|
error: String(err)
|
|
2999
2734
|
});
|
|
3000
2735
|
return null;
|
|
3001
2736
|
}
|
|
3002
|
-
|
|
2737
|
+
}
|
|
2738
|
+
/** Watch the single file `absPath` by watching its PARENT directory, so an
|
|
2739
|
+
* atomic replace can't strand the watch on a dead inode. `alsoAccept`
|
|
2740
|
+
* widens the filter beyond the exact basename (sqlite's `-wal`/`-journal`
|
|
2741
|
+
* sidecars). Reports are debounced: one replace, one call. */
|
|
2742
|
+
async function watchSingleFile(absPath, alsoAccept, onChange) {
|
|
2743
|
+
const dir = path.dirname(absPath);
|
|
2744
|
+
const base = path.basename(absPath);
|
|
2745
|
+
let timer = null;
|
|
2746
|
+
const fire = () => {
|
|
2747
|
+
if (timer) clearTimeout(timer);
|
|
2748
|
+
timer = setTimeout(() => {
|
|
2749
|
+
timer = null;
|
|
2750
|
+
onChange();
|
|
2751
|
+
}, REPLACE_DEBOUNCE_MS);
|
|
2752
|
+
timer.unref?.();
|
|
2753
|
+
};
|
|
2754
|
+
const handle = await watchDirectory(dir, (filename) => filename === base || alsoAccept(base, filename), fire);
|
|
2755
|
+
if (!handle) return null;
|
|
2756
|
+
return { close: () => {
|
|
2757
|
+
if (timer) clearTimeout(timer);
|
|
2758
|
+
timer = null;
|
|
2759
|
+
handle.close();
|
|
2760
|
+
} };
|
|
2761
|
+
}
|
|
2762
|
+
/** An `FsWatchHandle` as a bare unsubscribe — `null` straight through, so an
|
|
2763
|
+
* unarmed watch stays distinguishable from an armed one. Lives here rather
|
|
2764
|
+
* than beside the store contract so both `store.ts` and the backends it
|
|
2765
|
+
* registers can reach it without importing each other. */
|
|
2766
|
+
function closerFor(handle) {
|
|
2767
|
+
return handle === null ? null : () => handle.close();
|
|
2768
|
+
}
|
|
2769
|
+
//#endregion
|
|
2770
|
+
//#region src/collection/server/sqliteStore.ts
|
|
2771
|
+
/** A constructor's parameter and return types are not observable at runtime,
|
|
2772
|
+
* so the check stops at "DatabaseSync is constructible" — the only member of
|
|
2773
|
+
* the module this store ever touches. */
|
|
2774
|
+
function isSqliteModule(mod) {
|
|
2775
|
+
return isRecord(mod) && typeof mod.DatabaseSync === "function";
|
|
2776
|
+
}
|
|
2777
|
+
var sqliteModule = null;
|
|
2778
|
+
/** Drops the memo first so a later call can retry (e.g. tests stubbing the
|
|
2779
|
+
* runtime), then reports why the backend is unusable. */
|
|
2780
|
+
function sqliteUnavailable(reason) {
|
|
2781
|
+
sqliteModule = null;
|
|
2782
|
+
throw new BackendUnavailableError(`sqlite storage needs the node:sqlite module (Node.js >= 22.5) — this runtime cannot load it: ${reason}`);
|
|
2783
|
+
}
|
|
2784
|
+
/** Lazy-load node:sqlite once. A runtime without it (Node < 22.5) throws a
|
|
2785
|
+
* clearly-worded error the caller surfaces — never a bare MODULE_NOT_FOUND. */
|
|
2786
|
+
function loadSqlite() {
|
|
2787
|
+
sqliteModule ??= import("node:sqlite").then((mod) => isSqliteModule(mod) ? mod : sqliteUnavailable("the module exposes no DatabaseSync constructor"), (err) => sqliteUnavailable(String(err)));
|
|
2788
|
+
return sqliteModule;
|
|
2789
|
+
}
|
|
2790
|
+
/** The db file's on-disk state. A symlink or non-regular file is refused
|
|
2791
|
+
* (file-disclosure defense, same rule as io.ts record files); ENOENT is
|
|
2792
|
+
* just "no records yet". Any OTHER lstat failure (EACCES, EIO, …) is
|
|
2793
|
+
* rethrown so reads surface a real filesystem problem instead of
|
|
2794
|
+
* silently reporting an empty collection. */
|
|
2795
|
+
async function dbFileState(absPath) {
|
|
3003
2796
|
try {
|
|
3004
|
-
|
|
2797
|
+
return (await lstat(absPath)).isFile() ? "file" : "refused";
|
|
3005
2798
|
} catch (err) {
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
error: String(err)
|
|
3009
|
-
});
|
|
3010
|
-
return null;
|
|
2799
|
+
if (isErrorWithCode(err) && err.code === "ENOENT") return "missing";
|
|
2800
|
+
throw err;
|
|
3011
2801
|
}
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
2802
|
+
}
|
|
2803
|
+
var CREATE_TABLE = "CREATE TABLE IF NOT EXISTS records (id TEXT PRIMARY KEY, record TEXT NOT NULL)";
|
|
2804
|
+
/** Open the database for one operation, classifying the two unavailable
|
|
2805
|
+
* states so callers can map them honestly (`refused` ⇒ path-escape,
|
|
2806
|
+
* `missing` ⇒ empty / not-found — conflating them would misreport a
|
|
2807
|
+
* containment escape as "item not found"). The containment pre-check runs
|
|
2808
|
+
* BEFORE mkdir even when the file is missing — `isContainedInRoot`
|
|
2809
|
+
* resolves through the closest existing ancestor, so a symlinked-away
|
|
2810
|
+
* parent can never make the recursive mkdir create directories outside
|
|
2811
|
+
* the workspace (same pre/post belt-and-suspenders as io.ts writes). */
|
|
2812
|
+
async function openDb(absPath, workspaceRoot, mode) {
|
|
2813
|
+
const state = await dbFileState(absPath);
|
|
2814
|
+
if (state === "refused") {
|
|
2815
|
+
log.warn("collections", "sqlite database refused: not a regular file", { path: absPath });
|
|
2816
|
+
return { kind: "refused" };
|
|
3020
2817
|
}
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
}
|
|
3032
|
-
return null;
|
|
2818
|
+
if (!isContainedInRoot(path.dirname(absPath), workspaceRoot)) {
|
|
2819
|
+
log.warn("collections", "sqlite refused: database dir escapes workspace via symlink", { path: absPath });
|
|
2820
|
+
return { kind: "refused" };
|
|
2821
|
+
}
|
|
2822
|
+
if (mode === "read" && state === "missing") return { kind: "missing" };
|
|
2823
|
+
if (mode === "write") {
|
|
2824
|
+
await mkdir(path.dirname(absPath), { recursive: true });
|
|
2825
|
+
if (!isContainedInRoot(path.dirname(absPath), workspaceRoot)) {
|
|
2826
|
+
log.warn("collections", "sqlite write refused: database dir escapes workspace via symlink (post-mkdir)", { path: absPath });
|
|
2827
|
+
return { kind: "refused" };
|
|
2828
|
+
}
|
|
3033
2829
|
}
|
|
2830
|
+
const { DatabaseSync } = await loadSqlite();
|
|
2831
|
+
const database = new DatabaseSync(absPath);
|
|
2832
|
+
database.exec("PRAGMA busy_timeout = 5000");
|
|
2833
|
+
database.exec(CREATE_TABLE);
|
|
3034
2834
|
return {
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
2835
|
+
kind: "ok",
|
|
2836
|
+
database
|
|
2837
|
+
};
|
|
2838
|
+
}
|
|
2839
|
+
/** Run `operation` against the database and always close it; unavailable
|
|
2840
|
+
* states resolve through `onUnavailable` so each caller maps `missing`
|
|
2841
|
+
* vs `refused` to its own result kind. */
|
|
2842
|
+
async function withDb(absPath, workspaceRoot, mode, onUnavailable, operation) {
|
|
2843
|
+
const handle = await openDb(absPath, workspaceRoot, mode);
|
|
2844
|
+
if (handle.kind !== "ok") return onUnavailable(handle.kind);
|
|
2845
|
+
try {
|
|
2846
|
+
return await operation(handle.database);
|
|
2847
|
+
} finally {
|
|
2848
|
+
handle.database.close();
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
var SQLITE_CONSTRAINT_PRIMARYKEY = 1555;
|
|
2852
|
+
var SQLITE_CONSTRAINT_UNIQUE = 2067;
|
|
2853
|
+
/** node:sqlite throws ERR_SQLITE_ERROR with the SQLite extended result
|
|
2854
|
+
* code on `errcode`. Checked structurally (message text kept only as a
|
|
2855
|
+
* fallback for runtimes that don't expose `errcode`). */
|
|
2856
|
+
function isUniqueConstraintError(err) {
|
|
2857
|
+
if (hasNumberProp(err, "errcode")) return err.errcode === SQLITE_CONSTRAINT_PRIMARYKEY || err.errcode === SQLITE_CONSTRAINT_UNIQUE;
|
|
2858
|
+
return String(err).includes("UNIQUE constraint");
|
|
2859
|
+
}
|
|
2860
|
+
function parseRow(raw) {
|
|
2861
|
+
if (typeof raw !== "string") return null;
|
|
2862
|
+
try {
|
|
2863
|
+
const parsed = JSON.parse(raw);
|
|
2864
|
+
return isRecord(parsed) ? parsed : null;
|
|
2865
|
+
} catch {
|
|
2866
|
+
return null;
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
/** One column of a result row. node:sqlite types rows as `unknown`, so a
|
|
2870
|
+
* value that is not a row object yields no column at all. */
|
|
2871
|
+
function readColumn(row, column) {
|
|
2872
|
+
return isRecord(row) ? row[column] : void 0;
|
|
2873
|
+
}
|
|
2874
|
+
function rowsToItems(rows) {
|
|
2875
|
+
return rows.map((row) => parseRow(readColumn(row, "record"))).filter((item) => item !== null);
|
|
2876
|
+
}
|
|
2877
|
+
/** node:sqlite hands back an integer column as `number`, or as `bigint` once
|
|
2878
|
+
* it leaves the safe-integer range — COUNT(*) can be either. */
|
|
2879
|
+
function countRecords(database) {
|
|
2880
|
+
const count = readColumn(database.prepare("SELECT COUNT(*) AS n FROM records").get(), "n");
|
|
2881
|
+
if (typeof count === "number") return count;
|
|
2882
|
+
if (typeof count === "bigint") return Number(count);
|
|
2883
|
+
throw new Error(`sqlite COUNT(*) returned no numeric row count (got ${typeof count})`);
|
|
2884
|
+
}
|
|
2885
|
+
async function sqliteList(absPath, workspaceRoot) {
|
|
2886
|
+
return withDb(absPath, workspaceRoot, "read", () => [], (database) => rowsToItems(database.prepare("SELECT record FROM records ORDER BY id").all()));
|
|
2887
|
+
}
|
|
2888
|
+
async function sqlitePage(absPath, primaryKey, opts, workspaceRoot) {
|
|
2889
|
+
const emptyPage = {
|
|
2890
|
+
items: [],
|
|
2891
|
+
total: 0,
|
|
2892
|
+
truncated: false
|
|
2893
|
+
};
|
|
2894
|
+
return withDb(absPath, workspaceRoot, "read", () => emptyPage, (database) => {
|
|
2895
|
+
const total = countRecords(database);
|
|
2896
|
+
const offset = Math.max(0, opts.offset ?? 0);
|
|
2897
|
+
const limit = opts.limit === void 0 ? -1 : Math.max(0, opts.limit);
|
|
2898
|
+
return {
|
|
2899
|
+
items: projectItemFields(rowsToItems(database.prepare("SELECT record FROM records ORDER BY id LIMIT ? OFFSET ?").all(limit, offset)), opts.fields, primaryKey),
|
|
2900
|
+
total,
|
|
2901
|
+
truncated: false
|
|
2902
|
+
};
|
|
2903
|
+
});
|
|
2904
|
+
}
|
|
2905
|
+
async function sqliteRead(absPath, itemId, workspaceRoot) {
|
|
2906
|
+
const safeId = safeRecordId(itemId);
|
|
2907
|
+
if (safeId === null) return null;
|
|
2908
|
+
return withDb(absPath, workspaceRoot, "read", () => null, (database) => {
|
|
2909
|
+
return parseRow(readColumn(database.prepare("SELECT record FROM records WHERE id = ?").get(safeId), "record"));
|
|
2910
|
+
});
|
|
2911
|
+
}
|
|
2912
|
+
async function sqliteWrite(absPath, itemId, item, opts) {
|
|
2913
|
+
const safeId = safeRecordId(itemId);
|
|
2914
|
+
if (safeId === null) return {
|
|
2915
|
+
kind: "invalid-id",
|
|
2916
|
+
itemId
|
|
2917
|
+
};
|
|
2918
|
+
const outcome = await withDb(absPath, opts.workspaceRoot, "write", () => ({
|
|
2919
|
+
kind: "path-escape",
|
|
2920
|
+
itemId: safeId
|
|
2921
|
+
}), (database) => {
|
|
2922
|
+
const payload = JSON.stringify(item);
|
|
2923
|
+
if (opts.refuseOverwrite) try {
|
|
2924
|
+
database.prepare("INSERT INTO records (id, record) VALUES (?, ?)").run(safeId, payload);
|
|
2925
|
+
} catch (err) {
|
|
2926
|
+
if (isUniqueConstraintError(err)) return {
|
|
2927
|
+
kind: "conflict",
|
|
2928
|
+
itemId: safeId
|
|
2929
|
+
};
|
|
2930
|
+
throw err;
|
|
2931
|
+
}
|
|
2932
|
+
else database.prepare("INSERT INTO records (id, record) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET record = excluded.record").run(safeId, payload);
|
|
2933
|
+
return {
|
|
2934
|
+
kind: "ok",
|
|
2935
|
+
itemId: safeId,
|
|
2936
|
+
item
|
|
2937
|
+
};
|
|
2938
|
+
});
|
|
2939
|
+
if (outcome.kind === "ok" && opts.slug) publishCollectionChange(collectionChangePayload({
|
|
2940
|
+
slug: opts.slug,
|
|
2941
|
+
ids: [safeId],
|
|
2942
|
+
op: "upsert"
|
|
2943
|
+
}, opts.publishRoot));
|
|
2944
|
+
return outcome;
|
|
2945
|
+
}
|
|
2946
|
+
async function sqliteDelete(absPath, itemId, opts) {
|
|
2947
|
+
const safeId = safeRecordId(itemId);
|
|
2948
|
+
if (safeId === null) return {
|
|
2949
|
+
kind: "invalid-id",
|
|
2950
|
+
itemId
|
|
3043
2951
|
};
|
|
2952
|
+
const outcome = await withDb(absPath, opts.workspaceRoot, "read", (reason) => reason === "refused" ? {
|
|
2953
|
+
kind: "path-escape",
|
|
2954
|
+
itemId: safeId
|
|
2955
|
+
} : {
|
|
2956
|
+
kind: "not-found",
|
|
2957
|
+
itemId: safeId
|
|
2958
|
+
}, (database) => {
|
|
2959
|
+
const { changes } = database.prepare("DELETE FROM records WHERE id = ?").run(safeId);
|
|
2960
|
+
return Number(changes) === 0 ? {
|
|
2961
|
+
kind: "not-found",
|
|
2962
|
+
itemId: safeId
|
|
2963
|
+
} : {
|
|
2964
|
+
kind: "ok",
|
|
2965
|
+
itemId: safeId
|
|
2966
|
+
};
|
|
2967
|
+
});
|
|
2968
|
+
if (outcome.kind === "ok" && opts.slug) publishCollectionChange(collectionChangePayload({
|
|
2969
|
+
slug: opts.slug,
|
|
2970
|
+
ids: [safeId],
|
|
2971
|
+
op: "delete"
|
|
2972
|
+
}, opts.publishRoot));
|
|
2973
|
+
return outcome;
|
|
3044
2974
|
}
|
|
3045
|
-
|
|
3046
|
-
|
|
2975
|
+
/** Best-effort full WAL checkpoint so the MAIN db file alone is a
|
|
2976
|
+
* complete snapshot (committed pages in `<db>-wal` are folded in and the
|
|
2977
|
+
* WAL truncated). Used by `deleteCollection` before archiving. Returns
|
|
2978
|
+
* false on any failure (runtime without node:sqlite, locked db, missing
|
|
2979
|
+
* file) — the caller then archives the sidecar files alongside the db so
|
|
2980
|
+
* no committed data is lost either way. */
|
|
2981
|
+
async function checkpointSqliteDatabase(absPath) {
|
|
3047
2982
|
try {
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
if (isErrorWithCode(err) && err.code === "ENOENT") return [];
|
|
3051
|
-
log.warn("collections", "failed to list skills dir, returning empty", {
|
|
3052
|
-
root: skillsRoot,
|
|
3053
|
-
error: String(err)
|
|
3054
|
-
});
|
|
3055
|
-
return [];
|
|
3056
|
-
}
|
|
3057
|
-
const results = [];
|
|
3058
|
-
for (const name of entries) {
|
|
3059
|
-
if (name.startsWith(".")) continue;
|
|
3060
|
-
const safeName = safeSlugName(name);
|
|
3061
|
-
if (safeName === null) continue;
|
|
3062
|
-
const dirPath = path.join(skillsRoot, safeName);
|
|
3063
|
-
let dirStat;
|
|
2983
|
+
const { DatabaseSync } = await loadSqlite();
|
|
2984
|
+
const database = new DatabaseSync(absPath);
|
|
3064
2985
|
try {
|
|
3065
|
-
|
|
3066
|
-
}
|
|
3067
|
-
|
|
2986
|
+
database.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
2987
|
+
} finally {
|
|
2988
|
+
database.close();
|
|
3068
2989
|
}
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
2990
|
+
return true;
|
|
2991
|
+
} catch {
|
|
2992
|
+
return false;
|
|
3072
2993
|
}
|
|
3073
|
-
return results;
|
|
3074
2994
|
}
|
|
3075
|
-
/**
|
|
3076
|
-
*
|
|
3077
|
-
*
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
2995
|
+
/** A `storage: sqlite` store over `collection.storageFile`. A schema whose
|
|
2996
|
+
* `storageFile` failed to resolve yields a read-only EMPTY store rather
|
|
2997
|
+
* than a writable one — same fail-closed rule as the CSV store. */
|
|
2998
|
+
function sqliteStoreFor(collection, opts) {
|
|
2999
|
+
const file = collection.storageFile;
|
|
3000
|
+
const key = collection.schema.primaryKey;
|
|
3001
|
+
const slug = opts.slug ?? collection.slug;
|
|
3002
|
+
const root = () => opts.workspaceRoot ?? getWorkspaceRoot();
|
|
3003
|
+
const publishRoot = opts.workspaceRoot;
|
|
3004
|
+
if (file === void 0) return {
|
|
3005
|
+
capabilities: {
|
|
3006
|
+
writable: false,
|
|
3007
|
+
nativeQuery: false,
|
|
3008
|
+
nativePaging: false
|
|
3009
|
+
},
|
|
3010
|
+
list: () => Promise.resolve([]),
|
|
3011
|
+
page: () => Promise.resolve({
|
|
3012
|
+
items: [],
|
|
3013
|
+
total: 0,
|
|
3014
|
+
truncated: false
|
|
3015
|
+
}),
|
|
3016
|
+
read: () => Promise.resolve(null)
|
|
3017
|
+
};
|
|
3018
|
+
return {
|
|
3019
|
+
capabilities: {
|
|
3020
|
+
writable: true,
|
|
3021
|
+
nativeQuery: false,
|
|
3022
|
+
nativePaging: true
|
|
3023
|
+
},
|
|
3024
|
+
list: () => sqliteList(file, root()),
|
|
3025
|
+
page: (pageOpts = {}) => sqlitePage(file, key, pageOpts, root()),
|
|
3026
|
+
read: (itemId) => sqliteRead(file, itemId, root()),
|
|
3027
|
+
write: (itemId, item, writeOpts = {}) => sqliteWrite(file, itemId, item, {
|
|
3028
|
+
workspaceRoot: root(),
|
|
3029
|
+
publishRoot,
|
|
3030
|
+
slug,
|
|
3031
|
+
refuseOverwrite: writeOpts.refuseOverwrite
|
|
3032
|
+
}),
|
|
3033
|
+
delete: (itemId) => sqliteDelete(file, itemId, {
|
|
3034
|
+
workspaceRoot: root(),
|
|
3035
|
+
publishRoot,
|
|
3036
|
+
slug
|
|
3037
|
+
}),
|
|
3038
|
+
watch: async (onChange) => closerFor(await watchSingleFile(file, (base, name) => name.startsWith(base), () => onChange({ kind: "collection" })))
|
|
3039
|
+
};
|
|
3082
3040
|
}
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
*
|
|
3087
|
-
*
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
const userCollections = userDir === null ? [] : await collectFromDir(userDir, "user", workspaceRoot);
|
|
3096
|
-
const projectCollections = await collectFromDir(projectDir, "project", workspaceRoot);
|
|
3097
|
-
const merged = /* @__PURE__ */ new Map();
|
|
3098
|
-
for (const entry of feedCollections) merged.set(entry.slug, entry);
|
|
3099
|
-
for (const entry of userCollections) merged.set(entry.slug, entry);
|
|
3100
|
-
for (const entry of projectCollections) merged.set(entry.slug, entry);
|
|
3101
|
-
return [...merged.values()].sort((left, right) => left.slug.localeCompare(right.slug));
|
|
3041
|
+
//#endregion
|
|
3042
|
+
//#region src/collection/server/store.ts
|
|
3043
|
+
/** The file store's stable order: lexicographic by record id (codepoint
|
|
3044
|
+
* compare — locale-independent). `listItems` returns readdir order, which
|
|
3045
|
+
* is filesystem-dependent; paging needs determinism. */
|
|
3046
|
+
function sortByRecordId(items, primaryKey) {
|
|
3047
|
+
return [...items].sort((left, right) => {
|
|
3048
|
+
const leftId = fieldText(left[primaryKey]);
|
|
3049
|
+
const rightId = fieldText(right[primaryKey]);
|
|
3050
|
+
if (leftId < rightId) return -1;
|
|
3051
|
+
return leftId > rightId ? 1 : 0;
|
|
3052
|
+
});
|
|
3102
3053
|
}
|
|
3103
|
-
/**
|
|
3104
|
-
*
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
const userDir = resolveUserDir(opts, workspaceRoot);
|
|
3110
|
-
const projectCollection = await loadOneCollection(projectSkillsDir(workspaceRoot), safeName, "project", workspaceRoot);
|
|
3111
|
-
if (projectCollection) return projectCollection;
|
|
3112
|
-
const userCollection = userDir === null ? null : await loadOneCollection(userDir, safeName, "user", workspaceRoot);
|
|
3113
|
-
if (userCollection) return userCollection;
|
|
3114
|
-
return loadOneCollection(feedsRoot(workspaceRoot), safeName, "feed", workspaceRoot);
|
|
3054
|
+
/** True when the collection accepts UI/tool writes. A `dataSource`
|
|
3055
|
+
* collection is read-only: updates happen by editing/replacing the
|
|
3056
|
+
* data file itself. Every write entry point checks this BEFORE calling
|
|
3057
|
+
* `writeItem`/`deleteItem` — server-enforced, not just UI-hidden. */
|
|
3058
|
+
function collectionWritable(collection) {
|
|
3059
|
+
return !isReadOnlySchema(collection.schema);
|
|
3115
3060
|
}
|
|
3116
|
-
|
|
3061
|
+
/** The one-line refusal write paths surface (HTTP 405 / MCP error text). */
|
|
3062
|
+
function readOnlyRefusal(slug) {
|
|
3063
|
+
return `collection '${slug}' is read-only (backed by an external dataSource) — update the data file itself instead`;
|
|
3064
|
+
}
|
|
3065
|
+
/** A `dataSource` store over `file` (CSV row order; DuckDB-native query).
|
|
3066
|
+
* A schema whose `dataSourceFile` failed to resolve yields a read-only
|
|
3067
|
+
* EMPTY store rather than falling back to the (writable) file store — a
|
|
3068
|
+
* half-loaded read-only collection must never become writable. */
|
|
3069
|
+
function csvStoreFor(collection, opts) {
|
|
3070
|
+
const file = collection.dataSourceFile;
|
|
3071
|
+
const key = collection.schema.primaryKey;
|
|
3072
|
+
const listAll = () => file === void 0 ? Promise.resolve({
|
|
3073
|
+
items: [],
|
|
3074
|
+
truncated: false
|
|
3075
|
+
}) : csvList(file, key, opts.workspaceRoot);
|
|
3117
3076
|
return {
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3077
|
+
capabilities: {
|
|
3078
|
+
writable: false,
|
|
3079
|
+
nativeQuery: true,
|
|
3080
|
+
nativePaging: false
|
|
3081
|
+
},
|
|
3082
|
+
list: () => listAll().then((result) => result.items),
|
|
3083
|
+
page: (pageOpts = {}) => listAll().then((result) => pageFromFullRead(result.items, pageOpts, key, result.truncated)),
|
|
3084
|
+
read: (itemId) => file === void 0 ? Promise.resolve(null) : csvRead(file, key, itemId, opts.workspaceRoot),
|
|
3085
|
+
query: (query) => file === void 0 ? Promise.resolve([]) : csvRunQuery(file, key, query, opts.workspaceRoot),
|
|
3086
|
+
...file === void 0 ? {} : { watch: async (onChange) => closerFor(await watchSingleFile(file, () => false, () => onChange({ kind: "collection" }))) }
|
|
3124
3087
|
};
|
|
3125
3088
|
}
|
|
3126
|
-
|
|
3089
|
+
/** The classic file store over `<dataDir>/<itemId>.json` records. */
|
|
3090
|
+
function fileStoreFor(collection, opts) {
|
|
3091
|
+
const key = collection.schema.primaryKey;
|
|
3092
|
+
const ioOpts = {
|
|
3093
|
+
...opts,
|
|
3094
|
+
slug: opts.slug ?? collection.slug
|
|
3095
|
+
};
|
|
3127
3096
|
return {
|
|
3128
|
-
|
|
3129
|
-
|
|
3097
|
+
capabilities: {
|
|
3098
|
+
writable: true,
|
|
3099
|
+
nativeQuery: false,
|
|
3100
|
+
nativePaging: false
|
|
3101
|
+
},
|
|
3102
|
+
list: () => listItems(collection.dataDir, opts),
|
|
3103
|
+
page: async (pageOpts = {}) => pageFromFullRead(sortByRecordId(await listItems(collection.dataDir, opts), key), pageOpts, key, false),
|
|
3104
|
+
read: (itemId) => readItem(collection.dataDir, itemId, opts),
|
|
3105
|
+
write: (itemId, item, writeOpts = {}) => writeItem(collection.dataDir, itemId, item, {
|
|
3106
|
+
...ioOpts,
|
|
3107
|
+
refuseOverwrite: writeOpts.refuseOverwrite
|
|
3108
|
+
}),
|
|
3109
|
+
delete: (itemId) => deleteItem(collection.dataDir, itemId, ioOpts),
|
|
3110
|
+
watch: async (onChange) => closerFor(await watchDirectory(collection.dataDir, (name) => name.endsWith(".json") && !name.startsWith("."), (filename) => onChange(filename === null ? { kind: "collection" } : {
|
|
3111
|
+
kind: "item",
|
|
3112
|
+
itemId: filename.slice(0, -5)
|
|
3113
|
+
})))
|
|
3130
3114
|
};
|
|
3131
3115
|
}
|
|
3116
|
+
var storeFactories = /* @__PURE__ */ new Map([
|
|
3117
|
+
["file", fileStoreFor],
|
|
3118
|
+
["csv", csvStoreFor],
|
|
3119
|
+
["sqlite", sqliteStoreFor],
|
|
3120
|
+
["firestore", firestoreStoreFor]
|
|
3121
|
+
]);
|
|
3122
|
+
/** Pick the store implementation for a discovered collection via the
|
|
3123
|
+
* factory registry. An unknown kind cannot normally reach here (the
|
|
3124
|
+
* schema's `StorageZ` union gates it), so the throw is a loud invariant
|
|
3125
|
+
* breach, not a user-facing path. */
|
|
3126
|
+
function storeFor(collection, opts = {}) {
|
|
3127
|
+
const kind = storageKindFor(collection.schema);
|
|
3128
|
+
const factory = storeFactories.get(kind);
|
|
3129
|
+
if (!factory) throw new Error(`no store factory registered for storage kind '${kind}'`);
|
|
3130
|
+
return factory(collection, opts);
|
|
3131
|
+
}
|
|
3132
3132
|
//#endregion
|
|
3133
|
-
export { collectionsRegistriesConfigPath as $,
|
|
3133
|
+
export { collectionsRegistriesConfigPath as $, toSummary as A, SCHEMA_FILE as B, resolveCreateItemId as C, loadCollection as D, discoverCollections as E, loadAppManifest as F, safeRecordId as G, itemFilePath as H, parseAppManifest as I, isBackendUnavailable as J, safeSlugName as K, sharedItemsPath as L, resolveMutateSet as M, APP_MANIFEST_FILE as N, resolvePrimaryField as O, appManifestReason as P, collectionChangePayload as Q, pageFromFullRead as R, readItem as S, acceptParsedSchema as T, resolveDataDir as U, isContainedInRoot as V, resolveTemplatePath as W, archiveDir as X, COLLECTION_ROOT_REQUIRED as Y, collectionChangeKey as Z, MAX_QUERY_ROWS as _, MAX_CSV_ROWS as a, log as at, isRegularFile as b, decodeCsvRecordId as c, publishCollectionChange as ct, normalizeCsvValue as d, sharedCollectionChangePayload as dt, configureCollectionHost as et, queryCsv as f, skillsStagingDir as ft, DEFAULT_QUERY_ROWS as g, CollectionQueryZ as h, createHostSlot as ht, checkpointSqliteDatabase as i, localCollectionKey as it, CollectionSchemaZ as j, toDetail as k, dedupeByRecordId as l, setCollectionChangePublisher as lt, compileJsonlQuery as m, createForwardingLogger as mt, readOnlyRefusal as n, getWorkspaceRoot as nt, cacheDir as o, peekWorkspaceRoot as ot, compileCsvQuery as p, stagingSkillDir as pt, BackendUnavailableError as q, storeFor as r, isPresetSlug as rt, csvRowToItem as s, projectSkillsDir as st, collectionWritable as t, firestoreHandle as tt, encodeCsvRecordId as u, setFirestoreAccessor as ut, deleteItem as v, writeItem as w, listItems as x, generateItemId as y, projectItemFields as z };
|
|
3134
3134
|
|
|
3135
|
-
//# sourceMappingURL=
|
|
3135
|
+
//# sourceMappingURL=store-_61sO8K8.js.map
|