@supacloud/elysia 0.33.4 → 0.34.0

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/dist/index.d.ts CHANGED
@@ -79,16 +79,26 @@ export interface CompiledRoute {
79
79
  }
80
80
  export interface CompiledCommand {
81
81
  rpc?: string;
82
+ branches?: readonly CompiledCommandBranch[];
82
83
  className: string;
83
84
  name: string;
84
85
  permission?: string;
85
86
  transaction?: "required" | "none" | string;
86
87
  audit?: string;
87
88
  idempotency?: "required" | "none" | string;
89
+ tenantScope?: "required" | "none" | string;
88
90
  /** Statically generated command aspects. */
89
91
  aspects?: ApplicationAspect[];
90
92
  aspectPipeline?: ApplicationAspectPipeline;
91
93
  }
94
+ export interface CompiledCommandBranch {
95
+ when: string;
96
+ rpc: string;
97
+ transaction?: "required" | "none" | string;
98
+ audit?: string;
99
+ idempotency?: "required" | "none" | string;
100
+ boundary?: "database" | "external";
101
+ }
92
102
  export interface CompiledJob {
93
103
  className: string;
94
104
  name: string;
@@ -102,6 +112,7 @@ export interface CompiledJob {
102
112
  timeoutSec?: number;
103
113
  maxAttempts?: number;
104
114
  idempotency?: "required" | "none";
115
+ tenantScope?: "required" | "none" | string;
105
116
  aspects?: ApplicationAspect[];
106
117
  aspectPipeline?: ApplicationAspectPipeline;
107
118
  }
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  import { Elysia as Elysia3, setupTypebox, Validator as Validator2 } from "elysia";
3
+ import { requireFeatureRequestContext } from "@supacloud/app";
3
4
  import { PendingWorkRegistry as PendingWorkRegistry2, PendingWorkTimeoutError, PendingWorkCapacityError } from "@supacloud/app/runtime";
4
5
  import { commandFailureMetadata as commandFailureMetadata3, decodeCommandPreview } from "@supacloud/contracts";
5
6
 
@@ -2240,14 +2241,69 @@ function missingGovernanceAdapter(command, adapter) {
2240
2241
  code: codes[adapter]
2241
2242
  });
2242
2243
  }
