Skip to content

Commit 1cb285a

Browse files
committed
refactor(metadata-protocol): move the metadata-service code-layer read out of protocol.ts (#25467 cut 20)
Move the body of readItemFromMetadataService unedited into protocol-metadata-service-read.ts, behind a MetadataServiceReadHost that hands over the getServicesRegistry field as a getter. The class keeps the method, its docblock and declared signature; its body becomes a host literal plus a .call. Its callers keep calling the class method. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent e2129cb commit 1cb285a

2 files changed

Lines changed: 101 additions & 37 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25467 cut 20] The code-layer read of `ObjectStackProtocolImplementation`
5+
* through the `metadata` service (`readItemFromMetadataService`): one item,
6+
* package-scoped (ADR-0048), its diagnosis when the service gives one, the
7+
* singular/plural spelling as a fallback, and a stored object row's copy not
8+
* counted as a code layer (#23645). 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 service lookup, the
13+
* same two reads in the same order, and the same merged answer.
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 MetadataServiceReadHost} members it reads, plus a `.call`. The
22+
* `getServicesRegistry` field is handed over as a getter, as
23+
* `protocol-package-install.ts` and `protocol-save-door-refusals.ts` hand it,
24+
* so the body reads the protocol's field at call time. Every other reader of
25+
* the method keeps calling the class method.
26+
*
27+
* What the body reads of `protocol.ts`'s own module-level declarations it
28+
* imports from it; what it imports from elsewhere, it imports from where
29+
* `protocol.ts` imported it. Nothing re-exports this module, so the published
30+
* surface does not move.
31+
*/
32+
33+
import { isTenantAuthored } from '@objectstack/metadata-core';
34+
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
35+
import type { ObjectStackProtocolImplementation } from './protocol.js';
36+
import { canonicalMetaType } from './protocol.js';
37+
38+
/**
39+
* The `ObjectStackProtocolImplementation` members the function below reads —
40+
* every one of them, and no other; the function's `this` is the `Pick` of the
41+
* members it reads. Each is typed AS the class member (an indexed access on
42+
* the class), so a change to a member's signature on the class is a change
43+
* here too, and naming a member the class does not have is a compile error.
44+
*/
45+
export interface MetadataServiceReadHost {
46+
readonly getServicesRegistry: ObjectStackProtocolImplementation['getServicesRegistry'];
47+
}
48+
49+
/** The body of `ObjectStackProtocolImplementation#readItemFromMetadataService`; the method's docblock there is its contract. */
50+
export async function readItemFromMetadataService(
51+
this: Pick<MetadataServiceReadHost, 'getServicesRegistry'>,
52+
type: string,
53+
name: string,
54+
packageId?: string,
55+
): Promise<{ data: unknown; degraded: boolean; errors: string[] }> {
56+
const services = this.getServicesRegistry?.();
57+
const metadataService: any = services?.get('metadata');
58+
if (!metadataService || typeof metadataService.get !== 'function') {
59+
return { data: undefined, degraded: false, errors: [] };
60+
}
61+
// ADR-0048 — thread the caller's package id so a single-item fetch is
62+
// package-scoped. Passed positionally to BOTH reads, so whatever the
63+
// occupant of the `metadata` slot makes of a third argument today is
64+
// unchanged by which of the two methods answers.
65+
const read = async (t: string): Promise<{ data: unknown; degraded: boolean; errors: string[] }> => {
66+
let answer: { data: unknown; degraded: boolean; errors: string[] };
67+
if (typeof metadataService.getDiagnosed === 'function') {
68+
const diagnosed = await metadataService.getDiagnosed(t, name, packageId);
69+
answer = {
70+
data: diagnosed?.data,
71+
degraded: diagnosed?.degraded === true,
72+
errors: Array.isArray(diagnosed?.errors) ? diagnosed.errors : [],
73+
};
74+
} else {
75+
answer = { data: await metadataService.get(t, name, packageId), degraded: false, errors: [] };
76+
}
77+
// [#23645] A stored object row's copy is not a code layer. See the header.
78+
return canonicalMetaType(t) === 'object' && isTenantAuthored(answer.data)
79+
? { ...answer, data: undefined }
80+
: answer;
81+
};
82+
83+
const primary = await read(type);
84+
if (primary.data !== undefined && primary.data !== null) return primary;
85+
const alt = PLURAL_TO_SINGULAR[type] ?? SINGULAR_TO_PLURAL[type];
86+
if (!alt) return { data: undefined, degraded: primary.degraded, errors: primary.errors };
87+
const secondary = await read(alt);
88+
if (secondary.data !== undefined && secondary.data !== null) return secondary;
89+
return {
90+
data: undefined,
91+
degraded: primary.degraded || secondary.degraded,
92+
errors: [...primary.errors, ...secondary.errors],
93+
};
94+
}

