@supacloud/elysia 0.34.0 → 0.35.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.
@@ -9,7 +9,7 @@ export interface CompiledCommandCallContext {
9
9
  }
10
10
  export interface CompiledCommandBindingOptions<Input, Result> {
11
11
  module: Pick<CompiledModule, "name" | "commands" | "aspects" | "aspectPipeline">;
12
- /** Compiled command class name, as required by executeCompiledCommand. */
12
+ /** Compiled command class name or logical name. */
13
13
  command: string;
14
14
  governance: CommandGovernance;
15
15
  observer?: ExecutionObserver;
package/dist/index.d.ts CHANGED
@@ -73,6 +73,8 @@ export interface CompiledRoute {
73
73
  }) => Promise<unknown> | unknown;
74
74
  /** Class name of the @Command explicitly bound to this route. */
75
75
  command?: string;
76
+ /** Transport-level idempotency contract, independent of route command binding. */
77
+ idempotency?: "required" | "none";
76
78
  /** Statically generated route aspects. */
77
79
  aspects?: ApplicationAspect[];
78
80
  aspectPipeline?: ApplicationAspectPipeline;
package/dist/index.js CHANGED
@@ -2020,6 +2020,7 @@ setupTypebox();
2020
2020
  var VERIFIED_JWT_SUBJECT_HEADER = "x-supacloud-jwt-sub";
2021
2021
  var EXECUTION_ID_HEADER = "x-sb-execution-id";
2022
2022
  var IDEMPOTENCY_KEY_HEADER = "idempotency-key";
