@_linked/server 3.12.1 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/lib/esm/shapes/LinkedServer.d.ts +13 -6
- package/lib/esm/shapes/LinkedServer.js +47 -27
- package/lib/esm/shapes/LinkedServer.js.map +1 -1
- package/lib/esm/shapes/quadstores/RemoteDataset.d.ts +17 -14
- package/lib/esm/shapes/quadstores/RemoteDataset.js +57 -20
- package/lib/esm/shapes/quadstores/RemoteDataset.js.map +1 -1
- package/lib/esm/utils/adminChannel.d.ts +7 -6
- package/lib/esm/utils/adminChannel.js +2 -2
- package/lib/esm/utils/adminChannel.js.map +1 -1
- package/lib/esm/utils/graphTargets.d.ts +17 -9
- package/lib/esm/utils/graphTargets.js +34 -20
- package/lib/esm/utils/graphTargets.js.map +1 -1
- package/lib/esm/utils/linkedDependencies.d.ts +53 -7
- package/lib/esm/utils/linkedDependencies.js +130 -12
- package/lib/esm/utils/linkedDependencies.js.map +1 -1
- package/lib/esm/utils/ontologySync.d.ts +19 -12
- package/lib/esm/utils/ontologySync.js +45 -15
- package/lib/esm/utils/ontologySync.js.map +1 -1
- package/lib/esm/utils/syncShapes.d.ts +5 -0
- package/lib/esm/utils/syncShapes.js +17 -5
- package/lib/esm/utils/syncShapes.js.map +1 -1
- package/package.json +4 -4
- package/readme.md +24 -14
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* this server depends on has all of them. Kept apart from `LinkedServer` so the order and the failure handling can be tested without
|
|
26
26
|
* booting a server.
|
|
27
27
|
*/
|
|
28
|
-
import type
|
|
28
|
+
import { type BootOrphanScope } from './syncShapes.js';
|
|
29
29
|
/** The subset of core's ontology and shape-check API this module calls. */
|
|
30
30
|
export interface OntologyCore {
|
|
31
31
|
syncOntologies(ds: unknown, options: {
|
|
@@ -35,7 +35,6 @@ export interface OntologyCore {
|
|
|
35
35
|
skipped: string[];
|
|
36
36
|
pruned: string[];
|
|
37
37
|
target?: string;
|
|
38
|
-
fallback?: true;
|
|
39
38
|
}>;
|
|
40
39
|
checkShapeHierarchy(options: {
|
|
41
40
|
dependencies?: Record<string, string[]>;
|
|
@@ -64,6 +63,12 @@ export interface OntologyCore {
|
|
|
64
63
|
importModule?: (file: string) => Promise<unknown>;
|
|
65
64
|
}) => Promise<string[]>;
|
|
66
65
|
}
|
|
66
|
+
/** The shape check's findings by severity; `failed` holds the message of a check that threw. */
|
|
67
|
+
export interface ShapeCheckSummary {
|
|
68
|
+
errors: number;
|
|
69
|
+
warnings: number;
|
|
70
|
+
failed?: string;
|
|
71
|
+
}
|
|
67
72
|
/** What the ontology sync and the shape check did, as `POST /admin/sync-shapes` reports it. */
|
|
68
73
|
export interface OntologyPhaseResult {
|
|
69
74
|
/**
|
|
@@ -77,12 +82,12 @@ export interface OntologyPhaseResult {
|
|
|
77
82
|
graphs?: string[];
|
|
78
83
|
droppedGraphs?: string[];
|
|
79
84
|
};
|
|
80
|
-
/**
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
/** The
|
|
85
|
+
/**
|
|
86
|
+
* The shape check's findings, counted by severity, and the message of the error the check
|
|
87
|
+
* threw, if it did (`failed`). A thrown check is reported here and never fails the phase.
|
|
88
|
+
*/
|
|
89
|
+
shapeCheck: ShapeCheckSummary;
|
|
90
|
+
/** The error the ontology sync threw; the phase went on without it. */
|
|
86
91
|
failure?: Error;
|
|
87
92
|
}
|
|
88
93
|
/**
|
|
@@ -119,8 +124,9 @@ export declare function sourceModuleImporter(vite: {
|
|
|
119
124
|
/**
|
|
120
125
|
* Register the ontologies of the installed linked packages in `packageDirs` (package roots), sync
|
|
121
126
|
* the registered ontologies into `ds` (when `sync` is set and there is a dataset), then run the
|
|
122
|
-
* shape check. Never throws: a failed registration is logged and the phase goes on; a failed sync
|
|
123
|
-
*
|
|
127
|
+
* shape check. Never throws: a failed registration is logged and the phase goes on; a failed sync
|
|
128
|
+
* is logged and returned as `failure`, and the check still runs; a check that throws is logged and
|
|
129
|
+
* returned as `shapeCheck.failed`. Logs one line for the sync, one summary line for
|
|
124
130
|
* the check and one line per finding.
|
|
125
131
|
*/
|
|
126
132
|
export declare function runOntologyPhase(ds: unknown, { core, sync, orphanScope, dependencies, packageDirs, importModule, log, warn, }: {
|
|
@@ -152,7 +158,8 @@ export declare function graphsWrittenAndDropped(ops: Array<{
|
|
|
152
158
|
};
|
|
153
159
|
/**
|
|
154
160
|
* The host's re-sync: the ontology phase, then the shape sync, which runs also when the ontology
|
|
155
|
-
*
|
|
156
|
-
*
|
|
161
|
+
* sync failed. The ontology sync's failure is then thrown, so the route answers with it, as it
|
|
162
|
+
* does for a failed shape sync. A shape check that threw fails nothing: the answer reports it as
|
|
163
|
+
* `shapeCheck.failed`.
|
|
157
164
|
*/
|
|
158
165
|
export declare function syncOntologiesThenShapes<T extends object>(ontologyPhase: () => Promise<OntologyPhaseResult>, shapeSync: () => Promise<T>): Promise<T & Pick<OntologyPhaseResult, 'ontologies' | 'shapeCheck'>>;
|
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ontology sync and the shape check, as the server runs them before its shape sync: at boot and
|
|
3
|
+
* when the host asks for a re-sync (`POST /admin/sync-shapes`).
|
|
4
|
+
*
|
|
5
|
+
* - The ontology sync (core `syncOntologies`) writes each registered package's ontology data into
|
|
6
|
+
* its own named graph of the app's default dataset (`{package IRI}/graph/ontology`, with a
|
|
7
|
+
* record in the graph registry that marks it as a hierarchy source), skipping a package whose
|
|
8
|
+
* stored hash matches, so the store's RDFS view infers the class hierarchy. A store without
|
|
9
|
+
* graph queries (it answers `unsupported-query-kind`, or has no `graphQuery`) gets core's
|
|
10
|
+
* earlier sync of the `rdfs:subClassOf` triples into the default graph instead. The graphs the
|
|
11
|
+
* sync wrote or dropped are observed on core's query dispatch and reported by name.
|
|
12
|
+
* - The shape check (core `checkShapeHierarchy`) compares the registered shapes with that
|
|
13
|
+
* hierarchy. A running app is never stopped by it: its findings are logged and counted.
|
|
14
|
+
* - The in-memory class hierarchy (core `loadOntologyHierarchy`) is loaded also when the sync
|
|
15
|
+
* is off; the sync loads it itself when it runs.
|
|
16
|
+
* - Before either, every linked package directory the server discovered registers its ontologies
|
|
17
|
+
* (core `registerInstalledOntologies`): a package the app only deep-imports shape files from
|
|
18
|
+
* never evaluates its register modules, so its data would be missing from both. A source
|
|
19
|
+
* package without built register modules (a workspace package in dev) registers through
|
|
20
|
+
* `importModule` (Vite's `ssrLoadModule`), when the caller passes one.
|
|
21
|
+
*
|
|
22
|
+
* The four core modules (`ontology/syncOntologies`, `shapes/checkShapeHierarchy`,
|
|
23
|
+
* `ontology/hierarchy`, `ontology/registerInstalled`) are server only and not in core's index, so
|
|
24
|
+
* {@link loadOntologyCore} imports them by their fixed subpaths when the phase runs; the core
|
|
25
|
+
* this server depends on has all of them. Kept apart from `LinkedServer` so the order and the failure handling can be tested without
|
|
26
|
+
* booting a server.
|
|
27
|
+
*/
|
|
28
|
+
import { writtenAndUnchanged } from './syncShapes.js';
|
|
1
29
|
/**
|
|
2
30
|
* Whether the server syncs shapes (and so ontologies) into its app data: off when `linked.config`
|
|
3
31
|
* sets `syncShapesOnBoot: false` or the environment sets `LINKED_SYNC_SHAPES_ON_BOOT=false`. One
|
|
@@ -57,12 +85,13 @@ const list = (names) => (names.length ? names.join(', ') : 'none');
|
|
|
57
85
|
/**
|
|
58
86
|
* Register the ontologies of the installed linked packages in `packageDirs` (package roots), sync
|
|
59
87
|
* the registered ontologies into `ds` (when `sync` is set and there is a dataset), then run the
|
|
60
|
-
* shape check. Never throws: a failed registration is logged and the phase goes on; a failed sync
|
|
61
|
-
*
|
|
88
|
+
* shape check. Never throws: a failed registration is logged and the phase goes on; a failed sync
|
|
89
|
+
* is logged and returned as `failure`, and the check still runs; a check that throws is logged and
|
|
90
|
+
* returned as `shapeCheck.failed`. Logs one line for the sync, one summary line for
|
|
62
91
|
* the check and one line per finding.
|
|
63
92
|
*/
|
|
64
93
|
export async function runOntologyPhase(ds, { core, sync, orphanScope, dependencies, packageDirs = [], importModule, log = console.log, warn = console.warn, }) {
|
|
65
|
-
var _a
|
|
94
|
+
var _a;
|
|
66
95
|
const result = {
|
|
67
96
|
ontologies: { written: [], skipped: [], graphs: [], droppedGraphs: [] },
|
|
68
97
|
shapeCheck: { errors: 0, warnings: 0 },
|
|
@@ -94,17 +123,15 @@ export async function runOntologyPhase(ds, { core, sync, orphanScope, dependenci
|
|
|
94
123
|
const { graphs, droppedGraphs } = graphsWrittenAndDropped(ops.filter((op) => op.ok));
|
|
95
124
|
result.ontologies = { written: sync.written, skipped: sync.skipped, graphs, droppedGraphs };
|
|
96
125
|
hierarchyLoaded = true;
|
|
97
|
-
// Core
|
|
98
|
-
//
|
|
126
|
+
// Core says where it wrote (the global `ontologyGraph`). A store without graph queries makes
|
|
127
|
+
// the sync throw in the 'package' and shared modes, reported below as a failed sync.
|
|
99
128
|
const where = sync.target === 'default'
|
|
100
129
|
? " (default graph: ontologyGraph is 'default')"
|
|
101
|
-
: sync.
|
|
102
|
-
?
|
|
103
|
-
:
|
|
104
|
-
? ` (one shared graph <${sync.target}>)`
|
|
105
|
-
: '';
|
|
130
|
+
: sync.target && sync.target !== 'package'
|
|
131
|
+
? ` (one shared graph <${sync.target}>)`
|
|
132
|
+
: '';
|
|
106
133
|
log(`[LinkedServer] ontology sync${where}: ` +
|
|
107
|
-
|
|
134
|
+
writtenAndUnchanged(sync.written, sync.skipped) +
|
|
108
135
|
(orphanScope === 'all' ? `; pruned ${list(sync.pruned)}` : '') +
|
|
109
136
|
(graphs.length ? `; graphs written ${list(graphs)}` : '') +
|
|
110
137
|
(droppedGraphs.length ? `; graphs dropped ${list(droppedGraphs)}` : ''));
|
|
@@ -129,12 +156,14 @@ export async function runOntologyPhase(ds, { core, sync, orphanScope, dependenci
|
|
|
129
156
|
const findings = await core.checkShapeHierarchy({ dependencies });
|
|
130
157
|
const errors = findings.filter((f) => f.severity === 'error').length;
|
|
131
158
|
result.shapeCheck = { errors, warnings: findings.length - errors };
|
|
132
|
-
|
|
159
|
+
if (findings.length) {
|
|
160
|
+
log(`[LinkedServer] shape check: ${errors} error(s), ${findings.length - errors} warning(s)`);
|
|
161
|
+
}
|
|
133
162
|
for (const f of findings)
|
|
134
163
|
warn(`[LinkedServer] shape check ${f.severity} (${f.kind}): ${f.message}`);
|
|
135
164
|
}
|
|
136
165
|
catch (err) {
|
|
137
|
-
|
|
166
|
+
result.shapeCheck = { errors: 0, warnings: 0, failed: asError(err).message };
|
|
138
167
|
warn('[LinkedServer] shape check failed:', err);
|
|
139
168
|
}
|
|
140
169
|
return result;
|
|
@@ -165,8 +194,9 @@ export function graphsWrittenAndDropped(ops) {
|
|
|
165
194
|
}
|
|
166
195
|
/**
|
|
167
196
|
* The host's re-sync: the ontology phase, then the shape sync, which runs also when the ontology
|
|
168
|
-
*
|
|
169
|
-
*
|
|
197
|
+
* sync failed. The ontology sync's failure is then thrown, so the route answers with it, as it
|
|
198
|
+
* does for a failed shape sync. A shape check that threw fails nothing: the answer reports it as
|
|
199
|
+
* `shapeCheck.failed`.
|
|
170
200
|
*/
|
|
171
201
|
export async function syncOntologiesThenShapes(ontologyPhase, shapeSync) {
|
|
172
202
|
const { ontologies, shapeCheck, failure } = await ontologyPhase();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ontologySync.js","sourceRoot":"","sources":["../../../src/utils/ontologySync.ts"],"names":[],"mappings":"AAwEA;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,GAAuC,EACvC,UAAmB;IAEnB,OAAO,UAAU,KAAK,KAAK,IAAI,GAAG,CAAC,0BAA0B,KAAK,OAAO,CAAC;AAC5E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CACjC,GAAuC,EACvC,UAAoB;IAEpB,OAAO,WAAW,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,GAAG,CAAC,8BAA8B,KAAK,OAAO,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AC;AAC7C,CAAC","sourcesContent":["/**\n * Ontology sync and the shape check, as the server runs them before its shape sync: at boot and\n * when the host asks for a re-sync (`POST /admin/sync-shapes`).\n *\n * - The ontology sync (core `syncOntologies`) writes each registered package's ontology data into\n * its own named graph of the app's default dataset (`{package IRI}/graph/ontology`, with a\n * record in the graph registry that marks it as a hierarchy source), skipping a package whose\n * stored hash matches, so the store's RDFS view infers the class hierarchy. A store without\n * graph queries (it answers `unsupported-query-kind`, or has no `graphQuery`) gets core's\n * earlier sync of the `rdfs:subClassOf` triples into the default graph instead. The graphs the\n * sync wrote or dropped are observed on core's query dispatch and reported by name.\n * - The shape check (core `checkShapeHierarchy`) compares the registered shapes with that\n * hierarchy. A running app is never stopped by it: its findings are logged and counted.\n * - The in-memory class hierarchy (core `loadOntologyHierarchy`) is loaded also when the sync\n * is off; the sync loads it itself when it runs.\n * - Before either, every linked package directory the server discovered registers its ontologies\n * (core `registerInstalledOntologies`): a package the app only deep-imports shape files from\n * never evaluates its register modules, so its data would be missing from both. A source\n * package without built register modules (a workspace package in dev) registers through\n * `importModule` (Vite's `ssrLoadModule`), when the caller passes one.\n *\n * The four core modules (`ontology/syncOntologies`, `shapes/checkShapeHierarchy`,\n * `ontology/hierarchy`, `ontology/registerInstalled`) are server only and not in core's index, so\n * {@link loadOntologyCore} imports them by their fixed subpaths when the phase runs; the core\n * this server depends on has all of them. Kept apart from `LinkedServer` so the order and the failure handling can be tested without\n * booting a server.\n */\nimport type {BootOrphanScope} from './syncShapes.js';\n\n/** The subset of core's ontology and shape-check API this module calls. */\nexport interface OntologyCore {\n syncOntologies(\n ds: unknown,\n options: {orphanScope: BootOrphanScope},\n ): Promise<{written: string[]; skipped: string[]; pruned: string[]; target?: string; fallback?: true}>;\n checkShapeHierarchy(options: {dependencies?: Record<string, string[]>}): Promise<\n Array<{kind: string; severity: 'error' | 'warning'; shape?: string; iri?: string; message: string}>\n >;\n /** Loads the in-memory class hierarchy. Optional so a test double can leave it out. */\n loadOntologyHierarchy?: () => Promise<unknown>;\n /**\n * Calls `onGraph` for every graph query on `ds` that succeeds until the returned function is\n * called (core's `subscribeQueryDispatch`). Optional so a test double can leave it out.\n */\n observeGraphQueries?: (\n ds: unknown,\n onGraph: (query: {kind: string; graph: string}, settled: Promise<unknown>) => void,\n ) => () => void;\n /**\n * Registers the ontologies of the installed packages in the given directories. Optional so a\n * test double can leave it out.\n */\n registerInstalledOntologies?: (\n packageDirs: string[],\n options?: {importModule?: (file: string) => Promise<unknown>},\n ) => Promise<string[]>;\n}\n\n/** What the ontology sync and the shape check did, as `POST /admin/sync-shapes` reports it. */\nexport interface OntologyPhaseResult {\n /**\n * Packages whose ontology graph (or, on a store without graph queries, hierarchy) was written,\n * packages left as they were (unchanged or unreadable), and the graphs the sync wrote and\n * dropped (empty on a store without graph queries).\n */\n ontologies: {written: string[]; skipped: string[]; graphs?: string[]; droppedGraphs?: string[]};\n /** The shape check's findings, counted by severity. */\n shapeCheck: {errors: number; warnings: number};\n /** The first error the sync or the check threw; the phase went on without it. */\n failure?: Error;\n}\n\n/**\n * Whether the server syncs shapes (and so ontologies) into its app data: off when `linked.config`\n * sets `syncShapesOnBoot: false` or the environment sets `LINKED_SYNC_SHAPES_ON_BOOT=false`. One\n * switch for both syncs, at boot and on the host's request.\n */\nexport function shapeSyncOn(\n env: Record<string, string | undefined>,\n configFlag: unknown,\n): boolean {\n return configFlag !== false && env.LINKED_SYNC_SHAPES_ON_BOOT !== 'false';\n}\n\n/**\n * Whether the boot syncs ontologies: when shape sync is on ({@link shapeSyncOn}) and the\n * environment does not opt out with `LINKED_SYNC_ONTOLOGIES_ON_BOOT=false`.\n */\nexport function bootSyncsOntologies(\n env: Record<string, string | undefined>,\n configFlag?: unknown,\n): boolean {\n return shapeSyncOn(env, configFlag) && env.LINKED_SYNC_ONTOLOGIES_ON_BOOT !== 'false';\n}\n\n/**\n * Import core's ontology sync, shape check, hierarchy loader and installed packages'\n * registration.\n */\nexport async function loadOntologyCore(): Promise<OntologyCore> {\n const sync = await import('@_linked/core/ontology/syncOntologies');\n const check = await import('@_linked/core/shapes/checkShapeHierarchy');\n const hierarchy = await import('@_linked/core/ontology/hierarchy');\n const registerInstalled = await import('@_linked/core/ontology/registerInstalled');\n const dispatch = await import('@_linked/core/queries/queryDispatch');\n return {\n syncOntologies: (ds, options) =>\n sync.syncOntologies(ds as Parameters<typeof sync.syncOntologies>[0], options),\n checkShapeHierarchy: (options) => check.checkShapeHierarchy(options),\n loadOntologyHierarchy: () => hierarchy.loadOntologyHierarchy(),\n registerInstalledOntologies: (packageDirs, options) =>\n registerInstalled.registerInstalledOntologies(packageDirs, options),\n observeGraphQueries: (ds, onGraph) =>\n dispatch.subscribeQueryDispatch((event) => {\n if (event.kind === 'graph' && event.target === ds) onGraph(event.query, event.result);\n }),\n };\n}\n\n/**\n * The package directories whose ontologies register before the sync: every linked package\n * discovered in the app's dependency tree, then the app's own workspace packages (an ontology\n * package in `packages/` that no package imports is one, so the dependency walk never reaches it).\n * Order kept, duplicates dropped.\n */\nexport function ontologyPackageDirs(\n linkedPackageDirs: Iterable<string>,\n workspacePackageDirs: Iterable<string>,\n): string[] {\n return [...new Set([...linkedPackageDirs, ...workspacePackageDirs])];\n}\n\n/**\n * In dev, the loader for source register modules: Vite's `ssrLoadModule`, so a workspace package\n * that was never built registers on the core the SSR graph uses. Undefined without Vite (a built\n * app), where source packages are skipped.\n */\nexport function sourceModuleImporter(\n vite: {ssrLoadModule?: (url: string) => Promise<unknown>} | undefined | null,\n): ((file: string) => Promise<unknown>) | undefined {\n const ssrLoadModule = vite?.ssrLoadModule;\n return typeof ssrLoadModule === 'function' ? (file) => ssrLoadModule.call(vite, file) : undefined;\n}\n\nconst asError = (err: unknown): Error => (err instanceof Error ? err : new Error(String(err)));\nconst list = (names: string[]) => (names.length ? names.join(', ') : 'none');\n\n/**\n * Register the ontologies of the installed linked packages in `packageDirs` (package roots), sync\n * the registered ontologies into `ds` (when `sync` is set and there is a dataset), then run the\n * shape check. Never throws: a failed registration is logged and the phase goes on; a failed sync is logged and returned as `failure`, and the\n * check still runs; a failed check likewise. Logs one line for the sync, one summary line for\n * the check and one line per finding.\n */\nexport async function runOntologyPhase(\n ds: unknown,\n {\n core,\n sync,\n orphanScope,\n dependencies,\n packageDirs = [],\n importModule,\n log = console.log,\n warn = console.warn,\n }: {\n core: OntologyCore;\n sync: boolean;\n orphanScope: BootOrphanScope;\n dependencies?: Record<string, string[]>;\n /** Every linked package directory the server discovered (transitively). */\n packageDirs?: string[];\n /**\n * Imports a source register module (`src/ontologies/*.register.ts`) of a package directory\n * without built ones; in dev, Vite's `ssrLoadModule`.\n */\n importModule?: (file: string) => Promise<unknown>;\n log?: (message: string) => void;\n warn?: (message: string, err?: unknown) => void;\n },\n): Promise<OntologyPhaseResult> {\n const result: OntologyPhaseResult = {\n ontologies: {written: [], skipped: [], graphs: [], droppedGraphs: []},\n shapeCheck: {errors: 0, warnings: 0},\n };\n\n if (core.registerInstalledOntologies && packageDirs.length) {\n try {\n if (importModule) await core.registerInstalledOntologies(packageDirs, {importModule});\n else await core.registerInstalledOntologies(packageDirs);\n } catch (err) {\n warn(\"[LinkedServer] registering the installed packages' ontologies failed:\", err);\n }\n }\n\n let hierarchyLoaded = false;\n if (sync && ds) {\n // Every graph query that succeeded, in the order the sync sent them.\n const ops: Array<{kind: string; graph: string; ok?: boolean}> = [];\n const pending: Promise<unknown>[] = [];\n const stop = core.observeGraphQueries?.(ds, (query, settled) => {\n const op: {kind: string; graph: string; ok?: boolean} = {kind: query.kind, graph: query.graph};\n ops.push(op);\n pending.push(\n settled.then(\n () => (op.ok = true),\n () => undefined,\n ),\n );\n });\n try {\n const sync = await core.syncOntologies(ds, {orphanScope});\n await Promise.all(pending);\n const {graphs, droppedGraphs} = graphsWrittenAndDropped(ops.filter((op) => op.ok));\n result.ontologies = {written: sync.written, skipped: sync.skipped, graphs, droppedGraphs};\n hierarchyLoaded = true;\n // Core ≥ 2.41 says where it wrote (the global `ontologyGraph`); an older core is told\n // apart by a write that sent no graph query.\n const where =\n sync.target === 'default'\n ? \" (default graph: ontologyGraph is 'default')\"\n : sync.fallback || (sync.target === undefined && !!stop && sync.written.length > 0 && graphs.length === 0)\n ? ' (default graph: the store has no graph queries)'\n : sync.target && sync.target !== 'package'\n ? ` (one shared graph <${sync.target}>)`\n : '';\n log(\n `[LinkedServer] ontology sync${where}: ` +\n `written ${list(sync.written)}; skipped ${list(sync.skipped)}` +\n (orphanScope === 'all' ? `; pruned ${list(sync.pruned)}` : '') +\n (graphs.length ? `; graphs written ${list(graphs)}` : '') +\n (droppedGraphs.length ? `; graphs dropped ${list(droppedGraphs)}` : ''),\n );\n } catch (err) {\n result.failure = asError(err);\n warn('[LinkedServer] ontology sync failed:', err);\n } finally {\n stop?.();\n }\n }\n if (!hierarchyLoaded && core.loadOntologyHierarchy) {\n try {\n await core.loadOntologyHierarchy();\n } catch (err) {\n warn('[LinkedServer] loading the class hierarchy failed:', err);\n }\n }\n\n try {\n const findings = await core.checkShapeHierarchy({dependencies});\n const errors = findings.filter((f) => f.severity === 'error').length;\n result.shapeCheck = {errors, warnings: findings.length - errors};\n log(`[LinkedServer] shape check: ${errors} error(s), ${findings.length - errors} warning(s)`);\n for (const f of findings) warn(`[LinkedServer] shape check ${f.severity} (${f.kind}): ${f.message}`);\n } catch (err) {\n result.failure ??= asError(err);\n warn('[LinkedServer] shape check failed:', err);\n }\n return result;\n}\n\n/**\n * The graphs a sync left written and those it left dropped, from its successful graph queries in\n * order. A graph the run created and then dropped (the inference probe's temporary graphs) is in\n * neither list.\n */\nexport function graphsWrittenAndDropped(ops: Array<{kind: string; graph: string}>): {\n graphs: string[];\n droppedGraphs: string[];\n} {\n const byGraph = new Map<string, string[]>();\n for (const {kind, graph} of ops) {\n const kinds = byGraph.get(graph) ?? [];\n kinds.push(kind);\n byGraph.set(graph, kinds);\n }\n const graphs: string[] = [];\n const droppedGraphs: string[] = [];\n for (const [graph, kinds] of byGraph) {\n const last = kinds[kinds.length - 1];\n if (last !== 'graph_drop') graphs.push(graph);\n else if (kinds[0] !== 'graph_create') droppedGraphs.push(graph);\n }\n return {graphs: graphs.sort(), droppedGraphs: droppedGraphs.sort()};\n}\n\n/**\n * The host's re-sync: the ontology phase, then the shape sync, which runs also when the ontology\n * phase failed. The ontology phase's failure is then thrown, so the route answers with it, as\n * it does for a failed shape sync.\n */\nexport async function syncOntologiesThenShapes<T extends object>(\n ontologyPhase: () => Promise<OntologyPhaseResult>,\n shapeSync: () => Promise<T>,\n): Promise<T & Pick<OntologyPhaseResult, 'ontologies' | 'shapeCheck'>> {\n const {ontologies, shapeCheck, failure} = await ontologyPhase();\n const shapes = await shapeSync();\n if (failure) throw failure;\n return {...shapes, ontologies, shapeCheck};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ontologySync.js","sourceRoot":"","sources":["../../../src/utils/ontologySync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAC,mBAAmB,EAAuB,MAAM,iBAAiB,CAAC;AAuD1E;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,GAAuC,EACvC,UAAmB;IAEnB,OAAO,UAAU,KAAK,KAAK,IAAI,GAAG,CAAC,0BAA0B,KAAK,OAAO,CAAC;AAC5E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CACjC,GAAuC,EACvC,UAAoB;IAEpB,OAAO,WAAW,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,GAAG,CAAC,8BAA8B,KAAK,OAAO,CAAC;AACxF,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,uCAAuC,CAAC,CAAC;IACnE,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,0CAA0C,CAAC,CAAC;IACvE,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,kCAAkC,CAAC,CAAC;IACnE,MAAM,iBAAi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AAC;IAC5B,MAAM,aAAa,GAAa,EAAE,CAAC;IACnC,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,OAAO,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACrC,IAAI,IAAI,KAAK,YAAY;YAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aACzC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,cAAc;YAAE,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,EAAC,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,EAAE,aAAa,EAAE,aAAa,CAAC,IAAI,EAAE,EAAC,CAAC;AACtE,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,aAAiD,EACjD,SAA2B;IAE3B,MAAM,EAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAC,GAAG,MAAM,aAAa,EAAE,CAAC;IAChE,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAC;IACjC,IAAI,OAAO;QAAE,MAAM,OAAO,CAAC;IAC3B,OAAO,EAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAAU,EAAC,CAAC;AAC7C,CAAC","sourcesContent":["/**\n * Ontology sync and the shape check, as the server runs them before its shape sync: at boot and\n * when the host asks for a re-sync (`POST /admin/sync-shapes`).\n *\n * - The ontology sync (core `syncOntologies`) writes each registered package's ontology data into\n * its own named graph of the app's default dataset (`{package IRI}/graph/ontology`, with a\n * record in the graph registry that marks it as a hierarchy source), skipping a package whose\n * stored hash matches, so the store's RDFS view infers the class hierarchy. A store without\n * graph queries (it answers `unsupported-query-kind`, or has no `graphQuery`) gets core's\n * earlier sync of the `rdfs:subClassOf` triples into the default graph instead. The graphs the\n * sync wrote or dropped are observed on core's query dispatch and reported by name.\n * - The shape check (core `checkShapeHierarchy`) compares the registered shapes with that\n * hierarchy. A running app is never stopped by it: its findings are logged and counted.\n * - The in-memory class hierarchy (core `loadOntologyHierarchy`) is loaded also when the sync\n * is off; the sync loads it itself when it runs.\n * - Before either, every linked package directory the server discovered registers its ontologies\n * (core `registerInstalledOntologies`): a package the app only deep-imports shape files from\n * never evaluates its register modules, so its data would be missing from both. A source\n * package without built register modules (a workspace package in dev) registers through\n * `importModule` (Vite's `ssrLoadModule`), when the caller passes one.\n *\n * The four core modules (`ontology/syncOntologies`, `shapes/checkShapeHierarchy`,\n * `ontology/hierarchy`, `ontology/registerInstalled`) are server only and not in core's index, so\n * {@link loadOntologyCore} imports them by their fixed subpaths when the phase runs; the core\n * this server depends on has all of them. Kept apart from `LinkedServer` so the order and the failure handling can be tested without\n * booting a server.\n */\nimport {writtenAndUnchanged, type BootOrphanScope} from './syncShapes.js';\n\n/** The subset of core's ontology and shape-check API this module calls. */\nexport interface OntologyCore {\n syncOntologies(\n ds: unknown,\n options: {orphanScope: BootOrphanScope},\n ): Promise<{written: string[]; skipped: string[]; pruned: string[]; target?: string}>;\n checkShapeHierarchy(options: {dependencies?: Record<string, string[]>}): Promise<\n Array<{kind: string; severity: 'error' | 'warning'; shape?: string; iri?: string; message: string}>\n >;\n /** Loads the in-memory class hierarchy. Optional so a test double can leave it out. */\n loadOntologyHierarchy?: () => Promise<unknown>;\n /**\n * Calls `onGraph` for every graph query on `ds` that succeeds until the returned function is\n * called (core's `subscribeQueryDispatch`). Optional so a test double can leave it out.\n */\n observeGraphQueries?: (\n ds: unknown,\n onGraph: (query: {kind: string; graph: string}, settled: Promise<unknown>) => void,\n ) => () => void;\n /**\n * Registers the ontologies of the installed packages in the given directories. Optional so a\n * test double can leave it out.\n */\n registerInstalledOntologies?: (\n packageDirs: string[],\n options?: {importModule?: (file: string) => Promise<unknown>},\n ) => Promise<string[]>;\n}\n\n/** The shape check's findings by severity; `failed` holds the message of a check that threw. */\nexport interface ShapeCheckSummary {\n errors: number;\n warnings: number;\n failed?: string;\n}\n\n/** What the ontology sync and the shape check did, as `POST /admin/sync-shapes` reports it. */\nexport interface OntologyPhaseResult {\n /**\n * Packages whose ontology graph (or, on a store without graph queries, hierarchy) was written,\n * packages left as they were (unchanged or unreadable), and the graphs the sync wrote and\n * dropped (empty on a store without graph queries).\n */\n ontologies: {written: string[]; skipped: string[]; graphs?: string[]; droppedGraphs?: string[]};\n /**\n * The shape check's findings, counted by severity, and the message of the error the check\n * threw, if it did (`failed`). A thrown check is reported here and never fails the phase.\n */\n shapeCheck: ShapeCheckSummary;\n /** The error the ontology sync threw; the phase went on without it. */\n failure?: Error;\n}\n\n/**\n * Whether the server syncs shapes (and so ontologies) into its app data: off when `linked.config`\n * sets `syncShapesOnBoot: false` or the environment sets `LINKED_SYNC_SHAPES_ON_BOOT=false`. One\n * switch for both syncs, at boot and on the host's request.\n */\nexport function shapeSyncOn(\n env: Record<string, string | undefined>,\n configFlag: unknown,\n): boolean {\n return configFlag !== false && env.LINKED_SYNC_SHAPES_ON_BOOT !== 'false';\n}\n\n/**\n * Whether the boot syncs ontologies: when shape sync is on ({@link shapeSyncOn}) and the\n * environment does not opt out with `LINKED_SYNC_ONTOLOGIES_ON_BOOT=false`.\n */\nexport function bootSyncsOntologies(\n env: Record<string, string | undefined>,\n configFlag?: unknown,\n): boolean {\n return shapeSyncOn(env, configFlag) && env.LINKED_SYNC_ONTOLOGIES_ON_BOOT !== 'false';\n}\n\n/**\n * Import core's ontology sync, shape check, hierarchy loader and installed packages'\n * registration.\n */\nexport async function loadOntologyCore(): Promise<OntologyCore> {\n const sync = await import('@_linked/core/ontology/syncOntologies');\n const check = await import('@_linked/core/shapes/checkShapeHierarchy');\n const hierarchy = await import('@_linked/core/ontology/hierarchy');\n const registerInstalled = await import('@_linked/core/ontology/registerInstalled');\n const dispatch = await import('@_linked/core/queries/queryDispatch');\n return {\n syncOntologies: (ds, options) =>\n sync.syncOntologies(ds as Parameters<typeof sync.syncOntologies>[0], options),\n checkShapeHierarchy: (options) => check.checkShapeHierarchy(options),\n loadOntologyHierarchy: () => hierarchy.loadOntologyHierarchy(),\n registerInstalledOntologies: (packageDirs, options) =>\n registerInstalled.registerInstalledOntologies(packageDirs, options),\n observeGraphQueries: (ds, onGraph) =>\n dispatch.subscribeQueryDispatch((event) => {\n if (event.kind === 'graph' && event.target === ds) onGraph(event.query, event.result);\n }),\n };\n}\n\n/**\n * The package directories whose ontologies register before the sync: every linked package\n * discovered in the app's dependency tree, then the app's own workspace packages (an ontology\n * package in `packages/` that no package imports is one, so the dependency walk never reaches it).\n * Order kept, duplicates dropped.\n */\nexport function ontologyPackageDirs(\n linkedPackageDirs: Iterable<string>,\n workspacePackageDirs: Iterable<string>,\n): string[] {\n return [...new Set([...linkedPackageDirs, ...workspacePackageDirs])];\n}\n\n/**\n * In dev, the loader for source register modules: Vite's `ssrLoadModule`, so a workspace package\n * that was never built registers on the core the SSR graph uses. Undefined without Vite (a built\n * app), where source packages are skipped.\n */\nexport function sourceModuleImporter(\n vite: {ssrLoadModule?: (url: string) => Promise<unknown>} | undefined | null,\n): ((file: string) => Promise<unknown>) | undefined {\n const ssrLoadModule = vite?.ssrLoadModule;\n return typeof ssrLoadModule === 'function' ? (file) => ssrLoadModule.call(vite, file) : undefined;\n}\n\nconst asError = (err: unknown): Error => (err instanceof Error ? err : new Error(String(err)));\nconst list = (names: string[]) => (names.length ? names.join(', ') : 'none');\n\n/**\n * Register the ontologies of the installed linked packages in `packageDirs` (package roots), sync\n * the registered ontologies into `ds` (when `sync` is set and there is a dataset), then run the\n * shape check. Never throws: a failed registration is logged and the phase goes on; a failed sync\n * is logged and returned as `failure`, and the check still runs; a check that throws is logged and\n * returned as `shapeCheck.failed`. Logs one line for the sync, one summary line for\n * the check and one line per finding.\n */\nexport async function runOntologyPhase(\n ds: unknown,\n {\n core,\n sync,\n orphanScope,\n dependencies,\n packageDirs = [],\n importModule,\n log = console.log,\n warn = console.warn,\n }: {\n core: OntologyCore;\n sync: boolean;\n orphanScope: BootOrphanScope;\n dependencies?: Record<string, string[]>;\n /** Every linked package directory the server discovered (transitively). */\n packageDirs?: string[];\n /**\n * Imports a source register module (`src/ontologies/*.register.ts`) of a package directory\n * without built ones; in dev, Vite's `ssrLoadModule`.\n */\n importModule?: (file: string) => Promise<unknown>;\n log?: (message: string) => void;\n warn?: (message: string, err?: unknown) => void;\n },\n): Promise<OntologyPhaseResult> {\n const result: OntologyPhaseResult = {\n ontologies: {written: [], skipped: [], graphs: [], droppedGraphs: []},\n shapeCheck: {errors: 0, warnings: 0},\n };\n\n if (core.registerInstalledOntologies && packageDirs.length) {\n try {\n if (importModule) await core.registerInstalledOntologies(packageDirs, {importModule});\n else await core.registerInstalledOntologies(packageDirs);\n } catch (err) {\n warn(\"[LinkedServer] registering the installed packages' ontologies failed:\", err);\n }\n }\n\n let hierarchyLoaded = false;\n if (sync && ds) {\n // Every graph query that succeeded, in the order the sync sent them.\n const ops: Array<{kind: string; graph: string; ok?: boolean}> = [];\n const pending: Promise<unknown>[] = [];\n const stop = core.observeGraphQueries?.(ds, (query, settled) => {\n const op: {kind: string; graph: string; ok?: boolean} = {kind: query.kind, graph: query.graph};\n ops.push(op);\n pending.push(\n settled.then(\n () => (op.ok = true),\n () => undefined,\n ),\n );\n });\n try {\n const sync = await core.syncOntologies(ds, {orphanScope});\n await Promise.all(pending);\n const {graphs, droppedGraphs} = graphsWrittenAndDropped(ops.filter((op) => op.ok));\n result.ontologies = {written: sync.written, skipped: sync.skipped, graphs, droppedGraphs};\n hierarchyLoaded = true;\n // Core says where it wrote (the global `ontologyGraph`). A store without graph queries makes\n // the sync throw in the 'package' and shared modes, reported below as a failed sync.\n const where =\n sync.target === 'default'\n ? \" (default graph: ontologyGraph is 'default')\"\n : sync.target && sync.target !== 'package'\n ? ` (one shared graph <${sync.target}>)`\n : '';\n log(\n `[LinkedServer] ontology sync${where}: ` +\n writtenAndUnchanged(sync.written, sync.skipped) +\n (orphanScope === 'all' ? `; pruned ${list(sync.pruned)}` : '') +\n (graphs.length ? `; graphs written ${list(graphs)}` : '') +\n (droppedGraphs.length ? `; graphs dropped ${list(droppedGraphs)}` : ''),\n );\n } catch (err) {\n result.failure = asError(err);\n warn('[LinkedServer] ontology sync failed:', err);\n } finally {\n stop?.();\n }\n }\n if (!hierarchyLoaded && core.loadOntologyHierarchy) {\n try {\n await core.loadOntologyHierarchy();\n } catch (err) {\n warn('[LinkedServer] loading the class hierarchy failed:', err);\n }\n }\n\n try {\n const findings = await core.checkShapeHierarchy({dependencies});\n const errors = findings.filter((f) => f.severity === 'error').length;\n result.shapeCheck = {errors, warnings: findings.length - errors};\n if (findings.length) {\n log(`[LinkedServer] shape check: ${errors} error(s), ${findings.length - errors} warning(s)`);\n }\n for (const f of findings) warn(`[LinkedServer] shape check ${f.severity} (${f.kind}): ${f.message}`);\n } catch (err) {\n result.shapeCheck = {errors: 0, warnings: 0, failed: asError(err).message};\n warn('[LinkedServer] shape check failed:', err);\n }\n return result;\n}\n\n/**\n * The graphs a sync left written and those it left dropped, from its successful graph queries in\n * order. A graph the run created and then dropped (the inference probe's temporary graphs) is in\n * neither list.\n */\nexport function graphsWrittenAndDropped(ops: Array<{kind: string; graph: string}>): {\n graphs: string[];\n droppedGraphs: string[];\n} {\n const byGraph = new Map<string, string[]>();\n for (const {kind, graph} of ops) {\n const kinds = byGraph.get(graph) ?? [];\n kinds.push(kind);\n byGraph.set(graph, kinds);\n }\n const graphs: string[] = [];\n const droppedGraphs: string[] = [];\n for (const [graph, kinds] of byGraph) {\n const last = kinds[kinds.length - 1];\n if (last !== 'graph_drop') graphs.push(graph);\n else if (kinds[0] !== 'graph_create') droppedGraphs.push(graph);\n }\n return {graphs: graphs.sort(), droppedGraphs: droppedGraphs.sort()};\n}\n\n/**\n * The host's re-sync: the ontology phase, then the shape sync, which runs also when the ontology\n * sync failed. The ontology sync's failure is then thrown, so the route answers with it, as it\n * does for a failed shape sync. A shape check that threw fails nothing: the answer reports it as\n * `shapeCheck.failed`.\n */\nexport async function syncOntologiesThenShapes<T extends object>(\n ontologyPhase: () => Promise<OntologyPhaseResult>,\n shapeSync: () => Promise<T>,\n): Promise<T & Pick<OntologyPhaseResult, 'ontologies' | 'shapeCheck'>> {\n const {ontologies, shapeCheck, failure} = await ontologyPhase();\n const shapes = await shapeSync();\n if (failure) throw failure;\n return {...shapes, ontologies, shapeCheck};\n}\n"]}
|
|
@@ -114,6 +114,11 @@ export interface ShapeGraphsReport {
|
|
|
114
114
|
/** Set when the store has no graph queries, so the shapes went into the default graph instead of `target`. */
|
|
115
115
|
fallback?: true;
|
|
116
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* The head of a sync's log line: the packages written, then how many were unchanged (`skipped`
|
|
119
|
+
* in the syncs' results): `written a, b; 17 unchanged`, `written a, b`, or `19 unchanged`.
|
|
120
|
+
*/
|
|
121
|
+
export declare function writtenAndUnchanged(written: string[], unchanged: string[]): string;
|
|
117
122
|
/**
|
|
118
123
|
* Sync the registered shapes into `ds`, one shape graph per package (see the module doc), and log
|
|
119
124
|
* one line naming what was written and skipped, the graphs written, the packages without
|
|
@@ -100,6 +100,18 @@ export function viteShapesTurtleGenerator(vite) {
|
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
const list = (names) => (names.length ? names.join(', ') : 'none');
|
|
103
|
+
/**
|
|
104
|
+
* The head of a sync's log line: the packages written, then how many were unchanged (`skipped`
|
|
105
|
+
* in the syncs' results): `written a, b; 17 unchanged`, `written a, b`, or `19 unchanged`.
|
|
106
|
+
*/
|
|
107
|
+
export function writtenAndUnchanged(written, unchanged) {
|
|
108
|
+
const parts = written.length ? [`written ${written.join(', ')}`] : [];
|
|
109
|
+
if (unchanged.length || !written.length)
|
|
110
|
+
parts.push(`${unchanged.length} unchanged`);
|
|
111
|
+
return parts.join('; ');
|
|
112
|
+
}
|
|
113
|
+
/** An app graph's status in a log line: core's `skipped` is an unchanged graph. */
|
|
114
|
+
const appGraphStatus = (status) => (status === 'skipped' ? 'unchanged' : status);
|
|
103
115
|
/**
|
|
104
116
|
* Sync the registered shapes into `ds`, one shape graph per package (see the module doc), and log
|
|
105
117
|
* one line naming what was written and skipped, the graphs written, the packages without
|
|
@@ -177,9 +189,9 @@ export async function syncShapeGraphsInto(ds, { core, orphanScope, isOwn, dev, p
|
|
|
177
189
|
log(`[LinkedServer] shape sync (default graph: ${why}): ${report.defaultGraph} shape(s) written`);
|
|
178
190
|
}
|
|
179
191
|
else if (shared) {
|
|
180
|
-
log(`[LinkedServer] shape graph sync (one shared graph <${shared}>):
|
|
181
|
-
|
|
182
|
-
(report.appGraph ? `; app shapes ${report.appGraph.status}` : '') +
|
|
192
|
+
log(`[LinkedServer] shape graph sync (one shared graph <${shared}>): ` +
|
|
193
|
+
writtenAndUnchanged(report.written, report.skipped) +
|
|
194
|
+
(report.appGraph ? `; app shapes ${appGraphStatus(report.appGraph.status)}` : '') +
|
|
183
195
|
(report.pruned.length ? `; dropped from the graph ${list(report.pruned)}` : '') +
|
|
184
196
|
(withoutTurtle.length ? `; no shapes.ttl (not in the graph): ${list(withoutTurtle)}` : ''));
|
|
185
197
|
// Unlike package graphs, which stay when a package goes missing, the shared graph is rebuilt
|
|
@@ -190,8 +202,8 @@ export async function syncShapeGraphsInto(ds, { core, orphanScope, isOwn, dev, p
|
|
|
190
202
|
}
|
|
191
203
|
}
|
|
192
204
|
else {
|
|
193
|
-
log(`[LinkedServer] shape graph sync:
|
|
194
|
-
(report.appGraph ? `; app graph ${report.appGraph.status}` : '; no app graph (DATA_ROOT not set)') +
|
|
205
|
+
log(`[LinkedServer] shape graph sync: ${writtenAndUnchanged(report.written, report.skipped)}` +
|
|
206
|
+
(report.appGraph ? `; app graph ${appGraphStatus(report.appGraph.status)}` : '; no app graph (DATA_ROOT not set)') +
|
|
195
207
|
(report.graphs.length ? `; graphs written ${list(report.graphs)}` : '') +
|
|
196
208
|
(orphanScope === 'all' ? `; pruned ${list(report.pruned)}` : '') +
|
|
197
209
|
(report.cleaned ? `; ${report.cleaned} default-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E,WAAW,EAAC,CAAC;AAC7C,CAAC","sourcesContent":["/**\n * The shape sync, as the server runs it at boot and when the host asks for a re-sync\n * (`POST /admin/sync-shapes`): core's `syncShapeGraphs` writes each package's shapes into its own\n * named graph of the app's default dataset (`{package IRI}/graph/shapes`, a `ShapesGraph` record\n * in the graph registry), skipped when the stored content hash matches, and the app's generated\n * single-owner shapes into the app's own shape graph, `{DATA_ROOT}/graph/shapes`. Shapes an\n * earlier sync wrote into the default graph are removed there once their package's graph holds\n * them. A store without graph queries gets core's default-graph shape sync instead.\n *\n * Where a package's Turtle comes from ({@link shapeTurtleSource}):\n * - an installed package: its built `lib/esm/data/shapes.ttl` (written by `linked build`); a\n * package without that file gets no shape graph, and nothing is generated for it;\n * - the app's own packages (the app and its workspace packages): in dev, generated in process\n * from their source shapes, through Vite's loader, so the core the SSR graph registered the\n * shapes on writes them (the same bytes as `packageShapesTurtle`, which `linked build` writes);\n * in a built app, their `lib/esm/data/shapes.ttl` when there is one, else generated in process.\n *\n * Kept apart from `LinkedServer` so the choices and the log can be tested without booting a\n * server.\n */\nimport fs from 'fs';\nimport path from 'path';\n\n/** The orphan policies a booting server may run with. */\nexport type BootOrphanScope = 'all' | 'none';\n\n/**\n * The orphan policy for the shape sync.\n *\n * `'none'` unless the operator sets `LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true`, which drops the\n * shape graph of every package that is no longer registered in this process (never the app's own\n * graph).\n *\n * The registry is routinely a subset of what the dataset holds: shapes are deep-importable one\n * file at a time, an app's backend may import no shapes at all, and an app-data dataset also holds\n * shapes other writers put there. So nothing is pruned by default.\n */\nexport function bootOrphanScope(\n env: Record<string, string | undefined> = process.env,\n): BootOrphanScope {\n return env.LINKED_SYNC_SHAPES_PRUNE_ORPHANS === 'true' ? 'all' : 'none';\n}\n\n/** What core's `syncShapeGraphs` reports (`@_linked/core/shapes/syncShapeGraphs`). */\nexport interface ShapeGraphSyncResult {\n written: string[];\n skipped: string[];\n pruned: string[];\n cleaned: number;\n appGraph?: 'written' | 'skipped' | 'dropped';\n defaultGraph?: number;\n /** The global `shapeGraph` target the run wrote in (core ≥ 2.41): `'package'`, `'default'` or an IRI. */\n target?: string;\n /** Set when a store without graph queries took the default-graph sync instead of `target`. */\n fallback?: true;\n}\n\n/** The subset of core's shape graph API this module calls. */\nexport interface ShapeGraphCore {\n syncShapeGraphs(\n ds: unknown,\n options: {\n orphanScope: BootOrphanScope;\n packages?: string[];\n turtleFor: (packageName: string) => Promise<string | undefined>;\n appShapes?: {graph: string; package?: string};\n },\n ): Promise<ShapeGraphSyncResult>;\n /** Every package with registered shapes (core counted for its built-ins). */\n packagesWithShapes(): string[];\n /** A package's shape graph IRI (`ShapesGraph.iriFor`). */\n shapesGraphFor(packageName: string): string;\n}\n\n/** Import core's shape graph sync. */\nexport async function loadShapeGraphCore(): Promise<ShapeGraphCore> {\n const sync = await import('@_linked/core/shapes/syncShapeGraphs');\n const turtle = await import('@_linked/core/shapes/shapesToTurtle');\n const graphs = await import('@_linked/core/shapes/NamedGraph');\n return {\n syncShapeGraphs: (ds, options) =>\n sync.syncShapeGraphs(ds as Parameters<typeof sync.syncShapeGraphs>[0], options),\n packagesWithShapes: () => turtle.packagesWithShapes(),\n shapesGraphFor: (packageName) => graphs.ShapesGraph.iriFor(packageName),\n };\n}\n\n/** Where `linked build` writes a package's shapes, relative to the package root. */\nexport const SHAPES_TURTLE_FILE = path.join('lib', 'esm', 'data', 'shapes.ttl');\n\n/** A package's built `shapes.ttl`, or undefined when the package root has none. */\nexport function readShapesTurtle(packageDir: string | undefined): string | undefined {\n if (!packageDir) return undefined;\n try {\n return fs.readFileSync(path.join(packageDir, SHAPES_TURTLE_FILE), 'utf-8');\n } catch {\n return undefined;\n }\n}\n\n/** Where one package's Turtle comes from. */\nexport type ShapeTurtleSource =\n | {source: 'file'; text: string}\n | {source: 'generated'}\n | {source: 'none'};\n\n/**\n * Where a registered package's Turtle comes from: in dev an own package is generated; otherwise\n * its built file when it has one; else an own package is generated and an installed one has\n * none (no shape graph, no generation).\n */\nexport function shapeTurtleSource(\n packageName: string,\n {\n own,\n dev,\n packageDir,\n read = readShapesTurtle,\n }: {\n /** Whether the package is the app or one of its workspace packages. */\n own: boolean;\n /** Whether the server runs with Vite (dev). */\n dev: boolean;\n /** The package root, when the server knows it. */\n packageDir: string | undefined;\n read?: (packageDir: string | undefined) => string | undefined;\n },\n): ShapeTurtleSource {\n if (own && dev) return {source: 'generated'};\n const text = read(packageDir);\n if (text !== undefined) return {source: 'file', text};\n return own ? {source: 'generated'} : {source: 'none'};\n}\n\n/**\n * The app's own shape graph, `{DATA_ROOT}/graph/shapes`, or undefined when `DATA_ROOT` is not\n * an absolute IRI.\n */\nexport function appShapesGraph(\n env: Record<string, string | undefined> = process.env,\n): string | undefined {\n const root = env.DATA_ROOT?.trim().replace(/\\/+$/, '');\n if (!root || !/^[a-z][a-z0-9+.-]*:\\/\\/[^/]/i.test(root)) return undefined;\n return `${root}/graph/shapes`;\n}\n\n/**\n * In dev, the generator for the app's own packages: core's `packageShapesTurtle` imported through\n * Vite's `ssrLoadModule`, the core the SSR graph registered the source shapes on. Undefined\n * without Vite.\n */\nexport function viteShapesTurtleGenerator(\n vite: {ssrLoadModule?: (url: string) => Promise<unknown>} | undefined | null,\n): ((packageName: string) => Promise<string | undefined>) | undefined {\n const ssrLoadModule = vite?.ssrLoadModule;\n if (typeof ssrLoadModule !== 'function') return undefined;\n return async (packageName) => {\n const mod = (await ssrLoadModule.call(vite, '@_linked/core/shapes/shapesToTurtle')) as {\n packageShapesTurtle?: (name: string) => string | undefined;\n };\n if (typeof mod?.packageShapesTurtle !== 'function') {\n throw new Error('@_linked/core/shapes/shapesToTurtle has no packageShapesTurtle');\n }\n return mod.packageShapesTurtle(packageName);\n };\n}\n\n/** What one shape sync did, as the boot logs it and `POST /admin/sync-shapes` reports it. */\nexport interface ShapeGraphsReport {\n /** Packages whose shape graph was written, and those left as they were. */\n written: string[];\n skipped: string[];\n /** Packages no longer registered whose shape graph was dropped (`orphanScope: 'all'`). */\n pruned: string[];\n /** The shape graphs written (package graphs, then the app graph), by IRI. */\n graphs: string[];\n /** The app's own shape graph and what the sync did with it; absent without `DATA_ROOT`. */\n appGraph?: {graph: string; status: 'written' | 'skipped' | 'dropped'};\n /**\n * Registered packages with no Turtle, so with no shape graph: an installed package without\n * `shapes.ttl`, or an own package whose shapes could not be generated.\n */\n withoutTurtle: string[];\n /** Shapes an earlier sync had written into the default graph, removed now. */\n cleaned: number;\n /**\n * Set when the shapes went into the default graph (the global `shapeGraph` is `'default'`, or\n * it has no graph queries): the default-graph sync's writes.\n */\n defaultGraph?: number;\n /** The global `shapeGraph` target (`'package'`, `'default'` or a shared graph IRI), when core reports it. */\n target?: string;\n /** Set when the store has no graph queries, so the shapes went into the default graph instead of `target`. */\n fallback?: true;\n}\n\nconst list = (names: string[]) => (names.length ? names.join(', ') : 'none');\n\n/**\n * Sync the registered shapes into `ds`, one shape graph per package (see the module doc), and log\n * one line naming what was written and skipped, the graphs written, the packages without\n * `shapes.ttl` and the default-graph shapes removed. Throws when the sync fails.\n */\nexport async function syncShapeGraphsInto(\n ds: unknown,\n {\n core,\n orphanScope,\n isOwn,\n dev,\n packageDirOf,\n generate,\n appGraph,\n appPackage,\n read = readShapesTurtle,\n log = console.log,\n warn = console.warn,\n }: {\n core: ShapeGraphCore;\n orphanScope: BootOrphanScope;\n /** Whether a package is the app or one of its workspace packages. */\n isOwn: (packageName: string) => boolean;\n /** Whether the server runs with Vite (dev). */\n dev: boolean;\n /** A package's root directory, when the server knows it. */\n packageDirOf: (packageName: string) => string | undefined;\n /**\n * Generates an own package's Turtle (in dev through Vite's loader); undefined, or a result of\n * undefined, leaves the generation to the core that runs the sync.\n */\n generate?: (packageName: string) => Promise<string | undefined>;\n /** The app's own shape graph (`{DATA_ROOT}/graph/shapes`). */\n appGraph?: string;\n /** The app's package name, recorded on its shape graph's record. */\n appPackage?: string;\n read?: (packageDir: string | undefined) => string | undefined;\n log?: (message: string) => void;\n warn?: (message: string, err?: unknown) => void;\n },\n): Promise<ShapeGraphsReport> {\n const sources = new Map<string, ShapeTurtleSource>();\n for (const name of core.packagesWithShapes()) {\n sources.set(name, shapeTurtleSource(name, {own: isOwn(name), dev, packageDir: packageDirOf(name), read}));\n }\n const noFile = [...sources].filter(([, s]) => s.source === 'none').map(([name]) => name);\n\n const turtleFor = async (name: string): Promise<string | undefined> => {\n const source = sources.get(name);\n if (source?.source === 'file') return source.text;\n if (source?.source !== 'generated' || !generate) return undefined;\n try {\n return await generate(name);\n } catch (err) {\n warn(`[LinkedServer] generating the shapes of ${name} through the dev loader failed; trying the server's core instead:`, err);\n return undefined;\n }\n };\n\n const result = await core.syncShapeGraphs(ds, {\n orphanScope,\n // A package without Turtle is left out: core would generate it otherwise.\n ...(noFile.length ? {packages: [...sources.keys()].filter((n) => !noFile.includes(n))} : {}),\n turtleFor,\n ...(appGraph ? {appShapes: {graph: appGraph, ...(appPackage ? {package: appPackage} : {})}} : {}),\n });\n\n // A package core neither wrote nor skipped got no Turtle at all: generation produced nothing\n // (in dev, the server's core may not hold the source shapes the dev loader registered). The\n // default-graph sync (a store without graph queries) reports no packages, so it is not checked.\n const covered = new Set([...result.written, ...result.skipped]);\n const notGenerated =\n result.defaultGraph !== undefined\n ? []\n : [...sources.keys()].filter((n) => !noFile.includes(n) && !covered.has(n));\n if (notGenerated.length) {\n warn(\n `[LinkedServer] no shapes could be generated for ${list(notGenerated.sort())}, so ` +\n `${notGenerated.length === 1 ? 'it has' : 'they have'} no shape graph this sync`,\n );\n }\n const withoutTurtle = [...noFile, ...notGenerated].sort();\n\n // A shared graph (the global `shapeGraph` is an IRI) holds every package and the app's shapes.\n const shared = result.target && result.target !== 'package' && result.target !== 'default' ? result.target : undefined;\n const report: ShapeGraphsReport = {\n written: result.written,\n skipped: result.skipped,\n pruned: result.pruned,\n // A shared graph is written whole or not at all: listed when the run wrote anything into it.\n graphs: shared\n ? result.written.length || result.appGraph === 'written' || result.pruned.length\n ? [shared]\n : []\n : result.written.map((name) => core.shapesGraphFor(name)),\n withoutTurtle,\n cleaned: result.cleaned,\n };\n if (result.target) report.target = result.target;\n if (result.fallback) report.fallback = true;\n if (appGraph && result.appGraph) {\n report.appGraph = {graph: shared ?? appGraph, status: result.appGraph};\n if (result.appGraph === 'written' && !shared) report.graphs.push(appGraph);\n }\n if (result.defaultGraph !== undefined) report.defaultGraph = result.defaultGraph;\n\n if (report.defaultGraph !== undefined) {\n const why =\n result.target === 'default' && !result.fallback\n ? \"shapeGraph is 'default'\"\n : 'the store has no graph queries';\n log(`[LinkedServer] shape sync (default graph: ${why}): ${report.defaultGraph} shape(s) written`);\n } else if (shared) {\n log(\n `[LinkedServer] shape graph sync (one shared graph <${shared}>): written ${list(report.written)}; ` +\n `skipped ${list(report.skipped)}` +\n (report.appGraph ? `; app shapes ${report.appGraph.status}` : '') +\n (report.pruned.length ? `; dropped from the graph ${list(report.pruned)}` : '') +\n (withoutTurtle.length ? `; no shapes.ttl (not in the graph): ${list(withoutTurtle)}` : ''),\n );\n // Unlike package graphs, which stay when a package goes missing, the shared graph is rebuilt\n // from the packages the run covers.\n if (withoutTurtle.length) {\n warn(\n `[LinkedServer] the shared shape graph <${shared}> holds no shapes of ${list(withoutTurtle)}: ` +\n 'a package without Turtle in a run is left out of the rebuilt graph',\n );\n }\n } else {\n log(\n `[LinkedServer] shape graph sync: written ${list(report.written)}; skipped ${list(report.skipped)}` +\n (report.appGraph ? `; app graph ${report.appGraph.status}` : '; no app graph (DATA_ROOT not set)') +\n (report.graphs.length ? `; graphs written ${list(report.graphs)}` : '') +\n (orphanScope === 'all' ? `; pruned ${list(report.pruned)}` : '') +\n (report.cleaned ? `; ${report.cleaned} default-graph shape(s) removed` : '') +\n (withoutTurtle.length ? `; no shapes.ttl (no shape graph): ${list(withoutTurtle)}` : ''),\n );\n }\n if (orphanScope === 'all' && withoutTurtle.length && !shared) {\n warn(\n '[LinkedServer] orphaned shape graphs were not pruned: core prunes only when the sync covers ' +\n `every registered package, and ${list(withoutTurtle)} has no Turtle`,\n );\n }\n return report;\n}\n\n/**\n * @deprecated The default-graph shape sync's callback type; the server now syncs shape graphs with\n * {@link syncShapeGraphsInto}. Kept so code built against an earlier server still compiles.\n */\nexport type SyncShapesFn = (\n ds: unknown,\n options: {orphanScope: BootOrphanScope},\n) => Promise<Array<() => Promise<void>>>;\n\n/**\n * @deprecated Use {@link syncShapeGraphsInto}, which writes each package's shapes into its own\n * shape graph. This runs the old default-graph sync: it plans every registered shape with\n * `syncShapes` (the orphan policy from {@link bootOrphanScope}) and runs the writes in batches\n * of 8. Kept so code built against an earlier server keeps working.\n */\nexport async function materializeShapes(\n ds: unknown,\n syncShapes: SyncShapesFn,\n {\n env = process.env,\n log = console.log,\n }: {\n env?: Record<string, string | undefined>;\n log?: (message: string) => void;\n } = {},\n): Promise<{count: number; orphanScope: BootOrphanScope}> {\n const orphanScope = bootOrphanScope(env);\n const thunks = await syncShapes(ds, {orphanScope});\n for (let i = 0; i < thunks.length; i += 8) {\n await Promise.all(thunks.slice(i, i + 8).map((run) => run()));\n }\n log(\n `[LinkedServer] synced shapes into the default graph with orphanScope '${orphanScope}': ` +\n `${thunks.length} operation(s)`,\n );\n return {count: thunks.length, orphanScope};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"syncShapes.js","sourceRoot":"","sources":["../../../src/utils/syncShapes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAKxB;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe,CAC7B,MAA0C,OAAO,CAAC,GAAG;IAErD,OAAO,GAAG,CAAC,gCAAgC,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;AAC1E,CAAC;AAiCD,sCAAsC;AACtC,MAAM,CAAC,KAAK,UAAU,kBAAkB;IACtC,MAAM,IAA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AM,eAAe,CAClC,CAAC;IACF,OAAO,EAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,EAAC,CAAC;AAC7C,CAAC","sourcesContent":["/**\n * The shape sync, as the server runs it at boot and when the host asks for a re-sync\n * (`POST /admin/sync-shapes`): core's `syncShapeGraphs` writes each package's shapes into its own\n * named graph of the app's default dataset (`{package IRI}/graph/shapes`, a `ShapesGraph` record\n * in the graph registry), skipped when the stored content hash matches, and the app's generated\n * single-owner shapes into the app's own shape graph, `{DATA_ROOT}/graph/shapes`. Shapes an\n * earlier sync wrote into the default graph are removed there once their package's graph holds\n * them. A store without graph queries gets core's default-graph shape sync instead.\n *\n * Where a package's Turtle comes from ({@link shapeTurtleSource}):\n * - an installed package: its built `lib/esm/data/shapes.ttl` (written by `linked build`); a\n * package without that file gets no shape graph, and nothing is generated for it;\n * - the app's own packages (the app and its workspace packages): in dev, generated in process\n * from their source shapes, through Vite's loader, so the core the SSR graph registered the\n * shapes on writes them (the same bytes as `packageShapesTurtle`, which `linked build` writes);\n * in a built app, their `lib/esm/data/shapes.ttl` when there is one, else generated in process.\n *\n * Kept apart from `LinkedServer` so the choices and the log can be tested without booting a\n * server.\n */\nimport fs from 'fs';\nimport path from 'path';\n\n/** The orphan policies a booting server may run with. */\nexport type BootOrphanScope = 'all' | 'none';\n\n/**\n * The orphan policy for the shape sync.\n *\n * `'none'` unless the operator sets `LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true`, which drops the\n * shape graph of every package that is no longer registered in this process (never the app's own\n * graph).\n *\n * The registry is routinely a subset of what the dataset holds: shapes are deep-importable one\n * file at a time, an app's backend may import no shapes at all, and an app-data dataset also holds\n * shapes other writers put there. So nothing is pruned by default.\n */\nexport function bootOrphanScope(\n env: Record<string, string | undefined> = process.env,\n): BootOrphanScope {\n return env.LINKED_SYNC_SHAPES_PRUNE_ORPHANS === 'true' ? 'all' : 'none';\n}\n\n/** What core's `syncShapeGraphs` reports (`@_linked/core/shapes/syncShapeGraphs`). */\nexport interface ShapeGraphSyncResult {\n written: string[];\n skipped: string[];\n pruned: string[];\n cleaned: number;\n appGraph?: 'written' | 'skipped' | 'dropped';\n defaultGraph?: number;\n /** The global `shapeGraph` target the run wrote in (core ≥ 2.41): `'package'`, `'default'` or an IRI. */\n target?: string;\n /** Set when a store without graph queries took the default-graph sync instead of `target`. */\n fallback?: true;\n}\n\n/** The subset of core's shape graph API this module calls. */\nexport interface ShapeGraphCore {\n syncShapeGraphs(\n ds: unknown,\n options: {\n orphanScope: BootOrphanScope;\n packages?: string[];\n turtleFor: (packageName: string) => Promise<string | undefined>;\n appShapes?: {graph: string; package?: string};\n },\n ): Promise<ShapeGraphSyncResult>;\n /** Every package with registered shapes (core counted for its built-ins). */\n packagesWithShapes(): string[];\n /** A package's shape graph IRI (`ShapesGraph.iriFor`). */\n shapesGraphFor(packageName: string): string;\n}\n\n/** Import core's shape graph sync. */\nexport async function loadShapeGraphCore(): Promise<ShapeGraphCore> {\n const sync = await import('@_linked/core/shapes/syncShapeGraphs');\n const turtle = await import('@_linked/core/shapes/shapesToTurtle');\n const graphs = await import('@_linked/core/shapes/NamedGraph');\n return {\n syncShapeGraphs: (ds, options) =>\n sync.syncShapeGraphs(ds as Parameters<typeof sync.syncShapeGraphs>[0], options),\n packagesWithShapes: () => turtle.packagesWithShapes(),\n shapesGraphFor: (packageName) => graphs.ShapesGraph.iriFor(packageName),\n };\n}\n\n/** Where `linked build` writes a package's shapes, relative to the package root. */\nexport const SHAPES_TURTLE_FILE = path.join('lib', 'esm', 'data', 'shapes.ttl');\n\n/** A package's built `shapes.ttl`, or undefined when the package root has none. */\nexport function readShapesTurtle(packageDir: string | undefined): string | undefined {\n if (!packageDir) return undefined;\n try {\n return fs.readFileSync(path.join(packageDir, SHAPES_TURTLE_FILE), 'utf-8');\n } catch {\n return undefined;\n }\n}\n\n/** Where one package's Turtle comes from. */\nexport type ShapeTurtleSource =\n | {source: 'file'; text: string}\n | {source: 'generated'}\n | {source: 'none'};\n\n/**\n * Where a registered package's Turtle comes from: in dev an own package is generated; otherwise\n * its built file when it has one; else an own package is generated and an installed one has\n * none (no shape graph, no generation).\n */\nexport function shapeTurtleSource(\n packageName: string,\n {\n own,\n dev,\n packageDir,\n read = readShapesTurtle,\n }: {\n /** Whether the package is the app or one of its workspace packages. */\n own: boolean;\n /** Whether the server runs with Vite (dev). */\n dev: boolean;\n /** The package root, when the server knows it. */\n packageDir: string | undefined;\n read?: (packageDir: string | undefined) => string | undefined;\n },\n): ShapeTurtleSource {\n if (own && dev) return {source: 'generated'};\n const text = read(packageDir);\n if (text !== undefined) return {source: 'file', text};\n return own ? {source: 'generated'} : {source: 'none'};\n}\n\n/**\n * The app's own shape graph, `{DATA_ROOT}/graph/shapes`, or undefined when `DATA_ROOT` is not\n * an absolute IRI.\n */\nexport function appShapesGraph(\n env: Record<string, string | undefined> = process.env,\n): string | undefined {\n const root = env.DATA_ROOT?.trim().replace(/\\/+$/, '');\n if (!root || !/^[a-z][a-z0-9+.-]*:\\/\\/[^/]/i.test(root)) return undefined;\n return `${root}/graph/shapes`;\n}\n\n/**\n * In dev, the generator for the app's own packages: core's `packageShapesTurtle` imported through\n * Vite's `ssrLoadModule`, the core the SSR graph registered the source shapes on. Undefined\n * without Vite.\n */\nexport function viteShapesTurtleGenerator(\n vite: {ssrLoadModule?: (url: string) => Promise<unknown>} | undefined | null,\n): ((packageName: string) => Promise<string | undefined>) | undefined {\n const ssrLoadModule = vite?.ssrLoadModule;\n if (typeof ssrLoadModule !== 'function') return undefined;\n return async (packageName) => {\n const mod = (await ssrLoadModule.call(vite, '@_linked/core/shapes/shapesToTurtle')) as {\n packageShapesTurtle?: (name: string) => string | undefined;\n };\n if (typeof mod?.packageShapesTurtle !== 'function') {\n throw new Error('@_linked/core/shapes/shapesToTurtle has no packageShapesTurtle');\n }\n return mod.packageShapesTurtle(packageName);\n };\n}\n\n/** What one shape sync did, as the boot logs it and `POST /admin/sync-shapes` reports it. */\nexport interface ShapeGraphsReport {\n /** Packages whose shape graph was written, and those left as they were. */\n written: string[];\n skipped: string[];\n /** Packages no longer registered whose shape graph was dropped (`orphanScope: 'all'`). */\n pruned: string[];\n /** The shape graphs written (package graphs, then the app graph), by IRI. */\n graphs: string[];\n /** The app's own shape graph and what the sync did with it; absent without `DATA_ROOT`. */\n appGraph?: {graph: string; status: 'written' | 'skipped' | 'dropped'};\n /**\n * Registered packages with no Turtle, so with no shape graph: an installed package without\n * `shapes.ttl`, or an own package whose shapes could not be generated.\n */\n withoutTurtle: string[];\n /** Shapes an earlier sync had written into the default graph, removed now. */\n cleaned: number;\n /**\n * Set when the shapes went into the default graph (the global `shapeGraph` is `'default'`, or\n * it has no graph queries): the default-graph sync's writes.\n */\n defaultGraph?: number;\n /** The global `shapeGraph` target (`'package'`, `'default'` or a shared graph IRI), when core reports it. */\n target?: string;\n /** Set when the store has no graph queries, so the shapes went into the default graph instead of `target`. */\n fallback?: true;\n}\n\nconst list = (names: string[]) => (names.length ? names.join(', ') : 'none');\n\n/**\n * The head of a sync's log line: the packages written, then how many were unchanged (`skipped`\n * in the syncs' results): `written a, b; 17 unchanged`, `written a, b`, or `19 unchanged`.\n */\nexport function writtenAndUnchanged(written: string[], unchanged: string[]): string {\n const parts = written.length ? [`written ${written.join(', ')}`] : [];\n if (unchanged.length || !written.length) parts.push(`${unchanged.length} unchanged`);\n return parts.join('; ');\n}\n\n/** An app graph's status in a log line: core's `skipped` is an unchanged graph. */\nconst appGraphStatus = (status: 'written' | 'skipped' | 'dropped') => (status === 'skipped' ? 'unchanged' : status);\n\n/**\n * Sync the registered shapes into `ds`, one shape graph per package (see the module doc), and log\n * one line naming what was written and skipped, the graphs written, the packages without\n * `shapes.ttl` and the default-graph shapes removed. Throws when the sync fails.\n */\nexport async function syncShapeGraphsInto(\n ds: unknown,\n {\n core,\n orphanScope,\n isOwn,\n dev,\n packageDirOf,\n generate,\n appGraph,\n appPackage,\n read = readShapesTurtle,\n log = console.log,\n warn = console.warn,\n }: {\n core: ShapeGraphCore;\n orphanScope: BootOrphanScope;\n /** Whether a package is the app or one of its workspace packages. */\n isOwn: (packageName: string) => boolean;\n /** Whether the server runs with Vite (dev). */\n dev: boolean;\n /** A package's root directory, when the server knows it. */\n packageDirOf: (packageName: string) => string | undefined;\n /**\n * Generates an own package's Turtle (in dev through Vite's loader); undefined, or a result of\n * undefined, leaves the generation to the core that runs the sync.\n */\n generate?: (packageName: string) => Promise<string | undefined>;\n /** The app's own shape graph (`{DATA_ROOT}/graph/shapes`). */\n appGraph?: string;\n /** The app's package name, recorded on its shape graph's record. */\n appPackage?: string;\n read?: (packageDir: string | undefined) => string | undefined;\n log?: (message: string) => void;\n warn?: (message: string, err?: unknown) => void;\n },\n): Promise<ShapeGraphsReport> {\n const sources = new Map<string, ShapeTurtleSource>();\n for (const name of core.packagesWithShapes()) {\n sources.set(name, shapeTurtleSource(name, {own: isOwn(name), dev, packageDir: packageDirOf(name), read}));\n }\n const noFile = [...sources].filter(([, s]) => s.source === 'none').map(([name]) => name);\n\n const turtleFor = async (name: string): Promise<string | undefined> => {\n const source = sources.get(name);\n if (source?.source === 'file') return source.text;\n if (source?.source !== 'generated' || !generate) return undefined;\n try {\n return await generate(name);\n } catch (err) {\n warn(`[LinkedServer] generating the shapes of ${name} through the dev loader failed; trying the server's core instead:`, err);\n return undefined;\n }\n };\n\n const result = await core.syncShapeGraphs(ds, {\n orphanScope,\n // A package without Turtle is left out: core would generate it otherwise.\n ...(noFile.length ? {packages: [...sources.keys()].filter((n) => !noFile.includes(n))} : {}),\n turtleFor,\n ...(appGraph ? {appShapes: {graph: appGraph, ...(appPackage ? {package: appPackage} : {})}} : {}),\n });\n\n // A package core neither wrote nor skipped got no Turtle at all: generation produced nothing\n // (in dev, the server's core may not hold the source shapes the dev loader registered). The\n // default-graph sync (a store without graph queries) reports no packages, so it is not checked.\n const covered = new Set([...result.written, ...result.skipped]);\n const notGenerated =\n result.defaultGraph !== undefined\n ? []\n : [...sources.keys()].filter((n) => !noFile.includes(n) && !covered.has(n));\n if (notGenerated.length) {\n warn(\n `[LinkedServer] no shapes could be generated for ${list(notGenerated.sort())}, so ` +\n `${notGenerated.length === 1 ? 'it has' : 'they have'} no shape graph this sync`,\n );\n }\n const withoutTurtle = [...noFile, ...notGenerated].sort();\n\n // A shared graph (the global `shapeGraph` is an IRI) holds every package and the app's shapes.\n const shared = result.target && result.target !== 'package' && result.target !== 'default' ? result.target : undefined;\n const report: ShapeGraphsReport = {\n written: result.written,\n skipped: result.skipped,\n pruned: result.pruned,\n // A shared graph is written whole or not at all: listed when the run wrote anything into it.\n graphs: shared\n ? result.written.length || result.appGraph === 'written' || result.pruned.length\n ? [shared]\n : []\n : result.written.map((name) => core.shapesGraphFor(name)),\n withoutTurtle,\n cleaned: result.cleaned,\n };\n if (result.target) report.target = result.target;\n if (result.fallback) report.fallback = true;\n if (appGraph && result.appGraph) {\n report.appGraph = {graph: shared ?? appGraph, status: result.appGraph};\n if (result.appGraph === 'written' && !shared) report.graphs.push(appGraph);\n }\n if (result.defaultGraph !== undefined) report.defaultGraph = result.defaultGraph;\n\n if (report.defaultGraph !== undefined) {\n const why =\n result.target === 'default' && !result.fallback\n ? \"shapeGraph is 'default'\"\n : 'the store has no graph queries';\n log(`[LinkedServer] shape sync (default graph: ${why}): ${report.defaultGraph} shape(s) written`);\n } else if (shared) {\n log(\n `[LinkedServer] shape graph sync (one shared graph <${shared}>): ` +\n writtenAndUnchanged(report.written, report.skipped) +\n (report.appGraph ? `; app shapes ${appGraphStatus(report.appGraph.status)}` : '') +\n (report.pruned.length ? `; dropped from the graph ${list(report.pruned)}` : '') +\n (withoutTurtle.length ? `; no shapes.ttl (not in the graph): ${list(withoutTurtle)}` : ''),\n );\n // Unlike package graphs, which stay when a package goes missing, the shared graph is rebuilt\n // from the packages the run covers.\n if (withoutTurtle.length) {\n warn(\n `[LinkedServer] the shared shape graph <${shared}> holds no shapes of ${list(withoutTurtle)}: ` +\n 'a package without Turtle in a run is left out of the rebuilt graph',\n );\n }\n } else {\n log(\n `[LinkedServer] shape graph sync: ${writtenAndUnchanged(report.written, report.skipped)}` +\n (report.appGraph ? `; app graph ${appGraphStatus(report.appGraph.status)}` : '; no app graph (DATA_ROOT not set)') +\n (report.graphs.length ? `; graphs written ${list(report.graphs)}` : '') +\n (orphanScope === 'all' ? `; pruned ${list(report.pruned)}` : '') +\n (report.cleaned ? `; ${report.cleaned} default-graph shape(s) removed` : '') +\n (withoutTurtle.length ? `; no shapes.ttl (no shape graph): ${list(withoutTurtle)}` : ''),\n );\n }\n if (orphanScope === 'all' && withoutTurtle.length && !shared) {\n warn(\n '[LinkedServer] orphaned shape graphs were not pruned: core prunes only when the sync covers ' +\n `every registered package, and ${list(withoutTurtle)} has no Turtle`,\n );\n }\n return report;\n}\n\n/**\n * @deprecated The default-graph shape sync's callback type; the server now syncs shape graphs with\n * {@link syncShapeGraphsInto}. Kept so code built against an earlier server still compiles.\n */\nexport type SyncShapesFn = (\n ds: unknown,\n options: {orphanScope: BootOrphanScope},\n) => Promise<Array<() => Promise<void>>>;\n\n/**\n * @deprecated Use {@link syncShapeGraphsInto}, which writes each package's shapes into its own\n * shape graph. This runs the old default-graph sync: it plans every registered shape with\n * `syncShapes` (the orphan policy from {@link bootOrphanScope}) and runs the writes in batches\n * of 8. Kept so code built against an earlier server keeps working.\n */\nexport async function materializeShapes(\n ds: unknown,\n syncShapes: SyncShapesFn,\n {\n env = process.env,\n log = console.log,\n }: {\n env?: Record<string, string | undefined>;\n log?: (message: string) => void;\n } = {},\n): Promise<{count: number; orphanScope: BootOrphanScope}> {\n const orphanScope = bootOrphanScope(env);\n const thunks = await syncShapes(ds, {orphanScope});\n for (let i = 0; i < thunks.length; i += 8) {\n await Promise.all(thunks.slice(i, i + 8).map((run) => run()));\n }\n log(\n `[LinkedServer] synced shapes into the default graph with orphanScope '${orphanScope}': ` +\n `${thunks.length} operation(s)`,\n );\n return {count: thunks.length, orphanScope};\n}\n"]}
|
package/package.json
CHANGED
|
@@ -10,9 +10,8 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "git+https://github.com/linked-fw/server.git"
|
|
12
12
|
},
|
|
13
|
-
"version": "3.
|
|
13
|
+
"version": "3.13.0",
|
|
14
14
|
"packageManager": "npm@12.2.0",
|
|
15
|
-
"linkedPackage": true,
|
|
16
15
|
"type": "module",
|
|
17
16
|
"main": "lib/esm/index.js",
|
|
18
17
|
"types": "index.d.ts",
|
|
@@ -39,8 +38,8 @@
|
|
|
39
38
|
"web3"
|
|
40
39
|
],
|
|
41
40
|
"dependencies": {
|
|
42
|
-
"@_linked/cli": "^1.
|
|
43
|
-
"@_linked/core": "^2.
|
|
41
|
+
"@_linked/cli": "^1.58.0",
|
|
42
|
+
"@_linked/core": "^2.45.0",
|
|
44
43
|
"@_linked/server-utils": "^1.16.1",
|
|
45
44
|
"autoprefixer": "^10.4.18",
|
|
46
45
|
"compression": "^1.7.4",
|
|
@@ -91,6 +90,7 @@
|
|
|
91
90
|
}
|
|
92
91
|
},
|
|
93
92
|
"linked": {
|
|
93
|
+
"kind": "library",
|
|
94
94
|
"serverOnly": [
|
|
95
95
|
".",
|
|
96
96
|
"./backend",
|
package/readme.md
CHANGED
|
@@ -323,7 +323,7 @@ export interface RemoteDatasetConfig {
|
|
|
323
323
|
token?: string; // sent as Authorization: Bearer <token> when set and url is set
|
|
324
324
|
name?: string; // id = ${DATA_ROOT}/backend-api-store/<name>; does not affect routing
|
|
325
325
|
id?: string;
|
|
326
|
-
|
|
326
|
+
forwardUserToken?: boolean | string; // url form: send the signed-in user's access token (default true)
|
|
327
327
|
}
|
|
328
328
|
```
|
|
329
329
|
|
|
@@ -353,22 +353,32 @@ semantics are under [Error semantics](#error-semantics). `token` is ignored.
|
|
|
353
353
|
`deleteQuery`) with `Content-Type` and `Accept` set to `application/json` and, when `token` is
|
|
354
354
|
non-empty, `Authorization: Bearer <token>`. It resolves the parsed body; `null` is a valid
|
|
355
355
|
result. Nothing is retried, so a write is never applied twice.
|
|
356
|
-
- **The
|
|
357
|
-
answered (the call context is `http`
|
|
358
|
-
`
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
356
|
+
- **The user's token.** When the query is made while a signed-in user's HTTP request is being
|
|
357
|
+
answered (the call context is `http`), the post asks the hook on
|
|
358
|
+
`globalThis[Symbol.for('@_linked/auth:userTokenHeaders')]` (exported as
|
|
359
|
+
`USER_TOKEN_HEADERS_HOOK`; `@_linked/auth`'s backend installs it) with the request and
|
|
360
|
+
`{method: 'POST', url}` (the exact URL posted to). When it answers, the post also carries
|
|
361
|
+
`x-linked-user-token: <the user's Solid-OIDC access token>` (exported as `USER_TOKEN_HEADER`) and
|
|
362
|
+
`DPoP: <a proof for this one request>`, beside the bearer. auth answers for a session signed in
|
|
363
|
+
through an issuer that gave a DPoP-bound JWT access token (webid.email does), while the token has
|
|
364
|
+
more than 30 seconds left; other sessions, a system context (`runAsSystem`), an anonymous request
|
|
365
|
+
and code outside any call send neither. A hook that throws or answers something else sends
|
|
366
|
+
neither (one warning per process) and the query still goes. The token still decides which data
|
|
367
|
+
the store reaches; the data server verifies the user's token and proof (`@_linked/auth`'s
|
|
368
|
+
`verifySolidAccessToken`) and acts as that user. `forwardUserToken: false` (or the string
|
|
369
|
+
`"false"`) turns it off. No other headers are sent.
|
|
370
|
+
|
|
371
|
+
> **Warning: on by default.** Every query of a signed-in user's request sends that user's access
|
|
372
|
+
> token to the data server at `url`. Each proof is for one method and URL and the server cannot
|
|
373
|
+
> make new ones (the key stays with the app), but it learns who the user is. Point `url` only at
|
|
374
|
+
> a server you trust with your users' identities (for an app CN hosts, CN's gateway). To send
|
|
375
|
+
> none, set it off in the dataset's config:
|
|
368
376
|
>
|
|
369
377
|
> ```json
|
|
370
|
-
> "config": { "url": "${DATA_SERVER_URL:-}", "token": "${DATA_SERVER_TOKEN:-}", "
|
|
378
|
+
> "config": { "url": "${DATA_SERVER_URL:-}", "token": "${DATA_SERVER_TOKEN:-}", "forwardUserToken": false }
|
|
371
379
|
> ```
|
|
380
|
+
- A redirect is never followed (`redirect: 'manual'`): a 3xx answer is a `RemoteDatasetError`
|
|
381
|
+
with its status, so the bearer and the user's token go to `url` only.
|
|
372
382
|
- Trailing slashes are trimmed from `url`. A `url` that is present but empty (an unset
|
|
373
383
|
`${DATA_SERVER_URL:-}`) never falls back to the no-url form: `init()` and every method throw
|
|
374
384
|
`RemoteDatasetUnreachableError` with `url: ''`.
|