@_linked/server 3.8.0 → 3.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/CHANGELOG.md +71 -0
- package/lib/esm/data/shapes.ttl +26 -0
- package/lib/esm/shapes/LinkedServer.d.ts +20 -16
- package/lib/esm/shapes/LinkedServer.js +55 -28
- package/lib/esm/shapes/LinkedServer.js.map +1 -1
- package/lib/esm/shapes/quadstores/RemoteDataset.d.ts +1 -1
- package/lib/esm/shapes/quadstores/RemoteDataset.js +1 -1
- package/lib/esm/shapes/quadstores/RemoteDataset.js.map +1 -1
- package/lib/esm/utils/adminChannel.d.ts +24 -2
- package/lib/esm/utils/adminChannel.js +40 -13
- package/lib/esm/utils/adminChannel.js.map +1 -1
- package/lib/esm/utils/effectiveRoutes.d.ts +26 -0
- package/lib/esm/utils/effectiveRoutes.js +40 -0
- package/lib/esm/utils/effectiveRoutes.js.map +1 -0
- package/lib/esm/utils/ontologySync.d.ts +2 -2
- package/lib/esm/utils/ontologySync.js +2 -2
- package/lib/esm/utils/ontologySync.js.map +1 -1
- package/lib/esm/utils/queryPlane.js +9 -3
- package/lib/esm/utils/queryPlane.js.map +1 -1
- package/lib/esm/utils/syncShapes.d.ts +136 -26
- package/lib/esm/utils/syncShapes.js +175 -26
- package/lib/esm/utils/syncShapes.js.map +1 -1
- package/package.json +3 -3
- package/readme.md +38 -3
|
@@ -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,E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,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 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[]}>;\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 (a CN clone's\n * `packages/<slug>-ont` is one no package imports, 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 const written = new Set<string>();\n const dropped = new Set<string>();\n const pending: Promise<unknown>[] = [];\n const stop = core.observeGraphQueries?.(ds, (query, settled) => {\n pending.push(\n settled.then(\n () => (query.kind === 'graph_drop' ? dropped : written).add(query.graph),\n () => undefined,\n ),\n );\n });\n try {\n const sync = await core.syncOntologies(ds, {orphanScope});\n await Promise.all(pending);\n const graphs = [...written].sort();\n const droppedGraphs = [...dropped].sort();\n result.ontologies = {written: sync.written, skipped: sync.skipped, graphs, droppedGraphs};\n hierarchyLoaded = true;\n const defaultGraph = !!stop && sync.written.length > 0 && graphs.length === 0;\n log(\n `[LinkedServer] ontology sync${defaultGraph ? ' (default graph: the store has no graph queries)' : ''}: ` +\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 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":"AAwEA;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,GAAuC,EACvC,UAAmB;IAEnB,OAAO,UAAU,KAAK,KAAK,IAAI,GAAG,CAAC,0BAA0B,KAAK,OAAO,CAAC;AAC5E,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,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 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[]}>;\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 const written = new Set<string>();\n const dropped = new Set<string>();\n const pending: Promise<unknown>[] = [];\n const stop = core.observeGraphQueries?.(ds, (query, settled) => {\n pending.push(\n settled.then(\n () => (query.kind === 'graph_drop' ? dropped : written).add(query.graph),\n () => undefined,\n ),\n );\n });\n try {\n const sync = await core.syncOntologies(ds, {orphanScope});\n await Promise.all(pending);\n const graphs = [...written].sort();\n const droppedGraphs = [...dropped].sort();\n result.ontologies = {written: sync.written, skipped: sync.skipped, graphs, droppedGraphs};\n hierarchyLoaded = true;\n const defaultGraph = !!stop && sync.written.length > 0 && graphs.length === 0;\n log(\n `[LinkedServer] ontology sync${defaultGraph ? ' (default graph: the store has no graph queries)' : ''}: ` +\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 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"]}
|
|
@@ -208,12 +208,16 @@ export async function authorizeGraphQuery(query, ir, endpoint) {
|
|
|
208
208
|
endpoint,
|
|
209
209
|
});
|
|
210
210
|
}
|
|
211
|
-
/**
|
|
211
|
+
/**
|
|
212
|
+
* The HTTP status each graph query refusal is answered with. A code this map does not know (a
|
|
213
|
+
* newer core than the one this server was built against) is answered with 400, never 500.
|
|
214
|
+
*/
|
|
212
215
|
const GRAPH_REFUSAL_STATUS = {
|
|
213
216
|
'unsupported-query-kind': 501,
|
|
214
217
|
'graph-name-read-only': 400,
|
|
215
218
|
'graph-not-registered': 409,
|
|
216
219
|
'graph-exists': 409,
|
|
220
|
+
'graph-not-routed': 400,
|
|
217
221
|
};
|
|
218
222
|
/**
|
|
219
223
|
* A store's graph query refusal (`GraphQueryError`) as the answer the caller gets: a
|
|
@@ -222,10 +226,12 @@ const GRAPH_REFUSAL_STATUS = {
|
|
|
222
226
|
* error is returned as is.
|
|
223
227
|
*/
|
|
224
228
|
export function graphQueryCallError(err) {
|
|
225
|
-
var _a;
|
|
226
229
|
if (!isGraphQueryError(err))
|
|
227
230
|
return err;
|
|
228
|
-
const
|
|
231
|
+
const code = err.code;
|
|
232
|
+
const status = Object.prototype.hasOwnProperty.call(GRAPH_REFUSAL_STATUS, code)
|
|
233
|
+
? GRAPH_REFUSAL_STATUS[code]
|
|
234
|
+
: 400;
|
|
229
235
|
return Object.assign(new ServerCallError(status, err.message, { cause: err }), { code: err.code });
|
|
230
236
|
}
|
|
231
237
|
//# sourceMappingURL=queryPlane.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryPlane.js","sourceRoot":"","sources":["../../../src/utils/queryPlane.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,iBAAiB,EACjB,eAAe,GAGhB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAEpD,OAAO,EACL,gBAAgB,GAEjB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,6CAA6C,CAAC;AAC9E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,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,KAAc,EACd,EAA0B,EAC1B,QAAgB;IAEhB,MAAM,gBAAgB,CAAC;QACrB,SAAS,EAAE,OAAO;QAClB,KAAK;QACL,EAAE;QACF,IAAI,EAAE,kBAAkB,EAAE;QAC1B,QAAQ;KACT,CAAC,CAAC;AACL,CAAC;AAED,iEAAiE;AACjE,MAAM,oBAAoB,GAAwC;IAChE,wBAAwB,EAAE,GAAG;IAC7B,sBAAsB,EAAE,GAAG;IAC3B,sBAAsB,EAAE,GAAG;IAC3B,cAAc,EAAE,GAAG;CACpB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAY;;IAC9C,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IACxC,MAAM,MAAM,GAAG,MAAA,oBAAoB,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,GAAG,CAAC;IACrD,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACrG,CAAC","sourcesContent":["/**\n * Authorization for the generic query plane: the endpoints that run a query the\n * client built (`LincdServerBackendProvider.*Query` behind\n * `/call/@_linked/server/*Query`, and `LincdAPI` `post_*` behind `/api/*`).\n *\n * The rules are declared per store in the app's storage config\n * (`linked.backend.storage.js`) with `withAccess` from\n * `@_linked/server-utils/utils/QueryAccess`; see `checkQueryAccess` there for\n * how a query is mapped to stores and the order the checks apply in. This\n * module turns the incoming query into the builder the store runs and the IR\n * the rules read.\n *\n * Graph queries (`graph_create`, `graph_replace`, `graph_patch`, `graph_drop`) arrive as plain\n * data on `/call/@_linked/server/graphQuery`: {@link toGraphQuery} checks the JSON, the store's\n * `graph` rule decides ({@link authorizeGraphQuery}), and a refusal of the store goes back with\n * its code ({@link graphQueryCallError}).\n *\n * The host can supply the shape catalog a request's query is decoded in\n * (`setQueryCatalogResolver`), so one server can resolve the same shape IRI\n * differently per request (for example per project branch).\n *\n * INTERIM: that per-store access model needs to be revised and rethought from\n * the ground up (see QueryAccess).\n */\nimport { fromJSON } from '@_linked/core';\nimport {\n isGraphQueryError,\n parseGraphQuery,\n type GraphQuery,\n type GraphQueryErrorCode,\n} from '@_linked/core/queries/graphQuery';\nimport { lower } from '@_linked/core/queries/lower';\nimport type { ShapeCatalog } from '@_linked/core/utils/ShapeCatalog';\nimport {\n checkQueryAccess,\n type QueryOperation,\n} from '@_linked/server-utils/utils/QueryAccess';\nimport { ServerCallError } from '@_linked/server-utils/utils/ServerCallError';\nimport { getRawQueriesMode } from './rawQueries.js';\nimport { getRpcExposureMode } from './rpcExposure.js';\nimport { sharedState } from '@_linked/core/utils/sharedState';\n\n/** The DSL-JSON tag of a query-context reference (`CONTEXT_REF_KEY` in core). */\nconst CONTEXT_REF_KEY = '@ctx';\n\n/**\n * Whether DSL-JSON carries a query-context reference (`{\"@ctx\": name}`) in any\n * position: a select or count subject, an update target, a delete id, a\n * where-clause operand or a mutation field value. Every DSL-JSON decoder in\n * core reads a context reference from this one key.\n */\nexport function containsContextRef(json: unknown): boolean {\n const seen = new Set<object>();\n const stack: unknown[] = [json];\n while (stack.length) {\n const value = stack.pop();\n if (!value || typeof value !== 'object' || seen.has(value)) continue;\n seen.add(value);\n if (Array.isArray(value)) {\n for (const item of value) stack.push(item);\n continue;\n }\n if (Object.prototype.hasOwnProperty.call(value, CONTEXT_REF_KEY)) return true;\n for (const key of Object.keys(value)) stack.push((value as any)[key]);\n }\n return false;\n}\n\n/**\n * Supplies the shape catalog a generic query request is decoded in, from the\n * DSL-JSON the client sent (the request context is available through the\n * host's own means, e.g. `getCallContext`). `undefined` means the global\n * shape registry.\n */\nexport type QueryCatalogResolver = (\n query: unknown\n) => ShapeCatalog | undefined | Promise<ShapeCatalog | undefined>;\n\n/**\n * The host's resolver, shared by every copy of this module in the process: the host sets it\n * through one copy, and the query endpoint served by another must use it rather than decode in\n * the global registry. See https://github.com/linked-fw/cli/blob/main/docs/architecture/shared-state-and-error-brands.md.\n */\nconst queryPlaneState = sharedState('@_linked/server', 'queryPlane', () => ({\n catalogResolver: undefined as QueryCatalogResolver | undefined,\n}));\n\n/**\n * Set (or, with `undefined`, clear) the host's catalog resolver for the\n * generic query endpoints. Without one, every query decodes in the global\n * shape registry.\n */\nexport function setQueryCatalogResolver(resolver: QueryCatalogResolver | undefined): void {\n queryPlaneState.catalogResolver = resolver;\n}\n\n/** A live builder passed by backend code (never a plain object from JSON). */\nfunction isLiveBuilder(query: any): boolean {\n return (\n !!query &&\n typeof query === 'object' &&\n Object.getPrototypeOf(query) !== Object.prototype &&\n typeof query.__queryKind === 'string'\n );\n}\n\n/**\n * The catalog `query` is decoded in: the host resolver's answer, or\n * `undefined` (the global registry) without a resolver. A live builder already\n * carries its catalog, so the resolver is not asked for it.\n *\n * A `ServerCallError` the resolver throws is passed on as is (the host chose\n * the status); any other failure is logged and answers 500.\n */\nexport async function resolveQueryCatalog(query: unknown): Promise<ShapeCatalog | undefined> {\n const resolver = queryPlaneState.catalogResolver;\n if (!resolver || isLiveBuilder(query)) return undefined;\n try {\n return await resolver(query);\n } catch (error) {\n if (ServerCallError.is(error)) throw error;\n console.error('[queryPlane] the shape catalog resolver failed:', error);\n throw new ServerCallError(500, 'Shape catalog unavailable');\n }\n}\n\n/**\n * A rehydrated query builder from the DSL-JSON a client sent, decoded in\n * `catalog` (`undefined`: the global registry, whatever catalog is active;\n * see `resolveQueryCatalog`). The builder keeps that catalog, so\n * the access-check lowering and the store's lowering both resolve against it.\n * The result is what the store runs, so the access check and the store see\n * the same query. A body that is not a query answers 400.\n *\n * A query-context reference (`{\"@ctx\": \"user\"}`) also answers 400. Core\n * resolves one at lowering time against a process-wide context map, which on a\n * server holds whatever some code last set there, not the caller's identity,\n * and lowering runs twice (once for the access check, once in the store), so\n * the two could even resolve it differently. A query built from client JSON\n * names its nodes by id.\n *\n * Without context references, lowering a rehydrated builder is a pure function\n * of the builder (closed and never mutated after `fromJSON`) and its shape\n * catalog: the IR `authorizeGenericQuery` checks is the IR the store lowers\n * from the same builder.\n */\nexport function toQueryBuilder(query: unknown, catalog?: ShapeCatalog): any {\n if (isLiveBuilder(query)) return query;\n if (containsContextRef(query)) {\n throw new ServerCallError(\n 400,\n 'Query context references (@ctx) are not accepted by the generic query endpoints'\n );\n }\n try {\n const builder = fromJSON(query as any, { catalog });\n if (!builder || typeof builder !== 'object') throw new Error('not a query');\n return builder;\n } catch {\n throw new ServerCallError(400, 'Invalid query');\n }\n}\n\n/** The lowered IR of a query, or `undefined` when it cannot be lowered (refused). */\nexport function lowerForAccessCheck(builder: any): unknown {\n try {\n return lower(builder);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Check `builder` (rehydrated from `query` with `toQueryBuilder`) against the\n * store access rules. Throws a `ServerCallError` (400/401/403) when the current\n * call may not run it; the caller then hands the same builder to the store.\n *\n * Deliberately returns nothing: a query builder is a thenable, so resolving an\n * async function with one would run it.\n */\nexport async function authorizeGenericQuery(\n operation: QueryOperation,\n query: unknown,\n builder: any,\n endpoint: string\n): Promise<void> {\n await checkQueryAccess({\n operation,\n query,\n ir: lowerForAccessCheck(builder),\n mode: getRpcExposureMode(),\n endpoint,\n });\n}\n\n/** Check a raw SPARQL query against the `rawQueries` setting (see utils/rawQueries). */\nexport async function authorizeRawQuery(query: unknown, endpoint: string): Promise<void> {\n if (typeof query !== 'string') {\n throw new ServerCallError(400, 'Invalid query');\n }\n await checkQueryAccess({\n operation: 'select',\n query,\n ir: undefined,\n raw: true,\n rawQueries: getRawQueriesMode(),\n mode: getRpcExposureMode(),\n endpoint,\n });\n}\n\n/**\n * The graph query `json` describes, checked with core's `parseGraphQuery`; `undefined` when it is\n * not a valid graph query (the access check then refuses it with 400).\n */\nexport function toGraphQuery(json: unknown): GraphQuery | undefined {\n try {\n return parseGraphQuery(json);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Check a graph query against the `graph` rule of the store `LinkedStorage.graphQuery` routes its\n * record class to (`QueryAccess`, operation `graph`): no rule, or `'none'`, refuses it (403, 401\n * without a session); a malformed one answers 400. `ir` is {@link toGraphQuery}'s result. No\n * catalog is resolved for a graph query (its record shapes are core's built-ins), so the check\n * resolves the store in the global registry, where `LinkedStorage.graphQuery` then runs it.\n */\nexport async function authorizeGraphQuery(\n query: unknown,\n ir: GraphQuery | undefined,\n endpoint: string\n): Promise<void> {\n await checkQueryAccess({\n operation: 'graph',\n query,\n ir,\n mode: getRpcExposureMode(),\n endpoint,\n });\n}\n\n/** The HTTP status each graph query refusal is answered with. */\nconst GRAPH_REFUSAL_STATUS: Record<GraphQueryErrorCode, number> = {\n 'unsupported-query-kind': 501,\n 'graph-name-read-only': 400,\n 'graph-not-registered': 409,\n 'graph-exists': 409,\n};\n\n/**\n * A store's graph query refusal (`GraphQueryError`) as the answer the caller gets: a\n * `ServerCallError` with the refusal's status and message and its `code`, which the call route\n * sends as `{error, code}` (`RemoteDataset` turns it back into a `GraphQueryError`). Any other\n * error is returned as is.\n */\nexport function graphQueryCallError(err: unknown): unknown {\n if (!isGraphQueryError(err)) return err;\n const status = GRAPH_REFUSAL_STATUS[err.code] ?? 400;\n return Object.assign(new ServerCallError(status, err.message, { cause: err }), { code: err.code });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"queryPlane.js","sourceRoot":"","sources":["../../../src/utils/queryPlane.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,iBAAiB,EACjB,eAAe,GAGhB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE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C,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,IAAa;IACxC,IAAI,CAAC;QACH,OAAO,eAAe,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,KAAc,EACd,EAA0B,EAC1B,QAAgB;IAEhB,MAAM,gBAAgB,CAAC;QACrB,SAAS,EAAE,OAAO;QAClB,KAAK;QACL,EAAE;QACF,IAAI,EAAE,kBAAkB,EAAE;QAC1B,QAAQ;KACT,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,oBAAoB,GAAwC;IAChE,wBAAwB,EAAE,GAAG;IAC7B,sBAAsB,EAAE,GAAG;IAC3B,sBAAsB,EAAE,GAAG;IAC3B,cAAc,EAAE,GAAG;IACnB,kBAAkB,EAAE,GAAG;CACxB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAY;IAC9C,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IACxC,MAAM,IAAI,GAAW,GAAG,CAAC,IAAI,CAAC;IAC9B,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,oBAAoB,EAAE,IAAI,CAAC;QAC7E,CAAC,CAAC,oBAAoB,CAAC,IAA2B,CAAC;QACnD,CAAC,CAAC,GAAG,CAAC;IACR,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACrG,CAAC","sourcesContent":["/**\n * Authorization for the generic query plane: the endpoints that run a query the\n * client built (`LincdServerBackendProvider.*Query` behind\n * `/call/@_linked/server/*Query`, and `LincdAPI` `post_*` behind `/api/*`).\n *\n * The rules are declared per store in the app's storage config\n * (`linked.backend.storage.js`) with `withAccess` from\n * `@_linked/server-utils/utils/QueryAccess`; see `checkQueryAccess` there for\n * how a query is mapped to stores and the order the checks apply in. This\n * module turns the incoming query into the builder the store runs and the IR\n * the rules read.\n *\n * Graph queries (`graph_create`, `graph_replace`, `graph_patch`, `graph_drop`) arrive as plain\n * data on `/call/@_linked/server/graphQuery`: {@link toGraphQuery} checks the JSON, the store's\n * `graph` rule decides ({@link authorizeGraphQuery}), and a refusal of the store goes back with\n * its code ({@link graphQueryCallError}).\n *\n * The host can supply the shape catalog a request's query is decoded in\n * (`setQueryCatalogResolver`), so one server can resolve the same shape IRI\n * differently per request (for example per project branch).\n *\n * INTERIM: that per-store access model needs to be revised and rethought from\n * the ground up (see QueryAccess).\n */\nimport { fromJSON } from '@_linked/core';\nimport {\n isGraphQueryError,\n parseGraphQuery,\n type GraphQuery,\n type GraphQueryErrorCode,\n} from '@_linked/core/queries/graphQuery';\nimport { lower } from '@_linked/core/queries/lower';\nimport type { ShapeCatalog } from '@_linked/core/utils/ShapeCatalog';\nimport {\n checkQueryAccess,\n type QueryOperation,\n} from '@_linked/server-utils/utils/QueryAccess';\nimport { ServerCallError } from '@_linked/server-utils/utils/ServerCallError';\nimport { getRawQueriesMode } from './rawQueries.js';\nimport { getRpcExposureMode } from './rpcExposure.js';\nimport { sharedState } from '@_linked/core/utils/sharedState';\n\n/** The DSL-JSON tag of a query-context reference (`CONTEXT_REF_KEY` in core). */\nconst CONTEXT_REF_KEY = '@ctx';\n\n/**\n * Whether DSL-JSON carries a query-context reference (`{\"@ctx\": name}`) in any\n * position: a select or count subject, an update target, a delete id, a\n * where-clause operand or a mutation field value. Every DSL-JSON decoder in\n * core reads a context reference from this one key.\n */\nexport function containsContextRef(json: unknown): boolean {\n const seen = new Set<object>();\n const stack: unknown[] = [json];\n while (stack.length) {\n const value = stack.pop();\n if (!value || typeof value !== 'object' || seen.has(value)) continue;\n seen.add(value);\n if (Array.isArray(value)) {\n for (const item of value) stack.push(item);\n continue;\n }\n if (Object.prototype.hasOwnProperty.call(value, CONTEXT_REF_KEY)) return true;\n for (const key of Object.keys(value)) stack.push((value as any)[key]);\n }\n return false;\n}\n\n/**\n * Supplies the shape catalog a generic query request is decoded in, from the\n * DSL-JSON the client sent (the request context is available through the\n * host's own means, e.g. `getCallContext`). `undefined` means the global\n * shape registry.\n */\nexport type QueryCatalogResolver = (\n query: unknown\n) => ShapeCatalog | undefined | Promise<ShapeCatalog | undefined>;\n\n/**\n * The host's resolver, shared by every copy of this module in the process: the host sets it\n * through one copy, and the query endpoint served by another must use it rather than decode in\n * the global registry. See https://github.com/linked-fw/cli/blob/main/docs/architecture/shared-state-and-error-brands.md.\n */\nconst queryPlaneState = sharedState('@_linked/server', 'queryPlane', () => ({\n catalogResolver: undefined as QueryCatalogResolver | undefined,\n}));\n\n/**\n * Set (or, with `undefined`, clear) the host's catalog resolver for the\n * generic query endpoints. Without one, every query decodes in the global\n * shape registry.\n */\nexport function setQueryCatalogResolver(resolver: QueryCatalogResolver | undefined): void {\n queryPlaneState.catalogResolver = resolver;\n}\n\n/** A live builder passed by backend code (never a plain object from JSON). */\nfunction isLiveBuilder(query: any): boolean {\n return (\n !!query &&\n typeof query === 'object' &&\n Object.getPrototypeOf(query) !== Object.prototype &&\n typeof query.__queryKind === 'string'\n );\n}\n\n/**\n * The catalog `query` is decoded in: the host resolver's answer, or\n * `undefined` (the global registry) without a resolver. A live builder already\n * carries its catalog, so the resolver is not asked for it.\n *\n * A `ServerCallError` the resolver throws is passed on as is (the host chose\n * the status); any other failure is logged and answers 500.\n */\nexport async function resolveQueryCatalog(query: unknown): Promise<ShapeCatalog | undefined> {\n const resolver = queryPlaneState.catalogResolver;\n if (!resolver || isLiveBuilder(query)) return undefined;\n try {\n return await resolver(query);\n } catch (error) {\n if (ServerCallError.is(error)) throw error;\n console.error('[queryPlane] the shape catalog resolver failed:', error);\n throw new ServerCallError(500, 'Shape catalog unavailable');\n }\n}\n\n/**\n * A rehydrated query builder from the DSL-JSON a client sent, decoded in\n * `catalog` (`undefined`: the global registry, whatever catalog is active;\n * see `resolveQueryCatalog`). The builder keeps that catalog, so\n * the access-check lowering and the store's lowering both resolve against it.\n * The result is what the store runs, so the access check and the store see\n * the same query. A body that is not a query answers 400.\n *\n * A query-context reference (`{\"@ctx\": \"user\"}`) also answers 400. Core\n * resolves one at lowering time against a process-wide context map, which on a\n * server holds whatever some code last set there, not the caller's identity,\n * and lowering runs twice (once for the access check, once in the store), so\n * the two could even resolve it differently. A query built from client JSON\n * names its nodes by id.\n *\n * Without context references, lowering a rehydrated builder is a pure function\n * of the builder (closed and never mutated after `fromJSON`) and its shape\n * catalog: the IR `authorizeGenericQuery` checks is the IR the store lowers\n * from the same builder.\n */\nexport function toQueryBuilder(query: unknown, catalog?: ShapeCatalog): any {\n if (isLiveBuilder(query)) return query;\n if (containsContextRef(query)) {\n throw new ServerCallError(\n 400,\n 'Query context references (@ctx) are not accepted by the generic query endpoints'\n );\n }\n try {\n const builder = fromJSON(query as any, { catalog });\n if (!builder || typeof builder !== 'object') throw new Error('not a query');\n return builder;\n } catch {\n throw new ServerCallError(400, 'Invalid query');\n }\n}\n\n/** The lowered IR of a query, or `undefined` when it cannot be lowered (refused). */\nexport function lowerForAccessCheck(builder: any): unknown {\n try {\n return lower(builder);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Check `builder` (rehydrated from `query` with `toQueryBuilder`) against the\n * store access rules. Throws a `ServerCallError` (400/401/403) when the current\n * call may not run it; the caller then hands the same builder to the store.\n *\n * Deliberately returns nothing: a query builder is a thenable, so resolving an\n * async function with one would run it.\n */\nexport async function authorizeGenericQuery(\n operation: QueryOperation,\n query: unknown,\n builder: any,\n endpoint: string\n): Promise<void> {\n await checkQueryAccess({\n operation,\n query,\n ir: lowerForAccessCheck(builder),\n mode: getRpcExposureMode(),\n endpoint,\n });\n}\n\n/** Check a raw SPARQL query against the `rawQueries` setting (see utils/rawQueries). */\nexport async function authorizeRawQuery(query: unknown, endpoint: string): Promise<void> {\n if (typeof query !== 'string') {\n throw new ServerCallError(400, 'Invalid query');\n }\n await checkQueryAccess({\n operation: 'select',\n query,\n ir: undefined,\n raw: true,\n rawQueries: getRawQueriesMode(),\n mode: getRpcExposureMode(),\n endpoint,\n });\n}\n\n/**\n * The graph query `json` describes, checked with core's `parseGraphQuery`; `undefined` when it is\n * not a valid graph query (the access check then refuses it with 400).\n */\nexport function toGraphQuery(json: unknown): GraphQuery | undefined {\n try {\n return parseGraphQuery(json);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Check a graph query against the `graph` rule of the store `LinkedStorage.graphQuery` routes its\n * record class to (`QueryAccess`, operation `graph`): no rule, or `'none'`, refuses it (403, 401\n * without a session); a malformed one answers 400. `ir` is {@link toGraphQuery}'s result. No\n * catalog is resolved for a graph query (its record shapes are core's built-ins), so the check\n * resolves the store in the global registry, where `LinkedStorage.graphQuery` then runs it.\n */\nexport async function authorizeGraphQuery(\n query: unknown,\n ir: GraphQuery | undefined,\n endpoint: string\n): Promise<void> {\n await checkQueryAccess({\n operation: 'graph',\n query,\n ir,\n mode: getRpcExposureMode(),\n endpoint,\n });\n}\n\n/**\n * The HTTP status each graph query refusal is answered with. A code this map does not know (a\n * newer core than the one this server was built against) is answered with 400, never 500.\n */\nconst GRAPH_REFUSAL_STATUS: Record<GraphQueryErrorCode, number> = {\n 'unsupported-query-kind': 501,\n 'graph-name-read-only': 400,\n 'graph-not-registered': 409,\n 'graph-exists': 409,\n 'graph-not-routed': 400,\n};\n\n/**\n * A store's graph query refusal (`GraphQueryError`) as the answer the caller gets: a\n * `ServerCallError` with the refusal's status and message and its `code`, which the call route\n * sends as `{error, code}` (`RemoteDataset` turns it back into a `GraphQueryError`). Any other\n * error is returned as is.\n */\nexport function graphQueryCallError(err: unknown): unknown {\n if (!isGraphQueryError(err)) return err;\n const code: string = err.code;\n const status = Object.prototype.hasOwnProperty.call(GRAPH_REFUSAL_STATUS, code)\n ? GRAPH_REFUSAL_STATUS[code as GraphQueryErrorCode]\n : 400;\n return Object.assign(new ServerCallError(status, err.message, { cause: err }), { code: err.code });\n}\n"]}
|
|
@@ -1,37 +1,147 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Boot-time shape materialization: write the shapes this process registered into
|
|
3
|
-
* its app-data dataset as SHACL, via core `syncShapes`.
|
|
4
|
-
*
|
|
5
|
-
* Kept apart from `LinkedServer` so the orphan policy can be tested without
|
|
6
|
-
* booting a server.
|
|
7
|
-
*/
|
|
8
1
|
/** The orphan policies a booting server may run with. */
|
|
9
2
|
export type BootOrphanScope = 'all' | 'none';
|
|
10
|
-
/** The subset of core's `syncShapes` this module calls. */
|
|
11
|
-
export type SyncShapesFn = (ds: unknown, options: {
|
|
12
|
-
orphanScope: BootOrphanScope;
|
|
13
|
-
}) => Promise<Array<() => Promise<void>>>;
|
|
14
3
|
/**
|
|
15
|
-
* The orphan policy for
|
|
4
|
+
* The orphan policy for the shape sync.
|
|
16
5
|
*
|
|
17
|
-
* `'none'` unless the operator sets `LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true
|
|
6
|
+
* `'none'` unless the operator sets `LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true`, which drops the
|
|
7
|
+
* shape graph of every package that is no longer registered in this process (never the app's own
|
|
8
|
+
* graph).
|
|
18
9
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* all, and an app-data dataset also holds shapes other writers put there (Create
|
|
23
|
-
* Now's `bindShape` / `enableCapability`). With pruning on, an app whose backend
|
|
24
|
-
* imports no shapes deletes every NodeShape in its app-data on each boot.
|
|
25
|
-
* `'ownedNamespaces'` is deliberately not offered: a namespace is a whole
|
|
26
|
-
* package, so a deep import of one shape file still prunes the rest of it.
|
|
10
|
+
* The registry is routinely a subset of what the dataset holds: shapes are deep-importable one
|
|
11
|
+
* file at a time, an app's backend may import no shapes at all, and an app-data dataset also holds
|
|
12
|
+
* shapes other writers put there. So nothing is pruned by default.
|
|
27
13
|
*/
|
|
28
14
|
export declare function bootOrphanScope(env?: Record<string, string | undefined>): BootOrphanScope;
|
|
15
|
+
/** What core's `syncShapeGraphs` reports (`@_linked/core/shapes/syncShapeGraphs`). */
|
|
16
|
+
export interface ShapeGraphSyncResult {
|
|
17
|
+
written: string[];
|
|
18
|
+
skipped: string[];
|
|
19
|
+
pruned: string[];
|
|
20
|
+
cleaned: number;
|
|
21
|
+
appGraph?: 'written' | 'skipped' | 'dropped';
|
|
22
|
+
defaultGraph?: number;
|
|
23
|
+
}
|
|
24
|
+
/** The subset of core's shape graph API this module calls. */
|
|
25
|
+
export interface ShapeGraphCore {
|
|
26
|
+
syncShapeGraphs(ds: unknown, options: {
|
|
27
|
+
orphanScope: BootOrphanScope;
|
|
28
|
+
packages?: string[];
|
|
29
|
+
turtleFor: (packageName: string) => Promise<string | undefined>;
|
|
30
|
+
appShapes?: {
|
|
31
|
+
graph: string;
|
|
32
|
+
package?: string;
|
|
33
|
+
};
|
|
34
|
+
}): Promise<ShapeGraphSyncResult>;
|
|
35
|
+
/** Every package with registered shapes (core counted for its built-ins). */
|
|
36
|
+
packagesWithShapes(): string[];
|
|
37
|
+
/** A package's shape graph IRI (`ShapesGraph.iriFor`). */
|
|
38
|
+
shapesGraphFor(packageName: string): string;
|
|
39
|
+
}
|
|
40
|
+
/** Import core's shape graph sync. */
|
|
41
|
+
export declare function loadShapeGraphCore(): Promise<ShapeGraphCore>;
|
|
42
|
+
/** Where `linked build` writes a package's shapes, relative to the package root. */
|
|
43
|
+
export declare const SHAPES_TURTLE_FILE: string;
|
|
44
|
+
/** A package's built `shapes.ttl`, or undefined when the package root has none. */
|
|
45
|
+
export declare function readShapesTurtle(packageDir: string | undefined): string | undefined;
|
|
46
|
+
/** Where one package's Turtle comes from. */
|
|
47
|
+
export type ShapeTurtleSource = {
|
|
48
|
+
source: 'file';
|
|
49
|
+
text: string;
|
|
50
|
+
} | {
|
|
51
|
+
source: 'generated';
|
|
52
|
+
} | {
|
|
53
|
+
source: 'none';
|
|
54
|
+
};
|
|
29
55
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
56
|
+
* Where a registered package's Turtle comes from: in dev an own package is generated; otherwise
|
|
57
|
+
* its built file when it has one; else an own package is generated and an installed one has
|
|
58
|
+
* none (no shape graph, no generation).
|
|
59
|
+
*/
|
|
60
|
+
export declare function shapeTurtleSource(packageName: string, { own, dev, packageDir, read, }: {
|
|
61
|
+
/** Whether the package is the app or one of its workspace packages. */
|
|
62
|
+
own: boolean;
|
|
63
|
+
/** Whether the server runs with Vite (dev). */
|
|
64
|
+
dev: boolean;
|
|
65
|
+
/** The package root, when the server knows it. */
|
|
66
|
+
packageDir: string | undefined;
|
|
67
|
+
read?: (packageDir: string | undefined) => string | undefined;
|
|
68
|
+
}): ShapeTurtleSource;
|
|
69
|
+
/**
|
|
70
|
+
* The app's own shape graph, `{DATA_ROOT}/graph/shapes`, or undefined when `DATA_ROOT` is not
|
|
71
|
+
* an absolute IRI.
|
|
72
|
+
*/
|
|
73
|
+
export declare function appShapesGraph(env?: Record<string, string | undefined>): string | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* In dev, the generator for the app's own packages: core's `packageShapesTurtle` imported through
|
|
76
|
+
* Vite's `ssrLoadModule`, the core the SSR graph registered the source shapes on. Undefined
|
|
77
|
+
* without Vite.
|
|
78
|
+
*/
|
|
79
|
+
export declare function viteShapesTurtleGenerator(vite: {
|
|
80
|
+
ssrLoadModule?: (url: string) => Promise<unknown>;
|
|
81
|
+
} | undefined | null): ((packageName: string) => Promise<string | undefined>) | undefined;
|
|
82
|
+
/** What one shape sync did, as the boot logs it and `POST /admin/sync-shapes` reports it. */
|
|
83
|
+
export interface ShapeGraphsReport {
|
|
84
|
+
/** Packages whose shape graph was written, and those left as they were. */
|
|
85
|
+
written: string[];
|
|
86
|
+
skipped: string[];
|
|
87
|
+
/** Packages no longer registered whose shape graph was dropped (`orphanScope: 'all'`). */
|
|
88
|
+
pruned: string[];
|
|
89
|
+
/** The shape graphs written (package graphs, then the app graph), by IRI. */
|
|
90
|
+
graphs: string[];
|
|
91
|
+
/** The app's own shape graph and what the sync did with it; absent without `DATA_ROOT`. */
|
|
92
|
+
appGraph?: {
|
|
93
|
+
graph: string;
|
|
94
|
+
status: 'written' | 'skipped' | 'dropped';
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Registered packages with no Turtle, so with no shape graph: an installed package without
|
|
98
|
+
* `shapes.ttl`, or an own package whose shapes could not be generated.
|
|
99
|
+
*/
|
|
100
|
+
withoutTurtle: string[];
|
|
101
|
+
/** Shapes an earlier sync had written into the default graph, removed now. */
|
|
102
|
+
cleaned: number;
|
|
103
|
+
/** Set when the store has no graph queries: the default-graph sync's writes. */
|
|
104
|
+
defaultGraph?: number;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sync the registered shapes into `ds`, one shape graph per package (see the module doc), and log
|
|
108
|
+
* one line naming what was written and skipped, the graphs written, the packages without
|
|
109
|
+
* `shapes.ttl` and the default-graph shapes removed. Throws when the sync fails.
|
|
110
|
+
*/
|
|
111
|
+
export declare function syncShapeGraphsInto(ds: unknown, { core, orphanScope, isOwn, dev, packageDirOf, generate, appGraph, appPackage, read, log, warn, }: {
|
|
112
|
+
core: ShapeGraphCore;
|
|
113
|
+
orphanScope: BootOrphanScope;
|
|
114
|
+
/** Whether a package is the app or one of its workspace packages. */
|
|
115
|
+
isOwn: (packageName: string) => boolean;
|
|
116
|
+
/** Whether the server runs with Vite (dev). */
|
|
117
|
+
dev: boolean;
|
|
118
|
+
/** A package's root directory, when the server knows it. */
|
|
119
|
+
packageDirOf: (packageName: string) => string | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Generates an own package's Turtle (in dev through Vite's loader); undefined, or a result of
|
|
122
|
+
* undefined, leaves the generation to the core that runs the sync.
|
|
123
|
+
*/
|
|
124
|
+
generate?: (packageName: string) => Promise<string | undefined>;
|
|
125
|
+
/** The app's own shape graph (`{DATA_ROOT}/graph/shapes`). */
|
|
126
|
+
appGraph?: string;
|
|
127
|
+
/** The app's package name, recorded on its shape graph's record. */
|
|
128
|
+
appPackage?: string;
|
|
129
|
+
read?: (packageDir: string | undefined) => string | undefined;
|
|
130
|
+
log?: (message: string) => void;
|
|
131
|
+
warn?: (message: string, err?: unknown) => void;
|
|
132
|
+
}): Promise<ShapeGraphsReport>;
|
|
133
|
+
/**
|
|
134
|
+
* @deprecated The default-graph shape sync's callback type; the server now syncs shape graphs with
|
|
135
|
+
* {@link syncShapeGraphsInto}. Kept so code built against an earlier server still compiles.
|
|
136
|
+
*/
|
|
137
|
+
export type SyncShapesFn = (ds: unknown, options: {
|
|
138
|
+
orphanScope: BootOrphanScope;
|
|
139
|
+
}) => Promise<Array<() => Promise<void>>>;
|
|
140
|
+
/**
|
|
141
|
+
* @deprecated Use {@link syncShapeGraphsInto}, which writes each package's shapes into its own
|
|
142
|
+
* shape graph. This runs the old default-graph sync: it plans every registered shape with
|
|
143
|
+
* `syncShapes` (the orphan policy from {@link bootOrphanScope}) and runs the writes in batches
|
|
144
|
+
* of 8. Kept so code built against an earlier server keeps working.
|
|
35
145
|
*/
|
|
36
146
|
export declare function materializeShapes(ds: unknown, syncShapes: SyncShapesFn, { env, log, }?: {
|
|
37
147
|
env?: Record<string, string | undefined>;
|
|
@@ -1,33 +1,187 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* The shape sync, as the server runs it at boot and when the host asks for a re-sync
|
|
3
|
+
* (`POST /admin/sync-shapes`): core's `syncShapeGraphs` writes each package's shapes into its own
|
|
4
|
+
* named graph of the app's default dataset (`{package IRI}/graph/shapes`, a `ShapesGraph` record
|
|
5
|
+
* in the graph registry), skipped when the stored content hash matches, and the app's generated
|
|
6
|
+
* single-owner shapes into the app's own shape graph, `{DATA_ROOT}/graph/shapes`. Shapes an
|
|
7
|
+
* earlier sync wrote into the default graph are removed there once their package's graph holds
|
|
8
|
+
* them. A store without graph queries gets core's default-graph shape sync instead.
|
|
4
9
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
10
|
+
* Where a package's Turtle comes from ({@link shapeTurtleSource}):
|
|
11
|
+
* - an installed package: its built `lib/esm/data/shapes.ttl` (written by `linked build`); a
|
|
12
|
+
* package without that file gets no shape graph, and nothing is generated for it;
|
|
13
|
+
* - the app's own packages (the app and its workspace packages): in dev, generated in process
|
|
14
|
+
* from their source shapes, through Vite's loader, so the core the SSR graph registered the
|
|
15
|
+
* shapes on writes them (the same bytes as `packageShapesTurtle`, which `linked build` writes);
|
|
16
|
+
* in a built app, their `lib/esm/data/shapes.ttl` when there is one, else generated in process.
|
|
17
|
+
*
|
|
18
|
+
* Kept apart from `LinkedServer` so the choices and the log can be tested without booting a
|
|
19
|
+
* server.
|
|
7
20
|
*/
|
|
21
|
+
import fs from 'fs';
|
|
22
|
+
import path from 'path';
|
|
8
23
|
/**
|
|
9
|
-
* The orphan policy for
|
|
24
|
+
* The orphan policy for the shape sync.
|
|
10
25
|
*
|
|
11
|
-
* `'none'` unless the operator sets `LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true
|
|
26
|
+
* `'none'` unless the operator sets `LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true`, which drops the
|
|
27
|
+
* shape graph of every package that is no longer registered in this process (never the app's own
|
|
28
|
+
* graph).
|
|
12
29
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* all, and an app-data dataset also holds shapes other writers put there (Create
|
|
17
|
-
* Now's `bindShape` / `enableCapability`). With pruning on, an app whose backend
|
|
18
|
-
* imports no shapes deletes every NodeShape in its app-data on each boot.
|
|
19
|
-
* `'ownedNamespaces'` is deliberately not offered: a namespace is a whole
|
|
20
|
-
* package, so a deep import of one shape file still prunes the rest of it.
|
|
30
|
+
* The registry is routinely a subset of what the dataset holds: shapes are deep-importable one
|
|
31
|
+
* file at a time, an app's backend may import no shapes at all, and an app-data dataset also holds
|
|
32
|
+
* shapes other writers put there. So nothing is pruned by default.
|
|
21
33
|
*/
|
|
22
34
|
export function bootOrphanScope(env = process.env) {
|
|
23
35
|
return env.LINKED_SYNC_SHAPES_PRUNE_ORPHANS === 'true' ? 'all' : 'none';
|
|
24
36
|
}
|
|
37
|
+
/** Import core's shape graph sync. */
|
|
38
|
+
export async function loadShapeGraphCore() {
|
|
39
|
+
const sync = await import('@_linked/core/shapes/syncShapeGraphs');
|
|
40
|
+
const turtle = await import('@_linked/core/shapes/shapesToTurtle');
|
|
41
|
+
const graphs = await import('@_linked/core/shapes/NamedGraph');
|
|
42
|
+
return {
|
|
43
|
+
syncShapeGraphs: (ds, options) => sync.syncShapeGraphs(ds, options),
|
|
44
|
+
packagesWithShapes: () => turtle.packagesWithShapes(),
|
|
45
|
+
shapesGraphFor: (packageName) => graphs.ShapesGraph.iriFor(packageName),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Where `linked build` writes a package's shapes, relative to the package root. */
|
|
49
|
+
export const SHAPES_TURTLE_FILE = path.join('lib', 'esm', 'data', 'shapes.ttl');
|
|
50
|
+
/** A package's built `shapes.ttl`, or undefined when the package root has none. */
|
|
51
|
+
export function readShapesTurtle(packageDir) {
|
|
52
|
+
if (!packageDir)
|
|
53
|
+
return undefined;
|
|
54
|
+
try {
|
|
55
|
+
return fs.readFileSync(path.join(packageDir, SHAPES_TURTLE_FILE), 'utf-8');
|
|
56
|
+
}
|
|
57
|
+
catch (_a) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
25
61
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
62
|
+
* Where a registered package's Turtle comes from: in dev an own package is generated; otherwise
|
|
63
|
+
* its built file when it has one; else an own package is generated and an installed one has
|
|
64
|
+
* none (no shape graph, no generation).
|
|
65
|
+
*/
|
|
66
|
+
export function shapeTurtleSource(packageName, { own, dev, packageDir, read = readShapesTurtle, }) {
|
|
67
|
+
if (own && dev)
|
|
68
|
+
return { source: 'generated' };
|
|
69
|
+
const text = read(packageDir);
|
|
70
|
+
if (text !== undefined)
|
|
71
|
+
return { source: 'file', text };
|
|
72
|
+
return own ? { source: 'generated' } : { source: 'none' };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The app's own shape graph, `{DATA_ROOT}/graph/shapes`, or undefined when `DATA_ROOT` is not
|
|
76
|
+
* an absolute IRI.
|
|
77
|
+
*/
|
|
78
|
+
export function appShapesGraph(env = process.env) {
|
|
79
|
+
var _a;
|
|
80
|
+
const root = (_a = env.DATA_ROOT) === null || _a === void 0 ? void 0 : _a.trim().replace(/\/+$/, '');
|
|
81
|
+
if (!root || !/^[a-z][a-z0-9+.-]*:\/\/[^/]/i.test(root))
|
|
82
|
+
return undefined;
|
|
83
|
+
return `${root}/graph/shapes`;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* In dev, the generator for the app's own packages: core's `packageShapesTurtle` imported through
|
|
87
|
+
* Vite's `ssrLoadModule`, the core the SSR graph registered the source shapes on. Undefined
|
|
88
|
+
* without Vite.
|
|
89
|
+
*/
|
|
90
|
+
export function viteShapesTurtleGenerator(vite) {
|
|
91
|
+
const ssrLoadModule = vite === null || vite === void 0 ? void 0 : vite.ssrLoadModule;
|
|
92
|
+
if (typeof ssrLoadModule !== 'function')
|
|
93
|
+
return undefined;
|
|
94
|
+
return async (packageName) => {
|
|
95
|
+
const mod = (await ssrLoadModule.call(vite, '@_linked/core/shapes/shapesToTurtle'));
|
|
96
|
+
if (typeof (mod === null || mod === void 0 ? void 0 : mod.packageShapesTurtle) !== 'function') {
|
|
97
|
+
throw new Error('@_linked/core/shapes/shapesToTurtle has no packageShapesTurtle');
|
|
98
|
+
}
|
|
99
|
+
return mod.packageShapesTurtle(packageName);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const list = (names) => (names.length ? names.join(', ') : 'none');
|
|
103
|
+
/**
|
|
104
|
+
* Sync the registered shapes into `ds`, one shape graph per package (see the module doc), and log
|
|
105
|
+
* one line naming what was written and skipped, the graphs written, the packages without
|
|
106
|
+
* `shapes.ttl` and the default-graph shapes removed. Throws when the sync fails.
|
|
107
|
+
*/
|
|
108
|
+
export async function syncShapeGraphsInto(ds, { core, orphanScope, isOwn, dev, packageDirOf, generate, appGraph, appPackage, read = readShapesTurtle, log = console.log, warn = console.warn, }) {
|
|
109
|
+
const sources = new Map();
|
|
110
|
+
for (const name of core.packagesWithShapes()) {
|
|
111
|
+
sources.set(name, shapeTurtleSource(name, { own: isOwn(name), dev, packageDir: packageDirOf(name), read }));
|
|
112
|
+
}
|
|
113
|
+
const noFile = [...sources].filter(([, s]) => s.source === 'none').map(([name]) => name);
|
|
114
|
+
const turtleFor = async (name) => {
|
|
115
|
+
const source = sources.get(name);
|
|
116
|
+
if ((source === null || source === void 0 ? void 0 : source.source) === 'file')
|
|
117
|
+
return source.text;
|
|
118
|
+
if ((source === null || source === void 0 ? void 0 : source.source) !== 'generated' || !generate)
|
|
119
|
+
return undefined;
|
|
120
|
+
try {
|
|
121
|
+
return await generate(name);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
warn(`[LinkedServer] generating the shapes of ${name} through the dev loader failed; trying the server's core instead:`, err);
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const result = await core.syncShapeGraphs(ds, {
|
|
129
|
+
orphanScope,
|
|
130
|
+
// A package without Turtle is left out: core would generate it otherwise.
|
|
131
|
+
...(noFile.length ? { packages: [...sources.keys()].filter((n) => !noFile.includes(n)) } : {}),
|
|
132
|
+
turtleFor,
|
|
133
|
+
...(appGraph ? { appShapes: { graph: appGraph, ...(appPackage ? { package: appPackage } : {}) } } : {}),
|
|
134
|
+
});
|
|
135
|
+
// A package core neither wrote nor skipped got no Turtle at all: generation produced nothing
|
|
136
|
+
// (in dev, the server's core may not hold the source shapes the dev loader registered). The
|
|
137
|
+
// default-graph sync (a store without graph queries) reports no packages, so it is not checked.
|
|
138
|
+
const covered = new Set([...result.written, ...result.skipped]);
|
|
139
|
+
const notGenerated = result.defaultGraph !== undefined
|
|
140
|
+
? []
|
|
141
|
+
: [...sources.keys()].filter((n) => !noFile.includes(n) && !covered.has(n));
|
|
142
|
+
if (notGenerated.length) {
|
|
143
|
+
warn(`[LinkedServer] no shapes could be generated for ${list(notGenerated.sort())}, so ` +
|
|
144
|
+
`${notGenerated.length === 1 ? 'it has' : 'they have'} no shape graph this sync`);
|
|
145
|
+
}
|
|
146
|
+
const withoutTurtle = [...noFile, ...notGenerated].sort();
|
|
147
|
+
const report = {
|
|
148
|
+
written: result.written,
|
|
149
|
+
skipped: result.skipped,
|
|
150
|
+
pruned: result.pruned,
|
|
151
|
+
graphs: result.written.map((name) => core.shapesGraphFor(name)),
|
|
152
|
+
withoutTurtle,
|
|
153
|
+
cleaned: result.cleaned,
|
|
154
|
+
};
|
|
155
|
+
if (appGraph && result.appGraph) {
|
|
156
|
+
report.appGraph = { graph: appGraph, status: result.appGraph };
|
|
157
|
+
if (result.appGraph === 'written')
|
|
158
|
+
report.graphs.push(appGraph);
|
|
159
|
+
}
|
|
160
|
+
if (result.defaultGraph !== undefined)
|
|
161
|
+
report.defaultGraph = result.defaultGraph;
|
|
162
|
+
if (report.defaultGraph !== undefined) {
|
|
163
|
+
log(`[LinkedServer] shape sync (default graph: the store has no graph queries): ` +
|
|
164
|
+
`${report.defaultGraph} shape(s) written`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
log(`[LinkedServer] shape graph sync: written ${list(report.written)}; skipped ${list(report.skipped)}` +
|
|
168
|
+
(report.appGraph ? `; app graph ${report.appGraph.status}` : '; no app graph (DATA_ROOT not set)') +
|
|
169
|
+
(report.graphs.length ? `; graphs written ${list(report.graphs)}` : '') +
|
|
170
|
+
(orphanScope === 'all' ? `; pruned ${list(report.pruned)}` : '') +
|
|
171
|
+
(report.cleaned ? `; ${report.cleaned} default-graph shape(s) removed` : '') +
|
|
172
|
+
(withoutTurtle.length ? `; no shapes.ttl (no shape graph): ${list(withoutTurtle)}` : ''));
|
|
173
|
+
}
|
|
174
|
+
if (orphanScope === 'all' && withoutTurtle.length) {
|
|
175
|
+
warn('[LinkedServer] orphaned shape graphs were not pruned: core prunes only when the sync covers ' +
|
|
176
|
+
`every registered package, and ${list(withoutTurtle)} has no Turtle`);
|
|
177
|
+
}
|
|
178
|
+
return report;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* @deprecated Use {@link syncShapeGraphsInto}, which writes each package's shapes into its own
|
|
182
|
+
* shape graph. This runs the old default-graph sync: it plans every registered shape with
|
|
183
|
+
* `syncShapes` (the orphan policy from {@link bootOrphanScope}) and runs the writes in batches
|
|
184
|
+
* of 8. Kept so code built against an earlier server keeps working.
|
|
31
185
|
*/
|
|
32
186
|
export async function materializeShapes(ds, syncShapes, { env = process.env, log = console.log, } = {}) {
|
|
33
187
|
const orphanScope = bootOrphanScope(env);
|
|
@@ -35,13 +189,8 @@ export async function materializeShapes(ds, syncShapes, { env = process.env, log
|
|
|
35
189
|
for (let i = 0; i < thunks.length; i += 8) {
|
|
36
190
|
await Promise.all(thunks.slice(i, i + 8).map((run) => run()));
|
|
37
191
|
}
|
|
38
|
-
log(orphanScope
|
|
39
|
-
|
|
40
|
-
`(LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true): ${thunks.length} operation(s), ` +
|
|
41
|
-
`store shapes not registered in this process were deleted`
|
|
42
|
-
: `[LinkedServer] synced shapes into app-data with orphanScope 'none': ` +
|
|
43
|
-
`${thunks.length} shape(s) written, no store shapes deleted ` +
|
|
44
|
-
`(set LINKED_SYNC_SHAPES_PRUNE_ORPHANS=true to prune)`);
|
|
192
|
+
log(`[LinkedServer] synced shapes into the default graph with orphanScope '${orphanScope}': ` +
|
|
193
|
+
`${thunks.length} operation(s)`);
|
|
45
194
|
return { count: thunks.length, orphanScope };
|
|
46
195
|
}
|
|
47
196
|
//# sourceMappingURL=syncShapes.js.map
|