|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29985 codemod] The "Write gates: overlay, code-only, organization, sealed |
| 5 | + * and packaged-base refusals, locks, version tokens, conflict audit" section of |
| 6 | + * `ObjectStackProtocolImplementation`: the bodies of `isArtifactBacked`, |
| 7 | + * `isNestedArtifactField`, `packagedBaseRefusal`, `packagedArtifactOwner`, |
| 8 | + * `storedFlowBindingAgrees`, `refusePackagedBaseRemoval`, |
| 9 | + * `baseServesBundleMember`, `pairOfAddedMember`, `findPairAddedMemberRow`, |
| 10 | + * `registryMemberServedAt` moved out of `protocol.ts` whole by |
| 11 | + * `scripts/codemod-split-protocol.mjs`. |
| 12 | + * |
| 13 | + * A MOVE, not a redesign. Each function below is the body of the class method |
| 14 | + * of the same name as `packages/metadata-protocol/scripts/host-split.mjs` moves |
| 15 | + * one (ruling E, item E1): re-indented one level, with `this.<member>` read as |
| 16 | + * `host.<member>` and nothing else changed. Each method stays on the class |
| 17 | + * with its docblock and signature, which the published `.d.ts` prints, and |
| 18 | + * hands its body the host (`protocol-host.ts`); the docblock there is the |
| 19 | + * contract. |
| 20 | + * |
| 21 | + * What a body reads from elsewhere, it imports from where `protocol.ts` |
| 22 | + * imports it. Nothing re-exports this module, so the published surface does |
| 23 | + * not move. |
| 24 | + */ |
| 25 | + |
| 26 | +import { managedItemSealedSentence } from './packaged-base-regime.js'; |
| 27 | +import type { StoredOverlayRow } from './item-lock.js'; |
| 28 | +import { itemDiscriminator } from '@objectstack/metadata-core'; |
| 29 | +import { PLURAL_TO_SINGULAR } from '@objectstack/spec/shared'; |
| 30 | +import { customizationPackageId } from '@objectstack/spec/kernel'; |
| 31 | +import { baseAddressServesMember, shippingPackageIds } from './protocol-environment-rows.js'; |
| 32 | +import { canonicalMetaType, canonicalizeMetaRequestType } from './protocol-meta-type-schema.js'; |
| 33 | +import { boundPackageOf } from './protocol-package-bindings.js'; |
| 34 | +import type { ShippingPackages } from './protocol-package-bindings.js'; |
| 35 | +import { memberOption } from './protocol-bundle-members.js'; |
| 36 | +import type { ProtocolHost } from './protocol-host.js'; |
| 37 | + |
| 38 | +/** The body of `ObjectStackProtocolImplementation#isArtifactBacked`; the method's docblock there is its contract. */ |
| 39 | +export function isArtifactBacked(host: Pick<ProtocolHost, 'isDeclaredCodeDatasource' | 'isNestedArtifactField' | 'lookupArtifactItem'>, type: string, name: string): boolean { |
| 40 | + // `lookupArtifactItem` only returns items whose `_packageId` marks a |
| 41 | + // genuine code package (the `'sys_metadata'` rehydration sentinel is |
| 42 | + // excluded), and — via `SchemaRegistry.getArtifactItem` — is immune |
| 43 | + // to plain-key shadows hydrated from overlay rows. |
| 44 | + if (host.lookupArtifactItem(type, name) !== undefined) return true; |
| 45 | + return host.isNestedArtifactField(type, name) || host.isDeclaredCodeDatasource(type, name); |
| 46 | +} |
| 47 | + |
| 48 | +/** The body of `ObjectStackProtocolImplementation#isNestedArtifactField`; the method's docblock there is its contract. */ |
| 49 | +export function isNestedArtifactField(host: Pick<ProtocolHost, 'lookupArtifactItem'>, type: string, name: string): boolean { |
| 50 | + if ((PLURAL_TO_SINGULAR[type] ?? type) !== 'field') return false; |
| 51 | + // `<object>.<field>` — both halves are snake_case and carry no dot of |
| 52 | + // their own, so the FIRST separator is the only one. |
| 53 | + const sep = name.indexOf('.'); |
| 54 | + if (sep <= 0 || sep === name.length - 1) return false; |
| 55 | + const objectArtifact = host.lookupArtifactItem('object', name.slice(0, sep)) as |
| 56 | + { fields?: Record<string, unknown> } | undefined; |
| 57 | + const fields = objectArtifact?.fields; |
| 58 | + if (!fields || typeof fields !== 'object') return false; |
| 59 | + return Object.prototype.hasOwnProperty.call(fields, name.slice(sep + 1)); |
| 60 | +} |
| 61 | + |
| 62 | +/** The body of `ObjectStackProtocolImplementation#packagedBaseRefusal`; the method's docblock there is its contract. */ |
| 63 | +export function packagedBaseRefusal(host: Pick<ProtocolHost, 'isPackagedBaseRefusal' | 'refusePackagedBaseOverride' | 'refusePackagedBaseRemoval'>, request: { type: string; name: string; operation: 'save' | 'delete'; packageId?: string | null }): Error | null { |
| 64 | + // [#9009] Folded HERE, at the producer of the verdict, so a caller that |
| 65 | + // arrives with a plural spelling cannot address around the lock. |
| 66 | + const folded = canonicalizeMetaRequestType(request); |
| 67 | + try { |
| 68 | + if (folded.operation === 'delete') host.refusePackagedBaseRemoval(folded); |
| 69 | + else { |
| 70 | + host.refusePackagedBaseOverride({ |
| 71 | + type: folded.type, |
| 72 | + name: folded.name, |
| 73 | + ...(folded.packageId ? { packageId: folded.packageId } : {}), |
| 74 | + }); |
| 75 | + } |
| 76 | + } catch (err) { |
| 77 | + if (host.isPackagedBaseRefusal(err)) return err; |
| 78 | + throw err; |
| 79 | + } |
| 80 | + return null; |
| 81 | +} |
| 82 | + |
| 83 | +/** The body of `ObjectStackProtocolImplementation#packagedArtifactOwner`; the method's docblock there is its contract. */ |
| 84 | +export function packagedArtifactOwner(host: Pick<ProtocolHost, 'lookupArtifactItem'>, request: { type: string; name: string }): string | undefined { |
| 85 | + const folded = canonicalizeMetaRequestType(request); |
| 86 | + const artifact = host.lookupArtifactItem(folded.type, folded.name) as { _packageId?: unknown } | undefined; |
| 87 | + const packageId = artifact?._packageId; |
| 88 | + return typeof packageId === 'string' && packageId !== '' ? packageId : undefined; |
| 89 | +} |
| 90 | + |
| 91 | +/** The body of `ObjectStackProtocolImplementation#storedFlowBindingAgrees`; the method's docblock there is its contract. */ |
| 92 | +export async function storedFlowBindingAgrees(host: Pick<ProtocolHost, 'findEnvironmentRow' | 'rethrowUnlessMetadataStoreUnprovisioned'>, name: string, packageId: string): Promise<boolean> { |
| 93 | + try { |
| 94 | + // [#21911] The explicit system opt-in — see findServedOverlayRow. |
| 95 | + const row = await host.findEnvironmentRow('sys_metadata', { |
| 96 | + where: { type: 'flow', name, package_id: packageId }, |
| 97 | + context: { isSystem: true }, |
| 98 | + }); |
| 99 | + return row != null; |
| 100 | + } catch (error) { |
| 101 | + host.rethrowUnlessMetadataStoreUnprovisioned(error, 'sys_metadata'); |
| 102 | + return false; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** The body of `ObjectStackProtocolImplementation#refusePackagedBaseRemoval`; the method's docblock there is its contract. */ |
| 107 | +export function refusePackagedBaseRemoval(host: Pick<ProtocolHost, 'isSealedManagedItem' | 'mergesOverlayAtRead'>, request: { type: string; name: string }): void { |
| 108 | + const legacyOverlayRemoval = host |
| 109 | + .mergesOverlayAtRead(request.type); |
| 110 | + if (host.isSealedManagedItem(request.type, request.name) && !legacyOverlayRemoval) { |
| 111 | + // [#20819, ADR-0131 D6] Sentence per ADR-0126 regime, as in the save door. |
| 112 | + const err = new Error(managedItemSealedSentence(request.type, request.name, 'delete')); |
| 113 | + (err as any).code = 'NOT_OVERRIDABLE'; |
| 114 | + (err as any).status = 403; |
| 115 | + throw err; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** The body of `ObjectStackProtocolImplementation#baseServesBundleMember`; the method's docblock there is its contract. */ |
| 120 | +export function baseServesBundleMember(host: Pick<ProtocolHost, 'shippedArtifactsOf'>, type: string, name: string, base: string, item: unknown, shipping: ShippingPackages): boolean { |
| 121 | + const member = itemDiscriminator(canonicalMetaType(type), item); |
| 122 | + const baseShipsMember = shippingPackageIds(host.shippedArtifactsOf(type, name, shipping, member)).has(base); |
| 123 | + return baseAddressServesMember(base, boundPackageOf((item as { _packageId?: unknown } | null | undefined)?._packageId), baseShipsMember); |
| 124 | +} |
| 125 | + |
| 126 | +/** The body of `ObjectStackProtocolImplementation#pairOfAddedMember`; the method's docblock there is its contract. */ |
| 127 | +export function pairOfAddedMember(host: Pick<ProtocolHost, 'shippedArtifactsOf' | 'shippingPackagesOf' | 'shipsBundleName'>, type: string, name: string, base: string, member: string): string | undefined { |
| 128 | + const shipping = host.shippingPackagesOf(type); |
| 129 | + if (!host.shipsBundleName(type, name, base, shipping)) return undefined; |
| 130 | + if (shippingPackageIds(host.shippedArtifactsOf(type, name, shipping, member)).has(base)) return undefined; |
| 131 | + const pair = customizationPackageId(base); |
| 132 | + return pair.ok ? pair.value : undefined; |
| 133 | +} |
| 134 | + |
| 135 | +/** The body of `ObjectStackProtocolImplementation#findPairAddedMemberRow`; the method's docblock there is its contract. */ |
| 136 | +export async function findPairAddedMemberRow(host: Pick<ProtocolHost, 'findServedOverlayRow' | 'pairOfAddedMember'>, type: string, name: string, base: string, member: string | undefined, wantedMember: string, |
| 137 | + activeRows?: readonly StoredOverlayRow[],): Promise<any> { |
| 138 | + const pairOfAddition = host.pairOfAddedMember(type, name, base, wantedMember); |
| 139 | + if (pairOfAddition === undefined) return undefined; |
| 140 | + return (await host.findServedOverlayRow({ |
| 141 | + type, name, state: 'active', packageId: pairOfAddition, ...memberOption(member), |
| 142 | + }, activeRows))?.row; |
| 143 | +} |
| 144 | + |
| 145 | +/** The body of `ObjectStackProtocolImplementation#registryMemberServedAt`; the method's docblock there is its contract. */ |
| 146 | +export function registryMemberServedAt(host: Pick<ProtocolHost, 'baseServesBundleMember' | 'engine' | 'shippedArtifactsOf' | 'shippingPackagesOf' | 'shipsBundleName'>, type: string, name: string, base: string, item: unknown, member: string | undefined): unknown { |
| 147 | + const shipping = host.shippingPackagesOf(type); |
| 148 | + if (!host.shipsBundleName(type, name, base, shipping)) return item; |
| 149 | + if (host.baseServesBundleMember(type, name, base, item, shipping)) return item; |
| 150 | + if (member !== undefined) return undefined; |
| 151 | + const own = host.shippedArtifactsOf(type, name, shipping) |
| 152 | + .find((artifact) => (artifact as { _packageId?: unknown } | null)?._packageId === base); |
| 153 | + if (own === undefined) return undefined; |
| 154 | + const candidate = host.engine.registry.getItem(type, name, base, itemDiscriminator(canonicalMetaType(type), own)); |
| 155 | + return candidate !== undefined && host.baseServesBundleMember(type, name, base, candidate, shipping) ? candidate : undefined; |
| 156 | +} |
0 commit comments