|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 18] The flow save's retired-spelling replay verdict of |
| 5 | + * `ObjectStackProtocolImplementation` (`flowSaveReplaysRetired`, #23903 |
| 6 | + * ruling B′): whether a flow save may keep the full conversion chain, because |
| 7 | + * every retired spelling its body carries is one a stored row already carries, |
| 8 | + * 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 retired set, the same |
| 13 | + * conversion pass and site naming, and the same stored-row reads in the same |
| 14 | + * order. |
| 15 | + * |
| 16 | + * ## How the moved body reaches the class |
| 17 | + * |
| 18 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 19 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 20 | + * The class keeps the method — the published `dist/*.d.ts` lists its name, |
| 21 | + * docblock and declared signature — and its body is one literal of exactly |
| 22 | + * the {@link FlowSaveReplayHost} members it reads, plus a `.call`. The overlay |
| 23 | + * repository is handed over as a closure that calls the method on the protocol |
| 24 | + * at call time, so a test double or spy set on the instance is still the one |
| 25 | + * the body reaches. Every other reader of the method keeps calling the class |
| 26 | + * method. |
| 27 | + * |
| 28 | + * What the body reads of `protocol.ts`'s own module-level declarations it |
| 29 | + * imports from it; what it imports from elsewhere, it imports from where |
| 30 | + * `protocol.ts` imported it. Nothing re-exports this module, so the published |
| 31 | + * surface does not move. |
| 32 | + */ |
| 33 | + |
| 34 | +import { ALL_CONVERSIONS, applyConversionsToFlow, type ConversionNotice } from '@objectstack/spec'; |
| 35 | +import { itemDiscriminator } from '@objectstack/metadata-core'; |
| 36 | +import type { MetadataWriteFace, ObjectStackProtocolImplementation } from './protocol.js'; |
| 37 | +import { FLOW_FALLBACK_VERDICT_NOT_REPLAYED } from './protocol.js'; |
| 38 | + |
| 39 | +/** |
| 40 | + * The `ObjectStackProtocolImplementation` members the function below reads — |
| 41 | + * every one of them, and no other; the function's `this` is the `Pick` of the |
| 42 | + * members it reads. Each is typed AS the class member (an indexed access on |
| 43 | + * the class), so a change to a member's signature on the class is a change |
| 44 | + * here too, and naming a member the class does not have is a compile error. |
| 45 | + */ |
| 46 | +export interface FlowSaveReplayHost { |
| 47 | + getOverlayRepo: ObjectStackProtocolImplementation['getOverlayRepo']; |
| 48 | +} |
| 49 | + |
| 50 | +/** The body of `ObjectStackProtocolImplementation#flowSaveReplaysRetired`; the method's docblock there is its contract. */ |
| 51 | +export async function flowSaveReplaysRetired( |
| 52 | + this: Pick<FlowSaveReplayHost, 'getOverlayRepo'>, |
| 53 | + request: { |
| 54 | + name: string; |
| 55 | + item?: unknown; |
| 56 | + packageId?: string | null; |
| 57 | + source?: string; |
| 58 | + writeFace?: MetadataWriteFace; |
| 59 | + }, |
| 60 | + mode: 'draft' | 'publish' |
| 61 | +): Promise<boolean> { |
| 62 | + if (request.source !== undefined) return true; |
| 63 | + if (request.writeFace === 'package-duplicate' || request.writeFace === 'package-unmanaged-import') return true; |
| 64 | + const retired = new Set(ALL_CONVERSIONS.filter((c) => c.retiredFromLoadPath).map((c) => c.id)); |
| 65 | + // The site of a notice's path, ids standing in for the indexes of the |
| 66 | + // array elements that carry one, read on the converted body it names. |
| 67 | + const siteOf = (root: unknown, path: string): string => { |
| 68 | + let at: unknown = root; |
| 69 | + let site = ''; |
| 70 | + for (const [, key, index] of path.matchAll(/([^.[\]]+)|\[(\d+)\]/g)) { |
| 71 | + if (key !== undefined) { |
| 72 | + site += site === '' ? key : `.${key}`; |
| 73 | + at = at !== null && typeof at === 'object' ? (at as Record<string, unknown>)[key] : undefined; |
| 74 | + continue; |
| 75 | + } |
| 76 | + const element: unknown = Array.isArray(at) ? at[Number(index)] : undefined; |
| 77 | + const id = element !== null && typeof element === 'object' ? (element as { id?: unknown }).id : undefined; |
| 78 | + site += typeof id === 'string' && id !== '' ? `[#${id}]` : '[' + index + ']'; |
| 79 | + at = element; |
| 80 | + } |
| 81 | + return site; |
| 82 | + }; |
| 83 | + const retiredHits = (body: unknown): Set<string> => { |
| 84 | + const fired: ConversionNotice[] = []; |
| 85 | + const converted = applyConversionsToFlow(body, { |
| 86 | + includeRetired: true, |
| 87 | + excludeConversionIds: FLOW_FALLBACK_VERDICT_NOT_REPLAYED, |
| 88 | + onNotice: (n) => { if (retired.has(n.conversionId)) fired.push(n); }, |
| 89 | + }); |
| 90 | + const root = { flows: [converted] }; |
| 91 | + return new Set(fired.map((n) => `${n.conversionId}|${n.from}|${siteOf(root, n.path)}`)); |
| 92 | + }; |
| 93 | + // The request's retired hits; each one a stored row carries is struck off. |
| 94 | + const uncarried = retiredHits(request.item); |
| 95 | + const strikeCarried = (body: unknown): void => { |
| 96 | + if (uncarried.size === 0) return; |
| 97 | + for (const hit of retiredHits(body)) uncarried.delete(hit); |
| 98 | + }; |
| 99 | + const repo = this.getOverlayRepo(); |
| 100 | + const ref = { type: 'flow', name: request.name, org: 'env' } as Parameters<typeof repo.headAt>[0]; |
| 101 | + const member = itemDiscriminator('flow', request.item); |
| 102 | + const state: 'active' | 'draft' = mode === 'draft' ? 'draft' : 'active'; |
| 103 | + const target = await repo.headAt(ref, { state, packageId: request.packageId ?? null, member }); |
| 104 | + if (target.head) { |
| 105 | + strikeCarried(target.head.body); |
| 106 | + if (uncarried.size === 0) return true; |
| 107 | + if (state !== 'draft') return false; |
| 108 | + } else if (state !== 'draft') { |
| 109 | + return false; |
| 110 | + } |
| 111 | + const active = (await repo.headAt(ref, { state: 'active', packageId: target.packageId, member })).head; |
| 112 | + if (!active) return false; |
| 113 | + strikeCarried(active.body); |
| 114 | + return uncarried.size === 0; |
| 115 | +} |
0 commit comments