cloesce 0.0.5-unstable.1 → 0.0.5-unstable.2
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/ast.d.ts +80 -100
- package/dist/ast.js +12 -12
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +331 -368
- package/dist/extractor/err.d.ts +26 -26
- package/dist/extractor/err.js +100 -129
- package/dist/extractor/extract.d.ts +17 -60
- package/dist/extractor/extract.js +764 -826
- package/dist/orm.wasm +0 -0
- package/dist/router/crud.d.ts +1 -1
- package/dist/router/crud.js +43 -42
- package/dist/router/router.d.ts +98 -135
- package/dist/router/router.js +381 -424
- package/dist/router/validator.d.ts +6 -11
- package/dist/router/validator.js +144 -158
- package/dist/router/wasm.d.ts +22 -56
- package/dist/router/wasm.js +79 -91
- package/dist/ui/backend.d.ts +181 -214
- package/dist/ui/backend.js +245 -258
- package/dist/ui/client.d.ts +1 -1
- package/dist/ui/common.d.ts +31 -54
- package/dist/ui/common.d.ts.map +1 -1
- package/dist/ui/common.js +159 -171
- package/package.json +2 -2
package/dist/router/router.js
CHANGED
|
@@ -2,262 +2,246 @@ import { mapSql, loadOrmWasm } from "./wasm.js";
|
|
|
2
2
|
import { proxyCrud } from "./crud.js";
|
|
3
3
|
import { HttpResult, Orm } from "../ui/backend.js";
|
|
4
4
|
import { Either } from "../ui/common.js";
|
|
5
|
-
import { NO_DATA_SOURCE, MediaType } from "../ast.js";
|
|
5
|
+
import { NO_DATA_SOURCE, MediaType, } from "../ast.js";
|
|
6
6
|
import { RuntimeValidator } from "./validator.js";
|
|
7
7
|
/**
|
|
8
8
|
* Singleton instance containing the cidl, constructor registry, and wasm binary.
|
|
9
9
|
* These values are guaranteed to never change throughout a workers lifetime.
|
|
10
10
|
*/
|
|
11
11
|
export class RuntimeContainer {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
12
|
+
ast;
|
|
13
|
+
constructorRegistry;
|
|
14
|
+
wasm;
|
|
15
|
+
static instance;
|
|
16
|
+
constructor(ast, constructorRegistry, wasm) {
|
|
17
|
+
this.ast = ast;
|
|
18
|
+
this.constructorRegistry = constructorRegistry;
|
|
19
|
+
this.wasm = wasm;
|
|
20
|
+
}
|
|
21
|
+
static async init(ast, constructorRegistry, wasm) {
|
|
22
|
+
if (this.instance)
|
|
23
|
+
return;
|
|
24
|
+
const wasmAbi = await loadOrmWasm(ast, wasm);
|
|
25
|
+
this.instance = new RuntimeContainer(ast, constructorRegistry, wasmAbi);
|
|
26
|
+
}
|
|
27
|
+
static get() {
|
|
28
|
+
return this.instance;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Disposes the singleton instance. For testing purposes only.
|
|
32
|
+
*/
|
|
33
|
+
static dispose() {
|
|
34
|
+
this.instance = undefined;
|
|
35
|
+
}
|
|
35
36
|
}
|
|
36
37
|
/**
|
|
37
38
|
* Expected states in which the router may exit.
|
|
38
39
|
*/
|
|
39
40
|
export var RouterError;
|
|
40
41
|
(function (RouterError) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
RouterError[(RouterError["MissingDependency"] = 8)] = "MissingDependency";
|
|
54
|
-
RouterError[(RouterError["InvalidDatabaseQuery"] = 9)] =
|
|
55
|
-
"InvalidDatabaseQuery";
|
|
56
|
-
RouterError[(RouterError["ModelNotFound"] = 10)] = "ModelNotFound";
|
|
57
|
-
RouterError[(RouterError["UncaughtException"] = 11)] = "UncaughtException";
|
|
42
|
+
RouterError[RouterError["UnknownPrefix"] = 0] = "UnknownPrefix";
|
|
43
|
+
RouterError[RouterError["UnknownRoute"] = 1] = "UnknownRoute";
|
|
44
|
+
RouterError[RouterError["UnmatchedHttpVerb"] = 2] = "UnmatchedHttpVerb";
|
|
45
|
+
RouterError[RouterError["InstantiatedMethodMissingId"] = 3] = "InstantiatedMethodMissingId";
|
|
46
|
+
RouterError[RouterError["RequestMissingBody"] = 4] = "RequestMissingBody";
|
|
47
|
+
RouterError[RouterError["RequestBodyMissingParameters"] = 5] = "RequestBodyMissingParameters";
|
|
48
|
+
RouterError[RouterError["RequestBodyInvalidParameter"] = 6] = "RequestBodyInvalidParameter";
|
|
49
|
+
RouterError[RouterError["InstantiatedMethodMissingDataSource"] = 7] = "InstantiatedMethodMissingDataSource";
|
|
50
|
+
RouterError[RouterError["MissingDependency"] = 8] = "MissingDependency";
|
|
51
|
+
RouterError[RouterError["InvalidDatabaseQuery"] = 9] = "InvalidDatabaseQuery";
|
|
52
|
+
RouterError[RouterError["ModelNotFound"] = 10] = "ModelNotFound";
|
|
53
|
+
RouterError[RouterError["UncaughtException"] = 11] = "UncaughtException";
|
|
58
54
|
})(RouterError || (RouterError = {}));
|
|
59
55
|
export class CloesceApp {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
resultMiddleware = [];
|
|
73
|
-
/**
|
|
74
|
-
* Registers middleware which runs after the response is generated, but before
|
|
75
|
-
* it is returned to the client.
|
|
76
|
-
*
|
|
77
|
-
* Optionally, return a new HttpResult to short-circuit the response.
|
|
78
|
-
*
|
|
79
|
-
* Errors thrown in response middleware are caught and returned as a 500 response.
|
|
80
|
-
*
|
|
81
|
-
* TODO: Middleware may violate the API contract and return unexpected types
|
|
82
|
-
*
|
|
83
|
-
* @param m - The middleware function to register.
|
|
84
|
-
*/
|
|
85
|
-
onResult(m) {
|
|
86
|
-
this.resultMiddleware.push(m);
|
|
87
|
-
}
|
|
88
|
-
namespaceMiddleware = new Map();
|
|
89
|
-
/**
|
|
90
|
-
* Registers middleware for a specific namespace (being, a model or service)
|
|
91
|
-
*
|
|
92
|
-
* Runs before request validation and method middleware.
|
|
93
|
-
*
|
|
94
|
-
* TODO: Middleware may violate the API contract and return unexpected types
|
|
95
|
-
*
|
|
96
|
-
* @typeParam T - The namespace type
|
|
97
|
-
* @param ctor - The namespace's constructor (used to derive its name).
|
|
98
|
-
* @param m - The middleware function to register.
|
|
99
|
-
*/
|
|
100
|
-
onNamespace(ctor, m) {
|
|
101
|
-
if (this.namespaceMiddleware.has(ctor.name)) {
|
|
102
|
-
this.namespaceMiddleware.get(ctor.name).push(m);
|
|
103
|
-
} else {
|
|
104
|
-
this.namespaceMiddleware.set(ctor.name, [m]);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
methodMiddleware = new Map();
|
|
108
|
-
/**
|
|
109
|
-
* Registers middleware for a specific method on a namespace
|
|
110
|
-
*
|
|
111
|
-
* Runs after namespace middleware and request validation.
|
|
112
|
-
*
|
|
113
|
-
* TODO: Middleware may violate the API contract and return unexpected types
|
|
114
|
-
*
|
|
115
|
-
* @typeParam T - The namespace type
|
|
116
|
-
* @param ctor - The namespace constructor
|
|
117
|
-
* @param method - The method name on the namespace.
|
|
118
|
-
* @param m - The middleware function to register.
|
|
119
|
-
*/
|
|
120
|
-
onMethod(ctor, method, m) {
|
|
121
|
-
if (!this.methodMiddleware.has(ctor.name)) {
|
|
122
|
-
this.methodMiddleware.set(ctor.name, new Map());
|
|
123
|
-
}
|
|
124
|
-
const methods = this.methodMiddleware.get(ctor.name);
|
|
125
|
-
if (!methods.has(method)) {
|
|
126
|
-
methods.set(method, []);
|
|
56
|
+
routePrefix = "api";
|
|
57
|
+
globalMiddleware = [];
|
|
58
|
+
/**
|
|
59
|
+
* Registers global middleware which runs before any route matching.
|
|
60
|
+
*
|
|
61
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
62
|
+
*
|
|
63
|
+
* @param m - The middleware function to register.
|
|
64
|
+
*/
|
|
65
|
+
onRequest(m) {
|
|
66
|
+
this.globalMiddleware.push(m);
|
|
127
67
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
68
|
+
resultMiddleware = [];
|
|
69
|
+
/**
|
|
70
|
+
* Registers middleware which runs after the response is generated, but before
|
|
71
|
+
* it is returned to the client.
|
|
72
|
+
*
|
|
73
|
+
* Optionally, return a new HttpResult to short-circuit the response.
|
|
74
|
+
*
|
|
75
|
+
* Errors thrown in response middleware are caught and returned as a 500 response.
|
|
76
|
+
*
|
|
77
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
78
|
+
*
|
|
79
|
+
* @param m - The middleware function to register.
|
|
80
|
+
*/
|
|
81
|
+
onResult(m) {
|
|
82
|
+
this.resultMiddleware.push(m);
|
|
137
83
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const { params, dataSource } = validation.unwrap();
|
|
157
|
-
// Method middleware
|
|
158
|
-
for (const m of this.methodMiddleware
|
|
159
|
-
.get(route.namespace)
|
|
160
|
-
?.get(route.method.name) ?? []) {
|
|
161
|
-
const res = await m(di);
|
|
162
|
-
if (res) {
|
|
163
|
-
return res;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
// Hydration
|
|
167
|
-
const hydrated = await (async () => {
|
|
168
|
-
if (route.kind == "model") {
|
|
169
|
-
// TODO: Support multiple D1 bindings
|
|
170
|
-
// It's been verified by the compiler that wrangler_env exists if a D1 model is present
|
|
171
|
-
const d1 = env[ast.wrangler_env.db_binding];
|
|
172
|
-
// Proxy CRUD
|
|
173
|
-
if (route.method.is_static) {
|
|
174
|
-
return Either.right(
|
|
175
|
-
proxyCrud(ctorReg[route.namespace], ctorReg[route.namespace], d1),
|
|
176
|
-
);
|
|
84
|
+
namespaceMiddleware = new Map();
|
|
85
|
+
/**
|
|
86
|
+
* Registers middleware for a specific namespace (being, a model or service)
|
|
87
|
+
*
|
|
88
|
+
* Runs before request validation and method middleware.
|
|
89
|
+
*
|
|
90
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
91
|
+
*
|
|
92
|
+
* @typeParam T - The namespace type
|
|
93
|
+
* @param ctor - The namespace's constructor (used to derive its name).
|
|
94
|
+
* @param m - The middleware function to register.
|
|
95
|
+
*/
|
|
96
|
+
onNamespace(ctor, m) {
|
|
97
|
+
if (this.namespaceMiddleware.has(ctor.name)) {
|
|
98
|
+
this.namespaceMiddleware.get(ctor.name).push(m);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
this.namespaceMiddleware.set(ctor.name, [m]);
|
|
177
102
|
}
|
|
178
|
-
return await hydrateModelD1(
|
|
179
|
-
ctorReg,
|
|
180
|
-
d1,
|
|
181
|
-
route.model,
|
|
182
|
-
route.id,
|
|
183
|
-
dataSource,
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
// Services
|
|
187
|
-
if (route.method.is_static) {
|
|
188
|
-
return Either.right(ctorReg[route.namespace]);
|
|
189
|
-
}
|
|
190
|
-
return Either.right(di.get(route.namespace));
|
|
191
|
-
})();
|
|
192
|
-
if (hydrated.isLeft()) {
|
|
193
|
-
return hydrated.value;
|
|
194
103
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
104
|
+
methodMiddleware = new Map();
|
|
105
|
+
/**
|
|
106
|
+
* Registers middleware for a specific method on a namespace
|
|
107
|
+
*
|
|
108
|
+
* Runs after namespace middleware and request validation.
|
|
109
|
+
*
|
|
110
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
111
|
+
*
|
|
112
|
+
* @typeParam T - The namespace type
|
|
113
|
+
* @param ctor - The namespace constructor
|
|
114
|
+
* @param method - The method name on the namespace.
|
|
115
|
+
* @param m - The middleware function to register.
|
|
116
|
+
*/
|
|
117
|
+
onMethod(ctor, method, m) {
|
|
118
|
+
if (!this.methodMiddleware.has(ctor.name)) {
|
|
119
|
+
this.methodMiddleware.set(ctor.name, new Map());
|
|
120
|
+
}
|
|
121
|
+
const methods = this.methodMiddleware.get(ctor.name);
|
|
122
|
+
if (!methods.has(method)) {
|
|
123
|
+
methods.set(method, []);
|
|
124
|
+
}
|
|
125
|
+
methods.get(method).push(m);
|
|
206
126
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
127
|
+
async router(request, env, ast, ctorReg, di) {
|
|
128
|
+
// Global middleware
|
|
129
|
+
for (const m of this.globalMiddleware) {
|
|
130
|
+
const res = await m(di);
|
|
131
|
+
if (res) {
|
|
132
|
+
return res;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Route match
|
|
136
|
+
const routeRes = matchRoute(request, ast, this.routePrefix);
|
|
137
|
+
if (routeRes.isLeft()) {
|
|
138
|
+
return routeRes.value;
|
|
139
|
+
}
|
|
140
|
+
const route = routeRes.unwrap();
|
|
141
|
+
// Model middleware
|
|
142
|
+
for (const m of this.namespaceMiddleware.get(route.namespace) ?? []) {
|
|
143
|
+
const res = await m(di);
|
|
144
|
+
if (res) {
|
|
145
|
+
return res;
|
|
146
|
+
}
|
|
221
147
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
148
|
+
// Request validation
|
|
149
|
+
const validation = await validateRequest(request, ast, ctorReg, route);
|
|
150
|
+
if (validation.isLeft()) {
|
|
151
|
+
return validation.value;
|
|
152
|
+
}
|
|
153
|
+
const { params, dataSource } = validation.unwrap();
|
|
154
|
+
// Method middleware
|
|
155
|
+
for (const m of this.methodMiddleware
|
|
156
|
+
.get(route.namespace)
|
|
157
|
+
?.get(route.method.name) ?? []) {
|
|
158
|
+
const res = await m(di);
|
|
159
|
+
if (res) {
|
|
160
|
+
return res;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// Hydration
|
|
164
|
+
const hydrated = await (async () => {
|
|
165
|
+
if (route.kind == "model") {
|
|
166
|
+
// TODO: Support multiple D1 bindings
|
|
167
|
+
// It's been verified by the compiler that wrangler_env exists if a D1 model is present
|
|
168
|
+
const d1 = env[ast.wrangler_env.db_binding];
|
|
169
|
+
// Proxy CRUD
|
|
170
|
+
if (route.method.is_static) {
|
|
171
|
+
return Either.right(proxyCrud(ctorReg[route.namespace], ctorReg[route.namespace], d1));
|
|
172
|
+
}
|
|
173
|
+
return await hydrateModelD1(ctorReg, d1, route.model, route.id, dataSource);
|
|
174
|
+
}
|
|
175
|
+
// Services
|
|
176
|
+
if (route.method.is_static) {
|
|
177
|
+
return Either.right(ctorReg[route.namespace]);
|
|
178
|
+
}
|
|
179
|
+
return Either.right(di.get(route.namespace));
|
|
180
|
+
})();
|
|
181
|
+
if (hydrated.isLeft()) {
|
|
182
|
+
return hydrated.value;
|
|
183
|
+
}
|
|
184
|
+
// Method dispatch
|
|
185
|
+
return await methodDispatch(hydrated.unwrap(), di, route, params);
|
|
226
186
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Runs the Cloesce app. Intended to be called from the generated workers code.
|
|
189
|
+
*/
|
|
190
|
+
async run(request, env, ast, ctorReg) {
|
|
191
|
+
await RuntimeContainer.init(ast, ctorReg);
|
|
192
|
+
const di = new Map();
|
|
193
|
+
if (ast.wrangler_env) {
|
|
194
|
+
di.set(ast.wrangler_env.name, env);
|
|
195
|
+
}
|
|
196
|
+
di.set("Request", request);
|
|
197
|
+
// Note: Services are in topological order
|
|
198
|
+
for (const name in ast.services) {
|
|
199
|
+
const service = ast.services[name];
|
|
200
|
+
for (const attr of service.attributes) {
|
|
201
|
+
const injected = di.get(attr.injected);
|
|
202
|
+
if (!injected) {
|
|
203
|
+
return exit(500, RouterError.MissingDependency, `An injected parameter was missing from the instance registry: ${JSON.stringify(attr.injected)}`)
|
|
204
|
+
.unwrapLeft()
|
|
205
|
+
.toResponse();
|
|
206
|
+
}
|
|
207
|
+
service[attr.var_name] = injected;
|
|
208
|
+
}
|
|
209
|
+
// Inject services
|
|
210
|
+
di.set(name, Object.assign(new ctorReg[name](), service));
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
let httpResult = await this.router(request, env, ast, ctorReg, di);
|
|
214
|
+
// Response middleware
|
|
215
|
+
for (const m of this.resultMiddleware) {
|
|
216
|
+
const res = await m(di, httpResult);
|
|
217
|
+
if (res) {
|
|
218
|
+
return res.toResponse();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return httpResult.toResponse();
|
|
222
|
+
}
|
|
223
|
+
catch (e) {
|
|
224
|
+
let debug;
|
|
225
|
+
if (e instanceof Error) {
|
|
226
|
+
debug = {
|
|
227
|
+
name: e.name,
|
|
228
|
+
message: e.message,
|
|
229
|
+
stack: e.stack,
|
|
230
|
+
cause: e.cause,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
debug = {
|
|
235
|
+
name: "NonErrorThrown",
|
|
236
|
+
message: typeof e === "string" ? e : JSON.stringify(e),
|
|
237
|
+
stack: undefined,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const res = HttpResult.fail(500, JSON.stringify(debug));
|
|
241
|
+
console.error("An uncaught error occurred in the Cloesce Router: ", debug);
|
|
242
|
+
return res.toResponse();
|
|
234
243
|
}
|
|
235
|
-
}
|
|
236
|
-
return httpResult.toResponse();
|
|
237
|
-
} catch (e) {
|
|
238
|
-
let debug;
|
|
239
|
-
if (e instanceof Error) {
|
|
240
|
-
debug = {
|
|
241
|
-
name: e.name,
|
|
242
|
-
message: e.message,
|
|
243
|
-
stack: e.stack,
|
|
244
|
-
cause: e.cause,
|
|
245
|
-
};
|
|
246
|
-
} else {
|
|
247
|
-
debug = {
|
|
248
|
-
name: "NonErrorThrown",
|
|
249
|
-
message: typeof e === "string" ? e : JSON.stringify(e),
|
|
250
|
-
stack: undefined,
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
const res = HttpResult.fail(500, JSON.stringify(debug));
|
|
254
|
-
console.error(
|
|
255
|
-
"An uncaught error occurred in the Cloesce Router: ",
|
|
256
|
-
debug,
|
|
257
|
-
);
|
|
258
|
-
return res.toResponse();
|
|
259
244
|
}
|
|
260
|
-
}
|
|
261
245
|
}
|
|
262
246
|
/**
|
|
263
247
|
* Matches a request to an ApiInvocation
|
|
@@ -265,54 +249,57 @@ export class CloesceApp {
|
|
|
265
249
|
* @returns 404 or a matched route.
|
|
266
250
|
*/
|
|
267
251
|
function matchRoute(request, ast, routePrefix) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
// Extract pattern
|
|
281
|
-
const namespace = parts[0];
|
|
282
|
-
const methodName = parts[parts.length - 1];
|
|
283
|
-
const id = parts.length === 3 ? parts[1] : null;
|
|
284
|
-
const model = ast.models[namespace];
|
|
285
|
-
if (model) {
|
|
286
|
-
const method = model.methods[methodName];
|
|
287
|
-
if (!method) return notFound(RouterError.UnknownRoute);
|
|
288
|
-
if (request.method !== method.http_verb) {
|
|
289
|
-
return notFound(RouterError.UnmatchedHttpVerb);
|
|
252
|
+
const url = new URL(request.url);
|
|
253
|
+
const parts = url.pathname.split("/").filter(Boolean);
|
|
254
|
+
const prefix = routePrefix.split("/").filter(Boolean);
|
|
255
|
+
// Error state: We expect an exact request format, and expect that the model
|
|
256
|
+
// and are apart of the CIDL
|
|
257
|
+
const notFound = (c) => exit(404, c, "Unknown route");
|
|
258
|
+
for (const p of prefix) {
|
|
259
|
+
if (parts.shift() !== p)
|
|
260
|
+
return notFound(RouterError.UnknownPrefix);
|
|
261
|
+
}
|
|
262
|
+
if (parts.length < 2) {
|
|
263
|
+
return notFound(RouterError.UnknownPrefix);
|
|
290
264
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
265
|
+
// Extract pattern
|
|
266
|
+
const namespace = parts[0];
|
|
267
|
+
const methodName = parts[parts.length - 1];
|
|
268
|
+
const id = parts.length === 3 ? parts[1] : null;
|
|
269
|
+
const model = ast.models[namespace];
|
|
270
|
+
if (model) {
|
|
271
|
+
const method = model.methods[methodName];
|
|
272
|
+
if (!method)
|
|
273
|
+
return notFound(RouterError.UnknownRoute);
|
|
274
|
+
if (request.method !== method.http_verb) {
|
|
275
|
+
return notFound(RouterError.UnmatchedHttpVerb);
|
|
276
|
+
}
|
|
277
|
+
return Either.right({
|
|
278
|
+
kind: "model",
|
|
279
|
+
namespace,
|
|
280
|
+
method,
|
|
281
|
+
model,
|
|
282
|
+
id,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
const service = ast.services[namespace];
|
|
286
|
+
if (service) {
|
|
287
|
+
const method = service.methods[methodName];
|
|
288
|
+
// Services do not have IDs.
|
|
289
|
+
if (!method || id)
|
|
290
|
+
return notFound(RouterError.UnknownRoute);
|
|
291
|
+
if (request.method !== method.http_verb) {
|
|
292
|
+
return notFound(RouterError.UnmatchedHttpVerb);
|
|
293
|
+
}
|
|
294
|
+
return Either.right({
|
|
295
|
+
kind: "service",
|
|
296
|
+
namespace,
|
|
297
|
+
method,
|
|
298
|
+
service,
|
|
299
|
+
id: null,
|
|
300
|
+
});
|
|
306
301
|
}
|
|
307
|
-
return
|
|
308
|
-
kind: "service",
|
|
309
|
-
namespace,
|
|
310
|
-
method,
|
|
311
|
-
service,
|
|
312
|
-
id: null,
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
return notFound(RouterError.UnknownRoute);
|
|
302
|
+
return notFound(RouterError.UnknownRoute);
|
|
316
303
|
}
|
|
317
304
|
/**
|
|
318
305
|
* Validates the request's body/search params against a ModelMethod
|
|
@@ -320,76 +307,65 @@ function matchRoute(request, ast, routePrefix) {
|
|
|
320
307
|
* a data source
|
|
321
308
|
*/
|
|
322
309
|
async function validateRequest(request, ast, ctorReg, route) {
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
310
|
+
// Error state: any missing parameter, body, or malformed input will exit with 400.
|
|
311
|
+
const invalidRequest = (c) => exit(400, c, "Invalid Request Body");
|
|
312
|
+
// Models must have an ID on instantiated methods.
|
|
313
|
+
if (route.kind === "model" && !route.method.is_static && route.id == null) {
|
|
314
|
+
return invalidRequest(RouterError.InstantiatedMethodMissingId);
|
|
315
|
+
}
|
|
316
|
+
// Filter out injected parameters
|
|
317
|
+
const requiredParams = route.method.parameters.filter((p) => !(typeof p.cidl_type === "object" && "Inject" in p.cidl_type));
|
|
318
|
+
const url = new URL(request.url);
|
|
319
|
+
let params = Object.fromEntries(url.searchParams.entries());
|
|
320
|
+
// Extract all method parameters from the body
|
|
321
|
+
if (route.method.http_verb !== "GET") {
|
|
322
|
+
try {
|
|
323
|
+
switch (route.method.parameters_media) {
|
|
324
|
+
case MediaType.Json: {
|
|
325
|
+
const body = await request.json();
|
|
326
|
+
params = { ...params, ...body };
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
case MediaType.Octet: {
|
|
330
|
+
// Octet streams are verified by the Cloesce compiler to have
|
|
331
|
+
// one Stream type
|
|
332
|
+
const streamParam = requiredParams.find((p) => typeof p.cidl_type === "string" && p.cidl_type === "Stream");
|
|
333
|
+
params[streamParam.name] = request.body;
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
default: {
|
|
337
|
+
throw new Error("not implemented");
|
|
338
|
+
}
|
|
339
|
+
}
|
|
352
340
|
}
|
|
353
|
-
|
|
354
|
-
|
|
341
|
+
catch {
|
|
342
|
+
return invalidRequest(RouterError.RequestMissingBody);
|
|
355
343
|
}
|
|
356
|
-
}
|
|
357
|
-
} catch {
|
|
358
|
-
return invalidRequest(RouterError.RequestMissingBody);
|
|
359
344
|
}
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
);
|
|
374
|
-
if (res.isLeft()) {
|
|
375
|
-
return invalidRequest(RouterError.RequestBodyInvalidParameter);
|
|
376
|
-
}
|
|
377
|
-
params[p.name] = res.unwrap();
|
|
345
|
+
if (!requiredParams.every((p) => p.name in params)) {
|
|
346
|
+
return invalidRequest(RouterError.RequestBodyMissingParameters);
|
|
347
|
+
}
|
|
348
|
+
// Validate all parameters type
|
|
349
|
+
// Octet streams can be passed as is
|
|
350
|
+
if (route.method.parameters_media !== MediaType.Octet) {
|
|
351
|
+
for (const p of requiredParams) {
|
|
352
|
+
const res = RuntimeValidator.validate(params[p.name], p.cidl_type, ast, ctorReg);
|
|
353
|
+
if (res.isLeft()) {
|
|
354
|
+
return invalidRequest(RouterError.RequestBodyInvalidParameter);
|
|
355
|
+
}
|
|
356
|
+
params[p.name] = res.unwrap();
|
|
357
|
+
}
|
|
378
358
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
.filter(
|
|
383
|
-
(p) =>
|
|
384
|
-
typeof p.cidl_type === "object" &&
|
|
359
|
+
// A data source is required for instantiated model methods
|
|
360
|
+
const dataSource = requiredParams
|
|
361
|
+
.filter((p) => typeof p.cidl_type === "object" &&
|
|
385
362
|
"DataSource" in p.cidl_type &&
|
|
386
|
-
p.cidl_type.DataSource === route.namespace
|
|
387
|
-
|
|
388
|
-
.
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
return Either.right({ params, dataSource: dataSource ?? null });
|
|
363
|
+
p.cidl_type.DataSource === route.namespace)
|
|
364
|
+
.map((p) => params[p.name])[0];
|
|
365
|
+
if (route.kind === "model" && !route.method.is_static && !dataSource) {
|
|
366
|
+
return invalidRequest(RouterError.InstantiatedMethodMissingDataSource);
|
|
367
|
+
}
|
|
368
|
+
return Either.right({ params, dataSource: dataSource ?? null });
|
|
393
369
|
}
|
|
394
370
|
/**
|
|
395
371
|
* Queries D1 for a particular model's ID, then transforms the SQL column output into
|
|
@@ -399,101 +375,82 @@ async function validateRequest(request, ast, ctorReg, route) {
|
|
|
399
375
|
* @returns The instantiated model on success
|
|
400
376
|
*/
|
|
401
377
|
async function hydrateModelD1(constructorRegistry, d1, model, id, dataSource) {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
378
|
+
// Error state: If some outside force tweaked the database schema, the query may fail.
|
|
379
|
+
// Otherwise, this indicates a bug in the compiler or runtime.
|
|
380
|
+
const malformedQuery = (e) => exit(500, RouterError.InvalidDatabaseQuery, `Error in hydration query, is the database out of sync with the backend?: ${e instanceof Error ? e.message : String(e)}`);
|
|
381
|
+
// Query DB
|
|
382
|
+
let records;
|
|
383
|
+
try {
|
|
384
|
+
let includeTree = dataSource === NO_DATA_SOURCE
|
|
385
|
+
? null
|
|
386
|
+
: constructorRegistry[model.name][dataSource];
|
|
387
|
+
records = await d1
|
|
388
|
+
.prepare(Orm.getQuery(constructorRegistry[model.name], includeTree))
|
|
389
|
+
.bind(id)
|
|
390
|
+
.run();
|
|
391
|
+
// Error state: If no record is found for the id, return a 404
|
|
392
|
+
if (!records?.results) {
|
|
393
|
+
return exit(404, RouterError.ModelNotFound, "Record not found");
|
|
394
|
+
}
|
|
395
|
+
if (records.error) {
|
|
396
|
+
return malformedQuery(records.error);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
catch (e) {
|
|
400
|
+
return malformedQuery(e);
|
|
424
401
|
}
|
|
425
|
-
|
|
426
|
-
|
|
402
|
+
// Hydrate
|
|
403
|
+
const models = mapSql(constructorRegistry[model.name], records.results, model.data_sources[dataSource]?.tree ?? {});
|
|
404
|
+
if (models.isLeft()) {
|
|
405
|
+
return malformedQuery(models.value);
|
|
427
406
|
}
|
|
428
|
-
|
|
429
|
-
return malformedQuery(e);
|
|
430
|
-
}
|
|
431
|
-
// Hydrate
|
|
432
|
-
const models = mapSql(
|
|
433
|
-
constructorRegistry[model.name],
|
|
434
|
-
records.results,
|
|
435
|
-
model.data_sources[dataSource]?.tree ?? {},
|
|
436
|
-
);
|
|
437
|
-
if (models.isLeft()) {
|
|
438
|
-
return malformedQuery(models.value);
|
|
439
|
-
}
|
|
440
|
-
return Either.right(models.unwrap()[0]);
|
|
407
|
+
return Either.right(models.unwrap()[0]);
|
|
441
408
|
}
|
|
442
409
|
/**
|
|
443
410
|
* Calls a method on a model given a list of parameters.
|
|
444
411
|
* @returns 500 on an uncaught client error, 200 with a result body on success
|
|
445
412
|
*/
|
|
446
413
|
async function methodDispatch(obj, di, route, params) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
414
|
+
const paramArray = [];
|
|
415
|
+
for (const param of route.method.parameters) {
|
|
416
|
+
if (param.name in params) {
|
|
417
|
+
paramArray.push(params[param.name]);
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
// Injected type
|
|
421
|
+
const injected = di.get(param.cidl_type["Inject"]);
|
|
422
|
+
if (!injected) {
|
|
423
|
+
// Error state: Injected parameters cannot be found at compile time, only at runtime.
|
|
424
|
+
// If a injected reference does not exist, throw a 500.
|
|
425
|
+
return exit(500, RouterError.MissingDependency, `An injected parameter was missing from the instance registry: ${JSON.stringify(param.cidl_type)}`).unwrapLeft();
|
|
426
|
+
}
|
|
427
|
+
paramArray.push(injected);
|
|
428
|
+
}
|
|
429
|
+
const wrapResult = (res) => {
|
|
430
|
+
const rt = route.method.return_type;
|
|
431
|
+
const httpResult = typeof rt === "object" && rt !== null && "HttpResult" in rt
|
|
432
|
+
? res
|
|
433
|
+
: HttpResult.ok(200, res);
|
|
434
|
+
return httpResult.setMediaType(route.method.return_media);
|
|
435
|
+
};
|
|
436
|
+
try {
|
|
437
|
+
const res = await obj[route.method.name](...paramArray);
|
|
438
|
+
return wrapResult(res);
|
|
452
439
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
// Error state: Injected parameters cannot be found at compile time, only at runtime.
|
|
457
|
-
// If a injected reference does not exist, throw a 500.
|
|
458
|
-
return exit(
|
|
459
|
-
500,
|
|
460
|
-
RouterError.MissingDependency,
|
|
461
|
-
`An injected parameter was missing from the instance registry: ${JSON.stringify(param.cidl_type)}`,
|
|
462
|
-
).unwrapLeft();
|
|
440
|
+
catch (e) {
|
|
441
|
+
// Error state: Client code threw an uncaught exception.
|
|
442
|
+
return exit(500, RouterError.UncaughtException, `Uncaught exception in method dispatch: ${e instanceof Error ? e.message : String(e)}`).unwrapLeft();
|
|
463
443
|
}
|
|
464
|
-
paramArray.push(injected);
|
|
465
|
-
}
|
|
466
|
-
const wrapResult = (res) => {
|
|
467
|
-
const rt = route.method.return_type;
|
|
468
|
-
const httpResult =
|
|
469
|
-
typeof rt === "object" && rt !== null && "HttpResult" in rt
|
|
470
|
-
? res
|
|
471
|
-
: HttpResult.ok(200, res);
|
|
472
|
-
return httpResult.setMediaType(route.method.return_media);
|
|
473
|
-
};
|
|
474
|
-
try {
|
|
475
|
-
const res = await obj[route.method.name](...paramArray);
|
|
476
|
-
return wrapResult(res);
|
|
477
|
-
} catch (e) {
|
|
478
|
-
// Error state: Client code threw an uncaught exception.
|
|
479
|
-
return exit(
|
|
480
|
-
500,
|
|
481
|
-
RouterError.UncaughtException,
|
|
482
|
-
`Uncaught exception in method dispatch: ${e instanceof Error ? e.message : String(e)}`,
|
|
483
|
-
).unwrapLeft();
|
|
484
|
-
}
|
|
485
444
|
}
|
|
486
445
|
function exit(status, state, message, debugMessage = "") {
|
|
487
|
-
|
|
488
|
-
HttpResult.fail(status, `${message} (ErrorCode: ${state}${debugMessage})`),
|
|
489
|
-
);
|
|
446
|
+
return Either.left(HttpResult.fail(status, `${message} (ErrorCode: ${state}${debugMessage})`));
|
|
490
447
|
}
|
|
491
448
|
/**
|
|
492
449
|
* For testing purposes
|
|
493
450
|
*/
|
|
494
451
|
export const _cloesceInternal = {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
452
|
+
matchRoute,
|
|
453
|
+
validateRequest,
|
|
454
|
+
methodDispatch,
|
|
455
|
+
RuntimeContainer,
|
|
499
456
|
};
|