fatedb 0.0.0-stage → 0.10.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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +264 -2
  3. package/dist/bin.d.ts +2 -0
  4. package/dist/bin.js +3 -0
  5. package/dist/cli.d.ts +15 -0
  6. package/dist/cli.js +256 -0
  7. package/dist/client.d.ts +152 -0
  8. package/dist/client.js +416 -0
  9. package/dist/cloudflare.d.ts +197 -0
  10. package/dist/cloudflare.js +159 -0
  11. package/dist/datalog/ast.d.ts +35 -0
  12. package/dist/datalog/ast.js +40 -0
  13. package/dist/datalog/evaluator.d.ts +14 -0
  14. package/dist/datalog/evaluator.js +395 -0
  15. package/dist/datalog/fact-source.d.ts +30 -0
  16. package/dist/datalog/fact-source.js +1 -0
  17. package/dist/datalog/fingerprint.d.ts +12 -0
  18. package/dist/datalog/fingerprint.js +50 -0
  19. package/dist/datalog/parser.d.ts +3 -0
  20. package/dist/datalog/parser.js +302 -0
  21. package/dist/datalog/value.d.ts +34 -0
  22. package/dist/datalog/value.js +104 -0
  23. package/dist/domain/date-validation.d.ts +2 -0
  24. package/dist/domain/date-validation.js +32 -0
  25. package/dist/domain/fact-value.d.ts +91 -0
  26. package/dist/domain/fact-value.js +167 -0
  27. package/dist/domain/fact.d.ts +14 -0
  28. package/dist/domain/fact.js +47 -0
  29. package/dist/domain/failure.d.ts +44 -0
  30. package/dist/domain/failure.js +30 -0
  31. package/dist/domain/graph-id.d.ts +7 -0
  32. package/dist/domain/graph-id.js +10 -0
  33. package/dist/domain/histogram.d.ts +29 -0
  34. package/dist/domain/histogram.js +48 -0
  35. package/dist/domain/identifiers.d.ts +26 -0
  36. package/dist/domain/identifiers.js +75 -0
  37. package/dist/domain/numbers.d.ts +12 -0
  38. package/dist/domain/numbers.js +77 -0
  39. package/dist/domain/projection.d.ts +252 -0
  40. package/dist/domain/projection.js +51 -0
  41. package/dist/domain/query-metrics.d.ts +31 -0
  42. package/dist/domain/query-metrics.js +35 -0
  43. package/dist/domain/read-token.d.ts +37 -0
  44. package/dist/domain/read-token.js +93 -0
  45. package/dist/domain/result.d.ts +22 -0
  46. package/dist/domain/result.js +16 -0
  47. package/dist/domain/schema-observation.d.ts +20 -0
  48. package/dist/domain/schema-observation.js +61 -0
  49. package/dist/domain/string-order.d.ts +2 -0
  50. package/dist/domain/string-order.js +15 -0
  51. package/dist/domain/vocabulary.d.ts +101 -0
  52. package/dist/domain/vocabulary.js +276 -0
  53. package/dist/index.d.ts +15 -0
  54. package/dist/index.js +14 -0
  55. package/dist/projection-config.d.ts +3 -0
  56. package/dist/projection-config.js +212 -0
  57. package/dist/server/access.d.ts +32 -0
  58. package/dist/server/access.js +117 -0
  59. package/dist/server/admin.d.ts +233 -0
  60. package/dist/server/admin.js +483 -0
  61. package/dist/server/authorization.d.ts +35 -0
  62. package/dist/server/authorization.js +76 -0
  63. package/dist/server/console-assets.d.ts +6 -0
  64. package/dist/server/console-assets.js +2 -0
  65. package/dist/server/console.d.ts +8 -0
  66. package/dist/server/console.js +301 -0
  67. package/dist/server/index.d.ts +122 -0
  68. package/dist/server/index.js +487 -0
  69. package/dist/server/mcp.d.ts +31 -0
  70. package/dist/server/mcp.js +169 -0
  71. package/dist/server/telemetry-do.d.ts +31 -0
  72. package/dist/server/telemetry-do.js +342 -0
  73. package/dist/server/telemetry.d.ts +27 -0
  74. package/dist/server/telemetry.js +191 -0
  75. package/dist/storage/constants.d.ts +20 -0
  76. package/dist/storage/constants.js +21 -0
  77. package/dist/storage/environment.d.ts +438 -0
  78. package/dist/storage/environment.js +145 -0
  79. package/dist/storage/facts.d.ts +41 -0
  80. package/dist/storage/facts.js +73 -0
  81. package/dist/storage/http.d.ts +55 -0
  82. package/dist/storage/http.js +169 -0
  83. package/dist/storage/keys.d.ts +50 -0
  84. package/dist/storage/keys.js +84 -0
  85. package/dist/storage/layout.d.ts +67 -0
  86. package/dist/storage/layout.js +134 -0
  87. package/dist/storage/log.d.ts +41 -0
  88. package/dist/storage/log.js +137 -0
  89. package/dist/storage/manifest.d.ts +26 -0
  90. package/dist/storage/manifest.js +206 -0
  91. package/dist/storage/operations.d.ts +303 -0
  92. package/dist/storage/operations.js +719 -0
  93. package/dist/storage/partition.d.ts +125 -0
  94. package/dist/storage/partition.js +816 -0
  95. package/dist/storage/projection.d.ts +72 -0
  96. package/dist/storage/projection.js +1349 -0
  97. package/dist/storage/root.d.ts +120 -0
  98. package/dist/storage/root.js +596 -0
  99. package/dist/storage/segment.d.ts +145 -0
  100. package/dist/storage/segment.js +451 -0
  101. package/dist/storage/shard.d.ts +82 -0
  102. package/dist/storage/shard.js +710 -0
  103. package/dist/storage/source.d.ts +60 -0
  104. package/dist/storage/source.js +199 -0
  105. package/dist/telemetry/model.d.ts +123 -0
  106. package/dist/telemetry/model.js +292 -0
  107. package/dist/version.d.ts +4 -0
  108. package/dist/version.js +6 -0
  109. package/package.json +82 -4
