@4fu/arbor-journal-sqlite 0.0.0-stage → 2026.1106.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/stored.js ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Reading refs back out of storage.
3
+ *
4
+ * The journal stores refs as text. A ref that does not match the vocabulary in
5
+ * docs/journal.md#refs means the database was written by something else, so
6
+ * reading it fails in the core's error shape instead of handing a caller a
7
+ * string that is not a ref.
8
+ */
9
+ import { arborError } from "@4fu/arbor-errors";
10
+ const PATTERNS = [
11
+ /^r\d+$/, // an event
12
+ /^c\d+$/, // a window
13
+ /^n\d+(@\d+)?$/, // a note, or one of its revisions
14
+ /^t\d+$/, // a task
15
+ /^m\d+$/, // a message between windows
16
+ /^ws\d+$/, // a workspace
17
+ /^e\d+$/, // an environment revision
18
+ /^(sk|mcp|svc):[^\s@]+@\d+$/, // a skill, an MCP server or a service
19
+ /^b\d+$/, // media stored beside the journal
20
+ /^[^\s:]+:[^\s]+$/, // an outside message, by its native id
21
+ ];
22
+ function malformed(kind, value) {
23
+ throw arborError({
24
+ code: "internal",
25
+ rule: "journal.storage.ref",
26
+ message: `journal: stored ${kind} ${value} is not a ref`,
27
+ nextAction: "the journal database was written by another build; rebuild it",
28
+ details: { kind, value },
29
+ });
30
+ }
31
+ /** Any ref. */
32
+ export function storedRef(value) {
33
+ for (const pattern of PATTERNS) {
34
+ if (pattern.test(value))
35
+ return value;
36
+ }
37
+ return malformed("ref", value);
38
+ }
39
+ export function storedWindowRef(value) {
40
+ if (value === null)
41
+ return null;
42
+ if (!/^c\d+$/.test(value))
43
+ return malformed("window ref", value);
44
+ return value;
45
+ }
46
+ export function storedBlobRef(value) {
47
+ if (!/^b\d+$/.test(value))
48
+ return malformed("blob ref", value);
49
+ return value;
50
+ }
51
+ export function storedExternalRef(value) {
52
+ if (value === null)
53
+ return null;
54
+ if (!/^[^\s:]+:[^\s]+$/.test(value))
55
+ return malformed("external ref", value);
56
+ return value;
57
+ }