@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
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
hostGlobalCall as __hostGlobalCall,
|
|
4
|
+
isNumber as __isNumber,
|
|
5
|
+
isString as __isString,
|
|
6
|
+
isBool as __isBool,
|
|
7
|
+
isObject as __isObject,
|
|
8
|
+
isArray as __isArray,
|
|
9
|
+
entries as __entries,
|
|
10
|
+
requireBoolean as __requireBoolean,
|
|
11
|
+
normalize as __normalize,
|
|
12
|
+
boundary as __boundary,
|
|
13
|
+
requireArity as __requireArity,
|
|
14
|
+
} from "./.lango/runtime.mjs";
|
|
15
|
+
|
|
16
|
+
export function longpollOptions(spec) {
|
|
17
|
+
__requireArity(arguments.length, [0], "longpollOptions");
|
|
18
|
+
spec = __boundary(spec);
|
|
19
|
+
let result = { kind: "longpoll", path: __normalize(spec.path), auth: "login", waitTimeoutMs: 20000, retentionMs: 300000, maxMessages: 1000, fallback: null };
|
|
20
|
+
for (const [key, value] of __entries(spec)) {
|
|
21
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(result, key)))) || __requireBoolean(key === "kind"))) {
|
|
22
|
+
throw `Unknown longpoll option: ${key}`;
|
|
23
|
+
}
|
|
24
|
+
result[key] = value;
|
|
25
|
+
}
|
|
26
|
+
if (__requireBoolean(__normalize(result.auth) === null)) {
|
|
27
|
+
result.auth = "login";
|
|
28
|
+
}
|
|
29
|
+
validPath(__normalize(result.path));
|
|
30
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(result.auth)))) || __requireBoolean(__normalize(result.auth) === "")) || __requireBoolean(__normalize(result.auth.includes(","))))) {
|
|
31
|
+
throw "Longpoll auth must name one permission";
|
|
32
|
+
}
|
|
33
|
+
if (__requireBoolean(__normalize(result.fallback) !== null)) {
|
|
34
|
+
validPath(__normalize(result.fallback));
|
|
35
|
+
}
|
|
36
|
+
let limits = { waitTimeoutMs: 60000, retentionMs: 86400000, maxMessages: 10000 };
|
|
37
|
+
for (const [key, limit] of __entries(limits)) {
|
|
38
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isNumber(__normalize(result[key])))) && __requireBoolean(!__requireBoolean(__isString(__normalize(result[key])))))) {
|
|
39
|
+
throw `Invalid longpoll option: ${key}`;
|
|
40
|
+
}
|
|
41
|
+
let value = __hostGlobalCall(["Number"], __normalize(result[key]));
|
|
42
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(value)))) || __requireBoolean(value < 1)) || __requireBoolean(value > limit))) {
|
|
43
|
+
throw `Invalid longpoll option: ${key}`;
|
|
44
|
+
}
|
|
45
|
+
result[key] = value;
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function validPath(value) {
|
|
52
|
+
__requireArity(arguments.length, [0], "validPath");
|
|
53
|
+
value = __boundary(value);
|
|
54
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(!__requireBoolean(__normalize(value.startsWith("/"))))) || __requireBoolean(__normalize(value.includes("*")))) || __requireBoolean(__normalize(value.includes("?")))) || __requireBoolean(__normalize(value.includes("#")))) || __requireBoolean(__normalize(value.includes("\\")))) || __requireBoolean(__normalize(value.includes("//"))))) {
|
|
55
|
+
throw "Longpoll requires an exact absolute URL path";
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function redisOptions(spec) {
|
|
62
|
+
__requireArity(arguments.length, [0], "redisOptions");
|
|
63
|
+
spec = __boundary(spec);
|
|
64
|
+
if (__requireBoolean(spec === null)) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
68
|
+
throw "Redis configuration must be a map";
|
|
69
|
+
}
|
|
70
|
+
let allowed = ["connectionEnv", "namespace", "maxPendingPolls"];
|
|
71
|
+
for (const [key, value] of __entries(spec)) {
|
|
72
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
73
|
+
throw `Unknown Redis option: ${key}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.connectionEnv)))) || __requireBoolean(__normalize(spec.connectionEnv) === "")) || __requireBoolean(!__requireBoolean(__isString(__normalize(spec.namespace))))) || __requireBoolean(__normalize(spec.namespace) === "")) || __requireBoolean(spec.namespace.length > 128))) {
|
|
77
|
+
throw "Redis requires connectionEnv and namespace";
|
|
78
|
+
}
|
|
79
|
+
let name = __normalize(spec.connectionEnv);
|
|
80
|
+
let value = __normalize(__hostGlobal(["process"]).env[name]);
|
|
81
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(value === ""))) {
|
|
82
|
+
throw "Redis connection environment variable is missing";
|
|
83
|
+
}
|
|
84
|
+
let url = new (__hostGlobal(["URL"]))(value);
|
|
85
|
+
let protocols = ["redis:", "rediss:"];
|
|
86
|
+
if (__requireBoolean(!__requireBoolean(__normalize(protocols.includes(__normalize(url.protocol)))))) {
|
|
87
|
+
throw "Redis requires a redis:// or rediss:// URL";
|
|
88
|
+
}
|
|
89
|
+
let maximum = 1024;
|
|
90
|
+
if (__requireBoolean(__normalize(spec.maxPendingPolls) !== null)) {
|
|
91
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isNumber(__normalize(spec.maxPendingPolls)))) && __requireBoolean(!__requireBoolean(__isString(__normalize(spec.maxPendingPolls)))))) {
|
|
92
|
+
throw "Invalid Redis maxPendingPolls";
|
|
93
|
+
}
|
|
94
|
+
maximum = __hostGlobalCall(["Number"], __normalize(spec.maxPendingPolls));
|
|
95
|
+
}
|
|
96
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(maximum)))) || __requireBoolean(maximum < 1)) || __requireBoolean(maximum > 10000))) {
|
|
97
|
+
throw "Invalid Redis maxPendingPolls";
|
|
98
|
+
}
|
|
99
|
+
return { connectionEnv: __normalize(spec.connectionEnv), namespace: __normalize(spec.namespace), maxPendingPolls: maximum };
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=longpoll_options.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longpoll_options.mjs","sources":["longpoll_options.lango"],"sourcesContent":["import js global Number\nimport js global Object\nimport js global process\nimport js global URL\n\nexport longpollOptions(spec)\nexport redisOptions(spec)\n\nlongpollOptions(spec)\n result = {\n kind: \"longpoll\"\n path: spec.path\n auth: \"login\"\n waitTimeoutMs: 20000\n retentionMs: 300000\n maxMessages: 1000\n fallback: null\n }\n for spec as key: value\n if !Object/hasOwn(result, key) or key == \"kind\"\n throw \"Unknown longpoll option: ${key}\"\n result[key] = value\n if result.auth == null\n result.auth = \"login\"\n validPath(result.path)\n if !is_string(result.auth) or result.auth == \"\" or result.auth.includes(\",\")\n throw \"Longpoll auth must name one permission\"\n if result.fallback != null\n validPath(result.fallback)\n limits = {\n waitTimeoutMs: 60000\n retentionMs: 86400000\n maxMessages: 10000\n }\n for limits as key: limit\n if !is_number(result[key]) and !is_string(result[key])\n throw \"Invalid longpoll option: ${key}\"\n value = Number(result[key])\n if !Number/isSafeInteger(value) or value < 1 or value > limit\n throw \"Invalid longpoll option: ${key}\"\n result[key] = value\n return result\n\nvalidPath(value)\n if !is_string(value) or !value.startsWith(\"/\") or value.includes(\"*\") or value.includes(\"?\") or value.includes(\"#\") or value.includes(\"\\\\\") or value.includes(\"//\")\n throw \"Longpoll requires an exact absolute URL path\"\n return null\n\nredisOptions(spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Redis configuration must be a map\"\n allowed = [\"connectionEnv\", \"namespace\", \"maxPendingPolls\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown Redis option: ${key}\"\n if !is_string(spec.connectionEnv) or spec.connectionEnv == \"\" or !is_string(spec.namespace) or spec.namespace == \"\" or spec.namespace.length > 128\n throw \"Redis requires connectionEnv and namespace\"\n name = spec.connectionEnv\n value = process.env[name]\n if !is_string(value) or value == \"\"\n throw \"Redis connection environment variable is missing\"\n url = new URL(value)\n protocols = [\"redis:\", \"rediss:\"]\n if !protocols.includes(url.protocol)\n throw \"Redis requires a redis:// or rediss:// URL\"\n maximum = 1024\n if spec.maxPendingPolls != null\n if !is_number(spec.maxPendingPolls) and !is_string(spec.maxPendingPolls)\n throw \"Invalid Redis maxPendingPolls\"\n maximum = Number(spec.maxPendingPolls)\n if !Number/isSafeInteger(maximum) or maximum < 1 or maximum > 10000\n throw \"Invalid Redis maxPendingPolls\"\n return {\n connectionEnv: spec.connectionEnv\n namespace: spec.namespace\n maxPendingPolls: maximum\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
hostGlobalCall as __hostGlobalCall,
|
|
4
|
+
fromJson as __fromJson,
|
|
5
|
+
toJson as __toJson,
|
|
6
|
+
isNumber as __isNumber,
|
|
7
|
+
isString as __isString,
|
|
8
|
+
isBool as __isBool,
|
|
9
|
+
isObject as __isObject,
|
|
10
|
+
isArray as __isArray,
|
|
11
|
+
requireBoolean as __requireBoolean,
|
|
12
|
+
normalize as __normalize,
|
|
13
|
+
finite as __finite,
|
|
14
|
+
numericAdd as __numericAdd,
|
|
15
|
+
call as __call,
|
|
16
|
+
boundary as __boundary,
|
|
17
|
+
requireArity as __requireArity,
|
|
18
|
+
withArity as __withArity,
|
|
19
|
+
} from "./.lango/runtime.mjs";
|
|
20
|
+
|
|
21
|
+
export class LongpollSession {
|
|
22
|
+
#broker;
|
|
23
|
+
#route;
|
|
24
|
+
#authorize;
|
|
25
|
+
#body;
|
|
26
|
+
#cancelled;
|
|
27
|
+
#timer;
|
|
28
|
+
#sleep;
|
|
29
|
+
#token;
|
|
30
|
+
#deadline;
|
|
31
|
+
#input;
|
|
32
|
+
#cursor;
|
|
33
|
+
constructor(broker, route, authorize, body) {
|
|
34
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "constructor");
|
|
35
|
+
broker = __boundary(broker);
|
|
36
|
+
route = __boundary(route);
|
|
37
|
+
authorize = __boundary(authorize);
|
|
38
|
+
body = __boundary(body);
|
|
39
|
+
this.#broker = broker;
|
|
40
|
+
this.#route = route;
|
|
41
|
+
this.#authorize = authorize;
|
|
42
|
+
this.#body = body;
|
|
43
|
+
this.#cancelled = false;
|
|
44
|
+
this.#timer = null;
|
|
45
|
+
this.#sleep = null;
|
|
46
|
+
this.#token = null;
|
|
47
|
+
this.#deadline = 0;
|
|
48
|
+
this.#input = null;
|
|
49
|
+
this.#cursor = null;
|
|
50
|
+
}
|
|
51
|
+
cancel() {
|
|
52
|
+
this.#cancelled = true;
|
|
53
|
+
__hostGlobalCall(["clearTimeout"], __normalize(this.#timer));
|
|
54
|
+
if (__requireBoolean(__normalize(this.#sleep) !== null)) {
|
|
55
|
+
__normalize(this.#sleep.resolve(null));
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
run() {
|
|
61
|
+
try {
|
|
62
|
+
this.#input = __fromJson(__normalize(this.#body));
|
|
63
|
+
let input = __normalize(this.#input);
|
|
64
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(input))) || __requireBoolean(__isArray(input))) || __requireBoolean(!__requireBoolean(__isString(__normalize(input.key))))) || __requireBoolean(__normalize(input.key) === "")) || __requireBoolean(__normalize(__hostGlobal(["Buffer"]).byteLength(__normalize(input.key))) > 1024))) {
|
|
65
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(failure(400, "InvalidLongpollInput")));
|
|
66
|
+
}
|
|
67
|
+
if (__requireBoolean(__requireBoolean(__normalize(input.cursor) !== null) && __requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(input.cursor)))) || __requireBoolean(input.cursor.length > 512)))) {
|
|
68
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(failure(400, "InvalidLongpollInput")));
|
|
69
|
+
}
|
|
70
|
+
this.#cursor = __normalize(input.cursor);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(failure(400, "InvalidJson")));
|
|
73
|
+
}
|
|
74
|
+
let auth = __normalize(this.#authorize);
|
|
75
|
+
return __call(auth, null, null).then(__withArity((result) => {
|
|
76
|
+
result = __boundary(result);
|
|
77
|
+
if (__requireBoolean(__normalize(result.status) !== 200)) {
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
this.#token = __normalize(result.authorization);
|
|
81
|
+
this.#deadline = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __normalize(this.#route.waitTimeoutMs));
|
|
82
|
+
return this.#poll();
|
|
83
|
+
return null;
|
|
84
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
85
|
+
error = __boundary(error);
|
|
86
|
+
return failure(503, "LongpollUnavailable");
|
|
87
|
+
return null;
|
|
88
|
+
}, [0], "closure")).finally(() => {
|
|
89
|
+
this.cancel();
|
|
90
|
+
return null;
|
|
91
|
+
});
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
#poll() {
|
|
95
|
+
if (__requireBoolean(__normalize(this.#cancelled))) {
|
|
96
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(failure(503, "Unavailable")));
|
|
97
|
+
}
|
|
98
|
+
return __normalize(this.#broker.read(__normalize(this.#route.path), __normalize(this.#input.key), __normalize(this.#cursor))).then(__withArity((batch) => {
|
|
99
|
+
batch = __boundary(batch);
|
|
100
|
+
if (__requireBoolean(__normalize(this.#cancelled))) {
|
|
101
|
+
return failure(503, "Unavailable");
|
|
102
|
+
}
|
|
103
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(batch.resyncRequired)) || __requireBoolean(batch.messages.length > 0)) || __requireBoolean(__normalize(__hostGlobal(["Date"]).now()) >= __normalize(this.#deadline)))) {
|
|
104
|
+
if (__requireBoolean(__normalize(batch.resyncRequired))) {
|
|
105
|
+
batch.fallback = __normalize(this.#route.fallback);
|
|
106
|
+
}
|
|
107
|
+
let auth = __normalize(this.#authorize);
|
|
108
|
+
return __call(auth, __normalize(this.#token), batch);
|
|
109
|
+
}
|
|
110
|
+
this.#cursor = __normalize(batch.cursor);
|
|
111
|
+
this.#sleep = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
112
|
+
let sleep = __normalize(this.#sleep);
|
|
113
|
+
let wake = () => {
|
|
114
|
+
__normalize(sleep.resolve(null));
|
|
115
|
+
return null;
|
|
116
|
+
};
|
|
117
|
+
this.#timer = __hostGlobalCall(["setTimeout"], wake, __normalize(__hostGlobal(["Math"]).min(250, __normalize(__hostGlobal(["Math"]).max(1, __finite(__normalize(this.#deadline) - __normalize(__hostGlobal(["Date"]).now())))))));
|
|
118
|
+
return __normalize(sleep.promise).then(__withArity((ignored) => {
|
|
119
|
+
ignored = __boundary(ignored);
|
|
120
|
+
return this.#poll();
|
|
121
|
+
return null;
|
|
122
|
+
}, [0], "closure"));
|
|
123
|
+
return null;
|
|
124
|
+
}, [0], "closure"));
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function failure(status, code) {
|
|
129
|
+
__requireArity(arguments.length, [0, 1], "failure");
|
|
130
|
+
status = __boundary(status);
|
|
131
|
+
code = __boundary(code);
|
|
132
|
+
return { status: status, headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }, body: __toJson({ error: code }) };
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=longpoll_session.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longpoll_session.mjs","sources":["longpoll_session.lango"],"sourcesContent":["import js global Promise\nimport js global Buffer\nimport js global Date\nimport js global Math\nimport js global setTimeout\nimport js global clearTimeout\n\nexport class LongpollSession(broker, route, authorize, body)\n methods\n run()\n cancel()\n\nLongpollSession(broker, route, authorize, body)\n @broker = broker\n @route = route\n @authorize = authorize\n @body = body\n @cancelled = false\n @timer = null\n @sleep = null\n @token = null\n @deadline = 0\n @input = null\n @cursor = null\n\nLongpollSession:cancel()\n @cancelled = true\n clearTimeout(@timer)\n if @sleep != null\n @sleep.resolve(null)\n return null\n\nLongpollSession:run()\n try\n @input = from_json(@body)\n input = @input\n if !is_object(input) or is_array(input) or !is_string(input.key) or input.key == \"\" or Buffer/byteLength(input.key) > 1024\n return Promise/resolve(failure(400, \"InvalidLongpollInput\"))\n if input.cursor != null and (!is_string(input.cursor) or input.cursor.length > 512)\n return Promise/resolve(failure(400, \"InvalidLongpollInput\"))\n @cursor = input.cursor\n catch error\n return Promise/resolve(failure(400, \"InvalidJson\"))\n auth = @authorize\n return chain auth(null, null)\n then(result)\n if result.status != 200\n return result\n @token = result.authorization\n @deadline = Date/now() + @route.waitTimeoutMs\n return poll()\n catch(error)\n return failure(503, \"LongpollUnavailable\")\n finally\n cancel()\n\nLongpollSession:poll()\n if @cancelled\n return Promise/resolve(failure(503, \"Unavailable\"))\n return chain @broker.read(@route.path, @input.key, @cursor)\n then(batch)\n if @cancelled\n return failure(503, \"Unavailable\")\n if batch.resyncRequired or batch.messages.length > 0 or Date/now() >= @deadline\n if batch.resyncRequired\n batch.fallback = @route.fallback\n auth = @authorize\n return auth(@token, batch)\n @cursor = batch.cursor\n @sleep = Promise/withResolvers()\n sleep = @sleep\n wake = ()\n capture sleep\n sleep.resolve(null)\n @timer = setTimeout(wake, Math/min(250, Math/max(1, @deadline - Date/now())))\n return chain sleep.promise\n then(ignored)\n return poll()\n\nfailure(status, code)\n return {\n status: status\n headers: {\n \"content-type\": \"application/json; charset=utf-8\"\n \"cache-control\": \"no-store\"\n }\n body: to_json({\n error: code\n })\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA;;;;;;;;"}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
requireBoolean as __requireBoolean,
|
|
9
|
+
normalize as __normalize,
|
|
10
|
+
boundary as __boundary,
|
|
11
|
+
requireArity as __requireArity,
|
|
12
|
+
} from "./.lango/runtime.mjs";
|
|
13
|
+
|
|
14
|
+
export function middlewareName(name) {
|
|
15
|
+
__requireArity(arguments.length, [0], "middlewareName");
|
|
16
|
+
name = __boundary(name);
|
|
17
|
+
let reserved = ["routes", "endpoint", "longpoll", "group", "include", "event", "middleware"];
|
|
18
|
+
let pattern = new (__hostGlobal(["RegExp"]))("^[A-Za-z_][A-Za-z0-9_-]*$");
|
|
19
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(name))))) || __requireBoolean(__normalize(reserved.includes(name))))) {
|
|
20
|
+
throw `Invalid or reserved middleware name: ${name}`;
|
|
21
|
+
}
|
|
22
|
+
return name;
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=middleware.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.mjs","sources":["middleware.lango"],"sourcesContent":["import js global RegExp\n\nexport middlewareName(name)\n\nmiddlewareName(name)\n reserved = [\"routes\", \"endpoint\", \"longpoll\", \"group\", \"include\", \"event\", \"middleware\"]\n pattern = new RegExp(\"^[A-Za-z_][A-Za-z0-9_-]*$\")\n if !is_string(name) or !pattern.test(name) or reserved.includes(name)\n throw \"Invalid or reserved middleware name: ${name}\"\n return name\n"],"names":[],"mappings":";;;;;;;;;;;;;AAIA;;;;;;;;;;;;"}
|
package/dist/cli/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/cli/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/cli/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/cli/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;;;;;;;;;;"}
|