Skip to content

Commit 6b074fc

Browse files
committed
refactor(metadata-protocol): move the bodies of eight members out of protocol.ts into four themed modules (#25467 cut 35)
Cut 35 of the protocol.ts split, under ruling B: the read-seam census now follows a moved body across the host split, so the members it alone rejected at cuts 33 and 34 move. - protocol-environment-rows.ts: findEnvironmentRows, findEnvironmentRow, findServedOverlayRow, packagelessAddress - protocol-stored-collection-fold.ts: foldStoredCollection - protocol-package-uninstall.ts: deletePackage - protocol-unmanaged-import.ts: unmanagedImportCollisions, rollBackUnmanagedImport Shape B as cuts 20-34: the class keeps each method, its docblock and signature, and its body becomes one host literal plus a .call. Four top-level helpers the bodies read gain `export` in place; index.ts names none of them. The bracketed-refusal pin gets PRODUCERS rows for the two new modules with new Error( sites. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4czJUan4AwAS6HLpzZN
1 parent 2e955dd commit 6b074fc

6 files changed

Lines changed: 895 additions & 542 deletions
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25467 cut 35] The environment's own `sys_metadata` rows (ADR-0131
5+
* D6/D7), from `ObjectStackProtocolImplementation`, moved out of
6+
* `protocol.ts` whole:
7+
*
8+
* - `findEnvironmentRows` — the family read, without a legacy
9+
* organization's rows;
10+
* - `findEnvironmentRow` — the same for one row;
11+
* - `findServedOverlayRow` — the stored row a by-name read serves;
12+
* - `packagelessAddress` — the package a write that names none addresses.
13+
*
14+
* A MOVE, not a redesign. Each function below is the body of the
15+
* `ObjectStackProtocolImplementation` method of the same name as it stood,
16+
* re-indented one level and otherwise unedited.
17+
*
18+
* ## How the moved bodies reach the class
19+
*
20+
* The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts`
21+
* header carries the full reasoning), as `protocol-batch-data.ts` applies it.
22+
* The class keeps each method — the published `dist/*.d.ts` lists its name,
23+
* docblock and declared signature — and its body is one literal of exactly
24+
* the {@link EnvironmentRowsHost} members it reads, plus a `.call`. The `engine`
25+
* field is handed over as a getter, so each read reaches the class field as
26+
* it stands at call time; each call to another member is a closure, so it
27+
* still reaches the class method.
28+
* Every caller keeps calling the class methods.
29+
*
30+
* The read-seam census follows both directions of this shape (its
31+
* `HOST_SPLIT` note in `scripts/check-durability-degradation-log-level.mjs`),
32+
* so the storage reads below are still counted as the seams they were.
33+
*
34+
* What the bodies read from elsewhere, they import from where `protocol.ts`
35+
* imports it. Nothing re-exports this module, so the published surface does
36+
* not move.
37+
*/
38+
39+
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
40+
import type { StoredOverlayRow } from './item-lock.js';
41+
import { overlayMemberWhere } from './migrations/overlay-index.js';
42+
import type { ObjectStackProtocolImplementation } from './protocol.js';
43+
import { rowMatchesOverlayWhere, servedOverlayRowCandidates } from './protocol.js';
44+
45+
/**
46+
* The `ObjectStackProtocolImplementation` members the functions below read —
47+
* every one of them, and no other; each function's `this` is the `Pick` of
48+
* the members it reads. Each is typed AS the class member (an indexed access
49+
* on the class), so a change to a member's signature on the class is a change
50+
* here too, and naming a member the class does not have is a compile error.
51+
*/
52+
export interface EnvironmentRowsHost {
53+
readonly engine: ObjectStackProtocolImplementation['engine'];
54+
readonly findEnvironmentRow: ObjectStackProtocolImplementation['findEnvironmentRow'];
55+
readonly findEnvironmentRows: ObjectStackProtocolImplementation['findEnvironmentRows'];
56+
readonly legacyOrganizationRowsOf: ObjectStackProtocolImplementation['legacyOrganizationRowsOf'];
57+
readonly rethrowUnlessMetadataStoreUnprovisioned: ObjectStackProtocolImplementation['rethrowUnlessMetadataStoreUnprovisioned'];
58+
readonly withoutLegacyOrganizationRows: ObjectStackProtocolImplementation['withoutLegacyOrganizationRows'];
59+
}
60+
61+
/** The body of `ObjectStackProtocolImplementation#findEnvironmentRows`; the method's docblock there is its contract. */
62+
export async function findEnvironmentRows(this: Pick<EnvironmentRowsHost, 'engine' | 'legacyOrganizationRowsOf' | 'withoutLegacyOrganizationRows'>, table: string, query: { where: Record<string, unknown>; context?: unknown; [key: string]: unknown }): Promise<any[]> {
63+
const rows = (await this.engine.find(table, query as never)) as any[] | null | undefined;
64+
if (query.limit === undefined && query.offset === undefined) {
65+
return this.withoutLegacyOrganizationRows(table, rows, query.context);
66+
}
67+
// An offset counts rows before the page, so any legacy row can shift
68+
// it; a limit alone, only one the page itself holds.
69+
if ((!rows || rows.length === 0) && query.offset === undefined) return rows ?? [];
70+
const legacy = await this.legacyOrganizationRowsOf(table, query.context);
71+
if (legacy.size === 0) return rows ?? [];
72+
if (query.offset === undefined && !(rows ?? []).some((row) => legacy.has(String(row?.id)))) return rows ?? [];
73+
const where = { $and: [query.where, { id: { $nin: [...legacy.keys()] } }] };
74+
return ((await this.engine.find(table, { ...query, where } as never)) as any[] | null | undefined) ?? [];
75+
}
76+
77+
/** The body of `ObjectStackProtocolImplementation#findEnvironmentRow`; the method's docblock there is its contract. */
78+
export async function findEnvironmentRow(this: Pick<EnvironmentRowsHost, 'engine' | 'legacyOrganizationRowsOf'>, table: string, query: { where: Record<string, unknown>; context?: unknown; [key: string]: unknown }): Promise<any> {
79+
const row = await this.engine.findOne(table, query as never);
80+
if (!row) return row ?? null;
81+
const legacy = await this.legacyOrganizationRowsOf(table, query.context);
82+
if (!legacy.has(String((row as { id?: unknown }).id))) return row;
83+
const rows = (await this.engine.find(table, query as never)) as any[] | null | undefined;
84+
return (rows ?? []).find((r) => !legacy.has(String(r?.id))) ?? null;
85+
}
86+
87+
/** The body of `ObjectStackProtocolImplementation#findServedOverlayRow`; the method's docblock there is its contract. */
88+
export async function findServedOverlayRow(
89+
this: Pick<EnvironmentRowsHost, 'findEnvironmentRow'>,
90+
args: {
91+
type: string;
92+
name: string;
93+
state: 'active' | 'draft';
94+
/** [#23715] `null`: the package-unbound row alone (see {@link servedOverlayRowCandidates}). */
95+
packageId?: string | null;
96+
/** [#22877] The bundle member a by-name door's selector named; omitted, the canonical one. */
97+
member?: string;
98+
},
99+
activeRows?: readonly StoredOverlayRow[],
100+
): Promise<{ row: any } | undefined> {
101+
// [#21804] The order is {@link servedOverlayRowCandidates}, the one
102+
// the list's merge takes over the rows it read: the canonical
103+
// spelling then the other, and within each, the package's
104+
// own row then the package-less row (never another package's, or a
105+
// collision would serve package B's customization for a package A
106+
// read); with no package context (legacy/runtime reader), any row.
107+
const other = PLURAL_TO_SINGULAR[args.type] ?? SINGULAR_TO_PLURAL[args.type];
108+
for (const candidate of servedOverlayRowCandidates({ packageId: args.packageId })) {
109+
const type = candidate.spelling === 'canonical' ? args.type : other;
110+
if (type === undefined) continue;
111+
const where: Record<string, unknown> = {
112+
type, name: args.name, state: args.state,
113+
// [#22877] A by-name read of a bundled type answers the member
114+
// its selector named, else its CANONICAL member, as the
115+
// registry's bare-name read does; nothing for every other type.
116+
...overlayMemberWhere(type, args.member),
117+
};
118+
if (candidate.packageId !== undefined) where.package_id = candidate.packageId;
119+
// [#21911, ADR-0096] The explicit system opt-in: a platform store
120+
// read, which no caller's grants scope — the door that asked
121+
// already authorized the caller, and the protocol scopes the row
122+
// itself ({@link findEnvironmentRow}: the environment's rows only).
123+
// It no longer reaches the data engine as a principal-less context.
124+
const row = activeRows !== undefined && args.state === 'active'
125+
? activeRows.find((stored) => rowMatchesOverlayWhere(stored, where))
126+
: await this.findEnvironmentRow('sys_metadata', { where, context: { isSystem: true } });
127+
if (row) return { row };
128+
}
129+
return undefined;
130+
}
131+
132+
/** The body of `ObjectStackProtocolImplementation#packagelessAddress`; the method's docblock there is its contract. */
133+
export async function packagelessAddress(
134+
this: Pick<EnvironmentRowsHost, 'findEnvironmentRows' | 'rethrowUnlessMetadataStoreUnprovisioned'>,
135+
type: string,
136+
name: string,
137+
member?: string,
138+
state: 'active' | 'draft' = 'active',
139+
activeRows?: readonly StoredOverlayRow[],
140+
): Promise<{ packageId: string | null; candidates: readonly string[]; unbound: boolean }> {
141+
let rows: Array<{ package_id?: unknown }> | null | undefined;
142+
const where = { type, name, state, ...overlayMemberWhere(type, member) };
143+
try {
144+
// [#21911] The explicit system opt-in — see findServedOverlayRow.
145+
rows = activeRows !== undefined && state === 'active'
146+
? activeRows.filter((row) => rowMatchesOverlayWhere(row, where))
147+
: await this.findEnvironmentRows('sys_metadata', { where, context: { isSystem: true } });
148+
} catch (error) {
149+
// [#5532] An unprovisioned store holds no row, so no binding; any
150+
// other failure is a read that did not happen, never "no binding".
151+
this.rethrowUnlessMetadataStoreUnprovisioned(error, 'sys_metadata');
152+
}
153+
const bindings = new Set<string>();
154+
let unbound = false;
155+
for (const row of rows ?? []) {
156+
if (typeof row?.package_id === 'string' && row.package_id !== '') bindings.add(row.package_id);
157+
else unbound = true;
158+
}
159+
const candidates = [...bindings].sort();
160+
return { packageId: candidates.length === 1 ? candidates[0] ?? null : null, candidates, unbound };
161+
}

0 commit comments

Comments
 (0)