@prisma-next/framework-components 0.5.0-dev.3 → 0.5.0-dev.30

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.
Files changed (83) hide show
  1. package/README.md +44 -0
  2. package/dist/authoring.d.mts +1 -1
  3. package/dist/authoring.mjs +1 -1
  4. package/dist/authoring.mjs.map +1 -1
  5. package/dist/codec-types-DXv-ROhF.d.mts +84 -0
  6. package/dist/codec-types-DXv-ROhF.d.mts.map +1 -0
  7. package/dist/codec.d.mts +2 -2
  8. package/dist/codec.mjs +1 -1
  9. package/dist/codec.mjs.map +1 -1
  10. package/dist/components.d.mts +1 -1
  11. package/dist/components.mjs +1 -1
  12. package/dist/control.d.mts +80 -27
  13. package/dist/control.d.mts.map +1 -1
  14. package/dist/control.mjs +11 -5
  15. package/dist/control.mjs.map +1 -1
  16. package/dist/{emission-types-BPAALJbF.d.mts → emission-types-D234HxUz.d.mts} +3 -3
  17. package/dist/emission-types-D234HxUz.d.mts.map +1 -0
  18. package/dist/emission.d.mts +2 -2
  19. package/dist/execution.d.mts +5 -5
  20. package/dist/execution.d.mts.map +1 -1
  21. package/dist/execution.mjs +3 -3
  22. package/dist/execution.mjs.map +1 -1
  23. package/dist/{framework-authoring-D1-JZ37B.d.mts → framework-authoring-BdrFDx4x.d.mts} +2 -2
  24. package/dist/framework-authoring-BdrFDx4x.d.mts.map +1 -0
  25. package/dist/{framework-components-C8ZhSwXe.mjs → framework-components-BsWST1Rn.mjs} +2 -2
  26. package/dist/framework-components-BsWST1Rn.mjs.map +1 -0
  27. package/dist/{framework-components-EJXe-pum.d.mts → framework-components-Buvf7mnC.d.mts} +6 -6
  28. package/dist/framework-components-Buvf7mnC.d.mts.map +1 -0
  29. package/dist/psl-ast-9X5rwo98.d.mts +159 -0
  30. package/dist/psl-ast-9X5rwo98.d.mts.map +1 -0
  31. package/dist/psl-ast.d.mts +2 -0
  32. package/dist/psl-ast.mjs +1 -0
  33. package/dist/runtime.d.mts +251 -19
  34. package/dist/runtime.d.mts.map +1 -1
  35. package/dist/runtime.mjs +215 -4
  36. package/dist/runtime.mjs.map +1 -1
  37. package/dist/{types-import-spec-C4sc7wbb.d.mts → types-import-spec-D-O6GotH.d.mts} +2 -2
  38. package/dist/types-import-spec-D-O6GotH.d.mts.map +1 -0
  39. package/package.json +5 -4
  40. package/src/control/control-capabilities.ts +71 -0
  41. package/src/{control-descriptors.ts → control/control-descriptors.ts} +7 -7
  42. package/src/{control-instances.ts → control/control-instances.ts} +6 -6
  43. package/src/{control-migration-types.ts → control/control-migration-types.ts} +27 -15
  44. package/src/control/control-operation-preview.ts +23 -0
  45. package/src/{control-stack.ts → control/control-stack.ts} +13 -13
  46. package/src/{emission-types.ts → control/emission-types.ts} +1 -1
  47. package/src/control/psl-ast.ts +193 -0
  48. package/src/{execution-descriptors.ts → execution/execution-descriptors.ts} +7 -7
  49. package/src/{execution-instances.ts → execution/execution-instances.ts} +1 -1
  50. package/src/{execution-requirements.ts → execution/execution-requirements.ts} +1 -1
  51. package/src/execution/query-plan.ts +53 -0
  52. package/src/execution/race-against-abort.ts +85 -0
  53. package/src/execution/run-with-middleware.ts +77 -0
  54. package/src/execution/runtime-core.ts +133 -0
  55. package/src/execution/runtime-error.ts +83 -0
  56. package/src/{runtime-middleware.ts → execution/runtime-middleware.ts} +32 -12
  57. package/src/exports/authoring.ts +2 -2
  58. package/src/exports/codec.ts +2 -2
  59. package/src/exports/components.ts +2 -2
  60. package/src/exports/control.ts +26 -12
  61. package/src/exports/emission.ts +2 -2
  62. package/src/exports/execution.ts +5 -5
  63. package/src/exports/psl-ast.ts +1 -0
  64. package/src/exports/runtime.ts +16 -5
  65. package/src/shared/codec-types.ts +91 -0
  66. package/dist/codec-types-B58nCJiu.d.mts +0 -40
  67. package/dist/codec-types-B58nCJiu.d.mts.map +0 -1
  68. package/dist/emission-types-BPAALJbF.d.mts.map +0 -1
  69. package/dist/framework-authoring-D1-JZ37B.d.mts.map +0 -1
  70. package/dist/framework-components-C8ZhSwXe.mjs.map +0 -1
  71. package/dist/framework-components-EJXe-pum.d.mts.map +0 -1
  72. package/dist/types-import-spec-C4sc7wbb.d.mts.map +0 -1
  73. package/src/codec-types.ts +0 -46
  74. package/src/control-capabilities.ts +0 -34
  75. package/src/runtime-error.ts +0 -39
  76. /package/src/{control-result-types.ts → control/control-result-types.ts} +0 -0
  77. /package/src/{control-schema-view.ts → control/control-schema-view.ts} +0 -0
  78. /package/src/{async-iterable-result.ts → execution/async-iterable-result.ts} +0 -0
  79. /package/src/{execution-stack.ts → execution/execution-stack.ts} +0 -0
  80. /package/src/{framework-authoring.ts → shared/framework-authoring.ts} +0 -0
  81. /package/src/{framework-components.ts → shared/framework-components.ts} +0 -0
  82. /package/src/{mutation-default-types.ts → shared/mutation-default-types.ts} +0 -0
  83. /package/src/{types-import-spec.ts → shared/types-import-spec.ts} +0 -0
