@lango-project/lango-server 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +710 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/runtime.mjs +683 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/body_reader.mjs +179 -0
- package/dist/body_reader.mjs.map +1 -0
- package/dist/builder.mjs +84 -0
- package/dist/builder.mjs.map +1 -0
- package/dist/cli/.lango/bignumber.mjs +3119 -0
- package/dist/cli/.lango/runtime.mjs +683 -0
- package/dist/cli/.lango/xmldom/conventions.js +429 -0
- package/dist/cli/.lango/xmldom/dom-parser.js +591 -0
- package/dist/cli/.lango/xmldom/dom.js +3759 -0
- package/dist/cli/.lango/xmldom/entities.js +2171 -0
- package/dist/cli/.lango/xmldom/errors.js +205 -0
- package/dist/cli/.lango/xmldom/grammar.js +559 -0
- package/dist/cli/.lango/xmldom/index.js +41 -0
- package/dist/cli/.lango/xmldom/package.json +1 -0
- package/dist/cli/.lango/xmldom/sax.js +983 -0
- package/dist/cli/.lango/xmldom.LICENSE.md +8 -0
- package/dist/cli/body_reader.mjs +179 -0
- package/dist/cli/body_reader.mjs.map +1 -0
- package/dist/cli/database.mjs +101 -0
- package/dist/cli/database.mjs.map +1 -0
- package/dist/cli/database_options.mjs +83 -0
- package/dist/cli/database_options.mjs.map +1 -0
- package/dist/cli/database_scope.mjs +82 -0
- package/dist/cli/database_scope.mjs.map +1 -0
- package/dist/cli/endpoint.mjs +50 -0
- package/dist/cli/endpoint.mjs.map +1 -0
- package/dist/cli/longpoll_broker.mjs +214 -0
- package/dist/cli/longpoll_broker.mjs.map +1 -0
- package/dist/cli/longpoll_options.mjs +102 -0
- package/dist/cli/longpoll_options.mjs.map +1 -0
- package/dist/cli/longpoll_session.mjs +135 -0
- package/dist/cli/longpoll_session.mjs.map +1 -0
- package/dist/cli/middleware.mjs +25 -0
- package/dist/cli/middleware.mjs.map +1 -0
- package/dist/cli/options.mjs +177 -0
- package/dist/cli/options.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +166 -0
- package/dist/cli/postgres_adapter.mjs.map +1 -0
- package/dist/cli/routes.mjs +297 -0
- package/dist/cli/routes.mjs.map +1 -0
- package/dist/cli/server.mjs +792 -0
- package/dist/cli/server.mjs.map +1 -0
- package/dist/cli/sqlite_adapter.mjs +163 -0
- package/dist/cli/sqlite_adapter.mjs.map +1 -0
- package/dist/cli/static_files.mjs +125 -0
- package/dist/cli/static_files.mjs.map +1 -0
- package/dist/database.mjs +101 -0
- package/dist/database.mjs.map +1 -0
- package/dist/database_options.mjs +83 -0
- package/dist/database_options.mjs.map +1 -0
- package/dist/database_scope.mjs +82 -0
- package/dist/database_scope.mjs.map +1 -0
- package/dist/endpoint.mjs +50 -0
- package/dist/endpoint.mjs.map +1 -0
- package/dist/group.mjs +91 -0
- package/dist/group.mjs.map +1 -0
- package/dist/library.mjs +6 -0
- package/dist/library.mjs.map +1 -0
- package/dist/longpoll_broker.mjs +214 -0
- package/dist/longpoll_broker.mjs.map +1 -0
- package/dist/longpoll_options.mjs +102 -0
- package/dist/longpoll_options.mjs.map +1 -0
- package/dist/longpoll_session.mjs +135 -0
- package/dist/longpoll_session.mjs.map +1 -0
- package/dist/middleware.mjs +25 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/options.mjs +177 -0
- package/dist/options.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +166 -0
- package/dist/postgres_adapter.mjs.map +1 -0
- package/dist/postgres_worker.mjs +119 -0
- package/dist/postgres_worker.mjs.map +1 -0
- package/dist/response.mjs +62 -0
- package/dist/response.mjs.map +1 -0
- package/dist/routes.mjs +297 -0
- package/dist/routes.mjs.map +1 -0
- package/dist/runtime.mjs +549 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/server.mjs +788 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server_context.mjs +78 -0
- package/dist/server_context.mjs.map +1 -0
- package/dist/sqlite_adapter.mjs +163 -0
- package/dist/sqlite_adapter.mjs.map +1 -0
- package/dist/static_files.mjs +125 -0
- package/dist/static_files.mjs.map +1 -0
- package/dist/token.mjs +106 -0
- package/dist/token.mjs.map +1 -0
- package/dist/tokens.mjs +76 -0
- package/dist/tokens.mjs.map +1 -0
- package/dist/worker.mjs +57 -0
- package/dist/worker.mjs.map +1 -0
- package/package.json +44 -4
- package/src/body_reader.lango +113 -0
- package/src/builder.lango +64 -0
- package/src/database.lango +66 -0
- package/src/database_options.lango +57 -0
- package/src/database_scope.lango +59 -0
- package/src/endpoint.lango +30 -0
- package/src/group.lango +61 -0
- package/src/library.lango +10 -0
- package/src/longpoll.lua +61 -0
- package/src/longpoll_broker.lango +161 -0
- package/src/longpoll_options.lango +79 -0
- package/src/longpoll_session.lango +90 -0
- package/src/middleware.lango +10 -0
- package/src/options.lango +144 -0
- package/src/postgres_adapter.lango +138 -0
- package/src/postgres_worker.lango +114 -0
- package/src/response.lango +42 -0
- package/src/routes.lango +224 -0
- package/src/runtime.lango +382 -0
- package/src/server.lango +597 -0
- package/src/server_context.lango +58 -0
- package/src/sqlite_adapter.lango +127 -0
- package/src/static_files.lango +90 -0
- package/src/token.lango +70 -0
- package/src/tokens.lango +70 -0
- package/src/worker.lango +43 -0
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { LongpollBroker } from "./longpoll_broker.mjs";
|
|
3
|
+
import { LongpollSession } from "./longpoll_session.mjs";
|
|
4
|
+
import { prepareDatabase } from "./database.mjs";
|
|
5
|
+
import { deployment } from "./options.mjs";
|
|
6
|
+
import { Routes } from "./routes.mjs";
|
|
7
|
+
import { BodyReader } from "./body_reader.mjs";
|
|
8
|
+
import { StaticFiles } from "./static_files.mjs";
|
|
9
|
+
import * as http from "node:http";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import * as modules from "node:module";
|
|
12
|
+
import * as crypto from "node:crypto";
|
|
13
|
+
import * as piscina from "piscina";
|
|
14
|
+
import {
|
|
15
|
+
hostGlobal as __hostGlobal,
|
|
16
|
+
hostGlobalCall as __hostGlobalCall,
|
|
17
|
+
contextGet as __contextGet,
|
|
18
|
+
contextSet as __contextSet,
|
|
19
|
+
contextRestore as __contextRestore,
|
|
20
|
+
fromJson as __fromJson,
|
|
21
|
+
toJson as __toJson,
|
|
22
|
+
isNumber as __isNumber,
|
|
23
|
+
isString as __isString,
|
|
24
|
+
isBool as __isBool,
|
|
25
|
+
isObject as __isObject,
|
|
26
|
+
isArray as __isArray,
|
|
27
|
+
includes as __includes,
|
|
28
|
+
entries as __entries,
|
|
29
|
+
range as __range,
|
|
30
|
+
add as __add,
|
|
31
|
+
requireBoolean as __requireBoolean,
|
|
32
|
+
normalize as __normalize,
|
|
33
|
+
finite as __finite,
|
|
34
|
+
numericAdd as __numericAdd,
|
|
35
|
+
call as __call,
|
|
36
|
+
boundary as __boundary,
|
|
37
|
+
requireArity as __requireArity,
|
|
38
|
+
withArity as __withArity,
|
|
39
|
+
} from "./.lango/runtime.mjs";
|
|
40
|
+
|
|
41
|
+
export class Server {
|
|
42
|
+
#definition;
|
|
43
|
+
#broker;
|
|
44
|
+
#polls;
|
|
45
|
+
#state;
|
|
46
|
+
#connections;
|
|
47
|
+
#server;
|
|
48
|
+
#pool;
|
|
49
|
+
#starting;
|
|
50
|
+
#closing;
|
|
51
|
+
#waiting;
|
|
52
|
+
#active;
|
|
53
|
+
#tasks;
|
|
54
|
+
#ready;
|
|
55
|
+
#readyIds;
|
|
56
|
+
#destroying;
|
|
57
|
+
#static;
|
|
58
|
+
#lango;
|
|
59
|
+
#shutdownDeadline;
|
|
60
|
+
constructor(path, definition = null) {
|
|
61
|
+
__requireArity(arguments.length, [0], "constructor");
|
|
62
|
+
path = __boundary(path);
|
|
63
|
+
definition = __boundary(definition);
|
|
64
|
+
if (__requireBoolean(path !== null)) {
|
|
65
|
+
let directory = path;
|
|
66
|
+
let routes = new Routes(directory);
|
|
67
|
+
__normalize(routes.xml(pathJoin(directory, "routes.xml")));
|
|
68
|
+
definition = { settings: deployment(directory), routes: __normalize(routes.definitions()), events: __normalize(routes.events()), middlewares: __normalize(routes.middlewares()) };
|
|
69
|
+
}
|
|
70
|
+
if (__requireBoolean(definition === null)) {
|
|
71
|
+
throw "Use Server(directory) or Builder.build()";
|
|
72
|
+
}
|
|
73
|
+
this.#definition = __fromJson(__toJson(definition));
|
|
74
|
+
this.#broker = new LongpollBroker(__normalize(this.#definition.settings), __normalize(this.#definition.routes));
|
|
75
|
+
this.#polls = __normalize(__hostGlobal(["Object"]).create(null));
|
|
76
|
+
this.#state = "new";
|
|
77
|
+
this.#connections = [];
|
|
78
|
+
this.#server = null;
|
|
79
|
+
this.#pool = null;
|
|
80
|
+
this.#starting = null;
|
|
81
|
+
this.#closing = null;
|
|
82
|
+
this.#waiting = [];
|
|
83
|
+
this.#active = 0;
|
|
84
|
+
this.#tasks = __normalize(__hostGlobal(["Object"]).create(null));
|
|
85
|
+
this.#ready = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
86
|
+
this.#readyIds = [];
|
|
87
|
+
this.#destroying = false;
|
|
88
|
+
this.#static = null;
|
|
89
|
+
}
|
|
90
|
+
publish(path, key, data) {
|
|
91
|
+
__requireArity(arguments.length, [0, 1, 2], "publish");
|
|
92
|
+
path = __boundary(path);
|
|
93
|
+
key = __boundary(key);
|
|
94
|
+
data = __boundary(data);
|
|
95
|
+
if (__requireBoolean(__normalize(this.#state) !== "running")) {
|
|
96
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Server is not running"));
|
|
97
|
+
}
|
|
98
|
+
return __normalize(this.#broker.publish(path, key, data));
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
cursor(path, key) {
|
|
102
|
+
__requireArity(arguments.length, [0, 1], "cursor");
|
|
103
|
+
path = __boundary(path);
|
|
104
|
+
key = __boundary(key);
|
|
105
|
+
if (__requireBoolean(__normalize(this.#state) !== "running")) {
|
|
106
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Server is not running"));
|
|
107
|
+
}
|
|
108
|
+
return __normalize(this.#broker.cursor(path, key));
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
start() {
|
|
112
|
+
if (__requireBoolean(__normalize(this.#state) !== "new")) {
|
|
113
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Server can only be started once"));
|
|
114
|
+
}
|
|
115
|
+
this.#state = "starting";
|
|
116
|
+
this.#starting = this.#launch();
|
|
117
|
+
return __normalize(this.#starting);
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
#launch() {
|
|
121
|
+
let settings = __normalize(this.#definition.settings);
|
|
122
|
+
if (__requireBoolean(__normalize(settings.static) !== null)) {
|
|
123
|
+
try {
|
|
124
|
+
this.#static = new StaticFiles(__normalize(settings.static));
|
|
125
|
+
} catch (error) {
|
|
126
|
+
this.#state = "closed";
|
|
127
|
+
return __normalize(__hostGlobal(["Promise"]).reject(error));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return __normalize(this.#broker.open()).then(__withArity((ignored) => {
|
|
131
|
+
ignored = __boundary(ignored);
|
|
132
|
+
return prepareDatabase(__normalize(settings.database));
|
|
133
|
+
return null;
|
|
134
|
+
}, [0], "closure")).then(__withArity((ignored) => {
|
|
135
|
+
ignored = __boundary(ignored);
|
|
136
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(__normalize(settings.root), "package.json"))));
|
|
137
|
+
let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
|
|
138
|
+
this.#pool = new piscina.Piscina({ filename: __normalize(path.join(__normalize(path.dirname(entry)), "worker.mjs")), name: "execute", minThreads: __normalize(settings.workers), maxThreads: __normalize(settings.workers), maxQueue: __normalize(settings.maxQueue), concurrentTasksPerWorker: 1, closeTimeout: __normalize(settings.shutdownTimeoutMs), workerData: __normalize(this.#definition) });
|
|
139
|
+
let pool = __normalize(this.#pool);
|
|
140
|
+
__normalize(pool.on("message", __withArity((message) => {
|
|
141
|
+
message = __boundary(message);
|
|
142
|
+
this.#messageReceived(message);
|
|
143
|
+
return null;
|
|
144
|
+
}, [0], "closure")));
|
|
145
|
+
__normalize(pool.on("error", __withArity((error) => {
|
|
146
|
+
const __contextScope = [];
|
|
147
|
+
try {
|
|
148
|
+
error = __boundary(error);
|
|
149
|
+
__normalize(this.#ready.reject(error));
|
|
150
|
+
__normalize(__contextGet("log").err("Worker pool error"));
|
|
151
|
+
return null;
|
|
152
|
+
} finally {
|
|
153
|
+
__contextRestore(__contextScope);
|
|
154
|
+
}
|
|
155
|
+
}, [0], "closure")));
|
|
156
|
+
return startupDeadline(__normalize(this.#ready.promise), __normalize(settings.startupTimeoutMs));
|
|
157
|
+
return null;
|
|
158
|
+
}, [0], "closure")).then(__withArity((ignored) => {
|
|
159
|
+
ignored = __boundary(ignored);
|
|
160
|
+
if (__requireBoolean(__normalize(this.#state) !== "starting")) {
|
|
161
|
+
throw "Server closed during startup";
|
|
162
|
+
}
|
|
163
|
+
let server = new http.Server(__withArity((req, res) => {
|
|
164
|
+
req = __boundary(req);
|
|
165
|
+
res = __boundary(res);
|
|
166
|
+
this.#receive(req, res);
|
|
167
|
+
return null;
|
|
168
|
+
}, [0, 1], "closure"));
|
|
169
|
+
this.#server = server;
|
|
170
|
+
__normalize(server.on("connection", __withArity((socket) => {
|
|
171
|
+
socket = __boundary(socket);
|
|
172
|
+
this.#connected(socket);
|
|
173
|
+
return null;
|
|
174
|
+
}, [0], "closure")));
|
|
175
|
+
server.headersTimeout = __normalize(settings.bodyTimeoutMs);
|
|
176
|
+
server.requestTimeout = __normalize(settings.bodyTimeoutMs);
|
|
177
|
+
let listening = new (__hostGlobal(["Promise"]))(__withArity((resolve, reject) => {
|
|
178
|
+
resolve = __boundary(resolve);
|
|
179
|
+
reject = __boundary(reject);
|
|
180
|
+
__normalize(server.once("error", reject));
|
|
181
|
+
__normalize(server.listen(__normalize(settings.port), __normalize(settings.host), () => {
|
|
182
|
+
__normalize(server.removeListener("error", reject));
|
|
183
|
+
if (__requireBoolean(__normalize(this.#state) !== "starting")) {
|
|
184
|
+
__call(reject, "Server closed during startup");
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
this.#state = "running";
|
|
188
|
+
let address = __normalize(server.address());
|
|
189
|
+
__call(resolve, { host: __normalize(address.address), port: __normalize(address.port) });
|
|
190
|
+
return null;
|
|
191
|
+
}));
|
|
192
|
+
return null;
|
|
193
|
+
}, [0, 1], "closure"));
|
|
194
|
+
return listening;
|
|
195
|
+
return null;
|
|
196
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
197
|
+
error = __boundary(error);
|
|
198
|
+
__normalize(this.#broker.close());
|
|
199
|
+
this.#state = "closed";
|
|
200
|
+
return this.#destroyPool().then(__withArity((ignored) => {
|
|
201
|
+
ignored = __boundary(ignored);
|
|
202
|
+
throw error;
|
|
203
|
+
return null;
|
|
204
|
+
}, [0], "closure"));
|
|
205
|
+
return null;
|
|
206
|
+
}, [0], "closure"));
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
#messageReceived(message) {
|
|
210
|
+
__requireArity(arguments.length, [0], "messageReceived");
|
|
211
|
+
message = __boundary(message);
|
|
212
|
+
if (__requireBoolean(__normalize(message.kind) === "longpoll")) {
|
|
213
|
+
this.#brokerMessage(message);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
if (__requireBoolean(__normalize(message.kind) === "ready")) {
|
|
217
|
+
if (__requireBoolean(!__requireBoolean(__normalize(this.#readyIds.includes(__normalize(message.threadId)))))) {
|
|
218
|
+
__add(this.#readyIds, __normalize(message.threadId));
|
|
219
|
+
}
|
|
220
|
+
if (__requireBoolean(__normalize(this.#readyIds.length) >= __normalize(this.#definition.settings.workers))) {
|
|
221
|
+
__normalize(this.#ready.resolve(null));
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
if (__requireBoolean(!__requireBoolean(__includes(__normalize(message.id), __normalize(this.#tasks))))) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
let record = __normalize(this.#tasks[__normalize(message.id)]);
|
|
229
|
+
if (__requireBoolean(__normalize(message.kind) === "prepared")) {
|
|
230
|
+
record.result = __normalize(message.result);
|
|
231
|
+
this.#resetDeadline(record, __normalize(this.#definition.settings.eventTimeoutMs));
|
|
232
|
+
}
|
|
233
|
+
if (__requireBoolean(__normalize(message.kind) === "event")) {
|
|
234
|
+
this.#resetDeadline(record, __normalize(this.#definition.settings.eventTimeoutMs));
|
|
235
|
+
}
|
|
236
|
+
if (__requireBoolean(__requireBoolean(__normalize(message.kind) === "handler") && __requireBoolean(__normalize(record.result) === null))) {
|
|
237
|
+
this.#resetDeadline(record, __finite(__normalize(record.deadline) - __normalize(__hostGlobal(["Date"]).now())));
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
#resetDeadline(record, duration) {
|
|
243
|
+
__requireArity(arguments.length, [0, 1], "resetDeadline");
|
|
244
|
+
record = __boundary(record);
|
|
245
|
+
duration = __boundary(duration);
|
|
246
|
+
__hostGlobalCall(["clearTimeout"], __normalize(record.timer));
|
|
247
|
+
if (__requireBoolean(__normalize(record.result) === null)) {
|
|
248
|
+
duration = __normalize(__hostGlobal(["Math"]).min(duration, __finite(__normalize(record.deadline) - __normalize(__hostGlobal(["Date"]).now()))));
|
|
249
|
+
}
|
|
250
|
+
let expired = () => {
|
|
251
|
+
this.#expire(record);
|
|
252
|
+
return null;
|
|
253
|
+
};
|
|
254
|
+
record.timer = __hostGlobalCall(["setTimeout"], expired, __normalize(__hostGlobal(["Math"]).max(1, duration)));
|
|
255
|
+
return null;
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
#connected(socket) {
|
|
259
|
+
__requireArity(arguments.length, [0], "connected");
|
|
260
|
+
socket = __boundary(socket);
|
|
261
|
+
let record = { socket: socket, active: 0 };
|
|
262
|
+
__add(this.#connections, record);
|
|
263
|
+
__normalize(socket.once("close", () => {
|
|
264
|
+
let index = __normalize(this.#connections.indexOf(record));
|
|
265
|
+
if (__requireBoolean(index >= 0)) {
|
|
266
|
+
__normalize(this.#connections.splice(index, 1));
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}));
|
|
270
|
+
return null;
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
#trackResponse(req, res) {
|
|
274
|
+
__requireArity(arguments.length, [0, 1], "trackResponse");
|
|
275
|
+
req = __boundary(req);
|
|
276
|
+
res = __boundary(res);
|
|
277
|
+
for (const record of __normalize(this.#connections)) {
|
|
278
|
+
if (__requireBoolean(__normalize(record.socket) === __normalize(req.socket))) {
|
|
279
|
+
record.active = __numericAdd(__normalize(record.active), 1);
|
|
280
|
+
let state = { done: false };
|
|
281
|
+
let complete = () => {
|
|
282
|
+
if (__requireBoolean(__normalize(state.done))) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
state.done = true;
|
|
286
|
+
record.active = __finite(__normalize(record.active) - 1);
|
|
287
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#state) === "closing") && __requireBoolean(__normalize(record.active) === 0))) {
|
|
288
|
+
__normalize(record.socket.end());
|
|
289
|
+
}
|
|
290
|
+
return null;
|
|
291
|
+
return null;
|
|
292
|
+
};
|
|
293
|
+
__normalize(res.once("finish", complete));
|
|
294
|
+
__normalize(res.once("close", complete));
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
#receive(req, res) {
|
|
302
|
+
__requireArity(arguments.length, [0, 1], "receive");
|
|
303
|
+
req = __boundary(req);
|
|
304
|
+
res = __boundary(res);
|
|
305
|
+
this.#trackResponse(req, res);
|
|
306
|
+
__normalize(req.on("error", __withArity((error) => {
|
|
307
|
+
error = __boundary(error);
|
|
308
|
+
return null;
|
|
309
|
+
return null;
|
|
310
|
+
}, [0], "closure")));
|
|
311
|
+
if (__requireBoolean(__normalize(this.#state) !== "running")) {
|
|
312
|
+
return rejectHttp(req, res, 503, "Unavailable");
|
|
313
|
+
}
|
|
314
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#static) !== null) && __requireBoolean(__requireBoolean(__normalize(req.method) === "GET") || __requireBoolean(__normalize(req.method) === "HEAD")))) {
|
|
315
|
+
let failed = __withArity((status, code) => {
|
|
316
|
+
status = __boundary(status);
|
|
317
|
+
code = __boundary(code);
|
|
318
|
+
this.#send(res, failure(status, code));
|
|
319
|
+
return null;
|
|
320
|
+
}, [0, 1], "closure");
|
|
321
|
+
return __normalize(this.#static.receive(req, res, failed));
|
|
322
|
+
}
|
|
323
|
+
if (__requireBoolean(__normalize(req.method) !== "POST")) {
|
|
324
|
+
let allowed = "POST";
|
|
325
|
+
if (__requireBoolean(__normalize(this.#static) !== null)) {
|
|
326
|
+
allowed = "GET, HEAD, POST";
|
|
327
|
+
}
|
|
328
|
+
__normalize(res.setHeader("Allow", allowed));
|
|
329
|
+
return rejectHttp(req, res, 405, "MethodNotAllowed");
|
|
330
|
+
}
|
|
331
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(req.url.includes("?"))) || __requireBoolean(__normalize(req.url.includes("#")))) || __requireBoolean(__normalize(req.url.includes("\\"))))) {
|
|
332
|
+
return rejectHttp(req, res, 400, "InvalidPath");
|
|
333
|
+
}
|
|
334
|
+
let type = req.headers["content-type"];
|
|
335
|
+
let media = "";
|
|
336
|
+
if (__requireBoolean(__isString(type))) {
|
|
337
|
+
let parts = __normalize(type.split(";"));
|
|
338
|
+
media = __normalize(__normalize(parts[0]).trim());
|
|
339
|
+
}
|
|
340
|
+
if (__requireBoolean(__normalize(media.toLowerCase()) !== "application/json")) {
|
|
341
|
+
return rejectHttp(req, res, 415, "JsonRequired");
|
|
342
|
+
}
|
|
343
|
+
let settings = __normalize(this.#definition.settings);
|
|
344
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#active) >= __normalize(settings.workers)) && __requireBoolean(__normalize(this.#waiting.length) >= __normalize(settings.maxQueue)))) {
|
|
345
|
+
return rejectHttp(req, res, 503, "QueueFull");
|
|
346
|
+
}
|
|
347
|
+
let complete = __withArity((body) => {
|
|
348
|
+
body = __boundary(body);
|
|
349
|
+
this.#submit(req, res, body);
|
|
350
|
+
return null;
|
|
351
|
+
}, [0], "closure");
|
|
352
|
+
let failed = __withArity((status, code) => {
|
|
353
|
+
status = __boundary(status);
|
|
354
|
+
code = __boundary(code);
|
|
355
|
+
this.#send(res, failure(status, code));
|
|
356
|
+
return null;
|
|
357
|
+
}, [0, 1], "closure");
|
|
358
|
+
let reader = new BodyReader(req, res, settings, complete, failed);
|
|
359
|
+
__normalize(reader.start());
|
|
360
|
+
return null;
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
#submit(req, res, body) {
|
|
364
|
+
__requireArity(arguments.length, [0, 1, 2], "submit");
|
|
365
|
+
req = __boundary(req);
|
|
366
|
+
res = __boundary(res);
|
|
367
|
+
body = __boundary(body);
|
|
368
|
+
let pending = null;
|
|
369
|
+
let route = __normalize(this.#broker.route(__normalize(req.url)));
|
|
370
|
+
if (__requireBoolean(route === null)) {
|
|
371
|
+
pending = this.#dispatch(req, res, body);
|
|
372
|
+
} else {
|
|
373
|
+
pending = this.#longpoll(req, res, body, route);
|
|
374
|
+
}
|
|
375
|
+
return pending.then(__withArity((result) => {
|
|
376
|
+
result = __boundary(result);
|
|
377
|
+
this.#send(res, result);
|
|
378
|
+
return null;
|
|
379
|
+
return null;
|
|
380
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
381
|
+
error = __boundary(error);
|
|
382
|
+
this.#send(res, failure(500, "InternalServerError"));
|
|
383
|
+
return null;
|
|
384
|
+
return null;
|
|
385
|
+
}, [0], "closure"));
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
#longpoll(req, res, body, route) {
|
|
389
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "longpoll");
|
|
390
|
+
req = __boundary(req);
|
|
391
|
+
res = __boundary(res);
|
|
392
|
+
body = __boundary(body);
|
|
393
|
+
route = __boundary(route);
|
|
394
|
+
if (__requireBoolean(__normalize(__normalize(__hostGlobal(["Object"]).keys(__normalize(this.#polls))).length) >= __normalize(this.#definition.settings.redis.maxPendingPolls))) {
|
|
395
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(failure(503, "PollLimit")));
|
|
396
|
+
}
|
|
397
|
+
let authorize = __withArity((token, result) => {
|
|
398
|
+
token = __boundary(token);
|
|
399
|
+
result = __boundary(result);
|
|
400
|
+
return this.#dispatch(req, res, body, true, token, result);
|
|
401
|
+
return null;
|
|
402
|
+
}, [0, 1], "closure");
|
|
403
|
+
let session = new LongpollSession(__normalize(this.#broker), route, authorize, body);
|
|
404
|
+
let id = __normalize(crypto.randomUUID());
|
|
405
|
+
this.#polls[id] = session;
|
|
406
|
+
let disconnected = () => {
|
|
407
|
+
__normalize(session.cancel());
|
|
408
|
+
return null;
|
|
409
|
+
};
|
|
410
|
+
__normalize(res.once("close", disconnected));
|
|
411
|
+
return __normalize(session.run()).finally(() => {
|
|
412
|
+
__normalize(res.removeListener("close", disconnected));
|
|
413
|
+
__normalize(__hostGlobal(["Reflect"]).deleteProperty(__normalize(this.#polls), id));
|
|
414
|
+
return null;
|
|
415
|
+
});
|
|
416
|
+
return null;
|
|
417
|
+
}
|
|
418
|
+
#brokerMessage(message) {
|
|
419
|
+
__requireArity(arguments.length, [0], "brokerMessage");
|
|
420
|
+
message = __boundary(message);
|
|
421
|
+
let port = __normalize(message.port);
|
|
422
|
+
let pending = null;
|
|
423
|
+
try {
|
|
424
|
+
if (__normalize(message.operation) === "publish") {
|
|
425
|
+
pending = this.publish(__normalize(message.path), __normalize(message.key), __normalize(message.data));
|
|
426
|
+
} else if (__normalize(message.operation) === "cursor") {
|
|
427
|
+
pending = this.cursor(__normalize(message.path), __normalize(message.key));
|
|
428
|
+
} else {
|
|
429
|
+
pending = __normalize(__hostGlobal(["Promise"]).reject("Unknown longpoll operation"));
|
|
430
|
+
}
|
|
431
|
+
} catch (error) {
|
|
432
|
+
pending = __normalize(__hostGlobal(["Promise"]).reject(error));
|
|
433
|
+
}
|
|
434
|
+
return pending.then(__withArity((value) => {
|
|
435
|
+
value = __boundary(value);
|
|
436
|
+
brokerReply(port, { ok: true, value: value });
|
|
437
|
+
return null;
|
|
438
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
439
|
+
error = __boundary(error);
|
|
440
|
+
brokerReply(port, { ok: false });
|
|
441
|
+
return null;
|
|
442
|
+
}, [0], "closure")).finally(() => {
|
|
443
|
+
__normalize(port.close());
|
|
444
|
+
return null;
|
|
445
|
+
});
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
#dispatch(req, res, body, authorize = false, token = null, pollResult = null) {
|
|
449
|
+
__requireArity(arguments.length, [0, 1, 2], "dispatch");
|
|
450
|
+
req = __boundary(req);
|
|
451
|
+
res = __boundary(res);
|
|
452
|
+
body = __boundary(body);
|
|
453
|
+
authorize = __boundary(authorize);
|
|
454
|
+
token = __boundary(token);
|
|
455
|
+
pollResult = __boundary(pollResult);
|
|
456
|
+
let settings = __normalize(this.#definition.settings);
|
|
457
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#state) !== "running") || __requireBoolean(__requireBoolean(__normalize(this.#active) >= __normalize(settings.workers)) && __requireBoolean(__normalize(this.#waiting.length) >= __normalize(settings.maxQueue))))) {
|
|
458
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(failure(503, "Unavailable")));
|
|
459
|
+
}
|
|
460
|
+
let id = __normalize(crypto.randomUUID());
|
|
461
|
+
let record = { id: id, task: { authorize: authorize, pollResult: pollResult, body: body, request: { id: id, method: __normalize(req.method), path: __normalize(req.url), headers: __normalize(__hostGlobal(["Object"]).assign({ }, __normalize(req.headers))), remoteAddress: req.socket.remoteAddress } }, completed: __normalize(__hostGlobal(["Promise"]).withResolvers()), controller: new (__hostGlobal(["AbortController"]))(), started: false, result: null, expired: false, deadline: __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __normalize(settings.requestTimeoutMs)), timer: null };
|
|
462
|
+
if (__requireBoolean(token !== null)) {
|
|
463
|
+
record.task.request.headers.authorization = `Bearer ${token}`;
|
|
464
|
+
}
|
|
465
|
+
this.#tasks[id] = record;
|
|
466
|
+
this.#resetDeadline(record, __normalize(settings.requestTimeoutMs));
|
|
467
|
+
__add(this.#waiting, record);
|
|
468
|
+
let disconnected = () => {
|
|
469
|
+
if (__requireBoolean(!__requireBoolean(__normalize(res.writableEnded)))) {
|
|
470
|
+
this.#cancel(record);
|
|
471
|
+
}
|
|
472
|
+
return null;
|
|
473
|
+
};
|
|
474
|
+
__normalize(res.once("close", disconnected));
|
|
475
|
+
this.#pump();
|
|
476
|
+
return record.completed.promise.catch(__withArity((error) => {
|
|
477
|
+
error = __boundary(error);
|
|
478
|
+
if (__requireBoolean(__normalize(record.expired))) {
|
|
479
|
+
return failure(504, "RequestTimeout");
|
|
480
|
+
}
|
|
481
|
+
return failure(500, "InternalServerError");
|
|
482
|
+
return null;
|
|
483
|
+
}, [0], "closure")).finally(() => {
|
|
484
|
+
__normalize(res.removeListener("close", disconnected));
|
|
485
|
+
return null;
|
|
486
|
+
});
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
#expire(record) {
|
|
490
|
+
__requireArity(arguments.length, [0], "expire");
|
|
491
|
+
record = __boundary(record);
|
|
492
|
+
record.expired = true;
|
|
493
|
+
this.#cancel(record);
|
|
494
|
+
return null;
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
#cancel(record) {
|
|
498
|
+
__requireArity(arguments.length, [0], "cancel");
|
|
499
|
+
record = __boundary(record);
|
|
500
|
+
if (__requireBoolean(!__requireBoolean(__includes(__normalize(record.id), __normalize(this.#tasks))))) {
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
if (__requireBoolean(__normalize(record.started))) {
|
|
504
|
+
__normalize(record.controller.abort());
|
|
505
|
+
} else {
|
|
506
|
+
let index = __normalize(this.#waiting.indexOf(record));
|
|
507
|
+
if (__requireBoolean(index >= 0)) {
|
|
508
|
+
__normalize(this.#waiting.splice(index, 1));
|
|
509
|
+
}
|
|
510
|
+
this.#finish(record, null, "Request cancelled");
|
|
511
|
+
}
|
|
512
|
+
return null;
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
#pump() {
|
|
516
|
+
__loop1: while (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(this.#destroying))) && __requireBoolean(__normalize(this.#waiting.length) > 0)) && __requireBoolean(__normalize(this.#active) < __normalize(this.#definition.settings.workers)))) {
|
|
517
|
+
let record = __normalize(this.#waiting.shift());
|
|
518
|
+
record.started = true;
|
|
519
|
+
this.#active = __numericAdd(__normalize(this.#active), 1);
|
|
520
|
+
let pending = __normalize(this.#pool.run(__normalize(record.task), { signal: record.controller.signal }));
|
|
521
|
+
this.#settle(record, pending);
|
|
522
|
+
}
|
|
523
|
+
return null;
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
#settle(record, pending) {
|
|
527
|
+
__requireArity(arguments.length, [0, 1], "settle");
|
|
528
|
+
record = __boundary(record);
|
|
529
|
+
pending = __boundary(pending);
|
|
530
|
+
return pending.then(__withArity((result) => {
|
|
531
|
+
result = __boundary(result);
|
|
532
|
+
this.#finish(record, result, null);
|
|
533
|
+
return null;
|
|
534
|
+
return null;
|
|
535
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
536
|
+
const __contextScope = [];
|
|
537
|
+
try {
|
|
538
|
+
error = __boundary(error);
|
|
539
|
+
if (__requireBoolean(__normalize(record.result) !== null)) {
|
|
540
|
+
__normalize(__contextGet("log").err(`[${__normalize(record.id)}] Request event interrupted; preserving prepared result`));
|
|
541
|
+
}
|
|
542
|
+
this.#finish(record, __normalize(record.result), error);
|
|
543
|
+
return null;
|
|
544
|
+
return null;
|
|
545
|
+
} finally {
|
|
546
|
+
__contextRestore(__contextScope);
|
|
547
|
+
}
|
|
548
|
+
}, [0], "closure")).finally(() => {
|
|
549
|
+
this.#active = __finite(__normalize(this.#active) - 1);
|
|
550
|
+
this.#pump();
|
|
551
|
+
return null;
|
|
552
|
+
});
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
#finish(record, result, error) {
|
|
556
|
+
__requireArity(arguments.length, [0, 1, 2], "finish");
|
|
557
|
+
record = __boundary(record);
|
|
558
|
+
result = __boundary(result);
|
|
559
|
+
error = __boundary(error);
|
|
560
|
+
__hostGlobalCall(["clearTimeout"], __normalize(record.timer));
|
|
561
|
+
__normalize(__hostGlobal(["Reflect"]).deleteProperty(__normalize(this.#tasks), __normalize(record.id)));
|
|
562
|
+
if (__requireBoolean(result !== null)) {
|
|
563
|
+
__normalize(record.completed.resolve(result));
|
|
564
|
+
} else {
|
|
565
|
+
__normalize(record.completed.reject(error));
|
|
566
|
+
}
|
|
567
|
+
return null;
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
close() {
|
|
571
|
+
if (__requireBoolean(__normalize(this.#closing) !== null)) {
|
|
572
|
+
return __normalize(this.#closing);
|
|
573
|
+
}
|
|
574
|
+
let previous = __normalize(this.#state);
|
|
575
|
+
this.#state = "closing";
|
|
576
|
+
this.#shutdownDeadline = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __normalize(this.#definition.settings.shutdownTimeoutMs));
|
|
577
|
+
if (__requireBoolean(__requireBoolean(previous === "new") || __requireBoolean(previous === "closed"))) {
|
|
578
|
+
this.#state = "closed";
|
|
579
|
+
this.#closing = __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
580
|
+
return __normalize(this.#closing);
|
|
581
|
+
}
|
|
582
|
+
__normalize(this.#ready.reject("Server closed during startup"));
|
|
583
|
+
this.#closing = this.#stop();
|
|
584
|
+
return __normalize(this.#closing);
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
587
|
+
#stop() {
|
|
588
|
+
for (const record of __normalize(this.#connections)) {
|
|
589
|
+
if (__requireBoolean(__normalize(record.active) === 0)) {
|
|
590
|
+
__normalize(record.socket.destroy());
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
for (const [id, session] of __entries(__normalize(this.#polls))) {
|
|
594
|
+
__normalize(session.cancel());
|
|
595
|
+
}
|
|
596
|
+
let drained = new (__hostGlobal(["Promise"]))(__withArity((resolve) => {
|
|
597
|
+
resolve = __boundary(resolve);
|
|
598
|
+
let server = __normalize(this.#server);
|
|
599
|
+
if (__requireBoolean(__requireBoolean(server === null) || __requireBoolean(!__requireBoolean(__normalize(server.listening))))) {
|
|
600
|
+
__call(resolve, null);
|
|
601
|
+
} else {
|
|
602
|
+
server.close(() => {
|
|
603
|
+
__call(resolve, null);
|
|
604
|
+
return null;
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
return null;
|
|
608
|
+
}, [0], "closure"));
|
|
609
|
+
let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
610
|
+
let force = () => {
|
|
611
|
+
this.#destroying = true;
|
|
612
|
+
for (const record of __normalize(this.#waiting)) {
|
|
613
|
+
this.#finish(record, null, "Server closed");
|
|
614
|
+
}
|
|
615
|
+
this.#waiting = [];
|
|
616
|
+
if (__requireBoolean(__normalize(this.#server) !== null)) {
|
|
617
|
+
__normalize(this.#server.closeAllConnections());
|
|
618
|
+
}
|
|
619
|
+
__normalize(expired.reject("Server shutdown timed out"));
|
|
620
|
+
return null;
|
|
621
|
+
};
|
|
622
|
+
let timer = __hostGlobalCall(["setTimeout"], force, __normalize(this.#definition.settings.shutdownTimeoutMs));
|
|
623
|
+
return __normalize(__hostGlobal(["Promise"]).race([drained, __normalize(expired.promise)])).finally(() => {
|
|
624
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
625
|
+
return this.#destroyPool();
|
|
626
|
+
return null;
|
|
627
|
+
}).finally(() => {
|
|
628
|
+
__normalize(this.#broker.close());
|
|
629
|
+
this.#state = "closed";
|
|
630
|
+
return null;
|
|
631
|
+
});
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
#destroyPool() {
|
|
635
|
+
this.#destroying = true;
|
|
636
|
+
if (__requireBoolean(__normalize(this.#pool) === null)) {
|
|
637
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
638
|
+
}
|
|
639
|
+
let pool = __normalize(this.#pool);
|
|
640
|
+
let pending = __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
641
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.#definition.settings.database) !== null) && __requireBoolean(__normalize(this.#state) === "closing")) && __requireBoolean(__normalize(this.#active) === 0))) {
|
|
642
|
+
let remaining = __finite(__normalize(this.#shutdownDeadline) - __normalize(__hostGlobal(["Date"]).now()));
|
|
643
|
+
if (__requireBoolean(remaining > 0)) {
|
|
644
|
+
let tasks = [];
|
|
645
|
+
for (const index of __range(0, __normalize(this.#definition.settings.workers))) {
|
|
646
|
+
tasks.push(__normalize(pool.run({ close: true })));
|
|
647
|
+
}
|
|
648
|
+
pending = startupDeadline(__normalize(__hostGlobal(["Promise"]).all(tasks)), remaining);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return pending.finally(() => {
|
|
652
|
+
return __normalize(pool.destroy());
|
|
653
|
+
return null;
|
|
654
|
+
});
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
#send(res, result) {
|
|
658
|
+
__requireArity(arguments.length, [0, 1], "send");
|
|
659
|
+
res = __boundary(res);
|
|
660
|
+
result = __boundary(result);
|
|
661
|
+
if (__requireBoolean(__normalize(this.#state) === "closing")) {
|
|
662
|
+
result.headers.connection = "close";
|
|
663
|
+
}
|
|
664
|
+
return write(res, result);
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function pathJoin(directory, file) {
|
|
669
|
+
__requireArity(arguments.length, [0, 1], "pathJoin");
|
|
670
|
+
directory = __boundary(directory);
|
|
671
|
+
file = __boundary(file);
|
|
672
|
+
return __normalize(path.join(directory, file));
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function startupDeadline(pending, timeout) {
|
|
677
|
+
__requireArity(arguments.length, [0, 1], "startupDeadline");
|
|
678
|
+
pending = __boundary(pending);
|
|
679
|
+
timeout = __boundary(timeout);
|
|
680
|
+
let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
681
|
+
let expire = () => {
|
|
682
|
+
__normalize(expired.reject("Worker startup timed out"));
|
|
683
|
+
return null;
|
|
684
|
+
};
|
|
685
|
+
let timer = __hostGlobalCall(["setTimeout"], expire, timeout);
|
|
686
|
+
return __normalize(__hostGlobal(["Promise"]).race([pending, __normalize(expired.promise)])).finally(() => {
|
|
687
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
688
|
+
return null;
|
|
689
|
+
});
|
|
690
|
+
return null;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function failure(status, code) {
|
|
694
|
+
__requireArity(arguments.length, [0, 1], "failure");
|
|
695
|
+
status = __boundary(status);
|
|
696
|
+
code = __boundary(code);
|
|
697
|
+
return { status: status, headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }, body: __toJson({ error: code }) };
|
|
698
|
+
return null;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function write(res, result) {
|
|
702
|
+
__requireArity(arguments.length, [0, 1], "write");
|
|
703
|
+
res = __boundary(res);
|
|
704
|
+
result = __boundary(result);
|
|
705
|
+
if (__requireBoolean(__requireBoolean(__normalize(res.destroyed)) || __requireBoolean(__normalize(res.writableEnded)))) {
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
__normalize(res.writeHead(__normalize(result.status), __normalize(result.headers)));
|
|
710
|
+
__normalize(res.end(__normalize(result.body)));
|
|
711
|
+
} catch (error) {
|
|
712
|
+
__normalize(res.destroy());
|
|
713
|
+
}
|
|
714
|
+
return null;
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function rejectHttp(req, res, status, code) {
|
|
719
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "rejectHttp");
|
|
720
|
+
req = __boundary(req);
|
|
721
|
+
res = __boundary(res);
|
|
722
|
+
status = __boundary(status);
|
|
723
|
+
code = __boundary(code);
|
|
724
|
+
__normalize(res.setHeader("Connection", "close"));
|
|
725
|
+
__normalize(res.once("finish", () => {
|
|
726
|
+
__normalize(req.destroy());
|
|
727
|
+
return null;
|
|
728
|
+
}));
|
|
729
|
+
__normalize(req.resume());
|
|
730
|
+
return write(res, failure(status, code));
|
|
731
|
+
return null;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
export function main(args) {
|
|
735
|
+
__requireArity(arguments.length, [0], "main");
|
|
736
|
+
args = __boundary(args);
|
|
737
|
+
if (__requireBoolean(__normalize(args.length) !== 1)) {
|
|
738
|
+
throw "Usage: lango-server <application-directory>";
|
|
739
|
+
}
|
|
740
|
+
let server = new Server(__normalize(args[0]));
|
|
741
|
+
let stop = () => {
|
|
742
|
+
return __normalize(server.close()).catch(__withArity((error) => {
|
|
743
|
+
const __contextScope = [];
|
|
744
|
+
try {
|
|
745
|
+
error = __boundary(error);
|
|
746
|
+
__normalize(__contextGet("log").err("Server shutdown failed"));
|
|
747
|
+
__hostGlobal(["process"]).exitCode = 1;
|
|
748
|
+
return null;
|
|
749
|
+
} finally {
|
|
750
|
+
__contextRestore(__contextScope);
|
|
751
|
+
}
|
|
752
|
+
}, [0], "closure"));
|
|
753
|
+
return null;
|
|
754
|
+
};
|
|
755
|
+
__normalize(__hostGlobal(["process"]).once("SIGTERM", stop));
|
|
756
|
+
__normalize(__hostGlobal(["process"]).once("SIGINT", stop));
|
|
757
|
+
return __normalize(server.start()).then(__withArity((address) => {
|
|
758
|
+
const __contextScope = [];
|
|
759
|
+
try {
|
|
760
|
+
address = __boundary(address);
|
|
761
|
+
__normalize(__contextGet("log").info(`Listening on ${__normalize(address.host)}:${__normalize(address.port)}`));
|
|
762
|
+
return null;
|
|
763
|
+
return null;
|
|
764
|
+
} finally {
|
|
765
|
+
__contextRestore(__contextScope);
|
|
766
|
+
}
|
|
767
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
768
|
+
error = __boundary(error);
|
|
769
|
+
__normalize(__hostGlobal(["process"]).removeListener("SIGTERM", stop));
|
|
770
|
+
__normalize(__hostGlobal(["process"]).removeListener("SIGINT", stop));
|
|
771
|
+
throw error;
|
|
772
|
+
return null;
|
|
773
|
+
}, [0], "closure"));
|
|
774
|
+
return null;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function brokerReply(port, message) {
|
|
778
|
+
__requireArity(arguments.length, [0, 1], "brokerReply");
|
|
779
|
+
port = __boundary(port);
|
|
780
|
+
message = __boundary(message);
|
|
781
|
+
try {
|
|
782
|
+
__normalize(port.postMessage(message));
|
|
783
|
+
} catch (error) {
|
|
784
|
+
return null;
|
|
785
|
+
}
|
|
786
|
+
return null;
|
|
787
|
+
return null;
|
|
788
|
+
}
|
|
789
|
+
const { configure } = await import("./.lango/runtime.mjs");
|
|
790
|
+
configure({ output: (value) => console.log(value) });
|
|
791
|
+
await main(process.argv.slice(2));
|
|
792
|
+
//# sourceMappingURL=server.mjs.map
|