|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 28] The code-datasource predicates, from |
| 5 | + * `ObjectStackProtocolImplementation`, moved out of `protocol.ts` whole: the |
| 6 | + * two answers to "is this datasource name defined in code?" (#21899, #21944). |
| 7 | + * |
| 8 | + * - `isDeclaredCodeDatasource` — does an installed package declare it, or |
| 9 | + * does the host register it from code? The `/meta` door's artifact-backed |
| 10 | + * verdict and the stored-row lock read it. |
| 11 | + * - `isHostCodeDatasource` — the host's code-datasource set alone, the |
| 12 | + * first half of that answer and exactly what the datasource-admin boot |
| 13 | + * restore reads. |
| 14 | + * |
| 15 | + * A MOVE, not a redesign. Each function below is the body of the |
| 16 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 17 | + * re-indented one level and otherwise unedited. |
| 18 | + * |
| 19 | + * ## How the moved bodies reach the class |
| 20 | + * |
| 21 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 22 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 23 | + * The class keeps each method — the published `dist/*.d.ts` lists its name, |
| 24 | + * docblock and declared signature — and its body is one literal of exactly |
| 25 | + * the {@link CodeDatasourcePredicatesHost} members it reads, plus a `.call`. |
| 26 | + * The `engine` and `getServicesRegistry` fields are handed over as getters, |
| 27 | + * so each read finds them as they stand at call time; |
| 28 | + * `isDeclaredCodeDatasource`'s call to `isHostCodeDatasource` is handed over |
| 29 | + * as a closure, so it still reaches the class method. Every caller keeps |
| 30 | + * calling the class methods. |
| 31 | + * |
| 32 | + * What the bodies read from elsewhere, they import from where `protocol.ts` |
| 33 | + * imports it. Nothing re-exports this module, so the published surface does |
| 34 | + * not move. |
| 35 | + */ |
| 36 | + |
| 37 | +import { PLURAL_TO_SINGULAR } from '@objectstack/spec/shared'; |
| 38 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 39 | + |
| 40 | +/** |
| 41 | + * The `ObjectStackProtocolImplementation` members the functions below read — |
| 42 | + * every one of them, and no other; each function's `this` is the `Pick` of |
| 43 | + * the members it reads. Each is typed AS the class member (an indexed access |
| 44 | + * on the class), so a change to a member's signature on the class is a change |
| 45 | + * here too, and naming a member the class does not have is a compile error. |
| 46 | + */ |
| 47 | +export interface CodeDatasourcePredicatesHost { |
| 48 | + readonly engine: ObjectStackProtocolImplementation['engine']; |
| 49 | + readonly getServicesRegistry: ObjectStackProtocolImplementation['getServicesRegistry']; |
| 50 | + readonly isHostCodeDatasource: ObjectStackProtocolImplementation['isHostCodeDatasource']; |
| 51 | +} |
| 52 | + |
| 53 | +/** The body of `ObjectStackProtocolImplementation#isDeclaredCodeDatasource`; the method's docblock there is its contract. */ |
| 54 | +export function isDeclaredCodeDatasource( |
| 55 | + this: Pick<CodeDatasourcePredicatesHost, 'engine' | 'isHostCodeDatasource'>, |
| 56 | + type: string, |
| 57 | + name: string, |
| 58 | +): boolean { |
| 59 | + if ((PLURAL_TO_SINGULAR[type] ?? type) !== 'datasource') return false; |
| 60 | + if (this.isHostCodeDatasource(type, name)) return true; |
| 61 | + const registry = (this.engine as any)?.registry; |
| 62 | + if (typeof registry?.getAllPackages !== 'function') return false; |
| 63 | + for (const record of registry.getAllPackages() as unknown[]) { |
| 64 | + const declared = (record as { manifest?: { datasources?: unknown } } | null | undefined) |
| 65 | + ?.manifest?.datasources; |
| 66 | + if (Array.isArray(declared) && declared.some((ds) => (ds as { name?: unknown } | null)?.name === name)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + return false; |
| 71 | +} |
| 72 | + |
| 73 | +/** The body of `ObjectStackProtocolImplementation#isHostCodeDatasource`; the method's docblock there is its contract. */ |
| 74 | +export function isHostCodeDatasource( |
| 75 | + this: Pick<CodeDatasourcePredicatesHost, 'getServicesRegistry'>, |
| 76 | + type: string, |
| 77 | + name: string, |
| 78 | +): boolean { |
| 79 | + if ((PLURAL_TO_SINGULAR[type] ?? type) !== 'datasource') return false; |
| 80 | + const hostCode = this.getServicesRegistry?.().get('code-datasource-names') as { has?: unknown } | undefined; |
| 81 | + return typeof hostCode?.has === 'function' && (hostCode as { has(n: string): boolean }).has(name); |
| 82 | +} |
0 commit comments