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