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.
- package/LICENSE +21 -0
- package/README.md +264 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +3 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.js +256 -0
- package/dist/client.d.ts +152 -0
- package/dist/client.js +416 -0
- package/dist/cloudflare.d.ts +197 -0
- package/dist/cloudflare.js +159 -0
- package/dist/datalog/ast.d.ts +35 -0
- package/dist/datalog/ast.js +40 -0
- package/dist/datalog/evaluator.d.ts +14 -0
- package/dist/datalog/evaluator.js +395 -0
- package/dist/datalog/fact-source.d.ts +30 -0
- package/dist/datalog/fact-source.js +1 -0
- package/dist/datalog/fingerprint.d.ts +12 -0
- package/dist/datalog/fingerprint.js +50 -0
- package/dist/datalog/parser.d.ts +3 -0
- package/dist/datalog/parser.js +302 -0
- package/dist/datalog/value.d.ts +34 -0
- package/dist/datalog/value.js +104 -0
- package/dist/domain/date-validation.d.ts +2 -0
- package/dist/domain/date-validation.js +32 -0
- package/dist/domain/fact-value.d.ts +91 -0
- package/dist/domain/fact-value.js +167 -0
- package/dist/domain/fact.d.ts +14 -0
- package/dist/domain/fact.js +47 -0
- package/dist/domain/failure.d.ts +44 -0
- package/dist/domain/failure.js +30 -0
- package/dist/domain/graph-id.d.ts +7 -0
- package/dist/domain/graph-id.js +10 -0
- package/dist/domain/histogram.d.ts +29 -0
- package/dist/domain/histogram.js +48 -0
- package/dist/domain/identifiers.d.ts +26 -0
- package/dist/domain/identifiers.js +75 -0
- package/dist/domain/numbers.d.ts +12 -0
- package/dist/domain/numbers.js +77 -0
- package/dist/domain/projection.d.ts +252 -0
- package/dist/domain/projection.js +51 -0
- package/dist/domain/query-metrics.d.ts +31 -0
- package/dist/domain/query-metrics.js +35 -0
- package/dist/domain/read-token.d.ts +37 -0
- package/dist/domain/read-token.js +93 -0
- package/dist/domain/result.d.ts +22 -0
- package/dist/domain/result.js +16 -0
- package/dist/domain/schema-observation.d.ts +20 -0
- package/dist/domain/schema-observation.js +61 -0
- package/dist/domain/string-order.d.ts +2 -0
- package/dist/domain/string-order.js +15 -0
- package/dist/domain/vocabulary.d.ts +101 -0
- package/dist/domain/vocabulary.js +276 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +14 -0
- package/dist/projection-config.d.ts +3 -0
- package/dist/projection-config.js +212 -0
- package/dist/server/access.d.ts +32 -0
- package/dist/server/access.js +117 -0
- package/dist/server/admin.d.ts +233 -0
- package/dist/server/admin.js +483 -0
- package/dist/server/authorization.d.ts +35 -0
- package/dist/server/authorization.js +76 -0
- package/dist/server/console-assets.d.ts +6 -0
- package/dist/server/console-assets.js +2 -0
- package/dist/server/console.d.ts +8 -0
- package/dist/server/console.js +301 -0
- package/dist/server/index.d.ts +122 -0
- package/dist/server/index.js +487 -0
- package/dist/server/mcp.d.ts +31 -0
- package/dist/server/mcp.js +169 -0
- package/dist/server/telemetry-do.d.ts +31 -0
- package/dist/server/telemetry-do.js +342 -0
- package/dist/server/telemetry.d.ts +27 -0
- package/dist/server/telemetry.js +191 -0
- package/dist/storage/constants.d.ts +20 -0
- package/dist/storage/constants.js +21 -0
- package/dist/storage/environment.d.ts +438 -0
- package/dist/storage/environment.js +145 -0
- package/dist/storage/facts.d.ts +41 -0
- package/dist/storage/facts.js +73 -0
- package/dist/storage/http.d.ts +55 -0
- package/dist/storage/http.js +169 -0
- package/dist/storage/keys.d.ts +50 -0
- package/dist/storage/keys.js +84 -0
- package/dist/storage/layout.d.ts +67 -0
- package/dist/storage/layout.js +134 -0
- package/dist/storage/log.d.ts +41 -0
- package/dist/storage/log.js +137 -0
- package/dist/storage/manifest.d.ts +26 -0
- package/dist/storage/manifest.js +206 -0
- package/dist/storage/operations.d.ts +303 -0
- package/dist/storage/operations.js +719 -0
- package/dist/storage/partition.d.ts +125 -0
- package/dist/storage/partition.js +816 -0
- package/dist/storage/projection.d.ts +72 -0
- package/dist/storage/projection.js +1349 -0
- package/dist/storage/root.d.ts +120 -0
- package/dist/storage/root.js +596 -0
- package/dist/storage/segment.d.ts +145 -0
- package/dist/storage/segment.js +451 -0
- package/dist/storage/shard.d.ts +82 -0
- package/dist/storage/shard.js +710 -0
- package/dist/storage/source.d.ts +60 -0
- package/dist/storage/source.js +199 -0
- package/dist/telemetry/model.d.ts +123 -0
- package/dist/telemetry/model.js +292 -0
- package/dist/version.d.ts +4 -0
- package/dist/version.js +6 -0
- package/package.json +82 -4
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the Cloudflare Worker configuration for a FateDB instance inside `cloudflare.config.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Runs in Node when the `cf` CLI loads the config: nothing here may runtime-import
|
|
5
|
+
* `cloudflare:workers`. The returned Worker config is plain data, typed so both `cf`'s
|
|
6
|
+
* `defineConfig` and the Vite plugin's `auxiliaryWorkers` accept it.
|
|
7
|
+
*/
|
|
8
|
+
import type { WorkerConfig } from "cf/config";
|
|
9
|
+
import type { ProjectionDefinitions } from "./domain/projection.js";
|
|
10
|
+
import { type GraphGrants } from "./server/authorization.js";
|
|
11
|
+
export type { GraphGrants, GraphPermission } from "./server/authorization.js";
|
|
12
|
+
export type { ProjectionBatchOptions, ProjectionConfig, ProjectionDefinitions, ProjectionR2SinkConfig, ProjectionRetryOptions, ProjectionSelector, ProjectionSinkConfig, ProjectionWorkerSinkConfig, } from "./domain/projection.js";
|
|
13
|
+
/** An entry module: the module a `with { type: "cf-worker" }` import gives, or a path. */
|
|
14
|
+
type Entrypoint = NonNullable<WorkerConfig["entrypoint"]>;
|
|
15
|
+
export type FateDBOptions<E extends Entrypoint = Entrypoint> = Readonly<{
|
|
16
|
+
/**
|
|
17
|
+
* The Worker's name, which apps bind to. The Worker owns the Durable Objects that hold the
|
|
18
|
+
* graphs: a Worker of another name starts empty, so never change it once there is data.
|
|
19
|
+
*/
|
|
20
|
+
name: string;
|
|
21
|
+
/** The instance's entry module: `import * as entrypoint from "./index.ts" with { type: "cf-worker" }`. */
|
|
22
|
+
entrypoint: E;
|
|
23
|
+
/**
|
|
24
|
+
* Which apps may use which graphs. An app binds the instance with `props: { clientId }`; "*"
|
|
25
|
+
* covers every graph the client has no entry of its own for. Without grants, every call is denied.
|
|
26
|
+
*/
|
|
27
|
+
grants?: GraphGrants;
|
|
28
|
+
/** The R2 bucket of the graphs' index, `<name>-index` by default. Never change it once there is data. */
|
|
29
|
+
bucket?: string;
|
|
30
|
+
/** The Workers compatibility date. The default is the one this version of FateDB is tested with. */
|
|
31
|
+
compatibilityDate?: string;
|
|
32
|
+
/** Where new Durable Objects are placed, such as "weur" or "enam". */
|
|
33
|
+
locationHint?: string;
|
|
34
|
+
observability?: WorkerConfig["observability"];
|
|
35
|
+
/**
|
|
36
|
+
* The console, and /mcp for agents outside the account, on a hostname of a zone in the account.
|
|
37
|
+
* Put a Cloudflare Access application on the hostname first: the instance checks every request
|
|
38
|
+
* carries its token, and refuses all of them without one. Callers act as `clientId` ("console"
|
|
39
|
+
* by default), so grant it the graphs it may see. Without a console the instance has no URL.
|
|
40
|
+
*/
|
|
41
|
+
console?: ConsoleOptions;
|
|
42
|
+
/**
|
|
43
|
+
* Static projections (RFD0016): watch a graph's published watermark, select changed entities by
|
|
44
|
+
* kind, field, or Datalog filter, and deliver batches to a bound Worker RPC sink or an R2 bucket.
|
|
45
|
+
*/
|
|
46
|
+
projections?: ProjectionDefinitions;
|
|
47
|
+
/**
|
|
48
|
+
* Origin URL used for Cloudflare Cache API keys of immutable R2 segment ranges (reducing R2 Class B reads).
|
|
49
|
+
* Defaults to `https://<console.hostname>` when `console` is configured.
|
|
50
|
+
*/
|
|
51
|
+
cacheOrigin?: string;
|
|
52
|
+
/** Interval between active index shard flushes in milliseconds (100–300000). Default 2000. */
|
|
53
|
+
flushMs?: number;
|
|
54
|
+
/** Partitions a new graph starts with before auto-growth (1–512). Default 1. */
|
|
55
|
+
initialPartitions?: number;
|
|
56
|
+
/** Optional Workers Analytics Engine dataset name for zero-DO-cost raw event telemetry. */
|
|
57
|
+
analytics?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Telemetry routing mode:
|
|
60
|
+
* - `"analytics"`: write only to Workers Analytics Engine (skips global TelemetryDO writes; requires `analytics`).
|
|
61
|
+
* - `"both"`: write to both Analytics Engine (when configured) and TelemetryDO (powers Console live charts).
|
|
62
|
+
* - `"do"`: write to TelemetryDO (default when `analytics` is not set, or when `console` is configured).
|
|
63
|
+
* Defaults to `"analytics"` when `analytics` is set without `console`, otherwise `"both"`/`"do"`.
|
|
64
|
+
*/
|
|
65
|
+
telemetry?: "do" | "analytics" | "both";
|
|
66
|
+
}>;
|
|
67
|
+
export type ConsoleOptions = Readonly<{
|
|
68
|
+
hostname: string;
|
|
69
|
+
/** The Access application on the hostname: the team's domain (`<team>.cloudflareaccess.com`) and the application's AUD tag. */
|
|
70
|
+
access: Readonly<{
|
|
71
|
+
teamDomain: string;
|
|
72
|
+
aud: string;
|
|
73
|
+
}>;
|
|
74
|
+
clientId?: string;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* A FateDB instance inside an app's own `cf dev` session, for the Vite plugin:
|
|
78
|
+
*
|
|
79
|
+
* cloudflare({ auxiliaryWorkers: [devInstance({ name: "my-db", grants: { "my-app": { "*": ["read", "write"] } } })] })
|
|
80
|
+
*
|
|
81
|
+
* The app's binding then reaches it with its props; a call between two dev sessions arrives
|
|
82
|
+
* without them, and is denied. It never ships with the app: deploy the instance from its project.
|
|
83
|
+
*/
|
|
84
|
+
export declare function devInstance(options: Omit<FateDBOptions, "entrypoint">): {
|
|
85
|
+
devOnly: true;
|
|
86
|
+
config: InstanceWorker<string>;
|
|
87
|
+
};
|
|
88
|
+
export { COMPATIBILITY_DATE } from "./version.js";
|
|
89
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
90
|
+
[key: string]: JsonValue;
|
|
91
|
+
};
|
|
92
|
+
type DurableObjectNamespaceBinding = {
|
|
93
|
+
type: "durable-object";
|
|
94
|
+
worker: string;
|
|
95
|
+
exportName: string;
|
|
96
|
+
};
|
|
97
|
+
type SqliteClass = {
|
|
98
|
+
type: "durable-object";
|
|
99
|
+
storage: "sqlite";
|
|
100
|
+
};
|
|
101
|
+
type WorkerServiceBinding = {
|
|
102
|
+
type: "worker";
|
|
103
|
+
worker: string;
|
|
104
|
+
exportName?: string;
|
|
105
|
+
};
|
|
106
|
+
type R2BucketBinding = {
|
|
107
|
+
type: "r2";
|
|
108
|
+
name: string;
|
|
109
|
+
};
|
|
110
|
+
type AnalyticsEngineDatasetBinding = {
|
|
111
|
+
type: "analytics-engine-dataset";
|
|
112
|
+
name?: string;
|
|
113
|
+
};
|
|
114
|
+
type InstanceBinding = DurableObjectNamespaceBinding | R2BucketBinding | WorkerServiceBinding | AnalyticsEngineDatasetBinding | {
|
|
115
|
+
type: "worker-loader";
|
|
116
|
+
} | {
|
|
117
|
+
type: "text";
|
|
118
|
+
value: string;
|
|
119
|
+
} | {
|
|
120
|
+
type: "json";
|
|
121
|
+
value: JsonValue;
|
|
122
|
+
};
|
|
123
|
+
/** An instance's Worker, as `cf` and the Vite plugin take it (the plugin takes a path as the entrypoint). */
|
|
124
|
+
export type InstanceWorker<E extends Entrypoint = Entrypoint> = {
|
|
125
|
+
name: string;
|
|
126
|
+
entrypoint: E;
|
|
127
|
+
compatibilityDate: string;
|
|
128
|
+
compatibilityFlags: string[];
|
|
129
|
+
workersDev: false;
|
|
130
|
+
previewUrls: false;
|
|
131
|
+
domains?: string[];
|
|
132
|
+
limits: {
|
|
133
|
+
cpuMs: number;
|
|
134
|
+
subrequests: number;
|
|
135
|
+
};
|
|
136
|
+
observability: NonNullable<WorkerConfig["observability"]>;
|
|
137
|
+
env: {
|
|
138
|
+
PARTITIONS: DurableObjectNamespaceBinding;
|
|
139
|
+
SHARDS: DurableObjectNamespaceBinding;
|
|
140
|
+
ROOTS: DurableObjectNamespaceBinding;
|
|
141
|
+
PROJECTIONS: DurableObjectNamespaceBinding;
|
|
142
|
+
TELEMETRY: DurableObjectNamespaceBinding;
|
|
143
|
+
INDEX: R2BucketBinding;
|
|
144
|
+
GRAPH_GRANTS: {
|
|
145
|
+
type: "json";
|
|
146
|
+
value: Record<string, Record<string, string[]>>;
|
|
147
|
+
};
|
|
148
|
+
LOADER: {
|
|
149
|
+
type: "worker-loader";
|
|
150
|
+
};
|
|
151
|
+
LOCATION_HINT?: {
|
|
152
|
+
type: "text";
|
|
153
|
+
value: string;
|
|
154
|
+
};
|
|
155
|
+
CACHE_ORIGIN?: {
|
|
156
|
+
type: "text";
|
|
157
|
+
value: string;
|
|
158
|
+
};
|
|
159
|
+
FLUSH_MS?: {
|
|
160
|
+
type: "text";
|
|
161
|
+
value: string;
|
|
162
|
+
};
|
|
163
|
+
INITIAL_PARTITIONS?: {
|
|
164
|
+
type: "text";
|
|
165
|
+
value: string;
|
|
166
|
+
};
|
|
167
|
+
ANALYTICS?: AnalyticsEngineDatasetBinding;
|
|
168
|
+
TELEMETRY_MODE?: {
|
|
169
|
+
type: "text";
|
|
170
|
+
value: string;
|
|
171
|
+
};
|
|
172
|
+
CONSOLE?: {
|
|
173
|
+
type: "json";
|
|
174
|
+
value: {
|
|
175
|
+
hostname: string;
|
|
176
|
+
teamDomain: string;
|
|
177
|
+
aud: string;
|
|
178
|
+
clientId: string;
|
|
179
|
+
name: string;
|
|
180
|
+
bucket: string;
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
PROJECTIONS_CONFIG?: {
|
|
184
|
+
type: "json";
|
|
185
|
+
value: JsonValue;
|
|
186
|
+
};
|
|
187
|
+
} & Record<string, InstanceBinding>;
|
|
188
|
+
exports: {
|
|
189
|
+
PartitionDO: SqliteClass;
|
|
190
|
+
IndexShardDO: SqliteClass;
|
|
191
|
+
GraphRootDO: SqliteClass;
|
|
192
|
+
ProjectionDO: SqliteClass;
|
|
193
|
+
TelemetryDO: SqliteClass;
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
export declare function fatedb<E extends Entrypoint>(options: FateDBOptions<E>): InstanceWorker<E>;
|
|
197
|
+
export declare function toJsonValue(value: unknown): JsonValue;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the Cloudflare Worker configuration for a FateDB instance inside `cloudflare.config.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Runs in Node when the `cf` CLI loads the config: nothing here may runtime-import
|
|
5
|
+
* `cloudflare:workers`. The returned Worker config is plain data, typed so both `cf`'s
|
|
6
|
+
* `defineConfig` and the Vite plugin's `auxiliaryWorkers` accept it.
|
|
7
|
+
*/
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { parseProjectionsConfig } from "./projection-config.js";
|
|
10
|
+
import { AUD, HOSTNAME, TEAM_DOMAIN } from "./server/access.js";
|
|
11
|
+
import { CLIENT_ID, parseGrants } from "./server/authorization.js";
|
|
12
|
+
import { COMPATIBILITY_DATE } from "./version.js";
|
|
13
|
+
/**
|
|
14
|
+
* A FateDB instance inside an app's own `cf dev` session, for the Vite plugin:
|
|
15
|
+
*
|
|
16
|
+
* cloudflare({ auxiliaryWorkers: [devInstance({ name: "my-db", grants: { "my-app": { "*": ["read", "write"] } } })] })
|
|
17
|
+
*
|
|
18
|
+
* The app's binding then reaches it with its props; a call between two dev sessions arrives
|
|
19
|
+
* without them, and is denied. It never ships with the app: deploy the instance from its project.
|
|
20
|
+
*/
|
|
21
|
+
export function devInstance(options) {
|
|
22
|
+
const entrypoint = fileURLToPath(new URL("./server/index.js", import.meta.url));
|
|
23
|
+
return { devOnly: true, config: fatedb({ ...options, entrypoint }) };
|
|
24
|
+
}
|
|
25
|
+
/** A copy of the grants as plain JSON, as the binding takes it. */
|
|
26
|
+
function plainGrants(grants) {
|
|
27
|
+
return Object.fromEntries(Object.entries(grants).map(([client, graphs]) => [client, Object.fromEntries(Object.entries(graphs).map(([graph, permissions]) => [graph, [...permissions]]))]));
|
|
28
|
+
}
|
|
29
|
+
export { COMPATIBILITY_DATE } from "./version.js";
|
|
30
|
+
const WORKER_NAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
31
|
+
const BUCKET_NAME = /^[a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])$/;
|
|
32
|
+
export function fatedb(options) {
|
|
33
|
+
const { name, entrypoint } = options;
|
|
34
|
+
if (!WORKER_NAME.test(name)) {
|
|
35
|
+
throw new RangeError(`fatedb: name ${JSON.stringify(name)} must be 1 to 63 lowercase letters, digits and hyphens, not starting or ending with a hyphen`);
|
|
36
|
+
}
|
|
37
|
+
const bucket = options.bucket ?? `${name}-index`;
|
|
38
|
+
if (!BUCKET_NAME.test(bucket)) {
|
|
39
|
+
throw new RangeError(`fatedb: bucket ${JSON.stringify(bucket)} must be 3 to 63 lowercase letters, digits and hyphens${options.bucket === undefined ? "; pass a shorter name or a bucket" : ""}`);
|
|
40
|
+
}
|
|
41
|
+
const grants = options.grants ?? {};
|
|
42
|
+
try {
|
|
43
|
+
parseGrants(grants);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new RangeError(`fatedb: ${error instanceof Error ? error.message : String(error)}`);
|
|
47
|
+
}
|
|
48
|
+
const console = options.console === undefined ? undefined : consoleBinding(options.console, name, bucket);
|
|
49
|
+
let projections;
|
|
50
|
+
try {
|
|
51
|
+
projections = parseProjectionsConfig(options.projections);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw new RangeError(`fatedb: ${error instanceof Error ? error.message : String(error)}`);
|
|
55
|
+
}
|
|
56
|
+
if (options.telemetry === "analytics" && options.analytics === undefined) {
|
|
57
|
+
throw new RangeError('fatedb: telemetry: "analytics" requires an `analytics` dataset name');
|
|
58
|
+
}
|
|
59
|
+
const telemetryMode = options.telemetry === "analytics" || (options.telemetry === undefined && options.analytics !== undefined && console === undefined)
|
|
60
|
+
? "analytics"
|
|
61
|
+
: undefined;
|
|
62
|
+
return {
|
|
63
|
+
name,
|
|
64
|
+
entrypoint,
|
|
65
|
+
compatibilityDate: options.compatibilityDate ?? COMPATIBILITY_DATE,
|
|
66
|
+
compatibilityFlags: ["nodejs_compat"],
|
|
67
|
+
// Apps reach the instance through service bindings; only a console gives it a URL, its own hostname.
|
|
68
|
+
workersDev: false,
|
|
69
|
+
previewUrls: false,
|
|
70
|
+
...(console === undefined ? {} : { domains: [console.hostname] }),
|
|
71
|
+
// A query reads segments from R2 and the caller's writes from partitions: one subrequest each.
|
|
72
|
+
limits: { cpuMs: 30_000, subrequests: 100_000 },
|
|
73
|
+
observability: options.observability ?? { enabled: true },
|
|
74
|
+
env: buildInstanceEnv({
|
|
75
|
+
name,
|
|
76
|
+
bucket,
|
|
77
|
+
grants,
|
|
78
|
+
locationHint: options.locationHint,
|
|
79
|
+
cacheOrigin: options.cacheOrigin ?? (console === undefined ? undefined : `https://${console.hostname}`),
|
|
80
|
+
flushMs: options.flushMs,
|
|
81
|
+
initialPartitions: options.initialPartitions,
|
|
82
|
+
analytics: options.analytics,
|
|
83
|
+
telemetryMode,
|
|
84
|
+
console,
|
|
85
|
+
projections,
|
|
86
|
+
}),
|
|
87
|
+
exports: {
|
|
88
|
+
PartitionDO: { type: "durable-object", storage: "sqlite" },
|
|
89
|
+
IndexShardDO: { type: "durable-object", storage: "sqlite" },
|
|
90
|
+
GraphRootDO: { type: "durable-object", storage: "sqlite" },
|
|
91
|
+
ProjectionDO: { type: "durable-object", storage: "sqlite" },
|
|
92
|
+
TelemetryDO: { type: "durable-object", storage: "sqlite" },
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export function toJsonValue(value) {
|
|
97
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean")
|
|
98
|
+
return value;
|
|
99
|
+
if (Array.isArray(value))
|
|
100
|
+
return value.map(toJsonValue);
|
|
101
|
+
if (typeof value === "object") {
|
|
102
|
+
const out = {};
|
|
103
|
+
for (const [k, v] of Object.entries(value)) {
|
|
104
|
+
if (v !== undefined)
|
|
105
|
+
out[k] = toJsonValue(v);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
function buildInstanceEnv(input) {
|
|
112
|
+
const env = {
|
|
113
|
+
PARTITIONS: { type: "durable-object", worker: input.name, exportName: "PartitionDO" },
|
|
114
|
+
SHARDS: { type: "durable-object", worker: input.name, exportName: "IndexShardDO" },
|
|
115
|
+
ROOTS: { type: "durable-object", worker: input.name, exportName: "GraphRootDO" },
|
|
116
|
+
PROJECTIONS: { type: "durable-object", worker: input.name, exportName: "ProjectionDO" },
|
|
117
|
+
TELEMETRY: { type: "durable-object", worker: input.name, exportName: "TelemetryDO" },
|
|
118
|
+
INDEX: { type: "r2", name: input.bucket },
|
|
119
|
+
GRAPH_GRANTS: { type: "json", value: plainGrants(input.grants) },
|
|
120
|
+
// Runs the code agents send to /mcp, each in a sandbox of its own (Dynamic Workers).
|
|
121
|
+
LOADER: { type: "worker-loader" },
|
|
122
|
+
};
|
|
123
|
+
if (input.locationHint !== undefined)
|
|
124
|
+
env.LOCATION_HINT = { type: "text", value: input.locationHint };
|
|
125
|
+
if (input.cacheOrigin !== undefined)
|
|
126
|
+
env.CACHE_ORIGIN = { type: "text", value: input.cacheOrigin };
|
|
127
|
+
if (input.flushMs !== undefined)
|
|
128
|
+
env.FLUSH_MS = { type: "text", value: String(input.flushMs) };
|
|
129
|
+
if (input.initialPartitions !== undefined)
|
|
130
|
+
env.INITIAL_PARTITIONS = { type: "text", value: String(input.initialPartitions) };
|
|
131
|
+
if (input.analytics !== undefined)
|
|
132
|
+
env.ANALYTICS = { type: "analytics-engine-dataset", name: input.analytics };
|
|
133
|
+
if (input.telemetryMode !== undefined)
|
|
134
|
+
env.TELEMETRY_MODE = { type: "text", value: input.telemetryMode };
|
|
135
|
+
if (input.console !== undefined)
|
|
136
|
+
env.CONSOLE = { type: "json", value: input.console };
|
|
137
|
+
if (input.projections.configs.length > 0) {
|
|
138
|
+
env.PROJECTIONS_CONFIG = { type: "json", value: toJsonValue(input.projections.configs) };
|
|
139
|
+
}
|
|
140
|
+
for (const [k, v] of Object.entries(input.projections.workerBindings))
|
|
141
|
+
env[k] = v;
|
|
142
|
+
for (const [k, v] of Object.entries(input.projections.r2Bindings))
|
|
143
|
+
env[k] = v;
|
|
144
|
+
return env;
|
|
145
|
+
}
|
|
146
|
+
function consoleBinding(options, name, bucket) {
|
|
147
|
+
const { hostname, access } = options;
|
|
148
|
+
const clientId = options.clientId ?? "console";
|
|
149
|
+
if (typeof hostname !== "string" || !HOSTNAME.test(hostname))
|
|
150
|
+
throw new RangeError(`fatedb: console.hostname ${JSON.stringify(hostname)} must be a hostname such as db.example.com`);
|
|
151
|
+
if (typeof access?.teamDomain !== "string" || !TEAM_DOMAIN.test(access.teamDomain)) {
|
|
152
|
+
throw new RangeError(`fatedb: console.access.teamDomain ${JSON.stringify(access?.teamDomain)} must be your Access team's domain, <team>.cloudflareaccess.com`);
|
|
153
|
+
}
|
|
154
|
+
if (typeof access.aud !== "string" || !AUD.test(access.aud))
|
|
155
|
+
throw new RangeError("fatedb: console.access.aud must be the Access application's AUD tag, 64 hexadecimal characters");
|
|
156
|
+
if (!CLIENT_ID.test(clientId))
|
|
157
|
+
throw new RangeError(`fatedb: console.clientId ${JSON.stringify(clientId)} is not a client id (${CLIENT_ID.source})`);
|
|
158
|
+
return { hostname, teamDomain: access.teamDomain, aud: access.aud, clientId, name, bucket };
|
|
159
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { NonEmptyReadonlyArray } from "../domain/fact.js";
|
|
2
|
+
import { type ParseResult } from "../domain/result.js";
|
|
3
|
+
import type { Uri } from "../domain/identifiers.js";
|
|
4
|
+
import type { DatalogValue } from "./value.js";
|
|
5
|
+
export type Term = Readonly<{
|
|
6
|
+
kind: "variable";
|
|
7
|
+
name: string;
|
|
8
|
+
}> | Readonly<{
|
|
9
|
+
kind: "constant";
|
|
10
|
+
value: DatalogValue;
|
|
11
|
+
}> | Readonly<{
|
|
12
|
+
kind: "wildcard";
|
|
13
|
+
}>;
|
|
14
|
+
export type FactClause = Readonly<{
|
|
15
|
+
kind: "fact";
|
|
16
|
+
predicate: Uri;
|
|
17
|
+
entity: Term;
|
|
18
|
+
value: Term;
|
|
19
|
+
}>;
|
|
20
|
+
export type BuiltinName = "=" | "<" | "<=" | ">" | ">=" | "eq" | "lt" | "lte" | "gt" | "gte" | "before" | "after" | "contains" | "startsWith" | "endsWith";
|
|
21
|
+
export type BuiltinClause = Readonly<{
|
|
22
|
+
kind: "builtin";
|
|
23
|
+
name: BuiltinName;
|
|
24
|
+
left: Term;
|
|
25
|
+
right: Term;
|
|
26
|
+
}>;
|
|
27
|
+
export type DatalogClause = FactClause | BuiltinClause;
|
|
28
|
+
export type DatalogQuery = Readonly<{
|
|
29
|
+
facts: NonEmptyReadonlyArray<FactClause>;
|
|
30
|
+
constraints: readonly BuiltinClause[];
|
|
31
|
+
}>;
|
|
32
|
+
export declare const MAX_QUERY_CLAUSES = 64;
|
|
33
|
+
/** Maximum number of queries accepted in a single `queryMany` batch. */
|
|
34
|
+
export declare const MAX_QUERY_MANY = 32;
|
|
35
|
+
export declare function createDatalogQuery(facts: readonly FactClause[], constraints: readonly BuiltinClause[]): ParseResult<DatalogQuery>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { parsed, rejected } from "../domain/result.js";
|
|
2
|
+
export const MAX_QUERY_CLAUSES = 64;
|
|
3
|
+
/** Maximum number of queries accepted in a single `queryMany` batch. */
|
|
4
|
+
export const MAX_QUERY_MANY = 32;
|
|
5
|
+
export function createDatalogQuery(facts, constraints) {
|
|
6
|
+
if (facts.length === 0) {
|
|
7
|
+
return rejected("query", "a query must contain at least one positive fact relation");
|
|
8
|
+
}
|
|
9
|
+
if (facts.length + constraints.length > MAX_QUERY_CLAUSES) {
|
|
10
|
+
return rejected("query", `a query cannot contain more than ${MAX_QUERY_CLAUSES} clauses`);
|
|
11
|
+
}
|
|
12
|
+
const boundVariables = new Set();
|
|
13
|
+
for (const fact of facts) {
|
|
14
|
+
collectVariables(fact.entity, boundVariables);
|
|
15
|
+
collectVariables(fact.value, boundVariables);
|
|
16
|
+
}
|
|
17
|
+
for (const [index, constraint] of constraints.entries()) {
|
|
18
|
+
for (const term of [constraint.left, constraint.right]) {
|
|
19
|
+
if (term.kind === "wildcard") {
|
|
20
|
+
return rejected(`query.constraints[${index}]`, "wildcards are not valid builtin arguments");
|
|
21
|
+
}
|
|
22
|
+
if (term.kind === "variable" && !boundVariables.has(term.name)) {
|
|
23
|
+
return rejected(`query.constraints[${index}]`, `builtin variable ${term.name} must be bound by a fact relation`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const [firstFact, ...restFacts] = facts;
|
|
28
|
+
if (firstFact === undefined) {
|
|
29
|
+
return rejected("query", "query must contain at least one fact relation");
|
|
30
|
+
}
|
|
31
|
+
const frozenFacts = Object.freeze([firstFact, ...restFacts]);
|
|
32
|
+
return parsed(Object.freeze({
|
|
33
|
+
facts: frozenFacts,
|
|
34
|
+
constraints: Object.freeze([...constraints]),
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
function collectVariables(term, variables) {
|
|
38
|
+
if (term.kind === "variable")
|
|
39
|
+
variables.add(term.name);
|
|
40
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DatalogQuery } from "./ast.js";
|
|
2
|
+
import type { FactSource } from "./fact-source.js";
|
|
3
|
+
import { type BoundValue } from "./value.js";
|
|
4
|
+
/** A variable's value: the fact value of the first atom that bound it, with what it matches by. */
|
|
5
|
+
export type Binding = ReadonlyMap<string, BoundValue>;
|
|
6
|
+
export type EvaluationOptions = Readonly<{
|
|
7
|
+
maxResults?: number;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}>;
|
|
10
|
+
export declare class QueryResultLimitExceeded extends Error {
|
|
11
|
+
readonly maxResults: number;
|
|
12
|
+
constructor(maxResults: number);
|
|
13
|
+
}
|
|
14
|
+
export declare function evaluateDatalog(query: DatalogQuery, facts: FactSource, options?: EvaluationOptions): AsyncGenerator<Binding>;
|