@supacloud/elysia 0.40.0 → 0.41.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 +35 -0
- package/dist/bun-aot.js +77 -19
- package/dist/index.d.ts +2 -0
- package/dist/index.js +78 -19
- package/dist/zenstack-read.d.ts +48 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -65,6 +65,41 @@ evidence. A server using this adapter still installs Elysia; application metadat
|
|
|
65
65
|
and business modules need not import native Elysia types. See the
|
|
66
66
|
[dependency boundary and upgrade policy](../../docs/elysia-compatibility.md).
|
|
67
67
|
|
|
68
|
+
## Identity-bound ZenStack read adapter
|
|
69
|
+
|
|
70
|
+
`createZenStackReadPlugin` mounts explicit GET/HEAD resources on native Elysia 2.
|
|
71
|
+
Each request calls `bindIdentity`, receives the read-only `models` facade from
|
|
72
|
+
`createZenStackDatabase(...).forIdentity(...)`, and invokes one allow-listed
|
|
73
|
+
ZenStack read operation. TypeBox contracts and argument builders are supplied
|
|
74
|
+
per resource:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const read = createZenStackReadPlugin({
|
|
78
|
+
resources: [{
|
|
79
|
+
path: "/model/orders/:id",
|
|
80
|
+
model: "order",
|
|
81
|
+
operation: "findUnique",
|
|
82
|
+
contract: { params: Type.Object({ id: Type.String() }) },
|
|
83
|
+
args: ({ params, identity }) => ({
|
|
84
|
+
where: { tenantId: identity, id: params["id"] },
|
|
85
|
+
}),
|
|
86
|
+
}],
|
|
87
|
+
bindIdentity: async request => ({
|
|
88
|
+
identity: await verifyTenant(request),
|
|
89
|
+
models: database.forIdentity(await verifyPrincipal(request)).models,
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const app = new Elysia().use(read);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The adapter does not expose writes, raw clients, arbitrary model names, or
|
|
97
|
+
automatic CRUD routes. Durable writes remain Commands and use
|
|
98
|
+
`transaction.db`; business routes continue through the compiled module adapter.
|
|
99
|
+
The official `@zenstackhq/server` Elysia handler is not used here because its
|
|
100
|
+
current peer baseline targets Elysia 1.x and its generic CRUD surface would add
|
|
101
|
+
another application boundary.
|
|
102
|
+
|
|
68
103
|
Run `bun run test:conformance` in this package after building the local
|
|
69
104
|
`@supacloud/contracts` and `@supacloud/app` dependencies and installing this
|
|
70
105
|
package's dependencies. The suite runs through `app.handle(Request)` without a
|
package/dist/bun-aot.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { Elysia as
|
|
2
|
+
import { Elysia as Elysia4, setupTypebox, Validator as Validator2 } from "elysia";
|
|
3
3
|
import { requireFeatureRequestContext } from "@supacloud/app";
|
|
4
4
|
import { composeAspects as composeApplicationAspects, invokeAspect, freezeAspectData } from "@supacloud/app/execution";
|
|
5
5
|
import { PendingWorkRegistry as PendingWorkRegistry2, PendingWorkTimeoutError, PendingWorkCapacityError } from "@supacloud/app/runtime";
|
|
@@ -1155,6 +1155,64 @@ function bindCompiledCommand(options) {
|
|
|
1155
1155
|
} : {}
|
|
1156
1156
|
};
|
|
1157
1157
|
}
|
|
1158
|
+
// src/zenstack-read.ts
|
|
1159
|
+
import { Elysia as Elysia3 } from "elysia";
|
|
1160
|
+
var READ_OPERATIONS = new Set([
|
|
1161
|
+
"findUnique",
|
|
1162
|
+
"findUniqueOrThrow",
|
|
1163
|
+
"findFirst",
|
|
1164
|
+
"findFirstOrThrow",
|
|
1165
|
+
"findMany",
|
|
1166
|
+
"count",
|
|
1167
|
+
"aggregate",
|
|
1168
|
+
"groupBy"
|
|
1169
|
+
]);
|
|
1170
|
+
function isRecord2(value) {
|
|
1171
|
+
return value !== null && typeof value === "object";
|
|
1172
|
+
}
|
|
1173
|
+
function invokeRead(models, modelName, operation, args) {
|
|
1174
|
+
const model = Reflect.get(models, modelName);
|
|
1175
|
+
if (!model || typeof model !== "object")
|
|
1176
|
+
throw new Error(`ZenStack read model is unavailable: ${modelName}`);
|
|
1177
|
+
const method = Reflect.get(model, operation);
|
|
1178
|
+
if (typeof method !== "function")
|
|
1179
|
+
throw new Error(`ZenStack read operation is unavailable: ${modelName}.${operation}`);
|
|
1180
|
+
return Promise.resolve(args === undefined ? Reflect.apply(method, model, []) : Reflect.apply(method, model, [args]));
|
|
1181
|
+
}
|
|
1182
|
+
function createZenStackReadPlugin(options) {
|
|
1183
|
+
if (options.resources.length === 0)
|
|
1184
|
+
throw new TypeError("ZenStack read adapter requires at least one resource");
|
|
1185
|
+
const app = new Elysia3({ name: options.name ?? "supacloud:zenstack-read" });
|
|
1186
|
+
const paths = new Set;
|
|
1187
|
+
for (const resource of options.resources) {
|
|
1188
|
+
if (!resource.path.startsWith("/") || paths.has(resource.path)) {
|
|
1189
|
+
throw new TypeError(`Duplicate or invalid ZenStack read path: ${resource.path}`);
|
|
1190
|
+
}
|
|
1191
|
+
paths.add(resource.path);
|
|
1192
|
+
if (!READ_OPERATIONS.has(resource.operation)) {
|
|
1193
|
+
throw new TypeError(`Unsupported ZenStack read operation: ${resource.operation}`);
|
|
1194
|
+
}
|
|
1195
|
+
const contract = resource.contract ?? {};
|
|
1196
|
+
const schema = toElysiaRouteSchema(contract);
|
|
1197
|
+
const handler = async (context) => {
|
|
1198
|
+
const binding = await options.bindIdentity(context.request);
|
|
1199
|
+
if (!binding || !isRecord2(binding) || !isRecord2(binding.models) || binding.identity === null || binding.identity === undefined) {
|
|
1200
|
+
throw new Error("ZenStack identity binding must provide a read-only models facade");
|
|
1201
|
+
}
|
|
1202
|
+
const input = {
|
|
1203
|
+
request: context.request,
|
|
1204
|
+
params: isRecord2(context.params) ? context.params : {},
|
|
1205
|
+
query: isRecord2(context.query) ? context.query : {},
|
|
1206
|
+
headers: isRecord2(context.headers) ? context.headers : {},
|
|
1207
|
+
identity: binding.identity
|
|
1208
|
+
};
|
|
1209
|
+
return invokeRead(binding.models, resource.model, resource.operation, resource.args?.(input));
|
|
1210
|
+
};
|
|
1211
|
+
app.get(resource.path, schema, handler);
|
|
1212
|
+
app.head(resource.path, schema, handler);
|
|
1213
|
+
}
|
|
1214
|
+
return app;
|
|
1215
|
+
}
|
|
1158
1216
|
// src/persistent-command.ts
|
|
1159
1217
|
import {
|
|
1160
1218
|
CommandError,
|
|
@@ -2086,7 +2144,7 @@ function createCommandAuthorizationAdapter(options) {
|
|
|
2086
2144
|
});
|
|
2087
2145
|
}
|
|
2088
2146
|
const context = invocation.requestContext;
|
|
2089
|
-
const identity =
|
|
2147
|
+
const identity = isRecord3(context) && isTrustedIdentity(context.identity) ? context.identity : undefined;
|
|
2090
2148
|
if (!isAuthenticatedTrustedIdentity(identity)) {
|
|
2091
2149
|
throw new ApplicationError("Authentication required", { status: 401, code: "AUTHENTICATION_REQUIRED" });
|
|
2092
2150
|
}
|
|
@@ -2101,7 +2159,7 @@ function createCommandAuthorizationAdapter(options) {
|
|
|
2101
2159
|
} catch {
|
|
2102
2160
|
throw new ApplicationError("Authorization is unavailable", { status: 503, code: "AUTHORIZATION_UNAVAILABLE" });
|
|
2103
2161
|
}
|
|
2104
|
-
if (!
|
|
2162
|
+
if (!isRecord3(resolved) || !Array.isArray(resolved.permissions) || !resolved.permissions.every((value) => typeof value === "string") || resolved.permissionCatalogVersion !== undefined && typeof resolved.permissionCatalogVersion !== "string" || resolved.permissionCatalogDigest !== undefined && typeof resolved.permissionCatalogDigest !== "string" || resolved.applicationId !== options.applicationId || options.catalog !== undefined && (resolved.permissionCatalogVersion !== options.catalog.version || options.catalog.digest !== undefined && resolved.permissionCatalogDigest !== options.catalog.digest)) {
|
|
2105
2163
|
throw new ApplicationError("Authorization context is not bound to this application", {
|
|
2106
2164
|
status: 503,
|
|
2107
2165
|
code: "AUTHORIZATION_CONTEXT_INVALID"
|
|
@@ -2188,10 +2246,10 @@ function contextPlugin(http) {
|
|
|
2188
2246
|
seed = ++nextHttpPluginId;
|
|
2189
2247
|
httpPluginIds.set(http, seed);
|
|
2190
2248
|
}
|
|
2191
|
-
return new
|
|
2249
|
+
return new Elysia4({ name: "supacloud:http-context", seed }).use(http ?? new Elysia4).as("plugin");
|
|
2192
2250
|
}
|
|
2193
2251
|
function mountHttp(http, name, normalize = true) {
|
|
2194
|
-
return new
|
|
2252
|
+
return new Elysia4({ name, normalize }).use(contextPlugin(http));
|
|
2195
2253
|
}
|
|
2196
2254
|
function resolveCompiledCommand(module, reference) {
|
|
2197
2255
|
const matches = (module.commands ?? []).filter((command) => command.className === reference || command.name === reference);
|
|
@@ -2216,14 +2274,14 @@ function safeHeaderValue(value, maxLength) {
|
|
|
2216
2274
|
}
|
|
2217
2275
|
return value;
|
|
2218
2276
|
}
|
|
2219
|
-
function
|
|
2277
|
+
function isRecord3(value) {
|
|
2220
2278
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2221
2279
|
}
|
|
2222
2280
|
function isPromiseLike(value) {
|
|
2223
2281
|
return value instanceof Promise;
|
|
2224
2282
|
}
|
|
2225
2283
|
function isTrustedIdentity(value) {
|
|
2226
|
-
return
|
|
2284
|
+
return isRecord3(value) && typeof value.authenticated === "boolean" && (value.subject === undefined || typeof value.subject === "string") && (value.accessToken === undefined || typeof value.accessToken === "string");
|
|
2227
2285
|
}
|
|
2228
2286
|
function isAuthenticatedTrustedIdentity(value) {
|
|
2229
2287
|
return value?.authenticated === true && typeof value.subject === "string" && value.subject.length > 0 && typeof value.accessToken === "string" && value.accessToken.length > 0;
|
|
@@ -2288,7 +2346,7 @@ function selectCommandBranch(command, input) {
|
|
|
2288
2346
|
const root = parts[0];
|
|
2289
2347
|
let value = root === "body" || root === "params" || root === "query" ? input[root] : input.body;
|
|
2290
2348
|
for (const part of root === "body" || root === "params" || root === "query" ? parts.slice(1) : parts) {
|
|
2291
|
-
if (!
|
|
2349
|
+
if (!isRecord3(value)) {
|
|
2292
2350
|
value = undefined;
|
|
2293
2351
|
break;
|
|
2294
2352
|
}
|
|
@@ -2467,7 +2525,7 @@ function once(next) {
|
|
|
2467
2525
|
};
|
|
2468
2526
|
}
|
|
2469
2527
|
function requireTrustedIdentity(requestContext) {
|
|
2470
|
-
const context =
|
|
2528
|
+
const context = isRecord3(requestContext) ? requestContext : {};
|
|
2471
2529
|
const identity = isTrustedIdentity(context.identity) ? context.identity : undefined;
|
|
2472
2530
|
if (!isAuthenticatedTrustedIdentity(identity)) {
|
|
2473
2531
|
throw new ApplicationError("Authenticated user context is required", {
|
|
@@ -2478,7 +2536,7 @@ function requireTrustedIdentity(requestContext) {
|
|
|
2478
2536
|
return identity;
|
|
2479
2537
|
}
|
|
2480
2538
|
function requireIdempotencyKey(invocation) {
|
|
2481
|
-
const context =
|
|
2539
|
+
const context = isRecord3(invocation.requestContext) ? invocation.requestContext : {};
|
|
2482
2540
|
const key = (typeof context.idempotencyKey === "string" ? context.idempotencyKey : undefined) ?? safeHeaderValue(invocation.request.headers.get(IDEMPOTENCY_KEY_HEADER), 512);
|
|
2483
2541
|
if (!key || !idempotencyKeyPattern.test(key)) {
|
|
2484
2542
|
throw new ApplicationError("A valid Idempotency-Key header is required", {
|
|
@@ -2531,7 +2589,7 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
|
|
|
2531
2589
|
});
|
|
2532
2590
|
}
|
|
2533
2591
|
const adapter = Object.hasOwn(governance.rpc ?? {}, branch.rpc) ? governance.rpc?.[branch.rpc] : undefined;
|
|
2534
|
-
if (!adapter || typeof adapter.execute !== "function" || !
|
|
2592
|
+
if (!adapter || typeof adapter.execute !== "function" || !isRecord3(adapter.capabilities)) {
|
|
2535
2593
|
throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" has no RPC governance adapter`, {
|
|
2536
2594
|
code: "COMMAND_RPC_UNCONFIGURED"
|
|
2537
2595
|
});
|
|
@@ -2574,7 +2632,7 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
|
|
|
2574
2632
|
}
|
|
2575
2633
|
if (command.rpc !== undefined) {
|
|
2576
2634
|
const adapter = Object.hasOwn(governance.rpc ?? {}, command.rpc) ? governance.rpc?.[command.rpc] : undefined;
|
|
2577
|
-
if (!adapter || typeof adapter.execute !== "function" || !
|
|
2635
|
+
if (!adapter || typeof adapter.execute !== "function" || !isRecord3(adapter.capabilities)) {
|
|
2578
2636
|
throw new ApplicationError(`Command "${command.name}" has no RPC governance adapter`, {
|
|
2579
2637
|
code: "COMMAND_RPC_UNCONFIGURED"
|
|
2580
2638
|
});
|
|
@@ -2602,17 +2660,17 @@ function joinPaths(prefix, path) {
|
|
|
2602
2660
|
return joined.length > 1 ? joined.replace(/\/+$/, "") : joined;
|
|
2603
2661
|
}
|
|
2604
2662
|
function controllerInstance(value) {
|
|
2605
|
-
return
|
|
2663
|
+
return isRecord3(value) ? value : undefined;
|
|
2606
2664
|
}
|
|
2607
2665
|
function canUseDirectRoute(compiled, controller, route) {
|
|
2608
2666
|
return route.executionMode === "direct" && route.directInvoker !== undefined && controller.scope === "application" && compiled.createRequestScope === undefined && compiled.aspectPipeline === undefined && (compiled.aspects?.length ?? 0) === 0 && route.aspectPipeline === undefined && (route.aspects?.length ?? 0) === 0 && route.command === undefined && route.effect === undefined && route.responses === undefined;
|
|
2609
2667
|
}
|
|
2610
2668
|
function cookieValues(value) {
|
|
2611
|
-
if (!
|
|
2669
|
+
if (!isRecord3(value))
|
|
2612
2670
|
return;
|
|
2613
2671
|
const result = {};
|
|
2614
2672
|
for (const [name, entry] of Object.entries(value)) {
|
|
2615
|
-
result[name] =
|
|
2673
|
+
result[name] = isRecord3(entry) && "value" in entry ? entry.value : entry;
|
|
2616
2674
|
}
|
|
2617
2675
|
return result;
|
|
2618
2676
|
}
|
|
@@ -2903,7 +2961,7 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2903
2961
|
const handlerCall = () => {
|
|
2904
2962
|
if (route.invoker)
|
|
2905
2963
|
return route.invoker(instance, input);
|
|
2906
|
-
if (!
|
|
2964
|
+
if (!isRecord3(instance))
|
|
2907
2965
|
throw new TypeError("Route controller is not an object");
|
|
2908
2966
|
const method = instance[route.handler];
|
|
2909
2967
|
if (typeof method !== "function")
|
|
@@ -3095,7 +3153,7 @@ function defaultErrorResponse(error, frameworkCode) {
|
|
|
3095
3153
|
}, { status: 400 });
|
|
3096
3154
|
}
|
|
3097
3155
|
if (frameworkCode === "VALIDATION") {
|
|
3098
|
-
if (
|
|
3156
|
+
if (isRecord3(error) && error.type === "response") {
|
|
3099
3157
|
return Response.json({
|
|
3100
3158
|
ok: false,
|
|
3101
3159
|
code: "RESPONSE_VALIDATION_ERROR",
|
|
@@ -3115,13 +3173,13 @@ function defaultErrorResponse(error, frameworkCode) {
|
|
|
3115
3173
|
}, { status: 500 });
|
|
3116
3174
|
}
|
|
3117
3175
|
function isPublicApplicationError(error) {
|
|
3118
|
-
if (!(error instanceof Error) || !
|
|
3176
|
+
if (!(error instanceof Error) || !isRecord3(error))
|
|
3119
3177
|
return false;
|
|
3120
3178
|
return error.expose === true && typeof error.status === "number" && Number.isInteger(error.status) && error.status >= 400 && error.status <= 599 && typeof error.code === "string" && error.code.length > 0;
|
|
3121
3179
|
}
|
|
3122
3180
|
function createApplication(options) {
|
|
3123
3181
|
const pendingWork = new PendingWorkRegistry2(options.pendingWork ? { capacity: options.pendingWork.capacity } : {});
|
|
3124
|
-
const app = new
|
|
3182
|
+
const app = new Elysia4({ name: options.name ?? "supacloud:app", normalize: options.normalize ?? true }).use(contextPlugin(options.http).as("global"));
|
|
3125
3183
|
if (options.documentation)
|
|
3126
3184
|
app.use(createDocumentationPlugin(options.documentation));
|
|
3127
3185
|
const ctxFactory = options.requestContext === undefined ? defaultRequestContext : (() => {
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export type { CompiledCommandBinding, CompiledCommandBindingOptions, CompiledCom
|
|
|
23
23
|
export { createSchemaDecoder, assertResponseStatusDeclared, defineElysiaRoute, defineJsonContract, defineRouteContract, registerElysiaRoute, responseSchemaForStatus, responseStatusDeclared, responseStatusOf, SchemaContractError, toElysiaRouteSchema, } from "./schema_contract";
|
|
24
24
|
export { createDefaultEffectRuntime, createEffectRuntimeFromLayer, EffectApplicationError, runCompiledEffect, } from "./effect";
|
|
25
25
|
export type { CompiledEffectDescriptor, SupaCloudEffectRuntime } from "./effect";
|
|
26
|
+
export { createZenStackReadPlugin } from "./zenstack-read";
|
|
27
|
+
export type { ZenStackReadAdapterOptions, ZenStackReadIdentityBinding, ZenStackReadInput, ZenStackReadModels, ZenStackReadOperation, ZenStackReadResource, } from "./zenstack-read";
|
|
26
28
|
export type { ElysiaRouteContext, ElysiaRouteDefinition, ElysiaRouteHandler, ElysiaRouteSchema, ResponseMapSelector, RouteContractSchemas, SchemaDecoderOptions, SchemaNormalizeMode, } from "./schema_contract";
|
|
27
29
|
export { createPersistentCommandAdapter, type PersistentCommandHandler } from "./persistent-command";
|
|
28
30
|
export { createTransactionalCommandGovernance } from "./transactional-command";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { Elysia as
|
|
2
|
+
import { Elysia as Elysia4, setupTypebox, Validator as Validator2 } from "elysia";
|
|
3
3
|
import { requireFeatureRequestContext } from "@supacloud/app";
|
|
4
4
|
import { composeAspects as composeApplicationAspects, invokeAspect, freezeAspectData } from "@supacloud/app/execution";
|
|
5
5
|
import { PendingWorkRegistry as PendingWorkRegistry2, PendingWorkTimeoutError, PendingWorkCapacityError } from "@supacloud/app/runtime";
|
|
@@ -1155,6 +1155,64 @@ function bindCompiledCommand(options) {
|
|
|
1155
1155
|
} : {}
|
|
1156
1156
|
};
|
|
1157
1157
|
}
|
|
1158
|
+
// src/zenstack-read.ts
|
|
1159
|
+
import { Elysia as Elysia3 } from "elysia";
|
|
1160
|
+
var READ_OPERATIONS = new Set([
|
|
1161
|
+
"findUnique",
|
|
1162
|
+
"findUniqueOrThrow",
|
|
1163
|
+
"findFirst",
|
|
1164
|
+
"findFirstOrThrow",
|
|
1165
|
+
"findMany",
|
|
1166
|
+
"count",
|
|
1167
|
+
"aggregate",
|
|
1168
|
+
"groupBy"
|
|
1169
|
+
]);
|
|
1170
|
+
function isRecord2(value) {
|
|
1171
|
+
return value !== null && typeof value === "object";
|
|
1172
|
+
}
|
|
1173
|
+
function invokeRead(models, modelName, operation, args) {
|
|
1174
|
+
const model = Reflect.get(models, modelName);
|
|
1175
|
+
if (!model || typeof model !== "object")
|
|
1176
|
+
throw new Error(`ZenStack read model is unavailable: ${modelName}`);
|
|
1177
|
+
const method = Reflect.get(model, operation);
|
|
1178
|
+
if (typeof method !== "function")
|
|
1179
|
+
throw new Error(`ZenStack read operation is unavailable: ${modelName}.${operation}`);
|
|
1180
|
+
return Promise.resolve(args === undefined ? Reflect.apply(method, model, []) : Reflect.apply(method, model, [args]));
|
|
1181
|
+
}
|
|
1182
|
+
function createZenStackReadPlugin(options) {
|
|
1183
|
+
if (options.resources.length === 0)
|
|
1184
|
+
throw new TypeError("ZenStack read adapter requires at least one resource");
|
|
1185
|
+
const app = new Elysia3({ name: options.name ?? "supacloud:zenstack-read" });
|
|
1186
|
+
const paths = new Set;
|
|
1187
|
+
for (const resource of options.resources) {
|
|
1188
|
+
if (!resource.path.startsWith("/") || paths.has(resource.path)) {
|
|
1189
|
+
throw new TypeError(`Duplicate or invalid ZenStack read path: ${resource.path}`);
|
|
1190
|
+
}
|
|
1191
|
+
paths.add(resource.path);
|
|
1192
|
+
if (!READ_OPERATIONS.has(resource.operation)) {
|
|
1193
|
+
throw new TypeError(`Unsupported ZenStack read operation: ${resource.operation}`);
|
|
1194
|
+
}
|
|
1195
|
+
const contract = resource.contract ?? {};
|
|
1196
|
+
const schema = toElysiaRouteSchema(contract);
|
|
1197
|
+
const handler = async (context) => {
|
|
1198
|
+
const binding = await options.bindIdentity(context.request);
|
|
1199
|
+
if (!binding || !isRecord2(binding) || !isRecord2(binding.models) || binding.identity === null || binding.identity === undefined) {
|
|
1200
|
+
throw new Error("ZenStack identity binding must provide a read-only models facade");
|
|
1201
|
+
}
|
|
1202
|
+
const input = {
|
|
1203
|
+
request: context.request,
|
|
1204
|
+
params: isRecord2(context.params) ? context.params : {},
|
|
1205
|
+
query: isRecord2(context.query) ? context.query : {},
|
|
1206
|
+
headers: isRecord2(context.headers) ? context.headers : {},
|
|
1207
|
+
identity: binding.identity
|
|
1208
|
+
};
|
|
1209
|
+
return invokeRead(binding.models, resource.model, resource.operation, resource.args?.(input));
|
|
1210
|
+
};
|
|
1211
|
+
app.get(resource.path, schema, handler);
|
|
1212
|
+
app.head(resource.path, schema, handler);
|
|
1213
|
+
}
|
|
1214
|
+
return app;
|
|
1215
|
+
}
|
|
1158
1216
|
// src/persistent-command.ts
|
|
1159
1217
|
import {
|
|
1160
1218
|
CommandError,
|
|
@@ -2086,7 +2144,7 @@ function createCommandAuthorizationAdapter(options) {
|
|
|
2086
2144
|
});
|
|
2087
2145
|
}
|
|
2088
2146
|
const context = invocation.requestContext;
|
|
2089
|
-
const identity =
|
|
2147
|
+
const identity = isRecord3(context) && isTrustedIdentity(context.identity) ? context.identity : undefined;
|
|
2090
2148
|
if (!isAuthenticatedTrustedIdentity(identity)) {
|
|
2091
2149
|
throw new ApplicationError("Authentication required", { status: 401, code: "AUTHENTICATION_REQUIRED" });
|
|
2092
2150
|
}
|
|
@@ -2101,7 +2159,7 @@ function createCommandAuthorizationAdapter(options) {
|
|
|
2101
2159
|
} catch {
|
|
2102
2160
|
throw new ApplicationError("Authorization is unavailable", { status: 503, code: "AUTHORIZATION_UNAVAILABLE" });
|
|
2103
2161
|
}
|
|
2104
|
-
if (!
|
|
2162
|
+
if (!isRecord3(resolved) || !Array.isArray(resolved.permissions) || !resolved.permissions.every((value) => typeof value === "string") || resolved.permissionCatalogVersion !== undefined && typeof resolved.permissionCatalogVersion !== "string" || resolved.permissionCatalogDigest !== undefined && typeof resolved.permissionCatalogDigest !== "string" || resolved.applicationId !== options.applicationId || options.catalog !== undefined && (resolved.permissionCatalogVersion !== options.catalog.version || options.catalog.digest !== undefined && resolved.permissionCatalogDigest !== options.catalog.digest)) {
|
|
2105
2163
|
throw new ApplicationError("Authorization context is not bound to this application", {
|
|
2106
2164
|
status: 503,
|
|
2107
2165
|
code: "AUTHORIZATION_CONTEXT_INVALID"
|
|
@@ -2188,10 +2246,10 @@ function contextPlugin(http) {
|
|
|
2188
2246
|
seed = ++nextHttpPluginId;
|
|
2189
2247
|
httpPluginIds.set(http, seed);
|
|
2190
2248
|
}
|
|
2191
|
-
return new
|
|
2249
|
+
return new Elysia4({ name: "supacloud:http-context", seed }).use(http ?? new Elysia4).as("plugin");
|
|
2192
2250
|
}
|
|
2193
2251
|
function mountHttp(http, name, normalize = true) {
|
|
2194
|
-
return new
|
|
2252
|
+
return new Elysia4({ name, normalize }).use(contextPlugin(http));
|
|
2195
2253
|
}
|
|
2196
2254
|
function resolveCompiledCommand(module, reference) {
|
|
2197
2255
|
const matches = (module.commands ?? []).filter((command) => command.className === reference || command.name === reference);
|
|
@@ -2216,14 +2274,14 @@ function safeHeaderValue(value, maxLength) {
|
|
|
2216
2274
|
}
|
|
2217
2275
|
return value;
|
|
2218
2276
|
}
|
|
2219
|
-
function
|
|
2277
|
+
function isRecord3(value) {
|
|
2220
2278
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2221
2279
|
}
|
|
2222
2280
|
function isPromiseLike(value) {
|
|
2223
2281
|
return value instanceof Promise;
|
|
2224
2282
|
}
|
|
2225
2283
|
function isTrustedIdentity(value) {
|
|
2226
|
-
return
|
|
2284
|
+
return isRecord3(value) && typeof value.authenticated === "boolean" && (value.subject === undefined || typeof value.subject === "string") && (value.accessToken === undefined || typeof value.accessToken === "string");
|
|
2227
2285
|
}
|
|
2228
2286
|
function isAuthenticatedTrustedIdentity(value) {
|
|
2229
2287
|
return value?.authenticated === true && typeof value.subject === "string" && value.subject.length > 0 && typeof value.accessToken === "string" && value.accessToken.length > 0;
|
|
@@ -2288,7 +2346,7 @@ function selectCommandBranch(command, input) {
|
|
|
2288
2346
|
const root = parts[0];
|
|
2289
2347
|
let value = root === "body" || root === "params" || root === "query" ? input[root] : input.body;
|
|
2290
2348
|
for (const part of root === "body" || root === "params" || root === "query" ? parts.slice(1) : parts) {
|
|
2291
|
-
if (!
|
|
2349
|
+
if (!isRecord3(value)) {
|
|
2292
2350
|
value = undefined;
|
|
2293
2351
|
break;
|
|
2294
2352
|
}
|
|
@@ -2467,7 +2525,7 @@ function once(next) {
|
|
|
2467
2525
|
};
|
|
2468
2526
|
}
|
|
2469
2527
|
function requireTrustedIdentity(requestContext) {
|
|
2470
|
-
const context =
|
|
2528
|
+
const context = isRecord3(requestContext) ? requestContext : {};
|
|
2471
2529
|
const identity = isTrustedIdentity(context.identity) ? context.identity : undefined;
|
|
2472
2530
|
if (!isAuthenticatedTrustedIdentity(identity)) {
|
|
2473
2531
|
throw new ApplicationError("Authenticated user context is required", {
|
|
@@ -2478,7 +2536,7 @@ function requireTrustedIdentity(requestContext) {
|
|
|
2478
2536
|
return identity;
|
|
2479
2537
|
}
|
|
2480
2538
|
function requireIdempotencyKey(invocation) {
|
|
2481
|
-
const context =
|
|
2539
|
+
const context = isRecord3(invocation.requestContext) ? invocation.requestContext : {};
|
|
2482
2540
|
const key = (typeof context.idempotencyKey === "string" ? context.idempotencyKey : undefined) ?? safeHeaderValue(invocation.request.headers.get(IDEMPOTENCY_KEY_HEADER), 512);
|
|
2483
2541
|
if (!key || !idempotencyKeyPattern.test(key)) {
|
|
2484
2542
|
throw new ApplicationError("A valid Idempotency-Key header is required", {
|
|
@@ -2531,7 +2589,7 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
|
|
|
2531
2589
|
});
|
|
2532
2590
|
}
|
|
2533
2591
|
const adapter = Object.hasOwn(governance.rpc ?? {}, branch.rpc) ? governance.rpc?.[branch.rpc] : undefined;
|
|
2534
|
-
if (!adapter || typeof adapter.execute !== "function" || !
|
|
2592
|
+
if (!adapter || typeof adapter.execute !== "function" || !isRecord3(adapter.capabilities)) {
|
|
2535
2593
|
throw new ApplicationError(`Command "${command.name}" branch "${branch.when}" has no RPC governance adapter`, {
|
|
2536
2594
|
code: "COMMAND_RPC_UNCONFIGURED"
|
|
2537
2595
|
});
|
|
@@ -2574,7 +2632,7 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
|
|
|
2574
2632
|
}
|
|
2575
2633
|
if (command.rpc !== undefined) {
|
|
2576
2634
|
const adapter = Object.hasOwn(governance.rpc ?? {}, command.rpc) ? governance.rpc?.[command.rpc] : undefined;
|
|
2577
|
-
if (!adapter || typeof adapter.execute !== "function" || !
|
|
2635
|
+
if (!adapter || typeof adapter.execute !== "function" || !isRecord3(adapter.capabilities)) {
|
|
2578
2636
|
throw new ApplicationError(`Command "${command.name}" has no RPC governance adapter`, {
|
|
2579
2637
|
code: "COMMAND_RPC_UNCONFIGURED"
|
|
2580
2638
|
});
|
|
@@ -2602,17 +2660,17 @@ function joinPaths(prefix, path) {
|
|
|
2602
2660
|
return joined.length > 1 ? joined.replace(/\/+$/, "") : joined;
|
|
2603
2661
|
}
|
|
2604
2662
|
function controllerInstance(value) {
|
|
2605
|
-
return
|
|
2663
|
+
return isRecord3(value) ? value : undefined;
|
|
2606
2664
|
}
|
|
2607
2665
|
function canUseDirectRoute(compiled, controller, route) {
|
|
2608
2666
|
return route.executionMode === "direct" && route.directInvoker !== undefined && controller.scope === "application" && compiled.createRequestScope === undefined && compiled.aspectPipeline === undefined && (compiled.aspects?.length ?? 0) === 0 && route.aspectPipeline === undefined && (route.aspects?.length ?? 0) === 0 && route.command === undefined && route.effect === undefined && route.responses === undefined;
|
|
2609
2667
|
}
|
|
2610
2668
|
function cookieValues(value) {
|
|
2611
|
-
if (!
|
|
2669
|
+
if (!isRecord3(value))
|
|
2612
2670
|
return;
|
|
2613
2671
|
const result = {};
|
|
2614
2672
|
for (const [name, entry] of Object.entries(value)) {
|
|
2615
|
-
result[name] =
|
|
2673
|
+
result[name] = isRecord3(entry) && "value" in entry ? entry.value : entry;
|
|
2616
2674
|
}
|
|
2617
2675
|
return result;
|
|
2618
2676
|
}
|
|
@@ -2903,7 +2961,7 @@ function createModulePlugin(compiled, services, ctxFactory = defaultRequestConte
|
|
|
2903
2961
|
const handlerCall = () => {
|
|
2904
2962
|
if (route.invoker)
|
|
2905
2963
|
return route.invoker(instance, input);
|
|
2906
|
-
if (!
|
|
2964
|
+
if (!isRecord3(instance))
|
|
2907
2965
|
throw new TypeError("Route controller is not an object");
|
|
2908
2966
|
const method = instance[route.handler];
|
|
2909
2967
|
if (typeof method !== "function")
|
|
@@ -3095,7 +3153,7 @@ function defaultErrorResponse(error, frameworkCode) {
|
|
|
3095
3153
|
}, { status: 400 });
|
|
3096
3154
|
}
|
|
3097
3155
|
if (frameworkCode === "VALIDATION") {
|
|
3098
|
-
if (
|
|
3156
|
+
if (isRecord3(error) && error.type === "response") {
|
|
3099
3157
|
return Response.json({
|
|
3100
3158
|
ok: false,
|
|
3101
3159
|
code: "RESPONSE_VALIDATION_ERROR",
|
|
@@ -3115,13 +3173,13 @@ function defaultErrorResponse(error, frameworkCode) {
|
|
|
3115
3173
|
}, { status: 500 });
|
|
3116
3174
|
}
|
|
3117
3175
|
function isPublicApplicationError(error) {
|
|
3118
|
-
if (!(error instanceof Error) || !
|
|
3176
|
+
if (!(error instanceof Error) || !isRecord3(error))
|
|
3119
3177
|
return false;
|
|
3120
3178
|
return error.expose === true && typeof error.status === "number" && Number.isInteger(error.status) && error.status >= 400 && error.status <= 599 && typeof error.code === "string" && error.code.length > 0;
|
|
3121
3179
|
}
|
|
3122
3180
|
function createApplication(options) {
|
|
3123
3181
|
const pendingWork = new PendingWorkRegistry2(options.pendingWork ? { capacity: options.pendingWork.capacity } : {});
|
|
3124
|
-
const app = new
|
|
3182
|
+
const app = new Elysia4({ name: options.name ?? "supacloud:app", normalize: options.normalize ?? true }).use(contextPlugin(options.http).as("global"));
|
|
3125
3183
|
if (options.documentation)
|
|
3126
3184
|
app.use(createDocumentationPlugin(options.documentation));
|
|
3127
3185
|
const ctxFactory = options.requestContext === undefined ? defaultRequestContext : (() => {
|
|
@@ -3269,6 +3327,7 @@ export {
|
|
|
3269
3327
|
createTestApp,
|
|
3270
3328
|
createTransactionalCommandGovernance,
|
|
3271
3329
|
createWorker,
|
|
3330
|
+
createZenStackReadPlugin,
|
|
3272
3331
|
defaultErrorResponse,
|
|
3273
3332
|
defineElysiaRoute,
|
|
3274
3333
|
defineJsonContract,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Elysia } from "elysia";
|
|
2
|
+
import type { TSchema } from "typebox";
|
|
3
|
+
import { type RouteContractSchemas } from "./schema_contract";
|
|
4
|
+
/** Read operations exposed by the identity-bound ZenStack facade. */
|
|
5
|
+
export type ZenStackReadOperation = "findUnique" | "findUniqueOrThrow" | "findFirst" | "findFirstOrThrow" | "findMany" | "count" | "aggregate" | "groupBy";
|
|
6
|
+
/** Structural facade so @supacloud/elysia does not depend on the DB package at runtime. */
|
|
7
|
+
export type ZenStackReadModels = object;
|
|
8
|
+
export interface ZenStackReadIdentityBinding {
|
|
9
|
+
/** Read-only model facade returned after trusted identity verification. */
|
|
10
|
+
readonly models: ZenStackReadModels;
|
|
11
|
+
/** Identity used to create the facade; available to the argument builder only. */
|
|
12
|
+
readonly identity: unknown;
|
|
13
|
+
}
|
|
14
|
+
export interface ZenStackReadInput {
|
|
15
|
+
readonly request: Request;
|
|
16
|
+
readonly params: Record<string, unknown>;
|
|
17
|
+
readonly query: Record<string, unknown>;
|
|
18
|
+
readonly headers: Record<string, unknown>;
|
|
19
|
+
readonly identity: unknown;
|
|
20
|
+
}
|
|
21
|
+
export interface ZenStackReadResource {
|
|
22
|
+
/** URL path. The path is registered as GET and HEAD only. */
|
|
23
|
+
readonly path: string;
|
|
24
|
+
/** Model key as exposed by ModelReadClient, for example `order`. */
|
|
25
|
+
readonly model: string;
|
|
26
|
+
readonly operation: ZenStackReadOperation;
|
|
27
|
+
/** Optional TypeBox request/response schemas. */
|
|
28
|
+
readonly contract?: RouteContractSchemas & {
|
|
29
|
+
readonly response?: TSchema;
|
|
30
|
+
};
|
|
31
|
+
/** Builds exact ZenStack query args from the decoded request and verified identity. */
|
|
32
|
+
readonly args?: (input: ZenStackReadInput) => unknown;
|
|
33
|
+
}
|
|
34
|
+
export interface ZenStackReadAdapterOptions {
|
|
35
|
+
readonly name?: string;
|
|
36
|
+
readonly resources: readonly ZenStackReadResource[];
|
|
37
|
+
/** Resolve and bind identity for every request. Never accept identity from query/body. */
|
|
38
|
+
readonly bindIdentity: (request: Request) => Promise<ZenStackReadIdentityBinding> | ZenStackReadIdentityBinding;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Mounts explicit, read-only ZenStack resources on native Elysia 2.
|
|
42
|
+
*
|
|
43
|
+
* This adapter intentionally does not use @zenstackhq/server automatic CRUD:
|
|
44
|
+
* the current server package targets Elysia 1.x and its generic CRUD surface
|
|
45
|
+
* would create a second application boundary. Each request binds identity,
|
|
46
|
+
* selects a read-only facade, and calls one allow-listed operation.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createZenStackReadPlugin(options: ZenStackReadAdapterOptions): Elysia;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/elysia",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.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,7 +54,7 @@
|
|
|
54
54
|
"directory": "packages/elysia"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@supacloud/app": "0.
|
|
57
|
+
"@supacloud/app": "0.39.0",
|
|
58
58
|
"@supacloud/commands": "0.12.0",
|
|
59
59
|
"@supacloud/contracts": "0.16.0",
|
|
60
60
|
"effect": "4.0.5",
|
|
@@ -74,9 +74,9 @@
|
|
|
74
74
|
"elysia": "2.0.0-rc.1"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@supacloud/compiler": "0.
|
|
77
|
+
"@supacloud/compiler": "0.58.0",
|
|
78
78
|
"@supacloud/delivery": "0.25.0",
|
|
79
|
-
"@supacloud/db": "0.
|
|
79
|
+
"@supacloud/db": "0.22.0",
|
|
80
80
|
"@types/bun": "^1.4.4",
|
|
81
81
|
"elysia": "2.0.0-rc.1",
|
|
82
82
|
"typescript": "7.1.0-beta"
|