|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 19] A package write's validation scope, as |
| 5 | + * `ObjectStackProtocolImplementation` resolves it |
| 6 | + * (`resolveWritePackageScope`): the written package and the closure of the |
| 7 | + * dependencies its manifest declares, read from the registry, or `undefined` |
| 8 | + * wherever that cannot be read. Moved out of `protocol.ts` whole. |
| 9 | + * |
| 10 | + * A MOVE, not a redesign. The function below is the body of the |
| 11 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 12 | + * re-indented one level and otherwise unedited: the same guards, the same |
| 13 | + * dependency walk, and the same silent `catch` answering `undefined`. |
| 14 | + * |
| 15 | + * ## How the moved body reaches the class |
| 16 | + * |
| 17 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 18 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 19 | + * The class keeps the method — the published `dist/*.d.ts` lists its name, |
| 20 | + * docblock and declared signature — and its body is one literal of exactly |
| 21 | + * the {@link WritePackageScopeHost} members it reads, plus a `.call`. The |
| 22 | + * engine is handed over as a getter, so the body reads the protocol's engine |
| 23 | + * (and so its registry) at call time. Every other reader of the method keeps |
| 24 | + * calling the class method: `tenantAuthoredWriteRefusal`, and the authoring |
| 25 | + * gate's host (cut 8, `protocol-authoring-gate.ts`), whose closure calls it. |
| 26 | + * |
| 27 | + * The body reads no module-level declaration of `protocol.ts` and no import, |
| 28 | + * so this module imports only the class type. Nothing re-exports this module, |
| 29 | + * so the published surface does not move. |
| 30 | + */ |
| 31 | + |
| 32 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 33 | + |
| 34 | +/** |
| 35 | + * The `ObjectStackProtocolImplementation` members the function below reads — |
| 36 | + * every one of them, and no other; the function's `this` is the `Pick` of the |
| 37 | + * members it reads. Each is typed AS the class member (an indexed access on |
| 38 | + * the class), so a change to a member's signature on the class is a change |
| 39 | + * here too, and naming a member the class does not have is a compile error. |
| 40 | + */ |
| 41 | +export interface WritePackageScopeHost { |
| 42 | + readonly engine: ObjectStackProtocolImplementation['engine']; |
| 43 | +} |
| 44 | + |
| 45 | +/** The body of `ObjectStackProtocolImplementation#resolveWritePackageScope`; the method's docblock there is its contract. */ |
| 46 | +export function resolveWritePackageScope( |
| 47 | + this: Pick<WritePackageScopeHost, 'engine'>, |
| 48 | + packageId: string | null | undefined, |
| 49 | +): { packageId: string; dependencies: string[] } | undefined { |
| 50 | + if (typeof packageId !== 'string' || packageId === '' || packageId === 'sys_metadata') return undefined; |
| 51 | + try { |
| 52 | + const registry = this.engine.registry as |
| 53 | + | { getPackage?: (id: string) => { manifest?: unknown } | undefined } |
| 54 | + | undefined; |
| 55 | + if (typeof registry?.getPackage !== 'function') return undefined; |
| 56 | + // The written package itself must be readable: its manifest is the |
| 57 | + // ONLY declaration of what it may reference, and narrowing without |
| 58 | + // it would be narrowing on nothing. |
| 59 | + if (!registry.getPackage(packageId)) return undefined; |
| 60 | + |
| 61 | + const dependencies = new Set<string>(); |
| 62 | + const frontier = [packageId]; |
| 63 | + while (frontier.length > 0) { |
| 64 | + const current = frontier.pop() as string; |
| 65 | + const manifest = registry.getPackage(current)?.manifest as |
| 66 | + | { dependencies?: Record<string, unknown> } |
| 67 | + | undefined; |
| 68 | + const declared = manifest?.dependencies; |
| 69 | + if (!declared || typeof declared !== 'object') continue; |
| 70 | + for (const dep of Object.keys(declared)) { |
| 71 | + // A dependency the registry cannot resolve is still IN the |
| 72 | + // closure: the package declared it, so an object stamped |
| 73 | + // with it is reachable by declaration. Only the transitive |
| 74 | + // walk stops there — `getPackage` returns undefined and the |
| 75 | + // loop above simply contributes nothing further. |
| 76 | + if (dep === packageId || dependencies.has(dep)) continue; |
| 77 | + dependencies.add(dep); |
| 78 | + frontier.push(dep); |
| 79 | + } |
| 80 | + } |
| 81 | + return { packageId, dependencies: [...dependencies] }; |
| 82 | + } catch { |
| 83 | + // Context gathering must never fail a write (the same contract |
| 84 | + // `listCollection` above holds). An unreadable registry means an |
| 85 | + // unnarrowed gate, not a refused publish. |
| 86 | + return undefined; |
| 87 | + } |
| 88 | +} |
0 commit comments