@supacloud/elysia 0.30.1 → 0.31.1

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 CHANGED
@@ -193,6 +193,14 @@ concurrent request isolation. Run it with `bun run typecheck:test` as well as
193
193
 
194
194
  ## Persistent Command Adapters
195
195
 
196
+ For a local compiled Command whose handler must run in a PostgreSQL transaction,
197
+ use `createTransactionalCommandGovernance` with `createPostgresCommandStore`.
198
+ It supplies the governance callbacks and a request-isolated `transaction()`
199
+ accessor, delegating receipt, authorization-on-replay and audit to the existing
200
+ durable executor. The approval starter uses this binding and derives decoders
201
+ from its HTTP contracts. See [transactional defaults](../../docs/transactional-command-defaults.md)
202
+ for ownership, migration and native acceptance boundaries.
203
+
196
204
  `createPersistentCommandAdapter(command, { identity, input })` binds a
197
205
  `createTransactionalCommand` or `createExternalCommand` from `@supacloud/commands`
198
206
  to `commandGovernance.rpc`.
package/dist/index.d.ts CHANGED
@@ -25,6 +25,8 @@ export { createDefaultEffectRuntime, createEffectRuntimeFromLayer, EffectApplica
25
25
  export type { CompiledEffectDescriptor, SupaCloudEffectRuntime } from "./effect";
26
26
  export type { ElysiaRouteContext, ElysiaRouteDefinition, ElysiaRouteHandler, ElysiaRouteSchema, ResponseMapSelector, RouteContractSchemas, SchemaDecoderOptions, SchemaNormalizeMode, } from "./schema_contract";
27
27
  export { createPersistentCommandAdapter, type PersistentCommandHandler } from "./persistent-command";
28
+ export { createTransactionalCommandGovernance } from "./transactional-command";
29
+ export type { TransactionalCommandGovernanceOptions } from "./transactional-command";
28
30
  export { createDocumentationPlugin } from "./documentation";
29
31
  export type { ApplicationDocumentationOptions, DocumentationSource, GraphqlDocumentationOptions, OpenApiDocumentationOptions, } from "./documentation";
30
32
  export { createSupaCloudFramework } from "./framework";
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/index.ts
2
2
  import { Elysia as Elysia3, setupTypebox, Validator as Validator2 } from "elysia";
3
3
  import { PendingWorkRegistry as PendingWorkRegistry2, PendingWorkTimeoutError, PendingWorkCapacityError } from "@supacloud/app/runtime";
4
- import { commandFailureMetadata as commandFailureMetadata2, decodeCommandPreview } from "@supacloud/contracts";
4
+ import { commandFailureMetadata as commandFailureMetadata3, decodeCommandPreview } from "@supacloud/contracts";
5
5
 
6
6
  // src/command-errors.ts
7
7
  function commandErrorCode(error) {
@@ -1149,6 +1149,89 @@ function createPersistentCommandAdapter(command, options) {
1149
1149
  }
1150
1150
  };
1151
1151
  }