@@ -0,0 +1,53 @@
1
+ import type { PlanMeta } from '@prisma-next/contract/types';
2
+
3
+ /**
4
+ * Family-agnostic plan marker.
5
+ *
6
+ * Carries only `meta` (the family-agnostic plan metadata) and the optional
7
+ * phantom `_row` parameter that lets type-level utilities recover the row
8
+ * type from a plan value. SQL and Mongo extend this marker with their own
9
+ * concrete shapes (`SqlQueryPlan`, `MongoQueryPlan`).
10
+ *
11
+ * `QueryPlan` is the *pre-lowering* marker — i.e. the surface a builder
12
+ * produces before family-specific lowering turns it into an executable
13
+ * plan (`ExecutionPlan`).
14
+ */
15
+ export interface QueryPlan<Row = unknown> {
16
+ readonly meta: PlanMeta;
17
+ /**
18
+ * Phantom property to carry the Row generic for type-level utilities.
19
+ * Not set at runtime; used only for `ResultType` extraction.
20
+ */
21
+ readonly _row?: Row;
22
+ }
23
+
24
+ /**
25
+ * Family-agnostic execution-plan marker.
26
+ *
27
+ * Extends `QueryPlan` with no additional structural fields — the marker
28
+ * exists to nominally distinguish executable plans from pre-lowering plans
29
+ * in the type system. Family-specific execution plans (`SqlExecutionPlan`,
30
+ * `MongoExecutionPlan`) extend this marker with their concrete shapes
31
+ * (e.g. `sql + params` for SQL, `wireCommand` for Mongo).
32
+ */
33
+ export interface ExecutionPlan<Row = unknown> extends QueryPlan<Row> {}
34
+
35
+ /**
36
+ * Extracts the `Row` type from a plan via the phantom `_row` property.
37
+ *
38
+ * Works with any plan that extends `QueryPlan<Row>` — including
39
+ * `ExecutionPlan<Row>`, `SqlQueryPlan<Row>`, `SqlExecutionPlan<Row>`,
40
+ * `MongoQueryPlan<Row>`, and `MongoExecutionPlan<Row>`.
41
+ *
42
+ * The `_row` property must be present in the plan's static type for the
43
+ * conditional to bind `R`; objects whose type lacks `_row` resolve to
44
+ * `never`. Without the `keyof` guard, `extends { _row?: infer R }` would
45
+ * silently match any object and infer `unknown`.
46
+ *
47
+ * Example: `type Row = ResultType<typeof plan>`.
48
+ */
49
+ export type ResultType<P> = '_row' extends keyof P
50
+ ? P extends { readonly _row?: infer R }
51
+ ? R
52
+ : never
53
+ : never;
@@ -0,0 +1,85 @@
1
+ import type { CodecCallContext } from '../shared/codec-types';
2
+ import type { RuntimeAbortedPhase } from './runtime-error';
3
+ import { runtimeAborted } from './runtime-error';
4
+
5
+ /**
6
+ * Throw a phase-tagged `RUNTIME.ABORTED` envelope if the supplied
7
+ * codec-call context is already aborted at the precheck site. Centralises
8
+ * the `if (ctx.signal?.aborted) throw runtimeAborted(...)` pattern that
9
+ * every codec dispatch site repeats.
10
+ */
11
+ export function checkAborted(ctx: CodecCallContext, phase: RuntimeAbortedPhase): void {
12
+ if (ctx.signal?.aborted) {
13
+ throw runtimeAborted(phase, ctx.signal.reason);
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Race a per-cell `Promise.all` (or any other in-flight work promise) against
19
+ * the supplied abort signal so the runtime returns `RUNTIME.ABORTED` promptly
20
+ * even when codec bodies ignore the signal. In-flight bodies that ignore the
21
+ * signal are abandoned and run to completion in the background — the
22
+ * cooperative-cancellation contract documented in ADR 204.
23
+ *
24
+ * Call sites still SHOULD pre-check `signal.aborted` and short-circuit with
25
+ * a phase-tagged `RUNTIME.ABORTED` envelope before invoking this helper —
26
+ * that path is the canonical "aborted at entry" surface and avoids
27
+ * scheduling the work promise. As a defensive belt-and-braces, this helper
28
+ * also handles the already-aborted case internally: `AbortSignal` does not
29
+ * replay past abort events to listeners registered after the abort, so we
30
+ * inspect `signal.aborted` synchronously and reject with the sentinel
31
+ * before installing the listener. The rejection is still attributed to the
32
+ * abort path via the sentinel-identity check.
33
+ *
34
+ * Distinguishing the rejection source is load-bearing for AC-ERR4
35
+ * (`RUNTIME.ENCODE_FAILED` / `RUNTIME.DECODE_FAILED` pass through unchanged).
36
+ * The semantically equivalent `abortable(signal)` helper in
37
+ * `@prisma-next/utils` rejects with `signal.reason ?? new DOMException(...)`,
38
+ * which is not stably distinguishable from a codec-thrown error by identity
39
+ * alone (a fresh fallback DOMException is allocated per call). We instead
40
+ * track abort attribution with a unique sentinel: only the `onAbort` listener
41
+ * installed here ever rejects with the sentinel, so an `error === sentinel`
42
+ * identity check after the race is unambiguous.
43
+ *
44
+ * Lives in `framework-components` (rather than the SQL family, where it
45
+ * originated in m2) so every family runtime that needs cooperative
46
+ * cancellation around a codec-dispatch `Promise.all` (SQL encode + decode
47
+ * today, Mongo encode in m3) shares the same attribution logic.
48
+ */
49
+ export async function raceAgainstAbort<T>(
50
+ work: Promise<T>,
51
+ signal: AbortSignal | undefined,
52
+ phase: RuntimeAbortedPhase,
53
+ ): Promise<T> {
54
+ if (signal === undefined) {
55
+ return await work;
56
+ }
57
+ const sentinel: { reason: unknown } = { reason: undefined };
58
+ let onAbort: (() => void) | undefined;
59
+
60
+ const abortPromise = new Promise<never>((_, reject) => {
61
+ if (signal.aborted) {
62
+ sentinel.reason = signal.reason;
63
+ reject(sentinel);
64
+ return;
65
+ }
66
+ onAbort = () => {
67
+ sentinel.reason = signal.reason;
68
+ reject(sentinel);
69
+ };
70
+ signal.addEventListener('abort', onAbort, { once: true });
71
+ });
72
+
73
+ try {
74
+ return await Promise.race([work, abortPromise]);
75
+ } catch (error) {
76
+ if (error === sentinel) {
77
+ throw runtimeAborted(phase, sentinel.reason);
78
+ }
79
+ throw error;
80
+ } finally {
81
+ if (onAbort) {
82
+ signal.removeEventListener('abort', onAbort);
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,77 @@
1
+ import { AsyncIterableResult } from './async-iterable-result';
2
+ import type { ExecutionPlan } from './query-plan';
3
+ import type { RuntimeMiddleware, RuntimeMiddlewareContext } from './runtime-middleware';
4
+
5
+ /**
6
+ * Drives a single execution of `runDriver()` through the middleware lifecycle.
7
+ *
8
+ * Lifecycle, in order:
9
+ * 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.
10
+ * 2. For each row yielded by `runDriver()`: for each middleware in registration
11
+ * order: `onRow(row, exec, ctx)`; then yield the row to the consumer.
12
+ * 3. On successful completion: for each middleware in registration order:
13
+ * `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.
14
+ * 4. On any error thrown by the driver loop: for each middleware in
15
+ * registration order: `afterExecute(exec, { rowCount, latencyMs,
16
+ * completed: false }, ctx)`. Errors thrown by `afterExecute` during the
17
+ * error path are swallowed so they do not mask the original driver error.
18
+ * The original error is then rethrown.
19
+ *
20
+ * This helper is the single canonical implementation of the middleware
21
+ * orchestration loop; family runtimes should not reimplement it.
22
+ */
23
+ export function runWithMiddleware<TExec extends ExecutionPlan, Row>(
24
+ exec: TExec,
25
+ middleware: ReadonlyArray<RuntimeMiddleware<TExec>>,
26
+ ctx: RuntimeMiddlewareContext,
27
+ runDriver: () => AsyncIterable<Row>,
28
+ ): AsyncIterableResult<Row> {
29
+ const iterator = async function* (): AsyncGenerator<Row, void, unknown> {
30
+ const startedAt = Date.now();
31
+ let rowCount = 0;
32
+ let completed = false;
33
+
34
+ try {
35
+ for (const mw of middleware) {
36
+ if (mw.beforeExecute) {
37
+ await mw.beforeExecute(exec, ctx);
38
+ }
39
+ }
40
+
41
+ for await (const row of runDriver()) {
42
+ for (const mw of middleware) {
43
+ if (mw.onRow) {
44
+ await mw.onRow(row as Record<string, unknown>, exec, ctx);
45
+ }
46
+ }
47
+ rowCount++;
48
+ yield row;
49
+ }
50
+
51
+ completed = true;
52
+ } catch (error) {
53
+ const latencyMs = Date.now() - startedAt;
54
+ for (const mw of middleware) {
55
+ if (mw.afterExecute) {
56
+ try {
57
+ await mw.afterExecute(exec, { rowCount, latencyMs, completed }, ctx);
58
+ } catch {
59
+ // Swallow afterExecute errors during the error path so they do not
60
+ // mask the original driver error.
61
+ }
62
+ }
63
+ }
64
+
65
+ throw error;
66
+ }
67
+
68
+ const latencyMs = Date.now() - startedAt;
69
+ for (const mw of middleware) {
70
+ if (mw.afterExecute) {
71
+ await mw.afterExecute(exec, { rowCount, latencyMs, completed }, ctx);
72
+ }
73
+ }
74
+ };
75
+
76
+ return new AsyncIterableResult(iterator());
77
+ }
@@ -0,0 +1,133 @@
1
+ import type { CodecCallContext } from '../shared/codec-types';
2
+ import { AsyncIterableResult } from './async-iterable-result';
3
+ import type { ExecutionPlan, QueryPlan } from './query-plan';
4
+ import { checkAborted } from './race-against-abort';
5
+ import { runWithMiddleware } from './run-with-middleware';
6
+ import type {
7
+ RuntimeExecuteOptions,
8
+ RuntimeExecutor,
9
+ RuntimeMiddleware,
10
+ RuntimeMiddlewareContext,
11
+ } from './runtime-middleware';
12
+
13
+ /**
14
+ * Constructor options shared by every concrete `RuntimeCore` subclass.
15
+ *
16
+ * Family runtimes typically build the middleware list and the
17
+ * `RuntimeMiddlewareContext` themselves (running compatibility checks,
18
+ * narrowing the context's `contract` field, etc.) before calling `super`.
19
+ */
20
+ export interface RuntimeCoreOptions<TMiddleware extends RuntimeMiddleware<ExecutionPlan>> {
21
+ readonly middleware: ReadonlyArray<TMiddleware>;
22
+ readonly ctx: RuntimeMiddlewareContext;
23
+ }
24
+
25
+ /**
26
+ * Family-agnostic abstract runtime base.
27
+ *
28
+ * Defines the entire `execute(plan)` template in one place:
29
+ *
30
+ * 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides
31
+ * this to run its `beforeCompile` middleware-hook chain.
32
+ * 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`
33
+ * (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).
34
+ * 3. `runWithMiddleware(exec, this.middleware, this.ctx,
35
+ * () => runDriver(exec))` — concrete; lifts the middleware lifecycle
36
+ * out of the family runtimes into the canonical helper.
37
+ *
38
+ * Concrete subclasses must implement `lower`, `runDriver`, and `close`.
39
+ *
40
+ * The class is generic over:
41
+ * - `TPlan` — the family's pre-lowering plan type.
42
+ * - `TExec` — the family's post-lowering (executable) plan type.
43
+ * - `TMiddleware` — the family's middleware type. Constrained to
44
+ * `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the
45
+ * `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered
46
+ * `TExec`. (The spec/plan wording "RuntimeMiddleware<TPlan>" is
47
+ * tightened to `<TExec>` here so the helper call typechecks; the
48
+ * intent is unchanged — middleware sees the post-lowering plan.)
49
+ */
50
+ export abstract class RuntimeCore<
51
+ TPlan extends QueryPlan,
52
+ TExec extends ExecutionPlan,
53
+ TMiddleware extends RuntimeMiddleware<TExec>,
54
+ > implements RuntimeExecutor<TPlan>
55
+ {
56
+ protected readonly middleware: ReadonlyArray<TMiddleware>;
57
+ protected readonly ctx: RuntimeMiddlewareContext;
58
+
59
+ constructor(options: RuntimeCoreOptions<TMiddleware>) {
60
+ this.middleware = options.middleware;
61
+ this.ctx = options.ctx;
62
+ }
63
+
64
+ /**
65
+ * Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses
66
+ * may override to run a `beforeCompile` middleware chain (SQL does this
67
+ * to support typed AST rewrites — see `before-compile-chain.ts`).
68
+ */
69
+ protected runBeforeCompile(plan: TPlan): TPlan | Promise<TPlan> {
70
+ return plan;
71
+ }
72
+
73
+ /**
74
+ * Lower a pre-lowering `TPlan` into the family's executable `TExec`.
75
+ * Family-specific: SQL produces `{ sql, params, ast?, ... }`; Mongo
76
+ * produces `{ command, ... }`.
77
+ *
78
+ * `ctx` carries per-query cancellation (and any future fields on
79
+ * `CodecCallContext`); concrete subclasses forward it to the
80
+ * encode-side codec dispatch site (e.g. SQL's `encodeParams` in m2,
81
+ * Mongo's `resolveValue` in m3). The runtime allocates one ctx per
82
+ * `execute()` call and threads the same reference everywhere; the
83
+ * `signal` field inside may be `undefined`, but the ctx object itself
84
+ * is always present.
85
+ */
86
+ protected abstract lower(plan: TPlan, ctx: CodecCallContext): TExec | Promise<TExec>;
87
+
88
+ /**
89
+ * Drive the underlying transport for a lowered `TExec`. Yields raw rows
90
+ * directly from the driver as `Record<string, unknown>`; codec decoding
91
+ * (if any) is the subclass's responsibility, applied by wrapping
92
+ * `execute()` rather than living inside this hook.
93
+ *
94
+ * The `Row` type parameter on `execute()` is satisfied by the caller via
95
+ * the plan's phantom `_row`; the runtime treats rows as opaque records
96
+ * here and trusts the caller's row typing.
97
+ */
98
+ protected abstract runDriver(exec: TExec): AsyncIterable<Record<string, unknown>>;
99
+
100
+ abstract close(): Promise<void>;
101
+
102
+ execute<Row>(
103
+ plan: TPlan & { readonly _row?: Row },
104
+ options?: RuntimeExecuteOptions,
105
+ ): AsyncIterableResult<Row> {
106
+ const self = this;
107
+ const signal = options?.signal;
108
+ // One ctx per execute() call. The ctx object is always allocated; the
109
+ // `signal` field is only included when a signal was supplied (required
110
+ // under exactOptionalPropertyTypes — `{ signal: undefined }` would not
111
+ // satisfy `signal?: AbortSignal`).
112
+ const codecCtx: CodecCallContext = signal === undefined ? {} : { signal };
113
+
114
+ async function* generator(): AsyncGenerator<Row, void, unknown> {
115
+ // Pre-check the signal at entry so an already-aborted caller observes
116
+ // RUNTIME.ABORTED on the first `next()` without any work being done.
117
+ checkAborted(codecCtx, 'stream');
118
+
119
+ const compiled = await self.runBeforeCompile(plan);
120
+ const exec = await self.lower(compiled, codecCtx);
121
+ // The driver yields raw `Record<string, unknown>`; we cast to `Row` here.
122
+ // The Row contract is enforced by the caller via `plan._row`.
123
+ yield* runWithMiddleware<TExec, Row>(
124
+ exec,
125
+ self.middleware,
126
+ self.ctx,
127
+ () => self.runDriver(exec) as AsyncIterable<Row>,
128
+ );
129
+ }
130
+
131
+ return new AsyncIterableResult(generator());
132
+ }
133
+ }
@@ -0,0 +1,83 @@
1
+ export interface RuntimeErrorEnvelope extends Error {
2
+ readonly code: string;
3
+ readonly category: 'PLAN' | 'CONTRACT' | 'LINT' | 'BUDGET' | 'RUNTIME';
4
+ readonly severity: 'error';
5
+ readonly details?: Record<string, unknown>;
6
+ }
7
+
8
+ /**
9
+ * Stable code emitted by the runtime when an in-flight `execute()`
10
+ * is cancelled via the per-query `AbortSignal`. The envelope's
11
+ * `details.phase` distinguishes where the abort was observed:
12
+ *
13
+ * - `'encode'` — abort fired during `encodeParams` (SQL) or
14
+ * `resolveValue` (Mongo).
15
+ * - `'decode'` — abort fired during `decodeRow` / `decodeField`.
16
+ * - `'stream'` — abort fired between rows or before any codec call
17
+ * (already-aborted at entry).
18
+ */
19
+ export const RUNTIME_ABORTED = 'RUNTIME.ABORTED' as const;
20
+
21
+ /** Discriminator placed in `details.phase` of a `RUNTIME.ABORTED` envelope. */
22
+ export type RuntimeAbortedPhase = 'encode' | 'decode' | 'stream';
23
+
24
+ /**
25
+ * Type guard for the runtime-error envelope produced by `runtimeError`.
26
+ *
27
+ * Prefer this over duck-typing on `error.code` directly so consumers stay
28
+ * insulated from the envelope's internal shape.
29
+ */
30
+ export function isRuntimeError(error: unknown): error is RuntimeErrorEnvelope {
31
+ return (
32
+ error instanceof Error &&
33
+ 'code' in error &&
34
+ typeof (error as { code?: unknown }).code === 'string' &&
35
+ 'category' in error &&
36
+ 'severity' in error
37
+ );
38
+ }
39
+
40
+ export function runtimeError(
41
+ code: string,
42
+ message: string,
43
+ details?: Record<string, unknown>,
44
+ ): RuntimeErrorEnvelope {
45
+ const error = new Error(message) as RuntimeErrorEnvelope;
46
+ Object.defineProperty(error, 'name', {
47
+ value: 'RuntimeError',
48
+ configurable: true,
49
+ });
50
+
51
+ return Object.assign(error, {
52
+ code,
53
+ category: resolveCategory(code),
54
+ severity: 'error' as const,
55
+ message,
56
+ details,
57
+ });
58
+ }
59
+
60
+ function resolveCategory(code: string): RuntimeErrorEnvelope['category'] {
61
+ const prefix = code.split('.')[0] ?? 'RUNTIME';
62
+ switch (prefix) {
63
+ case 'PLAN':
64
+ case 'CONTRACT':
65
+ case 'LINT':
66
+ case 'BUDGET':
67
+ return prefix;
68
+ default:
69
+ return 'RUNTIME';
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Construct a `RUNTIME.ABORTED` envelope. Phase distinguishes where the
75
+ * abort was observed (encode / decode / stream); cause carries `signal.reason`
76
+ * verbatim from the platform — native abort produces a `DOMException`,
77
+ * explicit `controller.abort(reason)` produces whatever the caller passed.
78
+ * No synthesis happens here.
79
+ */
80
+ export function runtimeAborted(phase: RuntimeAbortedPhase, cause?: unknown): RuntimeErrorEnvelope {
81
+ const envelope = runtimeError(RUNTIME_ABORTED, `Operation aborted during ${phase}`, { phase });
82
+ return Object.assign(envelope, { cause });
83
+ }
@@ -1,5 +1,5 @@
1
- import type { PlanMeta } from '@prisma-next/contract/types';
2
1
  import type { AsyncIterableResult } from './async-iterable-result';
2
+ import type { QueryPlan } from './query-plan';
3
3
  import { runtimeError } from './runtime-error';
4
4
 
5
5
  export interface RuntimeLog {
@@ -22,33 +22,53 @@ export interface AfterExecuteResult {
22
22
  readonly completed: boolean;
23
23
  }
24
24
 
25
- export interface RuntimeMiddleware {
25
+ /**
26
+ * Family-agnostic middleware SPI parameterized over the plan marker.
27
+ *
28
+ * `TPlan` defaults to the framework `QueryPlan` marker so a generic
29
+ * middleware (e.g. cross-family telemetry) can be authored without
30
+ * naming a family. Family-specific middleware (`SqlMiddleware`,
31
+ * `MongoMiddleware`) narrow `TPlan` to their concrete plan type.
32
+ */
33
+ export interface RuntimeMiddleware<TPlan extends QueryPlan = QueryPlan> {
26
34
  readonly name: string;
27
35
  readonly familyId?: string;
28
36
  readonly targetId?: string;
29
- beforeExecute?(plan: { readonly meta: PlanMeta }, ctx: RuntimeMiddlewareContext): Promise<void>;
30
- onRow?(
31
- row: Record<string, unknown>,
32
- plan: { readonly meta: PlanMeta },
33
- ctx: RuntimeMiddlewareContext,
34
- ): Promise<void>;
37
+ beforeExecute?(plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
38
+ onRow?(row: Record<string, unknown>, plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
35
39
  afterExecute?(
36
- plan: { readonly meta: PlanMeta },
40
+ plan: TPlan,
37
41
  result: AfterExecuteResult,
38
42
  ctx: RuntimeMiddlewareContext,
39
43
  ): Promise<void>;
40
44
  }
41
45
 
46
+ /**
47
+ * Optional per-`execute` options accepted by every family runtime.
48
+ *
49
+ * `signal` is the per-query cancellation signal. The runtime threads the
50
+ * signal through to every codec call for the query and uses it to short-
51
+ * circuit the row stream with `RUNTIME.ABORTED` when the caller aborts.
52
+ * Omitting the option (or passing `undefined`) preserves today's behavior
53
+ * bit-for-bit.
54
+ */
55
+ export interface RuntimeExecuteOptions {
56
+ readonly signal?: AbortSignal;
57
+ }
58
+
42
59
  /**
43
60
  * Cross-family SPI for any runtime that can execute plans and be shut down.
44
61
  * Each family runtime (SQL, Mongo) satisfies this interface — SQL nominally,
45
62
  * Mongo structurally (due to its phantom Row parameter using a unique symbol).
46
63
  *
47
64
  * The `_row` intersection on `execute` connects the `Row` type parameter to the
48
- * plan, mirroring how `ExecutionPlan<Row>` carries a phantom `_row?: Row`.
65
+ * plan, mirroring how `QueryPlan<Row>` carries a phantom `_row?: Row`.
49
66
  */
50
- export interface RuntimeExecutor<TPlan extends { readonly meta: PlanMeta }> {
51
- execute<Row>(plan: TPlan & { readonly _row?: Row }): AsyncIterableResult<Row>;
67
+ export interface RuntimeExecutor<TPlan extends QueryPlan> {
68
+ execute<Row>(
69
+ plan: TPlan & { readonly _row?: Row },
70
+ options?: RuntimeExecuteOptions,
71
+ ): AsyncIterableResult<Row>;
52
72
  close(): Promise<void>;
53
73
  }
54
74
 
@@ -10,7 +10,7 @@ export type {
10
10
  AuthoringTemplateValue,
11
11
  AuthoringTypeConstructorDescriptor,
12
12
  AuthoringTypeNamespace,
13
- } from '../framework-authoring';
13
+ } from '../shared/framework-authoring';
14
14
  export {
15
15
  instantiateAuthoringFieldPreset,
16
16
  instantiateAuthoringTypeConstructor,
@@ -19,4 +19,4 @@ export {
19
19
  isAuthoringTypeConstructorDescriptor,
20
20
  resolveAuthoringTemplateValue,
21
21
  validateAuthoringHelperArguments,
22
- } from '../framework-authoring';
22
+ } from '../shared/framework-authoring';
@@ -1,2 +1,2 @@
1
- export type { Codec, CodecLookup, CodecTrait } from '../codec-types';
2
- export { emptyCodecLookup } from '../codec-types';
1
+ export type { Codec, CodecCallContext, CodecLookup, CodecTrait } from '../shared/codec-types';
2
+ export { emptyCodecLookup } from '../shared/codec-types';
@@ -20,5 +20,5 @@ export type {
20
20
  TargetDescriptor,
21
21
  TargetInstance,
22
22
  TargetPackRef,
23
- } from '../framework-components';
24
- export { checkContractComponentRequirements } from '../framework-components';
23
+ } from '../shared/framework-components';
24
+ export { checkContractComponentRequirements } from '../shared/framework-components';
@@ -1,19 +1,29 @@
1
- export type { MigratableTargetDescriptor, SchemaViewCapable } from '../control-capabilities';
2
- export { hasMigrations, hasSchemaView } from '../control-capabilities';
1
+ export type {
2
+ MigratableTargetDescriptor,
3
+ OperationPreviewCapable,
4
+ PslContractInferCapable,
5
+ SchemaViewCapable,
6
+ } from '../control/control-capabilities';
7
+ export {
8
+ hasMigrations,
9
+ hasOperationPreview,
10
+ hasPslContractInfer,
11
+ hasSchemaView,
12
+ } from '../control/control-capabilities';
3
13
  export type {
4
14
  ControlAdapterDescriptor,
5
15
  ControlDriverDescriptor,
6
16
  ControlExtensionDescriptor,
7
17
  ControlFamilyDescriptor,
8
18
  ControlTargetDescriptor,
9
- } from '../control-descriptors';
19
+ } from '../control/control-descriptors';
10
20
  export type {
11
21
  ControlAdapterInstance,
12
22
  ControlDriverInstance,
13
23
  ControlExtensionInstance,
14
24
  ControlFamilyInstance,
15
25
  ControlTargetInstance,
16
- } from '../control-instances';
26
+ } from '../control/control-instances';
17
27
  export type {
18
28
  DataTransformOperation,
19
29
  MigrationOperationClass,
@@ -35,7 +45,11 @@ export type {
35
45
  OpFactoryCall,
36
46
  SerializedQueryPlan,
37
47
  TargetMigrationsCapability,
38
- } from '../control-migration-types';
48
+ } from '../control/control-migration-types';
49
+ export type {
50
+ OperationPreview,
51
+ OperationPreviewStatement,
52
+ } from '../control/control-operation-preview';
39
53
  export type {
40
54
  BaseSchemaIssue,
41
55
  EmitContractResult,
@@ -47,25 +61,25 @@ export type {
47
61
  SignDatabaseResult,
48
62
  VerifyDatabaseResult,
49
63
  VerifyDatabaseSchemaResult,
50
- } from '../control-result-types';
64
+ } from '../control/control-result-types';
51
65
  export {
52
66
  VERIFY_CODE_HASH_MISMATCH,
53
67
  VERIFY_CODE_MARKER_MISSING,
54
68
  VERIFY_CODE_SCHEMA_FAILURE,
55
69
  VERIFY_CODE_TARGET_MISMATCH,
56
- } from '../control-result-types';
70
+ } from '../control/control-result-types';
57
71
  export type {
58
72
  CoreSchemaView,
59
73
  SchemaNodeKind,
60
74
  SchemaTreeNodeOptions,
61
75
  SchemaTreeVisitor,
62
- } from '../control-schema-view';
63
- export { SchemaTreeNode } from '../control-schema-view';
76
+ } from '../control/control-schema-view';
77
+ export { SchemaTreeNode } from '../control/control-schema-view';
64
78
  export type {
65
79
  AssembledAuthoringContributions,
66
80
  ControlStack,
67
81
  CreateControlStackInput,
68
- } from '../control-stack';
82
+ } from '../control/control-stack';
69
83
  export {
70
84
  assembleAuthoringContributions,
71
85
  assembleControlMutationDefaults,
@@ -77,7 +91,7 @@ export {
77
91
  extractComponentIds,
78
92
  extractOperationTypeImports,
79
93
  extractQueryOperationTypeImports,
80
- } from '../control-stack';
94
+ } from '../control/control-stack';
81
95
  export type {
82
96
  ControlMutationDefaultEntry,
83
97
  ControlMutationDefaultRegistry,
@@ -92,4 +106,4 @@ export type {
92
106
  ParsedDefaultFunctionCall,
93
107
  SourceDiagnostic,
94
108
  SourceSpan,
95
- } from '../mutation-default-types';
109
+ } from '../shared/mutation-default-types';
@@ -2,5 +2,5 @@ export type {
2
2
  EmissionSpi,
3
3
  GenerateContractTypesOptions,
4
4
  ValidationContext,
5
- } from '../emission-types';
6
- export type { TypesImportSpec } from '../types-import-spec';
5
+ } from '../control/emission-types';
6
+ export type { TypesImportSpec } from '../shared/types-import-spec';
@@ -4,14 +4,14 @@ export type {
4
4
  RuntimeExtensionDescriptor,
5
5
  RuntimeFamilyDescriptor,
6
6
  RuntimeTargetDescriptor,
7
- } from '../execution-descriptors';
7
+ } from '../execution/execution-descriptors';
8
8
  export type {
9
9
  RuntimeAdapterInstance,
10
10
  RuntimeDriverInstance,
11
11
  RuntimeExtensionInstance,
12
12
  RuntimeFamilyInstance,
13
13
  RuntimeTargetInstance,
14
- } from '../execution-instances';
15
- export { assertRuntimeContractRequirementsSatisfied } from '../execution-requirements';
16
- export type { ExecutionStack, ExecutionStackInstance } from '../execution-stack';
17
- export { createExecutionStack, instantiateExecutionStack } from '../execution-stack';
14
+ } from '../execution/execution-instances';
15
+ export { assertRuntimeContractRequirementsSatisfied } from '../execution/execution-requirements';
16
+ export type { ExecutionStack, ExecutionStackInstance } from '../execution/execution-stack';
17
+ export { createExecutionStack, instantiateExecutionStack } from '../execution/execution-stack';
@@ -0,0 +1 @@
1
+ export * from '../control/psl-ast';