@prisma-next/framework-components 0.5.0-dev.4 → 0.5.0-dev.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/dist/authoring.d.mts +1 -1
- package/dist/authoring.mjs +1 -1
- package/dist/authoring.mjs.map +1 -1
- package/dist/codec-types-CB0jWeHU.d.mts +207 -0
- package/dist/codec-types-CB0jWeHU.d.mts.map +1 -0
- package/dist/codec.d.mts +2 -2
- package/dist/codec.mjs +38 -2
- package/dist/codec.mjs.map +1 -1
- package/dist/components.d.mts +1 -1
- package/dist/components.mjs +1 -1
- package/dist/control.d.mts +101 -35
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +11 -5
- package/dist/control.mjs.map +1 -1
- package/dist/emission-types-D6t3_a0x.d.mts +39 -0
- package/dist/emission-types-D6t3_a0x.d.mts.map +1 -0
- package/dist/emission.d.mts +2 -2
- package/dist/execution.d.mts +5 -5
- package/dist/execution.d.mts.map +1 -1
- package/dist/execution.mjs +3 -3
- package/dist/execution.mjs.map +1 -1
- package/dist/{framework-authoring-D1-JZ37B.d.mts → framework-authoring-BdrFDx4x.d.mts} +2 -2
- package/dist/framework-authoring-BdrFDx4x.d.mts.map +1 -0
- package/dist/{framework-components-EJXe-pum.d.mts → framework-components-AHI6V96G.d.mts} +6 -6
- package/dist/framework-components-AHI6V96G.d.mts.map +1 -0
- package/dist/{framework-components-C8ZhSwXe.mjs → framework-components-BsWST1Rn.mjs} +2 -2
- package/dist/framework-components-BsWST1Rn.mjs.map +1 -0
- package/dist/psl-ast-9X5rwo98.d.mts +159 -0
- package/dist/psl-ast-9X5rwo98.d.mts.map +1 -0
- package/dist/psl-ast.d.mts +2 -0
- package/dist/psl-ast.mjs +1 -0
- package/dist/runtime.d.mts +251 -19
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +215 -4
- package/dist/runtime.mjs.map +1 -1
- package/dist/{types-import-spec-C4sc7wbb.d.mts → types-import-spec-D-O6GotH.d.mts} +2 -2
- package/dist/types-import-spec-D-O6GotH.d.mts.map +1 -0
- package/package.json +8 -6
- package/src/control/control-capabilities.ts +71 -0
- package/src/{control-descriptors.ts → control/control-descriptors.ts} +7 -7
- package/src/{control-instances.ts → control/control-instances.ts} +6 -6
- package/src/{control-migration-types.ts → control/control-migration-types.ts} +48 -23
- package/src/control/control-operation-preview.ts +23 -0
- package/src/{control-stack.ts → control/control-stack.ts} +13 -13
- package/src/control/emission-types.ts +49 -0
- package/src/control/psl-ast.ts +193 -0
- package/src/{execution-descriptors.ts → execution/execution-descriptors.ts} +7 -7
- package/src/{execution-instances.ts → execution/execution-instances.ts} +1 -1
- package/src/{execution-requirements.ts → execution/execution-requirements.ts} +1 -1
- package/src/execution/query-plan.ts +53 -0
- package/src/execution/race-against-abort.ts +85 -0
- package/src/execution/run-with-middleware.ts +77 -0
- package/src/execution/runtime-core.ts +133 -0
- package/src/execution/runtime-error.ts +83 -0
- package/src/{runtime-middleware.ts → execution/runtime-middleware.ts} +32 -12
- package/src/exports/authoring.ts +2 -2
- package/src/exports/codec.ts +14 -2
- package/src/exports/components.ts +2 -2
- package/src/exports/control.ts +26 -12
- package/src/exports/emission.ts +2 -2
- package/src/exports/execution.ts +5 -5
- package/src/exports/psl-ast.ts +1 -0
- package/src/exports/runtime.ts +16 -5
- package/src/shared/codec-types.ts +261 -0
- package/dist/codec-types-B58nCJiu.d.mts +0 -40
- package/dist/codec-types-B58nCJiu.d.mts.map +0 -1
- package/dist/emission-types-BPAALJbF.d.mts +0 -24
- package/dist/emission-types-BPAALJbF.d.mts.map +0 -1
- package/dist/framework-authoring-D1-JZ37B.d.mts.map +0 -1
- package/dist/framework-components-C8ZhSwXe.mjs.map +0 -1
- package/dist/framework-components-EJXe-pum.d.mts.map +0 -1
- package/dist/types-import-spec-C4sc7wbb.d.mts.map +0 -1
- package/src/codec-types.ts +0 -46
- package/src/control-capabilities.ts +0 -34
- package/src/emission-types.ts +0 -28
- package/src/runtime-error.ts +0 -39
- /package/src/{control-result-types.ts → control/control-result-types.ts} +0 -0
- /package/src/{control-schema-view.ts → control/control-schema-view.ts} +0 -0
- /package/src/{async-iterable-result.ts → execution/async-iterable-result.ts} +0 -0
- /package/src/{execution-stack.ts → execution/execution-stack.ts} +0 -0
- /package/src/{framework-authoring.ts → shared/framework-authoring.ts} +0 -0
- /package/src/{framework-components.ts → shared/framework-components.ts} +0 -0
- /package/src/{mutation-default-types.ts → shared/mutation-default-types.ts} +0 -0
- /package/src/{types-import-spec.ts → shared/types-import-spec.ts} +0 -0
package/dist/runtime.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { n as CodecCallContext } from "./codec-types-CB0jWeHU.mjs";
|
|
1
2
|
import { PlanMeta } from "@prisma-next/contract/types";
|
|
2
3
|
|
|
3
|
-
//#region src/async-iterable-result.d.ts
|
|
4
|
+
//#region src/execution/async-iterable-result.d.ts
|
|
4
5
|
declare class AsyncIterableResult<Row> implements AsyncIterable<Row>, PromiseLike<Row[]> {
|
|
5
6
|
private readonly generator;
|
|
6
7
|
private consumed;
|
|
@@ -14,16 +15,136 @@ declare class AsyncIterableResult<Row> implements AsyncIterable<Row>, PromiseLik
|
|
|
14
15
|
then<TResult1 = Row[], TResult2 = never>(onfulfilled?: ((value: Row[]) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
|
|
15
16
|
}
|
|
16
17
|
//#endregion
|
|
17
|
-
//#region src/
|
|
18
|
+
//#region src/execution/query-plan.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* Family-agnostic plan marker.
|
|
21
|
+
*
|
|
22
|
+
* Carries only `meta` (the family-agnostic plan metadata) and the optional
|
|
23
|
+
* phantom `_row` parameter that lets type-level utilities recover the row
|
|
24
|
+
* type from a plan value. SQL and Mongo extend this marker with their own
|
|
25
|
+
* concrete shapes (`SqlQueryPlan`, `MongoQueryPlan`).
|
|
26
|
+
*
|
|
27
|
+
* `QueryPlan` is the *pre-lowering* marker — i.e. the surface a builder
|
|
28
|
+
* produces before family-specific lowering turns it into an executable
|
|
29
|
+
* plan (`ExecutionPlan`).
|
|
30
|
+
*/
|
|
31
|
+
interface QueryPlan<Row = unknown> {
|
|
32
|
+
readonly meta: PlanMeta;
|
|
33
|
+
/**
|
|
34
|
+
* Phantom property to carry the Row generic for type-level utilities.
|
|
35
|
+
* Not set at runtime; used only for `ResultType` extraction.
|
|
36
|
+
*/
|
|
37
|
+
readonly _row?: Row;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Family-agnostic execution-plan marker.
|
|
41
|
+
*
|
|
42
|
+
* Extends `QueryPlan` with no additional structural fields — the marker
|
|
43
|
+
* exists to nominally distinguish executable plans from pre-lowering plans
|
|
44
|
+
* in the type system. Family-specific execution plans (`SqlExecutionPlan`,
|
|
45
|
+
* `MongoExecutionPlan`) extend this marker with their concrete shapes
|
|
46
|
+
* (e.g. `sql + params` for SQL, `wireCommand` for Mongo).
|
|
47
|
+
*/
|
|
48
|
+
interface ExecutionPlan<Row = unknown> extends QueryPlan<Row> {}
|
|
49
|
+
/**
|
|
50
|
+
* Extracts the `Row` type from a plan via the phantom `_row` property.
|
|
51
|
+
*
|
|
52
|
+
* Works with any plan that extends `QueryPlan<Row>` — including
|
|
53
|
+
* `ExecutionPlan<Row>`, `SqlQueryPlan<Row>`, `SqlExecutionPlan<Row>`,
|
|
54
|
+
* `MongoQueryPlan<Row>`, and `MongoExecutionPlan<Row>`.
|
|
55
|
+
*
|
|
56
|
+
* The `_row` property must be present in the plan's static type for the
|
|
57
|
+
* conditional to bind `R`; objects whose type lacks `_row` resolve to
|
|
58
|
+
* `never`. Without the `keyof` guard, `extends { _row?: infer R }` would
|
|
59
|
+
* silently match any object and infer `unknown`.
|
|
60
|
+
*
|
|
61
|
+
* Example: `type Row = ResultType<typeof plan>`.
|
|
62
|
+
*/
|
|
63
|
+
type ResultType<P> = '_row' extends keyof P ? P extends {
|
|
64
|
+
readonly _row?: infer R;
|
|
65
|
+
} ? R : never : never;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/execution/runtime-error.d.ts
|
|
18
68
|
interface RuntimeErrorEnvelope extends Error {
|
|
19
69
|
readonly code: string;
|
|
20
70
|
readonly category: 'PLAN' | 'CONTRACT' | 'LINT' | 'BUDGET' | 'RUNTIME';
|
|
21
71
|
readonly severity: 'error';
|
|
22
72
|
readonly details?: Record<string, unknown>;
|
|
23
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Stable code emitted by the runtime when an in-flight `execute()`
|
|
76
|
+
* is cancelled via the per-query `AbortSignal`. The envelope's
|
|
77
|
+
* `details.phase` distinguishes where the abort was observed:
|
|
78
|
+
*
|
|
79
|
+
* - `'encode'` — abort fired during `encodeParams` (SQL) or
|
|
80
|
+
* `resolveValue` (Mongo).
|
|
81
|
+
* - `'decode'` — abort fired during `decodeRow` / `decodeField`.
|
|
82
|
+
* - `'stream'` — abort fired between rows or before any codec call
|
|
83
|
+
* (already-aborted at entry).
|
|
84
|
+
*/
|
|
85
|
+
declare const RUNTIME_ABORTED: "RUNTIME.ABORTED";
|
|
86
|
+
/** Discriminator placed in `details.phase` of a `RUNTIME.ABORTED` envelope. */
|
|
87
|
+
type RuntimeAbortedPhase = 'encode' | 'decode' | 'stream';
|
|
88
|
+
/**
|
|
89
|
+
* Type guard for the runtime-error envelope produced by `runtimeError`.
|
|
90
|
+
*
|
|
91
|
+
* Prefer this over duck-typing on `error.code` directly so consumers stay
|
|
92
|
+
* insulated from the envelope's internal shape.
|
|
93
|
+
*/
|
|
94
|
+
declare function isRuntimeError(error: unknown): error is RuntimeErrorEnvelope;
|
|
24
95
|
declare function runtimeError(code: string, message: string, details?: Record<string, unknown>): RuntimeErrorEnvelope;
|
|
96
|
+
/**
|
|
97
|
+
* Construct a `RUNTIME.ABORTED` envelope. Phase distinguishes where the
|
|
98
|
+
* abort was observed (encode / decode / stream); cause carries `signal.reason`
|
|
99
|
+
* verbatim from the platform — native abort produces a `DOMException`,
|
|
100
|
+
* explicit `controller.abort(reason)` produces whatever the caller passed.
|
|
101
|
+
* No synthesis happens here.
|
|
102
|
+
*/
|
|
103
|
+
declare function runtimeAborted(phase: RuntimeAbortedPhase, cause?: unknown): RuntimeErrorEnvelope;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/execution/race-against-abort.d.ts
|
|
106
|
+
/**
|
|
107
|
+
* Throw a phase-tagged `RUNTIME.ABORTED` envelope if the supplied
|
|
108
|
+
* codec-call context is already aborted at the precheck site. Centralises
|
|
109
|
+
* the `if (ctx.signal?.aborted) throw runtimeAborted(...)` pattern that
|
|
110
|
+
* every codec dispatch site repeats.
|
|
111
|
+
*/
|
|
112
|
+
declare function checkAborted(ctx: CodecCallContext, phase: RuntimeAbortedPhase): void;
|
|
113
|
+
/**
|
|
114
|
+
* Race a per-cell `Promise.all` (or any other in-flight work promise) against
|
|
115
|
+
* the supplied abort signal so the runtime returns `RUNTIME.ABORTED` promptly
|
|
116
|
+
* even when codec bodies ignore the signal. In-flight bodies that ignore the
|
|
117
|
+
* signal are abandoned and run to completion in the background — the
|
|
118
|
+
* cooperative-cancellation contract documented in ADR 204.
|
|
119
|
+
*
|
|
120
|
+
* Call sites still SHOULD pre-check `signal.aborted` and short-circuit with
|
|
121
|
+
* a phase-tagged `RUNTIME.ABORTED` envelope before invoking this helper —
|
|
122
|
+
* that path is the canonical "aborted at entry" surface and avoids
|
|
123
|
+
* scheduling the work promise. As a defensive belt-and-braces, this helper
|
|
124
|
+
* also handles the already-aborted case internally: `AbortSignal` does not
|
|
125
|
+
* replay past abort events to listeners registered after the abort, so we
|
|
126
|
+
* inspect `signal.aborted` synchronously and reject with the sentinel
|
|
127
|
+
* before installing the listener. The rejection is still attributed to the
|
|
128
|
+
* abort path via the sentinel-identity check.
|
|
129
|
+
*
|
|
130
|
+
* Distinguishing the rejection source is load-bearing for AC-ERR4
|
|
131
|
+
* (`RUNTIME.ENCODE_FAILED` / `RUNTIME.DECODE_FAILED` pass through unchanged).
|
|
132
|
+
* The semantically equivalent `abortable(signal)` helper in
|
|
133
|
+
* `@prisma-next/utils` rejects with `signal.reason ?? new DOMException(...)`,
|
|
134
|
+
* which is not stably distinguishable from a codec-thrown error by identity
|
|
135
|
+
* alone (a fresh fallback DOMException is allocated per call). We instead
|
|
136
|
+
* track abort attribution with a unique sentinel: only the `onAbort` listener
|
|
137
|
+
* installed here ever rejects with the sentinel, so an `error === sentinel`
|
|
138
|
+
* identity check after the race is unambiguous.
|
|
139
|
+
*
|
|
140
|
+
* Lives in `framework-components` (rather than the SQL family, where it
|
|
141
|
+
* originated in m2) so every family runtime that needs cooperative
|
|
142
|
+
* cancellation around a codec-dispatch `Promise.all` (SQL encode + decode
|
|
143
|
+
* today, Mongo encode in m3) shares the same attribution logic.
|
|
144
|
+
*/
|
|
145
|
+
declare function raceAgainstAbort<T>(work: Promise<T>, signal: AbortSignal | undefined, phase: RuntimeAbortedPhase): Promise<T>;
|
|
25
146
|
//#endregion
|
|
26
|
-
//#region src/runtime-middleware.d.ts
|
|
147
|
+
//#region src/execution/runtime-middleware.d.ts
|
|
27
148
|
interface RuntimeLog {
|
|
28
149
|
info(event: unknown): void;
|
|
29
150
|
warn(event: unknown): void;
|
|
@@ -41,19 +162,33 @@ interface AfterExecuteResult {
|
|
|
41
162
|
readonly latencyMs: number;
|
|
42
163
|
readonly completed: boolean;
|
|
43
164
|
}
|
|
44
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Family-agnostic middleware SPI parameterized over the plan marker.
|
|
167
|
+
*
|
|
168
|
+
* `TPlan` defaults to the framework `QueryPlan` marker so a generic
|
|
169
|
+
* middleware (e.g. cross-family telemetry) can be authored without
|
|
170
|
+
* naming a family. Family-specific middleware (`SqlMiddleware`,
|
|
171
|
+
* `MongoMiddleware`) narrow `TPlan` to their concrete plan type.
|
|
172
|
+
*/
|
|
173
|
+
interface RuntimeMiddleware<TPlan extends QueryPlan = QueryPlan> {
|
|
45
174
|
readonly name: string;
|
|
46
175
|
readonly familyId?: string;
|
|
47
176
|
readonly targetId?: string;
|
|
48
|
-
beforeExecute?(plan:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
177
|
+
beforeExecute?(plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
178
|
+
onRow?(row: Record<string, unknown>, plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
179
|
+
afterExecute?(plan: TPlan, result: AfterExecuteResult, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Optional per-`execute` options accepted by every family runtime.
|
|
183
|
+
*
|
|
184
|
+
* `signal` is the per-query cancellation signal. The runtime threads the
|
|
185
|
+
* signal through to every codec call for the query and uses it to short-
|
|
186
|
+
* circuit the row stream with `RUNTIME.ABORTED` when the caller aborts.
|
|
187
|
+
* Omitting the option (or passing `undefined`) preserves today's behavior
|
|
188
|
+
* bit-for-bit.
|
|
189
|
+
*/
|
|
190
|
+
interface RuntimeExecuteOptions {
|
|
191
|
+
readonly signal?: AbortSignal;
|
|
57
192
|
}
|
|
58
193
|
/**
|
|
59
194
|
* Cross-family SPI for any runtime that can execute plans and be shut down.
|
|
@@ -61,17 +196,114 @@ interface RuntimeMiddleware {
|
|
|
61
196
|
* Mongo structurally (due to its phantom Row parameter using a unique symbol).
|
|
62
197
|
*
|
|
63
198
|
* The `_row` intersection on `execute` connects the `Row` type parameter to the
|
|
64
|
-
* plan, mirroring how `
|
|
199
|
+
* plan, mirroring how `QueryPlan<Row>` carries a phantom `_row?: Row`.
|
|
65
200
|
*/
|
|
66
|
-
interface RuntimeExecutor<TPlan extends {
|
|
67
|
-
readonly meta: PlanMeta;
|
|
68
|
-
}> {
|
|
201
|
+
interface RuntimeExecutor<TPlan extends QueryPlan> {
|
|
69
202
|
execute<Row>(plan: TPlan & {
|
|
70
203
|
readonly _row?: Row;
|
|
71
|
-
}): AsyncIterableResult<Row>;
|
|
204
|
+
}, options?: RuntimeExecuteOptions): AsyncIterableResult<Row>;
|
|
72
205
|
close(): Promise<void>;
|
|
73
206
|
}
|
|
74
207
|
declare function checkMiddlewareCompatibility(middleware: RuntimeMiddleware, runtimeFamilyId: string, runtimeTargetId: string): void;
|
|
75
208
|
//#endregion
|
|
76
|
-
|
|
209
|
+
//#region src/execution/run-with-middleware.d.ts
|
|
210
|
+
/**
|
|
211
|
+
* Drives a single execution of `runDriver()` through the middleware lifecycle.
|
|
212
|
+
*
|
|
213
|
+
* Lifecycle, in order:
|
|
214
|
+
* 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.
|
|
215
|
+
* 2. For each row yielded by `runDriver()`: for each middleware in registration
|
|
216
|
+
* order: `onRow(row, exec, ctx)`; then yield the row to the consumer.
|
|
217
|
+
* 3. On successful completion: for each middleware in registration order:
|
|
218
|
+
* `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.
|
|
219
|
+
* 4. On any error thrown by the driver loop: for each middleware in
|
|
220
|
+
* registration order: `afterExecute(exec, { rowCount, latencyMs,
|
|
221
|
+
* completed: false }, ctx)`. Errors thrown by `afterExecute` during the
|
|
222
|
+
* error path are swallowed so they do not mask the original driver error.
|
|
223
|
+
* The original error is then rethrown.
|
|
224
|
+
*
|
|
225
|
+
* This helper is the single canonical implementation of the middleware
|
|
226
|
+
* orchestration loop; family runtimes should not reimplement it.
|
|
227
|
+
*/
|
|
228
|
+
declare function runWithMiddleware<TExec extends ExecutionPlan, Row>(exec: TExec, middleware: ReadonlyArray<RuntimeMiddleware<TExec>>, ctx: RuntimeMiddlewareContext, runDriver: () => AsyncIterable<Row>): AsyncIterableResult<Row>;
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/execution/runtime-core.d.ts
|
|
231
|
+
/**
|
|
232
|
+
* Constructor options shared by every concrete `RuntimeCore` subclass.
|
|
233
|
+
*
|
|
234
|
+
* Family runtimes typically build the middleware list and the
|
|
235
|
+
* `RuntimeMiddlewareContext` themselves (running compatibility checks,
|
|
236
|
+
* narrowing the context's `contract` field, etc.) before calling `super`.
|
|
237
|
+
*/
|
|
238
|
+
interface RuntimeCoreOptions<TMiddleware extends RuntimeMiddleware<ExecutionPlan>> {
|
|
239
|
+
readonly middleware: ReadonlyArray<TMiddleware>;
|
|
240
|
+
readonly ctx: RuntimeMiddlewareContext;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Family-agnostic abstract runtime base.
|
|
244
|
+
*
|
|
245
|
+
* Defines the entire `execute(plan)` template in one place:
|
|
246
|
+
*
|
|
247
|
+
* 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides
|
|
248
|
+
* this to run its `beforeCompile` middleware-hook chain.
|
|
249
|
+
* 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`
|
|
250
|
+
* (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).
|
|
251
|
+
* 3. `runWithMiddleware(exec, this.middleware, this.ctx,
|
|
252
|
+
* () => runDriver(exec))` — concrete; lifts the middleware lifecycle
|
|
253
|
+
* out of the family runtimes into the canonical helper.
|
|
254
|
+
*
|
|
255
|
+
* Concrete subclasses must implement `lower`, `runDriver`, and `close`.
|
|
256
|
+
*
|
|
257
|
+
* The class is generic over:
|
|
258
|
+
* - `TPlan` — the family's pre-lowering plan type.
|
|
259
|
+
* - `TExec` — the family's post-lowering (executable) plan type.
|
|
260
|
+
* - `TMiddleware` — the family's middleware type. Constrained to
|
|
261
|
+
* `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the
|
|
262
|
+
* `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered
|
|
263
|
+
* `TExec`. (The spec/plan wording "RuntimeMiddleware<TPlan>" is
|
|
264
|
+
* tightened to `<TExec>` here so the helper call typechecks; the
|
|
265
|
+
* intent is unchanged — middleware sees the post-lowering plan.)
|
|
266
|
+
*/
|
|
267
|
+
declare abstract class RuntimeCore<TPlan extends QueryPlan, TExec extends ExecutionPlan, TMiddleware extends RuntimeMiddleware<TExec>> implements RuntimeExecutor<TPlan> {
|
|
268
|
+
protected readonly middleware: ReadonlyArray<TMiddleware>;
|
|
269
|
+
protected readonly ctx: RuntimeMiddlewareContext;
|
|
270
|
+
constructor(options: RuntimeCoreOptions<TMiddleware>);
|
|
271
|
+
/**
|
|
272
|
+
* Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses
|
|
273
|
+
* may override to run a `beforeCompile` middleware chain (SQL does this
|
|
274
|
+
* to support typed AST rewrites — see `before-compile-chain.ts`).
|
|
275
|
+
*/
|
|
276
|
+
protected runBeforeCompile(plan: TPlan): TPlan | Promise<TPlan>;
|
|
277
|
+
/**
|
|
278
|
+
* Lower a pre-lowering `TPlan` into the family's executable `TExec`.
|
|
279
|
+
* Family-specific: SQL produces `{ sql, params, ast?, ... }`; Mongo
|
|
280
|
+
* produces `{ command, ... }`.
|
|
281
|
+
*
|
|
282
|
+
* `ctx` carries per-query cancellation (and any future fields on
|
|
283
|
+
* `CodecCallContext`); concrete subclasses forward it to the
|
|
284
|
+
* encode-side codec dispatch site (e.g. SQL's `encodeParams` in m2,
|
|
285
|
+
* Mongo's `resolveValue` in m3). The runtime allocates one ctx per
|
|
286
|
+
* `execute()` call and threads the same reference everywhere; the
|
|
287
|
+
* `signal` field inside may be `undefined`, but the ctx object itself
|
|
288
|
+
* is always present.
|
|
289
|
+
*/
|
|
290
|
+
protected abstract lower(plan: TPlan, ctx: CodecCallContext): TExec | Promise<TExec>;
|
|
291
|
+
/**
|
|
292
|
+
* Drive the underlying transport for a lowered `TExec`. Yields raw rows
|
|
293
|
+
* directly from the driver as `Record<string, unknown>`; codec decoding
|
|
294
|
+
* (if any) is the subclass's responsibility, applied by wrapping
|
|
295
|
+
* `execute()` rather than living inside this hook.
|
|
296
|
+
*
|
|
297
|
+
* The `Row` type parameter on `execute()` is satisfied by the caller via
|
|
298
|
+
* the plan's phantom `_row`; the runtime treats rows as opaque records
|
|
299
|
+
* here and trusts the caller's row typing.
|
|
300
|
+
*/
|
|
301
|
+
protected abstract runDriver(exec: TExec): AsyncIterable<Record<string, unknown>>;
|
|
302
|
+
abstract close(): Promise<void>;
|
|
303
|
+
execute<Row>(plan: TPlan & {
|
|
304
|
+
readonly _row?: Row;
|
|
305
|
+
}, options?: RuntimeExecuteOptions): AsyncIterableResult<Row>;
|
|
306
|
+
}
|
|
307
|
+
//#endregion
|
|
308
|
+
export { type AfterExecuteResult, AsyncIterableResult, type ExecutionPlan, type QueryPlan, RUNTIME_ABORTED, type ResultType, type RuntimeAbortedPhase, RuntimeCore, type RuntimeCoreOptions, type RuntimeErrorEnvelope, type RuntimeExecuteOptions, type RuntimeExecutor, type RuntimeLog, type RuntimeMiddleware, type RuntimeMiddlewareContext, checkAborted, checkMiddlewareCompatibility, isRuntimeError, raceAgainstAbort, runWithMiddleware, runtimeAborted, runtimeError };
|
|
77
309
|
//# sourceMappingURL=runtime.d.mts.map
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/async-iterable-result.ts","../src/runtime-error.ts","../src/runtime-middleware.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/execution/async-iterable-result.ts","../src/execution/query-plan.ts","../src/execution/runtime-error.ts","../src/execution/race-against-abort.ts","../src/execution/runtime-middleware.ts","../src/execution/run-with-middleware.ts","../src/execution/runtime-core.ts"],"sourcesContent":[],"mappings":";;;;cAEa,oCAAoC,cAAc,MAAM,YAAY;;;;EAApE,QAAA,oBAAmB;EAA+B,WAAA,CAAA,SAAA,EAMtC,cANsC,CAMvB,GANuB,EAAA,IAAA,EAAA,OAAA,CAAA;EAAkB,CAU9E,MAAA,CAAO,aAAA,GAV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,CAAA;EAmBW,SAAA,UAAA,EMXE,aNWF,CMXgB,WNWhB,CAAA;EAAR,SAAA,GAAA,EMVG,wBNUH;;;;;;;;;;;;;;;;;;;;;;ACjBb;AAkBA;AAgBA;;;uBKCsB,0BACN,yBACA,mCACM,kBAAkB,mBAC3B,gBAAgB;EJrDZ,mBAAA,UAAqB,EIuDL,aJvDa,CIuDC,WJvDI,CAAA;EAkBtC,mBAA4C,GAAA,EIsC/B,wBJtC+B;EAG7C,WAAA,CAAA,OAAA,EIqCW,kBJrCQ,CIqCW,WJrCX,CAAA;EAQf;AAUhB;AAwCA;;;mCIXmC,QAAQ,QAAQ,QAAQ;EH1D3C;AAsChB;;;;;;;;;;;AC5CA;EAOiB,mBAAA,KAAA,CAAA,IAAwB,EE0ER,KF1EQ,EAAA,GAIzB,EEsE6B,gBFtEnB,CAAA,EEsEsC,KFtEtC,GEsE8C,OFtE9C,CEsEsD,KFtEtD,CAAA;EAGT;AAcjB;;;;;;;;;EAKoF,mBAAA,SAAA,CAAA,IAAA,EE4D/C,KF5D+C,CAAA,EE4DvC,aF5DuC,CE4DzB,MF5DyB,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAE1E,SAAA,KAAA,CAAA,CAAA,EE4DU,OF5DV,CAAA,IAAA,CAAA;EACE,OAAA,CAAA,GAAA,CAAA,CAAA,IAAA,EE8DF,KF9DE,GAAA;IACH,SAAA,IAAA,CAAA,EE6D2B,GF7D3B;EACJ,CAAA,EAAA,OAAA,CAAA,EE6DS,qBF7DT,CAAA,EE8DA,mBF9DA,CE8DoB,GF9DpB,CAAA"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
//#region src/runtime-error.ts
|
|
1
|
+
//#region src/execution/runtime-error.ts
|
|
2
|
+
/**
|
|
3
|
+
* Stable code emitted by the runtime when an in-flight `execute()`
|
|
4
|
+
* is cancelled via the per-query `AbortSignal`. The envelope's
|
|
5
|
+
* `details.phase` distinguishes where the abort was observed:
|
|
6
|
+
*
|
|
7
|
+
* - `'encode'` — abort fired during `encodeParams` (SQL) or
|
|
8
|
+
* `resolveValue` (Mongo).
|
|
9
|
+
* - `'decode'` — abort fired during `decodeRow` / `decodeField`.
|
|
10
|
+
* - `'stream'` — abort fired between rows or before any codec call
|
|
11
|
+
* (already-aborted at entry).
|
|
12
|
+
*/
|
|
13
|
+
const RUNTIME_ABORTED = "RUNTIME.ABORTED";
|
|
14
|
+
/**
|
|
15
|
+
* Type guard for the runtime-error envelope produced by `runtimeError`.
|
|
16
|
+
*
|
|
17
|
+
* Prefer this over duck-typing on `error.code` directly so consumers stay
|
|
18
|
+
* insulated from the envelope's internal shape.
|
|
19
|
+
*/
|
|
20
|
+
function isRuntimeError(error) {
|
|
21
|
+
return error instanceof Error && "code" in error && typeof error.code === "string" && "category" in error && "severity" in error;
|
|
22
|
+
}
|
|
2
23
|
function runtimeError(code, message, details) {
|
|
3
24
|
const error = new Error(message);
|
|
4
25
|
Object.defineProperty(error, "name", {
|
|
@@ -23,9 +44,20 @@ function resolveCategory(code) {
|
|
|
23
44
|
default: return "RUNTIME";
|
|
24
45
|
}
|
|
25
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Construct a `RUNTIME.ABORTED` envelope. Phase distinguishes where the
|
|
49
|
+
* abort was observed (encode / decode / stream); cause carries `signal.reason`
|
|
50
|
+
* verbatim from the platform — native abort produces a `DOMException`,
|
|
51
|
+
* explicit `controller.abort(reason)` produces whatever the caller passed.
|
|
52
|
+
* No synthesis happens here.
|
|
53
|
+
*/
|
|
54
|
+
function runtimeAborted(phase, cause) {
|
|
55
|
+
const envelope = runtimeError(RUNTIME_ABORTED, `Operation aborted during ${phase}`, { phase });
|
|
56
|
+
return Object.assign(envelope, { cause });
|
|
57
|
+
}
|
|
26
58
|
|
|
27
59
|
//#endregion
|
|
28
|
-
//#region src/async-iterable-result.ts
|
|
60
|
+
//#region src/execution/async-iterable-result.ts
|
|
29
61
|
var AsyncIterableResult = class {
|
|
30
62
|
generator;
|
|
31
63
|
consumed = false;
|
|
@@ -72,7 +104,186 @@ var AsyncIterableResult = class {
|
|
|
72
104
|
};
|
|
73
105
|
|
|
74
106
|
//#endregion
|
|
75
|
-
//#region src/
|
|
107
|
+
//#region src/execution/race-against-abort.ts
|
|
108
|
+
/**
|
|
109
|
+
* Throw a phase-tagged `RUNTIME.ABORTED` envelope if the supplied
|
|
110
|
+
* codec-call context is already aborted at the precheck site. Centralises
|
|
111
|
+
* the `if (ctx.signal?.aborted) throw runtimeAborted(...)` pattern that
|
|
112
|
+
* every codec dispatch site repeats.
|
|
113
|
+
*/
|
|
114
|
+
function checkAborted(ctx, phase) {
|
|
115
|
+
if (ctx.signal?.aborted) throw runtimeAborted(phase, ctx.signal.reason);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Race a per-cell `Promise.all` (or any other in-flight work promise) against
|
|
119
|
+
* the supplied abort signal so the runtime returns `RUNTIME.ABORTED` promptly
|
|
120
|
+
* even when codec bodies ignore the signal. In-flight bodies that ignore the
|
|
121
|
+
* signal are abandoned and run to completion in the background — the
|
|
122
|
+
* cooperative-cancellation contract documented in ADR 204.
|
|
123
|
+
*
|
|
124
|
+
* Call sites still SHOULD pre-check `signal.aborted` and short-circuit with
|
|
125
|
+
* a phase-tagged `RUNTIME.ABORTED` envelope before invoking this helper —
|
|
126
|
+
* that path is the canonical "aborted at entry" surface and avoids
|
|
127
|
+
* scheduling the work promise. As a defensive belt-and-braces, this helper
|
|
128
|
+
* also handles the already-aborted case internally: `AbortSignal` does not
|
|
129
|
+
* replay past abort events to listeners registered after the abort, so we
|
|
130
|
+
* inspect `signal.aborted` synchronously and reject with the sentinel
|
|
131
|
+
* before installing the listener. The rejection is still attributed to the
|
|
132
|
+
* abort path via the sentinel-identity check.
|
|
133
|
+
*
|
|
134
|
+
* Distinguishing the rejection source is load-bearing for AC-ERR4
|
|
135
|
+
* (`RUNTIME.ENCODE_FAILED` / `RUNTIME.DECODE_FAILED` pass through unchanged).
|
|
136
|
+
* The semantically equivalent `abortable(signal)` helper in
|
|
137
|
+
* `@prisma-next/utils` rejects with `signal.reason ?? new DOMException(...)`,
|
|
138
|
+
* which is not stably distinguishable from a codec-thrown error by identity
|
|
139
|
+
* alone (a fresh fallback DOMException is allocated per call). We instead
|
|
140
|
+
* track abort attribution with a unique sentinel: only the `onAbort` listener
|
|
141
|
+
* installed here ever rejects with the sentinel, so an `error === sentinel`
|
|
142
|
+
* identity check after the race is unambiguous.
|
|
143
|
+
*
|
|
144
|
+
* Lives in `framework-components` (rather than the SQL family, where it
|
|
145
|
+
* originated in m2) so every family runtime that needs cooperative
|
|
146
|
+
* cancellation around a codec-dispatch `Promise.all` (SQL encode + decode
|
|
147
|
+
* today, Mongo encode in m3) shares the same attribution logic.
|
|
148
|
+
*/
|
|
149
|
+
async function raceAgainstAbort(work, signal, phase) {
|
|
150
|
+
if (signal === void 0) return await work;
|
|
151
|
+
const sentinel = { reason: void 0 };
|
|
152
|
+
let onAbort;
|
|
153
|
+
const abortPromise = new Promise((_, reject) => {
|
|
154
|
+
if (signal.aborted) {
|
|
155
|
+
sentinel.reason = signal.reason;
|
|
156
|
+
reject(sentinel);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
onAbort = () => {
|
|
160
|
+
sentinel.reason = signal.reason;
|
|
161
|
+
reject(sentinel);
|
|
162
|
+
};
|
|
163
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
164
|
+
});
|
|
165
|
+
try {
|
|
166
|
+
return await Promise.race([work, abortPromise]);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (error === sentinel) throw runtimeAborted(phase, sentinel.reason);
|
|
169
|
+
throw error;
|
|
170
|
+
} finally {
|
|
171
|
+
if (onAbort) signal.removeEventListener("abort", onAbort);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/execution/run-with-middleware.ts
|
|
177
|
+
/**
|
|
178
|
+
* Drives a single execution of `runDriver()` through the middleware lifecycle.
|
|
179
|
+
*
|
|
180
|
+
* Lifecycle, in order:
|
|
181
|
+
* 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.
|
|
182
|
+
* 2. For each row yielded by `runDriver()`: for each middleware in registration
|
|
183
|
+
* order: `onRow(row, exec, ctx)`; then yield the row to the consumer.
|
|
184
|
+
* 3. On successful completion: for each middleware in registration order:
|
|
185
|
+
* `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.
|
|
186
|
+
* 4. On any error thrown by the driver loop: for each middleware in
|
|
187
|
+
* registration order: `afterExecute(exec, { rowCount, latencyMs,
|
|
188
|
+
* completed: false }, ctx)`. Errors thrown by `afterExecute` during the
|
|
189
|
+
* error path are swallowed so they do not mask the original driver error.
|
|
190
|
+
* The original error is then rethrown.
|
|
191
|
+
*
|
|
192
|
+
* This helper is the single canonical implementation of the middleware
|
|
193
|
+
* orchestration loop; family runtimes should not reimplement it.
|
|
194
|
+
*/
|
|
195
|
+
function runWithMiddleware(exec, middleware, ctx, runDriver) {
|
|
196
|
+
const iterator = async function* () {
|
|
197
|
+
const startedAt = Date.now();
|
|
198
|
+
let rowCount = 0;
|
|
199
|
+
let completed = false;
|
|
200
|
+
try {
|
|
201
|
+
for (const mw of middleware) if (mw.beforeExecute) await mw.beforeExecute(exec, ctx);
|
|
202
|
+
for await (const row of runDriver()) {
|
|
203
|
+
for (const mw of middleware) if (mw.onRow) await mw.onRow(row, exec, ctx);
|
|
204
|
+
rowCount++;
|
|
205
|
+
yield row;
|
|
206
|
+
}
|
|
207
|
+
completed = true;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
const latencyMs$1 = Date.now() - startedAt;
|
|
210
|
+
for (const mw of middleware) if (mw.afterExecute) try {
|
|
211
|
+
await mw.afterExecute(exec, {
|
|
212
|
+
rowCount,
|
|
213
|
+
latencyMs: latencyMs$1,
|
|
214
|
+
completed
|
|
215
|
+
}, ctx);
|
|
216
|
+
} catch {}
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
const latencyMs = Date.now() - startedAt;
|
|
220
|
+
for (const mw of middleware) if (mw.afterExecute) await mw.afterExecute(exec, {
|
|
221
|
+
rowCount,
|
|
222
|
+
latencyMs,
|
|
223
|
+
completed
|
|
224
|
+
}, ctx);
|
|
225
|
+
};
|
|
226
|
+
return new AsyncIterableResult(iterator());
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/execution/runtime-core.ts
|
|
231
|
+
/**
|
|
232
|
+
* Family-agnostic abstract runtime base.
|
|
233
|
+
*
|
|
234
|
+
* Defines the entire `execute(plan)` template in one place:
|
|
235
|
+
*
|
|
236
|
+
* 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides
|
|
237
|
+
* this to run its `beforeCompile` middleware-hook chain.
|
|
238
|
+
* 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`
|
|
239
|
+
* (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).
|
|
240
|
+
* 3. `runWithMiddleware(exec, this.middleware, this.ctx,
|
|
241
|
+
* () => runDriver(exec))` — concrete; lifts the middleware lifecycle
|
|
242
|
+
* out of the family runtimes into the canonical helper.
|
|
243
|
+
*
|
|
244
|
+
* Concrete subclasses must implement `lower`, `runDriver`, and `close`.
|
|
245
|
+
*
|
|
246
|
+
* The class is generic over:
|
|
247
|
+
* - `TPlan` — the family's pre-lowering plan type.
|
|
248
|
+
* - `TExec` — the family's post-lowering (executable) plan type.
|
|
249
|
+
* - `TMiddleware` — the family's middleware type. Constrained to
|
|
250
|
+
* `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the
|
|
251
|
+
* `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered
|
|
252
|
+
* `TExec`. (The spec/plan wording "RuntimeMiddleware<TPlan>" is
|
|
253
|
+
* tightened to `<TExec>` here so the helper call typechecks; the
|
|
254
|
+
* intent is unchanged — middleware sees the post-lowering plan.)
|
|
255
|
+
*/
|
|
256
|
+
var RuntimeCore = class {
|
|
257
|
+
middleware;
|
|
258
|
+
ctx;
|
|
259
|
+
constructor(options) {
|
|
260
|
+
this.middleware = options.middleware;
|
|
261
|
+
this.ctx = options.ctx;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses
|
|
265
|
+
* may override to run a `beforeCompile` middleware chain (SQL does this
|
|
266
|
+
* to support typed AST rewrites — see `before-compile-chain.ts`).
|
|
267
|
+
*/
|
|
268
|
+
runBeforeCompile(plan) {
|
|
269
|
+
return plan;
|
|
270
|
+
}
|
|
271
|
+
execute(plan, options) {
|
|
272
|
+
const self = this;
|
|
273
|
+
const signal = options?.signal;
|
|
274
|
+
const codecCtx = signal === void 0 ? {} : { signal };
|
|
275
|
+
async function* generator() {
|
|
276
|
+
checkAborted(codecCtx, "stream");
|
|
277
|
+
const compiled = await self.runBeforeCompile(plan);
|
|
278
|
+
const exec = await self.lower(compiled, codecCtx);
|
|
279
|
+
yield* runWithMiddleware(exec, self.middleware, self.ctx, () => self.runDriver(exec));
|
|
280
|
+
}
|
|
281
|
+
return new AsyncIterableResult(generator());
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
//#endregion
|
|
286
|
+
//#region src/execution/runtime-middleware.ts
|
|
76
287
|
function checkMiddlewareCompatibility(middleware, runtimeFamilyId, runtimeTargetId) {
|
|
77
288
|
if (middleware.targetId !== void 0 && middleware.familyId === void 0) throw runtimeError("RUNTIME.MIDDLEWARE_INCOMPATIBLE", `Middleware '${middleware.name}' specifies targetId '${middleware.targetId}' without familyId`, {
|
|
78
289
|
middleware: middleware.name,
|
|
@@ -91,5 +302,5 @@ function checkMiddlewareCompatibility(middleware, runtimeFamilyId, runtimeTarget
|
|
|
91
302
|
}
|
|
92
303
|
|
|
93
304
|
//#endregion
|
|
94
|
-
export { AsyncIterableResult, checkMiddlewareCompatibility, runtimeError };
|
|
305
|
+
export { AsyncIterableResult, RUNTIME_ABORTED, RuntimeCore, checkAborted, checkMiddlewareCompatibility, isRuntimeError, raceAgainstAbort, runWithMiddleware, runtimeAborted, runtimeError };
|
|
95
306
|
//# sourceMappingURL=runtime.mjs.map
|