@lango-project/lango-server 0.2.0 → 0.2.3

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.
Files changed (121) hide show
  1. package/README.md +290 -27
  2. package/dist/.lango/runtime.mjs +24 -1
  3. package/dist/body_reader.mjs +15 -14
  4. package/dist/body_reader.mjs.map +1 -1
  5. package/dist/cli/.lango/runtime.mjs +24 -1
  6. package/dist/cli/body_reader.mjs +15 -14
  7. package/dist/cli/body_reader.mjs.map +1 -1
  8. package/dist/cli/client_identity.mjs +250 -0
  9. package/dist/cli/client_identity.mjs.map +1 -0
  10. package/dist/cli/database.mjs +23 -8
  11. package/dist/cli/database.mjs.map +1 -1
  12. package/dist/cli/endpoint.mjs +11 -7
  13. package/dist/cli/endpoint.mjs.map +1 -1
  14. package/dist/cli/error_details.mjs +45 -0
  15. package/dist/cli/error_details.mjs.map +1 -0
  16. package/dist/cli/longpoll_broker.mjs +9 -8
  17. package/dist/cli/longpoll_broker.mjs.map +1 -1
  18. package/dist/cli/longpoll_options.mjs +8 -7
  19. package/dist/cli/longpoll_options.mjs.map +1 -1
  20. package/dist/cli/longpoll_session.mjs +11 -10
  21. package/dist/cli/longpoll_session.mjs.map +1 -1
  22. package/dist/cli/options.mjs +29 -3
  23. package/dist/cli/options.mjs.map +1 -1
  24. package/dist/cli/origin.mjs +87 -0
  25. package/dist/cli/origin.mjs.map +1 -0
  26. package/dist/cli/policy.mjs +121 -0
  27. package/dist/cli/policy.mjs.map +1 -0
  28. package/dist/cli/postgres_adapter.mjs +3 -2
  29. package/dist/cli/postgres_adapter.mjs.map +1 -1
  30. package/dist/cli/routes.mjs +33 -17
  31. package/dist/cli/routes.mjs.map +1 -1
  32. package/dist/cli/server.mjs +148 -101
  33. package/dist/cli/server.mjs.map +1 -1
  34. package/dist/cli/static_files.mjs +7 -4
  35. package/dist/cli/static_files.mjs.map +1 -1
  36. package/dist/cli/trace.mjs +53 -0
  37. package/dist/cli/trace.mjs.map +1 -0
  38. package/dist/client_identity.mjs +250 -0
  39. package/dist/client_identity.mjs.map +1 -0
  40. package/dist/database.mjs +23 -8
  41. package/dist/database.mjs.map +1 -1
  42. package/dist/endpoint.mjs +11 -7
  43. package/dist/endpoint.mjs.map +1 -1
  44. package/dist/error_details.mjs +45 -0
  45. package/dist/error_details.mjs.map +1 -0
  46. package/dist/group.mjs +34 -16
  47. package/dist/group.mjs.map +1 -1
  48. package/dist/http_failure.mjs +9 -2
  49. package/dist/http_failure.mjs.map +1 -1
  50. package/dist/longpoll_broker.mjs +9 -8
  51. package/dist/longpoll_broker.mjs.map +1 -1
  52. package/dist/longpoll_options.mjs +8 -7
  53. package/dist/longpoll_options.mjs.map +1 -1
  54. package/dist/longpoll_session.mjs +11 -10
  55. package/dist/longpoll_session.mjs.map +1 -1
  56. package/dist/options.mjs +29 -3
  57. package/dist/options.mjs.map +1 -1
  58. package/dist/origin.mjs +87 -0
  59. package/dist/origin.mjs.map +1 -0
  60. package/dist/policy.mjs +121 -0
  61. package/dist/policy.mjs.map +1 -0
  62. package/dist/postgres_adapter.mjs +3 -2
  63. package/dist/postgres_adapter.mjs.map +1 -1
  64. package/dist/postgres_worker.mjs +15 -14
  65. package/dist/postgres_worker.mjs.map +1 -1
  66. package/dist/request_identity.mjs +22 -0
  67. package/dist/request_identity.mjs.map +1 -0
  68. package/dist/response.mjs +1 -1
  69. package/dist/response.mjs.map +1 -1
  70. package/dist/route_info.mjs +1 -1
  71. package/dist/route_info.mjs.map +1 -1
  72. package/dist/routes.mjs +33 -17
  73. package/dist/routes.mjs.map +1 -1
  74. package/dist/runtime.mjs +241 -110
  75. package/dist/runtime.mjs.map +1 -1
  76. package/dist/server.mjs +148 -101
  77. package/dist/server.mjs.map +1 -1
  78. package/dist/server_context.mjs +21 -9
  79. package/dist/server_context.mjs.map +1 -1
  80. package/dist/static_files.mjs +7 -4
  81. package/dist/static_files.mjs.map +1 -1
  82. package/dist/token.mjs +34 -7
  83. package/dist/token.mjs.map +1 -1
  84. package/dist/tokens.mjs +11 -2
  85. package/dist/tokens.mjs.map +1 -1
  86. package/dist/trace.mjs +53 -0
  87. package/dist/trace.mjs.map +1 -0
  88. package/dist/worker.mjs +8 -6
  89. package/dist/worker.mjs.map +1 -1
  90. package/docs/security.md +420 -0
  91. package/examples/security/README.md +160 -0
  92. package/examples/security/deploy.xml +12 -0
  93. package/examples/security/handlers.lango +610 -0
  94. package/examples/security/package.json +19 -0
  95. package/examples/security/passwords.lango +61 -0
  96. package/examples/security/routes.xml +14 -0
  97. package/examples/security/schema.lango +91 -0
  98. package/examples/security/setup.lango +30 -0
  99. package/package.json +12 -6
  100. package/src/client_identity.lango +184 -0
  101. package/src/database.lango +15 -4
  102. package/src/endpoint.lango +12 -5
  103. package/src/error_details.lango +21 -0
  104. package/src/group.lango +25 -21
  105. package/src/http_failure.lango +9 -5
  106. package/src/longpoll_options.lango +11 -5
  107. package/src/options.lango +34 -1
  108. package/src/origin.lango +56 -0
  109. package/src/policy.lango +84 -0
  110. package/src/request_identity.lango +9 -0
  111. package/src/response.lango +1 -1
  112. package/src/route_info.lango +2 -0
  113. package/src/routes.lango +30 -15
  114. package/src/runtime.lango +149 -77
  115. package/src/server.lango +48 -7
  116. package/src/server_context.lango +10 -2
  117. package/src/static_files.lango +3 -0
  118. package/src/token.lango +26 -5
  119. package/src/tokens.lango +10 -3
  120. package/src/trace.lango +23 -0
  121. package/src/worker.lango +1 -0