1152
+ // src/transactional-command.ts
1153
+ import { AsyncLocalStorage } from "node:async_hooks";
1154
+ import { createTransactionalCommand } from "@supacloud/commands";
1155
+ import {
1156
+ CommandError as CommandError2,
1157
+ commandFailureMetadata as commandFailureMetadata2,
1158
+ decodeCommandIdentity as decodeCommandIdentity2
1159
+ } from "@supacloud/contracts";
1160
+ function createTransactionalCommandGovernance(options) {
1161
+ const { definition, permission, identity, input, rejection } = options;
1162
+ const { name, audit } = definition;
1163
+ if (!permission.trim())
1164
+ throw new TypeError("Command permission is required");
1165
+ const calls = new AsyncLocalStorage;
1166
+ function current(invocation) {
1167
+ const call = calls.getStore();
1168
+ if (!call?.active || call.transaction === undefined || invocation !== undefined && call.invocation !== invocation) {
1169
+ throw new Error("Command transaction is not active for this invocation");
1170
+ }
1171
+ return { ...call, transaction: call.transaction };
1172
+ }
1173
+ function assertBinding(invocation) {
1174
+ const command = invocation.command;
1175
+ if (command.name !== name || command.permission !== permission || command.audit !== audit.event || command.transaction !== "required" || command.idempotency !== "required" || command.rpc !== undefined) {
1176
+ throw new ApplicationError("Unsupported transactional command", { status: 501, code: "COMMAND_BINDING_MISMATCH" });
1177
+ }
1178
+ }
1179
+ const command = createTransactionalCommand({
1180
+ ...definition,
1181
+ async execute(transaction) {
1182
+ const call = calls.getStore();
1183
+ if (!call || call.active)
1184
+ throw new Error("Command continuation is not available");
1185
+ call.transaction = transaction;
1186
+ call.active = true;
1187
+ try {
1188
+ return await call.next();
1189
+ } catch (error) {
1190
+ throw rejection?.(error) ?? error;
1191
+ } finally {
1192
+ call.active = false;
1193
+ call.transaction = undefined;
1194
+ }
1195
+ }
1196
+ });
1197
+ return {
1198
+ transaction: () => current().transaction,
1199
+ governance: {
1200
+ async authorize(invocation) {
1201
+ assertBinding(invocation);
1202
+ decodeCommandIdentity2(await identity(invocation));
1203
+ },
1204
+ async idempotency(invocation, next) {
1205
+ assertBinding(invocation);
1206
+ try {
1207
+ const actor = decodeCommandIdentity2(await identity(invocation));
1208
+ const key = requireIdempotencyKey(invocation);
1209
+ const value = input(invocation);
1210
+ const receipt = await calls.run({ invocation, next, active: false }, () => command.execute(actor, key, value, invocation.request.signal));
1211
+ if (receipt.status !== "confirmed")
1212
+ throw new CommandError2("COMMAND_OUTCOME_UNKNOWN");
1213
+ return receipt.result;
1214
+ } catch (error) {
1215
+ const code = commandErrorCode(error);
1216
+ if (!code)
1217
+ throw error;
1218
+ throw new ApplicationError(code, {
1219
+ code,
1220
+ status: commandErrorStatus(code),
1221
+ metadata: commandFailureMetadata2(code)
1222
+ });
1223
+ }
1224
+ },
1225
+ transaction(invocation, next) {
1226
+ current(invocation);
1227
+ return next();
1228
+ },
1229
+ audit: { succeeded(invocation) {
1230
+ current(invocation);
1231
+ }, failed() {} }
1232
+ }
1233
+ };
1234
+ }
1152
1235
  // src/framework.ts
