@lango-project/lango-server 0.0.0-stage → 0.1.0
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 +431 -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 +69 -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 +44 -0
- package/dist/cli/endpoint.mjs.map +1 -0
- package/dist/cli/options.mjs +165 -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 +233 -0
- package/dist/cli/routes.mjs.map +1 -0
- package/dist/cli/server.mjs +572 -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/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 +44 -0
- package/dist/endpoint.mjs.map +1 -0
- package/dist/group.mjs +75 -0
- package/dist/group.mjs.map +1 -0
- package/dist/library.mjs +6 -0
- package/dist/library.mjs.map +1 -0
- package/dist/options.mjs +165 -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 +233 -0
- package/dist/routes.mjs.map +1 -0
- package/dist/runtime.mjs +475 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/server.mjs +568 -0
- package/dist/server.mjs.map +1 -0
- package/dist/sqlite_adapter.mjs +163 -0
- package/dist/sqlite_adapter.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 +39 -4
- package/src/body_reader.lango +113 -0
- package/src/builder.lango +53 -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 +23 -0
- package/src/group.lango +47 -0
- package/src/library.lango +10 -0
- package/src/options.lango +128 -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 +160 -0
- package/src/runtime.lango +320 -0
- package/src/server.lango +432 -0
- package/src/sqlite_adapter.lango +127 -0
- package/src/token.lango +70 -0
- package/src/tokens.lango +70 -0
- package/src/worker.lango +43 -0
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
import { prepareDatabase } from "./database.mjs";
|
|
2
|
+
import { deployment } from "./options.mjs";
|
|
3
|
+
import { Routes } from "./routes.mjs";
|
|
4
|
+
import { BodyReader } from "./body_reader.mjs";
|
|
5
|
+
import * as http from "node:http";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import * as modules from "node:module";
|
|
8
|
+
import * as crypto from "node:crypto";
|
|
9
|
+
import * as piscina from "piscina";
|
|
10
|
+
import {
|
|
11
|
+
hostGlobal as __hostGlobal,
|
|
12
|
+
hostGlobalCall as __hostGlobalCall,
|
|
13
|
+
contextGet as __contextGet,
|
|
14
|
+
contextSet as __contextSet,
|
|
15
|
+
contextRestore as __contextRestore,
|
|
16
|
+
fromJson as __fromJson,
|
|
17
|
+
toJson as __toJson,
|
|
18
|
+
isNumber as __isNumber,
|
|
19
|
+
isString as __isString,
|
|
20
|
+
isBool as __isBool,
|
|
21
|
+
isObject as __isObject,
|
|
22
|
+
isArray as __isArray,
|
|
23
|
+
includes as __includes,
|
|
24
|
+
range as __range,
|
|
25
|
+
add as __add,
|
|
26
|
+
requireBoolean as __requireBoolean,
|
|
27
|
+
normalize as __normalize,
|
|
28
|
+
finite as __finite,
|
|
29
|
+
numericAdd as __numericAdd,
|
|
30
|
+
call as __call,
|
|
31
|
+
boundary as __boundary,
|
|
32
|
+
requireArity as __requireArity,
|
|
33
|
+
withArity as __withArity,
|
|
34
|
+
} from "./.lango/runtime.mjs";
|
|
35
|
+
|
|
36
|
+
export class Server {
|
|
37
|
+
#definition;
|
|
38
|
+
#state;
|
|
39
|
+
#server;
|
|
40
|
+
#pool;
|
|
41
|
+
#starting;
|
|
42
|
+
#closing;
|
|
43
|
+
#waiting;
|
|
44
|
+
#active;
|
|
45
|
+
#tasks;
|
|
46
|
+
#ready;
|
|
47
|
+
#readyIds;
|
|
48
|
+
#destroying;
|
|
49
|
+
#lango;
|
|
50
|
+
#shutdownDeadline;
|
|
51
|
+
constructor(path, definition = null) {
|
|
52
|
+
__requireArity(arguments.length, [0], "constructor");
|
|
53
|
+
path = __boundary(path);
|
|
54
|
+
definition = __boundary(definition);
|
|
55
|
+
if (__requireBoolean(path !== null)) {
|
|
56
|
+
let directory = path;
|
|
57
|
+
let routes = new Routes(directory);
|
|
58
|
+
__normalize(routes.xml(pathJoin(directory, "routes.xml")));
|
|
59
|
+
definition = { settings: deployment(directory), routes: __normalize(routes.definitions()), events: __normalize(routes.events()) };
|
|
60
|
+
}
|
|
61
|
+
if (__requireBoolean(definition === null)) {
|
|
62
|
+
throw "Use Server(directory) or Builder.build()";
|
|
63
|
+
}
|
|
64
|
+
this.#definition = __fromJson(__toJson(definition));
|
|
65
|
+
this.#state = "new";
|
|
66
|
+
this.#server = null;
|
|
67
|
+
this.#pool = null;
|
|
68
|
+
this.#starting = null;
|
|
69
|
+
this.#closing = null;
|
|
70
|
+
this.#waiting = [];
|
|
71
|
+
this.#active = 0;
|
|
72
|
+
this.#tasks = __normalize(__hostGlobal(["Object"]).create(null));
|
|
73
|
+
this.#ready = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
74
|
+
this.#readyIds = [];
|
|
75
|
+
this.#destroying = false;
|
|
76
|
+
}
|
|
77
|
+
start() {
|
|
78
|
+
if (__requireBoolean(__normalize(this.#state) !== "new")) {
|
|
79
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Server can only be started once"));
|
|
80
|
+
}
|
|
81
|
+
this.#state = "starting";
|
|
82
|
+
this.#starting = this.#launch();
|
|
83
|
+
return __normalize(this.#starting);
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
#launch() {
|
|
87
|
+
let settings = __normalize(this.#definition.settings);
|
|
88
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity((ignored) => {
|
|
89
|
+
ignored = __boundary(ignored);
|
|
90
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(__normalize(settings.root), "package.json"))));
|
|
91
|
+
let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
|
|
92
|
+
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) });
|
|
93
|
+
let pool = __normalize(this.#pool);
|
|
94
|
+
__normalize(pool.on("message", __withArity((message) => {
|
|
95
|
+
message = __boundary(message);
|
|
96
|
+
this.#messageReceived(message);
|
|
97
|
+
return null;
|
|
98
|
+
}, [0], "closure")));
|
|
99
|
+
__normalize(pool.on("error", __withArity((error) => {
|
|
100
|
+
const __contextScope = [];
|
|
101
|
+
try {
|
|
102
|
+
error = __boundary(error);
|
|
103
|
+
__normalize(this.#ready.reject(error));
|
|
104
|
+
__normalize(__contextGet("log").err("Worker pool error"));
|
|
105
|
+
return null;
|
|
106
|
+
} finally {
|
|
107
|
+
__contextRestore(__contextScope);
|
|
108
|
+
}
|
|
109
|
+
}, [0], "closure")));
|
|
110
|
+
return startupDeadline(__normalize(this.#ready.promise), __normalize(settings.startupTimeoutMs));
|
|
111
|
+
return null;
|
|
112
|
+
}, [0], "closure")).then(__withArity((ignored) => {
|
|
113
|
+
ignored = __boundary(ignored);
|
|
114
|
+
if (__requireBoolean(__normalize(this.#state) !== "starting")) {
|
|
115
|
+
throw "Server closed during startup";
|
|
116
|
+
}
|
|
117
|
+
let server = new http.Server(__withArity((req, res) => {
|
|
118
|
+
req = __boundary(req);
|
|
119
|
+
res = __boundary(res);
|
|
120
|
+
this.#receive(req, res);
|
|
121
|
+
return null;
|
|
122
|
+
}, [0, 1], "closure"));
|
|
123
|
+
this.#server = server;
|
|
124
|
+
server.headersTimeout = __normalize(settings.bodyTimeoutMs);
|
|
125
|
+
server.requestTimeout = __normalize(settings.bodyTimeoutMs);
|
|
126
|
+
let listening = new (__hostGlobal(["Promise"]))(__withArity((resolve, reject) => {
|
|
127
|
+
resolve = __boundary(resolve);
|
|
128
|
+
reject = __boundary(reject);
|
|
129
|
+
__normalize(server.once("error", reject));
|
|
130
|
+
__normalize(server.listen(__normalize(settings.port), __normalize(settings.host), () => {
|
|
131
|
+
__normalize(server.removeListener("error", reject));
|
|
132
|
+
if (__requireBoolean(__normalize(this.#state) !== "starting")) {
|
|
133
|
+
__call(reject, "Server closed during startup");
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
this.#state = "running";
|
|
137
|
+
let address = __normalize(server.address());
|
|
138
|
+
__call(resolve, { host: __normalize(address.address), port: __normalize(address.port) });
|
|
139
|
+
return null;
|
|
140
|
+
}));
|
|
141
|
+
return null;
|
|
142
|
+
}, [0, 1], "closure"));
|
|
143
|
+
return listening;
|
|
144
|
+
return null;
|
|
145
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
146
|
+
error = __boundary(error);
|
|
147
|
+
this.#state = "closed";
|
|
148
|
+
return this.#destroyPool().then(__withArity((ignored) => {
|
|
149
|
+
ignored = __boundary(ignored);
|
|
150
|
+
throw error;
|
|
151
|
+
return null;
|
|
152
|
+
}, [0], "closure"));
|
|
153
|
+
return null;
|
|
154
|
+
}, [0], "closure"));
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
#messageReceived(message) {
|
|
158
|
+
__requireArity(arguments.length, [0], "messageReceived");
|
|
159
|
+
message = __boundary(message);
|
|
160
|
+
if (__requireBoolean(__normalize(message.kind) === "ready")) {
|
|
161
|
+
if (__requireBoolean(!__requireBoolean(__normalize(this.#readyIds.includes(__normalize(message.threadId)))))) {
|
|
162
|
+
__add(this.#readyIds, __normalize(message.threadId));
|
|
163
|
+
}
|
|
164
|
+
if (__requireBoolean(__normalize(this.#readyIds.length) >= __normalize(this.#definition.settings.workers))) {
|
|
165
|
+
__normalize(this.#ready.resolve(null));
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
if (__requireBoolean(!__requireBoolean(__includes(__normalize(message.id), __normalize(this.#tasks))))) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
let record = __normalize(this.#tasks[__normalize(message.id)]);
|
|
173
|
+
if (__requireBoolean(__normalize(message.kind) === "prepared")) {
|
|
174
|
+
record.result = __normalize(message.result);
|
|
175
|
+
this.#resetDeadline(record, __normalize(this.#definition.settings.eventTimeoutMs));
|
|
176
|
+
}
|
|
177
|
+
if (__requireBoolean(__normalize(message.kind) === "event")) {
|
|
178
|
+
this.#resetDeadline(record, __normalize(this.#definition.settings.eventTimeoutMs));
|
|
179
|
+
}
|
|
180
|
+
if (__requireBoolean(__requireBoolean(__normalize(message.kind) === "handler") && __requireBoolean(__normalize(record.result) === null))) {
|
|
181
|
+
this.#resetDeadline(record, __finite(__normalize(record.deadline) - __normalize(__hostGlobal(["Date"]).now())));
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
#resetDeadline(record, duration) {
|
|
187
|
+
__requireArity(arguments.length, [0, 1], "resetDeadline");
|
|
188
|
+
record = __boundary(record);
|
|
189
|
+
duration = __boundary(duration);
|
|
190
|
+
__hostGlobalCall(["clearTimeout"], __normalize(record.timer));
|
|
191
|
+
if (__requireBoolean(__normalize(record.result) === null)) {
|
|
192
|
+
duration = __normalize(__hostGlobal(["Math"]).min(duration, __finite(__normalize(record.deadline) - __normalize(__hostGlobal(["Date"]).now()))));
|
|
193
|
+
}
|
|
194
|
+
let expired = () => {
|
|
195
|
+
this.#expire(record);
|
|
196
|
+
return null;
|
|
197
|
+
};
|
|
198
|
+
record.timer = __hostGlobalCall(["setTimeout"], expired, __normalize(__hostGlobal(["Math"]).max(1, duration)));
|
|
199
|
+
return null;
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
#receive(req, res) {
|
|
203
|
+
__requireArity(arguments.length, [0, 1], "receive");
|
|
204
|
+
req = __boundary(req);
|
|
205
|
+
res = __boundary(res);
|
|
206
|
+
__normalize(req.on("error", __withArity((error) => {
|
|
207
|
+
error = __boundary(error);
|
|
208
|
+
return null;
|
|
209
|
+
return null;
|
|
210
|
+
}, [0], "closure")));
|
|
211
|
+
if (__requireBoolean(__normalize(this.#state) !== "running")) {
|
|
212
|
+
return rejectHttp(req, res, 503, "Unavailable");
|
|
213
|
+
}
|
|
214
|
+
if (__requireBoolean(__normalize(req.method) !== "POST")) {
|
|
215
|
+
__normalize(res.setHeader("Allow", "POST"));
|
|
216
|
+
return rejectHttp(req, res, 405, "MethodNotAllowed");
|
|
217
|
+
}
|
|
218
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(req.url.includes("?"))) || __requireBoolean(__normalize(req.url.includes("#")))) || __requireBoolean(__normalize(req.url.includes("\\"))))) {
|
|
219
|
+
return rejectHttp(req, res, 400, "InvalidPath");
|
|
220
|
+
}
|
|
221
|
+
let type = req.headers["content-type"];
|
|
222
|
+
let media = "";
|
|
223
|
+
if (__requireBoolean(__isString(type))) {
|
|
224
|
+
let parts = __normalize(type.split(";"));
|
|
225
|
+
media = __normalize(__normalize(parts[0]).trim());
|
|
226
|
+
}
|
|
227
|
+
if (__requireBoolean(__normalize(media.toLowerCase()) !== "application/json")) {
|
|
228
|
+
return rejectHttp(req, res, 415, "JsonRequired");
|
|
229
|
+
}
|
|
230
|
+
let settings = __normalize(this.#definition.settings);
|
|
231
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#active) >= __normalize(settings.workers)) && __requireBoolean(__normalize(this.#waiting.length) >= __normalize(settings.maxQueue)))) {
|
|
232
|
+
return rejectHttp(req, res, 503, "QueueFull");
|
|
233
|
+
}
|
|
234
|
+
let complete = __withArity((body) => {
|
|
235
|
+
body = __boundary(body);
|
|
236
|
+
this.#submit(req, res, body);
|
|
237
|
+
return null;
|
|
238
|
+
}, [0], "closure");
|
|
239
|
+
let failed = __withArity((status, code) => {
|
|
240
|
+
status = __boundary(status);
|
|
241
|
+
code = __boundary(code);
|
|
242
|
+
this.#send(res, failure(status, code));
|
|
243
|
+
return null;
|
|
244
|
+
}, [0, 1], "closure");
|
|
245
|
+
let reader = new BodyReader(req, res, settings, complete, failed);
|
|
246
|
+
__normalize(reader.start());
|
|
247
|
+
return null;
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
#submit(req, res, body) {
|
|
251
|
+
__requireArity(arguments.length, [0, 1, 2], "submit");
|
|
252
|
+
req = __boundary(req);
|
|
253
|
+
res = __boundary(res);
|
|
254
|
+
body = __boundary(body);
|
|
255
|
+
let settings = __normalize(this.#definition.settings);
|
|
256
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#state) !== "running") || __requireBoolean(__requireBoolean(__normalize(this.#active) >= __normalize(settings.workers)) && __requireBoolean(__normalize(this.#waiting.length) >= __normalize(settings.maxQueue))))) {
|
|
257
|
+
return this.#send(res, failure(503, "Unavailable"));
|
|
258
|
+
}
|
|
259
|
+
let id = __normalize(crypto.randomUUID());
|
|
260
|
+
let record = { id: id, task: { body: body, request: { id: id, method: __normalize(req.method), path: __normalize(req.url), headers: __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 };
|
|
261
|
+
this.#tasks[id] = record;
|
|
262
|
+
this.#resetDeadline(record, __normalize(settings.requestTimeoutMs));
|
|
263
|
+
__add(this.#waiting, record);
|
|
264
|
+
__normalize(res.once("close", () => {
|
|
265
|
+
if (__requireBoolean(!__requireBoolean(__normalize(res.writableEnded)))) {
|
|
266
|
+
this.#cancel(record);
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}));
|
|
270
|
+
this.#pump();
|
|
271
|
+
return record.completed.promise.then(__withArity((result) => {
|
|
272
|
+
result = __boundary(result);
|
|
273
|
+
this.#send(res, result);
|
|
274
|
+
return null;
|
|
275
|
+
return null;
|
|
276
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
277
|
+
error = __boundary(error);
|
|
278
|
+
let code = 500;
|
|
279
|
+
let name = "InternalServerError";
|
|
280
|
+
if (__requireBoolean(__normalize(record.expired))) {
|
|
281
|
+
code = 504;
|
|
282
|
+
name = "RequestTimeout";
|
|
283
|
+
}
|
|
284
|
+
this.#send(res, failure(code, name));
|
|
285
|
+
return null;
|
|
286
|
+
return null;
|
|
287
|
+
}, [0], "closure"));
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
#expire(record) {
|
|
291
|
+
__requireArity(arguments.length, [0], "expire");
|
|
292
|
+
record = __boundary(record);
|
|
293
|
+
record.expired = true;
|
|
294
|
+
this.#cancel(record);
|
|
295
|
+
return null;
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
#cancel(record) {
|
|
299
|
+
__requireArity(arguments.length, [0], "cancel");
|
|
300
|
+
record = __boundary(record);
|
|
301
|
+
if (__requireBoolean(!__requireBoolean(__includes(__normalize(record.id), __normalize(this.#tasks))))) {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
if (__requireBoolean(__normalize(record.started))) {
|
|
305
|
+
__normalize(record.controller.abort());
|
|
306
|
+
} else {
|
|
307
|
+
let index = __normalize(this.#waiting.indexOf(record));
|
|
308
|
+
if (__requireBoolean(index >= 0)) {
|
|
309
|
+
__normalize(this.#waiting.splice(index, 1));
|
|
310
|
+
}
|
|
311
|
+
this.#finish(record, null, "Request cancelled");
|
|
312
|
+
}
|
|
313
|
+
return null;
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
#pump() {
|
|
317
|
+
__loop1: while (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(this.#destroying))) && __requireBoolean(__normalize(this.#waiting.length) > 0)) && __requireBoolean(__normalize(this.#active) < __normalize(this.#definition.settings.workers)))) {
|
|
318
|
+
let record = __normalize(this.#waiting.shift());
|
|
319
|
+
record.started = true;
|
|
320
|
+
this.#active = __numericAdd(__normalize(this.#active), 1);
|
|
321
|
+
let pending = __normalize(this.#pool.run(__normalize(record.task), { signal: record.controller.signal }));
|
|
322
|
+
this.#settle(record, pending);
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
#settle(record, pending) {
|
|
328
|
+
__requireArity(arguments.length, [0, 1], "settle");
|
|
329
|
+
record = __boundary(record);
|
|
330
|
+
pending = __boundary(pending);
|
|
331
|
+
return pending.then(__withArity((result) => {
|
|
332
|
+
result = __boundary(result);
|
|
333
|
+
this.#finish(record, result, null);
|
|
334
|
+
return null;
|
|
335
|
+
return null;
|
|
336
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
337
|
+
const __contextScope = [];
|
|
338
|
+
try {
|
|
339
|
+
error = __boundary(error);
|
|
340
|
+
if (__requireBoolean(__normalize(record.result) !== null)) {
|
|
341
|
+
__normalize(__contextGet("log").err(`[${__normalize(record.id)}] Request event interrupted; preserving prepared result`));
|
|
342
|
+
}
|
|
343
|
+
this.#finish(record, __normalize(record.result), error);
|
|
344
|
+
return null;
|
|
345
|
+
return null;
|
|
346
|
+
} finally {
|
|
347
|
+
__contextRestore(__contextScope);
|
|
348
|
+
}
|
|
349
|
+
}, [0], "closure")).finally(() => {
|
|
350
|
+
this.#active = __finite(__normalize(this.#active) - 1);
|
|
351
|
+
this.#pump();
|
|
352
|
+
return null;
|
|
353
|
+
});
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
#finish(record, result, error) {
|
|
357
|
+
__requireArity(arguments.length, [0, 1, 2], "finish");
|
|
358
|
+
record = __boundary(record);
|
|
359
|
+
result = __boundary(result);
|
|
360
|
+
error = __boundary(error);
|
|
361
|
+
__hostGlobalCall(["clearTimeout"], __normalize(record.timer));
|
|
362
|
+
__normalize(__hostGlobal(["Reflect"]).deleteProperty(__normalize(this.#tasks), __normalize(record.id)));
|
|
363
|
+
if (__requireBoolean(result !== null)) {
|
|
364
|
+
__normalize(record.completed.resolve(result));
|
|
365
|
+
} else {
|
|
366
|
+
__normalize(record.completed.reject(error));
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
close() {
|
|
372
|
+
if (__requireBoolean(__normalize(this.#closing) !== null)) {
|
|
373
|
+
return __normalize(this.#closing);
|
|
374
|
+
}
|
|
375
|
+
let previous = __normalize(this.#state);
|
|
376
|
+
this.#state = "closing";
|
|
377
|
+
this.#shutdownDeadline = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __normalize(this.#definition.settings.shutdownTimeoutMs));
|
|
378
|
+
if (__requireBoolean(__requireBoolean(previous === "new") || __requireBoolean(previous === "closed"))) {
|
|
379
|
+
this.#state = "closed";
|
|
380
|
+
this.#closing = __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
381
|
+
return __normalize(this.#closing);
|
|
382
|
+
}
|
|
383
|
+
__normalize(this.#ready.reject("Server closed during startup"));
|
|
384
|
+
this.#closing = this.#stop();
|
|
385
|
+
return __normalize(this.#closing);
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
#stop() {
|
|
389
|
+
let drained = new (__hostGlobal(["Promise"]))(__withArity((resolve) => {
|
|
390
|
+
resolve = __boundary(resolve);
|
|
391
|
+
let server = __normalize(this.#server);
|
|
392
|
+
if (__requireBoolean(__requireBoolean(server === null) || __requireBoolean(!__requireBoolean(__normalize(server.listening))))) {
|
|
393
|
+
__call(resolve, null);
|
|
394
|
+
} else {
|
|
395
|
+
server.close(() => {
|
|
396
|
+
__call(resolve, null);
|
|
397
|
+
return null;
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
return null;
|
|
401
|
+
}, [0], "closure"));
|
|
402
|
+
let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
403
|
+
let force = () => {
|
|
404
|
+
this.#destroying = true;
|
|
405
|
+
for (const record of __normalize(this.#waiting)) {
|
|
406
|
+
this.#finish(record, null, "Server closed");
|
|
407
|
+
}
|
|
408
|
+
this.#waiting = [];
|
|
409
|
+
if (__requireBoolean(__normalize(this.#server) !== null)) {
|
|
410
|
+
__normalize(this.#server.closeAllConnections());
|
|
411
|
+
}
|
|
412
|
+
__normalize(expired.reject("Server shutdown timed out"));
|
|
413
|
+
return null;
|
|
414
|
+
};
|
|
415
|
+
let timer = __hostGlobalCall(["setTimeout"], force, __normalize(this.#definition.settings.shutdownTimeoutMs));
|
|
416
|
+
return __normalize(__hostGlobal(["Promise"]).race([drained, __normalize(expired.promise)])).finally(() => {
|
|
417
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
418
|
+
return this.#destroyPool();
|
|
419
|
+
return null;
|
|
420
|
+
}).finally(() => {
|
|
421
|
+
this.#state = "closed";
|
|
422
|
+
return null;
|
|
423
|
+
});
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
#destroyPool() {
|
|
427
|
+
this.#destroying = true;
|
|
428
|
+
if (__requireBoolean(__normalize(this.#pool) === null)) {
|
|
429
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
430
|
+
}
|
|
431
|
+
let pool = __normalize(this.#pool);
|
|
432
|
+
let pending = __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
433
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.#definition.settings.database) !== null) && __requireBoolean(__normalize(this.#state) === "closing")) && __requireBoolean(__normalize(this.#active) === 0))) {
|
|
434
|
+
let remaining = __finite(__normalize(this.#shutdownDeadline) - __normalize(__hostGlobal(["Date"]).now()));
|
|
435
|
+
if (__requireBoolean(remaining > 0)) {
|
|
436
|
+
let tasks = [];
|
|
437
|
+
for (const index of __range(0, __normalize(this.#definition.settings.workers))) {
|
|
438
|
+
tasks.push(__normalize(pool.run({ close: true })));
|
|
439
|
+
}
|
|
440
|
+
pending = startupDeadline(__normalize(__hostGlobal(["Promise"]).all(tasks)), remaining);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return pending.finally(() => {
|
|
444
|
+
return __normalize(pool.destroy());
|
|
445
|
+
return null;
|
|
446
|
+
});
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
#send(res, result) {
|
|
450
|
+
__requireArity(arguments.length, [0, 1], "send");
|
|
451
|
+
res = __boundary(res);
|
|
452
|
+
result = __boundary(result);
|
|
453
|
+
if (__requireBoolean(__normalize(this.#state) === "closing")) {
|
|
454
|
+
result.headers.connection = "close";
|
|
455
|
+
}
|
|
456
|
+
return write(res, result);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
function pathJoin(directory, file) {
|
|
461
|
+
__requireArity(arguments.length, [0, 1], "pathJoin");
|
|
462
|
+
directory = __boundary(directory);
|
|
463
|
+
file = __boundary(file);
|
|
464
|
+
return __normalize(path.join(directory, file));
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function startupDeadline(pending, timeout) {
|
|
469
|
+
__requireArity(arguments.length, [0, 1], "startupDeadline");
|
|
470
|
+
pending = __boundary(pending);
|
|
471
|
+
timeout = __boundary(timeout);
|
|
472
|
+
let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
473
|
+
let expire = () => {
|
|
474
|
+
__normalize(expired.reject("Worker startup timed out"));
|
|
475
|
+
return null;
|
|
476
|
+
};
|
|
477
|
+
let timer = __hostGlobalCall(["setTimeout"], expire, timeout);
|
|
478
|
+
return __normalize(__hostGlobal(["Promise"]).race([pending, __normalize(expired.promise)])).finally(() => {
|
|
479
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
480
|
+
return null;
|
|
481
|
+
});
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function failure(status, code) {
|
|
486
|
+
__requireArity(arguments.length, [0, 1], "failure");
|
|
487
|
+
status = __boundary(status);
|
|
488
|
+
code = __boundary(code);
|
|
489
|
+
return { status: status, headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }, body: __toJson({ error: code }) };
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function write(res, result) {
|
|
494
|
+
__requireArity(arguments.length, [0, 1], "write");
|
|
495
|
+
res = __boundary(res);
|
|
496
|
+
result = __boundary(result);
|
|
497
|
+
if (__requireBoolean(__requireBoolean(__normalize(res.destroyed)) || __requireBoolean(__normalize(res.writableEnded)))) {
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
__normalize(res.writeHead(__normalize(result.status), __normalize(result.headers)));
|
|
502
|
+
__normalize(res.end(__normalize(result.body)));
|
|
503
|
+
} catch (error) {
|
|
504
|
+
__normalize(res.destroy());
|
|
505
|
+
}
|
|
506
|
+
return null;
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function rejectHttp(req, res, status, code) {
|
|
511
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "rejectHttp");
|
|
512
|
+
req = __boundary(req);
|
|
513
|
+
res = __boundary(res);
|
|
514
|
+
status = __boundary(status);
|
|
515
|
+
code = __boundary(code);
|
|
516
|
+
__normalize(res.setHeader("Connection", "close"));
|
|
517
|
+
__normalize(res.once("finish", () => {
|
|
518
|
+
__normalize(req.destroy());
|
|
519
|
+
return null;
|
|
520
|
+
}));
|
|
521
|
+
__normalize(req.resume());
|
|
522
|
+
return write(res, failure(status, code));
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export function main(args) {
|
|
527
|
+
__requireArity(arguments.length, [0], "main");
|
|
528
|
+
args = __boundary(args);
|
|
529
|
+
if (__requireBoolean(__normalize(args.length) !== 1)) {
|
|
530
|
+
throw "Usage: lango-server <application-directory>";
|
|
531
|
+
}
|
|
532
|
+
let server = new Server(__normalize(args[0]));
|
|
533
|
+
let stop = () => {
|
|
534
|
+
return __normalize(server.close()).catch(__withArity((error) => {
|
|
535
|
+
const __contextScope = [];
|
|
536
|
+
try {
|
|
537
|
+
error = __boundary(error);
|
|
538
|
+
__normalize(__contextGet("log").err("Server shutdown failed"));
|
|
539
|
+
__hostGlobal(["process"]).exitCode = 1;
|
|
540
|
+
return null;
|
|
541
|
+
} finally {
|
|
542
|
+
__contextRestore(__contextScope);
|
|
543
|
+
}
|
|
544
|
+
}, [0], "closure"));
|
|
545
|
+
return null;
|
|
546
|
+
};
|
|
547
|
+
__normalize(__hostGlobal(["process"]).once("SIGTERM", stop));
|
|
548
|
+
__normalize(__hostGlobal(["process"]).once("SIGINT", stop));
|
|
549
|
+
return __normalize(server.start()).then(__withArity((address) => {
|
|
550
|
+
const __contextScope = [];
|
|
551
|
+
try {
|
|
552
|
+
address = __boundary(address);
|
|
553
|
+
__normalize(__contextGet("log").info(`Listening on ${__normalize(address.host)}:${__normalize(address.port)}`));
|
|
554
|
+
return null;
|
|
555
|
+
return null;
|
|
556
|
+
} finally {
|
|
557
|
+
__contextRestore(__contextScope);
|
|
558
|
+
}
|
|
559
|
+
}, [0], "closure")).catch(__withArity((error) => {
|
|
560
|
+
error = __boundary(error);
|
|
561
|
+
__normalize(__hostGlobal(["process"]).removeListener("SIGTERM", stop));
|
|
562
|
+
__normalize(__hostGlobal(["process"]).removeListener("SIGINT", stop));
|
|
563
|
+
throw error;
|
|
564
|
+
return null;
|
|
565
|
+
}, [0], "closure"));
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
//# sourceMappingURL=server.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.mjs","sources":["server.lango"],"sourcesContent":["import \"./database\"\n prepareDatabase(settings)\nimport js \"node:http\" as http\nimport js \"node:path\" as path\nimport js \"node:module\" as modules\nimport js \"node:crypto\" as crypto\nimport js \"piscina\" as piscina\nimport js global process\nimport js global Promise\nimport js global Date\nimport js global Math\nimport js global Object\nimport js global Reflect\nimport js global AbortController\nimport js global setTimeout\nimport js global clearTimeout\nimport \"./options\"\n deployment(directory)\nimport \"./routes\"\n class Routes\nimport \"./body_reader\"\n class BodyReader\n\nexport main(args)\n\nexport class Server(path, definition = null)\n methods\n start()\n close()\n\nServer(path, definition = null)\n if path != null\n directory = path\n routes = new Routes(directory)\n routes.xml(pathJoin(directory, \"routes.xml\"))\n definition = {\n settings: deployment(directory)\n routes: routes.definitions()\n events: routes.events()\n }\n if definition == null\n throw \"Use Server(directory) or Builder.build()\"\n @definition = from_json(to_json(definition))\n @state = \"new\"\n @server = null\n @pool = null\n @starting = null\n @closing = null\n @waiting = []\n @active = 0\n @tasks = Object/create(null)\n @ready = Promise/withResolvers()\n @readyIds = []\n @destroying = false\n\npathJoin(directory, file)\n return path/join(directory, file)\n\nServer:start()\n if @state != \"new\"\n return Promise/reject(\"Server can only be started once\")\n @state = \"starting\"\n @starting = launch()\n return @starting\n\nServer:launch()\n settings = @definition.settings\n return chain Promise/resolve(null)\n then(ignored)\n capture path\n capture modules\n capture settings\n resolver = modules/createRequire(path/join(settings.root, \"package.json\"))\n entry = resolver.resolve(\"@lango-project/lango-server\")\n @pool = new piscina/Piscina({\n filename: path/join(path/dirname(entry), \"worker.mjs\")\n name: \"execute\"\n minThreads: settings.workers\n maxThreads: settings.workers\n maxQueue: settings.maxQueue\n concurrentTasksPerWorker: 1\n closeTimeout: settings.shutdownTimeoutMs\n workerData: @definition\n })\n pool = @pool\n pool.on(\"message\", (message)\n messageReceived(message)\n )\n pool.on(\"error\", (error)\n @ready.reject(error)\n context log\n log.err(\"Worker pool error\")\n )\n return startupDeadline(@ready.promise, settings.startupTimeoutMs)\n then(ignored)\n capture settings\n if @state != \"starting\"\n throw \"Server closed during startup\"\n server = new http/Server((req, res)\n receive(req, res)\n )\n @server = server\n server.headersTimeout = settings.bodyTimeoutMs\n server.requestTimeout = settings.bodyTimeoutMs\n listening = new Promise((resolve, reject)\n capture server\n capture settings\n server.once(\"error\", reject)\n server.listen(settings.port, settings.host, ()\n capture server\n capture resolve\n capture reject\n server.removeListener(\"error\", reject)\n if @state != \"starting\"\n reject(\"Server closed during startup\")\n return null\n @state = \"running\"\n address = server.address()\n resolve({\n host: address.address\n port: address.port\n })\n )\n )\n return listening\n catch(error)\n @state = \"closed\"\n return chain destroyPool()\n then(ignored)\n capture error\n throw error\n\nstartupDeadline(pending, timeout)\n expired = Promise/withResolvers()\n expire = ()\n capture expired\n expired.reject(\"Worker startup timed out\")\n timer = setTimeout(expire, timeout)\n return chain Promise/race([pending, expired.promise])\n finally\n capture timer\n clearTimeout(timer)\n\nServer:messageReceived(message)\n if message.kind == \"ready\"\n if !@readyIds.includes(message.threadId)\n @readyIds.add(message.threadId)\n if @readyIds.length >= @definition.settings.workers\n @ready.resolve(null)\n return null\n if !(message.id in @tasks)\n return null\n record = @tasks[message.id]\n if message.kind == \"prepared\"\n record.result = message.result\n resetDeadline(record, @definition.settings.eventTimeoutMs)\n if message.kind == \"event\"\n resetDeadline(record, @definition.settings.eventTimeoutMs)\n if message.kind == \"handler\" and record.result == null\n resetDeadline(record, record.deadline - Date/now())\n return null\n\nServer:resetDeadline(record, duration)\n clearTimeout(record.timer)\n if record.result == null\n duration = Math/min(duration, record.deadline - Date/now())\n expired = ()\n capture record\n expire(record)\n record.timer = setTimeout(expired, Math/max(1, duration))\n return null\n\nServer:receive(req, res)\n req.on(\"error\", (error)\n return null\n )\n if @state != \"running\"\n return rejectHttp(req, res, 503, \"Unavailable\")\n if req.method != \"POST\"\n res.setHeader(\"Allow\", \"POST\")\n return rejectHttp(req, res, 405, \"MethodNotAllowed\")\n if req.url.includes(\"?\") or req.url.includes(\"#\") or req.url.includes(\"\\\\\")\n return rejectHttp(req, res, 400, \"InvalidPath\")\n type = req.headers[\"content-type\"]\n media = \"\"\n if is_string(type)\n parts = type.split(\";\")\n media = parts[0].trim()\n if media.toLowerCase() != \"application/json\"\n return rejectHttp(req, res, 415, \"JsonRequired\")\n settings = @definition.settings\n if @active >= settings.workers and @waiting.length >= settings.maxQueue\n return rejectHttp(req, res, 503, \"QueueFull\")\n complete = (body)\n capture req\n capture res\n submit(req, res, body)\n failed = (status, code)\n capture res\n send(res, failure(status, code))\n reader = new BodyReader(req, res, settings, complete, failed)\n reader.start()\n return null\n\nServer:submit(req, res, body)\n settings = @definition.settings\n if @state != \"running\" or (@active >= settings.workers and @waiting.length >= settings.maxQueue)\n return send(res, failure(503, \"Unavailable\"))\n id = crypto/randomUUID()\n record = {\n id: id\n task: {\n body: body\n request: {\n id: id\n method: req.method\n path: req.url\n headers: req.headers\n remoteAddress: req.socket.remoteAddress\n }\n }\n completed: Promise/withResolvers()\n controller: new AbortController()\n started: false\n result: null\n expired: false\n deadline: Date/now() + settings.requestTimeoutMs\n timer: null\n }\n @tasks[id] = record\n resetDeadline(record, settings.requestTimeoutMs)\n @waiting.add(record)\n res.once(\"close\", ()\n capture res\n capture record\n if !res.writableEnded\n cancel(record)\n )\n pump()\n return chain record.completed.promise\n then(result)\n capture res\n send(res, result)\n return null\n catch(error)\n capture res\n capture record\n code = 500\n name = \"InternalServerError\"\n if record.expired\n code = 504\n name = \"RequestTimeout\"\n send(res, failure(code, name))\n return null\n\nServer:expire(record)\n record.expired = true\n cancel(record)\n return null\n\nServer:cancel(record)\n if !(record.id in @tasks)\n return null\n if record.started\n record.controller.abort()\n else\n index = @waiting.indexOf(record)\n if index >= 0\n @waiting.splice(index, 1)\n finish(record, null, \"Request cancelled\")\n return null\n\nServer:pump()\n loop while !@destroying and @waiting.length > 0 and @active < @definition.settings.workers\n record = @waiting.shift()\n record.started = true\n @active = @active + 1\n pending = @pool.run(record.task, {\n signal: record.controller.signal\n })\n settle(record, pending)\n return null\n\nServer:settle(record, pending)\n return chain pending\n then(result)\n capture record\n finish(record, result, null)\n return null\n catch(error)\n capture record\n if record.result != null\n context log\n log.err(\"[${record.id}] Request event interrupted; preserving prepared result\")\n finish(record, record.result, error)\n return null\n finally\n @active = @active - 1\n pump()\n\nServer:finish(record, result, error)\n clearTimeout(record.timer)\n Reflect/deleteProperty(@tasks, record.id)\n if result != null\n record.completed.resolve(result)\n else\n record.completed.reject(error)\n return null\n\nServer:close()\n if @closing != null\n return @closing\n previous = @state\n @state = \"closing\"\n @shutdownDeadline = Date/now() + @definition.settings.shutdownTimeoutMs\n if previous == \"new\" or previous == \"closed\"\n @state = \"closed\"\n @closing = Promise/resolve(null)\n return @closing\n @ready.reject(\"Server closed during startup\")\n @closing = stop()\n return @closing\n\nServer:stop()\n drained = new Promise((resolve)\n server = @server\n if server == null or !server.listening\n resolve(null)\n else\n server.close(()\n capture resolve\n resolve(null)\n )\n )\n expired = Promise/withResolvers()\n force = ()\n capture expired\n @destroying = true\n for @waiting as record\n finish(record, null, \"Server closed\")\n @waiting = []\n if @server != null\n @server.closeAllConnections()\n expired.reject(\"Server shutdown timed out\")\n timer = setTimeout(force, @definition.settings.shutdownTimeoutMs)\n return chain Promise/race([drained, expired.promise])\n finally\n capture timer\n clearTimeout(timer)\n return destroyPool()\n finally\n @state = \"closed\"\n\nServer:destroyPool()\n @destroying = true\n if @pool == null\n return Promise/resolve(null)\n pool = @pool\n pending = Promise/resolve(null)\n if @definition.settings.database != null and @state == \"closing\" and @active == 0\n remaining = @shutdownDeadline - Date/now()\n if remaining > 0\n tasks = []\n for range(@definition.settings.workers) as index\n tasks.add(pool.run({\n close: true\n }))\n pending = startupDeadline(Promise/all(tasks), remaining)\n return chain pending\n finally\n capture pool\n return pool.destroy()\n\nServer:send(res, result)\n if @state == \"closing\"\n result.headers.connection = \"close\"\n return write(res, result)\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\nwrite(res, result)\n if res.destroyed or res.writableEnded\n return null\n try\n res.writeHead(result.status, result.headers)\n res.end(result.body)\n catch error\n res.destroy()\n return null\n\nrejectHttp(req, res, status, code)\n res.setHeader(\"Connection\", \"close\")\n res.once(\"finish\", ()\n capture req\n req.destroy()\n )\n req.resume()\n return write(res, failure(status, code))\n\nmain(args)\n if args.length != 1\n throw \"Usage: lango-server <application-directory>\"\n server = new Server(args[0])\n stop = ()\n capture server\n return chain server.close()\n catch(error)\n context log\n log.err(\"Server shutdown failed\")\n process.exitCode = 1\n process.once(\"SIGTERM\", stop)\n process.once(\"SIGINT\", stop)\n return chain server.start()\n then(address)\n context log\n log.info(\"Listening on ${address.host}:${address.port}\")\n return null\n catch(error)\n capture stop\n process.removeListener(\"SIGTERM\", stop)\n process.removeListener(\"SIGINT\", stop)\n throw error\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA;;;;;;;;AA6EA;;;;;;;;;;;;;;;;;AAsPA;;;;;;;;AAYA;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|