alepha 0.20.3 → 0.20.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/audits/index.d.ts.map +1 -1
- package/dist/api/files/index.d.ts.map +1 -1
- package/dist/api/jobs/index.d.ts +14 -14
- package/dist/api/jobs/index.d.ts.map +1 -1
- package/dist/api/organizations/index.d.ts.map +1 -1
- package/dist/api/parameters/index.d.ts +6 -1
- package/dist/api/parameters/index.d.ts.map +1 -1
- package/dist/api/parameters/index.js +20 -4
- package/dist/api/parameters/index.js.map +1 -1
- package/dist/api/payments/index.d.ts.map +1 -1
- package/dist/api/users/index.browser.js +6 -0
- package/dist/api/users/index.browser.js.map +1 -1
- package/dist/api/users/index.d.ts +5032 -134
- package/dist/api/users/index.d.ts.map +1 -1
- package/dist/api/users/index.js +58 -10
- package/dist/api/users/index.js.map +1 -1
- package/dist/bin/index.js +0 -0
- package/dist/bucket/index.d.ts +77 -107
- package/dist/bucket/index.d.ts.map +1 -1
- package/dist/bucket/index.js +148 -4
- package/dist/bucket/index.js.map +1 -1
- package/dist/bucket/index.workerd.js +7 -1
- package/dist/bucket/index.workerd.js.map +1 -1
- package/dist/cache/core/index.d.ts +26 -0
- package/dist/cache/core/index.d.ts.map +1 -1
- package/dist/cache/core/index.js +11 -1
- package/dist/cache/core/index.js.map +1 -1
- package/dist/cache/core/index.workerd.js +11 -1
- package/dist/cache/core/index.workerd.js.map +1 -1
- package/dist/cli/config/index.d.ts +7 -5
- package/dist/cli/config/index.d.ts.map +1 -1
- package/dist/cli/config/index.js +2 -3
- package/dist/cli/config/index.js.map +1 -1
- package/dist/cli/core/index.d.ts +419 -12
- package/dist/cli/core/index.d.ts.map +1 -1
- package/dist/cli/core/index.js +22 -511
- package/dist/cli/core/index.js.map +1 -1
- package/dist/cli/devtools/index.d.ts +4 -8
- package/dist/cli/devtools/index.d.ts.map +1 -1
- package/dist/cli/devtools/index.js +13 -15
- package/dist/cli/devtools/index.js.map +1 -1
- package/dist/cli/platform/index.d.ts +10 -13
- package/dist/cli/platform/index.d.ts.map +1 -1
- package/dist/cli/platform/index.js +18 -15
- package/dist/cli/platform/index.js.map +1 -1
- package/dist/cli/vendor/index.d.ts +10 -13
- package/dist/cli/vendor/index.d.ts.map +1 -1
- package/dist/cli/vendor/index.js +16 -13
- package/dist/cli/vendor/index.js.map +1 -1
- package/dist/command/index.d.ts +1 -1
- package/dist/core/index.browser.js +27 -3
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +6 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +27 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +27 -3
- package/dist/core/index.native.js.map +1 -1
- package/dist/core/index.workerd.js +27 -3
- package/dist/core/index.workerd.js.map +1 -1
- package/dist/datetime/index.d.ts +69 -10
- package/dist/datetime/index.d.ts.map +1 -1
- package/dist/datetime/index.js +135 -13
- package/dist/datetime/index.js.map +1 -1
- package/dist/email/smtp/index.js +10636 -2
- package/dist/email/smtp/index.js.map +1 -1
- package/dist/fake/index.d.ts +8085 -4
- package/dist/fake/index.d.ts.map +1 -1
- package/dist/fake/index.js +33554 -3
- package/dist/fake/index.js.map +1 -1
- package/dist/lock/core/index.d.ts +30 -2
- package/dist/lock/core/index.d.ts.map +1 -1
- package/dist/lock/core/index.js +35 -12
- package/dist/lock/core/index.js.map +1 -1
- package/dist/mcp/index.d.ts +238 -31
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +198 -71
- package/dist/mcp/index.js.map +1 -1
- package/dist/orm/core/index.browser.js +1 -1
- package/dist/orm/core/index.browser.js.map +1 -1
- package/dist/orm/core/index.bun.js +4 -3
- package/dist/orm/core/index.bun.js.map +1 -1
- package/dist/orm/core/index.d.ts +4877 -9
- package/dist/orm/core/index.d.ts.map +1 -1
- package/dist/orm/core/index.js +4 -3
- package/dist/orm/core/index.js.map +1 -1
- package/dist/orm/postgres/index.d.ts +608 -1
- package/dist/orm/postgres/index.d.ts.map +1 -1
- package/dist/react/core/index.d.ts +102 -1
- package/dist/react/core/index.d.ts.map +1 -1
- package/dist/react/core/index.js +65 -1
- package/dist/react/core/index.js.map +1 -1
- package/dist/react/form/index.d.ts +6 -0
- package/dist/react/form/index.d.ts.map +1 -1
- package/dist/react/form/index.js +7 -7
- package/dist/react/form/index.js.map +1 -1
- package/dist/react/i18n/index.d.ts +7 -1
- package/dist/react/i18n/index.d.ts.map +1 -1
- package/dist/react/i18n/index.js +6 -0
- package/dist/react/i18n/index.js.map +1 -1
- package/dist/react/router/index.browser.js +20 -2
- package/dist/react/router/index.browser.js.map +1 -1
- package/dist/react/router/index.d.ts +36 -4
- package/dist/react/router/index.d.ts.map +1 -1
- package/dist/react/router/index.js +20 -2
- package/dist/react/router/index.js.map +1 -1
- package/dist/react/testing/chunk-6Ep1yQYe.js +16 -0
- package/dist/react/testing/index.d.ts +411 -1
- package/dist/react/testing/index.d.ts.map +1 -1
- package/dist/react/testing/index.js +12293 -13
- package/dist/react/testing/index.js.map +1 -1
- package/dist/react/ui/index.d.ts +195 -1
- package/dist/react/ui/index.d.ts.map +1 -1
- package/dist/react/ui/index.js +61 -1
- package/dist/react/ui/index.js.map +1 -1
- package/dist/scheduler/index.d.ts +84 -3
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/scheduler/index.js +390 -1
- package/dist/scheduler/index.js.map +1 -1
- package/dist/scheduler/index.workerd.js +390 -1
- package/dist/scheduler/index.workerd.js.map +1 -1
- package/dist/security/index.d.ts +325 -2
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/index.js +1361 -2
- package/dist/security/index.js.map +1 -1
- package/dist/server/auth/index.d.ts +1054 -1
- package/dist/server/auth/index.d.ts.map +1 -1
- package/dist/server/auth/index.js +1223 -1
- package/dist/server/auth/index.js.map +1 -1
- package/dist/server/core/index.browser.js +10 -3
- package/dist/server/core/index.browser.js.map +1 -1
- package/dist/server/core/index.d.ts.map +1 -1
- package/dist/server/core/index.js +28 -5
- package/dist/server/core/index.js.map +1 -1
- package/dist/server/metrics/index.d.ts +514 -1
- package/dist/server/metrics/index.d.ts.map +1 -1
- package/dist/server/metrics/index.js +4374 -4
- package/dist/server/metrics/index.js.map +1 -1
- package/dist/server/swagger/index.d.ts.map +1 -1
- package/dist/server/swagger/index.js +3 -4
- package/dist/server/swagger/index.js.map +1 -1
- package/dist/websocket/index.browser.js +11 -5
- package/dist/websocket/index.browser.js.map +1 -1
- package/dist/websocket/index.d.ts +3 -1
- package/dist/websocket/index.d.ts.map +1 -1
- package/dist/websocket/index.js +21 -6
- package/dist/websocket/index.js.map +1 -1
- package/package.json +416 -8
- package/src/api/parameters/services/ParameterProvider.ts +21 -4
- package/src/api/users/__tests__/SessionService.spec.ts +99 -0
- package/src/api/users/__tests__/UserJobs.spec.ts +67 -0
- package/src/api/users/atoms/realmAuthSettingsAtom.ts +15 -0
- package/src/api/users/entities/sessions.ts +6 -0
- package/src/api/users/jobs/UserJobs.ts +44 -17
- package/src/api/users/providers/RealmProvider.ts +4 -0
- package/src/api/users/services/SessionService.ts +27 -0
- package/src/bucket/__tests__/NodeS3BucketProvider.spec.ts +74 -0
- package/src/bucket/index.ts +19 -2
- package/src/bucket/primitives/$bucket.ts +9 -1
- package/src/bucket/providers/CloudflareR2Provider.ts +2 -137
- package/src/bucket/providers/NodeS3BucketProvider.ts +218 -0
- package/src/cache/core/index.ts +29 -0
- package/src/cache/core/primitives/$cache.ts +14 -1
- package/src/cli/config/defineConfig.ts +13 -15
- package/src/cli/core/__tests__/init.spec.ts +6 -7
- package/src/cli/core/services/ProjectScaffolder.ts +18 -14
- package/src/cli/core/tasks/BuildCloudflareTask.ts +5 -0
- package/src/cli/core/templates/agentMd.ts +2 -10
- package/src/cli/core/templates/saasAdminLayoutTsx.ts +3 -3
- package/src/cli/devtools/index.ts +12 -26
- package/src/cli/platform/index.ts +15 -24
- package/src/cli/vendor/atoms/vendorOptions.ts +1 -1
- package/src/cli/vendor/index.ts +14 -23
- package/src/core/Alepha.ts +11 -1
- package/src/core/helpers/ref.ts +18 -0
- package/src/core/index.shared.ts +1 -0
- package/src/core/providers/SchemaValidator.ts +9 -1
- package/src/core/providers/TypeProvider.ts +1 -2
- package/src/datetime/REFACTORING.md +118 -0
- package/src/datetime/providers/DateTimeProvider.ts +203 -24
- package/src/lock/core/index.ts +31 -0
- package/src/lock/core/primitives/$lock.ts +14 -1
- package/src/mcp/__tests__/jsonrpc.spec.ts +1 -1
- package/src/mcp/helpers/jsonrpc.ts +26 -1
- package/src/mcp/index.ts +10 -5
- package/src/mcp/interfaces/McpTypes.ts +83 -6
- package/src/mcp/primitives/$prompt.ts +18 -1
- package/src/mcp/primitives/$resource.ts +18 -1
- package/src/mcp/primitives/$tool.ts +83 -7
- package/src/mcp/providers/McpServerProvider.ts +74 -16
- package/src/mcp/transports/StreamableHttpMcpTransport.ts +226 -0
- package/src/orm/REFACTORING.md +330 -0
- package/src/orm/core/primitives/$transactional.ts +11 -0
- package/src/orm/core/schemas/updateSchema.ts +1 -1
- package/src/orm/core/services/PgRelationManager.ts +4 -2
- package/src/react/core/__tests__/useQuery.browser.spec.tsx +86 -0
- package/src/react/core/hooks/useQuery.ts +153 -0
- package/src/react/core/index.ts +1 -0
- package/src/react/form/services/FormModel.ts +15 -6
- package/src/react/form/services/parseField.ts +8 -0
- package/src/react/i18n/providers/I18nProvider.ts +8 -2
- package/src/react/router/__tests__/$page.spec.tsx +0 -16
- package/src/react/router/__tests__/ssr.spec.tsx +339 -0
- package/src/react/router/primitives/$page.ts +28 -4
- package/src/react/router/providers/ReactPageProvider.ts +27 -9
- package/src/react/ui/atoms/uiThemeListAtom.ts +36 -0
- package/src/react/ui/index.ts +6 -0
- package/src/react/ui/services/SchemaControl.ts +209 -0
- package/src/security/primitives/$issuer.ts +6 -3
- package/src/server/core/__tests__/ServerRouterProvider-serializationError.spec.ts +75 -0
- package/src/server/core/__tests__/ServerRouterProvider-validationError.spec.ts +306 -0
- package/src/server/core/errors/ValidationError.ts +13 -1
- package/src/server/core/primitives/$action.ts +16 -5
- package/src/server/core/providers/ServerRouterProvider.ts +26 -4
- package/src/server/swagger/providers/ServerSwaggerProvider.ts +5 -7
- package/src/websocket/providers/NodeWebSocketServerProvider.ts +10 -4
- package/src/websocket/services/WebSocketClient.ts +11 -5
- package/src/mcp/transports/SseMcpTransport.ts +0 -182
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { Alepha, t } from "alepha";
|
|
2
|
+
import { describe, it } from "vitest";
|
|
3
|
+
import { $route, ServerProvider } from "../index.ts";
|
|
4
|
+
|
|
5
|
+
class TestApp {
|
|
6
|
+
createUser = $route({
|
|
7
|
+
method: "POST",
|
|
8
|
+
path: "/users",
|
|
9
|
+
schema: {
|
|
10
|
+
body: t.object({
|
|
11
|
+
name: t.text(),
|
|
12
|
+
age: t.integer(),
|
|
13
|
+
}),
|
|
14
|
+
},
|
|
15
|
+
handler: () => {},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
getUser = $route({
|
|
19
|
+
method: "GET",
|
|
20
|
+
path: "/users/:id",
|
|
21
|
+
schema: {
|
|
22
|
+
params: t.object({
|
|
23
|
+
id: t.integer(),
|
|
24
|
+
}),
|
|
25
|
+
},
|
|
26
|
+
handler: () => {},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
searchUsers = $route({
|
|
30
|
+
method: "GET",
|
|
31
|
+
path: "/users",
|
|
32
|
+
schema: {
|
|
33
|
+
query: t.object({
|
|
34
|
+
limit: t.integer({ minimum: 1, maximum: 100 }),
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
handler: () => {},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
protectedRoute = $route({
|
|
41
|
+
method: "GET",
|
|
42
|
+
path: "/protected",
|
|
43
|
+
schema: {
|
|
44
|
+
headers: t.object({
|
|
45
|
+
"x-api-version": t.integer(),
|
|
46
|
+
}),
|
|
47
|
+
},
|
|
48
|
+
handler: () => {},
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fetchAndStop = async (
|
|
53
|
+
alepha: Alepha,
|
|
54
|
+
build: (hostname: string) => { url: string; init?: RequestInit },
|
|
55
|
+
) => {
|
|
56
|
+
await alepha.start();
|
|
57
|
+
const hostname = alepha.inject(ServerProvider).hostname;
|
|
58
|
+
const { url, init } = build(hostname);
|
|
59
|
+
const response = await fetch(url, init);
|
|
60
|
+
const json = await response.json();
|
|
61
|
+
await alepha.stop();
|
|
62
|
+
return { response, json };
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
describe("ServerRouterProvider - request validation error", () => {
|
|
66
|
+
describe("body validation", () => {
|
|
67
|
+
it("should expose a wrong-type rejection (expected integer, got string)", async ({
|
|
68
|
+
expect,
|
|
69
|
+
}) => {
|
|
70
|
+
const alepha = Alepha.create({
|
|
71
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
72
|
+
}).with(TestApp);
|
|
73
|
+
|
|
74
|
+
const { response, json } = await fetchAndStop(alepha, (host) => ({
|
|
75
|
+
url: `${host}/users`,
|
|
76
|
+
init: {
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: { "content-type": "application/json" },
|
|
79
|
+
body: JSON.stringify({ name: "John", age: "not-a-number" }),
|
|
80
|
+
},
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
expect(response.status).toBe(400);
|
|
84
|
+
expect(json.error).toBe("ValidationError");
|
|
85
|
+
expect(json.message).toMatch(/^Invalid request body:/);
|
|
86
|
+
expect(json.message.toLowerCase()).toMatch(/integer|expected/);
|
|
87
|
+
// path locates the offending field
|
|
88
|
+
expect(json.details).toBe("/age");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should expose a missing-field rejection (required property `age`)", async ({
|
|
92
|
+
expect,
|
|
93
|
+
}) => {
|
|
94
|
+
const alepha = Alepha.create({
|
|
95
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
96
|
+
}).with(TestApp);
|
|
97
|
+
|
|
98
|
+
const { response, json } = await fetchAndStop(alepha, (host) => ({
|
|
99
|
+
url: `${host}/users`,
|
|
100
|
+
init: {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: { "content-type": "application/json" },
|
|
103
|
+
body: JSON.stringify({ name: "John" }),
|
|
104
|
+
},
|
|
105
|
+
}));
|
|
106
|
+
|
|
107
|
+
expect(response.status).toBe(400);
|
|
108
|
+
expect(json.error).toBe("ValidationError");
|
|
109
|
+
expect(json.message).toMatch(/^Invalid request body:/);
|
|
110
|
+
// message must name the missing field
|
|
111
|
+
expect(json.message).toContain("age");
|
|
112
|
+
expect(json.message.toLowerCase()).toMatch(/required/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should behave identically in development mode", async ({ expect }) => {
|
|
116
|
+
const alepha = Alepha.create().with(TestApp);
|
|
117
|
+
|
|
118
|
+
const { response, json } = await fetchAndStop(alepha, (host) => ({
|
|
119
|
+
url: `${host}/users`,
|
|
120
|
+
init: {
|
|
121
|
+
method: "POST",
|
|
122
|
+
headers: { "content-type": "application/json" },
|
|
123
|
+
body: JSON.stringify({ name: "John" }),
|
|
124
|
+
},
|
|
125
|
+
}));
|
|
126
|
+
|
|
127
|
+
expect(response.status).toBe(400);
|
|
128
|
+
expect(json.error).toBe("ValidationError");
|
|
129
|
+
expect(json.message).toMatch(/^Invalid request body:/);
|
|
130
|
+
expect(json.message.toLowerCase()).toMatch(/age|required/);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("params validation", () => {
|
|
135
|
+
it("should expose the rejection reason in production", async ({
|
|
136
|
+
expect,
|
|
137
|
+
}) => {
|
|
138
|
+
const alepha = Alepha.create({
|
|
139
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
140
|
+
}).with(TestApp);
|
|
141
|
+
|
|
142
|
+
const { response, json } = await fetchAndStop(alepha, (host) => ({
|
|
143
|
+
url: `${host}/users/not-a-number`,
|
|
144
|
+
}));
|
|
145
|
+
|
|
146
|
+
expect(response.status).toBe(400);
|
|
147
|
+
expect(json.error).toBe("ValidationError");
|
|
148
|
+
expect(json.message).toMatch(/^Invalid request params:/);
|
|
149
|
+
expect(json.message.toLowerCase()).toMatch(/integer|number|expected/);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("query validation", () => {
|
|
154
|
+
it("should expose the rejection reason in production", async ({
|
|
155
|
+
expect,
|
|
156
|
+
}) => {
|
|
157
|
+
const alepha = Alepha.create({
|
|
158
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
159
|
+
}).with(TestApp);
|
|
160
|
+
|
|
161
|
+
const { response, json } = await fetchAndStop(alepha, (host) => ({
|
|
162
|
+
url: `${host}/users?limit=9999`,
|
|
163
|
+
}));
|
|
164
|
+
|
|
165
|
+
expect(response.status).toBe(400);
|
|
166
|
+
expect(json.error).toBe("ValidationError");
|
|
167
|
+
expect(json.message).toMatch(/^Invalid request query:/);
|
|
168
|
+
expect(json.message.toLowerCase()).toMatch(/maximum|less|<=|100/);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("header validation", () => {
|
|
173
|
+
it("should expose the rejection reason in production", async ({
|
|
174
|
+
expect,
|
|
175
|
+
}) => {
|
|
176
|
+
const alepha = Alepha.create({
|
|
177
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
178
|
+
}).with(TestApp);
|
|
179
|
+
|
|
180
|
+
const { response, json } = await fetchAndStop(alepha, (host) => ({
|
|
181
|
+
url: `${host}/protected`,
|
|
182
|
+
// omit required header
|
|
183
|
+
}));
|
|
184
|
+
|
|
185
|
+
expect(response.status).toBe(400);
|
|
186
|
+
expect(json.error).toBe("ValidationError");
|
|
187
|
+
expect(json.message).toMatch(/^Invalid request header:/);
|
|
188
|
+
expect(json.message.toLowerCase()).toMatch(/x-api-version|required/);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("should coerce string header values to declared schema types", async ({
|
|
192
|
+
expect,
|
|
193
|
+
}) => {
|
|
194
|
+
const seen: { version?: unknown } = {};
|
|
195
|
+
class CoerceApp {
|
|
196
|
+
captured = $route({
|
|
197
|
+
method: "GET",
|
|
198
|
+
path: "/coerce",
|
|
199
|
+
schema: {
|
|
200
|
+
headers: t.object({
|
|
201
|
+
"x-api-version": t.integer(),
|
|
202
|
+
}),
|
|
203
|
+
},
|
|
204
|
+
handler: ({ headers }) => {
|
|
205
|
+
seen.version = headers["x-api-version"];
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const alepha = Alepha.create({
|
|
211
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
212
|
+
}).with(CoerceApp);
|
|
213
|
+
|
|
214
|
+
await alepha.start();
|
|
215
|
+
const host = alepha.inject(ServerProvider).hostname;
|
|
216
|
+
const response = await fetch(`${host}/coerce`, {
|
|
217
|
+
headers: { "x-api-version": "42" },
|
|
218
|
+
});
|
|
219
|
+
await alepha.stop();
|
|
220
|
+
|
|
221
|
+
expect([200, 204]).toContain(response.status);
|
|
222
|
+
expect(seen.version).toBe(42);
|
|
223
|
+
expect(typeof seen.version).toBe("number");
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("should preserve undeclared headers (auth, user-agent, ...)", async ({
|
|
227
|
+
expect,
|
|
228
|
+
}) => {
|
|
229
|
+
const seen: { authorization?: string; userAgent?: string } = {};
|
|
230
|
+
class PreserveApp {
|
|
231
|
+
check = $route({
|
|
232
|
+
method: "GET",
|
|
233
|
+
path: "/preserve",
|
|
234
|
+
schema: {
|
|
235
|
+
headers: t.object({
|
|
236
|
+
"x-api-version": t.integer(),
|
|
237
|
+
}),
|
|
238
|
+
},
|
|
239
|
+
handler: ({ headers }) => {
|
|
240
|
+
const all = headers as unknown as Record<string, string>;
|
|
241
|
+
seen.authorization = all.authorization;
|
|
242
|
+
seen.userAgent = all["user-agent"];
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const alepha = Alepha.create({
|
|
248
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
249
|
+
}).with(PreserveApp);
|
|
250
|
+
|
|
251
|
+
await alepha.start();
|
|
252
|
+
const host = alepha.inject(ServerProvider).hostname;
|
|
253
|
+
const response = await fetch(`${host}/preserve`, {
|
|
254
|
+
headers: {
|
|
255
|
+
"x-api-version": "1",
|
|
256
|
+
authorization: "Bearer abc.def.ghi",
|
|
257
|
+
"user-agent": "MyClient/1.0",
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
await alepha.stop();
|
|
261
|
+
|
|
262
|
+
expect([200, 204]).toContain(response.status);
|
|
263
|
+
expect(seen.authorization).toBe("Bearer abc.def.ghi");
|
|
264
|
+
expect(seen.userAgent).toBe("MyClient/1.0");
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("should accept schema keys regardless of case (lowercase normalization)", async ({
|
|
268
|
+
expect,
|
|
269
|
+
}) => {
|
|
270
|
+
const seen: { version?: unknown } = {};
|
|
271
|
+
class CaseApp {
|
|
272
|
+
// Schema declares keys with mixed case — Node lowercases incoming
|
|
273
|
+
// header names, so the framework must lowercase schema keys when
|
|
274
|
+
// matching values.
|
|
275
|
+
check = $route({
|
|
276
|
+
method: "GET",
|
|
277
|
+
path: "/case",
|
|
278
|
+
schema: {
|
|
279
|
+
headers: t.object({
|
|
280
|
+
"X-Api-Version": t.integer(),
|
|
281
|
+
}),
|
|
282
|
+
},
|
|
283
|
+
handler: ({ headers }) => {
|
|
284
|
+
seen.version = (headers as Record<string, unknown>)[
|
|
285
|
+
"x-api-version"
|
|
286
|
+
];
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const alepha = Alepha.create({
|
|
292
|
+
env: { NODE_ENV: "production", SERVER_PORT: 0 },
|
|
293
|
+
}).with(CaseApp);
|
|
294
|
+
|
|
295
|
+
await alepha.start();
|
|
296
|
+
const host = alepha.inject(ServerProvider).hostname;
|
|
297
|
+
const response = await fetch(`${host}/case`, {
|
|
298
|
+
headers: { "x-api-version": "7" },
|
|
299
|
+
});
|
|
300
|
+
await alepha.stop();
|
|
301
|
+
|
|
302
|
+
expect([200, 204]).toContain(response.status);
|
|
303
|
+
expect(seen.version).toBe(7);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
});
|
|
@@ -1,11 +1,23 @@
|
|
|
1
|
+
import { TypeBoxError } from "alepha";
|
|
1
2
|
import { HttpError } from "./HttpError.ts";
|
|
2
3
|
|
|
3
4
|
export class ValidationError extends HttpError {
|
|
4
5
|
constructor(message = "Validation has failed", cause?: unknown) {
|
|
6
|
+
let fullMessage = message;
|
|
7
|
+
let details: string | undefined;
|
|
8
|
+
|
|
9
|
+
if (cause instanceof TypeBoxError) {
|
|
10
|
+
fullMessage = `${message}: ${cause.cause.message}`;
|
|
11
|
+
if (cause.cause.instancePath) {
|
|
12
|
+
details = cause.cause.instancePath;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
5
16
|
super(
|
|
6
17
|
{
|
|
7
|
-
message,
|
|
18
|
+
message: fullMessage,
|
|
8
19
|
status: 400,
|
|
20
|
+
details,
|
|
9
21
|
},
|
|
10
22
|
cause,
|
|
11
23
|
);
|
|
@@ -256,7 +256,7 @@ export class ActionPrimitive<
|
|
|
256
256
|
|
|
257
257
|
public get route(): ServerRoute {
|
|
258
258
|
return {
|
|
259
|
-
...
|
|
259
|
+
...this.options,
|
|
260
260
|
method: this.method,
|
|
261
261
|
path: `${this.prefix}${this.path}`,
|
|
262
262
|
handler: this.handler,
|
|
@@ -399,10 +399,21 @@ export class ActionPrimitive<
|
|
|
399
399
|
}
|
|
400
400
|
|
|
401
401
|
if (serverActionRequest.headers && this.options.schema?.headers) {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
402
|
+
// Per-key encode (matches the server-side decode pattern in
|
|
403
|
+
// ServerRouterProvider.validateRequest): coerces declared headers via
|
|
404
|
+
// the schema, leaves undeclared ones untouched. Schema keys are
|
|
405
|
+
// lowercased to match Node's incoming header convention.
|
|
406
|
+
const schemaHeaders = this.options.schema.headers;
|
|
407
|
+
const headers = serverActionRequest.headers as Record<string, unknown>;
|
|
408
|
+
for (const key of Object.keys(schemaHeaders.properties)) {
|
|
409
|
+
const lcKey = key.toLowerCase();
|
|
410
|
+
if (headers[lcKey] !== undefined) {
|
|
411
|
+
headers[lcKey] = this.alepha.codec.encode(
|
|
412
|
+
schemaHeaders.properties[key],
|
|
413
|
+
headers[lcKey],
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
406
417
|
}
|
|
407
418
|
|
|
408
419
|
if (serverActionRequest.body && this.options.schema?.body) {
|
|
@@ -484,10 +484,32 @@ export class ServerRouterProvider extends RouterProvider<ServerRouteMatcher> {
|
|
|
484
484
|
|
|
485
485
|
if (route.schema?.headers) {
|
|
486
486
|
try {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
)
|
|
487
|
+
const schemaHeaders = route.schema.headers;
|
|
488
|
+
|
|
489
|
+
// Per-key decode (mirrors `query` handling): coerces declared header
|
|
490
|
+
// values from strings to their schema types (int/bool/date). Then
|
|
491
|
+
// validate the decoded subset against the full schema so TypeBox
|
|
492
|
+
// produces consistent error messages (missing-required, type
|
|
493
|
+
// mismatch). Finally merge the decoded values back into
|
|
494
|
+
// `request.headers` so undeclared headers (auth, cookie, user-agent,
|
|
495
|
+
// ...) survive the validation step intact.
|
|
496
|
+
const decoded: Record<string, unknown> = {};
|
|
497
|
+
for (const key of Object.keys(schemaHeaders.properties)) {
|
|
498
|
+
const lcKey = key.toLowerCase();
|
|
499
|
+
const value = request.headers[lcKey];
|
|
500
|
+
if (value == null) continue;
|
|
501
|
+
decoded[key] = this.alepha.codec.decode(
|
|
502
|
+
schemaHeaders.properties[key],
|
|
503
|
+
value,
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
this.alepha.codec.validate(schemaHeaders, decoded);
|
|
508
|
+
|
|
509
|
+
for (const [key, value] of Object.entries(decoded)) {
|
|
510
|
+
(request.headers as Record<string, unknown>)[key.toLowerCase()] =
|
|
511
|
+
value;
|
|
512
|
+
}
|
|
491
513
|
} catch (error) {
|
|
492
514
|
throw new ValidationError("Invalid request header", error);
|
|
493
515
|
}
|
|
@@ -201,14 +201,12 @@ export class ServerSwaggerProvider {
|
|
|
201
201
|
hasSecurity = true;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
const g = t.raw;
|
|
205
|
-
|
|
206
204
|
if (
|
|
207
|
-
|
|
208
|
-
|
|
205
|
+
t.schema.isObject(route.options.schema.body) ||
|
|
206
|
+
t.schema.isArray(route.options.schema.body)
|
|
209
207
|
) {
|
|
210
208
|
if (
|
|
211
|
-
|
|
209
|
+
t.schema.isObject(route.options.schema.body) &&
|
|
212
210
|
this.isBodyMultipart(route.options.schema.body)
|
|
213
211
|
) {
|
|
214
212
|
operation.requestBody = {
|
|
@@ -231,7 +229,7 @@ export class ServerSwaggerProvider {
|
|
|
231
229
|
}
|
|
232
230
|
}
|
|
233
231
|
|
|
234
|
-
if (
|
|
232
|
+
if (t.schema.isObject(route.options.schema.query)) {
|
|
235
233
|
operation.parameters ??= [];
|
|
236
234
|
const requiredKeys: string[] =
|
|
237
235
|
route.options.schema.query.required ?? [];
|
|
@@ -250,7 +248,7 @@ export class ServerSwaggerProvider {
|
|
|
250
248
|
}
|
|
251
249
|
}
|
|
252
250
|
|
|
253
|
-
if (
|
|
251
|
+
if (t.schema.isObject(route.options.schema.params)) {
|
|
254
252
|
operation.parameters ??= [];
|
|
255
253
|
for (const [key, value] of Object.entries(
|
|
256
254
|
route.options.schema.params.properties,
|
|
@@ -6,9 +6,9 @@ import {
|
|
|
6
6
|
$state,
|
|
7
7
|
Alepha,
|
|
8
8
|
AlephaError,
|
|
9
|
+
SchemaValidator,
|
|
9
10
|
type Static,
|
|
10
11
|
t,
|
|
11
|
-
Value,
|
|
12
12
|
} from "alepha";
|
|
13
13
|
import { $logger } from "alepha/logger";
|
|
14
14
|
import { WebSocket, WebSocketServer } from "ws";
|
|
@@ -495,6 +495,7 @@ export class NodeWebSocketServerProvider extends WebSocketServerProvider {
|
|
|
495
495
|
|
|
496
496
|
export class NodeWebSocketConnection implements WebSocketConnection {
|
|
497
497
|
protected readonly log = $logger();
|
|
498
|
+
protected readonly schemaValidator = $inject(SchemaValidator);
|
|
498
499
|
public metadata?: Record<string, any>;
|
|
499
500
|
|
|
500
501
|
constructor(
|
|
@@ -556,10 +557,15 @@ export class NodeWebSocketConnection implements WebSocketConnection {
|
|
|
556
557
|
|
|
557
558
|
// Validate message against schema (out = client→server)
|
|
558
559
|
const outSchema = this.endpoint.channel.options.schema.out;
|
|
559
|
-
|
|
560
|
-
|
|
560
|
+
try {
|
|
561
|
+
this.schemaValidator.validate(outSchema, message, {
|
|
562
|
+
trim: false,
|
|
563
|
+
nullToUndefined: false,
|
|
564
|
+
deleteUndefined: false,
|
|
565
|
+
});
|
|
566
|
+
} catch (err) {
|
|
561
567
|
throw new WebSocketValidationError(
|
|
562
|
-
`Message validation failed: ${
|
|
568
|
+
`Message validation failed: ${(err as Error).message}`,
|
|
563
569
|
);
|
|
564
570
|
}
|
|
565
571
|
|
|
@@ -3,9 +3,9 @@ import {
|
|
|
3
3
|
$inject,
|
|
4
4
|
Alepha,
|
|
5
5
|
AlephaError,
|
|
6
|
+
SchemaValidator,
|
|
6
7
|
type Static,
|
|
7
8
|
t,
|
|
8
|
-
Value,
|
|
9
9
|
} from "alepha";
|
|
10
10
|
import { $logger } from "alepha/logger";
|
|
11
11
|
import type { ChannelPrimitive, TWSObject } from "../primitives/$channel.ts";
|
|
@@ -50,6 +50,7 @@ export class WebSocketChannelConnection<
|
|
|
50
50
|
TServer extends TWSObject,
|
|
51
51
|
> {
|
|
52
52
|
protected readonly alepha = $inject(Alepha);
|
|
53
|
+
protected readonly schemaValidator = $inject(SchemaValidator);
|
|
53
54
|
protected readonly log = $logger();
|
|
54
55
|
protected ws?: WebSocket;
|
|
55
56
|
protected reconnectAttempts = 0;
|
|
@@ -346,11 +347,16 @@ export class WebSocketChannelConnection<
|
|
|
346
347
|
|
|
347
348
|
// Validate outgoing message against schema
|
|
348
349
|
const outSchema = this.channel.options.schema.out;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
350
|
+
try {
|
|
351
|
+
this.schemaValidator.validate(outSchema, message, {
|
|
352
|
+
trim: false,
|
|
353
|
+
nullToUndefined: false,
|
|
354
|
+
deleteUndefined: false,
|
|
355
|
+
});
|
|
356
|
+
} catch (err) {
|
|
357
|
+
this.log.warn("Message validation failed", { error: err });
|
|
352
358
|
throw new AlephaError(
|
|
353
|
-
`Message validation failed: ${
|
|
359
|
+
`Message validation failed: ${(err as Error).message}`,
|
|
354
360
|
);
|
|
355
361
|
}
|
|
356
362
|
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { $atom, $inject, $state, t } from "alepha";
|
|
2
|
-
import { $logger } from "alepha/logger";
|
|
3
|
-
import { $route } from "alepha/server";
|
|
4
|
-
import {
|
|
5
|
-
createErrorResponse,
|
|
6
|
-
createNotification,
|
|
7
|
-
createParseError,
|
|
8
|
-
JsonRpcParseError,
|
|
9
|
-
parseMessage,
|
|
10
|
-
} from "../helpers/jsonrpc.ts";
|
|
11
|
-
import type { McpContext } from "../interfaces/McpTypes.ts";
|
|
12
|
-
import { McpServerProvider } from "../providers/McpServerProvider.ts";
|
|
13
|
-
|
|
14
|
-
// ---------------------------------------------------------------------------------------------------------------------
|
|
15
|
-
|
|
16
|
-
export const mcpSseOptions = $atom({
|
|
17
|
-
name: "alepha.mcp.sse.options",
|
|
18
|
-
description: "Configuration options for the MCP SSE transport.",
|
|
19
|
-
schema: t.object({
|
|
20
|
-
/**
|
|
21
|
-
* Path for the MCP SSE endpoint.
|
|
22
|
-
*/
|
|
23
|
-
path: t.text({ default: "/mcp" }),
|
|
24
|
-
}),
|
|
25
|
-
default: {
|
|
26
|
-
path: "/mcp",
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
// ---------------------------------------------------------------------------------------------------------------------
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* SSE (Server-Sent Events) transport for MCP communication.
|
|
34
|
-
*
|
|
35
|
-
* This transport uses HTTP with SSE for server-to-client messages
|
|
36
|
-
* and POST requests for client-to-server messages.
|
|
37
|
-
*
|
|
38
|
-
* Endpoints:
|
|
39
|
-
* - GET /mcp - SSE stream for server events
|
|
40
|
-
* - POST /mcp - JSON-RPC request endpoint
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* import { Alepha, run } from "alepha";
|
|
45
|
-
* import { AlephaServer } from "alepha/server";
|
|
46
|
-
* import { AlephaMcp, AlephaMcpSse } from "alepha/mcp";
|
|
47
|
-
*
|
|
48
|
-
* class MyTools {
|
|
49
|
-
* // ... tool definitions
|
|
50
|
-
* }
|
|
51
|
-
*
|
|
52
|
-
* run(
|
|
53
|
-
* Alepha.create()
|
|
54
|
-
* .with(AlephaServer)
|
|
55
|
-
* .with(AlephaMcp)
|
|
56
|
-
* .with(AlephaMcpSse)
|
|
57
|
-
* .with(MyTools)
|
|
58
|
-
* );
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export class SseMcpTransport {
|
|
62
|
-
protected readonly log = $logger();
|
|
63
|
-
protected readonly options = $state(mcpSseOptions);
|
|
64
|
-
protected readonly mcpServer = $inject(McpServerProvider);
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* SSE endpoint for server-to-client messages.
|
|
68
|
-
*
|
|
69
|
-
* Returns a text/event-stream response with server capabilities
|
|
70
|
-
* and keeps the connection open for notifications.
|
|
71
|
-
*/
|
|
72
|
-
sse = $route({
|
|
73
|
-
method: "GET",
|
|
74
|
-
path: this.options.path,
|
|
75
|
-
handler: async (request) => {
|
|
76
|
-
this.log.debug("MCP SSE connection established");
|
|
77
|
-
|
|
78
|
-
const encoder = new TextEncoder();
|
|
79
|
-
|
|
80
|
-
// Create SSE stream
|
|
81
|
-
const stream = new ReadableStream({
|
|
82
|
-
start: (controller) => {
|
|
83
|
-
// Send initial endpoint info
|
|
84
|
-
const endpointEvent = this.formatSseEvent(
|
|
85
|
-
"endpoint",
|
|
86
|
-
`${this.options.path}`,
|
|
87
|
-
);
|
|
88
|
-
controller.enqueue(encoder.encode(endpointEvent));
|
|
89
|
-
|
|
90
|
-
// Send capabilities notification
|
|
91
|
-
const capabilitiesNotification = createNotification(
|
|
92
|
-
"notifications/capabilities",
|
|
93
|
-
{ capabilities: this.mcpServer.getCapabilities() },
|
|
94
|
-
);
|
|
95
|
-
const capabilitiesEvent = this.formatSseEvent(
|
|
96
|
-
"message",
|
|
97
|
-
JSON.stringify(capabilitiesNotification),
|
|
98
|
-
);
|
|
99
|
-
controller.enqueue(encoder.encode(capabilitiesEvent));
|
|
100
|
-
},
|
|
101
|
-
cancel: () => {
|
|
102
|
-
this.log.debug("MCP SSE connection closed");
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
request.reply.status = 200;
|
|
107
|
-
request.reply.headers = {
|
|
108
|
-
"content-type": "text/event-stream",
|
|
109
|
-
"cache-control": "no-cache",
|
|
110
|
-
connection: "keep-alive",
|
|
111
|
-
};
|
|
112
|
-
request.reply.body = stream;
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* POST endpoint for client-to-server JSON-RPC messages.
|
|
118
|
-
*/
|
|
119
|
-
message = $route({
|
|
120
|
-
method: "POST",
|
|
121
|
-
path: this.options.path,
|
|
122
|
-
schema: {
|
|
123
|
-
body: t.json(),
|
|
124
|
-
},
|
|
125
|
-
handler: async (request) => {
|
|
126
|
-
try {
|
|
127
|
-
const body =
|
|
128
|
-
typeof request.body === "string"
|
|
129
|
-
? request.body
|
|
130
|
-
: JSON.stringify(request.body);
|
|
131
|
-
|
|
132
|
-
this.log.debug("MCP request body", {
|
|
133
|
-
body,
|
|
134
|
-
bodyType: typeof request.body,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
const rpcRequest = parseMessage(body);
|
|
138
|
-
|
|
139
|
-
// Build context from request headers
|
|
140
|
-
const headers = { ...request.headers } as Record<
|
|
141
|
-
string,
|
|
142
|
-
string | string[] | undefined
|
|
143
|
-
>;
|
|
144
|
-
|
|
145
|
-
const context: McpContext = { headers };
|
|
146
|
-
|
|
147
|
-
const response = await this.mcpServer.handleMessage(
|
|
148
|
-
rpcRequest,
|
|
149
|
-
context,
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
if (response) {
|
|
153
|
-
request.reply.headers["content-type"] = "application/json";
|
|
154
|
-
request.reply.body = JSON.stringify(response);
|
|
155
|
-
} else {
|
|
156
|
-
request.reply.status = 204;
|
|
157
|
-
}
|
|
158
|
-
} catch (error) {
|
|
159
|
-
if (error instanceof JsonRpcParseError) {
|
|
160
|
-
request.reply.status = 400;
|
|
161
|
-
request.reply.headers["content-type"] = "application/json";
|
|
162
|
-
request.reply.body = JSON.stringify(
|
|
163
|
-
createErrorResponse(0, createParseError(error.message)),
|
|
164
|
-
);
|
|
165
|
-
} else {
|
|
166
|
-
this.log.error("Failed to process MCP message", error);
|
|
167
|
-
request.reply.status = 500;
|
|
168
|
-
request.reply.body = JSON.stringify({
|
|
169
|
-
error: (error as Error).message,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Format a message as an SSE event.
|
|
178
|
-
*/
|
|
179
|
-
protected formatSseEvent(event: string, data: string): string {
|
|
180
|
-
return `event: ${event}\ndata: ${data}\n\n`;
|
|
181
|
-
}
|
|
182
|
-
}
|