Skip to content

Commit b542764

Browse files
committed
refactor(metadata-protocol): move the object-extender fold out of protocol.ts into protocol-object-extender-fold.ts (#25467 cut 30)
Move-only, shape B. foldObjectExtendersFromRegistry moves, unedited, into its own module. The engine field is handed over as a getter. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent cbb3a1a commit b542764

2 files changed

Lines changed: 76 additions & 18 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25467 cut 30] The object-extender fold, from
5+
* `ObjectStackProtocolImplementation`, moved out of `protocol.ts` whole:
6+
* `foldObjectExtendersFromRegistry`, which folds the registry's `extend`
7+
* contributors onto an OBJECT body that came from the MetadataService, so the
8+
* by-name read serves the object's resolved schema as the list read does
9+
* (#7556).
10+
*
11+
* A MOVE, not a redesign. The function below is the body of the
12+
* `ObjectStackProtocolImplementation` method of the same name as it stood,
13+
* re-indented one level and otherwise unedited.
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 ObjectExtenderFoldHost} member it reads, plus a `.call`. The
22+
* `engine` field is handed over as a getter, so the read finds it as it
23+
* stands at call time. Every caller keeps calling the class method.
24+
*
25+
* What the body reads from elsewhere, it imports from where `protocol.ts`
26+
* imports it. Nothing re-exports this module, so the published surface does
27+
* not move.
28+
*/
29+
30+
import { PLURAL_TO_SINGULAR } from '@objectstack/spec/shared';
31+
import type { ObjectStackProtocolImplementation } from './protocol.js';
32+
33+
/**
34+
* The `ObjectStackProtocolImplementation` member the function below reads —
35+
* the only one; the function's `this` is the `Pick` of it. It is typed AS the
36+
* class member (an indexed access on the class), so a change to its
37+
* signature on the class is a change here too.
38+
*/
39+
export interface ObjectExtenderFoldHost {
40+
readonly engine: ObjectStackProtocolImplementation['engine'];
41+
}
42+
43+
/** The body of `ObjectStackProtocolImplementation#foldObjectExtendersFromRegistry`; the method's docblock there is its contract. */
44+
export function foldObjectExtendersFromRegistry(
45+
this: Pick<ObjectExtenderFoldHost, 'engine'>,
46+
type: string,
47+
name: unknown,
48+
body: unknown,
49+
): unknown {
50+
const singular = PLURAL_TO_SINGULAR[type] ?? type;
51+
if (singular !== 'object') return body;
52+
if (body === null || typeof body !== 'object') return body;
53+
// [#8027] The list caller reads the name off the ROW rather than off a
54+
// request, and a row with no usable `name` has no contributor list to
55+
// look up — `mergePackageAwareOverlay` skips such rows too.
56+
if (typeof name !== 'string' || name === '') return body;
57+
const registry = (this.engine as any)?.registry;
58+
// Partial registry doubles in tests predate this method; a host that
59+
// cannot fold answers exactly as it did before.
60+
if (!registry || typeof registry.foldObjectExtendersOnto !== 'function') return body;
61+
try {
62+
return registry.foldObjectExtendersOnto(name, body);
63+
} catch {
64+
// The fold is a read over in-memory contributors; a failure here
65+
// must not turn a served schema into a 5xx.
66+
return body;
67+
}
68+
}

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ import { flowRegistrationsOf, resolveFlowCanonicalizer, resolveFlowRegistrationR
423423
import { isDeclaredCodeDatasource, isHostCodeDatasource } from './protocol-code-datasource-predicates.js';
424424
// [#25467 cut 29] The legacy-organization row filters: which rows a not-yet-migrated database still holds.
425425
import { legacyOrganizationRowsOf, withoutLegacyOrganizationRows } from './protocol-legacy-organization-rows.js';
426+
// [#25467 cut 30] The object-extender fold onto a MetadataService object body.
427+
import { foldObjectExtendersFromRegistry } from './protocol-object-extender-fold.js';
426428

427429
/**
428430
* Canonical Zod schema per metadata type lives in
@@ -8899,24 +8901,12 @@ export class ObjectStackProtocolImplementation implements
88998901
* would duplicate both.
89008902
*/
89018903
private foldObjectExtendersFromRegistry(type: string, name: unknown, body: unknown): unknown {
8902-
const singular = PLURAL_TO_SINGULAR[type] ?? type;
8903-
if (singular !== 'object') return body;
8904-
if (body === null || typeof body !== 'object') return body;
8905-
// [#8027] The list caller reads the name off the ROW rather than off a
8906-
// request, and a row with no usable `name` has no contributor list to
8907-
// look up — `mergePackageAwareOverlay` skips such rows too.
8908-
if (typeof name !== 'string' || name === '') return body;
8909-
const registry = (this.engine as any)?.registry;
8910-
// Partial registry doubles in tests predate this method; a host that
8911-
// cannot fold answers exactly as it did before.
8912-
if (!registry || typeof registry.foldObjectExtendersOnto !== 'function') return body;
8913-
try {
8914-
return registry.foldObjectExtendersOnto(name, body);
8915-
} catch {
8916-
// The fold is a read over in-memory contributors; a failure here
8917-
// must not turn a served schema into a 5xx.
8918-
return body;
8919-
}
8904+
// [#25467 cut 30] The body moved to `protocol-object-extender-fold.ts` (see `ObjectExtenderFoldHost`).
8905+
const readEngine = () => this.engine;
8906+
const host: ThisParameterType<typeof foldObjectExtendersFromRegistry> = {
8907+
get engine() { return readEngine(); },
8908+
};
8909+
return foldObjectExtendersFromRegistry.call(host, type, name, body);
89208910
}
89218911

89228912
/**

0 commit comments

Comments
 (0)