@@ -0,0 +1,302 @@
1
+ import { parseUri } from "../domain/identifiers.js";
2
+ import { parsed, rejected } from "../domain/result.js";
3
+ import { createDatalogQuery } from "./ast.js";
4
+ import { parseDecimal } from "../domain/numbers.js";
5
+ import { decimalValue, integerValue, stringValue } from "./value.js";
6
+ const MAX_QUERY_LENGTH = 8_192;
7
+ const BUILTIN_MAP = new Map([
8
+ ["=", "="], ["<", "<"], ["<=", "<="], [">", ">"], [">=", ">="],
9
+ ["eq", "eq"], ["lt", "lt"], ["lte", "lte"], ["gt", "gt"], ["gte", "gte"],
10
+ ["before", "before"], ["after", "after"], ["contains", "contains"], ["startsWith", "startsWith"], ["endsWith", "endsWith"],
11
+ ]);
12
+ function parseBuiltinName(name) {
13
+ return BUILTIN_MAP.get(name);
14
+ }
15
+ export function parseDatalogQuery(source) {
16
+ if (source.length > MAX_QUERY_LENGTH) {
17
+ return rejected("query", `query text cannot exceed ${MAX_QUERY_LENGTH} characters`);
18
+ }
19
+ const tokens = tokenize(source);
20
+ if (!tokens.ok)
21
+ return tokens;
22
+ if (tokens.value.length === 0)
23
+ return rejected("query", "expected a query");
24
+ const parser = new Parser(source, tokens.value);
25
+ return parser.parse();
26
+ }
27
+ function tokenize(source) {
28
+ const tokens = [];
29
+ let offset = 0;
30
+ while (offset < source.length) {
31
+ const character = source[offset];
32
+ if (character === undefined)
33
+ break;
34
+ if (/\s/.test(character)) {
35
+ offset += 1;
36
+ continue;
37
+ }
38
+ if (character === '"') {
39
+ const string = readJsonString(source, offset);
40
+ if (!string.ok)
41
+ return string;
42
+ tokens.push(string.value.token);
43
+ offset = string.value.nextOffset;
44
+ continue;
45
+ }
46
+ if (character === "'") {
47
+ const quoted = readQuotedIdentifier(source, offset);
48
+ if (!quoted.ok)
49
+ return quoted;
50
+ tokens.push(quoted.value.token);
51
+ offset = quoted.value.nextOffset;
52
+ continue;
53
+ }
54
+ if (isIdentifierStart(character) || character === "_") {
55
+ const start = offset;
56
+ offset += 1;
57
+ while (offset < source.length && isIdentifierContinuation(source[offset] ?? "")) {
58
+ offset += 1;
59
+ }
60
+ tokens.push({ kind: "identifier", value: source.slice(start, offset), offset: start });
61
+ continue;
62
+ }
63
+ if (isDigit(character) || (character === "-" && isDigit(source[offset + 1] ?? ""))) {
64
+ // An integer of any size, or a decimal: digits with a fraction (1.5) or an exponent (15e-1).
65
+ const start = offset;
66
+ offset += character === "-" ? 1 : 0;
67
+ while (isDigit(source[offset] ?? ""))
68
+ offset += 1;
69
+ let decimal = false;
70
+ if (source[offset] === "." && isDigit(source[offset + 1] ?? "")) {
71
+ decimal = true;
72
+ offset += 1;
73
+ while (isDigit(source[offset] ?? ""))
74
+ offset += 1;
75
+ }
76
+ const sign = source[offset + 1] === "+" || source[offset + 1] === "-" ? 1 : 0;
77
+ if ((source[offset] === "e" || source[offset] === "E") && isDigit(source[offset + 1 + sign] ?? "")) {
78
+ decimal = true;
79
+ offset += 1 + sign;
80
+ while (isDigit(source[offset] ?? ""))
81
+ offset += 1;
82
+ }
83
+ const text = source.slice(start, offset);
84
+ if (!decimal) {
85
+ tokens.push({ kind: "integer", value: BigInt(text).toString(), offset: start });
86
+ continue;
87
+ }
88
+ const value = parseDecimal(text.replace(/^(-?)0+(?=[0-9])/, "$1"), "query");
89
+ if (!value.ok)
90
+ return rejected("query", value.error.message, start);
91
+ tokens.push({ kind: "decimal", value: value.value, offset: start });
92
+ continue;
93
+ }
94
+ if (character === ">" || character === "<") {
95
+ const next = source[offset + 1];
96
+ const value = character === ">"
97
+ ? (next === "=" ? ">=" : ">")
98
+ : (next === "=" ? "<=" : "<");
99
+ tokens.push({ kind: "operator", value, offset });
100
+ offset += value.length;
101
+ continue;
102
+ }
103
+ if (character === "=") {
104
+ tokens.push({ kind: "operator", value: "=", offset });
105
+ offset += 1;
106
+ continue;
107
+ }
108
+ if (character === "(" || character === ")" || character === "," || character === "!") {
109
+ tokens.push({ kind: "punctuation", value: character, offset });
110
+ offset += 1;
111
+ continue;
112
+ }
113
+ return rejected("query", `unexpected character ${JSON.stringify(character)}`, offset);
114
+ }
115
+ return parsed(Object.freeze(tokens));
116
+ }
117
+ function readJsonString(source, start) {
118
+ let escaped = false;
119
+ for (let offset = start + 1; offset < source.length; offset += 1) {
120
+ const character = source[offset];
121
+ if (character === undefined)
122
+ break;
123
+ if (escaped) {
124
+ escaped = false;
125
+ }
126
+ else if (character === "\\") {
127
+ escaped = true;
128
+ }
129
+ else if (character === '"') {
130
+ const raw = source.slice(start, offset + 1);
131
+ try {
132
+ const value = JSON.parse(raw);
133
+ if (typeof value !== "string")
134
+ return rejected("query", "expected a string literal", start);
135
+ return parsed({ token: { kind: "string", value, offset: start }, nextOffset: offset + 1 });
136
+ }
137
+ catch {
138
+ return rejected("query", "invalid JSON string literal", start);
139
+ }
140
+ }
141
+ }
142
+ return rejected("query", "unterminated string literal", start);
143
+ }
144
+ function readQuotedIdentifier(source, start) {
145
+ const end = source.indexOf("'", start + 1);
146
+ if (end < 0)
147
+ return rejected("query", "unterminated quoted identifier", start);
148
+ const value = source.slice(start + 1, end);
149
+ if (value.length === 0)
150
+ return rejected("query", "quoted identifier cannot be empty", start);
151
+ return parsed({ token: { kind: "quoted_identifier", value, offset: start }, nextOffset: end + 1 });
152
+ }
153
+ function isIdentifierStart(character) {
154
+ return /^[A-Za-z]$/.test(character);
155
+ }
156
+ function isIdentifierContinuation(character) {
157
+ return /^[A-Za-z0-9:_?+.-]$/.test(character);
158
+ }
159
+ function isDigit(character) {
160
+ return /^[0-9]$/.test(character);
161
+ }
162
+ class Parser {
163
+ source;
164
+ tokens;
165
+ cursor = 0;
166
+ constructor(source, tokens) {
167
+ this.source = source;
168
+ this.tokens = tokens;
169
+ }
170
+ parse() {
171
+ const facts = [];
172
+ const constraints = [];
173
+ while (this.cursor < this.tokens.length) {
174
+ const clause = this.parseClause();
175
+ if (!clause.ok)
176
+ return clause;
177
+ if (clause.value.kind === "fact")
178
+ facts.push(clause.value);
179
+ else
180
+ constraints.push(clause.value);
181
+ if (this.cursor === this.tokens.length)
182
+ break;
183
+ const comma = this.consumePunctuation(",");
184
+ if (!comma) {
185
+ const token = this.peek();
186
+ return this.errorAt(token, "expected a comma between clauses");
187
+ }
188
+ if (this.cursor === this.tokens.length) {
189
+ return rejected("query", "expected a clause after the comma", this.source.length);
190
+ }
191
+ }
192
+ return createDatalogQuery(facts, constraints);
193
+ }
194
+ parseClause() {
195
+ if (this.peek()?.kind === "punctuation" && this.peek()?.value === "!") {
196
+ return this.errorAt(this.take(), "negated relations are outside the initial Datalog subset");
197
+ }
198
+ if (this.isCallStart())
199
+ return this.parseCallClause();
200
+ return this.parseComparison();
201
+ }
202
+ parseCallClause() {
203
+ const predicate = this.take();
204
+ if (predicate === undefined || (predicate.kind !== "identifier" && predicate.kind !== "quoted_identifier")) {
205
+ return this.errorAt(predicate, "expected a relation name");
206
+ }
207
+ if (!this.consumePunctuation("("))
208
+ return this.errorAt(this.peek(), "expected `(` after relation name");
209
+ const left = this.parseTerm();
210
+ if (!left.ok)
211
+ return left;
212
+ if (!this.consumePunctuation(","))
213
+ return this.errorAt(this.peek(), "expected `,` between relation terms");
214
+ const right = this.parseTerm();
215
+ if (!right.ok)
216
+ return right;
217
+ if (!this.consumePunctuation(")")) {
218
+ return this.errorAt(this.peek(), "relations in this subset must have exactly two terms");
219
+ }
220
+ const builtinName = parseBuiltinName(predicate.value);
221
+ if (builtinName !== undefined) {
222
+ return parsed(Object.freeze({
223
+ kind: "builtin",
224
+ name: builtinName,
225
+ left: left.value,
226
+ right: right.value,
227
+ }));
228
+ }
229
+ const parsedPredicate = parseUri(predicate.value, "query.predicate");
230
+ if (!parsedPredicate.ok) {
231
+ return rejected("query", parsedPredicate.error.message, predicate.offset);
232
+ }
233
+ return parsed(Object.freeze({
234
+ kind: "fact",
235
+ predicate: parsedPredicate.value,
236
+ entity: left.value,
237
+ value: right.value,
238
+ }));
239
+ }
240
+ parseComparison() {
241
+ const left = this.parseTerm();
242
+ if (!left.ok)
243
+ return left;
244
+ const operator = this.take();
245
+ if (operator?.kind !== "operator")
246
+ return this.errorAt(operator, "expected a supported comparison operator");
247
+ const right = this.parseTerm();
248
+ if (!right.ok)
249
+ return right;
250
+ return parsed(Object.freeze({ kind: "builtin", name: operator.value, left: left.value, right: right.value }));
251
+ }
252
+ parseTerm() {
253
+ const token = this.take();
254
+ if (token === undefined)
255
+ return this.errorAt(undefined, "expected a term");
256
+ if (token.kind === "integer") {
257
+ return parsed(Object.freeze({ kind: "constant", value: integerValue(token.value) }));
258
+ }
259
+ if (token.kind === "decimal") {
260
+ return parsed(Object.freeze({ kind: "constant", value: decimalValue(token.value) }));
261
+ }
262
+ if (token.kind === "string" || token.kind === "quoted_identifier") {
263
+ return parsed(Object.freeze({ kind: "constant", value: stringValue(token.value) }));
264
+ }
265
+ if (token.kind === "identifier") {
266
+ if (token.value === "_")
267
+ return parsed(Object.freeze({ kind: "wildcard" }));
268
+ const firstCharacter = token.value[0] ?? "";
269
+ if (/^[A-Z]$/.test(firstCharacter)) {
270
+ return parsed(Object.freeze({ kind: "variable", name: token.value }));
271
+ }
272
+ return parsed(Object.freeze({ kind: "constant", value: stringValue(token.value) }));
273
+ }
274
+ return this.errorAt(token, "expected a variable, constant, or wildcard");
275
+ }
276
+ isCallStart() {
277
+ const current = this.peek();
278
+ const next = this.tokens[this.cursor + 1];
279
+ return (current?.kind === "identifier" || current?.kind === "quoted_identifier") &&
280
+ next?.kind === "punctuation" && next.value === "(";
281
+ }
282
+ consumePunctuation(value) {
283
+ const token = this.peek();
284
+ if (token?.kind !== "punctuation" || token.value !== value)
285
+ return false;
286
+ this.cursor += 1;
287
+ return true;
288
+ }
289
+ errorAt(token, message) {
290
+ const offset = token?.offset ?? this.source.length;
291
+ return rejected("query", message, offset);
292
+ }
293
+ peek() {
294
+ return this.tokens[this.cursor];
295
+ }
296
+ take() {
297
+ const token = this.tokens[this.cursor];
298
+ if (token !== undefined)
299
+ this.cursor += 1;
300
+ return token;
301
+ }
302
+ }
@@ -0,0 +1,34 @@
1
+ import type { FactValue } from "../domain/fact-value.js";
2
+ export type DatalogValue = Readonly<{
3
+ type: "string";
4
+ value: string;
5
+ }> | Readonly<{
6
+ type: "integer";
7
+ value: string;
8
+ }> | Readonly<{
9
+ type: "decimal";
10
+ value: string;
11
+ }>;
12
+ /** A value a fact gave a variable: what it matches by, and the fact value it is. */
13
+ export type BoundValue = DatalogValue & Readonly<{
14
+ fact: FactValue;
15
+ }>;
16
+ export declare function stringValue(value: string): DatalogValue;
17
+ /** An integer, from its canonical digits. */
18
+ export declare function integerValue(digits: string): DatalogValue;
19
+ /** A decimal, from its canonical form. */
20
+ export declare function decimalValue(canonical: string): DatalogValue;
21
+ /** An entity or a source: a reference, matched by its text. */
22
+ export declare function referenceValue(id: string): BoundValue;
23
+ /** What a fact value matches: its items, for a list; else itself. */
24
+ export declare function factValueToDatalogValues(value: FactValue): readonly BoundValue[];
25
+ /** A fact value, with what it matches by. Every tuple a scan reads makes some, so it allocates one object. */
26
+ export declare function boundValue(fact: FactValue): BoundValue;
27
+ /** The item of a fact value that matched `match`: a list's first such item, or the value itself. */
28
+ export declare function matchedItem(value: FactValue, match: DatalogValue): FactValue;
29
+ /** Whether two values match: the same kind, and the same text or number. An integer never matches a decimal. */
30
+ export declare function sameDatalogValue(left: DatalogValue, right: DatalogValue): boolean;
31
+ /** Whether `=` holds: numbers by value, an integer and a decimal too; anything else as it matches. */
32
+ export declare function equalDatalogValues(left: DatalogValue, right: DatalogValue): boolean;
33
+ /** Orders numbers by value, integers and decimals together, and strings by Unicode scalar; nothing else. */
34
+ export declare function compareDatalogValues(left: DatalogValue, right: DatalogValue): number | undefined;
@@ -0,0 +1,104 @@
1
+ // Values as a query sees them (RFD0014). A query matches and compares a fact value by its text, or
2
+ // by its number: text, references, dates, instants, booleans and null are strings, as are bytes,
3
+ // maps and lists nested in lists, as JSON; numbers are integers, and decimals decimals. So a
4
+ // reference matches text with the same characters, and the literal true matches the boolean true.
5
+ //
6
+ // A value a fact gives a variable also keeps that fact value, typed, which is what answers return.
7
+ // A variable that several atoms match keeps the value of the first atom that binds it, in the order
8
+ // the query states them.
9
+ import { compareNumbers } from "../domain/numbers.js";
10
+ import { compareUnicodeScalars } from "../domain/string-order.js";
11
+ export function stringValue(value) {
12
+ return Object.freeze({ type: "string", value });
13
+ }
14
+ /** An integer, from its canonical digits. */
15
+ export function integerValue(digits) {
16
+ return Object.freeze({ type: "integer", value: digits });
17
+ }
18
+ /** A decimal, from its canonical form. */
19
+ export function decimalValue(canonical) {
20
+ return Object.freeze({ type: "decimal", value: canonical });
21
+ }
22
+ /** An entity or a source: a reference, matched by its text. */
23
+ export function referenceValue(id) {
24
+ return Object.freeze({ type: "string", value: id, fact: Object.freeze({ type: "reference", value: id }) });
25
+ }
26
+ /** What a fact value matches: its items, for a list; else itself. */
27
+ export function factValueToDatalogValues(value) {
28
+ if (value.type === "list") {
29
+ return Object.freeze(value.value.map(boundValue));
30
+ }
31
+ return Object.freeze([boundValue(value)]);
32
+ }
33
+ /** A fact value, with what it matches by. Every tuple a scan reads makes some, so it allocates one object. */
34
+ export function boundValue(fact) {
35
+ const value = matchText(fact);
36
+ if (fact.type === "number")
37
+ return Object.freeze({ type: "integer", value, fact });
38
+ if (fact.type === "decimal")
39
+ return Object.freeze({ type: "decimal", value, fact });
40
+ return Object.freeze({ type: "string", value, fact });
41
+ }
42
+ function matchOf(fact) {
43
+ const value = matchText(fact);
44
+ if (fact.type === "number")
45
+ return Object.freeze({ type: "integer", value });
46
+ if (fact.type === "decimal")
47
+ return Object.freeze({ type: "decimal", value });
48
+ return Object.freeze({ type: "string", value });
49
+ }
50
+ function matchType(value) {
51
+ return value.type === "number" ? "integer" : value.type === "decimal" ? "decimal" : "string";
52
+ }
53
+ function matchText(value) {
54
+ switch (value.type) {
55
+ case "null":
56
+ return "null";
57
+ case "text":
58
+ case "reference":
59
+ case "date":
60
+ case "date_time":
61
+ case "number":
62
+ case "decimal":
63
+ return value.value;
64
+ case "boolean":
65
+ return String(value.value);
66
+ case "bytes":
67
+ return JSON.stringify(value.value);
68
+ case "list":
69
+ return JSON.stringify(value.value.map(toSerdeValue));
70
+ case "map":
71
+ return JSON.stringify(Object.fromEntries(Object.entries(value.value).map(([key, item]) => [key, toSerdeValue(item)])));
72
+ }
73
+ }
74
+ /** The item of a fact value that matched `match`: a list's first such item, or the value itself. */
75
+ export function matchedItem(value, match) {
76
+ if (value.type !== "list")
77
+ return value;
78
+ return value.value.find((item) => sameDatalogValue(matchOf(item), match)) ?? value;
79
+ }
80
+ /** Whether two values match: the same kind, and the same text or number. An integer never matches a decimal. */
81
+ export function sameDatalogValue(left, right) {
82
+ return left.type === right.type && left.value === right.value;
83
+ }
84
+ /** Whether `=` holds: numbers by value, an integer and a decimal too; anything else as it matches. */
85
+ export function equalDatalogValues(left, right) {
86
+ return isNumber(left) && isNumber(right) ? compareNumbers(left.value, right.value) === 0 : sameDatalogValue(left, right);
87
+ }
88
+ /** Orders numbers by value, integers and decimals together, and strings by Unicode scalar; nothing else. */
89
+ export function compareDatalogValues(left, right) {
90
+ if (isNumber(left) && isNumber(right))
91
+ return compareNumbers(left.value, right.value);
92
+ if (left.type === "string" && right.type === "string")
93
+ return compareUnicodeScalars(left.value, right.value);
94
+ return undefined;
95
+ }
96
+ const isNumber = (value) => value.type === "integer" || value.type === "decimal";
97
+ function toSerdeValue(value) {
98
+ switch (value.type) {
99
+ case "null":
100
+ return { type: "null" };
101
+ default:
102
+ return { type: value.type, value: value.value };
103
+ }
104
+ }
@@ -0,0 +1,2 @@
1
+ export declare function isCalendarDate(value: string): boolean;
2
+ export declare function isRfc3339Timestamp(value: string): boolean;
@@ -0,0 +1,32 @@
1
+ export function isCalendarDate(value) {
2
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
3
+ if (!match)
4
+ return false;
5
+ return calendarDatePartsAreValid(Number(match[1]), Number(match[2]), Number(match[3]));
6
+ }
7
+ export function isRfc3339Timestamp(value) {
8
+ const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|([+-])(\d{2}):(\d{2}))$/.exec(value);
9
+ if (!match)
10
+ return false;
11
+ const [year, month, day, hour, minute, second] = match.slice(1, 7).map(Number);
12
+ if (year === undefined || month === undefined || day === undefined ||
13
+ hour === undefined || minute === undefined || second === undefined ||
14
+ !calendarDatePartsAreValid(year, month, day) ||
15
+ hour > 23 || minute > 59 || second > 59) {
16
+ return false;
17
+ }
18
+ if (match[8] !== undefined) {
19
+ const offsetHour = Number(match[9]);
20
+ const offsetMinute = Number(match[10]);
21
+ if (offsetHour > 23 || offsetMinute > 59)
22
+ return false;
23
+ }
24
+ return Number.isFinite(Date.parse(value));
25
+ }
26
+ function calendarDatePartsAreValid(year, month, day) {
27
+ if (month < 1 || month > 12 || day < 1)
28
+ return false;
29
+ const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
30
+ const daysInMonth = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
31
+ return daysInMonth !== undefined && day <= daysInMonth;
32
+ }
@@ -0,0 +1,91 @@
1
+ import { type ParseResult } from "./result.js";
2
+ export type FactValue = Readonly<{
3
+ type: "null";
4
+ }> | Readonly<{
5
+ type: "text";
6
+ value: string;
7
+ }>
8
+ /** An integer of any size, as its digits. */
9
+ | Readonly<{
10
+ type: "number";
11
+ value: string;
12
+ }>
13
+ /** A decimal of any precision, written out: "-12.5", never "-12.50" or "-125e-1". */
14
+ | Readonly<{
15
+ type: "decimal";
16
+ value: string;
17
+ }> | Readonly<{
18
+ type: "boolean";
19
+ value: boolean;
20
+ }> | Readonly<{
21
+ type: "bytes";
22
+ value: readonly number[];
23
+ }> | Readonly<{
24
+ type: "reference";
25
+ value: string;
26
+ }>
27
+ /** A calendar date, YYYY-MM-DD. */
28
+ | Readonly<{
29
+ type: "date";
30
+ value: string;
31
+ }>
32
+ /** An instant, in UTC to the millisecond: YYYY-MM-DDTHH:MM:SS.sssZ. */
33
+ | Readonly<{
34
+ type: "date_time";
35
+ value: string;
36
+ }> | Readonly<{
37
+ type: "list";
38
+ value: readonly FactValue[];
39
+ }> | Readonly<{
40
+ type: "map";
41
+ value: Readonly<Record<string, FactValue>>;
42
+ }>;
43
+ /** A value as a write may give it: a fact value, or one whose payload is the JavaScript value `toValue` returns. */
44
+ export type FactValueInput = Readonly<{
45
+ type: "null";
46
+ value?: null;
47
+ }> | Readonly<{
48
+ type: "text";
49
+ value: string;
50
+ }> | Readonly<{
51
+ type: "number";
52
+ value: bigint | number | string;
53
+ }> | Readonly<{
54
+ type: "decimal";
55
+ value: number | string;
56
+ }> | Readonly<{
57
+ type: "boolean";
58
+ value: boolean;
59
+ }> | Readonly<{
60
+ type: "bytes";
61
+ value: Uint8Array | readonly number[];
62
+ }> | Readonly<{
63
+ type: "reference";
64
+ value: string;
65
+ }> | Readonly<{
66
+ type: "date";
67
+ value: Date | string;
68
+ }> | Readonly<{
69
+ type: "date_time";
70
+ value: Date | string;
71
+ }> | Readonly<{
72
+ type: "list";
73
+ value: readonly FactValueInput[];
74
+ }> | Readonly<{
75
+ type: "map";
76
+ value: Readonly<Record<string, FactValueInput>>;
77
+ }>;
78
+ /**
79
+ * A fact value as JavaScript holds it, which the typed API returns: text, references and decimals
80
+ * are strings (JavaScript has no decimal that keeps every digit), numbers bigints, booleans
81
+ * booleans, dates and instants Dates (a date is its midnight in UTC), bytes Uint8Arrays, lists
82
+ * arrays and maps objects.
83
+ */
84
+ export type Value = null | string | bigint | boolean | Date | Uint8Array | ValueList | ValueMap;
85
+ export interface ValueList extends ReadonlyArray<Value> {
86
+ }
87
+ export interface ValueMap {
88
+ readonly [key: string]: Value;
89
+ }
90
+ export declare function toValue(value: FactValue): Value;
91
+ export declare function parseFactValue(input: unknown, path?: string): ParseResult<FactValue>;