2023
+ var idempotencyKeyPattern = /^[A-Za-z0-9._:-]{1,512}$/;
2023
2024
  function composeAspects(...aspects) {
2024
2025
  const active = aspects.filter((aspect) => typeof aspect === "function");
2025
2026
  return (context, next) => {
@@ -2173,6 +2174,10 @@ function contextPlugin(http) {
2173
2174
  function mountHttp(http, name, normalize = true) {
2174
2175
  return new Elysia3({ name, normalize }).use(contextPlugin(http));
2175
2176
  }
2177
+ function resolveCompiledCommand(module, reference) {
2178
+ const matches = (module.commands ?? []).filter((command) => command.className === reference || command.name === reference);
2179
+ return matches.length === 1 ? matches[0] : undefined;
2180
+ }
2176
2181
  function decodeJobContract(schema, value, boundary) {
2177
2182
  if (schema === undefined)
2178
2183
  return value;
@@ -2369,12 +2374,9 @@ function createCommandExecutor(governance, observer) {
2369
2374
  };
2370
2375
  }
2371
2376
  async function executeCompiledCommand(options) {
2372
- const matches = options.module.commands?.filter((item) => item.className === options.command) ?? [];
2373
- if (matches.length !== 1)
2374
- throw new ApplicationError("Command descriptor missing or ambiguous", { code: "COMMAND_NOT_REGISTERED" });
2375
- const command = matches[0];
2377
+ const command = resolveCompiledCommand(options.module, options.command);
2376
2378
  if (!command)
2377
- throw new Error(`Command not found: ${options.command}`);
2379
+ throw new ApplicationError("Command descriptor missing or ambiguous", { code: "COMMAND_NOT_REGISTERED" });
2378
2380
  const invocation = {
2379
2381
  command,
2380
2382
  input: { body: options.input, params: {}, query: {} },
@@ -2397,12 +2399,9 @@ async function executeCompiledCommand(options) {
2397
2399
  return options.decode(value);
2398
2400
  }
2399
2401
  async function previewCompiledCommand(options) {
2400
- const matches = options.module.commands?.filter((item) => item.className === options.command) ?? [];
2401
- if (matches.length !== 1)
2402
- throw new ApplicationError("Command descriptor missing or ambiguous", { code: "COMMAND_NOT_REGISTERED" });
2403
- const command = matches[0];
2402
+ const command = resolveCompiledCommand(options.module, options.command);
2404
2403
  if (!command)
2405
- throw new Error(`Command not found: ${options.command}`);
2404
+ throw new ApplicationError("Command descriptor missing or ambiguous", { code: "COMMAND_NOT_REGISTERED" });
2406
2405
  const invocation = {
2407
2406
  command,
2408
2407
  input: { body: options.input, params: {}, query: {} },
@@ -2455,8 +2454,18 @@ function requireTrustedIdentity(requestContext) {
2455
2454
  function requireIdempotencyKey(invocation) {
2456
2455
  const context = isRecord2(invocation.requestContext) ? invocation.requestContext : {};
2457
2456
  const key = (typeof context.idempotencyKey === "string" ? context.idempotencyKey : undefined) ?? safeHeaderValue(invocation.request.headers.get(IDEMPOTENCY_KEY_HEADER), 512);
2458
- if (!key) {
2459
- throw new ApplicationError("Idempotency-Key header is required", {
2457
+ if (!key || !idempotencyKeyPattern.test(key)) {
2458
+ throw new ApplicationError("A valid Idempotency-Key header is required", {
2459
+ status: 400,
2460
+ code: "IDEMPOTENCY_KEY_REQUIRED"
2461
+ });
2462
+ }
2463
+ return key;
2464
+ }
2465
+ function requireRouteIdempotencyKey(request) {
2466
+ const key = safeHeaderValue(request.headers.get(IDEMPOTENCY_KEY_HEADER), 512);
2467
+ if (!key || !idempotencyKeyPattern.test(key)) {
2468
+ throw new ApplicationError("A valid Idempotency-Key header is required", {
2460
2469
  status: 400,
2461
2470
  code: "IDEMPOTENCY_KEY_REQUIRED"
2462
2471
  });
@@ -2616,6 +2625,7 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
2616
2625
  "queryDefaults",
2617
2626
  "title",
2618
2627
  "data",
2628
+ "idempotency",
2619
2629
  "input",
2620
2630
  "result",
2621
2631
  "request",
@@ -2631,6 +2641,12 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
2631
2641
  if (route.parse !== undefined && route.parse !== "none" || route.parse === "none" && (route.body !== undefined || route.contract?.body !== "domain" || !route.contract.evidence?.trim()) || route.allowDeleteBody !== undefined && (route.allowDeleteBody !== true || route.method !== "DELETE" || route.body === undefined)) {
2632
2642
  throw new ApplicationError(`Invalid compiled body policy on ${route.method} ${controller.path}${route.path}`, { code: "ROUTE_DESCRIPTOR_UNSUPPORTED" });
2633
2643
  }
2644
+ if (route.idempotency !== undefined && route.idempotency !== "required" && route.idempotency !== "none") {
2645
+ throw new ApplicationError(`Invalid idempotency policy on ${route.method} ${controller.path}${route.path}`, { code: "ROUTE_IDEMPOTENCY_INVALID" });
2646
+ }
2647
+ if (route.idempotency === "required" && ["GET", "HEAD", "OPTIONS"].includes(route.method)) {
2648
+ throw new ApplicationError(`Route ${route.method} ${controller.path}${route.path} cannot require idempotency`, { code: "ROUTE_IDEMPOTENCY_INVALID" });
2649
+ }
2634
2650
  }
2635
2651
  }
2636
2652
  const hasCommandRoutes = compiled.controllers.some((controller) => controller.routes.some((route) => route.command !== undefined));
@@ -2638,16 +2654,15 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
2638
2654
  throw new ApplicationError(`Module "${compiled.name}" has command routes but no commandGovernance`, { code: "COMMAND_GOVERNANCE_UNCONFIGURED" });
2639
2655
  }
2640
2656
  assertCommandGovernanceReady(compiled, options.commandGovernance, options.commandExecutor);
2641
- const commandsByClassName = new Map((compiled.commands ?? []).map((command) => [command.className, command]));
2642
2657
  for (const controller of compiled.controllers) {
2643
2658
  for (const route of controller.routes) {
2644
- if (route.command && !commandsByClassName.has(route.command)) {
2659
+ const command = route.command === undefined ? undefined : resolveCompiledCommand(compiled, route.command);
2660
+ if (route.command && !command) {
2645
2661
  throw new ApplicationError(`Command "${route.command}" is not registered`, {
2646
2662
  code: "COMMAND_NOT_REGISTERED"
2647
2663
  });
2648
2664
  }
2649
2665
  if (route.effect?.retry === "explicit" && route.command) {
2650
- const command = commandsByClassName.get(route.command);
2651
2666
  if (command && command.idempotency !== "required") {
2652
2667
  throw new ApplicationError(`Effect retry is not allowed for non-idempotent command "${command.name}"`, { code: "EFFECT_RETRY_IDEMPOTENCY_REQUIRED" });
2653
2668
  }
@@ -2737,6 +2752,15 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
2737
2752
  throw new Error("HTTP policies must return Response or undefined");
2738
2753
  }
2739
2754
  }
2755
+ if (route.idempotency === "required") {
2756
+ requireRouteIdempotencyKey(context.request);
2757
+ }
2758
+ }
2759
+ });
2760
+ } else if (route.idempotency === "required") {
2761
+ Object.assign(schema, {
2762
+ beforeHandle: (context) => {
2763
+ requireRouteIdempotencyKey(context.request);
2740
2764
  }
2741
2765
  });
2742
2766
  }
@@ -2832,7 +2856,7 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
2832
2856
  services,
2833
2857
  metadata: route
2834
2858
  };
2835
- const command = route.command ? commandsByClassName.get(route.command) : undefined;
2859
+ const command = route.command === undefined ? undefined : resolveCompiledCommand(compiled, route.command);
2836
2860
  const commandContext = {
2837
2861
  kind: "command",
2838
2862
  name: command?.name ?? route.command ?? `${route.method} ${path}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/elysia",
3
- "version": "0.34.0",
3
+ "version": "0.35.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.29.0",
53
- "@supacloud/commands": "0.10.0",
54
- "@supacloud/contracts": "0.12.0",
52
+ "@supacloud/app": "0.30.0",
53
+ "@supacloud/commands": "0.11.0",
54
+ "@supacloud/contracts": "0.13.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.20.0",
62
- "@supacloud/contracts": "0.12.0",
63
- "@supacloud/commands": "0.10.0",
61
+ "@supacloud/delivery": "0.21.0",
62
+ "@supacloud/contracts": "0.13.0",
63
+ "@supacloud/commands": "0.11.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.48.0",
71
- "@supacloud/delivery": "0.20.0",
72
- "@supacloud/db": "0.19.0",
70
+ "@supacloud/compiler": "0.49.0",
71
+ "@supacloud/delivery": "0.21.0",
72
+ "@supacloud/db": "0.20.0",
73
73
  "@types/bun": "^1.4.3",
74
74
  "elysia": "2.0.0-beta.32",
75
75
  "typescript": "^7.0.2"