@supacloud/elysia 0.38.2 → 0.40.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/README.md +40 -0
- package/compatibility.json +2 -3
- package/dist/bun-aot.js +226 -217
- package/dist/fixtures/webhook-generated/application.d.ts +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +226 -217
- package/package.json +14 -12
package/README.md
CHANGED
|
@@ -8,6 +8,46 @@ is a separately authorized business command, never an assumed rollback.
|
|
|
8
8
|
|
|
9
9
|
## Compatibility and Acceptance Boundary
|
|
10
10
|
|
|
11
|
+
### Static route registration migration
|
|
12
|
+
|
|
13
|
+
HTTP modules now require compiler-emitted `registerRoutes(register)` calls.
|
|
14
|
+
The adapter has no descriptor-array registration fallback. A module with routes
|
|
15
|
+
and a missing or non-callable plan fails at startup with
|
|
16
|
+
`COMPILED_ROUTE_REGISTRATION_REQUIRED`. Modules containing only providers,
|
|
17
|
+
commands or jobs may omit this HTTP-only plan.
|
|
18
|
+
|
|
19
|
+
Upgrade `@supacloud/compiler` and `@supacloud/elysia` together using a matched
|
|
20
|
+
release, then regenerate application artifacts with the installed compiler:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
bun run supacloud-compiler compile
|
|
24
|
+
bun run supacloud-compiler check --json
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Commit the regenerated `application.ts` and manifests together with the lockfile,
|
|
28
|
+
rebuild the application bundle, and exercise its affected HTTP entry points.
|
|
29
|
+
Do not patch generated files by hand or deploy a new adapter with an old bundle.
|
|
30
|
+
For rollback, restore the compiler, adapter, lockfile and compiled bundle as one
|
|
31
|
+
tested release. The package version numbers need not be identical.
|
|
32
|
+
|
|
33
|
+
Hand-authored test fixtures and custom integrations must register each route
|
|
34
|
+
explicitly, with its full path. Preserve descriptor identities and order:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
registerRoutes(register) {
|
|
38
|
+
register(this.controllers[0]!, this.controllers[0]!.routes[0]!, '/orders')
|
|
39
|
+
register(this.controllers[0]!, this.controllers[0]!.routes[1]!, '/orders/:id')
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The callback runs during plugin construction. The compiler emits full paths and
|
|
44
|
+
individual registrations; schema validation, HTTP policies, command governance,
|
|
45
|
+
scope cleanup and error handling stay in the adapter. Startup validation still
|
|
46
|
+
inspects descriptors. This is not a claim of zero overhead or a measured speedup.
|
|
47
|
+
The manifest describes static declarations; host options can add lifecycle hooks
|
|
48
|
+
or select the request pipeline. Native Elysia macros supplied by the host are
|
|
49
|
+
not inferred from application declarations.
|
|
50
|
+
|
|
11
51
|
The Elysia peer and development dependency are pinned to `2.0.0-rc.1`.
|
|
12
52
|
The runtime adapter pins Effect `4.0.5` and consumes its `Exit`-based
|
|
13
53
|
execution boundary.
|
package/compatibility.json
CHANGED
package/dist/bun-aot.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { Elysia as Elysia3, setupTypebox, Validator as Validator2 } from "elysia";
|
|
3
3
|
import { requireFeatureRequestContext } from "@supacloud/app";
|
|
4
|
+
import { composeAspects as composeApplicationAspects, invokeAspect, freezeAspectData } from "@supacloud/app/execution";
|
|
4
5
|
import { PendingWorkRegistry as PendingWorkRegistry2, PendingWorkTimeoutError, PendingWorkCapacityError } from "@supacloud/app/runtime";
|
|
5
6
|
import { commandFailureMetadata as commandFailureMetadata3, decodeCommandPreview } from "@supacloud/contracts";
|
|
6
7
|
|
|
@@ -2054,22 +2055,7 @@ var IDEMPOTENCY_KEY_HEADER = "idempotency-key";
|
|
|
2054
2055
|
var idempotencyKeyPattern = /^[A-Za-z0-9._:-]{1,512}$/;
|
|
2055
2056
|
function composeAspects(...aspects) {
|
|
2056
2057
|
const active = aspects.filter((aspect) => typeof aspect === "function");
|
|
2057
|
-
|
|
2058
|
-
return (_context, next) => next();
|
|
2059
|
-
return (context, next) => {
|
|
2060
|
-
let index = -1;
|
|
2061
|
-
const dispatch = (current) => {
|
|
2062
|
-
if (current <= index) {
|
|
2063
|
-
return Promise.reject(new Error("next() called multiple times"));
|
|
2064
|
-
}
|
|
2065
|
-
index = current;
|
|
2066
|
-
const aspect = active[current];
|
|
2067
|
-
if (!aspect)
|
|
2068
|
-
return Promise.resolve(next());
|
|
2069
|
-
return Promise.resolve(aspect(context, () => dispatch(current + 1)));
|
|
2070
|
-
};
|
|
2071
|
-
return dispatch(0);
|
|
2072
|
-
};
|
|
2058
|
+
return composeApplicationAspects(...active);
|
|
2073
2059
|
}
|
|
2074
2060
|
function observedAspects(aspects, boundary, observer) {
|
|
2075
2061
|
return composeAspects(...aspects.map((aspect, index) => (context, next) => observeExecution(observer, {
|
|
@@ -2678,8 +2664,15 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2678
2664
|
"parse",
|
|
2679
2665
|
"allowDeleteBody"
|
|
2680
2666
|
]);
|
|
2667
|
+
const pendingRoutes = new Map;
|
|
2681
2668
|
for (const controller of compiled.controllers) {
|
|
2682
2669
|
for (const route of controller.routes) {
|
|
2670
|
+
if (pendingRoutes.has(route)) {
|
|
2671
|
+
throw new ApplicationError(`Duplicate route descriptor in module "${compiled.name}". Recompile the application.`, {
|
|
2672
|
+
code: "COMPILED_ROUTE_REGISTRATION_INVALID"
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
pendingRoutes.set(route, { controller, path: joinPaths(controller.path, route.path) });
|
|
2683
2676
|
const unsupported = Object.keys(route).filter((field) => !supportedFields.has(field));
|
|
2684
2677
|
if (!supportedMethods.has(route.method) || unsupported.length > 0) {
|
|
2685
2678
|
throw new ApplicationError(`Unsupported compiled route ${route.method} ${controller.path}${route.path}` + (unsupported.length > 0 ? `: ${unsupported.join(", ")}` : ""), { code: "ROUTE_DESCRIPTOR_UNSUPPORTED" });
|
|
@@ -2695,6 +2688,9 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2695
2688
|
}
|
|
2696
2689
|
}
|
|
2697
2690
|
}
|
|
2691
|
+
if ((compiled.controllers.some((controller) => controller.routes.length > 0) || compiled.registerRoutes !== undefined) && typeof compiled.registerRoutes !== "function") {
|
|
2692
|
+
throw new ApplicationError(`Module "${compiled.name}" requires a compiler-emitted registerRoutes plan. Upgrade @supacloud/compiler with @supacloud/elysia and recompile the application.`, { code: "COMPILED_ROUTE_REGISTRATION_REQUIRED" });
|
|
2693
|
+
}
|
|
2698
2694
|
const hasCommandRoutes = compiled.controllers.some((controller) => controller.routes.some((route) => route.command !== undefined));
|
|
2699
2695
|
if (hasCommandRoutes && !options.commandGovernance && !options.commandExecutor) {
|
|
2700
2696
|
throw new ApplicationError(`Module "${compiled.name}" has command routes but no commandGovernance`, { code: "COMMAND_GOVERNANCE_UNCONFIGURED" });
|
|
@@ -2778,225 +2774,238 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2778
2774
|
plugin.onError(SchemaContractError, ({ error, request }) => mapError(error, frameworkErrorCode(error), request));
|
|
2779
2775
|
plugin.onError(({ error, request }) => mapError(error, frameworkErrorCode(error), request));
|
|
2780
2776
|
const routeRegistrar = plugin;
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
if (
|
|
2812
|
-
|
|
2777
|
+
const registerRoute = (controller, route, path) => {
|
|
2778
|
+
if (pendingRoutes.get(route)?.controller !== controller || pendingRoutes.get(route)?.path !== path) {
|
|
2779
|
+
throw new ApplicationError(`Invalid static route registration in module "${compiled.name}". Recompile the application.`, {
|
|
2780
|
+
code: "COMPILED_ROUTE_REGISTRATION_INVALID"
|
|
2781
|
+
});
|
|
2782
|
+
}
|
|
2783
|
+
pendingRoutes.delete(route);
|
|
2784
|
+
const documentation = route.data?.openapi;
|
|
2785
|
+
const hidden = documentation !== null && typeof documentation === "object" && "hide" in documentation && documentation.hide === true;
|
|
2786
|
+
const tags = documentation !== null && typeof documentation === "object" && "tags" in documentation && Array.isArray(documentation.tags) ? documentation.tags.filter((tag) => typeof tag === "string") : undefined;
|
|
2787
|
+
const schema = {
|
|
2788
|
+
...toElysiaRouteSchema(route),
|
|
2789
|
+
...route.parse === "none" ? { parse: "none" } : {},
|
|
2790
|
+
...route.title || hidden || tags ? { detail: {
|
|
2791
|
+
...route.title ? { summary: route.title } : {},
|
|
2792
|
+
...hidden ? { hide: true } : {},
|
|
2793
|
+
...tags ? { tags } : {}
|
|
2794
|
+
} } : {}
|
|
2795
|
+
};
|
|
2796
|
+
const policies = compileHttpPolicies(route, path, options.httpPolicies);
|
|
2797
|
+
if (policies.length > 0) {
|
|
2798
|
+
Object.assign(schema, {
|
|
2799
|
+
beforeHandle: async (context) => {
|
|
2800
|
+
for (const policy of policies) {
|
|
2801
|
+
const result = await policy({
|
|
2802
|
+
http: context,
|
|
2803
|
+
requestContext: requestContexts.get(context.request)
|
|
2804
|
+
});
|
|
2805
|
+
if (result instanceof Response)
|
|
2806
|
+
return result;
|
|
2807
|
+
if (result !== undefined) {
|
|
2808
|
+
throw new Error("HTTP policies must return Response or undefined");
|
|
2813
2809
|
}
|
|
2814
2810
|
}
|
|
2815
|
-
|
|
2816
|
-
} else if (route.idempotency === "required") {
|
|
2817
|
-
Object.assign(schema, {
|
|
2818
|
-
beforeHandle: (context) => {
|
|
2811
|
+
if (route.idempotency === "required") {
|
|
2819
2812
|
requireRouteIdempotencyKey(context.request);
|
|
2820
2813
|
}
|
|
2821
|
-
}
|
|
2822
|
-
}
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2814
|
+
}
|
|
2815
|
+
});
|
|
2816
|
+
} else if (route.idempotency === "required") {
|
|
2817
|
+
Object.assign(schema, {
|
|
2818
|
+
beforeHandle: (context) => {
|
|
2819
|
+
requireRouteIdempotencyKey(context.request);
|
|
2820
|
+
}
|
|
2821
|
+
});
|
|
2822
|
+
}
|
|
2823
|
+
const responsePolicies = policies.filter((policy) => policy.afterResponse);
|
|
2824
|
+
const mappingPolicies = policies.filter((policy) => policy.mapResponse);
|
|
2825
|
+
const routePipeline = descriptorPipeline(route, "route", options.onExecution);
|
|
2826
|
+
const command = route.command ? commandsByClassName.get(route.command) : undefined;
|
|
2827
|
+
const commandPipeline = route.command ? descriptorPipeline(command ?? {}, "command", options.onExecution) : undefined;
|
|
2828
|
+
const routeHasAspects = compiled.aspectPipeline !== undefined || (compiled.aspects?.length ?? 0) > 0 || route.aspectPipeline !== undefined || (route.aspects?.length ?? 0) > 0 || command?.aspectPipeline !== undefined || (command?.aspects?.length ?? 0) > 0;
|
|
2829
|
+
const routeInvoker = route.invoker;
|
|
2830
|
+
if (mappingPolicies.length > 0) {
|
|
2831
|
+
const responseValidators = Validator2.response(schema.response, { normalize: options.normalize ?? true });
|
|
2832
|
+
Object.assign(schema, {
|
|
2833
|
+
mapResponse: async (context) => {
|
|
2834
|
+
let response = context.responseValue;
|
|
2835
|
+
let mapped;
|
|
2836
|
+
for (const policy of mappingPolicies) {
|
|
2837
|
+
const result = await policy.mapResponse({
|
|
2838
|
+
http: context,
|
|
2839
|
+
requestContext: requestContexts.get(context.request),
|
|
2840
|
+
response,
|
|
2841
|
+
encodeResponse: (value) => {
|
|
2842
|
+
const validator = responseValidators?.get(responseStatusOf(value, context.set.status));
|
|
2843
|
+
if (!validator)
|
|
2844
|
+
return value;
|
|
2845
|
+
if (!("EncodeFrom" in validator) || typeof validator.EncodeFrom !== "function") {
|
|
2846
|
+
throw new ApplicationError("Response validation failed", { code: "RESPONSE_VALIDATION_FAILED" });
|
|
2847
|
+
}
|
|
2848
|
+
if ("mayReturnPromise" in validator && validator.mayReturnPromise && "From" in validator && typeof validator.From === "function") {
|
|
2849
|
+
return validator.From.call(validator, value, "response", true);
|
|
2851
2850
|
}
|
|
2852
|
-
|
|
2853
|
-
if (result !== undefined) {
|
|
2854
|
-
if (!(result instanceof Response))
|
|
2855
|
-
throw new Error("HTTP response policies must return Response or undefined");
|
|
2856
|
-
mapped = result;
|
|
2857
|
-
response = result;
|
|
2851
|
+
return validator.EncodeFrom.call(validator, value, "response");
|
|
2858
2852
|
}
|
|
2853
|
+
});
|
|
2854
|
+
if (result !== undefined) {
|
|
2855
|
+
if (!(result instanceof Response))
|
|
2856
|
+
throw new Error("HTTP response policies must return Response or undefined");
|
|
2857
|
+
mapped = result;
|
|
2858
|
+
response = result;
|
|
2859
2859
|
}
|
|
2860
|
-
return mapped;
|
|
2861
2860
|
}
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2861
|
+
return mapped;
|
|
2862
|
+
}
|
|
2863
|
+
});
|
|
2864
|
+
}
|
|
2865
|
+
if (responsePolicies.length > 0) {
|
|
2866
|
+
Object.assign(schema, {
|
|
2867
|
+
afterResponse: async (context) => {
|
|
2868
|
+
for (const policy of responsePolicies) {
|
|
2869
|
+
await policy.afterResponse({
|
|
2870
|
+
http: context,
|
|
2871
|
+
requestContext: requestContexts.get(context.request),
|
|
2872
|
+
response: context.responseValue
|
|
2873
|
+
});
|
|
2874
2874
|
}
|
|
2875
|
-
}
|
|
2876
|
-
}
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2875
|
+
}
|
|
2876
|
+
});
|
|
2877
|
+
}
|
|
2878
|
+
const directRoute = canUseDirectRoute(compiled, controller, route) && options.onExecution === undefined && policies.length === 0 && responsePolicies.length === 0 && mappingPolicies.length === 0;
|
|
2879
|
+
const directController = directRoute && route.directInvokerFast !== undefined ? controllerInstance(services[controller.serviceKey]) : undefined;
|
|
2880
|
+
const directMethod = directController?.[route.handler];
|
|
2881
|
+
const directInvokerFast = route.directInvokerFast;
|
|
2882
|
+
const directHandler = directController && typeof directMethod === "function" && directInvokerFast ? (ctx) => directInvokerFast(directController, directMethod, ctx) : (ctx) => route.directInvoker(services[controller.serviceKey], ctx);
|
|
2883
|
+
const handler = directRoute ? directHandler : async (ctx) => {
|
|
2884
|
+
const requestContext = requestContexts.get(ctx.request);
|
|
2885
|
+
const execute = async () => {
|
|
2886
|
+
const requestScope = createRequestScope ? await createRequestScope(services, requestContext, imported) : undefined;
|
|
2887
|
+
if (requestScope && compiled.destroyRequestScope)
|
|
2888
|
+
requestScopes.set(ctx.request, requestScope);
|
|
2889
|
+
const source = controller.scope === "request" ? requestScope : services;
|
|
2890
|
+
const instance = source?.[controller.serviceKey];
|
|
2891
|
+
const normalizedCookie = ctx.cookie === undefined ? undefined : cookieValues(ctx.cookie);
|
|
2892
|
+
const input = {
|
|
2893
|
+
body: ctx.body,
|
|
2894
|
+
params: ctx.params,
|
|
2895
|
+
query: ctx.query,
|
|
2896
|
+
headers: ctx.headers ?? Object.fromEntries(ctx.request.headers.entries()),
|
|
2897
|
+
...normalizedCookie === undefined ? {} : { cookie: normalizedCookie },
|
|
2898
|
+
context: requestContext,
|
|
2899
|
+
request: ctx.request,
|
|
2900
|
+
...requestScope ? { scope: requestScope } : {},
|
|
2901
|
+
requestContext
|
|
2902
|
+
};
|
|
2903
|
+
const handlerCall = () => {
|
|
2904
|
+
if (route.invoker)
|
|
2905
|
+
return route.invoker(instance, input);
|
|
2906
|
+
if (!isRecord2(instance))
|
|
2907
|
+
throw new TypeError("Route controller is not an object");
|
|
2908
|
+
const method = instance[route.handler];
|
|
2909
|
+
if (typeof method !== "function")
|
|
2910
|
+
throw new TypeError(`Route handler ${route.handler} is not callable`);
|
|
2911
|
+
return method.call(instance, input);
|
|
2912
|
+
};
|
|
2913
|
+
const invokeHandler = async () => runCompiledEffect(await handlerCall(), route.effect, options.effectRuntime);
|
|
2914
|
+
const invoke = once(() => route.command && options.commandGovernance ? invokeHandler() : options.onExecution ? observeExecution(options.onExecution, {
|
|
2915
|
+
kind: route.command ? "command" : "route",
|
|
2916
|
+
operation: route.command ?? `${route.method} ${path}`,
|
|
2917
|
+
stage: "handler",
|
|
2918
|
+
...executionTrace(requestContext)
|
|
2919
|
+
}, invokeHandler) : invokeHandler());
|
|
2920
|
+
const routeContext = {
|
|
2921
|
+
kind: "route",
|
|
2922
|
+
name: `${route.method} ${path}`,
|
|
2923
|
+
input,
|
|
2924
|
+
request: ctx.request,
|
|
2925
|
+
requestContext,
|
|
2926
|
+
...requestScope ? { scope: requestScope } : {},
|
|
2927
|
+
services,
|
|
2928
|
+
metadata: route
|
|
2929
|
+
};
|
|
2930
|
+
const command = route.command === undefined ? undefined : resolveCompiledCommand(compiled, route.command);
|
|
2931
|
+
const commandContext = route.command === undefined ? routeContext : {
|
|
2932
|
+
kind: "command",
|
|
2933
|
+
name: command?.name ?? route.command,
|
|
2934
|
+
input,
|
|
2935
|
+
request: ctx.request,
|
|
2936
|
+
requestContext,
|
|
2937
|
+
...requestScope ? { scope: requestScope } : {},
|
|
2938
|
+
services,
|
|
2939
|
+
metadata: command ?? route
|
|
2940
|
+
};
|
|
2941
|
+
const invokeRoute = () => route.command && routeHasAspects ? invokeAspect(commandContext, (_context, next) => modulePipeline(commandContext, () => routePipeline(routeContext, () => commandPipeline(commandContext, next))), invoke, (result) => {
|
|
2942
|
+
if (!(result instanceof Response))
|
|
2943
|
+
freezeAspectData(result);
|
|
2944
|
+
}) : modulePipeline(route.command ? commandContext : routeContext, once(() => routePipeline(routeContext, once(() => route.command ? commandPipeline(commandContext, once(invoke)) : invoke()))));
|
|
2945
|
+
const invokeCommand = () => {
|
|
2946
|
+
if (!route.command)
|
|
2947
|
+
return invokeRoute();
|
|
2948
|
+
if (!command) {
|
|
2949
|
+
throw new ApplicationError(`Command "${route.command}" is not registered`, {
|
|
2950
|
+
code: "COMMAND_NOT_REGISTERED"
|
|
2951
|
+
});
|
|
2952
|
+
}
|
|
2953
|
+
if (!commandExecutor) {
|
|
2954
|
+
throw new ApplicationError(`Command "${command.name}" has no executor`, {
|
|
2955
|
+
status: 501,
|
|
2956
|
+
code: "COMMAND_EXECUTOR_UNAVAILABLE"
|
|
2957
|
+
});
|
|
2958
|
+
}
|
|
2959
|
+
const invocation = {
|
|
2960
|
+
command,
|
|
2922
2961
|
input,
|
|
2923
2962
|
request: ctx.request,
|
|
2924
2963
|
requestContext,
|
|
2925
2964
|
...requestScope ? { scope: requestScope } : {},
|
|
2926
|
-
services
|
|
2927
|
-
metadata: route
|
|
2965
|
+
services
|
|
2928
2966
|
};
|
|
2929
|
-
|
|
2930
|
-
const commandContext = route.command === undefined ? routeContext : {
|
|
2967
|
+
return observeExecution(options.onExecution, {
|
|
2931
2968
|
kind: "command",
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
...requestScope ? { scope: requestScope } : {},
|
|
2937
|
-
services,
|
|
2938
|
-
metadata: command ?? route
|
|
2939
|
-
};
|
|
2940
|
-
const invokeRoute = () => modulePipeline(route.command ? commandContext : routeContext, once(() => routePipeline(routeContext, once(() => route.command ? commandPipeline(commandContext, once(invoke)) : invoke()))));
|
|
2941
|
-
const invokeCommand = () => {
|
|
2942
|
-
if (!route.command)
|
|
2943
|
-
return invokeRoute();
|
|
2944
|
-
if (!command) {
|
|
2945
|
-
throw new ApplicationError(`Command "${route.command}" is not registered`, {
|
|
2946
|
-
code: "COMMAND_NOT_REGISTERED"
|
|
2947
|
-
});
|
|
2948
|
-
}
|
|
2949
|
-
if (!commandExecutor) {
|
|
2950
|
-
throw new ApplicationError(`Command "${command.name}" has no executor`, {
|
|
2951
|
-
status: 501,
|
|
2952
|
-
code: "COMMAND_EXECUTOR_UNAVAILABLE"
|
|
2953
|
-
});
|
|
2954
|
-
}
|
|
2955
|
-
const invocation = {
|
|
2956
|
-
command,
|
|
2957
|
-
input,
|
|
2958
|
-
request: ctx.request,
|
|
2959
|
-
requestContext,
|
|
2960
|
-
...requestScope ? { scope: requestScope } : {},
|
|
2961
|
-
services
|
|
2962
|
-
};
|
|
2963
|
-
return observeExecution(options.onExecution, {
|
|
2964
|
-
kind: "command",
|
|
2965
|
-
operation: command.name,
|
|
2966
|
-
stage: "commandExecutor",
|
|
2967
|
-
...executionTrace(requestContext)
|
|
2968
|
-
}, () => commandExecutor(invocation, once(invokeRoute)));
|
|
2969
|
-
};
|
|
2970
|
-
return invokeCommand();
|
|
2969
|
+
operation: command.name,
|
|
2970
|
+
stage: "commandExecutor",
|
|
2971
|
+
...executionTrace(requestContext)
|
|
2972
|
+
}, () => commandExecutor(invocation, once(invokeRoute)));
|
|
2971
2973
|
};
|
|
2972
|
-
|
|
2973
|
-
assertDeclaredResponseStatus(route, result, ctx.set.status);
|
|
2974
|
-
return result;
|
|
2974
|
+
return invokeCommand();
|
|
2975
2975
|
};
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2976
|
+
const result = await execute();
|
|
2977
|
+
assertDeclaredResponseStatus(route, result, ctx.set.status);
|
|
2978
|
+
return result;
|
|
2979
|
+
};
|
|
2980
|
+
switch (route.method) {
|
|
2981
|
+
case "GET":
|
|
2982
|
+
routeRegistrar.get(path, schema, handler);
|
|
2983
|
+
break;
|
|
2984
|
+
case "POST":
|
|
2985
|
+
routeRegistrar.post(path, schema, handler);
|
|
2986
|
+
break;
|
|
2987
|
+
case "PUT":
|
|
2988
|
+
routeRegistrar.put(path, schema, handler);
|
|
2989
|
+
break;
|
|
2990
|
+
case "PATCH":
|
|
2991
|
+
routeRegistrar.patch(path, schema, handler);
|
|
2992
|
+
break;
|
|
2993
|
+
case "DELETE":
|
|
2994
|
+
routeRegistrar.delete(path, schema, handler);
|
|
2995
|
+
break;
|
|
2996
|
+
case "HEAD":
|
|
2997
|
+
routeRegistrar.head(path, schema, handler);
|
|
2998
|
+
break;
|
|
2999
|
+
case "OPTIONS":
|
|
3000
|
+
routeRegistrar.options(path, schema, handler);
|
|
3001
|
+
break;
|
|
2999
3002
|
}
|
|
3003
|
+
};
|
|
3004
|
+
compiled.registerRoutes?.(registerRoute);
|
|
3005
|
+
if (pendingRoutes.size > 0) {
|
|
3006
|
+
throw new ApplicationError(`Incomplete static route registration in module "${compiled.name}". Recompile the application.`, {
|
|
3007
|
+
code: "COMPILED_ROUTE_REGISTRATION_INVALID"
|
|
3008
|
+
});
|
|
3000
3009
|
}
|
|
3001
3010
|
return plugin;
|
|
3002
3011
|
}
|
|
@@ -61,6 +61,7 @@ export interface CompiledRoute {
|
|
|
61
61
|
context?: unknown;
|
|
62
62
|
}) => Promise<unknown> | unknown;
|
|
63
63
|
directInvoker?: (controller: unknown, context: unknown) => Promise<unknown> | unknown;
|
|
64
|
+
directInvokerFast?: (controller: Record<string, unknown>, handler: (...args: unknown[]) => unknown, context: unknown) => Promise<unknown> | unknown;
|
|
64
65
|
executionMode?: "direct" | "pipeline";
|
|
65
66
|
}
|
|
66
67
|
export interface CompiledCommand {
|
|
@@ -129,6 +130,7 @@ export interface CompiledModule {
|
|
|
129
130
|
createJobScope?(services: Record<string, unknown>, ctx: unknown, imported?: Record<string, Record<string, unknown>>): Promise<Record<string, unknown>>;
|
|
130
131
|
destroyJobScope?(scope: Record<string, unknown>): Promise<void>;
|
|
131
132
|
controllers: CompiledController[];
|
|
133
|
+
registerRoutes?(register: (controller: CompiledController, route: CompiledRoute, path: string) => void): void;
|
|
132
134
|
commands: CompiledCommand[];
|
|
133
135
|
jobs: CompiledJob[];
|
|
134
136
|
aspects?: CompiledAspect[];
|
package/dist/index.d.ts
CHANGED
|
@@ -139,6 +139,7 @@ export interface CompiledController {
|
|
|
139
139
|
scope: "application" | "request" | "job";
|
|
140
140
|
routes: CompiledRoute[];
|
|
141
141
|
}
|
|
142
|
+
export type CompiledRouteRegistrar = (controller: CompiledController, route: CompiledRoute, path: string) => void;
|
|
142
143
|
export interface CompiledModule {
|
|
143
144
|
name: string;
|
|
144
145
|
createServices(deps: Record<string, unknown>, imported: Record<string, Record<string, unknown>>): Record<string, unknown>;
|
|
@@ -151,6 +152,8 @@ export interface CompiledModule {
|
|
|
151
152
|
createJobScope?(services: Record<string, unknown>, ctx: unknown, imported?: Record<string, Record<string, unknown>>): Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
152
153
|
destroyJobScope?(scope: Record<string, unknown>): Promise<void>;
|
|
153
154
|
controllers: CompiledController[];
|
|
155
|
+
/** Required for HTTP modules. Recompile artifacts when upgrading the adapter. */
|
|
156
|
+
registerRoutes?(register: CompiledRouteRegistrar): void;
|
|
154
157
|
commands?: CompiledCommand[];
|
|
155
158
|
jobs?: CompiledJob[];
|
|
156
159
|
/** Statically generated module aspects. */
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { Elysia as Elysia3, setupTypebox, Validator as Validator2 } from "elysia";
|
|
3
3
|
import { requireFeatureRequestContext } from "@supacloud/app";
|
|
4
|
+
import { composeAspects as composeApplicationAspects, invokeAspect, freezeAspectData } from "@supacloud/app/execution";
|
|
4
5
|
import { PendingWorkRegistry as PendingWorkRegistry2, PendingWorkTimeoutError, PendingWorkCapacityError } from "@supacloud/app/runtime";
|
|
5
6
|
import { commandFailureMetadata as commandFailureMetadata3, decodeCommandPreview } from "@supacloud/contracts";
|
|
6
7
|
|
|
@@ -2054,22 +2055,7 @@ var IDEMPOTENCY_KEY_HEADER = "idempotency-key";
|
|
|
2054
2055
|
var idempotencyKeyPattern = /^[A-Za-z0-9._:-]{1,512}$/;
|
|
2055
2056
|
function composeAspects(...aspects) {
|
|
2056
2057
|
const active = aspects.filter((aspect) => typeof aspect === "function");
|
|
2057
|
-
|
|
2058
|
-
return (_context, next) => next();
|
|
2059
|
-
return (context, next) => {
|
|
2060
|
-
let index = -1;
|
|
2061
|
-
const dispatch = (current) => {
|
|
2062
|
-
if (current <= index) {
|
|
2063
|
-
return Promise.reject(new Error("next() called multiple times"));
|
|
2064
|
-
}
|
|
2065
|
-
index = current;
|
|
2066
|
-
const aspect = active[current];
|
|
2067
|
-
if (!aspect)
|
|
2068
|
-
return Promise.resolve(next());
|
|
2069
|
-
return Promise.resolve(aspect(context, () => dispatch(current + 1)));
|
|
2070
|
-
};
|
|
2071
|
-
return dispatch(0);
|
|
2072
|
-
};
|
|
2058
|
+
return composeApplicationAspects(...active);
|
|
2073
2059
|
}
|
|
2074
2060
|
function observedAspects(aspects, boundary, observer) {
|
|
2075
2061
|
return composeAspects(...aspects.map((aspect, index) => (context, next) => observeExecution(observer, {
|
|
@@ -2678,8 +2664,15 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2678
2664
|
"parse",
|
|
2679
2665
|
"allowDeleteBody"
|
|
2680
2666
|
]);
|
|
2667
|
+
const pendingRoutes = new Map;
|
|
2681
2668
|
for (const controller of compiled.controllers) {
|
|
2682
2669
|
for (const route of controller.routes) {
|
|
2670
|
+
if (pendingRoutes.has(route)) {
|
|
2671
|
+
throw new ApplicationError(`Duplicate route descriptor in module "${compiled.name}". Recompile the application.`, {
|
|
2672
|
+
code: "COMPILED_ROUTE_REGISTRATION_INVALID"
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
pendingRoutes.set(route, { controller, path: joinPaths(controller.path, route.path) });
|
|
2683
2676
|
const unsupported = Object.keys(route).filter((field) => !supportedFields.has(field));
|
|
2684
2677
|
if (!supportedMethods.has(route.method) || unsupported.length > 0) {
|
|
2685
2678
|
throw new ApplicationError(`Unsupported compiled route ${route.method} ${controller.path}${route.path}` + (unsupported.length > 0 ? `: ${unsupported.join(", ")}` : ""), { code: "ROUTE_DESCRIPTOR_UNSUPPORTED" });
|
|
@@ -2695,6 +2688,9 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2695
2688
|
}
|
|
2696
2689
|
}
|
|
2697
2690
|
}
|
|
2691
|
+
if ((compiled.controllers.some((controller) => controller.routes.length > 0) || compiled.registerRoutes !== undefined) && typeof compiled.registerRoutes !== "function") {
|
|
2692
|
+
throw new ApplicationError(`Module "${compiled.name}" requires a compiler-emitted registerRoutes plan. Upgrade @supacloud/compiler with @supacloud/elysia and recompile the application.`, { code: "COMPILED_ROUTE_REGISTRATION_REQUIRED" });
|
|
2693
|
+
}
|
|
2698
2694
|
const hasCommandRoutes = compiled.controllers.some((controller) => controller.routes.some((route) => route.command !== undefined));
|
|
2699
2695
|
if (hasCommandRoutes && !options.commandGovernance && !options.commandExecutor) {
|
|
2700
2696
|
throw new ApplicationError(`Module "${compiled.name}" has command routes but no commandGovernance`, { code: "COMMAND_GOVERNANCE_UNCONFIGURED" });
|
|
@@ -2778,225 +2774,238 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2778
2774
|
plugin.onError(SchemaContractError, ({ error, request }) => mapError(error, frameworkErrorCode(error), request));
|
|
2779
2775
|
plugin.onError(({ error, request }) => mapError(error, frameworkErrorCode(error), request));
|
|
2780
2776
|
const routeRegistrar = plugin;
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
if (
|
|
2812
|
-
|
|
2777
|
+
const registerRoute = (controller, route, path) => {
|
|
2778
|
+
if (pendingRoutes.get(route)?.controller !== controller || pendingRoutes.get(route)?.path !== path) {
|
|
2779
|
+
throw new ApplicationError(`Invalid static route registration in module "${compiled.name}". Recompile the application.`, {
|
|
2780
|
+
code: "COMPILED_ROUTE_REGISTRATION_INVALID"
|
|
2781
|
+
});
|
|
2782
|
+
}
|
|
2783
|
+
pendingRoutes.delete(route);
|
|
2784
|
+
const documentation = route.data?.openapi;
|
|
2785
|
+
const hidden = documentation !== null && typeof documentation === "object" && "hide" in documentation && documentation.hide === true;
|
|
2786
|
+
const tags = documentation !== null && typeof documentation === "object" && "tags" in documentation && Array.isArray(documentation.tags) ? documentation.tags.filter((tag) => typeof tag === "string") : undefined;
|
|
2787
|
+
const schema = {
|
|
2788
|
+
...toElysiaRouteSchema(route),
|
|
2789
|
+
...route.parse === "none" ? { parse: "none" } : {},
|
|
2790
|
+
...route.title || hidden || tags ? { detail: {
|
|
2791
|
+
...route.title ? { summary: route.title } : {},
|
|
2792
|
+
...hidden ? { hide: true } : {},
|
|
2793
|
+
...tags ? { tags } : {}
|
|
2794
|
+
} } : {}
|
|
2795
|
+
};
|
|
2796
|
+
const policies = compileHttpPolicies(route, path, options.httpPolicies);
|
|
2797
|
+
if (policies.length > 0) {
|
|
2798
|
+
Object.assign(schema, {
|
|
2799
|
+
beforeHandle: async (context) => {
|
|
2800
|
+
for (const policy of policies) {
|
|
2801
|
+
const result = await policy({
|
|
2802
|
+
http: context,
|
|
2803
|
+
requestContext: requestContexts.get(context.request)
|
|
2804
|
+
});
|
|
2805
|
+
if (result instanceof Response)
|
|
2806
|
+
return result;
|
|
2807
|
+
if (result !== undefined) {
|
|
2808
|
+
throw new Error("HTTP policies must return Response or undefined");
|
|
2813
2809
|
}
|
|
2814
2810
|
}
|
|
2815
|
-
|
|
2816
|
-
} else if (route.idempotency === "required") {
|
|
2817
|
-
Object.assign(schema, {
|
|
2818
|
-
beforeHandle: (context) => {
|
|
2811
|
+
if (route.idempotency === "required") {
|
|
2819
2812
|
requireRouteIdempotencyKey(context.request);
|
|
2820
2813
|
}
|
|
2821
|
-
}
|
|
2822
|
-
}
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2814
|
+
}
|
|
2815
|
+
});
|
|
2816
|
+
} else if (route.idempotency === "required") {
|
|
2817
|
+
Object.assign(schema, {
|
|
2818
|
+
beforeHandle: (context) => {
|
|
2819
|
+
requireRouteIdempotencyKey(context.request);
|
|
2820
|
+
}
|
|
2821
|
+
});
|
|
2822
|
+
}
|
|
2823
|
+
const responsePolicies = policies.filter((policy) => policy.afterResponse);
|
|
2824
|
+
const mappingPolicies = policies.filter((policy) => policy.mapResponse);
|
|
2825
|
+
const routePipeline = descriptorPipeline(route, "route", options.onExecution);
|
|
2826
|
+
const command = route.command ? commandsByClassName.get(route.command) : undefined;
|
|
2827
|
+
const commandPipeline = route.command ? descriptorPipeline(command ?? {}, "command", options.onExecution) : undefined;
|
|
2828
|
+
const routeHasAspects = compiled.aspectPipeline !== undefined || (compiled.aspects?.length ?? 0) > 0 || route.aspectPipeline !== undefined || (route.aspects?.length ?? 0) > 0 || command?.aspectPipeline !== undefined || (command?.aspects?.length ?? 0) > 0;
|
|
2829
|
+
const routeInvoker = route.invoker;
|
|
2830
|
+
if (mappingPolicies.length > 0) {
|
|
2831
|
+
const responseValidators = Validator2.response(schema.response, { normalize: options.normalize ?? true });
|
|
2832
|
+
Object.assign(schema, {
|
|
2833
|
+
mapResponse: async (context) => {
|
|
2834
|
+
let response = context.responseValue;
|
|
2835
|
+
let mapped;
|
|
2836
|
+
for (const policy of mappingPolicies) {
|
|
2837
|
+
const result = await policy.mapResponse({
|
|
2838
|
+
http: context,
|
|
2839
|
+
requestContext: requestContexts.get(context.request),
|
|
2840
|
+
response,
|
|
2841
|
+
encodeResponse: (value) => {
|
|
2842
|
+
const validator = responseValidators?.get(responseStatusOf(value, context.set.status));
|
|
2843
|
+
if (!validator)
|
|
2844
|
+
return value;
|
|
2845
|
+
if (!("EncodeFrom" in validator) || typeof validator.EncodeFrom !== "function") {
|
|
2846
|
+
throw new ApplicationError("Response validation failed", { code: "RESPONSE_VALIDATION_FAILED" });
|
|
2847
|
+
}
|
|
2848
|
+
if ("mayReturnPromise" in validator && validator.mayReturnPromise && "From" in validator && typeof validator.From === "function") {
|
|
2849
|
+
return validator.From.call(validator, value, "response", true);
|
|
2851
2850
|
}
|
|
2852
|
-
|
|
2853
|
-
if (result !== undefined) {
|
|
2854
|
-
if (!(result instanceof Response))
|
|
2855
|
-
throw new Error("HTTP response policies must return Response or undefined");
|
|
2856
|
-
mapped = result;
|
|
2857
|
-
response = result;
|
|
2851
|
+
return validator.EncodeFrom.call(validator, value, "response");
|
|
2858
2852
|
}
|
|
2853
|
+
});
|
|
2854
|
+
if (result !== undefined) {
|
|
2855
|
+
if (!(result instanceof Response))
|
|
2856
|
+
throw new Error("HTTP response policies must return Response or undefined");
|
|
2857
|
+
mapped = result;
|
|
2858
|
+
response = result;
|
|
2859
2859
|
}
|
|
2860
|
-
return mapped;
|
|
2861
2860
|
}
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2861
|
+
return mapped;
|
|
2862
|
+
}
|
|
2863
|
+
});
|
|
2864
|
+
}
|
|
2865
|
+
if (responsePolicies.length > 0) {
|
|
2866
|
+
Object.assign(schema, {
|
|
2867
|
+
afterResponse: async (context) => {
|
|
2868
|
+
for (const policy of responsePolicies) {
|
|
2869
|
+
await policy.afterResponse({
|
|
2870
|
+
http: context,
|
|
2871
|
+
requestContext: requestContexts.get(context.request),
|
|
2872
|
+
response: context.responseValue
|
|
2873
|
+
});
|
|
2874
2874
|
}
|
|
2875
|
-
}
|
|
2876
|
-
}
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2875
|
+
}
|
|
2876
|
+
});
|
|
2877
|
+
}
|
|
2878
|
+
const directRoute = canUseDirectRoute(compiled, controller, route) && options.onExecution === undefined && policies.length === 0 && responsePolicies.length === 0 && mappingPolicies.length === 0;
|
|
2879
|
+
const directController = directRoute && route.directInvokerFast !== undefined ? controllerInstance(services[controller.serviceKey]) : undefined;
|
|
2880
|
+
const directMethod = directController?.[route.handler];
|
|
2881
|
+
const directInvokerFast = route.directInvokerFast;
|
|
2882
|
+
const directHandler = directController && typeof directMethod === "function" && directInvokerFast ? (ctx) => directInvokerFast(directController, directMethod, ctx) : (ctx) => route.directInvoker(services[controller.serviceKey], ctx);
|
|
2883
|
+
const handler = directRoute ? directHandler : async (ctx) => {
|
|
2884
|
+
const requestContext = requestContexts.get(ctx.request);
|
|
2885
|
+
const execute = async () => {
|
|
2886
|
+
const requestScope = createRequestScope ? await createRequestScope(services, requestContext, imported) : undefined;
|
|
2887
|
+
if (requestScope && compiled.destroyRequestScope)
|
|
2888
|
+
requestScopes.set(ctx.request, requestScope);
|
|
2889
|
+
const source = controller.scope === "request" ? requestScope : services;
|
|
2890
|
+
const instance = source?.[controller.serviceKey];
|
|
2891
|
+
const normalizedCookie = ctx.cookie === undefined ? undefined : cookieValues(ctx.cookie);
|
|
2892
|
+
const input = {
|
|
2893
|
+
body: ctx.body,
|
|
2894
|
+
params: ctx.params,
|
|
2895
|
+
query: ctx.query,
|
|
2896
|
+
headers: ctx.headers ?? Object.fromEntries(ctx.request.headers.entries()),
|
|
2897
|
+
...normalizedCookie === undefined ? {} : { cookie: normalizedCookie },
|
|
2898
|
+
context: requestContext,
|
|
2899
|
+
request: ctx.request,
|
|
2900
|
+
...requestScope ? { scope: requestScope } : {},
|
|
2901
|
+
requestContext
|
|
2902
|
+
};
|
|
2903
|
+
const handlerCall = () => {
|
|
2904
|
+
if (route.invoker)
|
|
2905
|
+
return route.invoker(instance, input);
|
|
2906
|
+
if (!isRecord2(instance))
|
|
2907
|
+
throw new TypeError("Route controller is not an object");
|
|
2908
|
+
const method = instance[route.handler];
|
|
2909
|
+
if (typeof method !== "function")
|
|
2910
|
+
throw new TypeError(`Route handler ${route.handler} is not callable`);
|
|
2911
|
+
return method.call(instance, input);
|
|
2912
|
+
};
|
|
2913
|
+
const invokeHandler = async () => runCompiledEffect(await handlerCall(), route.effect, options.effectRuntime);
|
|
2914
|
+
const invoke = once(() => route.command && options.commandGovernance ? invokeHandler() : options.onExecution ? observeExecution(options.onExecution, {
|
|
2915
|
+
kind: route.command ? "command" : "route",
|
|
2916
|
+
operation: route.command ?? `${route.method} ${path}`,
|
|
2917
|
+
stage: "handler",
|
|
2918
|
+
...executionTrace(requestContext)
|
|
2919
|
+
}, invokeHandler) : invokeHandler());
|
|
2920
|
+
const routeContext = {
|
|
2921
|
+
kind: "route",
|
|
2922
|
+
name: `${route.method} ${path}`,
|
|
2923
|
+
input,
|
|
2924
|
+
request: ctx.request,
|
|
2925
|
+
requestContext,
|
|
2926
|
+
...requestScope ? { scope: requestScope } : {},
|
|
2927
|
+
services,
|
|
2928
|
+
metadata: route
|
|
2929
|
+
};
|
|
2930
|
+
const command = route.command === undefined ? undefined : resolveCompiledCommand(compiled, route.command);
|
|
2931
|
+
const commandContext = route.command === undefined ? routeContext : {
|
|
2932
|
+
kind: "command",
|
|
2933
|
+
name: command?.name ?? route.command,
|
|
2934
|
+
input,
|
|
2935
|
+
request: ctx.request,
|
|
2936
|
+
requestContext,
|
|
2937
|
+
...requestScope ? { scope: requestScope } : {},
|
|
2938
|
+
services,
|
|
2939
|
+
metadata: command ?? route
|
|
2940
|
+
};
|
|
2941
|
+
const invokeRoute = () => route.command && routeHasAspects ? invokeAspect(commandContext, (_context, next) => modulePipeline(commandContext, () => routePipeline(routeContext, () => commandPipeline(commandContext, next))), invoke, (result) => {
|
|
2942
|
+
if (!(result instanceof Response))
|
|
2943
|
+
freezeAspectData(result);
|
|
2944
|
+
}) : modulePipeline(route.command ? commandContext : routeContext, once(() => routePipeline(routeContext, once(() => route.command ? commandPipeline(commandContext, once(invoke)) : invoke()))));
|
|
2945
|
+
const invokeCommand = () => {
|
|
2946
|
+
if (!route.command)
|
|
2947
|
+
return invokeRoute();
|
|
2948
|
+
if (!command) {
|
|
2949
|
+
throw new ApplicationError(`Command "${route.command}" is not registered`, {
|
|
2950
|
+
code: "COMMAND_NOT_REGISTERED"
|
|
2951
|
+
});
|
|
2952
|
+
}
|
|
2953
|
+
if (!commandExecutor) {
|
|
2954
|
+
throw new ApplicationError(`Command "${command.name}" has no executor`, {
|
|
2955
|
+
status: 501,
|
|
2956
|
+
code: "COMMAND_EXECUTOR_UNAVAILABLE"
|
|
2957
|
+
});
|
|
2958
|
+
}
|
|
2959
|
+
const invocation = {
|
|
2960
|
+
command,
|
|
2922
2961
|
input,
|
|
2923
2962
|
request: ctx.request,
|
|
2924
2963
|
requestContext,
|
|
2925
2964
|
...requestScope ? { scope: requestScope } : {},
|
|
2926
|
-
services
|
|
2927
|
-
metadata: route
|
|
2965
|
+
services
|
|
2928
2966
|
};
|
|
2929
|
-
|
|
2930
|
-
const commandContext = route.command === undefined ? routeContext : {
|
|
2967
|
+
return observeExecution(options.onExecution, {
|
|
2931
2968
|
kind: "command",
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
...requestScope ? { scope: requestScope } : {},
|
|
2937
|
-
services,
|
|
2938
|
-
metadata: command ?? route
|
|
2939
|
-
};
|
|
2940
|
-
const invokeRoute = () => modulePipeline(route.command ? commandContext : routeContext, once(() => routePipeline(routeContext, once(() => route.command ? commandPipeline(commandContext, once(invoke)) : invoke()))));
|
|
2941
|
-
const invokeCommand = () => {
|
|
2942
|
-
if (!route.command)
|
|
2943
|
-
return invokeRoute();
|
|
2944
|
-
if (!command) {
|
|
2945
|
-
throw new ApplicationError(`Command "${route.command}" is not registered`, {
|
|
2946
|
-
code: "COMMAND_NOT_REGISTERED"
|
|
2947
|
-
});
|
|
2948
|
-
}
|
|
2949
|
-
if (!commandExecutor) {
|
|
2950
|
-
throw new ApplicationError(`Command "${command.name}" has no executor`, {
|
|
2951
|
-
status: 501,
|
|
2952
|
-
code: "COMMAND_EXECUTOR_UNAVAILABLE"
|
|
2953
|
-
});
|
|
2954
|
-
}
|
|
2955
|
-
const invocation = {
|
|
2956
|
-
command,
|
|
2957
|
-
input,
|
|
2958
|
-
request: ctx.request,
|
|
2959
|
-
requestContext,
|
|
2960
|
-
...requestScope ? { scope: requestScope } : {},
|
|
2961
|
-
services
|
|
2962
|
-
};
|
|
2963
|
-
return observeExecution(options.onExecution, {
|
|
2964
|
-
kind: "command",
|
|
2965
|
-
operation: command.name,
|
|
2966
|
-
stage: "commandExecutor",
|
|
2967
|
-
...executionTrace(requestContext)
|
|
2968
|
-
}, () => commandExecutor(invocation, once(invokeRoute)));
|
|
2969
|
-
};
|
|
2970
|
-
return invokeCommand();
|
|
2969
|
+
operation: command.name,
|
|
2970
|
+
stage: "commandExecutor",
|
|
2971
|
+
...executionTrace(requestContext)
|
|
2972
|
+
}, () => commandExecutor(invocation, once(invokeRoute)));
|
|
2971
2973
|
};
|
|
2972
|
-
|
|
2973
|
-
assertDeclaredResponseStatus(route, result, ctx.set.status);
|
|
2974
|
-
return result;
|
|
2974
|
+
return invokeCommand();
|
|
2975
2975
|
};
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2976
|
+
const result = await execute();
|
|
2977
|
+
assertDeclaredResponseStatus(route, result, ctx.set.status);
|
|
2978
|
+
return result;
|
|
2979
|
+
};
|
|
2980
|
+
switch (route.method) {
|
|
2981
|
+
case "GET":
|
|
2982
|
+
routeRegistrar.get(path, schema, handler);
|
|
2983
|
+
break;
|
|
2984
|
+
case "POST":
|
|
2985
|
+
routeRegistrar.post(path, schema, handler);
|
|
2986
|
+
break;
|
|
2987
|
+
case "PUT":
|
|
2988
|
+
routeRegistrar.put(path, schema, handler);
|
|
2989
|
+
break;
|
|
2990
|
+
case "PATCH":
|
|
2991
|
+
routeRegistrar.patch(path, schema, handler);
|
|
2992
|
+
break;
|
|
2993
|
+
case "DELETE":
|
|
2994
|
+
routeRegistrar.delete(path, schema, handler);
|
|
2995
|
+
break;
|
|
2996
|
+
case "HEAD":
|
|
2997
|
+
routeRegistrar.head(path, schema, handler);
|
|
2998
|
+
break;
|
|
2999
|
+
case "OPTIONS":
|
|
3000
|
+
routeRegistrar.options(path, schema, handler);
|
|
3001
|
+
break;
|
|
2999
3002
|
}
|
|
3003
|
+
};
|
|
3004
|
+
compiled.registerRoutes?.(registerRoute);
|
|
3005
|
+
if (pendingRoutes.size > 0) {
|
|
3006
|
+
throw new ApplicationError(`Incomplete static route registration in module "${compiled.name}". Recompile the application.`, {
|
|
3007
|
+
code: "COMPILED_ROUTE_REGISTRATION_INVALID"
|
|
3008
|
+
});
|
|
3000
3009
|
}
|
|
3001
3010
|
return plugin;
|
|
3002
3011
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/elysia",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.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",
|
|
@@ -54,29 +54,31 @@
|
|
|
54
54
|
"directory": "packages/elysia"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@supacloud/app": "0.
|
|
58
|
-
"@supacloud/commands": "0.
|
|
59
|
-
"@supacloud/contracts": "0.
|
|
57
|
+
"@supacloud/app": "0.38.0",
|
|
58
|
+
"@supacloud/commands": "0.12.0",
|
|
59
|
+
"@supacloud/contracts": "0.16.0",
|
|
60
60
|
"effect": "4.0.5",
|
|
61
61
|
"exact-mirror": "1.2.6",
|
|
62
62
|
"jose": "^6.2.13",
|
|
63
63
|
"typebox": "1.4.3"
|
|
64
64
|
},
|
|
65
65
|
"overrides": {
|
|
66
|
-
"@supacloud/delivery": "0.
|
|
67
|
-
"@supacloud/contracts": "0.
|
|
68
|
-
"@supacloud/commands": "0.
|
|
69
|
-
"@graphql-tools/utils": "^12.0.4"
|
|
66
|
+
"@supacloud/delivery": "0.25.0",
|
|
67
|
+
"@supacloud/contracts": "0.16.0",
|
|
68
|
+
"@supacloud/commands": "0.12.0",
|
|
69
|
+
"@graphql-tools/utils": "^12.0.4",
|
|
70
|
+
"deepmerge-ts": "8.0.2",
|
|
71
|
+
"prisma": "6.19.3"
|
|
70
72
|
},
|
|
71
73
|
"peerDependencies": {
|
|
72
74
|
"elysia": "2.0.0-rc.1"
|
|
73
75
|
},
|
|
74
76
|
"devDependencies": {
|
|
75
|
-
"@supacloud/compiler": "0.
|
|
76
|
-
"@supacloud/delivery": "0.
|
|
77
|
-
"@supacloud/db": "0.
|
|
77
|
+
"@supacloud/compiler": "0.57.0",
|
|
78
|
+
"@supacloud/delivery": "0.25.0",
|
|
79
|
+
"@supacloud/db": "0.21.0",
|
|
78
80
|
"@types/bun": "^1.4.4",
|
|
79
81
|
"elysia": "2.0.0-rc.1",
|
|
80
|
-
"typescript": "
|
|
82
|
+
"typescript": "7.1.0-beta"
|
|
81
83
|
}
|
|
82
84
|
}
|