@lango-project/lango-server 0.1.0 → 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/README.md +283 -4
- package/dist/builder.mjs +16 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +6 -0
- package/dist/cli/endpoint.mjs.map +1 -1
- 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 +14 -2
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/routes.mjs +66 -2
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +243 -23
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +125 -0
- package/dist/cli/static_files.mjs.map +1 -0
- package/dist/endpoint.mjs +6 -0
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +17 -1
- package/dist/group.mjs.map +1 -1
- 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 +14 -2
- package/dist/options.mjs.map +1 -1
- package/dist/routes.mjs +66 -2
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +118 -44
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +243 -23
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs +78 -0
- package/dist/server_context.mjs.map +1 -0
- package/dist/static_files.mjs +125 -0
- package/dist/static_files.mjs.map +1 -0
- package/package.json +9 -4
- package/src/builder.lango +11 -0
- package/src/endpoint.lango +7 -0
- package/src/group.lango +14 -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 +16 -0
- package/src/routes.lango +65 -1
- package/src/runtime.lango +62 -0
- package/src/server.lango +182 -17
- package/src/server_context.lango +58 -0
- package/src/static_files.lango +90 -0
package/dist/routes.mjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { contains } from "./options.mjs";
|
|
2
2
|
import { attributes } from "./options.mjs";
|
|
3
|
+
import { middlewareName } from "./middleware.mjs";
|
|
3
4
|
import { Endpoint } from "./endpoint.mjs";
|
|
5
|
+
import { longpollOptions } from "./longpoll_options.mjs";
|
|
4
6
|
import * as fs from "node:fs";
|
|
5
7
|
import * as path from "node:path";
|
|
6
8
|
import * as modules from "node:module";
|
|
7
9
|
import {
|
|
10
|
+
hostGlobal as __hostGlobal,
|
|
8
11
|
xml2obj as __xml2obj,
|
|
9
12
|
fromJson as __fromJson,
|
|
10
13
|
toJson as __toJson,
|
|
@@ -14,6 +17,7 @@ import {
|
|
|
14
17
|
isObject as __isObject,
|
|
15
18
|
isArray as __isArray,
|
|
16
19
|
includes as __includes,
|
|
20
|
+
entries as __entries,
|
|
17
21
|
add as __add,
|
|
18
22
|
requireBoolean as __requireBoolean,
|
|
19
23
|
normalize as __normalize,
|
|
@@ -26,12 +30,14 @@ export class Routes {
|
|
|
26
30
|
#root;
|
|
27
31
|
#items;
|
|
28
32
|
#events;
|
|
33
|
+
#middlewares;
|
|
29
34
|
constructor(root) {
|
|
30
35
|
__requireArity(arguments.length, [0], "constructor");
|
|
31
36
|
root = __boundary(root);
|
|
32
37
|
this.#root = __normalize(path.resolve(root));
|
|
33
38
|
this.#items = [];
|
|
34
39
|
this.#events = { };
|
|
40
|
+
this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
|
|
35
41
|
}
|
|
36
42
|
endpoint(spec) {
|
|
37
43
|
__requireArity(arguments.length, [0], "endpoint");
|
|
@@ -42,7 +48,47 @@ export class Routes {
|
|
|
42
48
|
throw `Duplicate endpoint: ${__normalize(item.path)}`;
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
|
-
__add(this.#items, { path: __normalize(item.path), module: resolveModule(__normalize(item.module), __normalize(this.#root)), handler: __normalize(item.handler), auth: __normalize(item.auth) });
|
|
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);
|
|
46
92
|
return this;
|
|
47
93
|
return null;
|
|
48
94
|
}
|
|
@@ -166,6 +212,15 @@ export class Routes {
|
|
|
166
212
|
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
167
213
|
leaf(tag);
|
|
168
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);
|
|
169
224
|
} else if (__normalize(tag.tag()) === "group") {
|
|
170
225
|
attributes(tag, ["path", "module", "auth"]);
|
|
171
226
|
this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false);
|
|
@@ -173,6 +228,13 @@ export class Routes {
|
|
|
173
228
|
attributes(tag, ["module", "path"]);
|
|
174
229
|
leaf(tag);
|
|
175
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) });
|
|
176
238
|
} else if (__normalize(tag.tag()) === "event") {
|
|
177
239
|
attributes(tag, ["name", "module", "handler"]);
|
|
178
240
|
leaf(tag);
|
|
@@ -181,7 +243,9 @@ export class Routes {
|
|
|
181
243
|
}
|
|
182
244
|
this.event({ name: __normalize(spec.name), module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
183
245
|
} else {
|
|
184
|
-
|
|
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()) });
|
|
185
249
|
}
|
|
186
250
|
}
|
|
187
251
|
return null;
|
package/dist/routes.mjs.map
CHANGED
|
@@ -1 +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 \"./endpoint\"\n class Endpoint\n\nexport class Routes(root)\n methods\n endpoint(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\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 })\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 \"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 \"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
|
|
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;;;;;;;;;;"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ServerContext } from "./server_context.mjs";
|
|
1
2
|
import { Database } from "./database.mjs";
|
|
2
3
|
import { Token } from "./token.mjs";
|
|
3
4
|
import { Tokens } from "./tokens.mjs";
|
|
@@ -28,25 +29,34 @@ import {
|
|
|
28
29
|
} from "./.lango/runtime.mjs";
|
|
29
30
|
|
|
30
31
|
export class Runtime {
|
|
32
|
+
#serverContext;
|
|
31
33
|
#definition;
|
|
32
34
|
#routes;
|
|
33
35
|
#events;
|
|
34
36
|
#tokens;
|
|
35
37
|
#modules;
|
|
38
|
+
#middlewares;
|
|
36
39
|
#database;
|
|
37
40
|
constructor(definition) {
|
|
38
41
|
__requireArity(arguments.length, [0], "constructor");
|
|
39
42
|
definition = __boundary(definition);
|
|
43
|
+
this.#serverContext = new ServerContext();
|
|
40
44
|
this.#definition = definition;
|
|
41
45
|
this.#routes = [];
|
|
42
46
|
this.#events = { };
|
|
43
47
|
this.#tokens = new Tokens(__normalize(definition.settings));
|
|
44
48
|
this.#modules = { };
|
|
49
|
+
this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
|
|
45
50
|
this.#database = new Database(definition.settings.database, definition.settings.root);
|
|
46
51
|
}
|
|
47
52
|
preload() {
|
|
48
53
|
let definitions = __normalize(this.#definition.routes);
|
|
49
54
|
let pending = __normalize(this.#database.open());
|
|
55
|
+
if (__requireBoolean(__normalize(this.#definition.middlewares) !== null)) {
|
|
56
|
+
for (const [name, spec] of __entries(__normalize(this.#definition.middlewares))) {
|
|
57
|
+
pending = this.#prepareMiddleware(pending, name, spec);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
50
60
|
for (const route of definitions) {
|
|
51
61
|
pending = this.#prepareRoute(pending, route);
|
|
52
62
|
}
|
|
@@ -67,17 +77,46 @@ export class Runtime {
|
|
|
67
77
|
return null;
|
|
68
78
|
return null;
|
|
69
79
|
}
|
|
80
|
+
#prepareMiddleware(pending, name, spec) {
|
|
81
|
+
__requireArity(arguments.length, [0, 1, 2], "prepareMiddleware");
|
|
82
|
+
pending = __boundary(pending);
|
|
83
|
+
name = __boundary(name);
|
|
84
|
+
spec = __boundary(spec);
|
|
85
|
+
return pending.then(__withArity((ignored) => {
|
|
86
|
+
ignored = __boundary(ignored);
|
|
87
|
+
return this.#target(spec);
|
|
88
|
+
return null;
|
|
89
|
+
}, [0], "closure")).then(__withArity((handler) => {
|
|
90
|
+
handler = __boundary(handler);
|
|
91
|
+
this.#middlewares[name] = handler;
|
|
92
|
+
return null;
|
|
93
|
+
return null;
|
|
94
|
+
}, [0], "closure"));
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
70
97
|
#prepareRoute(pending, route) {
|
|
71
98
|
__requireArity(arguments.length, [0, 1], "prepareRoute");
|
|
72
99
|
pending = __boundary(pending);
|
|
73
100
|
route = __boundary(route);
|
|
101
|
+
if (__requireBoolean(__normalize(route.kind) === "longpoll")) {
|
|
102
|
+
__add(this.#routes, route);
|
|
103
|
+
return pending;
|
|
104
|
+
}
|
|
74
105
|
return pending.then(__withArity((ignored) => {
|
|
75
106
|
ignored = __boundary(ignored);
|
|
76
107
|
return this.#target(route);
|
|
77
108
|
return null;
|
|
78
109
|
}, [0], "closure")).then(__withArity((handler) => {
|
|
79
110
|
handler = __boundary(handler);
|
|
80
|
-
|
|
111
|
+
let middleware = null;
|
|
112
|
+
let name = __normalize(route.middleware);
|
|
113
|
+
if (__requireBoolean(name !== null)) {
|
|
114
|
+
if (__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#middlewares), name))))) {
|
|
115
|
+
throw `Middleware is not registered: ${name}`;
|
|
116
|
+
}
|
|
117
|
+
middleware = __normalize(this.#middlewares[name]);
|
|
118
|
+
}
|
|
119
|
+
__add(this.#routes, { path: __normalize(route.path), auth: __normalize(route.auth), handler: handler, middleware: middleware });
|
|
81
120
|
return null;
|
|
82
121
|
return null;
|
|
83
122
|
}, [0], "closure"));
|
|
@@ -141,6 +180,7 @@ export class Runtime {
|
|
|
141
180
|
__requireArity(arguments.length, [0], "handle");
|
|
142
181
|
task = __boundary(task);
|
|
143
182
|
__normalize(__hostGlobal(["Object"]).freeze(task.request.headers));
|
|
183
|
+
__contextScope.push(__contextSet("server", __normalize(this.#serverContext)));
|
|
144
184
|
__contextScope.push(__contextSet("request", __normalize(__hostGlobal(["Object"]).freeze(__normalize(task.request)))));
|
|
145
185
|
__contextScope.push(__contextSet("response", new Response()));
|
|
146
186
|
let fresh = __normalize(__contextGet("request").headers.authorization) === null;
|
|
@@ -169,14 +209,15 @@ export class Runtime {
|
|
|
169
209
|
}
|
|
170
210
|
__contextScope.push(__contextSet("token", new Token(claims)));
|
|
171
211
|
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
172
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (ignored) => {
|
|
212
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
|
|
173
213
|
const __contextScope = [];
|
|
174
214
|
try {
|
|
175
|
-
__contextScope.push(__contextSet("
|
|
176
|
-
__contextScope.push(__contextSet("
|
|
177
|
-
__contextScope.push(__contextSet("
|
|
178
|
-
__contextScope.push(__contextSet("
|
|
179
|
-
__contextScope.push(__contextSet("
|
|
215
|
+
__contextScope.push(__contextSet("server", __capturedContext0));
|
|
216
|
+
__contextScope.push(__contextSet("request", __capturedContext1));
|
|
217
|
+
__contextScope.push(__contextSet("response", __capturedContext2));
|
|
218
|
+
__contextScope.push(__contextSet("token", __capturedContext3));
|
|
219
|
+
__contextScope.push(__contextSet("log", __capturedContext4));
|
|
220
|
+
__contextScope.push(__contextSet("db", __capturedContext5));
|
|
180
221
|
ignored = __boundary(ignored);
|
|
181
222
|
if (__requireBoolean(fresh)) {
|
|
182
223
|
return this.#event("register", input);
|
|
@@ -186,14 +227,15 @@ export class Runtime {
|
|
|
186
227
|
} finally {
|
|
187
228
|
__contextRestore(__contextScope);
|
|
188
229
|
}
|
|
189
|
-
})(__contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (validatedAt) => {
|
|
230
|
+
})(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (validatedAt) => {
|
|
190
231
|
const __contextScope = [];
|
|
191
232
|
try {
|
|
192
|
-
__contextScope.push(__contextSet("
|
|
193
|
-
__contextScope.push(__contextSet("
|
|
194
|
-
__contextScope.push(__contextSet("
|
|
195
|
-
__contextScope.push(__contextSet("
|
|
196
|
-
__contextScope.push(__contextSet("
|
|
233
|
+
__contextScope.push(__contextSet("server", __capturedContext0));
|
|
234
|
+
__contextScope.push(__contextSet("request", __capturedContext1));
|
|
235
|
+
__contextScope.push(__contextSet("response", __capturedContext2));
|
|
236
|
+
__contextScope.push(__contextSet("token", __capturedContext3));
|
|
237
|
+
__contextScope.push(__contextSet("log", __capturedContext4));
|
|
238
|
+
__contextScope.push(__contextSet("db", __capturedContext5));
|
|
197
239
|
validatedAt = __boundary(validatedAt);
|
|
198
240
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(fresh)) && __requireBoolean(__isNumber(validatedAt)))) {
|
|
199
241
|
stampValidation(claims, validatedAt);
|
|
@@ -201,21 +243,48 @@ export class Runtime {
|
|
|
201
243
|
if (__requireBoolean(__requireBoolean(__normalize(route.auth) !== "anonymous") && __requireBoolean(!__requireBoolean(__normalize(__contextGet("token").hasAuth(__normalize(route.auth))))))) {
|
|
202
244
|
throw { status: 401, error: "Unauthorized" };
|
|
203
245
|
}
|
|
246
|
+
if (__requireBoolean(__normalize(route.kind) === "longpoll")) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
204
249
|
let handler = __normalize(route.handler);
|
|
250
|
+
let middleware = __normalize(route.middleware);
|
|
251
|
+
if (__requireBoolean(middleware !== null)) {
|
|
252
|
+
return __call(middleware, handler, __normalize(__contextGet("request").path), input);
|
|
253
|
+
}
|
|
205
254
|
return __call(handler, input);
|
|
206
255
|
return null;
|
|
207
256
|
} finally {
|
|
208
257
|
__contextRestore(__contextScope);
|
|
209
258
|
}
|
|
210
|
-
})(__contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (output) => {
|
|
259
|
+
})(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (output) => {
|
|
211
260
|
const __contextScope = [];
|
|
212
261
|
try {
|
|
213
|
-
__contextScope.push(__contextSet("
|
|
214
|
-
__contextScope.push(__contextSet("
|
|
215
|
-
__contextScope.push(__contextSet("
|
|
216
|
-
__contextScope.push(__contextSet("
|
|
217
|
-
__contextScope.push(__contextSet("
|
|
262
|
+
__contextScope.push(__contextSet("server", __capturedContext0));
|
|
263
|
+
__contextScope.push(__contextSet("request", __capturedContext1));
|
|
264
|
+
__contextScope.push(__contextSet("response", __capturedContext2));
|
|
265
|
+
__contextScope.push(__contextSet("token", __capturedContext3));
|
|
266
|
+
__contextScope.push(__contextSet("log", __capturedContext4));
|
|
267
|
+
__contextScope.push(__contextSet("db", __capturedContext5));
|
|
218
268
|
output = __boundary(output);
|
|
269
|
+
if (__requireBoolean(__normalize(task.authorize) === true)) {
|
|
270
|
+
closeScope(__contextGet("db"), true);
|
|
271
|
+
let authorizedClaims = __normalize(__contextGet("token").snapshot());
|
|
272
|
+
authorizedClaims.validatedAt = __normalize(claims.validatedAt);
|
|
273
|
+
let encoded = __normalize(this.#tokens.sign(authorizedClaims));
|
|
274
|
+
if (__requireBoolean(__normalize(task.pollResult) === null)) {
|
|
275
|
+
return { status: 200, authorization: encoded };
|
|
276
|
+
}
|
|
277
|
+
if (__requireBoolean(task.pollResult.resyncRequired)) {
|
|
278
|
+
__normalize(__contextGet("response").status(409));
|
|
279
|
+
}
|
|
280
|
+
let pollBody = __normalize(__hostGlobal(["JSON"]).stringify(__normalize(task.pollResult)));
|
|
281
|
+
let pollResponse = __normalize(__contextGet("response").result(pollBody));
|
|
282
|
+
pollResponse.headers["x-lango-token"] = encoded;
|
|
283
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#definition.settings.requestEvent)) && __requireBoolean(__includes("request", __normalize(this.#events))))) {
|
|
284
|
+
return this.#audit(__normalize(__contextGet("request").id), pollResponse, __normalize(task.body), pollBody, authorizedClaims);
|
|
285
|
+
}
|
|
286
|
+
return pollResponse;
|
|
287
|
+
}
|
|
219
288
|
let body = __normalize(__hostGlobal(["JSON"]).stringify(output));
|
|
220
289
|
if (__requireBoolean(!__requireBoolean(__isString(body)))) {
|
|
221
290
|
throw "Handler result is not JSON serializable";
|
|
@@ -233,12 +302,13 @@ export class Runtime {
|
|
|
233
302
|
} finally {
|
|
234
303
|
__contextRestore(__contextScope);
|
|
235
304
|
}
|
|
236
|
-
})(__contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).catch(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2) => (error) => {
|
|
305
|
+
})(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).catch(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3) => (error) => {
|
|
237
306
|
const __contextScope = [];
|
|
238
307
|
try {
|
|
239
|
-
__contextScope.push(__contextSet("
|
|
240
|
-
__contextScope.push(__contextSet("
|
|
241
|
-
__contextScope.push(__contextSet("
|
|
308
|
+
__contextScope.push(__contextSet("server", __capturedContext0));
|
|
309
|
+
__contextScope.push(__contextSet("request", __capturedContext1));
|
|
310
|
+
__contextScope.push(__contextSet("log", __capturedContext2));
|
|
311
|
+
__contextScope.push(__contextSet("db", __capturedContext3));
|
|
242
312
|
error = __boundary(error);
|
|
243
313
|
closeScope(__contextGet("db"), false);
|
|
244
314
|
if (__requireBoolean(__requireBoolean(error === "TokenExpired") || __requireBoolean(__requireBoolean(__isObject(error)) && __requireBoolean(__normalize(error.status) === 401)))) {
|
|
@@ -250,7 +320,7 @@ export class Runtime {
|
|
|
250
320
|
} finally {
|
|
251
321
|
__contextRestore(__contextScope);
|
|
252
322
|
}
|
|
253
|
-
})(__contextGet("request"), __contextGet("log"), __contextGet("db")), [0], "closure")).finally(((__capturedContext0) => () => {
|
|
323
|
+
})(__contextGet("server"), __contextGet("request"), __contextGet("log"), __contextGet("db")), [0], "closure")).finally(((__capturedContext0) => () => {
|
|
254
324
|
const __contextScope = [];
|
|
255
325
|
try {
|
|
256
326
|
__contextScope.push(__contextSet("db", __capturedContext0));
|
|
@@ -276,30 +346,32 @@ export class Runtime {
|
|
|
276
346
|
}
|
|
277
347
|
let handler = __normalize(this.#events[name]);
|
|
278
348
|
__normalize(threads.parentPort.postMessage({ kind: "event", id: __normalize(__contextGet("request").id) }));
|
|
279
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (ignored) => {
|
|
349
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
|
|
280
350
|
const __contextScope = [];
|
|
281
351
|
try {
|
|
282
|
-
__contextScope.push(__contextSet("
|
|
283
|
-
__contextScope.push(__contextSet("
|
|
284
|
-
__contextScope.push(__contextSet("
|
|
285
|
-
__contextScope.push(__contextSet("
|
|
286
|
-
__contextScope.push(__contextSet("
|
|
352
|
+
__contextScope.push(__contextSet("server", __capturedContext0));
|
|
353
|
+
__contextScope.push(__contextSet("request", __capturedContext1));
|
|
354
|
+
__contextScope.push(__contextSet("response", __capturedContext2));
|
|
355
|
+
__contextScope.push(__contextSet("token", __capturedContext3));
|
|
356
|
+
__contextScope.push(__contextSet("log", __capturedContext4));
|
|
357
|
+
__contextScope.push(__contextSet("db", __capturedContext5));
|
|
287
358
|
ignored = __boundary(ignored);
|
|
288
359
|
return __call(handler, input);
|
|
289
360
|
return null;
|
|
290
361
|
} finally {
|
|
291
362
|
__contextRestore(__contextScope);
|
|
292
363
|
}
|
|
293
|
-
})(__contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).finally(((__capturedContext0) => () => {
|
|
364
|
+
})(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).finally(((__capturedContext0, __capturedContext1) => () => {
|
|
294
365
|
const __contextScope = [];
|
|
295
366
|
try {
|
|
296
|
-
__contextScope.push(__contextSet("
|
|
367
|
+
__contextScope.push(__contextSet("server", __capturedContext0));
|
|
368
|
+
__contextScope.push(__contextSet("request", __capturedContext1));
|
|
297
369
|
__normalize(threads.parentPort.postMessage({ kind: "handler", id: __normalize(__contextGet("request").id) }));
|
|
298
370
|
return null;
|
|
299
371
|
} finally {
|
|
300
372
|
__contextRestore(__contextScope);
|
|
301
373
|
}
|
|
302
|
-
})(__contextGet("request")));
|
|
374
|
+
})(__contextGet("server"), __contextGet("request")));
|
|
303
375
|
return null;
|
|
304
376
|
} finally {
|
|
305
377
|
__contextRestore(__contextScope);
|
|
@@ -314,21 +386,22 @@ export class Runtime {
|
|
|
314
386
|
}
|
|
315
387
|
let original = __normalize(__hostGlobal(["JSON"]).stringify(claims));
|
|
316
388
|
__contextScope.push(__contextSet("token", new Token(claims, true)));
|
|
317
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (ignored) => {
|
|
389
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
|
|
318
390
|
const __contextScope = [];
|
|
319
391
|
try {
|
|
320
392
|
__contextScope.push(__contextSet("token", __capturedContext0));
|
|
321
393
|
__contextScope.push(__contextSet("response", __capturedContext1));
|
|
322
|
-
__contextScope.push(__contextSet("
|
|
323
|
-
__contextScope.push(__contextSet("
|
|
324
|
-
__contextScope.push(__contextSet("
|
|
394
|
+
__contextScope.push(__contextSet("server", __capturedContext2));
|
|
395
|
+
__contextScope.push(__contextSet("request", __capturedContext3));
|
|
396
|
+
__contextScope.push(__contextSet("log", __capturedContext4));
|
|
397
|
+
__contextScope.push(__contextSet("db", __capturedContext5));
|
|
325
398
|
ignored = __boundary(ignored);
|
|
326
399
|
return this.#event("validate", null);
|
|
327
400
|
return null;
|
|
328
401
|
} finally {
|
|
329
402
|
__contextRestore(__contextScope);
|
|
330
403
|
}
|
|
331
|
-
})(__contextGet("token"), __contextGet("response"), __contextGet("request"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0) => (valid) => {
|
|
404
|
+
})(__contextGet("token"), __contextGet("response"), __contextGet("server"), __contextGet("request"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0) => (valid) => {
|
|
332
405
|
const __contextScope = [];
|
|
333
406
|
try {
|
|
334
407
|
__contextScope.push(__contextSet("token", __capturedContext0));
|
|
@@ -368,21 +441,22 @@ export class Runtime {
|
|
|
368
441
|
__contextScope.push(__contextSet("response", __normalize(__hostGlobal(["Object"]).freeze({ }))));
|
|
369
442
|
let metadata = { input: input, output: output, status: __normalize(result.status) };
|
|
370
443
|
__normalize(threads.parentPort.postMessage({ kind: "prepared", id: id, result: result }));
|
|
371
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (ignored) => {
|
|
444
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
|
|
372
445
|
const __contextScope = [];
|
|
373
446
|
try {
|
|
374
447
|
__contextScope.push(__contextSet("token", __capturedContext0));
|
|
375
|
-
__contextScope.push(__contextSet("
|
|
376
|
-
__contextScope.push(__contextSet("
|
|
377
|
-
__contextScope.push(__contextSet("
|
|
378
|
-
__contextScope.push(__contextSet("
|
|
448
|
+
__contextScope.push(__contextSet("server", __capturedContext1));
|
|
449
|
+
__contextScope.push(__contextSet("request", __capturedContext2));
|
|
450
|
+
__contextScope.push(__contextSet("response", __capturedContext3));
|
|
451
|
+
__contextScope.push(__contextSet("log", __capturedContext4));
|
|
452
|
+
__contextScope.push(__contextSet("db", __capturedContext5));
|
|
379
453
|
ignored = __boundary(ignored);
|
|
380
454
|
return this.#event("request", __normalize(__hostGlobal(["Object"]).freeze(metadata)));
|
|
381
455
|
return null;
|
|
382
456
|
} finally {
|
|
383
457
|
__contextRestore(__contextScope);
|
|
384
458
|
}
|
|
385
|
-
})(__contextGet("token"), __contextGet("request"), __contextGet("response"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0) => (ignored) => {
|
|
459
|
+
})(__contextGet("token"), __contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0) => (ignored) => {
|
|
386
460
|
const __contextScope = [];
|
|
387
461
|
try {
|
|
388
462
|
__contextScope.push(__contextSet("db", __capturedContext0));
|
package/dist/runtime.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","sources":["runtime.lango"],"sourcesContent":["import \"./database\"\n class Database\nimport js global Promise\nimport js global JSON\nimport js global Object\nimport js global Date\nimport js \"node:worker_threads\" as threads\nimport \"./token\"\n class Token\nimport \"./tokens\"\n class Tokens\nimport \"./response\"\n class Response\n\nexport class Runtime(definition)\n methods\n preload()\n handle(task)\n close()\n\nRuntime(definition)\n @definition = definition\n @routes = []\n @events = {}\n @tokens = new Tokens(definition.settings)\n @modules = {}\n @database = new Database(definition.settings.database, definition.settings.root)\n\nRuntime:preload()\n definitions = @definition.routes\n pending = @database.open()\n for definitions as route\n pending = prepareRoute(pending, route)\n events = @definition.events\n for events as name: target\n pending = prepareEvent(pending, name, target)\n return chain pending\n catch(error)\n close()\n throw error\n\nRuntime:close()\n @database.close()\n return null\n\nRuntime:prepareRoute(pending, route)\n return chain pending\n then(ignored)\n capture route\n return target(route)\n then(handler)\n capture route\n @routes.add({\n path: route.path\n auth: route.auth\n handler: handler\n })\n return null\n\nRuntime:prepareEvent(pending, name, spec)\n return chain pending\n then(ignored)\n capture spec\n return target(spec)\n then(handler)\n capture name\n @events[name] = handler\n return null\n\nRuntime:target(spec)\n file = spec.module\n if file in @modules\n return Promise/resolve(exported(@modules[file], spec.handler))\n return chain import_module(file)\n then(mod)\n capture file\n capture spec\n @modules[file] = mod\n return exported(mod, spec.handler)\n\nexported(mod, name)\n if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)\n throw \"Handler is not exported: ${name}\"\n return mod[name]\n\nRuntime:match(path)\n best = null\n length = -1\n for @routes as route\n if route.path == path\n return route\n if route.path.endsWith(\"/*\")\n prefix = route.path.slice(0, -1)\n if path.startsWith(prefix) and prefix.length > length\n best = route\n length = prefix.length\n return best\n\nRuntime:handle(task)\n Object/freeze(task.request.headers)\n context request = Object/freeze(task.request)\n context response = new Response()\n context log\n fresh = request.headers.authorization == null\n claims = null\n try\n if fresh\n claims = @tokens.create()\n else\n claims = @tokens.read(request.headers.authorization)\n catch error\n return failure(401, \"Unauthorized\")\n route = match(request.path)\n if route == null\n return failure(404, \"RouteNotFound\")\n input = null\n try\n input = JSON/parse(task.body)\n if !is_object(input) or is_array(input)\n return failure(400, \"InputMustBeObject\")\n catch error\n return failure(400, \"InvalidJson\")\n context token = new Token(claims)\n context db = @database.scope()\n return chain Promise/resolve(null)\n then(ignored)\n capture request\n capture response\n capture token\n capture log\n capture db\n capture fresh\n capture input\n if fresh\n return event(\"register\", input)\n return validate()\n then(validatedAt)\n capture request\n capture response\n capture token\n capture log\n capture db\n capture route\n capture input\n capture claims\n capture fresh\n if !fresh and is_number(validatedAt)\n stampValidation(claims, validatedAt)\n if route.auth != \"anonymous\" and !token.hasAuth(route.auth)\n throw {\n status: 401\n error: \"Unauthorized\"\n }\n handler = route.handler\n return handler(input)\n then(output)\n capture request\n capture response\n capture token\n capture log\n capture db\n capture task\n capture claims\n body = JSON/stringify(output)\n if !is_string(body)\n throw \"Handler result is not JSON serializable\"\n closeScope(db, true)\n result = response.result(body)\n updated = token.snapshot()\n updated.validatedAt = claims.validatedAt\n result.headers[\"x-lango-token\"] = @tokens.sign(updated)\n if @definition.settings.requestEvent and \"request\" in @events\n return audit(request.id, result, task.body, body, updated)\n return result\n catch(error)\n capture request\n capture log\n capture db\n closeScope(db, false)\n if error == \"TokenExpired\" or (is_object(error) and error.status == 401)\n return failure(401, \"Unauthorized\")\n log.err(\"[${request.id}] Request failed\")\n return failure(500, \"InternalServerError\")\n finally\n capture db\n closeScope(db, false)\n\nRuntime:event(name, input)\n if !(name in @events)\n return null\n context request\n context response\n context token\n context log\n context db\n handler = @events[name]\n threads/parentPort/postMessage({\n kind: \"event\"\n id: request.id\n })\n return chain Promise/resolve(null)\n then(ignored)\n capture request\n capture response\n capture token\n capture log\n capture db\n capture handler\n capture input\n return handler(input)\n finally\n capture request\n capture threads\n threads/parentPort/postMessage({\n kind: \"handler\"\n id: request.id\n })\n\nRuntime:validate()\n context db\n context token\n context response\n context request\n context log\n claims = token.snapshot()\n if !(\"validate\" in @events) or Date/now() - claims.validatedAt < 1000\n return null\n original = JSON/stringify(claims)\n context token = new Token(claims, true)\n return chain Promise/resolve(null)\n then(ignored)\n capture token\n capture response\n capture request\n capture log\n capture db\n return event(\"validate\", null)\n then(valid)\n capture token\n capture original\n capture claims\n if !is_bool(valid)\n throw \"Validate must return boolean\"\n if !valid\n throw {\n status: 401\n error: \"Unauthorized\"\n }\n if JSON/stringify(token.snapshot()) != original\n throw \"Validate cannot change the token\"\n claims.validatedAt = Date/now()\n return claims.validatedAt\n\nRuntime:audit(id, result, input, output, claims)\n context db = @database.scope()\n context request\n context log\n context token = new Token(claims, true)\n context response = Object/freeze({})\n metadata = {\n input: input\n output: output\n status: result.status\n }\n threads/parentPort/postMessage({\n kind: \"prepared\"\n id: id\n result: result\n })\n return chain Promise/resolve(null)\n then(ignored)\n capture token\n capture request\n capture response\n capture log\n capture db\n capture metadata\n return event(\"request\", Object/freeze(metadata))\n then(ignored)\n capture db\n closeScope(db, true)\n return null\n catch(error)\n capture log\n capture db\n closeScope(db, false)\n log.err(\"Request event failed\")\n return null\n then(ignored)\n capture result\n return result\n finally\n capture db\n closeScope(db, false)\n\nfailure(status, code)\n headers = {\n \"content-type\": \"application/json; charset=utf-8\"\n \"cache-control\": \"no-store\"\n }\n if status == 401\n headers[\"www-authenticate\"] = \"Bearer\"\n return {\n status: status\n headers: headers\n body: JSON/stringify({\n error: code\n })\n }\n\nstampValidation(claims, validatedAt)\n claims.validatedAt = validatedAt\n return null\n\ncloseScope(db, strict)\n if db != null\n dirty = db.finish()\n if dirty and strict\n throw \"UnclosedTransaction\"\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA;;;;;;;;;;;AAuNA;;;;;;;;;;;;AAeA;;;;;;;;;AAIA;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"runtime.mjs","sources":["runtime.lango"],"sourcesContent":["import \"./server_context\"\n class ServerContext\nimport \"./database\"\n class Database\nimport js global Promise\nimport js global JSON\nimport js global Object\nimport js global Date\nimport js \"node:worker_threads\" as threads\nimport \"./token\"\n class Token\nimport \"./tokens\"\n class Tokens\nimport \"./response\"\n class Response\n\nexport class Runtime(definition)\n methods\n preload()\n handle(task)\n close()\n\nRuntime(definition)\n @serverContext = new ServerContext()\n @definition = definition\n @routes = []\n @events = {}\n @tokens = new Tokens(definition.settings)\n @modules = {}\n @middlewares = Object/create(null)\n @database = new Database(definition.settings.database, definition.settings.root)\n\nRuntime:preload()\n definitions = @definition.routes\n pending = @database.open()\n if @definition.middlewares != null\n for @definition.middlewares as name: spec\n pending = prepareMiddleware(pending, name, spec)\n for definitions as route\n pending = prepareRoute(pending, route)\n events = @definition.events\n for events as name: target\n pending = prepareEvent(pending, name, target)\n return chain pending\n catch(error)\n close()\n throw error\n\nRuntime:close()\n @database.close()\n return null\n\nRuntime:prepareMiddleware(pending, name, spec)\n return chain pending\n then(ignored)\n capture spec\n return target(spec)\n then(handler)\n capture name\n @middlewares[name] = handler\n return null\n\nRuntime:prepareRoute(pending, route)\n if route.kind == \"longpoll\"\n @routes.add(route)\n return pending\n return chain pending\n then(ignored)\n capture route\n return target(route)\n then(handler)\n capture route\n middleware = null\n name = route.middleware\n if name != null\n if !Object/hasOwn(@middlewares, name)\n throw \"Middleware is not registered: ${name}\"\n middleware = @middlewares[name]\n @routes.add({\n path: route.path\n auth: route.auth\n handler: handler\n middleware: middleware\n })\n return null\n\nRuntime:prepareEvent(pending, name, spec)\n return chain pending\n then(ignored)\n capture spec\n return target(spec)\n then(handler)\n capture name\n @events[name] = handler\n return null\n\nRuntime:target(spec)\n file = spec.module\n if file in @modules\n return Promise/resolve(exported(@modules[file], spec.handler))\n return chain import_module(file)\n then(mod)\n capture file\n capture spec\n @modules[file] = mod\n return exported(mod, spec.handler)\n\nexported(mod, name)\n if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)\n throw \"Handler is not exported: ${name}\"\n return mod[name]\n\nRuntime:match(path)\n best = null\n length = -1\n for @routes as route\n if route.path == path\n return route\n if route.path.endsWith(\"/*\")\n prefix = route.path.slice(0, -1)\n if path.startsWith(prefix) and prefix.length > length\n best = route\n length = prefix.length\n return best\n\nRuntime:handle(task)\n Object/freeze(task.request.headers)\n context server = @serverContext\n context request = Object/freeze(task.request)\n context response = new Response()\n context log\n fresh = request.headers.authorization == null\n claims = null\n try\n if fresh\n claims = @tokens.create()\n else\n claims = @tokens.read(request.headers.authorization)\n catch error\n return failure(401, \"Unauthorized\")\n route = match(request.path)\n if route == null\n return failure(404, \"RouteNotFound\")\n input = null\n try\n input = JSON/parse(task.body)\n if !is_object(input) or is_array(input)\n return failure(400, \"InputMustBeObject\")\n catch error\n return failure(400, \"InvalidJson\")\n context token = new Token(claims)\n context db = @database.scope()\n return chain Promise/resolve(null)\n then(ignored)\n capture server\n capture request\n capture response\n capture token\n capture log\n capture db\n capture fresh\n capture input\n if fresh\n return event(\"register\", input)\n return validate()\n then(validatedAt)\n capture server\n capture request\n capture response\n capture token\n capture log\n capture db\n capture route\n capture input\n capture claims\n capture fresh\n if !fresh and is_number(validatedAt)\n stampValidation(claims, validatedAt)\n if route.auth != \"anonymous\" and !token.hasAuth(route.auth)\n throw {\n status: 401\n error: \"Unauthorized\"\n }\n if route.kind == \"longpoll\"\n return null\n handler = route.handler\n middleware = route.middleware\n if middleware != null\n return middleware(handler, request.path, input)\n return handler(input)\n then(output)\n capture server\n capture request\n capture response\n capture token\n capture log\n capture db\n capture task\n capture claims\n if task.authorize == true\n closeScope(db, true)\n authorizedClaims = token.snapshot()\n authorizedClaims.validatedAt = claims.validatedAt\n encoded = @tokens.sign(authorizedClaims)\n if task.pollResult == null\n return {\n status: 200\n authorization: encoded\n }\n if task.pollResult.resyncRequired\n response.status(409)\n pollBody = JSON/stringify(task.pollResult)\n pollResponse = response.result(pollBody)\n pollResponse.headers[\"x-lango-token\"] = encoded\n if @definition.settings.requestEvent and \"request\" in @events\n return audit(request.id, pollResponse, task.body, pollBody, authorizedClaims)\n return pollResponse\n body = JSON/stringify(output)\n if !is_string(body)\n throw \"Handler result is not JSON serializable\"\n closeScope(db, true)\n result = response.result(body)\n updated = token.snapshot()\n updated.validatedAt = claims.validatedAt\n result.headers[\"x-lango-token\"] = @tokens.sign(updated)\n if @definition.settings.requestEvent and \"request\" in @events\n return audit(request.id, result, task.body, body, updated)\n return result\n catch(error)\n capture server\n capture request\n capture log\n capture db\n closeScope(db, false)\n if error == \"TokenExpired\" or (is_object(error) and error.status == 401)\n return failure(401, \"Unauthorized\")\n log.err(\"[${request.id}] Request failed\")\n return failure(500, \"InternalServerError\")\n finally\n capture db\n closeScope(db, false)\n\nRuntime:event(name, input)\n if !(name in @events)\n return null\n context server\n context request\n context response\n context token\n context log\n context db\n handler = @events[name]\n threads/parentPort/postMessage({\n kind: \"event\"\n id: request.id\n })\n return chain Promise/resolve(null)\n then(ignored)\n capture server\n capture request\n capture response\n capture token\n capture log\n capture db\n capture handler\n capture input\n return handler(input)\n finally\n capture server\n capture request\n capture threads\n threads/parentPort/postMessage({\n kind: \"handler\"\n id: request.id\n })\n\nRuntime:validate()\n context db\n context token\n context response\n context server\n context request\n context log\n claims = token.snapshot()\n if !(\"validate\" in @events) or Date/now() - claims.validatedAt < 1000\n return null\n original = JSON/stringify(claims)\n context token = new Token(claims, true)\n return chain Promise/resolve(null)\n then(ignored)\n capture token\n capture response\n capture server\n capture request\n capture log\n capture db\n return event(\"validate\", null)\n then(valid)\n capture token\n capture original\n capture claims\n if !is_bool(valid)\n throw \"Validate must return boolean\"\n if !valid\n throw {\n status: 401\n error: \"Unauthorized\"\n }\n if JSON/stringify(token.snapshot()) != original\n throw \"Validate cannot change the token\"\n claims.validatedAt = Date/now()\n return claims.validatedAt\n\nRuntime:audit(id, result, input, output, claims)\n context db = @database.scope()\n context server\n context request\n context log\n context token = new Token(claims, true)\n context response = Object/freeze({})\n metadata = {\n input: input\n output: output\n status: result.status\n }\n threads/parentPort/postMessage({\n kind: \"prepared\"\n id: id\n result: result\n })\n return chain Promise/resolve(null)\n then(ignored)\n capture token\n capture server\n capture request\n capture response\n capture log\n capture db\n capture metadata\n return event(\"request\", Object/freeze(metadata))\n then(ignored)\n capture db\n closeScope(db, true)\n return null\n catch(error)\n capture log\n capture db\n closeScope(db, false)\n log.err(\"Request event failed\")\n return null\n then(ignored)\n capture result\n return result\n finally\n capture db\n closeScope(db, false)\n\nfailure(status, code)\n headers = {\n \"content-type\": \"application/json; charset=utf-8\"\n \"cache-control\": \"no-store\"\n }\n if status == 401\n headers[\"www-authenticate\"] = \"Bearer\"\n return {\n status: status\n headers: headers\n body: JSON/stringify({\n error: code\n })\n }\n\nstampValidation(claims, validatedAt)\n claims.validatedAt = validatedAt\n return null\n\ncloseScope(db, strict)\n if db != null\n dirty = db.finish()\n if dirty and strict\n throw \"UnclosedTransaction\"\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA;;;;;;;;;;;AA0PA;;;;;;;;;;;;AAeA;;;;;;;;;AAIA;;;;;;;;;;;;;;;"}
|