|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30052 codemod] The "Scoped execution context: repositories, elevation, |
| 5 | + * transactions" section: the bodies of `ScopedContext`'s methods `object`, |
| 6 | + * `sudo`, `withRunAs`, `releaseJoinedHandle`, `txDriver`, `beginTransaction`, |
| 7 | + * `commitTransaction`, `rollbackTransaction`, `serveFormulaField` moved out of |
| 8 | + * `engine.ts` whole by `scripts/codemod-split-engine.mjs` (its second pass). |
| 9 | + * |
| 10 | + * A MOVE, not a redesign. {@link ScopedContextFacet} is a stateless facet of |
| 11 | + * `ScopedContext`: each static method of it is the body of the `ScopedContext` |
| 12 | + * method of the same name exactly as it stood: not re-indented, not formatted, |
| 13 | + * not edited. The method stays on the class as a one-call stub with its |
| 14 | + * signature, so the published surface, an instance spy and a subclass override |
| 15 | + * are what they were. A public method keeps its docblock on the class, the |
| 16 | + * contract; a private method's docblock moved here with its body. The state |
| 17 | + * stays on `ScopedContext`. |
| 18 | + * |
| 19 | + * The bodies reach the instance through {@link ScopedContextHost}: exactly the |
| 20 | + * `ScopedContext` members they read, private ones included. Every stub hands |
| 21 | + * its body, by a `.call`, the same host: `scopedContextHost` in |
| 22 | + * `engine-scoped-hosts.ts`, built once per instance by the class's static |
| 23 | + * block. Methods go as closures that call them on the instance at call time; |
| 24 | + * fields and accessors go as GETTERS, read when the body reads them, and a |
| 25 | + * field a body assigns goes with a SETTER that assigns it on the instance. |
| 26 | + * |
| 27 | + * Values declared in `engine.ts` are imported from it (lever (a) of |
| 28 | + * ruling 6591753301); every use is inside a function body, so the import |
| 29 | + * cycle is never read at evaluation time. `index.ts` re-exports nothing from |
| 30 | + * this module. |
| 31 | + */ |
| 32 | + |
| 33 | +import { driverSupportsTransactions } from '@objectstack/spec/data'; |
| 34 | +import type { IScopedContext } from '@objectstack/spec/contracts'; |
| 35 | +import { appAutomationElevationOf, markAppAutomationElevation } from '@objectstack/core'; |
| 36 | +import { UnscopedHookApi } from './hook-run-as.js'; |
| 37 | +import type { HookRunAs, HookRunAsRef } from './hook-run-as.js'; |
| 38 | +import { ObjectRepository } from './engine.js'; |
| 39 | +import type { ObjectQL, ScopedContext } from './engine.js'; |
| 40 | + |
| 41 | +/** |
| 42 | + * The `ScopedContext` members the bodies of {@link ScopedContextFacet} read — |
| 43 | + * every one of them, and no other. Each is typed AS the class member (an |
| 44 | + * indexed access on the class), so a change to a member's signature on the |
| 45 | + * class is a change here too. `readonly` members are the getters; the others |
| 46 | + * are fields a body assigns. |
| 47 | + */ |
| 48 | +export interface ScopedContextHost { |
| 49 | + readonly appAuthored: ScopedContext['appAuthored']; |
| 50 | + derive: ScopedContext['derive']; |
| 51 | + readonly engine: ScopedContext['engine']; |
| 52 | + readonly executionContext: ScopedContext['executionContext']; |
| 53 | + readonly joinedHandles: ScopedContext['joinedHandles']; |
| 54 | + releaseJoinedHandle: ScopedContext['releaseJoinedHandle']; |
| 55 | + txDriver: ScopedContext['txDriver']; |
| 56 | +} |
| 57 | + |
| 58 | +/** The bodies of `ScopedContext`'s methods: a stateless facet, called by the class's stubs. */ |
| 59 | +export class ScopedContextFacet { |
| 60 | + /** The body of `ScopedContext#object`; the method's docblock there is its contract. */ |
| 61 | + static object(this: Pick<ScopedContextHost, 'engine' | 'executionContext'>, name: string): ObjectRepository { |
| 62 | + return new ObjectRepository(name, this.executionContext, this.engine as any); |
| 63 | + } |
| 64 | + |
| 65 | + /** The body of `ScopedContext#sudo`; the method's docblock there is its contract. */ |
| 66 | + static sudo(this: Pick<ScopedContextHost, 'appAuthored' | 'derive' | 'executionContext'>): ScopedContext { |
| 67 | + const elevated = { ...this.executionContext, isSystem: true }; |
| 68 | + return this.derive( |
| 69 | + this.appAuthored && !appAutomationElevationOf(elevated) |
| 70 | + ? markAppAutomationElevation(elevated, 'hook') |
| 71 | + : elevated, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** The body of `ScopedContext#withRunAs`; the method's docblock there is its contract. */ |
| 76 | + static withRunAs(this: Pick<ScopedContextHost, 'derive' | 'executionContext'>, runAs: Exclude<HookRunAs, 'inherit'>, ref: HookRunAsRef): IScopedContext { |
| 77 | + if (runAs === 'system') { |
| 78 | + return this.derive( |
| 79 | + markAppAutomationElevation({ ...this.executionContext, isSystem: true }, 'hook'), |
| 80 | + ); |
| 81 | + } |
| 82 | + if (!this.executionContext.userId) return new UnscopedHookApi(ref); |
| 83 | + return this.derive({ ...this.executionContext, isSystem: false }); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Was `handle` JOINED by this context rather than opened by it? Consumes the |
| 88 | + * record, so the trio's terminal call is also what forgets the handle. |
| 89 | + * |
| 90 | + * Two independent signals, because the trio's callers are exactly the ones |
| 91 | + * that cannot keep a closure on the stack and may not close on the same |
| 92 | + * object they opened on: |
| 93 | + * |
| 94 | + * 1. this context's own {@link joinedHandles} record, and |
| 95 | + * 2. identity with the CURRENT ambient handle — a handle the engine is |
| 96 | + * holding open right now is, by construction, not one the trio opened |
| 97 | + * (the trio never publishes into `txStore`, so a trio-owned handle is |
| 98 | + * never the ambient one). |
| 99 | + * |
| 100 | + * Both point the same way and both fail SAFE: the ambiguous answer is |
| 101 | + * "abstain", never "commit a transaction we do not own". |
| 102 | + */ |
| 103 | + static releaseJoinedHandle(this: Pick<ScopedContextHost, 'engine' | 'joinedHandles'>, handle: unknown): boolean { |
| 104 | + if (this.joinedHandles.delete(handle)) return true; |
| 105 | + if (handle == null) return false; |
| 106 | + const ambient = (this.engine as any)?.txStore?.getStore?.() as |
| 107 | + | { transaction?: unknown } |
| 108 | + | undefined; |
| 109 | + return ambient?.transaction === handle; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Resolve the default driver, if it exposes transaction primitives. |
| 114 | + * Shared by {@link transaction} and the discrete begin/commit/rollback trio. |
| 115 | + */ |
| 116 | + static txDriver(this: Pick<ScopedContextHost, 'engine'>): any | undefined { |
| 117 | + const engine = this.engine as any; |
| 118 | + const driver = engine.defaultDriver |
| 119 | + ? engine.drivers?.get(engine.defaultDriver) |
| 120 | + : undefined; |
| 121 | + // [#18063] Declaration, not bare method presence — the trio's `begin` |
| 122 | + // returns `null` and `commit`/`rollback` abstain for a transport that |
| 123 | + // declared it cannot honour a handle, which is the same graceful degrade a |
| 124 | + // driver with no `beginTransaction` already gets. |
| 125 | + return driverSupportsTransactions(driver) ? driver : undefined; |
| 126 | + } |
| 127 | + |
| 128 | + /** The body of `ScopedContext#beginTransaction`; the method's docblock there is its contract. */ |
| 129 | + static async beginTransaction(this: Pick<ScopedContextHost, 'derive' | 'engine' | 'executionContext' | 'joinedHandles' | 'txDriver'>): Promise<{ ctx: ScopedContext; handle: unknown; owned: boolean } | null> { |
| 130 | + // ADR-0067 D2 — JOIN before the driver lookup, the same first move and the |
| 131 | + // same position as both callback faces (#6168 / #6406). An ambient |
| 132 | + // transaction IS a transaction, so there is nothing to look a driver up for. |
| 133 | + const ambient = (this.engine as any)?.txStore?.getStore?.() as |
| 134 | + | { transaction?: unknown } |
| 135 | + | undefined; |
| 136 | + if (ambient?.transaction) { |
| 137 | + // Threaded explicitly into the child context, identity-equal to the |
| 138 | + // store's handle — so `buildDriverOptions` binds every op on it to the |
| 139 | + // outer connection and `transactionCoversDriverFor` still attributes the |
| 140 | + // handle to the OUTER owner (#5351 unchanged). |
| 141 | + const ctx = this.derive({ ...this.executionContext, transaction: ambient.transaction }); |
| 142 | + this.joinedHandles.add(ambient.transaction); |
| 143 | + return { ctx, handle: ambient.transaction, owned: false }; |
| 144 | + } |
| 145 | + const driver = this.txDriver(); |
| 146 | + if (!driver) return null; |
| 147 | + const trx = await driver.beginTransaction(); |
| 148 | + const ctx = this.derive({ ...this.executionContext, transaction: trx }); |
| 149 | + return { ctx, handle: trx, owned: true }; |
| 150 | + } |
| 151 | + |
| 152 | + /** The body of `ScopedContext#commitTransaction`; the method's docblock there is its contract. */ |
| 153 | + static async commitTransaction(this: Pick<ScopedContextHost, 'releaseJoinedHandle' | 'txDriver'>, handle: unknown): Promise<void> { |
| 154 | + if (this.releaseJoinedHandle(handle)) return; |
| 155 | + const driver = this.txDriver(); |
| 156 | + if (!driver) return; |
| 157 | + if (driver.commit) await driver.commit(handle); |
| 158 | + else if (driver.commitTransaction) await driver.commitTransaction(handle); |
| 159 | + } |
| 160 | + |
| 161 | + /** The body of `ScopedContext#rollbackTransaction`; the method's docblock there is its contract. */ |
| 162 | + static async rollbackTransaction(this: Pick<ScopedContextHost, 'releaseJoinedHandle' | 'txDriver'>, handle: unknown): Promise<void> { |
| 163 | + if (this.releaseJoinedHandle(handle)) return; |
| 164 | + const driver = this.txDriver(); |
| 165 | + if (!driver) return; |
| 166 | + if (driver.rollback) await driver.rollback(handle); |
| 167 | + else if (driver.rollbackTransaction) await driver.rollbackTransaction(handle); |
| 168 | + } |
| 169 | + |
| 170 | + /** The body of `ScopedContext#serveFormulaField`; the method's docblock there is its contract. */ |
| 171 | + static async serveFormulaField(this: Pick<ScopedContextHost, 'engine' | 'executionContext'>, objectName: string, record: Record<string, unknown>, field: string): Promise<unknown> { |
| 172 | + return (this.engine as unknown as ObjectQL).serveFormulaField(objectName, record, field, this.executionContext); |
| 173 | + } |
| 174 | +} |
0 commit comments