@lango-project/lango-server 0.1.0 → 0.1.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/README.md +368 -10
- package/dist/builder.mjs +16 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +8 -6
- 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/route_pattern.mjs +129 -0
- package/dist/cli/route_pattern.mjs.map +1 -0
- package/dist/cli/routes.mjs +80 -4
- 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 +8 -6
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +17 -1
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs +45 -0
- package/dist/http_failure.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 +14 -2
- package/dist/options.mjs.map +1 -1
- package/dist/response.mjs +8 -0
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs +22 -0
- package/dist/route_info.mjs.map +1 -0
- package/dist/route_pattern.mjs +129 -0
- package/dist/route_pattern.mjs.map +1 -0
- package/dist/routes.mjs +80 -4
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +170 -59
- 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 +88 -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/dist/token.mjs +12 -0
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs +16 -1
- package/dist/tokens.mjs.map +1 -1
- package/package.json +9 -4
- package/src/builder.lango +11 -0
- package/src/endpoint.lango +10 -4
- package/src/group.lango +14 -0
- package/src/http_failure.lango +26 -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/response.lango +6 -0
- package/src/route_info.lango +15 -0
- package/src/route_pattern.lango +86 -0
- package/src/routes.lango +80 -4
- package/src/runtime.lango +113 -13
- package/src/server.lango +182 -17
- package/src/server_context.lango +66 -0
- package/src/static_files.lango +90 -0
- package/src/token.lango +11 -0
- package/src/tokens.lango +11 -1
package/dist/options.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { redisOptions } from "./longpoll_options.mjs";
|
|
1
2
|
import { databaseOptions } from "./database_options.mjs";
|
|
3
|
+
import { staticOptions } from "./static_files.mjs";
|
|
2
4
|
import * as fs from "node:fs";
|
|
3
5
|
import * as path from "node:path";
|
|
4
6
|
import {
|
|
@@ -36,7 +38,7 @@ export function options(root, supplied = null) {
|
|
|
36
38
|
__requireArity(arguments.length, [0], "options");
|
|
37
39
|
root = __boundary(root);
|
|
38
40
|
supplied = __boundary(supplied);
|
|
39
|
-
let result = { root: __normalize(path.resolve(root)), host: "127.0.0.1", port: 3000, workers: 2, maxQueue: 64, bodyLimit: 1048576, bodyTimeoutMs: 30000, requestTimeoutMs: 30000, eventTimeoutMs: 5000, startupTimeoutMs: 30000, shutdownTimeoutMs: 30000, issuer: "lango-server", audience: "lango-client", ttlSeconds: 900, key: null, keys: { }, activeKeyId: "current", requestEvent: false, database: null };
|
|
41
|
+
let result = { root: __normalize(path.resolve(root)), host: "127.0.0.1", port: 3000, workers: 2, maxQueue: 64, bodyLimit: 1048576, bodyTimeoutMs: 30000, requestTimeoutMs: 30000, eventTimeoutMs: 5000, startupTimeoutMs: 30000, shutdownTimeoutMs: 30000, issuer: "lango-server", audience: "lango-client", ttlSeconds: 900, key: null, keys: { }, activeKeyId: "current", requestEvent: false, database: null, redis: null, static: null };
|
|
40
42
|
if (__requireBoolean(supplied !== null)) {
|
|
41
43
|
for (const [key, value] of __entries(supplied)) {
|
|
42
44
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(result, key)))) || __requireBoolean(key === "root"))) {
|
|
@@ -89,6 +91,8 @@ export function options(root, supplied = null) {
|
|
|
89
91
|
result.keys = keys;
|
|
90
92
|
result.key = null;
|
|
91
93
|
result.database = databaseOptions(__normalize(result.root), __normalize(result.database), __normalize(result.workers));
|
|
94
|
+
result.redis = redisOptions(__normalize(result.redis));
|
|
95
|
+
result.static = staticOptions(__normalize(result.root), __normalize(result.static));
|
|
92
96
|
return result;
|
|
93
97
|
return null;
|
|
94
98
|
}
|
|
@@ -110,12 +114,20 @@ export function deployment(directory) {
|
|
|
110
114
|
throw `Duplicate or nonempty deployment section: ${name}`;
|
|
111
115
|
}
|
|
112
116
|
seen.push(name);
|
|
113
|
-
if (name === "
|
|
117
|
+
if (name === "static") {
|
|
118
|
+
attributes(tag, ["path", "directory", "index"]);
|
|
119
|
+
settings.static = __normalize(tag.attributes());
|
|
120
|
+
continue;
|
|
121
|
+
} else if (name === "http") {
|
|
114
122
|
attributes(tag, ["host", "port", "bodyLimit", "bodyTimeoutMs"]);
|
|
115
123
|
} else if (name === "workers") {
|
|
116
124
|
attributes(tag, ["workers", "maxQueue", "requestTimeoutMs", "startupTimeoutMs", "shutdownTimeoutMs"]);
|
|
117
125
|
} else if (name === "events") {
|
|
118
126
|
attributes(tag, ["requestEvent", "eventTimeoutMs"]);
|
|
127
|
+
} else if (name === "redis") {
|
|
128
|
+
attributes(tag, ["connectionEnv", "namespace", "maxPendingPolls"]);
|
|
129
|
+
settings.redis = __normalize(tag.attributes());
|
|
130
|
+
continue;
|
|
119
131
|
} else if (name === "database") {
|
|
120
132
|
attributes(tag, ["type", "file", "connectionEnv", "busyTimeoutMs", "queryTimeoutMs", "connectTimeoutSeconds"]);
|
|
121
133
|
settings.database = __normalize(tag.attributes());
|
package/dist/options.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.mjs","sources":["options.lango"],"sourcesContent":["import \"./database_options\"\n databaseOptions(root, spec, workers)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js global Object\nimport js global Number\nimport js global process\n\nexport contains(items, value)\nexport options(root, supplied = null)\nexport deployment(directory)\nexport attributes(tag, allowed)\n\nattributes(tag, allowed)\n for tag.attributes() as key: value\n if !allowed.includes(key)\n throw \"Unknown ${tag.tag()} attribute: ${key}\"\n return null\n\noptions(root, supplied = null)\n result = {\n root: path/resolve(root)\n host: \"127.0.0.1\"\n port: 3000\n workers: 2\n maxQueue: 64\n bodyLimit: 1048576\n bodyTimeoutMs: 30000\n requestTimeoutMs: 30000\n eventTimeoutMs: 5000\n startupTimeoutMs: 30000\n shutdownTimeoutMs: 30000\n issuer: \"lango-server\"\n audience: \"lango-client\"\n ttlSeconds: 900\n key: null\n keys: {}\n activeKeyId: \"current\"\n requestEvent: false\n database: null\n }\n if supplied != null\n for supplied as key: value\n if !Object/hasOwn(result, key) or key == \"root\"\n throw \"Unknown server option: ${key}\"\n result[key] = value\n if !is_string(result.host) or result.host == \"\"\n throw \"Host must be a nonempty string\"\n for [\"port\", \"workers\", \"maxQueue\", \"bodyLimit\", \"bodyTimeoutMs\", \"requestTimeoutMs\", \"eventTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\", \"ttlSeconds\"] as key\n value = result[key]\n if !is_number(value) or !Number/isSafeInteger(value) or value < 0\n throw \"Invalid server option: ${key}\"\n if key != \"port\" and key != \"maxQueue\" and value == 0\n throw \"Server option must be positive: ${key}\"\n if key.endsWith(\"Ms\") and value > 2147483647\n throw \"Timeout exceeds timer limit: ${key}\"\n if result.port > 65535 or result.workers > 128 or result.ttlSeconds > 86400\n throw \"Server option exceeds supported limit\"\n if !is_bool(result.requestEvent)\n throw \"requestEvent must be boolean\"\n for [\"issuer\", \"audience\", \"activeKeyId\"] as key\n if !is_string(result[key]) or result[key] == \"\"\n throw \"Missing JWT option: ${key}\"\n if !is_object(result.keys) or is_array(result.keys)\n throw \"JWT keys must be a map\"\n keys = Object/assign(Object/create(null), result.keys)\n if result.key != null\n keys[result.activeKeyId] = result.key\n if !Object/hasOwn(keys, result.activeKeyId)\n throw \"JWT signing key is required\"\n for keys as id: secret\n if !is_string(secret) or secret.length < 32\n throw \"JWT keys must contain at least 32 characters\"\n result.keys = keys\n result.key = null\n result.database = databaseOptions(result.root, result.database, result.workers)\n return result\n\ndeployment(directory)\n file = path/join(directory, \"deploy.xml\")\n root = xml2obj(fs/readFileSync(file, \"utf8\"))\n if root.tag() != \"deploy\"\n throw \"Expected deploy root\"\n attributes(root, [])\n settings = {}\n seen = []\n for root.children() as tag\n name = tag.tag()\n if seen.includes(name) or tag.children().length != 0 or tag.innerText().trim() != \"\"\n throw \"Duplicate or nonempty deployment section: ${name}\"\n seen.add(name)\n switch name\n case \"http\"\n attributes(tag, [\"host\", \"port\", \"bodyLimit\", \"bodyTimeoutMs\"])\n case \"workers\"\n attributes(tag, [\"workers\", \"maxQueue\", \"requestTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\"])\n case \"events\"\n attributes(tag, [\"requestEvent\", \"eventTimeoutMs\"])\n case \"database\"\n attributes(tag, [\"type\", \"file\", \"connectionEnv\", \"busyTimeoutMs\", \"queryTimeoutMs\", \"connectTimeoutSeconds\"])\n settings.database = tag.attributes()\n continue\n case \"jwt\"\n attributes(tag, [\"issuer\", \"audience\", \"ttlSeconds\", \"keyEnv\", \"keysFile\", \"activeKeyId\"])\n else\n throw \"Unknown deployment section: ${name}\"\n for tag.attributes() as key: value\n if key == \"keyEnv\"\n settings.key = process.env[value]\n if settings.key == null\n throw \"JWT key environment variable is missing: ${value}\"\n else\n if key == \"keysFile\"\n settings.keys = from_json(fs/readFileSync(path/resolve(directory, value), \"utf8\"))\n else\n if contains([\"host\", \"issuer\", \"audience\", \"activeKeyId\"], key)\n settings[key] = value\n else\n if key == \"requestEvent\"\n if value != \"true\" and value != \"false\"\n throw \"requestEvent must be true or false\"\n settings[key] = value == \"true\"\n else\n settings[key] = Number(value)\n return options(directory, settings)\n\ncontains(items, value)\n return items.includes(value)\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"options.mjs","sources":["options.lango"],"sourcesContent":["import \"./longpoll_options\"\n redisOptions(spec)\nimport \"./database_options\"\n databaseOptions(root, spec, workers)\nimport \"./static_files\"\n staticOptions(root, spec)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js global Object\nimport js global Number\nimport js global process\n\nexport contains(items, value)\nexport options(root, supplied = null)\nexport deployment(directory)\nexport attributes(tag, allowed)\n\nattributes(tag, allowed)\n for tag.attributes() as key: value\n if !allowed.includes(key)\n throw \"Unknown ${tag.tag()} attribute: ${key}\"\n return null\n\noptions(root, supplied = null)\n result = {\n root: path/resolve(root)\n host: \"127.0.0.1\"\n port: 3000\n workers: 2\n maxQueue: 64\n bodyLimit: 1048576\n bodyTimeoutMs: 30000\n requestTimeoutMs: 30000\n eventTimeoutMs: 5000\n startupTimeoutMs: 30000\n shutdownTimeoutMs: 30000\n issuer: \"lango-server\"\n audience: \"lango-client\"\n ttlSeconds: 900\n key: null\n keys: {}\n activeKeyId: \"current\"\n requestEvent: false\n database: null\n redis: null\n static: null\n }\n if supplied != null\n for supplied as key: value\n if !Object/hasOwn(result, key) or key == \"root\"\n throw \"Unknown server option: ${key}\"\n result[key] = value\n if !is_string(result.host) or result.host == \"\"\n throw \"Host must be a nonempty string\"\n for [\"port\", \"workers\", \"maxQueue\", \"bodyLimit\", \"bodyTimeoutMs\", \"requestTimeoutMs\", \"eventTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\", \"ttlSeconds\"] as key\n value = result[key]\n if !is_number(value) or !Number/isSafeInteger(value) or value < 0\n throw \"Invalid server option: ${key}\"\n if key != \"port\" and key != \"maxQueue\" and value == 0\n throw \"Server option must be positive: ${key}\"\n if key.endsWith(\"Ms\") and value > 2147483647\n throw \"Timeout exceeds timer limit: ${key}\"\n if result.port > 65535 or result.workers > 128 or result.ttlSeconds > 86400\n throw \"Server option exceeds supported limit\"\n if !is_bool(result.requestEvent)\n throw \"requestEvent must be boolean\"\n for [\"issuer\", \"audience\", \"activeKeyId\"] as key\n if !is_string(result[key]) or result[key] == \"\"\n throw \"Missing JWT option: ${key}\"\n if !is_object(result.keys) or is_array(result.keys)\n throw \"JWT keys must be a map\"\n keys = Object/assign(Object/create(null), result.keys)\n if result.key != null\n keys[result.activeKeyId] = result.key\n if !Object/hasOwn(keys, result.activeKeyId)\n throw \"JWT signing key is required\"\n for keys as id: secret\n if !is_string(secret) or secret.length < 32\n throw \"JWT keys must contain at least 32 characters\"\n result.keys = keys\n result.key = null\n result.database = databaseOptions(result.root, result.database, result.workers)\n result.redis = redisOptions(result.redis)\n result.static = staticOptions(result.root, result.static)\n return result\n\ndeployment(directory)\n file = path/join(directory, \"deploy.xml\")\n root = xml2obj(fs/readFileSync(file, \"utf8\"))\n if root.tag() != \"deploy\"\n throw \"Expected deploy root\"\n attributes(root, [])\n settings = {}\n seen = []\n for root.children() as tag\n name = tag.tag()\n if seen.includes(name) or tag.children().length != 0 or tag.innerText().trim() != \"\"\n throw \"Duplicate or nonempty deployment section: ${name}\"\n seen.add(name)\n switch name\n case \"static\"\n attributes(tag, [\"path\", \"directory\", \"index\"])\n settings.static = tag.attributes()\n continue\n case \"http\"\n attributes(tag, [\"host\", \"port\", \"bodyLimit\", \"bodyTimeoutMs\"])\n case \"workers\"\n attributes(tag, [\"workers\", \"maxQueue\", \"requestTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\"])\n case \"events\"\n attributes(tag, [\"requestEvent\", \"eventTimeoutMs\"])\n case \"redis\"\n attributes(tag, [\"connectionEnv\", \"namespace\", \"maxPendingPolls\"])\n settings.redis = tag.attributes()\n continue\n case \"database\"\n attributes(tag, [\"type\", \"file\", \"connectionEnv\", \"busyTimeoutMs\", \"queryTimeoutMs\", \"connectTimeoutSeconds\"])\n settings.database = tag.attributes()\n continue\n case \"jwt\"\n attributes(tag, [\"issuer\", \"audience\", \"ttlSeconds\", \"keyEnv\", \"keysFile\", \"activeKeyId\"])\n else\n throw \"Unknown deployment section: ${name}\"\n for tag.attributes() as key: value\n if key == \"keyEnv\"\n settings.key = process.env[value]\n if settings.key == null\n throw \"JWT key environment variable is missing: ${value}\"\n else\n if key == \"keysFile\"\n settings.keys = from_json(fs/readFileSync(path/resolve(directory, value), \"utf8\"))\n else\n if contains([\"host\", \"issuer\", \"audience\", \"activeKeyId\"], key)\n settings[key] = value\n else\n if key == \"requestEvent\"\n if value != \"true\" and value != \"false\"\n throw \"requestEvent must be true or false\"\n settings[key] = value == \"true\"\n else\n settings[key] = Number(value)\n return options(directory, settings)\n\ncontains(items, value)\n return items.includes(value)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDA;;;;;;;;"}
|
package/dist/response.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { contains } from "./options.mjs";
|
|
2
|
+
import { HttpFailure } from "./http_failure.mjs";
|
|
2
3
|
import * as http from "node:http";
|
|
3
4
|
import {
|
|
4
5
|
fromJson as __fromJson,
|
|
@@ -58,5 +59,12 @@ export class Response {
|
|
|
58
59
|
return { status: __normalize(this.#status), headers: __fromJson(__toJson(__normalize(this.#headers))), body: body };
|
|
59
60
|
return null;
|
|
60
61
|
}
|
|
62
|
+
reject(status, code) {
|
|
63
|
+
__requireArity(arguments.length, [0, 1], "reject");
|
|
64
|
+
status = __boundary(status);
|
|
65
|
+
code = __boundary(code);
|
|
66
|
+
throw new HttpFailure(status, code, __normalize(this.#headers));
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
61
69
|
}
|
|
62
70
|
//# sourceMappingURL=response.mjs.map
|
package/dist/response.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.mjs","sources":["response.lango"],"sourcesContent":["import js \"node:http\" as http\n\nimport \"./options\"\n contains(items, value)\nexport class Response()\n methods\n status(code)\n header(name, value)\n result(body)\n\nResponse()\n @status = 200\n @headers = {\n \"content-type\": \"application/json; charset=utf-8\"\n \"cache-control\": \"no-store\"\n }\n\nResponse:status(code)\n if !is_number(code) or code % 1 != 0 or code < 200 or code > 599 or code == 204 or code == 205 or code == 304\n throw \"Response requires a JSON-compatible HTTP status\"\n @status = code\n return @this\n\nResponse:header(name, value)\n if !is_string(name) or !is_string(value)\n throw \"Header name and value must be strings\"\n name = name.toLowerCase()\n if contains([\"x-lango-token\", \"content-length\", \"transfer-encoding\", \"connection\", \"content-type\"], name)\n throw \"Reserved response header: ${name}\"\n if name.includes(\"\\r\") or name.includes(\"\\n\") or value.includes(\"\\r\") or value.includes(\"\\n\")\n throw \"Invalid response header\"\n http/validateHeaderName(name)\n http/validateHeaderValue(name, value)\n @headers[name] = value\n return @this\n\nResponse:result(body)\n return {\n status: @status\n headers: from_json(to_json(@headers))\n body: body\n }\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"response.mjs","sources":["response.lango"],"sourcesContent":["import js \"node:http\" as http\n\nimport \"./options\"\n contains(items, value)\nimport \"./http_failure\"\n class HttpFailure\nexport class Response()\n methods\n status(code)\n header(name, value)\n result(body)\n reject(status, code)\n\nResponse()\n @status = 200\n @headers = {\n \"content-type\": \"application/json; charset=utf-8\"\n \"cache-control\": \"no-store\"\n }\n\nResponse:status(code)\n if !is_number(code) or code % 1 != 0 or code < 200 or code > 599 or code == 204 or code == 205 or code == 304\n throw \"Response requires a JSON-compatible HTTP status\"\n @status = code\n return @this\n\nResponse:header(name, value)\n if !is_string(name) or !is_string(value)\n throw \"Header name and value must be strings\"\n name = name.toLowerCase()\n if contains([\"x-lango-token\", \"content-length\", \"transfer-encoding\", \"connection\", \"content-type\"], name)\n throw \"Reserved response header: ${name}\"\n if name.includes(\"\\r\") or name.includes(\"\\n\") or value.includes(\"\\r\") or value.includes(\"\\n\")\n throw \"Invalid response header\"\n http/validateHeaderName(name)\n http/validateHeaderValue(name, value)\n @headers[name] = value\n return @this\n\nResponse:result(body)\n return {\n status: @status\n headers: from_json(to_json(@headers))\n body: body\n }\n\nResponse:reject(status, code)\n throw new HttpFailure(status, code, @headers)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
requireBoolean as __requireBoolean,
|
|
3
|
+
normalize as __normalize,
|
|
4
|
+
boundary as __boundary,
|
|
5
|
+
requireArity as __requireArity,
|
|
6
|
+
} from "./.lango/runtime.mjs";
|
|
7
|
+
|
|
8
|
+
export function routeInfo(definitions) {
|
|
9
|
+
__requireArity(arguments.length, [0], "routeInfo");
|
|
10
|
+
definitions = __boundary(definitions);
|
|
11
|
+
let result = [];
|
|
12
|
+
for (const route of definitions) {
|
|
13
|
+
let kind = __normalize(route.kind);
|
|
14
|
+
if (__requireBoolean(kind === null)) {
|
|
15
|
+
kind = "endpoint";
|
|
16
|
+
}
|
|
17
|
+
result.push({ path: __normalize(route.path), kind: kind, auth: __normalize(route.auth), middleware: __normalize(route.middleware) });
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=route_info.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route_info.mjs","sources":["route_info.lango"],"sourcesContent":["export routeInfo(definitions)\n\nrouteInfo(definitions)\n result = []\n for definitions as route\n kind = route.kind\n if kind == null\n kind = \"endpoint\"\n result.add({\n path: route.path\n kind: kind\n auth: route.auth\n middleware: route.middleware\n })\n return result\n"],"names":[],"mappings":";;;;;;;AAEA;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
isNumber as __isNumber,
|
|
4
|
+
isString as __isString,
|
|
5
|
+
isBool as __isBool,
|
|
6
|
+
isObject as __isObject,
|
|
7
|
+
isArray as __isArray,
|
|
8
|
+
entries as __entries,
|
|
9
|
+
range as __range,
|
|
10
|
+
requireBoolean as __requireBoolean,
|
|
11
|
+
normalize as __normalize,
|
|
12
|
+
finite as __finite,
|
|
13
|
+
boundary as __boundary,
|
|
14
|
+
requireArity as __requireArity,
|
|
15
|
+
} from "./.lango/runtime.mjs";
|
|
16
|
+
|
|
17
|
+
export function routePattern(path) {
|
|
18
|
+
__requireArity(arguments.length, [0], "routePattern");
|
|
19
|
+
path = __boundary(path);
|
|
20
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(path))) || __requireBoolean(!__requireBoolean(__normalize(path.startsWith("/"))))) || __requireBoolean(__normalize(path.includes("?")))) || __requireBoolean(__normalize(path.includes("#")))) || __requireBoolean(__normalize(path.includes("\\")))) || __requireBoolean(__normalize(path.includes("//"))))) {
|
|
21
|
+
throw "Endpoint requires an absolute URL path";
|
|
22
|
+
}
|
|
23
|
+
if (__requireBoolean(__requireBoolean(__normalize(path.includes("*"))) && __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(path.endsWith("/*")))) || __requireBoolean(__normalize(path.indexOf("*")) !== __normalize(path.lastIndexOf("*")))))) {
|
|
24
|
+
throw "Only trailing /* wildcards are supported";
|
|
25
|
+
}
|
|
26
|
+
let segments = __normalize(path.split("/"));
|
|
27
|
+
segments = __normalize(segments.slice(1));
|
|
28
|
+
let wildcard = __normalize(path.endsWith("/*"));
|
|
29
|
+
if (__requireBoolean(wildcard)) {
|
|
30
|
+
__normalize(segments.pop());
|
|
31
|
+
}
|
|
32
|
+
let names = [];
|
|
33
|
+
let shape = [];
|
|
34
|
+
let identifier = new (__hostGlobal(["RegExp"]))("^[A-Za-z_][A-Za-z0-9_]*$");
|
|
35
|
+
let reserved = ["__proto__", "constructor", "prototype"];
|
|
36
|
+
for (const segment of segments) {
|
|
37
|
+
if (__requireBoolean(__requireBoolean(__normalize(segment.includes("{"))) || __requireBoolean(__normalize(segment.includes("}"))))) {
|
|
38
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(segment.startsWith("{")))) || __requireBoolean(!__requireBoolean(__normalize(segment.endsWith("}")))))) {
|
|
39
|
+
throw "Path parameters must occupy an entire segment";
|
|
40
|
+
}
|
|
41
|
+
let name = __normalize(segment.slice(1, __finite(-1)));
|
|
42
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(identifier.test(name)))) || __requireBoolean(__normalize(reserved.includes(name))))) {
|
|
43
|
+
throw `Invalid path parameter name: ${name}`;
|
|
44
|
+
}
|
|
45
|
+
if (__requireBoolean(__normalize(names.includes(name)))) {
|
|
46
|
+
throw `Duplicate path parameter: ${name}`;
|
|
47
|
+
}
|
|
48
|
+
names.push(name);
|
|
49
|
+
shape.push("{}");
|
|
50
|
+
} else {
|
|
51
|
+
shape.push(segment);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (__requireBoolean(wildcard)) {
|
|
55
|
+
shape.push("*");
|
|
56
|
+
}
|
|
57
|
+
let key = __normalize(shape.join("/"));
|
|
58
|
+
key = `/${key}`;
|
|
59
|
+
return { segments: segments, wildcard: wildcard, key: key };
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function matchPath(pattern, path) {
|
|
64
|
+
__requireArity(arguments.length, [0, 1], "matchPath");
|
|
65
|
+
pattern = __boundary(pattern);
|
|
66
|
+
path = __boundary(path);
|
|
67
|
+
let parts = __normalize(path.split("/"));
|
|
68
|
+
if (__requireBoolean(__normalize(parts[0]) !== "")) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
parts = __normalize(parts.slice(1));
|
|
72
|
+
let count = pattern.segments.length;
|
|
73
|
+
if (__requireBoolean(__normalize(pattern.wildcard))) {
|
|
74
|
+
if (__requireBoolean(__normalize(parts.length) <= count)) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
if (__requireBoolean(__normalize(parts.length) !== count)) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
let parameters = { };
|
|
83
|
+
for (const [index, segment] of __entries(__normalize(pattern.segments))) {
|
|
84
|
+
let value = __normalize(parts[index]);
|
|
85
|
+
if (__requireBoolean(__normalize(segment.startsWith("{")))) {
|
|
86
|
+
if (__requireBoolean(value === "")) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
let name = __normalize(segment.slice(1, __finite(-1)));
|
|
90
|
+
parameters[name] = value;
|
|
91
|
+
} else {
|
|
92
|
+
if (__requireBoolean(segment !== value)) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return parameters;
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function moreSpecific(candidate, current) {
|
|
102
|
+
__requireArity(arguments.length, [0, 1], "moreSpecific");
|
|
103
|
+
candidate = __boundary(candidate);
|
|
104
|
+
current = __boundary(current);
|
|
105
|
+
if (__requireBoolean(current === null)) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
if (__requireBoolean(__normalize(candidate.wildcard) !== __normalize(current.wildcard))) {
|
|
109
|
+
return !__requireBoolean(__normalize(candidate.wildcard));
|
|
110
|
+
}
|
|
111
|
+
let first = __normalize(candidate.segments);
|
|
112
|
+
let second = __normalize(current.segments);
|
|
113
|
+
let count = __normalize(first.length);
|
|
114
|
+
if (__requireBoolean(__normalize(second.length) < count)) {
|
|
115
|
+
count = __normalize(second.length);
|
|
116
|
+
}
|
|
117
|
+
for (const index of __range(0, count)) {
|
|
118
|
+
let segment = __normalize(first[index]);
|
|
119
|
+
let alternative = __normalize(second[index]);
|
|
120
|
+
let literal = !__requireBoolean(__normalize(segment.startsWith("{")));
|
|
121
|
+
let other = !__requireBoolean(__normalize(alternative.startsWith("{")));
|
|
122
|
+
if (__requireBoolean(literal !== other)) {
|
|
123
|
+
return literal;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return __normalize(first.length) > __normalize(second.length);
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=route_pattern.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route_pattern.mjs","sources":["route_pattern.lango"],"sourcesContent":["import js global RegExp\n\nexport routePattern(path)\nexport matchPath(pattern, path)\nexport moreSpecific(candidate, current)\n\nroutePattern(path)\n if !is_string(path) or !path.startsWith(\"/\") or path.includes(\"?\") or path.includes(\"#\") or path.includes(\"\\\\\") or path.includes(\"//\")\n throw \"Endpoint requires an absolute URL path\"\n if path.includes(\"*\") and (!path.endsWith(\"/*\") or path.indexOf(\"*\") != path.lastIndexOf(\"*\"))\n throw \"Only trailing /* wildcards are supported\"\n segments = path.split(\"/\")\n segments = segments.slice(1)\n wildcard = path.endsWith(\"/*\")\n if wildcard\n segments.pop()\n names = []\n shape = []\n identifier = new RegExp(\"^[A-Za-z_][A-Za-z0-9_]*$\")\n reserved = [\"__proto__\", \"constructor\", \"prototype\"]\n for segments as segment\n if segment.includes(\"{\") or segment.includes(\"}\")\n if !segment.startsWith(\"{\") or !segment.endsWith(\"}\")\n throw \"Path parameters must occupy an entire segment\"\n name = segment.slice(1, -1)\n if !identifier.test(name) or reserved.includes(name)\n throw \"Invalid path parameter name: ${name}\"\n if names.includes(name)\n throw \"Duplicate path parameter: ${name}\"\n names.add(name)\n shape.add(\"{}\")\n else\n shape.add(segment)\n if wildcard\n shape.add(\"*\")\n key = shape.join(\"/\")\n key = \"/${key}\"\n return {\n segments: segments\n wildcard: wildcard\n key: key\n }\n\nmatchPath(pattern, path)\n parts = path.split(\"/\")\n if parts[0] != \"\"\n return null\n parts = parts.slice(1)\n count = pattern.segments.length\n if pattern.wildcard\n if parts.length <= count\n return null\n else\n if parts.length != count\n return null\n parameters = {}\n for pattern.segments as index: segment\n value = parts[index]\n if segment.startsWith(\"{\")\n if value == \"\"\n return null\n name = segment.slice(1, -1)\n parameters[name] = value\n else\n if segment != value\n return null\n return parameters\n\nmoreSpecific(candidate, current)\n if current == null\n return true\n if candidate.wildcard != current.wildcard\n return !candidate.wildcard\n first = candidate.segments\n second = current.segments\n count = first.length\n if second.length < count\n count = second.length\n for range(count) as index\n segment = first[index]\n alternative = second[index]\n literal = !segment.startsWith(\"{\")\n other = !alternative.startsWith(\"{\")\n if literal != other\n return literal\n return first.length > second.length\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/routes.mjs
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { routePattern } from "./route_pattern.mjs";
|
|
1
2
|
import { contains } from "./options.mjs";
|
|
2
3
|
import { attributes } from "./options.mjs";
|
|
4
|
+
import { middlewareName } from "./middleware.mjs";
|
|
3
5
|
import { Endpoint } from "./endpoint.mjs";
|
|
6
|
+
import { longpollOptions } from "./longpoll_options.mjs";
|
|
4
7
|
import * as fs from "node:fs";
|
|
5
8
|
import * as path from "node:path";
|
|
6
9
|
import * as modules from "node:module";
|
|
7
10
|
import {
|
|
11
|
+
hostGlobal as __hostGlobal,
|
|
8
12
|
xml2obj as __xml2obj,
|
|
9
13
|
fromJson as __fromJson,
|
|
10
14
|
toJson as __toJson,
|
|
@@ -14,6 +18,7 @@ import {
|
|
|
14
18
|
isObject as __isObject,
|
|
15
19
|
isArray as __isArray,
|
|
16
20
|
includes as __includes,
|
|
21
|
+
entries as __entries,
|
|
17
22
|
add as __add,
|
|
18
23
|
requireBoolean as __requireBoolean,
|
|
19
24
|
normalize as __normalize,
|
|
@@ -26,23 +31,76 @@ export class Routes {
|
|
|
26
31
|
#root;
|
|
27
32
|
#items;
|
|
28
33
|
#events;
|
|
34
|
+
#middlewares;
|
|
29
35
|
constructor(root) {
|
|
30
36
|
__requireArity(arguments.length, [0], "constructor");
|
|
31
37
|
root = __boundary(root);
|
|
32
38
|
this.#root = __normalize(path.resolve(root));
|
|
33
39
|
this.#items = [];
|
|
34
40
|
this.#events = { };
|
|
41
|
+
this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
|
|
35
42
|
}
|
|
36
43
|
endpoint(spec) {
|
|
37
44
|
__requireArity(arguments.length, [0], "endpoint");
|
|
38
45
|
spec = __boundary(spec);
|
|
39
46
|
let item = new Endpoint(spec);
|
|
47
|
+
this.#checkPath(__normalize(item.path));
|
|
48
|
+
__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) });
|
|
49
|
+
return this;
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
#checkPath(path) {
|
|
53
|
+
__requireArity(arguments.length, [0], "checkPath");
|
|
54
|
+
path = __boundary(path);
|
|
55
|
+
let pattern = routePattern(path);
|
|
40
56
|
for (const other of __normalize(this.#items)) {
|
|
41
|
-
if (__requireBoolean(__normalize(other.path) ===
|
|
42
|
-
throw `Duplicate endpoint: ${
|
|
57
|
+
if (__requireBoolean(__normalize(other.path) === path)) {
|
|
58
|
+
throw `Duplicate endpoint: ${path}`;
|
|
59
|
+
}
|
|
60
|
+
let existing = routePattern(__normalize(other.path));
|
|
61
|
+
if (__requireBoolean(__normalize(existing.key) === __normalize(pattern.key))) {
|
|
62
|
+
throw `Conflicting endpoint pattern: ${path}`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
addMiddleware(name, spec) {
|
|
69
|
+
__requireArity(arguments.length, [0, 1], "addMiddleware");
|
|
70
|
+
name = __boundary(name);
|
|
71
|
+
spec = __boundary(spec);
|
|
72
|
+
middlewareName(name);
|
|
73
|
+
if (__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#middlewares), name)))) {
|
|
74
|
+
throw `Duplicate middleware: ${name}`;
|
|
75
|
+
}
|
|
76
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
77
|
+
throw "Middleware requires a module and exported handler";
|
|
78
|
+
}
|
|
79
|
+
for (const [key, value] of __entries(spec)) {
|
|
80
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler")) && __requireBoolean(key !== "validate"))) {
|
|
81
|
+
throw `Unknown middleware option: ${key}`;
|
|
43
82
|
}
|
|
44
83
|
}
|
|
45
|
-
|
|
84
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
|
|
85
|
+
throw "Middleware handler is required";
|
|
86
|
+
}
|
|
87
|
+
if (__requireBoolean(__requireBoolean(__normalize(spec.validate) !== null) && __requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.validate)))) || __requireBoolean(__normalize(spec.validate) === "")))) {
|
|
88
|
+
throw "Middleware validate must name an exported function";
|
|
89
|
+
}
|
|
90
|
+
this.#middlewares[name] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler), validate: __normalize(spec.validate) };
|
|
91
|
+
return this;
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
middlewares() {
|
|
95
|
+
return __fromJson(__toJson(__normalize(this.#middlewares)));
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
longpoll(spec) {
|
|
99
|
+
__requireArity(arguments.length, [0], "longpoll");
|
|
100
|
+
spec = __boundary(spec);
|
|
101
|
+
let item = longpollOptions(spec);
|
|
102
|
+
this.#checkPath(__normalize(item.path));
|
|
103
|
+
__add(this.#items, item);
|
|
46
104
|
return this;
|
|
47
105
|
return null;
|
|
48
106
|
}
|
|
@@ -166,6 +224,15 @@ export class Routes {
|
|
|
166
224
|
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
167
225
|
leaf(tag);
|
|
168
226
|
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth });
|
|
227
|
+
} else if (__normalize(tag.tag()) === "longpoll") {
|
|
228
|
+
attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"]);
|
|
229
|
+
leaf(tag);
|
|
230
|
+
spec.path = joinPath(prefix, __normalize(spec.path));
|
|
231
|
+
spec.auth = auth;
|
|
232
|
+
if (__requireBoolean(__normalize(spec.fallback) !== null)) {
|
|
233
|
+
spec.fallback = joinPath(prefix, __normalize(spec.fallback));
|
|
234
|
+
}
|
|
235
|
+
this.longpoll(spec);
|
|
169
236
|
} else if (__normalize(tag.tag()) === "group") {
|
|
170
237
|
attributes(tag, ["path", "module", "auth"]);
|
|
171
238
|
this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false);
|
|
@@ -173,6 +240,13 @@ export class Routes {
|
|
|
173
240
|
attributes(tag, ["module", "path"]);
|
|
174
241
|
leaf(tag);
|
|
175
242
|
this.#includeAt(spec, directory, prefix, stack, auth);
|
|
243
|
+
} else if (__normalize(tag.tag()) === "middleware") {
|
|
244
|
+
attributes(tag, ["name", "module", "handler", "validate"]);
|
|
245
|
+
leaf(tag);
|
|
246
|
+
if (__requireBoolean(!__requireBoolean(allowEvents))) {
|
|
247
|
+
throw "Middleware declarations are only allowed in the application's root routes.xml";
|
|
248
|
+
}
|
|
249
|
+
this.addMiddleware(__normalize(spec.name), { module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler), validate: __normalize(spec.validate) });
|
|
176
250
|
} else if (__normalize(tag.tag()) === "event") {
|
|
177
251
|
attributes(tag, ["name", "module", "handler"]);
|
|
178
252
|
leaf(tag);
|
|
@@ -181,7 +255,9 @@ export class Routes {
|
|
|
181
255
|
}
|
|
182
256
|
this.event({ name: __normalize(spec.name), module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
183
257
|
} else {
|
|
184
|
-
|
|
258
|
+
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
259
|
+
leaf(tag);
|
|
260
|
+
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, middleware: __normalize(tag.tag()) });
|
|
185
261
|
}
|
|
186
262
|
}
|
|
187
263
|
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 ==
|
|
1
|
+
{"version":3,"file":"routes.mjs","sources":["routes.lango"],"sourcesContent":["import \"./route_pattern\"\n routePattern(path)\nimport \"./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 checkPath(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:checkPath(path)\n pattern = routePattern(path)\n for @items as other\n if other.path == path\n throw \"Duplicate endpoint: ${path}\"\n existing = routePattern(other.path)\n if existing.key == pattern.key\n throw \"Conflicting endpoint pattern: ${path}\"\n return null\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\" and key != \"validate\"\n throw \"Unknown middleware option: ${key}\"\n if !is_string(spec.handler) or spec.handler == \"\"\n throw \"Middleware handler is required\"\n if spec.validate != null and (!is_string(spec.validate) or spec.validate == \"\")\n throw \"Middleware validate must name an exported function\"\n @middlewares[name] = {\n module: resolveModule(spec.module, @root)\n handler: spec.handler\n validate: spec.validate\n }\n return @this\n\nRoutes:middlewares()\n return from_json(to_json(@middlewares))\n\nRoutes:longpoll(spec)\n item = longpollOptions(spec)\n checkPath(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\", \"validate\"])\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 validate: spec.validate\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgGA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;AA6GA;;;;;;;;;;"}
|