‎packages/metadata-protocol/src/protocol.ts‎

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ import { classifyObjectContribution, releaseAdoptedObjectOwner } from './protoco
404404
import { flowSaveReplaysRetired } from './protocol-flow-save-replay.js';
405405
// [#25467 cut 19] A package write's validation scope: the written package and its declared dependency closure, from the registry.
406406
import { resolveWritePackageScope } from './protocol-write-package-scope.js';
407+
// [#25467 cut 20] The code-layer read through the `metadata` service: package-scoped, singular/plural fallback, a stored object row's copy not counted as code.
408+
import { readItemFromMetadataService } from './protocol-metadata-service-read.js';
407409

408410
/**
409411
* Canonical Zod schema per metadata type lives in
@@ -9330,44 +9332,12 @@ export class ObjectStackProtocolImplementation implements
93309332
name: string,
93319333
packageId?: string,
93329334
): Promise<{ data: unknown; degraded: boolean; errors: string[] }> {
9333-
const services = this.getServicesRegistry?.();
9334-
const metadataService: any = services?.get('metadata');
9335-
if (!metadataService || typeof metadataService.get !== 'function') {
9336-
return { data: undefined, degraded: false, errors: [] };
9337-
}
9338-
// ADR-0048 — thread the caller's package id so a single-item fetch is
9339-
// package-scoped. Passed positionally to BOTH reads, so whatever the
9340-
// occupant of the `metadata` slot makes of a third argument today is
9341-
// unchanged by which of the two methods answers.
9342-
const read = async (t: string): Promise<{ data: unknown; degraded: boolean; errors: string[] }> => {
9343-
let answer: { data: unknown; degraded: boolean; errors: string[] };
9344-
if (typeof metadataService.getDiagnosed === 'function') {
9345-
const diagnosed = await metadataService.getDiagnosed(t, name, packageId);
9346-
answer = {
9347-
data: diagnosed?.data,
9348-
degraded: diagnosed?.degraded === true,
9349-
errors: Array.isArray(diagnosed?.errors) ? diagnosed.errors : [],
9350-
};
9351-
} else {
9352-
answer = { data: await metadataService.get(t, name, packageId), degraded: false, errors: [] };
9353-
}
9354-
// [#23645] A stored object row's copy is not a code layer. See the header.
9355-
return canonicalMetaType(t) === 'object' && isTenantAuthored(answer.data)
9356-
? { ...answer, data: undefined }
9357-
: answer;
9358-
};
9359-
9360-
const primary = await read(type);
9361-
if (primary.data !== undefined && primary.data !== null) return primary;
9362-
const alt = PLURAL_TO_SINGULAR[type] ?? SINGULAR_TO_PLURAL[type];
9363-
if (!alt) return { data: undefined, degraded: primary.degraded, errors: primary.errors };
9364-
const secondary = await read(alt);
9365-
if (secondary.data !== undefined && secondary.data !== null) return secondary;
9366-
return {
9367-
data: undefined,
9368-
degraded: primary.degraded || secondary.degraded,
9369-
errors: [...primary.errors, ...secondary.errors],
9335+
// [#25467 cut 20] The body moved to `protocol-metadata-service-read.ts` (see `MetadataServiceReadHost`).
9336+
const readGetServicesRegistry = () => this.getServicesRegistry;
9337+
const host: ThisParameterType<typeof readItemFromMetadataService> = {
9338+
get getServicesRegistry() { return readGetServicesRegistry(); },
93709339
};
9340+
return readItemFromMetadataService.call(host, type, name, packageId);
93719341
}
93729342

93739343
/**

0 commit comments

Comments
 (0)