@lango-project/lango-server 0.0.0-stage → 0.1.1
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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +710 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/runtime.mjs +683 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/body_reader.mjs +179 -0
- package/dist/body_reader.mjs.map +1 -0
- package/dist/builder.mjs +84 -0
- package/dist/builder.mjs.map +1 -0
- package/dist/cli/.lango/bignumber.mjs +3119 -0
- package/dist/cli/.lango/runtime.mjs +683 -0
- package/dist/cli/.lango/xmldom/conventions.js +429 -0
- package/dist/cli/.lango/xmldom/dom-parser.js +591 -0
- package/dist/cli/.lango/xmldom/dom.js +3759 -0
- package/dist/cli/.lango/xmldom/entities.js +2171 -0
- package/dist/cli/.lango/xmldom/errors.js +205 -0
- package/dist/cli/.lango/xmldom/grammar.js +559 -0
- package/dist/cli/.lango/xmldom/index.js +41 -0
- package/dist/cli/.lango/xmldom/package.json +1 -0
- package/dist/cli/.lango/xmldom/sax.js +983 -0
- package/dist/cli/.lango/xmldom.LICENSE.md +8 -0
- package/dist/cli/body_reader.mjs +179 -0
- package/dist/cli/body_reader.mjs.map +1 -0
- package/dist/cli/database.mjs +101 -0
- package/dist/cli/database.mjs.map +1 -0
- package/dist/cli/database_options.mjs +83 -0
- package/dist/cli/database_options.mjs.map +1 -0
- package/dist/cli/database_scope.mjs +82 -0
- package/dist/cli/database_scope.mjs.map +1 -0
- package/dist/cli/endpoint.mjs +50 -0
- package/dist/cli/endpoint.mjs.map +1 -0
- package/dist/cli/longpoll_broker.mjs +214 -0
- package/dist/cli/longpoll_broker.mjs.map +1 -0
- package/dist/cli/longpoll_options.mjs +102 -0
- package/dist/cli/longpoll_options.mjs.map +1 -0
- package/dist/cli/longpoll_session.mjs +135 -0
- package/dist/cli/longpoll_session.mjs.map +1 -0
- package/dist/cli/middleware.mjs +25 -0
- package/dist/cli/middleware.mjs.map +1 -0
- package/dist/cli/options.mjs +177 -0
- package/dist/cli/options.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +166 -0
- package/dist/cli/postgres_adapter.mjs.map +1 -0
- package/dist/cli/routes.mjs +297 -0
- package/dist/cli/routes.mjs.map +1 -0
- package/dist/cli/server.mjs +792 -0
- package/dist/cli/server.mjs.map +1 -0
- package/dist/cli/sqlite_adapter.mjs +163 -0
- package/dist/cli/sqlite_adapter.mjs.map +1 -0
- package/dist/cli/static_files.mjs +125 -0
- package/dist/cli/static_files.mjs.map +1 -0
- package/dist/database.mjs +101 -0
- package/dist/database.mjs.map +1 -0
- package/dist/database_options.mjs +83 -0
- package/dist/database_options.mjs.map +1 -0
- package/dist/database_scope.mjs +82 -0
- package/dist/database_scope.mjs.map +1 -0
- package/dist/endpoint.mjs +50 -0
- package/dist/endpoint.mjs.map +1 -0
- package/dist/group.mjs +91 -0
- package/dist/group.mjs.map +1 -0
- package/dist/library.mjs +6 -0
- package/dist/library.mjs.map +1 -0
- package/dist/longpoll_broker.mjs +214 -0
- package/dist/longpoll_broker.mjs.map +1 -0
- package/dist/longpoll_options.mjs +102 -0
- package/dist/longpoll_options.mjs.map +1 -0
- package/dist/longpoll_session.mjs +135 -0
- package/dist/longpoll_session.mjs.map +1 -0
- package/dist/middleware.mjs +25 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/options.mjs +177 -0
- package/dist/options.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +166 -0
- package/dist/postgres_adapter.mjs.map +1 -0
- package/dist/postgres_worker.mjs +119 -0
- package/dist/postgres_worker.mjs.map +1 -0
- package/dist/response.mjs +62 -0
- package/dist/response.mjs.map +1 -0
- package/dist/routes.mjs +297 -0
- package/dist/routes.mjs.map +1 -0
- package/dist/runtime.mjs +549 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/server.mjs +788 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server_context.mjs +78 -0
- package/dist/server_context.mjs.map +1 -0
- package/dist/sqlite_adapter.mjs +163 -0
- package/dist/sqlite_adapter.mjs.map +1 -0
- package/dist/static_files.mjs +125 -0
- package/dist/static_files.mjs.map +1 -0
- package/dist/token.mjs +106 -0
- package/dist/token.mjs.map +1 -0
- package/dist/tokens.mjs +76 -0
- package/dist/tokens.mjs.map +1 -0
- package/dist/worker.mjs +57 -0
- package/dist/worker.mjs.map +1 -0
- package/package.json +44 -4
- package/src/body_reader.lango +113 -0
- package/src/builder.lango +64 -0
- package/src/database.lango +66 -0
- package/src/database_options.lango +57 -0
- package/src/database_scope.lango +59 -0
- package/src/endpoint.lango +30 -0
- package/src/group.lango +61 -0
- package/src/library.lango +10 -0
- package/src/longpoll.lua +61 -0
- package/src/longpoll_broker.lango +161 -0
- package/src/longpoll_options.lango +79 -0
- package/src/longpoll_session.lango +90 -0
- package/src/middleware.lango +10 -0
- package/src/options.lango +144 -0
- package/src/postgres_adapter.lango +138 -0
- package/src/postgres_worker.lango +114 -0
- package/src/response.lango +42 -0
- package/src/routes.lango +224 -0
- package/src/runtime.lango +382 -0
- package/src/server.lango +597 -0
- package/src/server_context.lango +58 -0
- package/src/sqlite_adapter.lango +127 -0
- package/src/static_files.lango +90 -0
- package/src/token.lango +70 -0
- package/src/tokens.lango +70 -0
- package/src/worker.lango +43 -0
package/dist/routes.mjs
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { contains } from "./options.mjs";
|
|
2
|
+
import { attributes } from "./options.mjs";
|
|
3
|
+
import { middlewareName } from "./middleware.mjs";
|
|
4
|
+
import { Endpoint } from "./endpoint.mjs";
|
|
5
|
+
import { longpollOptions } from "./longpoll_options.mjs";
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import * as modules from "node:module";
|
|
9
|
+
import {
|
|
10
|
+
hostGlobal as __hostGlobal,
|
|
11
|
+
xml2obj as __xml2obj,
|
|
12
|
+
fromJson as __fromJson,
|
|
13
|
+
toJson as __toJson,
|
|
14
|
+
isNumber as __isNumber,
|
|
15
|
+
isString as __isString,
|
|
16
|
+
isBool as __isBool,
|
|
17
|
+
isObject as __isObject,
|
|
18
|
+
isArray as __isArray,
|
|
19
|
+
includes as __includes,
|
|
20
|
+
entries as __entries,
|
|
21
|
+
add as __add,
|
|
22
|
+
requireBoolean as __requireBoolean,
|
|
23
|
+
normalize as __normalize,
|
|
24
|
+
finite as __finite,
|
|
25
|
+
boundary as __boundary,
|
|
26
|
+
requireArity as __requireArity,
|
|
27
|
+
} from "./.lango/runtime.mjs";
|
|
28
|
+
|
|
29
|
+
export class Routes {
|
|
30
|
+
#root;
|
|
31
|
+
#items;
|
|
32
|
+
#events;
|
|
33
|
+
#middlewares;
|
|
34
|
+
constructor(root) {
|
|
35
|
+
__requireArity(arguments.length, [0], "constructor");
|
|
36
|
+
root = __boundary(root);
|
|
37
|
+
this.#root = __normalize(path.resolve(root));
|
|
38
|
+
this.#items = [];
|
|
39
|
+
this.#events = { };
|
|
40
|
+
this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
|
|
41
|
+
}
|
|
42
|
+
endpoint(spec) {
|
|
43
|
+
__requireArity(arguments.length, [0], "endpoint");
|
|
44
|
+
spec = __boundary(spec);
|
|
45
|
+
let item = new Endpoint(spec);
|
|
46
|
+
for (const other of __normalize(this.#items)) {
|
|
47
|
+
if (__requireBoolean(__normalize(other.path) === __normalize(item.path))) {
|
|
48
|
+
throw `Duplicate endpoint: ${__normalize(item.path)}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
__add(this.#items, { path: __normalize(item.path), module: resolveModule(__normalize(item.module), __normalize(this.#root)), handler: __normalize(item.handler), auth: __normalize(item.auth), middleware: __normalize(item.middleware) });
|
|
52
|
+
return this;
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
addMiddleware(name, spec) {
|
|
56
|
+
__requireArity(arguments.length, [0, 1], "addMiddleware");
|
|
57
|
+
name = __boundary(name);
|
|
58
|
+
spec = __boundary(spec);
|
|
59
|
+
middlewareName(name);
|
|
60
|
+
if (__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#middlewares), name)))) {
|
|
61
|
+
throw `Duplicate middleware: ${name}`;
|
|
62
|
+
}
|
|
63
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
64
|
+
throw "Middleware requires a module and exported handler";
|
|
65
|
+
}
|
|
66
|
+
for (const [key, value] of __entries(spec)) {
|
|
67
|
+
if (__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler"))) {
|
|
68
|
+
throw `Unknown middleware option: ${key}`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
|
|
72
|
+
throw "Middleware handler is required";
|
|
73
|
+
}
|
|
74
|
+
this.#middlewares[name] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler) };
|
|
75
|
+
return this;
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
middlewares() {
|
|
79
|
+
return __fromJson(__toJson(__normalize(this.#middlewares)));
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
longpoll(spec) {
|
|
83
|
+
__requireArity(arguments.length, [0], "longpoll");
|
|
84
|
+
spec = __boundary(spec);
|
|
85
|
+
let item = longpollOptions(spec);
|
|
86
|
+
for (const other of __normalize(this.#items)) {
|
|
87
|
+
if (__requireBoolean(__normalize(other.path) === __normalize(item.path))) {
|
|
88
|
+
throw `Duplicate endpoint: ${__normalize(item.path)}`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
__add(this.#items, item);
|
|
92
|
+
return this;
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
event(spec) {
|
|
96
|
+
__requireArity(arguments.length, [0], "event");
|
|
97
|
+
spec = __boundary(spec);
|
|
98
|
+
if (__requireBoolean(!__requireBoolean(contains(["register", "validate", "request"], __normalize(spec.name))))) {
|
|
99
|
+
throw `Unknown event: ${__normalize(spec.name)}`;
|
|
100
|
+
}
|
|
101
|
+
if (__requireBoolean(__includes(__normalize(spec.name), __normalize(this.#events)))) {
|
|
102
|
+
throw `Duplicate event: ${__normalize(spec.name)}`;
|
|
103
|
+
}
|
|
104
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
|
|
105
|
+
throw "Event handler is required";
|
|
106
|
+
}
|
|
107
|
+
this.#events[__normalize(spec.name)] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler) };
|
|
108
|
+
return this;
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
include(spec, auth = null) {
|
|
112
|
+
__requireArity(arguments.length, [0], "include");
|
|
113
|
+
spec = __boundary(spec);
|
|
114
|
+
auth = __boundary(auth);
|
|
115
|
+
this.#includeAt(spec, __normalize(this.#root), "", [], auth);
|
|
116
|
+
return this;
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
xml(file) {
|
|
120
|
+
__requireArity(arguments.length, [0], "xml");
|
|
121
|
+
file = __boundary(file);
|
|
122
|
+
this.#readXml(__normalize(path.resolve(file)), "", __normalize(this.#root), [], true);
|
|
123
|
+
return this;
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
definitions() {
|
|
127
|
+
return __fromJson(__toJson(__normalize(this.#items)));
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
events() {
|
|
131
|
+
return __fromJson(__toJson(__normalize(this.#events)));
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
#includeAt(spec, directory, prefix, stack, inheritedAuth = null) {
|
|
135
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "includeAt");
|
|
136
|
+
spec = __boundary(spec);
|
|
137
|
+
directory = __boundary(directory);
|
|
138
|
+
prefix = __boundary(prefix);
|
|
139
|
+
stack = __boundary(stack);
|
|
140
|
+
inheritedAuth = __boundary(inheritedAuth);
|
|
141
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.module)))) || __requireBoolean(__normalize(spec.module) === "")) || __requireBoolean(__normalize(spec.module.startsWith(".")))) || __requireBoolean(__normalize(spec.module.startsWith("/")))) || __requireBoolean(__normalize(spec.module.startsWith("node:"))))) {
|
|
142
|
+
throw "Include requires an npm package name";
|
|
143
|
+
}
|
|
144
|
+
let mount = __normalize(spec.path);
|
|
145
|
+
if (__requireBoolean(mount === null)) {
|
|
146
|
+
mount = "/";
|
|
147
|
+
}
|
|
148
|
+
if (__requireBoolean(!__requireBoolean(__isString(mount)))) {
|
|
149
|
+
throw "Include path must be a string";
|
|
150
|
+
}
|
|
151
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(mount.includes("*"))) || __requireBoolean(__normalize(mount.includes("//")))) || __requireBoolean(__normalize(mount.includes("?")))) || __requireBoolean(__normalize(mount.includes("#")))) || __requireBoolean(__normalize(mount.includes("\\"))))) {
|
|
152
|
+
throw "Invalid include prefix";
|
|
153
|
+
}
|
|
154
|
+
if (__requireBoolean(__requireBoolean(__normalize(mount.endsWith("/"))) && __requireBoolean(mount !== "/"))) {
|
|
155
|
+
mount = __normalize(mount.slice(0, __finite(-1)));
|
|
156
|
+
}
|
|
157
|
+
let combined = joinPath(prefix, mount);
|
|
158
|
+
if (__requireBoolean(combined === "/")) {
|
|
159
|
+
combined = "";
|
|
160
|
+
}
|
|
161
|
+
let file = resolveModule(`${__normalize(spec.module)}/routes.xml`, directory);
|
|
162
|
+
this.#readXml(file, combined, __normalize(path.dirname(file)), stack, false, inheritedAuth);
|
|
163
|
+
return null;
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
#readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null) {
|
|
167
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4], "readXml");
|
|
168
|
+
file = __boundary(file);
|
|
169
|
+
prefix = __boundary(prefix);
|
|
170
|
+
directory = __boundary(directory);
|
|
171
|
+
stack = __boundary(stack);
|
|
172
|
+
allowEvents = __boundary(allowEvents);
|
|
173
|
+
inheritedAuth = __boundary(inheritedAuth);
|
|
174
|
+
let real = __normalize(fs.realpathSync(file));
|
|
175
|
+
if (__requireBoolean(__normalize(stack.includes(real)))) {
|
|
176
|
+
throw `Route include cycle: ${real}`;
|
|
177
|
+
}
|
|
178
|
+
let root = __xml2obj(__normalize(fs.readFileSync(real, "utf8")));
|
|
179
|
+
if (__requireBoolean(__normalize(root.tag()) !== "routes")) {
|
|
180
|
+
throw `Expected routes root: ${real}`;
|
|
181
|
+
}
|
|
182
|
+
attributes(root, []);
|
|
183
|
+
let nextStack = __normalize(stack.slice());
|
|
184
|
+
__add(nextStack, real);
|
|
185
|
+
this.#collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents);
|
|
186
|
+
return null;
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
#collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents) {
|
|
190
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4, 5, 6], "collect");
|
|
191
|
+
parent = __boundary(parent);
|
|
192
|
+
prefix = __boundary(prefix);
|
|
193
|
+
inheritedModule = __boundary(inheritedModule);
|
|
194
|
+
inheritedAuth = __boundary(inheritedAuth);
|
|
195
|
+
directory = __boundary(directory);
|
|
196
|
+
stack = __boundary(stack);
|
|
197
|
+
allowEvents = __boundary(allowEvents);
|
|
198
|
+
if (__requireBoolean(__normalize(__normalize(parent.innerText()).trim()) !== "")) {
|
|
199
|
+
throw "Route XML cannot contain text";
|
|
200
|
+
}
|
|
201
|
+
for (const tag of __normalize(parent.children())) {
|
|
202
|
+
let spec = __normalize(tag.attributes());
|
|
203
|
+
let mod = __normalize(spec.module);
|
|
204
|
+
let auth = __normalize(spec.auth);
|
|
205
|
+
if (__requireBoolean(mod === null)) {
|
|
206
|
+
mod = inheritedModule;
|
|
207
|
+
}
|
|
208
|
+
if (__requireBoolean(auth === null)) {
|
|
209
|
+
auth = inheritedAuth;
|
|
210
|
+
}
|
|
211
|
+
if (__normalize(tag.tag()) === "endpoint") {
|
|
212
|
+
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
213
|
+
leaf(tag);
|
|
214
|
+
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth });
|
|
215
|
+
} else if (__normalize(tag.tag()) === "longpoll") {
|
|
216
|
+
attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"]);
|
|
217
|
+
leaf(tag);
|
|
218
|
+
spec.path = joinPath(prefix, __normalize(spec.path));
|
|
219
|
+
spec.auth = auth;
|
|
220
|
+
if (__requireBoolean(__normalize(spec.fallback) !== null)) {
|
|
221
|
+
spec.fallback = joinPath(prefix, __normalize(spec.fallback));
|
|
222
|
+
}
|
|
223
|
+
this.longpoll(spec);
|
|
224
|
+
} else if (__normalize(tag.tag()) === "group") {
|
|
225
|
+
attributes(tag, ["path", "module", "auth"]);
|
|
226
|
+
this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false);
|
|
227
|
+
} else if (__normalize(tag.tag()) === "include") {
|
|
228
|
+
attributes(tag, ["module", "path"]);
|
|
229
|
+
leaf(tag);
|
|
230
|
+
this.#includeAt(spec, directory, prefix, stack, auth);
|
|
231
|
+
} else if (__normalize(tag.tag()) === "middleware") {
|
|
232
|
+
attributes(tag, ["name", "module", "handler"]);
|
|
233
|
+
leaf(tag);
|
|
234
|
+
if (__requireBoolean(!__requireBoolean(allowEvents))) {
|
|
235
|
+
throw "Middleware declarations are only allowed in the application's root routes.xml";
|
|
236
|
+
}
|
|
237
|
+
this.addMiddleware(__normalize(spec.name), { module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
238
|
+
} else if (__normalize(tag.tag()) === "event") {
|
|
239
|
+
attributes(tag, ["name", "module", "handler"]);
|
|
240
|
+
leaf(tag);
|
|
241
|
+
if (__requireBoolean(!__requireBoolean(allowEvents))) {
|
|
242
|
+
throw "Events are only allowed in the application's root routes.xml";
|
|
243
|
+
}
|
|
244
|
+
this.event({ name: __normalize(spec.name), module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
245
|
+
} else {
|
|
246
|
+
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
247
|
+
leaf(tag);
|
|
248
|
+
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, middleware: __normalize(tag.tag()) });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return null;
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function resolveModule(name, directory) {
|
|
256
|
+
__requireArity(arguments.length, [0, 1], "resolveModule");
|
|
257
|
+
name = __boundary(name);
|
|
258
|
+
directory = __boundary(directory);
|
|
259
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(name === "")) || __requireBoolean(__normalize(name.startsWith("node:"))))) {
|
|
260
|
+
throw "Application module is required";
|
|
261
|
+
}
|
|
262
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(directory, "package.json"))));
|
|
263
|
+
let resolved = __normalize(resolver.resolve(name));
|
|
264
|
+
if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(resolved))))) {
|
|
265
|
+
throw `Application module must resolve to a file: ${name}`;
|
|
266
|
+
}
|
|
267
|
+
return resolved;
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function joinPath(prefix, suffix) {
|
|
272
|
+
__requireArity(arguments.length, [0, 1], "joinPath");
|
|
273
|
+
prefix = __boundary(prefix);
|
|
274
|
+
suffix = __boundary(suffix);
|
|
275
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(suffix))) || __requireBoolean(!__requireBoolean(__normalize(suffix.startsWith("/")))))) {
|
|
276
|
+
throw "Route path must begin with /";
|
|
277
|
+
}
|
|
278
|
+
if (__requireBoolean(__requireBoolean(prefix === "") || __requireBoolean(prefix === "/"))) {
|
|
279
|
+
return suffix;
|
|
280
|
+
}
|
|
281
|
+
if (__requireBoolean(suffix === "/")) {
|
|
282
|
+
return `${prefix}/`;
|
|
283
|
+
}
|
|
284
|
+
return `${prefix}${suffix}`;
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function leaf(tag) {
|
|
289
|
+
__requireArity(arguments.length, [0], "leaf");
|
|
290
|
+
tag = __boundary(tag);
|
|
291
|
+
if (__requireBoolean(__requireBoolean(__normalize(__normalize(tag.children()).length) !== 0) || __requireBoolean(__normalize(__normalize(tag.innerText()).trim()) !== ""))) {
|
|
292
|
+
throw `Expected an empty ${__normalize(tag.tag())} element`;
|
|
293
|
+
}
|
|
294
|
+
return null;
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
//# sourceMappingURL=routes.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.mjs","sources":["routes.lango"],"sourcesContent":["import \"./options\"\n contains(items, value)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"node:module\" as modules\nimport \"./options\"\n attributes(tag, allowed)\nimport js global Object\nimport \"./middleware\"\n middlewareName(name)\nimport \"./endpoint\"\n class Endpoint\n\nimport \"./longpoll_options\"\n longpollOptions(spec)\n\nexport class Routes(root)\n methods\n endpoint(spec)\n addMiddleware(name, spec)\n middlewares()\n longpoll(spec)\n include(spec, auth = null)\n xml(file)\n definitions()\n events()\n event(spec)\n\nRoutes(root)\n @root = path/resolve(root)\n @items = []\n @events = {}\n @middlewares = Object/create(null)\n\nRoutes:endpoint(spec)\n item = new Endpoint(spec)\n for @items as other\n if other.path == item.path\n throw \"Duplicate endpoint: ${item.path}\"\n @items.add({\n path: item.path\n module: resolveModule(item.module, @root)\n handler: item.handler\n auth: item.auth\n middleware: item.middleware\n })\n return @this\n\nRoutes:addMiddleware(name, spec)\n middlewareName(name)\n if Object/hasOwn(@middlewares, name)\n throw \"Duplicate middleware: ${name}\"\n if !is_object(spec) or is_array(spec)\n throw \"Middleware requires a module and exported handler\"\n for spec as key: value\n if key != \"module\" and key != \"handler\"\n throw \"Unknown middleware option: ${key}\"\n if !is_string(spec.handler) or spec.handler == \"\"\n throw \"Middleware handler is required\"\n @middlewares[name] = {\n module: resolveModule(spec.module, @root)\n handler: spec.handler\n }\n return @this\n\nRoutes:middlewares()\n return from_json(to_json(@middlewares))\n\nRoutes:longpoll(spec)\n item = longpollOptions(spec)\n for @items as other\n if other.path == item.path\n throw \"Duplicate endpoint: ${item.path}\"\n @items.add(item)\n return @this\n\nRoutes:event(spec)\n if !contains([\"register\", \"validate\", \"request\"], spec.name)\n throw \"Unknown event: ${spec.name}\"\n if spec.name in @events\n throw \"Duplicate event: ${spec.name}\"\n if !is_string(spec.handler) or spec.handler == \"\"\n throw \"Event handler is required\"\n @events[spec.name] = {\n module: resolveModule(spec.module, @root)\n handler: spec.handler\n }\n return @this\n\nRoutes:include(spec, auth = null)\n includeAt(spec, @root, \"\", [], auth)\n return @this\n\nRoutes:xml(file)\n readXml(path/resolve(file), \"\", @root, [], true)\n return @this\n\nRoutes:definitions()\n return from_json(to_json(@items))\n\nRoutes:events()\n return from_json(to_json(@events))\n\nresolveModule(name, directory)\n if !is_string(name) or name == \"\" or name.startsWith(\"node:\")\n throw \"Application module is required\"\n resolver = modules/createRequire(path/join(directory, \"package.json\"))\n resolved = resolver.resolve(name)\n if !path/isAbsolute(resolved)\n throw \"Application module must resolve to a file: ${name}\"\n return resolved\n\njoinPath(prefix, suffix)\n if !is_string(suffix) or !suffix.startsWith(\"/\")\n throw \"Route path must begin with /\"\n if prefix == \"\" or prefix == \"/\"\n return suffix\n if suffix == \"/\"\n return \"${prefix}/\"\n return \"${prefix}${suffix}\"\n\nRoutes:includeAt(spec, directory, prefix, stack, inheritedAuth = null)\n if !is_string(spec.module) or spec.module == \"\" or spec.module.startsWith(\".\") or spec.module.startsWith(\"/\") or spec.module.startsWith(\"node:\")\n throw \"Include requires an npm package name\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n if !is_string(mount)\n throw \"Include path must be a string\"\n if mount.includes(\"*\") or mount.includes(\"//\") or mount.includes(\"?\") or mount.includes(\"#\") or mount.includes(\"\\\\\")\n throw \"Invalid include prefix\"\n if mount.endsWith(\"/\") and mount != \"/\"\n mount = mount.slice(0, -1)\n combined = joinPath(prefix, mount)\n if combined == \"/\"\n combined = \"\"\n file = resolveModule(\"${spec.module}/routes.xml\", directory)\n readXml(file, combined, path/dirname(file), stack, false, inheritedAuth)\n return null\n\nRoutes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null)\n real = fs/realpathSync(file)\n if stack.includes(real)\n throw \"Route include cycle: ${real}\"\n root = xml2obj(fs/readFileSync(real, \"utf8\"))\n if root.tag() != \"routes\"\n throw \"Expected routes root: ${real}\"\n attributes(root, [])\n nextStack = stack.slice()\n nextStack.add(real)\n collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents)\n return null\n\nRoutes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents)\n if parent.innerText().trim() != \"\"\n throw \"Route XML cannot contain text\"\n for parent.children() as tag\n spec = tag.attributes()\n mod = spec.module\n auth = spec.auth\n if mod == null\n mod = inheritedModule\n if auth == null\n auth = inheritedAuth\n switch tag.tag()\n case \"endpoint\"\n attributes(tag, [\"path\", \"module\", \"handler\", \"auth\"])\n leaf(tag)\n endpoint({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n auth: auth\n })\n case \"longpoll\"\n attributes(tag, [\"path\", \"auth\", \"waitTimeoutMs\", \"retentionMs\", \"maxMessages\", \"fallback\"])\n leaf(tag)\n spec.path = joinPath(prefix, spec.path)\n spec.auth = auth\n if spec.fallback != null\n spec.fallback = joinPath(prefix, spec.fallback)\n longpoll(spec)\n case \"group\"\n attributes(tag, [\"path\", \"module\", \"auth\"])\n collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false)\n case \"include\"\n attributes(tag, [\"module\", \"path\"])\n leaf(tag)\n includeAt(spec, directory, prefix, stack, auth)\n case \"middleware\"\n attributes(tag, [\"name\", \"module\", \"handler\"])\n leaf(tag)\n if !allowEvents\n throw \"Middleware declarations are only allowed in the application's root routes.xml\"\n addMiddleware(spec.name, {\n module: resolveModule(spec.module, directory)\n handler: spec.handler\n })\n case \"event\"\n attributes(tag, [\"name\", \"module\", \"handler\"])\n leaf(tag)\n if !allowEvents\n throw \"Events are only allowed in the application's root routes.xml\"\n event({\n name: spec.name\n module: resolveModule(spec.module, directory)\n handler: spec.handler\n })\n else\n attributes(tag, [\"path\", \"module\", \"handler\", \"auth\"])\n leaf(tag)\n endpoint({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n auth: auth\n middleware: tag.tag()\n })\n return null\n\nleaf(tag)\n if tag.children().length != 0 or tag.innerText().trim() != \"\"\n throw \"Expected an empty ${tag.tag()} element\"\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuFA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;AA4GA;;;;;;;;;;"}
|