@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
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import * as redis from "redis";
|
|
2
|
+
import * as crypto from "node:crypto";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as modules from "node:module";
|
|
6
|
+
import {
|
|
7
|
+
hostGlobal as __hostGlobal,
|
|
8
|
+
hostGlobalCall as __hostGlobalCall,
|
|
9
|
+
fromJson as __fromJson,
|
|
10
|
+
toJson as __toJson,
|
|
11
|
+
isNumber as __isNumber,
|
|
12
|
+
isString as __isString,
|
|
13
|
+
isBool as __isBool,
|
|
14
|
+
isObject as __isObject,
|
|
15
|
+
isArray as __isArray,
|
|
16
|
+
requireBoolean as __requireBoolean,
|
|
17
|
+
normalize as __normalize,
|
|
18
|
+
boundary as __boundary,
|
|
19
|
+
requireArity as __requireArity,
|
|
20
|
+
withArity as __withArity,
|
|
21
|
+
} from "./.lango/runtime.mjs";
|
|
22
|
+
|
|
23
|
+
export class LongpollBroker {
|
|
24
|
+
#routes;
|
|
25
|
+
#settings;
|
|
26
|
+
#client;
|
|
27
|
+
#script;
|
|
28
|
+
#root;
|
|
29
|
+
#lango;
|
|
30
|
+
constructor(settings, routes) {
|
|
31
|
+
__requireArity(arguments.length, [0, 1], "constructor");
|
|
32
|
+
settings = __boundary(settings);
|
|
33
|
+
routes = __boundary(routes);
|
|
34
|
+
this.#routes = __normalize(__hostGlobal(["Object"]).create(null));
|
|
35
|
+
for (const item of routes) {
|
|
36
|
+
if (__requireBoolean(__normalize(item.kind) === "longpoll")) {
|
|
37
|
+
this.#routes[__normalize(item.path)] = item;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
this.#settings = __normalize(settings.redis);
|
|
41
|
+
this.#client = null;
|
|
42
|
+
this.#script = null;
|
|
43
|
+
if (__requireBoolean(__requireBoolean(__normalize(__normalize(__hostGlobal(["Object"]).keys(__normalize(this.#routes))).length) > 0) && __requireBoolean(__normalize(this.#settings) === null))) {
|
|
44
|
+
throw "Longpoll routes require Redis configuration";
|
|
45
|
+
}
|
|
46
|
+
this.#root = __normalize(settings.root);
|
|
47
|
+
}
|
|
48
|
+
route(path) {
|
|
49
|
+
__requireArity(arguments.length, [0], "route");
|
|
50
|
+
path = __boundary(path);
|
|
51
|
+
if (__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#routes), path)))) {
|
|
52
|
+
return __normalize(this.#routes[path]);
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
open() {
|
|
58
|
+
if (__requireBoolean(__normalize(__normalize(__hostGlobal(["Object"]).keys(__normalize(this.#routes))).length) === 0)) {
|
|
59
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
60
|
+
}
|
|
61
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(__normalize(this.#root), "package.json"))));
|
|
62
|
+
let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
|
|
63
|
+
this.#script = __normalize(fs.readFileSync(__normalize(path.join(__normalize(path.dirname(entry)), "../src/longpoll.lua")), "utf8"));
|
|
64
|
+
let config = __normalize(this.#settings);
|
|
65
|
+
let name = __normalize(config.connectionEnv);
|
|
66
|
+
this.#client = __normalize(redis.createClient({ url: __normalize(__hostGlobal(["process"]).env[name]), disableOfflineQueue: true, commandsQueueMaxLength: 2048, socket: { connectTimeout: 2000, reconnectStrategy: 500 } }));
|
|
67
|
+
let client = __normalize(this.#client);
|
|
68
|
+
__normalize(client.on("error", __withArity((error) => {
|
|
69
|
+
error = __boundary(error);
|
|
70
|
+
return null;
|
|
71
|
+
return null;
|
|
72
|
+
}, [0], "closure")));
|
|
73
|
+
return bounded(__normalize(client.connect()), 3000);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
close() {
|
|
77
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#client) !== null) && __requireBoolean(__normalize(this.#client.isOpen)))) {
|
|
78
|
+
__normalize(this.#client.destroy());
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
publish(path, key, data) {
|
|
84
|
+
__requireArity(arguments.length, [0, 1, 2], "publish");
|
|
85
|
+
path = __boundary(path);
|
|
86
|
+
key = __boundary(key);
|
|
87
|
+
data = __boundary(data);
|
|
88
|
+
let body = __normalize(__hostGlobal(["JSON"]).stringify(data));
|
|
89
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(body))) || __requireBoolean(__normalize(__hostGlobal(["Buffer"]).byteLength(body)) > 262144))) {
|
|
90
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Longpoll message must be JSON within 262144 bytes"));
|
|
91
|
+
}
|
|
92
|
+
return this.#execute("publish", path, key, null, body);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
cursor(path, key) {
|
|
96
|
+
__requireArity(arguments.length, [0, 1], "cursor");
|
|
97
|
+
path = __boundary(path);
|
|
98
|
+
key = __boundary(key);
|
|
99
|
+
return this.#execute("cursor", path, key, null, "");
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
read(path, key, cursor) {
|
|
103
|
+
__requireArity(arguments.length, [0, 1, 2], "read");
|
|
104
|
+
path = __boundary(path);
|
|
105
|
+
key = __boundary(key);
|
|
106
|
+
cursor = __boundary(cursor);
|
|
107
|
+
return this.#execute("read", path, key, cursor, "");
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
#execute(operation, path, key, cursor, body) {
|
|
111
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4], "execute");
|
|
112
|
+
operation = __boundary(operation);
|
|
113
|
+
path = __boundary(path);
|
|
114
|
+
key = __boundary(key);
|
|
115
|
+
cursor = __boundary(cursor);
|
|
116
|
+
body = __boundary(body);
|
|
117
|
+
let item = this.route(path);
|
|
118
|
+
if (__requireBoolean(item === null)) {
|
|
119
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Unknown longpoll path"));
|
|
120
|
+
}
|
|
121
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(key))) || __requireBoolean(key === "")) || __requireBoolean(__normalize(__hostGlobal(["Buffer"]).byteLength(key)) > 1024))) {
|
|
122
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Longpoll key must contain 1 to 1024 bytes"));
|
|
123
|
+
}
|
|
124
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#client) === null) || __requireBoolean(!__requireBoolean(__normalize(this.#client.isReady))))) {
|
|
125
|
+
return __normalize(__hostGlobal(["Promise"]).reject("RedisUnavailable"));
|
|
126
|
+
}
|
|
127
|
+
let hash = __normalize(crypto.createHash("sha256"));
|
|
128
|
+
__normalize(hash.update(__toJson([__normalize(this.#settings.namespace), path, key])));
|
|
129
|
+
let channel = __normalize(hash.digest("hex"));
|
|
130
|
+
let prefix = `lango:longpoll:{${channel}}`;
|
|
131
|
+
let position = { generation: "", id: "0-0" };
|
|
132
|
+
if (__requireBoolean(cursor !== null)) {
|
|
133
|
+
position = decode(cursor, channel);
|
|
134
|
+
if (__requireBoolean(position === null)) {
|
|
135
|
+
return __normalize(__hostGlobal(["Promise"]).resolve({ resyncRequired: true }));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
let pending = __normalize(this.#client.eval(__normalize(this.#script), { keys: [`${prefix}:stream`, `${prefix}:meta`], arguments: [operation, __normalize(crypto.randomUUID()), `${__normalize(item.retentionMs)}`, `${__normalize(item.maxMessages)}`, __normalize(position.generation), __normalize(position.id), body] }));
|
|
139
|
+
return bounded(pending, 3000).then(__withArity((raw) => {
|
|
140
|
+
raw = __boundary(raw);
|
|
141
|
+
let result = __normalize(__hostGlobal(["JSON"]).parse(raw));
|
|
142
|
+
if (__requireBoolean(__normalize(result.resyncRequired) === true)) {
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
result.cursor = encode(channel, __normalize(result.generation), __normalize(result.id));
|
|
146
|
+
if (__requireBoolean(operation !== "read")) {
|
|
147
|
+
return __normalize(result.cursor);
|
|
148
|
+
}
|
|
149
|
+
let messages = [];
|
|
150
|
+
if (__requireBoolean(__isArray(__normalize(result.messages)))) {
|
|
151
|
+
for (const message of __normalize(result.messages)) {
|
|
152
|
+
messages.push({ id: encode(channel, __normalize(result.generation), __normalize(message.id)), data: __normalize(__hostGlobal(["JSON"]).parse(__normalize(message.data))) });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { messages: messages, cursor: __normalize(result.cursor), resyncRequired: false };
|
|
156
|
+
return null;
|
|
157
|
+
}, [0], "closure"));
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function encode(channel, generation, id) {
|
|
162
|
+
__requireArity(arguments.length, [0, 1, 2], "encode");
|
|
163
|
+
channel = __boundary(channel);
|
|
164
|
+
generation = __boundary(generation);
|
|
165
|
+
id = __boundary(id);
|
|
166
|
+
let buffer = __normalize(__hostGlobal(["Buffer"]).from(__toJson([channel, generation, id])));
|
|
167
|
+
return __normalize(buffer.toString("base64url"));
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function decode(cursor, channel) {
|
|
172
|
+
__requireArity(arguments.length, [0, 1], "decode");
|
|
173
|
+
cursor = __boundary(cursor);
|
|
174
|
+
channel = __boundary(channel);
|
|
175
|
+
let buffer;
|
|
176
|
+
let parts;
|
|
177
|
+
let pattern;
|
|
178
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(cursor))) || __requireBoolean(__normalize(cursor.length) > 512))) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
buffer = __normalize(__hostGlobal(["Buffer"]).from(cursor, "base64url"));
|
|
183
|
+
parts = __fromJson(__normalize(buffer.toString("utf8")));
|
|
184
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isArray(parts))) || __requireBoolean(__normalize(parts.length) !== 3)) || __requireBoolean(__normalize(parts[0]) !== channel)) || __requireBoolean(!__requireBoolean(__isString(__normalize(parts[1]))))) || __requireBoolean(!__requireBoolean(__isString(__normalize(parts[2])))))) {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
pattern = new (__hostGlobal(["RegExp"]))("^(0|[1-9][0-9]{0,18})-(0|[1-9][0-9]{0,18})$");
|
|
188
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(pattern.test(__normalize(parts[2]))))) || __requireBoolean(__normalize(__normalize(parts[2]).length) > 40))) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
return { generation: __normalize(parts[1]), id: __normalize(parts[2]) };
|
|
192
|
+
} catch (error) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function bounded(pending, ms) {
|
|
199
|
+
__requireArity(arguments.length, [0, 1], "bounded");
|
|
200
|
+
pending = __boundary(pending);
|
|
201
|
+
ms = __boundary(ms);
|
|
202
|
+
let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
203
|
+
let fail = () => {
|
|
204
|
+
__normalize(expired.reject("RedisUnavailable"));
|
|
205
|
+
return null;
|
|
206
|
+
};
|
|
207
|
+
let timer = __hostGlobalCall(["setTimeout"], fail, ms);
|
|
208
|
+
return __normalize(__hostGlobal(["Promise"]).race([pending, __normalize(expired.promise)])).finally(() => {
|
|
209
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
210
|
+
return null;
|
|
211
|
+
});
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=longpoll_broker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longpoll_broker.mjs","sources":["longpoll_broker.lango"],"sourcesContent":["import js \"redis\" as redis\nimport js \"node:crypto\" as crypto\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"node:module\" as modules\nimport js global Buffer\nimport js global Promise\nimport js global process\nimport js global JSON\nimport js global RegExp\nimport js global Object\nimport js global setTimeout\nimport js global clearTimeout\n\nexport class LongpollBroker(settings, routes)\n methods\n open()\n close()\n route(path)\n publish(path, key, data)\n cursor(path, key)\n read(path, key, cursor)\n\nLongpollBroker(settings, routes)\n @routes = Object/create(null)\n for routes as item\n if item.kind == \"longpoll\"\n @routes[item.path] = item\n @settings = settings.redis\n @client = null\n @script = null\n if Object/keys(@routes).length > 0 and @settings == null\n throw \"Longpoll routes require Redis configuration\"\n @root = settings.root\n\nLongpollBroker:route(path)\n if Object/hasOwn(@routes, path)\n return @routes[path]\n return null\n\nLongpollBroker:open()\n if Object/keys(@routes).length == 0\n return Promise/resolve(null)\n resolver = modules/createRequire(path/join(@root, \"package.json\"))\n entry = resolver.resolve(\"@lango-project/lango-server\")\n @script = fs/readFileSync(path/join(path/dirname(entry), \"../src/longpoll.lua\"), \"utf8\")\n config = @settings\n name = config.connectionEnv\n @client = redis/createClient({\n url: process.env[name]\n disableOfflineQueue: true\n commandsQueueMaxLength: 2048\n socket: {\n connectTimeout: 2000\n reconnectStrategy: 500\n }\n })\n client = @client\n client.on(\"error\", (error)\n return null\n )\n return bounded(client.connect(), 3000)\n\nLongpollBroker:close()\n if @client != null and @client.isOpen\n @client.destroy()\n return null\n\nLongpollBroker:publish(path, key, data)\n body = JSON/stringify(data)\n if !is_string(body) or Buffer/byteLength(body) > 262144\n return Promise/reject(\"Longpoll message must be JSON within 262144 bytes\")\n return execute(\"publish\", path, key, null, body)\n\nLongpollBroker:cursor(path, key)\n return execute(\"cursor\", path, key, null, \"\")\n\nLongpollBroker:read(path, key, cursor)\n return execute(\"read\", path, key, cursor, \"\")\n\nLongpollBroker:execute(operation, path, key, cursor, body)\n item = route(path)\n if item == null\n return Promise/reject(\"Unknown longpoll path\")\n if !is_string(key) or key == \"\" or Buffer/byteLength(key) > 1024\n return Promise/reject(\"Longpoll key must contain 1 to 1024 bytes\")\n if @client == null or !@client.isReady\n return Promise/reject(\"RedisUnavailable\")\n hash = crypto/createHash(\"sha256\")\n hash.update(to_json([@settings.namespace, path, key]))\n channel = hash.digest(\"hex\")\n prefix = \"lango:longpoll:{${channel}}\"\n position = {\n generation: \"\"\n id: \"0-0\"\n }\n if cursor != null\n position = decode(cursor, channel)\n if position == null\n return Promise/resolve({\n resyncRequired: true\n })\n pending = @client.eval(@script, {\n keys: [\"${prefix}:stream\", \"${prefix}:meta\"]\n arguments: [operation, crypto/randomUUID(), \"${item.retentionMs}\", \"${item.maxMessages}\", position.generation, position.id, body]\n })\n return chain bounded(pending, 3000)\n then(raw)\n capture channel\n capture operation\n result = JSON/parse(raw)\n if result.resyncRequired == true\n return result\n result.cursor = encode(channel, result.generation, result.id)\n if operation != \"read\"\n return result.cursor\n messages = []\n if is_array(result.messages)\n for result.messages as message\n messages.add({\n id: encode(channel, result.generation, message.id)\n data: JSON/parse(message.data)\n })\n return {\n messages: messages\n cursor: result.cursor\n resyncRequired: false\n }\n\nencode(channel, generation, id)\n buffer = Buffer/from(to_json([channel, generation, id]))\n return buffer.toString(\"base64url\")\n\ndecode(cursor, channel)\n if !is_string(cursor) or cursor.length > 512\n return null\n try\n buffer = Buffer/from(cursor, \"base64url\")\n parts = from_json(buffer.toString(\"utf8\"))\n if !is_array(parts) or parts.length != 3 or parts[0] != channel or !is_string(parts[1]) or !is_string(parts[2])\n return null\n pattern = new RegExp(\"^(0|[1-9][0-9]{0,18})-(0|[1-9][0-9]{0,18})$\")\n if !pattern.test(parts[2]) or parts[2].length > 40\n return null\n return {\n generation: parts[1]\n id: parts[2]\n }\n catch error\n return null\n\nbounded(pending, ms)\n expired = Promise/withResolvers()\n fail = ()\n capture expired\n expired.reject(\"RedisUnavailable\")\n timer = setTimeout(fail, ms)\n return chain Promise/race([pending, expired.promise])\n finally\n capture timer\n clearTimeout(timer)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmHA;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;;;;;;;"}
|
|
@@ -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(__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("#")))) || __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(\"#\") 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;;;;;;;;"}
|