2244
+ function selectCommandBranch(command, input) {
2245
+ if (!command.branches || command.branches.length === 0)
2246
+ return command;
2247
+ if (command.rpc !== undefined) {
2248
+ throw new ApplicationError(`Command "${command.name}" declares both rpc and branches`, {
2249
+ code: "COMMAND_BRANCHES_CONFLICT"
2250
+ });
2251
+ }
2252
+ for (const branch of command.branches) {
2253
+ const match = /^(?:([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*))\s*===\s*(["'])(.*)\2$/.exec(branch.when);
2254
+ if (!match) {
2255
+ throw new ApplicationError(`Command "${command.name}" has an invalid branch selector`, {
2256
+ code: "COMMAND_BRANCH_SELECTOR_INVALID"
2257
+ });
2258
+ }
2259
+ const path = match[1] ?? "";
2260
+ const parts = path.split(".");
2261
+ const root = parts[0];
2262
+ let value = root === "body" || root === "params" || root === "query" ? input[root] : input.body;
2263
+ for (const part of root === "body" || root === "params" || root === "query" ? parts.slice(1) : parts) {
2264
+ if (!isRecord2(value)) {
2265
+ value = undefined;
2266
+ break;
2267
+ }
2268
+ value = value[part];
2269
+ }
2270
+ if (value === match[3]) {
2271
+ return {
2272
+ ...command,
2273
+ rpc: branch.rpc,
2274
+ transaction: branch.transaction ?? "none",
2275
+ audit: branch.audit,
2276
+ idempotency: branch.idempotency ?? "none"
2277
+ };
2278
+ }
2279
+ }
2280
+ throw new ApplicationError(`Command "${command.name}" has no matching RPC branch`, {
2281
+ status: 422,
2282
+ code: "COMMAND_BRANCH_UNMATCHED"
2283
+ });
2284
+ }
2285
+ function validCommandBranchSelector(value) {
2286
+ return /^(?:[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*===\s*(["'])(.*)\1$/.test(value);
2287
+ }
2243
2288
  function createCommandExecutor(governance, observer) {
2244
2289
  return async (invocation, next) => {
2245
- const { command } = invocation;
2246
- for (const mode of [command.transaction, command.idempotency]) {
2290
+ const command = selectCommandBranch(invocation.command, invocation.input);
2291
+ const selectedInvocation = command === invocation.command ? invocation : { ...invocation, command };
2292
+ for (const mode of [command.transaction, command.idempotency, command.tenantScope]) {
2247
2293
  if (mode !== undefined && mode !== "required" && mode !== "none") {
2248
2294
  throw new ApplicationError("Invalid command governance mode", { code: "COMMAND_MODE_INVALID" });
2249
2295
  }
2250
2296
  }
2297
+ if (command.tenantScope === "required") {
2298
+ try {
2299
+ requireFeatureRequestContext(invocation.requestContext);
2300
+ } catch {
2301
+ throw new ApplicationError("Feature request context is required for this command", {
2302
+ status: 401,
2303
+ code: "COMMAND_TENANT_CONTEXT_REQUIRED"
2304
+ });
2305
+ }
2306
+ }
2251
2307
  if (command.rpc !== undefined) {
2252
2308
  const adapter = Object.hasOwn(governance.rpc ?? {}, command.rpc) ? governance.rpc?.[command.rpc] : undefined;
2253
2309
  if (!adapter)
@@ -2262,8 +2318,8 @@ function createCommandExecutor(governance, observer) {
2262
2318
  operation: command.name,
2263
2319
  ...executionTrace(invocation.requestContext)
2264
2320
  };
2265
- await observeExecution(observer, { ...event, stage: "authorize" }, () => governance.authorize(invocation));
2266
- return observeExecution(observer, { ...event, stage: `rpc:${command.rpc}` }, () => adapter.execute(invocation, once(next)));
2321
+ await observeExecution(observer, { ...event, stage: "authorize" }, () => governance.authorize(selectedInvocation));
2322
+ return observeExecution(observer, { ...event, stage: `rpc:${command.rpc}` }, () => adapter.execute(selectedInvocation, once(next)));
2267
2323
  }
2268
2324
  const audit = command.audit ? governance.audit : undefined;
2269
2325
  const transaction = command.transaction === "required" ? governance.transaction : undefined;
@@ -2284,11 +2340,11 @@ function createCommandExecutor(governance, observer) {
2284
2340
  stage,
2285
2341
  ...executionTrace(invocation.requestContext)
2286
2342
  }, next);
2287
- await observe("authorize", () => governance.authorize(invocation));
2343
+ await observe("authorize", () => governance.authorize(selectedInvocation));
2288
2344
  let execute = async () => {
2289
2345
  const result = await observe("handler", invokeOnce);
2290
2346
  if (audit)
2291
- await observe("audit", () => audit.succeeded.call(audit, invocation, result));
2347
+ await observe("audit", () => audit.succeeded.call(audit, selectedInvocation, result));
2292
2348
  return result;
2293
2349
  };
2294
2350
  const invokeOnce = once(next);
@@ -2296,18 +2352,18 @@ function createCommandExecutor(governance, observer) {
2296
2352
  if (!transaction)
2297
2353
  throw missingGovernanceAdapter(command, "transaction");
2298
2354
  const inner = execute;
2299
- execute = () => observe("transaction", () => transaction.call(governance, invocation, once(inner)));
2355
+ execute = () => observe("transaction", () => transaction.call(governance, selectedInvocation, once(inner)));
2300
2356
  }
2301
2357
  if (command.idempotency === "required") {
2302
2358
  if (!idempotency)
2303
2359
  throw missingGovernanceAdapter(command, "idempotency");
2304
2360
  const inner = execute;
2305
- execute = () => observe("idempotency", () => idempotency.call(governance, invocation, once(inner)));
2361
+ execute = () => observe("idempotency", () => idempotency.call(governance, selectedInvocation, once(inner)));
2306
2362
  }
2307
2363
  return await execute();
2308
2364
  } catch (error) {
2309
2365
  if (audit)
2310
- await audit.failed.call(audit, invocation, error);
2366
+ await audit.failed.call(audit, selectedInvocation, error);
2311
2367
  throw error;
2312
2368
  }
2313
2369
  };
@@ -2428,6 +2484,44 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
2428
2484
  });
2429
2485
  }
2430
2486
  for (const command of compiled.commands ?? []) {
2487
+ if (command.rpc !== undefined && command.branches !== undefined) {
2488
+ throw new ApplicationError(`Command "${command.name}" declares both rpc and branches`, {
2489
+ code: "COMMAND_BRANCHES_CONFLICT"
2490
+ });
2491
+ }
2492
+ for (const branch of command.branches ?? []) {
2493
+ if (!validCommandBranchSelector(branch.when)) {
2494
+ throw new ApplicationError(`Command "${command.name}" has an invalid branch selector`, {
2495
+ code: "COMMAND_BRANCH_SELECTOR_INVALID"
2496
+ });
2497
+ }
2498
+ const adapter = Object.hasOwn(governance.rpc ?? {}, branch.rpc) ? governance.rpc?.[branch.rpc] : undefined;
2499
+ if (!adapter || typeof adapter.execute !== "function" || !isRecord2(adapter.capabilities)) {
2500
+ throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" has no RPC governance adapter`, {
2501
+ code: "COMMAND_RPC_UNCONFIGURED"
2502
+ });
2503
+ }
2504
+ if (branch.audit && adapter.capabilities.audit !== true) {
2505
+ throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" has no audit capability`, {
2506
+ code: "COMMAND_AUDIT_UNCONFIGURED"
2507
+ });
2508
+ }
2509
+ if (branch.idempotency === "required" && adapter.capabilities.idempotency !== true) {
2510
+ throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" has no idempotency capability`, {
2511
+ code: "COMMAND_IDEMPOTENCY_UNCONFIGURED"
2512
+ });
2513
+ }
2514
+ if (branch.transaction === "required" && adapter.capabilities.transaction !== true) {
2515
+ throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" has no transaction capability`, {
2516
+ code: "COMMAND_TRANSACTION_UNCONFIGURED"
2517
+ });
2518
+ }
2519
+ if (branch.boundary === "external" && (branch.transaction === "required" || adapter.capabilities.transaction === true)) {
2520
+ throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" claims an external transaction`, {
2521
+ code: "COMMAND_EXTERNAL_TRANSACTION"
2522
+ });
2523
+ }
2524
+ }
2431
2525
  if (command.audit && command.rpc === undefined && (typeof governance.audit?.succeeded !== "function" || typeof governance.audit?.failed !== "function")) {
2432
2526
  throw new ApplicationError(`Command "${command.name}" has no audit adapter`, {
2433
2527
  code: "COMMAND_AUDIT_UNCONFIGURED"
@@ -2816,6 +2910,16 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
2816
2910
  return plugin;
2817
2911
  }
2818
2912
  async function executeJob(compiled, services, job, input, requestContext, imported = {}, executor, observer) {
2913
+ if (job.tenantScope === "required") {
2914
+ try {
2915
+ requireFeatureRequestContext(requestContext);
2916
+ } catch {
2917
+ throw new ApplicationError("Feature request context is required for this job", {
2918
+ status: 401,
2919
+ code: "JOB_TENANT_CONTEXT_REQUIRED"
2920
+ });
2921
+ }
2922
+ }
2819
2923
  const decodedInput = decodeJobContract(job.input, input, "input");
2820
2924
  const jobScope = job.scope === "job" && compiled.createJobScope ? await compiled.createJobScope(services, requestContext, imported) : undefined;
2821
2925
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/elysia",
3
- "version": "0.33.4",
3
+ "version": "0.34.0",
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",
@@ -49,27 +49,27 @@
49
49
  "directory": "packages/elysia"
50
50
  },
51
51
  "dependencies": {
52
- "@supacloud/app": "0.28.2",
53
- "@supacloud/commands": "0.9.6",
54
- "@supacloud/contracts": "0.11.4",
52
+ "@supacloud/app": "0.29.0",
53
+ "@supacloud/commands": "0.10.0",
54
+ "@supacloud/contracts": "0.12.0",
55
55
  "effect": "4.0.5",
56
56
  "exact-mirror": "1.2.6",
57
57
  "jose": "^6.2.12",
58
58
  "typebox": "1.3.37"
59
59
  },
60
60
  "overrides": {
61
- "@supacloud/delivery": "0.19.3",
62
- "@supacloud/contracts": "0.11.4",
63
- "@supacloud/commands": "0.9.6",
61
+ "@supacloud/delivery": "0.20.0",
62
+ "@supacloud/contracts": "0.12.0",
63
+ "@supacloud/commands": "0.10.0",
64
64
  "@graphql-tools/utils": "^12.0.4"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "elysia": "2.0.0-beta.32"
68
68
  },
69
69
  "devDependencies": {
70
- "@supacloud/compiler": "0.47.2",
71
- "@supacloud/delivery": "0.19.3",
72
- "@supacloud/db": "0.18.3",
70
+ "@supacloud/compiler": "0.48.0",
71
+ "@supacloud/delivery": "0.20.0",
72
+ "@supacloud/db": "0.19.0",
73
73
  "@types/bun": "^1.4.3",
74
74
  "elysia": "2.0.0-beta.32",
75
75
  "typescript": "^7.0.2"