1153
1236
  function createSupaCloudFramework(options) {
1154
1237
  return createApplication({
@@ -1174,7 +1257,7 @@ function assertFeatureTransition(spec, state, event) {
1174
1257
  return transition.to;
1175
1258
  }
1176
1259
  // src/memory.ts
1177
- import { AsyncLocalStorage } from "node:async_hooks";
1260
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
1178
1261
 
1179
1262
  // src/memory_policy.ts
1180
1263
  function createMemoryPolicy() {
@@ -1249,7 +1332,7 @@ function stableStringify(value) {
1249
1332
  class DefaultMemoryDatabase {
1250
1333
  tables = new Map;
1251
1334
  version = 0;
1252
- current = new AsyncLocalStorage;
1335
+ current = new AsyncLocalStorage2;
1253
1336
  get(table, key) {
1254
1337
  if (this.current.getStore())
1255
1338
  return this.current.getStore()?.get(table, key);
@@ -2774,7 +2857,7 @@ function defaultErrorResponse(error, frameworkCode) {
2774
2857
  ok: false,
2775
2858
  code: protocolCode,
2776
2859
  message: protocolCode,
2777
- metadata: commandFailureMetadata2(protocolCode)
2860
+ metadata: commandFailureMetadata3(protocolCode)
2778
2861
  }, {
2779
2862
  status: commandErrorStatus(protocolCode)
2780
2863
  });
@@ -2966,6 +3049,7 @@ export {
2966
3049
  createSupaCloudFramework,
2967
3050
  createSupaCloudRequestContext,
2968
3051
  createTestApp,
3052
+ createTransactionalCommandGovernance,
2969
3053
  createWorker,
2970
3054
  defaultErrorResponse,
2971
3055
  defineElysiaRoute,
@@ -0,0 +1,20 @@
1
+ import { type PersistentCommandDefinition } from "@supacloud/commands";
2
+ import { CommandError, type CommandIdentity } from "@supacloud/contracts";
3
+ import { type CommandGovernance, type CommandInvocation } from "./index";
4
+ export interface TransactionalCommandGovernanceOptions<Input, Result, Transaction> {
5
+ definition: PersistentCommandDefinition<Input, Result, Transaction>;
6
+ permission: string;
7
+ /** The host must verify identity and project ownership, never use body fields. */
8
+ identity(invocation: CommandInvocation): CommandIdentity | Promise<CommandIdentity>;
9
+ input(invocation: CommandInvocation): unknown;
10
+ /** Translate only known domain rejections; unknown failures stay outcome-unknown. */
11
+ rejection?(error: unknown): CommandError | undefined;
12
+ }
13
+ /**
14
+ * Adapts a local compiled Command to the existing durable executor. The handler,
15
+ * receipt and authoritative audit share one transaction; no second store exists.
16
+ */
17
+ export declare function createTransactionalCommandGovernance<Input, Result, Transaction>(options: TransactionalCommandGovernanceOptions<Input, Result, Transaction>): {
18
+ governance: CommandGovernance;
19
+ transaction(): Transaction;
20
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/elysia",
3
- "version": "0.30.1",
3
+ "version": "0.31.1",
4
4
  "description": "Elysia runtime adapter for SupaCloud compiled modules: application/request scopes, route registration and validation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -23,7 +23,7 @@
23
23
  "scripts": {
24
24
  "generate:example": "bun src/generate-webhook-example.ts",
25
25
  "build": "bun run clean && bun run build:js && bun run build:types",
26
- "build:js": "bun build src/index.ts --outdir dist --target node --external elysia --external effect --external jose --external @supacloud/app --external @supacloud/db --external @supacloud/contracts",
26
+ "build:js": "bun build src/index.ts --outdir dist --target node --external elysia --external effect --external jose --external @supacloud/app --external @supacloud/db --external @supacloud/contracts --external @supacloud/commands",
27
27
  "build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
28
28
  "clean": "rm -rf dist",
29
29
  "prepublishOnly": "bun run build",
@@ -50,7 +50,8 @@
50
50
  },
51
51
  "dependencies": {
52
52
  "@supacloud/app": "0.27.1",
53
- "@supacloud/contracts": "0.9.0",
53
+ "@supacloud/commands": "0.9.3",
54
+ "@supacloud/contracts": "0.10.0",
54
55
  "effect": "4.0.5",
55
56
  "exact-mirror": "1.2.6",
56
57
  "jose": "^6.2.12",
@@ -58,7 +59,7 @@
58
59
  },
59
60
  "overrides": {
60
61
  "@supacloud/delivery": "0.18.1",
61
- "@supacloud/contracts": "0.9.0",
62
+ "@supacloud/contracts": "0.10.0",
62
63
  "@supacloud/commands": "0.9.3",
63
64
  "@graphql-tools/utils": "^12.0.4"
64
65
  },
@@ -66,9 +67,8 @@
66
67
  "elysia": "2.0.0-beta.32"
67
68
  },
68
69
  "devDependencies": {
69
- "@supacloud/compiler": "0.46.2",
70
+ "@supacloud/compiler": "0.46.3",
70
71
  "@supacloud/delivery": "0.18.1",
71
- "@supacloud/commands": "0.9.3",
72
72
  "@supacloud/db": "0.17.1",
73
73
  "@types/bun": "^1.4.3",
74
74
  "elysia": "2.0.0-beta.32",