@@ -1,7 +1,10 @@
1
1
  #!/usr/bin/env node
2
+ import { resolvePolicies } from "./policy.mjs";
3
+ import { trace, responseLog } from "./trace.mjs";
2
4
  import { LongpollBroker } from "./longpoll_broker.mjs";
3
5
  import { LongpollSession } from "./longpoll_session.mjs";
4
6
  import { prepareDatabase } from "./database.mjs";
7
+ import { errorDetails } from "./error_details.mjs";
5
8
  import { deployment } from "./options.mjs";
6
9
  import { Routes } from "./routes.mjs";
7
10
  import { BodyReader } from "./body_reader.mjs";
@@ -17,6 +20,7 @@ import {
17
20
  contextGet as __contextGet,
18
21
  contextSet as __contextSet,
19
22
  contextRestore as __contextRestore,
23
+ bindContexts as __bindContexts,
20
24
  fromJson as __fromJson,
21
25
  toJson as __toJson,
22
26
  isNumber as __isNumber,
@@ -71,6 +75,8 @@ export class Server {
71
75
  throw "Use Server(directory) or Builder.build()";
72
76
  }
73
77
  this.#definition = __fromJson(__toJson(definition));
78
+ let settings = __normalize(this.#definition.settings);
79
+ resolvePolicies(__normalize(this.#definition.routes), __normalize(settings.policies));
74
80
  this.#broker = new LongpollBroker(__normalize(this.#definition.settings), __normalize(this.#definition.routes));
75
81
  this.#polls = __normalize(__hostGlobal(["Object"]).create(null));
76
82
  this.#state = "new";
@@ -119,6 +125,7 @@ export class Server {
119
125
  }
120
126
  #launch() {
121
127
  let settings = __normalize(this.#definition.settings);
128
+ trace(`Server starting: ${__normalize(settings.root)}; workers=${__normalize(settings.workers)}`);
122
129
  if (__requireBoolean(__normalize(settings.static) !== null)) {
123
130
  try {
124
131
  this.#static = new StaticFiles(__normalize(settings.static));
@@ -127,58 +134,58 @@ export class Server {
127
134
  return __normalize(__hostGlobal(["Promise"]).reject(error));
128
135
  }
129
136
  }
130
- return __normalize(this.#broker.open()).then(__withArity((ignored) => {
137
+ return __normalize(this.#broker.open()).then(__withArity(__bindContexts((ignored) => {
131
138
  ignored = __boundary(ignored);
132
- return prepareDatabase(__normalize(settings.database));
139
+ return prepareDatabase(__normalize(settings.database), __normalize(settings.root), __normalize(settings.client));
133
140
  return null;
134
- }, [0], "closure")).then(__withArity((ignored) => {
141
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
135
142
  ignored = __boundary(ignored);
136
143
  let resolver = __normalize(modules.createRequire(__normalize(path.join(__normalize(settings.root), "package.json"))));
137
144
  let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
138
145
  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
146
  let pool = __normalize(this.#pool);
140
- __normalize(pool.on("message", __withArity((message) => {
147
+ __normalize(pool.on("message", __withArity(__bindContexts((message) => {
141
148
  message = __boundary(message);
142
149
  this.#messageReceived(message);
143
150
  return null;
144
- }, [0], "closure")));
145
- __normalize(pool.on("error", __withArity((error) => {
151
+ }), [0], "closure")));
152
+ __normalize(pool.on("error", __withArity(__bindContexts((error) => {
146
153
  const __contextScope = [];
147
154
  try {
148
155
  error = __boundary(error);
149
156
  __normalize(this.#ready.reject(error));
150
- __normalize(__contextGet("log").err("Worker pool error"));
157
+ __normalize(__contextGet("log").err(`Worker pool error: ${errorDetails(error)}`));
151
158
  return null;
152
159
  } finally {
153
160
  __contextRestore(__contextScope);
154
161
  }
155
- }, [0], "closure")));
162
+ }), [0], "closure")));
156
163
  return startupDeadline(__normalize(this.#ready.promise), __normalize(settings.startupTimeoutMs));
157
164
  return null;
158
- }, [0], "closure")).then(__withArity((ignored) => {
165
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
159
166
  ignored = __boundary(ignored);
160
167
  if (__requireBoolean(__normalize(this.#state) !== "starting")) {
161
168
  throw "Server closed during startup";
162
169
  }
163
- let server = new http.Server(__withArity((req, res) => {
170
+ let server = new http.Server(__withArity(__bindContexts((req, res) => {
164
171
  req = __boundary(req);
165
172
  res = __boundary(res);
166
173
  this.#receive(req, res);
167
174
  return null;
168
- }, [0, 1], "closure"));
175
+ }), [0, 1], "closure"));
169
176
  this.#server = server;
170
- __normalize(server.on("connection", __withArity((socket) => {
177
+ __normalize(server.on("connection", __withArity(__bindContexts((socket) => {
171
178
  socket = __boundary(socket);
172
179
  this.#connected(socket);
173
180
  return null;
174
- }, [0], "closure")));
181
+ }), [0], "closure")));
175
182
  server.headersTimeout = __normalize(settings.bodyTimeoutMs);
176
183
  server.requestTimeout = __normalize(settings.bodyTimeoutMs);
177
- let listening = new (__hostGlobal(["Promise"]))(__withArity((resolve, reject) => {
184
+ let listening = new (__hostGlobal(["Promise"]))(__withArity(__bindContexts((resolve, reject) => {
178
185
  resolve = __boundary(resolve);
179
186
  reject = __boundary(reject);
180
187
  __normalize(server.once("error", reject));
181
- __normalize(server.listen(__normalize(settings.port), __normalize(settings.host), () => {
188
+ __normalize(server.listen(__normalize(settings.port), __normalize(settings.host), __bindContexts(() => {
182
189
  __normalize(server.removeListener("error", reject));
183
190
  if (__requireBoolean(__normalize(this.#state) !== "starting")) {
184
191
  __call(reject, "Server closed during startup");
@@ -188,22 +195,22 @@ export class Server {
188
195
  let address = __normalize(server.address());
189
196
  __call(resolve, { host: __normalize(address.address), port: __normalize(address.port) });
190
197
  return null;
191
- }));
198
+ })));
192
199
  return null;
193
- }, [0, 1], "closure"));
200
+ }), [0, 1], "closure"));
194
201
  return listening;
195
202
  return null;
196
- }, [0], "closure")).catch(__withArity((error) => {
203
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
197
204
  error = __boundary(error);
198
205
  __normalize(this.#broker.close());
199
206
  this.#state = "closed";
200
- return this.#destroyPool().then(__withArity((ignored) => {
207
+ return this.#destroyPool().then(__withArity(__bindContexts((ignored) => {
201
208
  ignored = __boundary(ignored);
202
209
  throw error;
203
210
  return null;
204
- }, [0], "closure"));
211
+ }), [0], "closure"));
205
212
  return null;
206
- }, [0], "closure"));
213
+ }), [0], "closure"));
207
214
  return null;
208
215
  }
209
216
  #messageReceived(message) {
@@ -228,6 +235,7 @@ export class Server {
228
235
  let record = __normalize(this.#tasks[__normalize(message.id)]);
229
236
  if (__requireBoolean(__normalize(message.kind) === "prepared")) {
230
237
  record.result = __normalize(message.result);
238
+ record.logIdentity = __normalize(message.logIdentity);
231
239
  this.#resetDeadline(record, __normalize(this.#definition.settings.eventTimeoutMs));
232
240
  }
233
241
  if (__requireBoolean(__normalize(message.kind) === "event")) {
@@ -247,10 +255,10 @@ export class Server {
247
255
  if (__requireBoolean(__normalize(record.result) === null)) {
248
256
  duration = __normalize(__hostGlobal(["Math"]).min(duration, __finite(__normalize(record.deadline) - __normalize(__hostGlobal(["Date"]).now()))));
249
257
  }
250
- let expired = () => {
258
+ let expired = __bindContexts(() => {
251
259
  this.#expire(record);
252
260
  return null;
253
- };
261
+ });
254
262
  record.timer = __hostGlobalCall(["setTimeout"], expired, __normalize(__hostGlobal(["Math"]).max(1, duration)));
255
263
  return null;
256
264
  return null;
@@ -260,13 +268,13 @@ export class Server {
260
268
  socket = __boundary(socket);
261
269
  let record = { socket: socket, active: 0 };
262
270
  __add(this.#connections, record);
263
- __normalize(socket.once("close", () => {
271
+ __normalize(socket.once("close", __bindContexts(() => {
264
272
  let index = __normalize(this.#connections.indexOf(record));
265
273
  if (__requireBoolean(index >= 0)) {
266
274
  __normalize(this.#connections.splice(index, 1));
267
275
  }
268
276
  return null;
269
- }));
277
+ })));
270
278
  return null;
271
279
  return null;
272
280
  }
@@ -274,22 +282,28 @@ export class Server {
274
282
  __requireArity(arguments.length, [0, 1], "trackResponse");
275
283
  req = __boundary(req);
276
284
  res = __boundary(res);
285
+ let target = __normalize(__normalize(req.url.split("?"))[0]);
277
286
  for (const record of __normalize(this.#connections)) {
278
287
  if (__requireBoolean(__normalize(record.socket) === __normalize(req.socket))) {
279
288
  record.active = __numericAdd(__normalize(record.active), 1);
280
289
  let state = { done: false };
281
- let complete = () => {
290
+ let complete = __bindContexts(() => {
282
291
  if (__requireBoolean(__normalize(state.done))) {
283
292
  return null;
284
293
  }
285
294
  state.done = true;
295
+ if (__requireBoolean(__normalize(res.writableFinished))) {
296
+ responseLog(__normalize(res.langoRequestId), __normalize(req.method), target, __normalize(res.statusCode));
297
+ } else {
298
+ trace(`HTTP ${__normalize(req.method)} ${target} connection closed before response completed`);
299
+ }
286
300
  record.active = __finite(__normalize(record.active) - 1);
287
301
  if (__requireBoolean(__requireBoolean(__normalize(this.#state) === "closing") && __requireBoolean(__normalize(record.active) === 0))) {
288
302
  __normalize(record.socket.end());
289
303
  }
290
304
  return null;
291
305
  return null;
292
- };
306
+ });
293
307
  __normalize(res.once("finish", complete));
294
308
  __normalize(res.once("close", complete));
295
309
  return null;
@@ -303,21 +317,22 @@ export class Server {
303
317
  req = __boundary(req);
304
318
  res = __boundary(res);
305
319
  this.#trackResponse(req, res);
306
- __normalize(req.on("error", __withArity((error) => {
320
+ __normalize(req.on("error", __withArity(__bindContexts((error) => {
307
321
  error = __boundary(error);
308
322
  return null;
309
323
  return null;
310
- }, [0], "closure")));
324
+ }), [0], "closure")));
311
325
  if (__requireBoolean(__normalize(this.#state) !== "running")) {
312
326
  return rejectHttp(req, res, 503, "Unavailable");
313
327
  }
328
+ trace(`HTTP ${__normalize(req.method)} ${__normalize(__normalize(req.url.split("?"))[0])}`);
314
329
  if (__requireBoolean(__requireBoolean(__normalize(this.#static) !== null) && __requireBoolean(__requireBoolean(__normalize(req.method) === "GET") || __requireBoolean(__normalize(req.method) === "HEAD")))) {
315
- let failed = __withArity((status, code) => {
330
+ let failed = __withArity(__bindContexts((status, code) => {
316
331
  status = __boundary(status);
317
332
  code = __boundary(code);
318
333
  this.#send(res, failure(status, code));
319
334
  return null;
320
- }, [0, 1], "closure");
335
+ }), [0, 1], "closure");
321
336
  return __normalize(this.#static.receive(req, res, failed));
322
337
  }
323
338
  if (__requireBoolean(__normalize(req.method) !== "POST")) {
@@ -344,17 +359,17 @@ export class Server {
344
359
  if (__requireBoolean(__requireBoolean(__normalize(this.#active) >= __normalize(settings.workers)) && __requireBoolean(__normalize(this.#waiting.length) >= __normalize(settings.maxQueue)))) {
345
360
  return rejectHttp(req, res, 503, "QueueFull");
346
361
  }
347
- let complete = __withArity((body) => {
362
+ let complete = __withArity(__bindContexts((body) => {
348
363
  body = __boundary(body);
349
364
  this.#submit(req, res, body);
350
365
  return null;
351
- }, [0], "closure");
352
- let failed = __withArity((status, code) => {
366
+ }), [0], "closure");
367
+ let failed = __withArity(__bindContexts((status, code) => {
353
368
  status = __boundary(status);
354
369
  code = __boundary(code);
355
370
  this.#send(res, failure(status, code));
356
371
  return null;
357
- }, [0, 1], "closure");
372
+ }), [0, 1], "closure");
358
373
  let reader = new BodyReader(req, res, settings, complete, failed);
359
374
  __normalize(reader.start());
360
375
  return null;
@@ -372,17 +387,23 @@ export class Server {
372
387
  } else {
373
388
  pending = this.#longpoll(req, res, body, route);
374
389
  }
375
- return pending.then(__withArity((result) => {
390
+ return pending.then(__withArity(__bindContexts((result) => {
376
391
  result = __boundary(result);
377
392
  this.#send(res, result);
378
393
  return null;
379
394
  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"));
395
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
396
+ const __contextScope = [];
397
+ try {
398
+ error = __boundary(error);
399
+ __normalize(__contextGet("log").err(`HTTP ${__normalize(req.method)} ${__normalize(__normalize(req.url.split("?"))[0])} request failed: ${errorDetails(error)}`));
400
+ this.#send(res, failure(500, "InternalServerError"));
401
+ return null;
402
+ return null;
403
+ } finally {
404
+ __contextRestore(__contextScope);
405
+ }
406
+ }), [0], "closure"));
386
407
  return null;
387
408
  }
388
409
  #longpoll(req, res, body, route) {
@@ -394,25 +415,25 @@ export class Server {
394
415
  if (__requireBoolean(__normalize(__normalize(__hostGlobal(["Object"]).keys(__normalize(this.#polls))).length) >= __normalize(this.#definition.settings.redis.maxPendingPolls))) {
395
416
  return __normalize(__hostGlobal(["Promise"]).resolve(failure(503, "PollLimit")));
396
417
  }
397
- let authorize = __withArity((token, result) => {
418
+ let authorize = __withArity(__bindContexts((token, result) => {
398
419
  token = __boundary(token);
399
420
  result = __boundary(result);
400
421
  return this.#dispatch(req, res, body, true, token, result);
401
422
  return null;
402
- }, [0, 1], "closure");
423
+ }), [0, 1], "closure");
403
424
  let session = new LongpollSession(__normalize(this.#broker), route, authorize, body);
404
425
  let id = __normalize(crypto.randomUUID());
405
426
  this.#polls[id] = session;
406
- let disconnected = () => {
427
+ let disconnected = __bindContexts(() => {
407
428
  __normalize(session.cancel());
408
429
  return null;
409
- };
430
+ });
410
431
  __normalize(res.once("close", disconnected));
411
- return __normalize(session.run()).finally(() => {
432
+ return __normalize(session.run()).finally(__bindContexts(() => {
412
433
  __normalize(res.removeListener("close", disconnected));
413
434
  __normalize(__hostGlobal(["Reflect"]).deleteProperty(__normalize(this.#polls), id));
414
435
  return null;
415
- });
436
+ }));
416
437
  return null;
417
438
  }
418
439
  #brokerMessage(message) {
@@ -431,18 +452,18 @@ export class Server {
431
452
  } catch (error) {
432
453
  pending = __normalize(__hostGlobal(["Promise"]).reject(error));
433
454
  }
434
- return pending.then(__withArity((value) => {
455
+ return pending.then(__withArity(__bindContexts((value) => {
435
456
  value = __boundary(value);
436
457
  brokerReply(port, { ok: true, value: value });
437
458
  return null;
438
- }, [0], "closure")).catch(__withArity((error) => {
459
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
439
460
  error = __boundary(error);
440
461
  brokerReply(port, { ok: false });
441
462
  return null;
442
- }, [0], "closure")).finally(() => {
463
+ }), [0], "closure")).finally(__bindContexts(() => {
443
464
  __normalize(port.close());
444
465
  return null;
445
- });
466
+ }));
446
467
  return null;
447
468
  }
448
469
  #dispatch(req, res, body, authorize = false, token = null, pollResult = null) {
@@ -458,6 +479,8 @@ export class Server {
458
479
  return __normalize(__hostGlobal(["Promise"]).resolve(failure(503, "Unavailable")));
459
480
  }
460
481
  let id = __normalize(crypto.randomUUID());
482
+ res.langoRequestId = id;
483
+ trace(`[${id}] Queued request ${__normalize(req.method)} ${__normalize(req.url)}`);
461
484
  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
485
  if (__requireBoolean(token !== null)) {
463
486
  record.task.request.headers.authorization = `Bearer ${token}`;
@@ -465,25 +488,25 @@ export class Server {
465
488
  this.#tasks[id] = record;
466
489
  this.#resetDeadline(record, __normalize(settings.requestTimeoutMs));
467
490
  __add(this.#waiting, record);
468
- let disconnected = () => {
491
+ let disconnected = __bindContexts(() => {
469
492
  if (__requireBoolean(!__requireBoolean(__normalize(res.writableEnded)))) {
470
493
  this.#cancel(record);
471
494
  }
472
495
  return null;
473
- };
496
+ });
474
497
  __normalize(res.once("close", disconnected));
475
498
  this.#pump();
476
- return record.completed.promise.catch(__withArity((error) => {
499
+ return record.completed.promise.catch(__withArity(__bindContexts((error) => {
477
500
  error = __boundary(error);
478
501
  if (__requireBoolean(__normalize(record.expired))) {
479
502
  return failure(504, "RequestTimeout");
480
503
  }
481
504
  return failure(500, "InternalServerError");
482
505
  return null;
483
- }, [0], "closure")).finally(() => {
506
+ }), [0], "closure")).finally(__bindContexts(() => {
484
507
  __normalize(res.removeListener("close", disconnected));
485
508
  return null;
486
- });
509
+ }));
487
510
  return null;
488
511
  }
489
512
  #expire(record) {
@@ -517,6 +540,7 @@ export class Server {
517
540
  let record = __normalize(this.#waiting.shift());
518
541
  record.started = true;
519
542
  this.#active = __numericAdd(__normalize(this.#active), 1);
543
+ trace(`[${__normalize(record.id)}] Dispatching to worker`);
520
544
  let pending = __normalize(this.#pool.run(__normalize(record.task), { signal: record.controller.signal }));
521
545
  this.#settle(record, pending);
522
546
  }
@@ -527,17 +551,17 @@ export class Server {
527
551
  __requireArity(arguments.length, [0, 1], "settle");
528
552
  record = __boundary(record);
529
553
  pending = __boundary(pending);
530
- return pending.then(__withArity((result) => {
554
+ return pending.then(__withArity(__bindContexts((result) => {
531
555
  result = __boundary(result);
532
556
  this.#finish(record, result, null);
533
557
  return null;
534
558
  return null;
535
- }, [0], "closure")).catch(__withArity((error) => {
559
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
536
560
  const __contextScope = [];
537
561
  try {
538
562
  error = __boundary(error);
539
563
  if (__requireBoolean(__normalize(record.result) !== null)) {
540
- __normalize(__contextGet("log").err(`[${__normalize(record.id)}] Request event interrupted; preserving prepared result`));
564
+ __normalize(__contextGet("log").err(`${__normalize(record.logIdentity)} Request event interrupted; preserving prepared result`));
541
565
  }
542
566
  this.#finish(record, __normalize(record.result), error);
543
567
  return null;
@@ -545,11 +569,11 @@ export class Server {
545
569
  } finally {
546
570
  __contextRestore(__contextScope);
547
571
  }
548
- }, [0], "closure")).finally(() => {
572
+ }), [0], "closure")).finally(__bindContexts(() => {
549
573
  this.#active = __finite(__normalize(this.#active) - 1);
550
574
  this.#pump();
551
575
  return null;
552
- });
576
+ }));
553
577
  return null;
554
578
  }
555
579
  #finish(record, result, error) {
@@ -593,21 +617,21 @@ export class Server {
593
617
  for (const [id, session] of __entries(__normalize(this.#polls))) {
594
618
  __normalize(session.cancel());
595
619
  }
596
- let drained = new (__hostGlobal(["Promise"]))(__withArity((resolve) => {
620
+ let drained = new (__hostGlobal(["Promise"]))(__withArity(__bindContexts((resolve) => {
597
621
  resolve = __boundary(resolve);
598
622
  let server = __normalize(this.#server);
599
623
  if (__requireBoolean(__requireBoolean(server === null) || __requireBoolean(!__requireBoolean(__normalize(server.listening))))) {
600
624
  __call(resolve, null);
601
625
  } else {
602
- server.close(() => {
626
+ server.close(__bindContexts(() => {
603
627
  __call(resolve, null);
604
628
  return null;
605
- });
629
+ }));
606
630
  }
607
631
  return null;
608
- }, [0], "closure"));
632
+ }), [0], "closure"));
609
633
  let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
610
- let force = () => {
634
+ let force = __bindContexts(() => {
611
635
  this.#destroying = true;
612
636
  for (const record of __normalize(this.#waiting)) {
613
637
  this.#finish(record, null, "Server closed");
@@ -618,17 +642,17 @@ export class Server {
618
642
  }
619
643
  __normalize(expired.reject("Server shutdown timed out"));
620
644
  return null;
621
- };
645
+ });
622
646
  let timer = __hostGlobalCall(["setTimeout"], force, __normalize(this.#definition.settings.shutdownTimeoutMs));
623
- return __normalize(__hostGlobal(["Promise"]).race([drained, __normalize(expired.promise)])).finally(() => {
647
+ return __normalize(__hostGlobal(["Promise"]).race([drained, __normalize(expired.promise)])).finally(__bindContexts(() => {
624
648
  __hostGlobalCall(["clearTimeout"], timer);
625
649
  return this.#destroyPool();
626
650
  return null;
627
- }).finally(() => {
651
+ })).finally(__bindContexts(() => {
628
652
  __normalize(this.#broker.close());
629
653
  this.#state = "closed";
630
654
  return null;
631
- });
655
+ }));
632
656
  return null;
633
657
  }
634
658
  #destroyPool() {
@@ -648,16 +672,17 @@ export class Server {
648
672
  pending = startupDeadline(__normalize(__hostGlobal(["Promise"]).all(tasks)), remaining);
649
673
  }
650
674
  }
651
- return pending.finally(() => {
675
+ return pending.finally(__bindContexts(() => {
652
676
  return __normalize(pool.destroy());
653
677
  return null;
654
- });
678
+ }));
655
679
  return null;
656
680
  }
657
681
  #send(res, result) {
658
682
  __requireArity(arguments.length, [0, 1], "send");
659
683
  res = __boundary(res);
660
684
  result = __boundary(result);
685
+ trace(`HTTP response status=${__normalize(result.status)}`);
661
686
  if (__requireBoolean(__normalize(this.#state) === "closing")) {
662
687
  result.headers.connection = "close";
663
688
  }
@@ -678,15 +703,15 @@ function startupDeadline(pending, timeout) {
678
703
  pending = __boundary(pending);
679
704
  timeout = __boundary(timeout);
680
705
  let expired = __normalize(__hostGlobal(["Promise"]).withResolvers());
681
- let expire = () => {
706
+ let expire = __bindContexts(() => {
682
707
  __normalize(expired.reject("Worker startup timed out"));
683
708
  return null;
684
- };
709
+ });
685
710
  let timer = __hostGlobalCall(["setTimeout"], expire, timeout);
686
- return __normalize(__hostGlobal(["Promise"]).race([pending, __normalize(expired.promise)])).finally(() => {
711
+ return __normalize(__hostGlobal(["Promise"]).race([pending, __normalize(expired.promise)])).finally(__bindContexts(() => {
687
712
  __hostGlobalCall(["clearTimeout"], timer);
688
713
  return null;
689
- });
714
+ }));
690
715
  return null;
691
716
  }
692
717
 
@@ -699,20 +724,26 @@ function failure(status, code) {
699
724
  }
700
725
 
701
726
  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
- }
727
+ const __contextScope = [];
708
728
  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());
729
+ __requireArity(arguments.length, [0, 1], "write");
730
+ res = __boundary(res);
731
+ result = __boundary(result);
732
+ if (__requireBoolean(__requireBoolean(__normalize(res.destroyed)) || __requireBoolean(__normalize(res.writableEnded)))) {
733
+ return null;
734
+ }
735
+ try {
736
+ __normalize(res.writeHead(__normalize(result.status), __normalize(result.headers)));
737
+ __normalize(res.end(__normalize(result.body)));
738
+ } catch (error) {
739
+ __normalize(__contextGet("log").err(`[${__normalize(res.langoRequestId)}] Response write failed: ${errorDetails(error)}`));
740
+ __normalize(res.destroy());
741
+ }
742
+ return null;
743
+ return null;
744
+ } finally {
745
+ __contextRestore(__contextScope);
713
746
  }
714
- return null;
715
- return null;
716
747
  }
717
748
 
718
749
  function rejectHttp(req, res, status, code) {
@@ -722,10 +753,10 @@ function rejectHttp(req, res, status, code) {
722
753
  status = __boundary(status);
723
754
  code = __boundary(code);
724
755
  __normalize(res.setHeader("Connection", "close"));
725
- __normalize(res.once("finish", () => {
756
+ __normalize(res.once("finish", __bindContexts(() => {
726
757
  __normalize(req.destroy());
727
758
  return null;
728
- }));
759
+ })));
729
760
  __normalize(req.resume());
730
761
  return write(res, failure(status, code));
731
762
  return null;
@@ -734,12 +765,28 @@ function rejectHttp(req, res, status, code) {
734
765
  export function main(args) {
735
766
  __requireArity(arguments.length, [0], "main");
736
767
  args = __boundary(args);
737
- if (__requireBoolean(__normalize(args.length) !== 1)) {
738
- throw "Usage: lango-server <application-directory>";
768
+ let directory = null;
769
+ let verbose = false;
770
+ for (const arg of args) {
771
+ if (__requireBoolean(arg === "--verbose")) {
772
+ verbose = true;
773
+ } else {
774
+ if (__requireBoolean(__requireBoolean(__normalize(arg.startsWith("--"))) || __requireBoolean(directory !== null))) {
775
+ throw "Usage: lango-server <application-directory> [--verbose]";
776
+ }
777
+ directory = arg;
778
+ }
739
779
  }
740
- let server = new Server(__normalize(args[0]));
741
- let stop = () => {
742
- return __normalize(server.close()).catch(__withArity((error) => {
780
+ if (__requireBoolean(directory === null)) {
781
+ throw "Usage: lango-server <application-directory> [--verbose]";
782
+ }
783
+ if (__requireBoolean(verbose)) {
784
+ __hostGlobal(["process"]).env.LANGO_VERBOSE = "1";
785
+ }
786
+ trace(`Loading server configuration: ${directory}`);
787
+ let server = new Server(directory);
788
+ let stop = __bindContexts(() => {
789
+ return __normalize(server.close()).catch(__withArity(__bindContexts((error) => {
743
790
  const __contextScope = [];
744
791
  try {
745
792
  error = __boundary(error);
@@ -749,12 +796,12 @@ export function main(args) {
749
796
  } finally {
750
797
  __contextRestore(__contextScope);
751
798
  }
752
- }, [0], "closure"));
799
+ }), [0], "closure"));
753
800
  return null;
754
- };
801
+ });
755
802
  __normalize(__hostGlobal(["process"]).once("SIGTERM", stop));
756
803
  __normalize(__hostGlobal(["process"]).once("SIGINT", stop));
757
- return __normalize(server.start()).then(__withArity((address) => {
804
+ return __normalize(server.start()).then(__withArity(__bindContexts((address) => {
758
805
  const __contextScope = [];
759
806
  try {
760
807
  address = __boundary(address);
@@ -764,13 +811,13 @@ export function main(args) {
764
811
  } finally {
765
812
  __contextRestore(__contextScope);
766
813
  }
767
- }, [0], "closure")).catch(__withArity((error) => {
814
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
768
815
  error = __boundary(error);
769
816
  __normalize(__hostGlobal(["process"]).removeListener("SIGTERM", stop));
770
817
  __normalize(__hostGlobal(["process"]).removeListener("SIGINT", stop));
771
818
  throw error;
772
819
  return null;
773
- }, [0], "closure"));
820
+ }), [0], "closure"));
774
821
  return null;
775
822
  }
776
823