@lango-project/lango-server 0.2.2 → 0.2.4

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 (158) hide show
  1. package/README.md +252 -29
  2. package/dist/.lango/runtime.mjs +16 -0
  3. package/dist/body_reader.mjs +15 -14
  4. package/dist/body_reader.mjs.map +1 -1
  5. package/dist/builder.mjs +26 -2
  6. package/dist/builder.mjs.map +1 -1
  7. package/dist/cli/.lango/runtime.mjs +16 -0
  8. package/dist/cli/body_reader.mjs +15 -14
  9. package/dist/cli/body_reader.mjs.map +1 -1
  10. package/dist/cli/client_identity.mjs +250 -0
  11. package/dist/cli/client_identity.mjs.map +1 -0
  12. package/dist/cli/database.mjs +23 -8
  13. package/dist/cli/database.mjs.map +1 -1
  14. package/dist/cli/database_scope.mjs +11 -0
  15. package/dist/cli/database_scope.mjs.map +1 -1
  16. package/dist/cli/endpoint.mjs +11 -7
  17. package/dist/cli/endpoint.mjs.map +1 -1
  18. package/dist/cli/longpoll_broker.mjs +9 -8
  19. package/dist/cli/longpoll_broker.mjs.map +1 -1
  20. package/dist/cli/longpoll_options.mjs +8 -7
  21. package/dist/cli/longpoll_options.mjs.map +1 -1
  22. package/dist/cli/longpoll_session.mjs +11 -10
  23. package/dist/cli/longpoll_session.mjs.map +1 -1
  24. package/dist/cli/middleware.mjs +1 -1
  25. package/dist/cli/middleware.mjs.map +1 -1
  26. package/dist/cli/options.mjs +41 -4
  27. package/dist/cli/options.mjs.map +1 -1
  28. package/dist/cli/origin.mjs +87 -0
  29. package/dist/cli/origin.mjs.map +1 -0
  30. package/dist/cli/policy.mjs +121 -0
  31. package/dist/cli/policy.mjs.map +1 -0
  32. package/dist/cli/postgres_adapter.mjs +15 -3
  33. package/dist/cli/postgres_adapter.mjs.map +1 -1
  34. package/dist/cli/routes.mjs +69 -18
  35. package/dist/cli/routes.mjs.map +1 -1
  36. package/dist/cli/server.mjs +88 -81
  37. package/dist/cli/server.mjs.map +1 -1
  38. package/dist/cli/static_files.mjs +5 -4
  39. package/dist/cli/static_files.mjs.map +1 -1
  40. package/dist/client_identity.mjs +250 -0
  41. package/dist/client_identity.mjs.map +1 -0
  42. package/dist/database.mjs +23 -8
  43. package/dist/database.mjs.map +1 -1
  44. package/dist/database_action.mjs +71 -0
  45. package/dist/database_action.mjs.map +1 -0
  46. package/dist/database_scope.mjs +11 -0
  47. package/dist/database_scope.mjs.map +1 -1
  48. package/dist/endpoint.mjs +11 -7
  49. package/dist/endpoint.mjs.map +1 -1
  50. package/dist/execution_failure.mjs +30 -0
  51. package/dist/execution_failure.mjs.map +1 -0
  52. package/dist/execution_identity.mjs +103 -0
  53. package/dist/execution_identity.mjs.map +1 -0
  54. package/dist/execution_log.mjs +47 -0
  55. package/dist/execution_log.mjs.map +1 -0
  56. package/dist/execution_runtime.mjs +201 -0
  57. package/dist/execution_runtime.mjs.map +1 -0
  58. package/dist/execution_worker.mjs +41 -0
  59. package/dist/execution_worker.mjs.map +1 -0
  60. package/dist/executor.mjs +384 -0
  61. package/dist/executor.mjs.map +1 -0
  62. package/dist/group.mjs +49 -16
  63. package/dist/group.mjs.map +1 -1
  64. package/dist/http_failure.mjs +9 -2
  65. package/dist/http_failure.mjs.map +1 -1
  66. package/dist/job_configuration.mjs +37 -0
  67. package/dist/job_configuration.mjs.map +1 -0
  68. package/dist/job_context.mjs +61 -0
  69. package/dist/job_context.mjs.map +1 -0
  70. package/dist/library.mjs +31 -0
  71. package/dist/library.mjs.map +1 -1
  72. package/dist/longpoll_broker.mjs +9 -8
  73. package/dist/longpoll_broker.mjs.map +1 -1
  74. package/dist/longpoll_options.mjs +8 -7
  75. package/dist/longpoll_options.mjs.map +1 -1
  76. package/dist/longpoll_session.mjs +11 -10
  77. package/dist/longpoll_session.mjs.map +1 -1
  78. package/dist/middleware.mjs +1 -1
  79. package/dist/middleware.mjs.map +1 -1
  80. package/dist/module_loader.mjs +44 -0
  81. package/dist/module_loader.mjs.map +1 -0
  82. package/dist/options.mjs +41 -4
  83. package/dist/options.mjs.map +1 -1
  84. package/dist/origin.mjs +87 -0
  85. package/dist/origin.mjs.map +1 -0
  86. package/dist/policy.mjs +121 -0
  87. package/dist/policy.mjs.map +1 -0
  88. package/dist/postgres_adapter.mjs +15 -3
  89. package/dist/postgres_adapter.mjs.map +1 -1
  90. package/dist/postgres_worker.mjs +82 -19
  91. package/dist/postgres_worker.mjs.map +1 -1
  92. package/dist/queue_provider.mjs +52 -0
  93. package/dist/queue_provider.mjs.map +1 -0
  94. package/dist/response.mjs +1 -1
  95. package/dist/response.mjs.map +1 -1
  96. package/dist/route_info.mjs +1 -1
  97. package/dist/route_info.mjs.map +1 -1
  98. package/dist/routes.mjs +69 -18
  99. package/dist/routes.mjs.map +1 -1
  100. package/dist/runtime.mjs +287 -132
  101. package/dist/runtime.mjs.map +1 -1
  102. package/dist/server.mjs +88 -81
  103. package/dist/server.mjs.map +1 -1
  104. package/dist/server_context.mjs +21 -9
  105. package/dist/server_context.mjs.map +1 -1
  106. package/dist/static_files.mjs +5 -4
  107. package/dist/static_files.mjs.map +1 -1
  108. package/dist/token.mjs +34 -7
  109. package/dist/token.mjs.map +1 -1
  110. package/dist/tokens.mjs +11 -2
  111. package/dist/tokens.mjs.map +1 -1
  112. package/dist/worker.mjs +7 -6
  113. package/dist/worker.mjs.map +1 -1
  114. package/docs/jobs.md +123 -0
  115. package/docs/security.md +420 -0
  116. package/examples/security/README.md +160 -0
  117. package/examples/security/deploy.xml +12 -0
  118. package/examples/security/handlers.lango +610 -0
  119. package/examples/security/package.json +19 -0
  120. package/examples/security/passwords.lango +61 -0
  121. package/examples/security/routes.xml +14 -0
  122. package/examples/security/schema.lango +91 -0
  123. package/examples/security/setup.lango +30 -0
  124. package/package.json +14 -6
  125. package/src/builder.lango +22 -0
  126. package/src/client_identity.lango +184 -0
  127. package/src/database.lango +15 -4
  128. package/src/database_action.lango +30 -0
  129. package/src/database_scope.lango +7 -0
  130. package/src/endpoint.lango +12 -5
  131. package/src/execution_failure.lango +14 -0
  132. package/src/execution_identity.lango +71 -0
  133. package/src/execution_log.lango +28 -0
  134. package/src/execution_runtime.lango +135 -0
  135. package/src/execution_worker.lango +32 -0
  136. package/src/executor.lango +284 -0
  137. package/src/group.lango +36 -21
  138. package/src/http_failure.lango +9 -5
  139. package/src/job_configuration.lango +29 -0
  140. package/src/job_context.lango +44 -0
  141. package/src/library.lango +21 -0
  142. package/src/longpoll_options.lango +11 -5
  143. package/src/middleware.lango +1 -1
  144. package/src/module_loader.lango +25 -0
  145. package/src/options.lango +41 -2
  146. package/src/origin.lango +56 -0
  147. package/src/policy.lango +84 -0
  148. package/src/postgres_adapter.lango +12 -0
  149. package/src/postgres_worker.lango +54 -3
  150. package/src/queue_provider.lango +30 -0
  151. package/src/response.lango +1 -1
  152. package/src/route_info.lango +2 -0
  153. package/src/routes.lango +74 -15
  154. package/src/runtime.lango +173 -94
  155. package/src/server.lango +9 -2
  156. package/src/server_context.lango +10 -2
  157. package/src/token.lango +26 -5
  158. package/src/tokens.lango +10 -3
@@ -0,0 +1,384 @@
1
+ import { readDatabaseConfiguration, readJobRoutes } from "./job_configuration.mjs";
2
+ import { executionSource } from "./execution_identity.mjs";
3
+ import { queueProvider } from "./queue_provider.mjs";
4
+ import { errorDetails } from "./error_details.mjs";
5
+ import * as path from "node:path";
6
+ import * as modules from "node:module";
7
+ import * as piscina from "piscina";
8
+ import {
9
+ hostGlobal as __hostGlobal,
10
+ hostGlobalCall as __hostGlobalCall,
11
+ contextGet as __contextGet,
12
+ contextSet as __contextSet,
13
+ contextRestore as __contextRestore,
14
+ bindContexts as __bindContexts,
15
+ fromJson as __fromJson,
16
+ toJson as __toJson,
17
+ getClass as __getClass,
18
+ isKindof as __isKindof,
19
+ isNumber as __isNumber,
20
+ isString as __isString,
21
+ isBool as __isBool,
22
+ isObject as __isObject,
23
+ isArray as __isArray,
24
+ entries as __entries,
25
+ add as __add,
26
+ requireBoolean as __requireBoolean,
27
+ normalize as __normalize,
28
+ boundary as __boundary,
29
+ requireArity as __requireArity,
30
+ withArity as __withArity,
31
+ } from "./.lango/runtime.mjs";
32
+
33
+ export class Executor {
34
+ #definition;
35
+ #workers;
36
+ #maxQueue;
37
+ #timeout;
38
+ #startupTimeout;
39
+ #shutdownTimeout;
40
+ #state;
41
+ #pool;
42
+ #closing;
43
+ #closeError;
44
+ #ready;
45
+ #readyIds;
46
+ #lango;
47
+ constructor(directory, settings) {
48
+ __requireArity(arguments.length, [0, 1], "constructor");
49
+ directory = __boundary(directory);
50
+ settings = __boundary(settings);
51
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(directory))) || __requireBoolean(directory === ""))) {
52
+ throw "Executor requires an application directory";
53
+ }
54
+ if (__requireBoolean(__requireBoolean(__requireBoolean(settings === null) || __requireBoolean(!__requireBoolean(__isObject(settings)))) || __requireBoolean(__isArray(settings)))) {
55
+ throw "Executor requires settings and an identity provider";
56
+ }
57
+ let allowed = ["identity", "workers", "maxQueue", "timeoutMs", "startupTimeoutMs", "shutdownTimeoutMs", "queueProvider"];
58
+ for (const [key, value] of __entries(settings)) {
59
+ if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
60
+ throw `Unknown executor option: ${key}`;
61
+ }
62
+ }
63
+ let root = __normalize(path.resolve(directory));
64
+ this.#definition = { root: root, database: readDatabaseConfiguration(root), targets: readJobRoutes(root), identity: provider(root, __normalize(settings.identity)), queueProvider: queueProvider(root, __normalize(settings.queueProvider)) };
65
+ this.#workers = limit(__normalize(settings.workers), 2, 1, 128, "workers");
66
+ this.#maxQueue = limit(__normalize(settings.maxQueue), 64, 0, 2147483647, "maxQueue");
67
+ this.#timeout = limit(__normalize(settings.timeoutMs), 900000, 1, 2147483647, "timeoutMs");
68
+ this.#startupTimeout = limit(__normalize(settings.startupTimeoutMs), 30000, 1, 2147483647, "startupTimeoutMs");
69
+ this.#shutdownTimeout = limit(__normalize(settings.shutdownTimeoutMs), 30000, 1, 2147483647, "shutdownTimeoutMs");
70
+ this.#state = "new";
71
+ this.#pool = null;
72
+ this.#closing = null;
73
+ this.#closeError = null;
74
+ this.#ready = __normalize(__hostGlobal(["Promise"]).withResolvers());
75
+ this.#readyIds = [];
76
+ }
77
+ start() {
78
+ if (__requireBoolean(__normalize(this.#state) !== "new")) {
79
+ return __normalize(__hostGlobal(["Promise"]).reject("Executor can only be started once"));
80
+ }
81
+ this.#state = "starting";
82
+ try {
83
+ let resolver = __normalize(modules.createRequire(__normalize(path.join(__normalize(this.#definition.root), "package.json"))));
84
+ let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
85
+ this.#pool = new piscina.Piscina({ filename: __normalize(path.join(__normalize(path.dirname(entry)), "execution_worker.mjs")), name: "execute", minThreads: __normalize(this.#workers), maxThreads: __normalize(this.#workers), maxQueue: __normalize(this.#maxQueue), concurrentTasksPerWorker: 1, closeTimeout: __normalize(this.#shutdownTimeout), workerData: __normalize(this.#definition) });
86
+ let pool = __normalize(this.#pool);
87
+ __normalize(pool.on("message", __withArity(__bindContexts((message) => {
88
+ message = __boundary(message);
89
+ if (__requireBoolean(__requireBoolean(__normalize(message.kind) === "ready") && __requireBoolean(!__requireBoolean(__normalize(this.#readyIds.includes(__normalize(message.threadId))))))) {
90
+ __add(this.#readyIds, __normalize(message.threadId));
91
+ if (__requireBoolean(__normalize(this.#readyIds.length) === __normalize(this.#workers))) {
92
+ __normalize(this.#ready.resolve(null));
93
+ }
94
+ }
95
+ return null;
96
+ }), [0], "closure")));
97
+ __normalize(pool.on("error", __withArity(__bindContexts((error) => {
98
+ const __contextScope = [];
99
+ try {
100
+ error = __boundary(error);
101
+ __normalize(this.#ready.reject(error));
102
+ if (__requireBoolean(__normalize(this.#state) === "closing")) {
103
+ this.#closeError = error;
104
+ }
105
+ __normalize(__contextGet("log").err(`Execution worker error: ${errorDetails(error)}`));
106
+ return null;
107
+ } finally {
108
+ __contextRestore(__contextScope);
109
+ }
110
+ }), [0], "closure")));
111
+ } catch (error) {
112
+ this.#state = "closed";
113
+ return __normalize(__hostGlobal(["Promise"]).reject(error));
114
+ }
115
+ let expire = __bindContexts(() => {
116
+ __normalize(this.#ready.reject("Executor startup timed out"));
117
+ return null;
118
+ });
119
+ let timer = __hostGlobalCall(["setTimeout"], expire, __normalize(this.#startupTimeout));
120
+ return __normalize(this.#ready.promise).then(__withArity(__bindContexts((ignored) => {
121
+ ignored = __boundary(ignored);
122
+ if (__requireBoolean(__normalize(this.#state) !== "starting")) {
123
+ throw "Executor closed during startup";
124
+ }
125
+ this.#state = "running";
126
+ return null;
127
+ return null;
128
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
129
+ error = __boundary(error);
130
+ return this.#destroy().then(__withArity(__bindContexts((ignored) => {
131
+ ignored = __boundary(ignored);
132
+ this.#state = "closed";
133
+ throw error;
134
+ return null;
135
+ }), [0], "closure"));
136
+ return null;
137
+ }), [0], "closure")).finally(__bindContexts(() => {
138
+ __hostGlobalCall(["clearTimeout"], timer);
139
+ return null;
140
+ }));
141
+ return null;
142
+ }
143
+ execute(task, options = null) {
144
+ __requireArity(arguments.length, [0], "execute");
145
+ task = __boundary(task);
146
+ options = __boundary(options);
147
+ return this.#run(task, options, false);
148
+ return null;
149
+ }
150
+ authorize(task, options = null) {
151
+ __requireArity(arguments.length, [0], "authorize");
152
+ task = __boundary(task);
153
+ options = __boundary(options);
154
+ return this.#run(task, options, true);
155
+ return null;
156
+ }
157
+ #run(task, options, authorize) {
158
+ __requireArity(arguments.length, [0, 1, 2], "run");
159
+ task = __boundary(task);
160
+ options = __boundary(options);
161
+ authorize = __boundary(authorize);
162
+ if (__requireBoolean(__normalize(this.#state) !== "running")) {
163
+ return __normalize(__hostGlobal(["Promise"]).reject("Executor is not running"));
164
+ }
165
+ try {
166
+ task = validateTask(task, __normalize(this.#definition.targets));
167
+ task.authorize = authorize;
168
+ options = executionOptions(options, __normalize(this.#timeout));
169
+ } catch (error) {
170
+ return __normalize(__hostGlobal(["Promise"]).reject(error));
171
+ }
172
+ let controller = new (__hostGlobal(["AbortController"]))();
173
+ let status = { expired: false };
174
+ let signal = __normalize(options.signal);
175
+ let abort = __bindContexts(() => {
176
+ __normalize(controller.abort());
177
+ return null;
178
+ });
179
+ if (__requireBoolean(signal !== null)) {
180
+ __normalize(signal.addEventListener("abort", abort, { once: true }));
181
+ if (__requireBoolean(__normalize(signal.aborted))) {
182
+ __normalize(controller.abort());
183
+ }
184
+ }
185
+ let expire = __bindContexts(() => {
186
+ if (__requireBoolean(__normalize(status.expired))) {
187
+ return null;
188
+ }
189
+ status.expired = true;
190
+ __normalize(controller.abort());
191
+ return null;
192
+ });
193
+ let timer = __hostGlobalCall(["setTimeout"], expire, __normalize(options.timeoutMs));
194
+ return __normalize(this.#pool.run(task, { signal: __normalize(controller.signal) })).then(__withArity(__bindContexts((result) => {
195
+ result = __boundary(result);
196
+ if (__requireBoolean(!__requireBoolean(__normalize(result.ok)))) {
197
+ let error = new (__hostGlobal(["Error"]))(__normalize(result.message));
198
+ error.code = __normalize(result.code);
199
+ error.permanent = __normalize(result.permanent);
200
+ error.logged = true;
201
+ throw error;
202
+ }
203
+ return __normalize(result.output);
204
+ return null;
205
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
206
+ const __contextScope = [];
207
+ try {
208
+ error = __boundary(error);
209
+ if (__requireBoolean(__normalize(status.expired))) {
210
+ error = new (__hostGlobal(["Error"]))("Execution attempt timed out");
211
+ error.code = "ExecutionTimeout";
212
+ error.permanent = false;
213
+ } else {
214
+ if (__requireBoolean(__requireBoolean(signal !== null) && __requireBoolean(__normalize(signal.aborted)))) {
215
+ error = new (__hostGlobal(["Error"]))("Execution attempt was aborted");
216
+ error.code = "ExecutionAborted";
217
+ error.permanent = false;
218
+ }
219
+ }
220
+ if (__requireBoolean(!__requireBoolean(__isObject(error)))) {
221
+ error = new (__hostGlobal(["Error"]))(`${error}`);
222
+ }
223
+ if (__requireBoolean(__normalize(error.permanent) !== true)) {
224
+ error.permanent = false;
225
+ }
226
+ if (__requireBoolean(__normalize(error.logged) !== true)) {
227
+ let source = __normalize(task.source);
228
+ __normalize(__contextGet("log").err(`[jobId=${__normalize(task.id)}] path=${__normalize(task.path)} attempt=${__normalize(task.attempt)} clientId=${__normalize(source.clientId)} userId=${__normalize(source.userId)} identity=${__normalize(source.identity)}: ${errorDetails(error)}`));
229
+ }
230
+ throw error;
231
+ return null;
232
+ } finally {
233
+ __contextRestore(__contextScope);
234
+ }
235
+ }), [0], "closure")).finally(__bindContexts(() => {
236
+ __hostGlobalCall(["clearTimeout"], timer);
237
+ if (__requireBoolean(signal !== null)) {
238
+ __normalize(signal.removeEventListener("abort", abort));
239
+ }
240
+ return null;
241
+ }));
242
+ return null;
243
+ }
244
+ close() {
245
+ if (__requireBoolean(__normalize(this.#closing) !== null)) {
246
+ return __normalize(this.#closing);
247
+ }
248
+ if (__requireBoolean(__requireBoolean(__normalize(this.#state) === "closed") || __requireBoolean(__normalize(this.#state) === "new"))) {
249
+ this.#state = "closed";
250
+ return __normalize(__hostGlobal(["Promise"]).resolve(null));
251
+ }
252
+ let state = __normalize(this.#state);
253
+ this.#state = "closing";
254
+ if (__requireBoolean(state === "starting")) {
255
+ __normalize(this.#ready.reject("Executor closed during startup"));
256
+ this.#closing = this.#destroy();
257
+ } else {
258
+ this.#closing = this.#drain();
259
+ }
260
+ return __normalize(this.#closing);
261
+ return null;
262
+ }
263
+ #drain() {
264
+ return __normalize(this.#pool.close()).then(__withArity(__bindContexts((ignored) => {
265
+ ignored = __boundary(ignored);
266
+ if (__requireBoolean(__normalize(this.#closeError) !== null)) {
267
+ throw __normalize(this.#closeError);
268
+ }
269
+ return null;
270
+ return null;
271
+ }), [0], "closure")).finally(__bindContexts(() => {
272
+ return this.#destroy();
273
+ return null;
274
+ })).finally(__bindContexts(() => {
275
+ this.#state = "closed";
276
+ return null;
277
+ }));
278
+ return null;
279
+ }
280
+ #destroy() {
281
+ if (__requireBoolean(__normalize(this.#pool) === null)) {
282
+ return __normalize(__hostGlobal(["Promise"]).resolve(null));
283
+ }
284
+ let pool = __normalize(this.#pool);
285
+ this.#pool = null;
286
+ return __normalize(pool.destroy());
287
+ return null;
288
+ }
289
+ }
290
+ function provider(root, spec) {
291
+ __requireArity(arguments.length, [0, 1], "provider");
292
+ root = __boundary(root);
293
+ spec = __boundary(spec);
294
+ if (__requireBoolean(__requireBoolean(__requireBoolean(spec === null) || __requireBoolean(!__requireBoolean(__isObject(spec)))) || __requireBoolean(__isArray(spec)))) {
295
+ throw "Executor identity provider requires module and handler";
296
+ }
297
+ for (const [key, value] of __entries(spec)) {
298
+ if (__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler"))) {
299
+ throw `Unknown identity provider option: ${key}`;
300
+ }
301
+ }
302
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.module)))) || __requireBoolean(__normalize(spec.module) === "")) || __requireBoolean(__normalize(spec.module.startsWith("node:")))) || __requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler))))) || __requireBoolean(__normalize(spec.handler) === ""))) {
303
+ throw "Executor identity provider requires module and handler";
304
+ }
305
+ let resolver = __normalize(modules.createRequire(__normalize(path.join(root, "package.json"))));
306
+ let file = __normalize(resolver.resolve(__normalize(spec.module)));
307
+ if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(file))))) {
308
+ throw "Identity provider must resolve to an application module";
309
+ }
310
+ return { module: file, handler: __normalize(spec.handler) };
311
+ return null;
312
+ }
313
+
314
+ function limit(value, fallback, minimum, maximum, name) {
315
+ __requireArity(arguments.length, [0, 1, 2, 3, 4], "limit");
316
+ value = __boundary(value);
317
+ fallback = __boundary(fallback);
318
+ minimum = __boundary(minimum);
319
+ maximum = __boundary(maximum);
320
+ name = __boundary(name);
321
+ if (__requireBoolean(value === null)) {
322
+ return fallback;
323
+ }
324
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(value)))) || __requireBoolean(value < minimum)) || __requireBoolean(value > maximum))) {
325
+ throw `Invalid executor option: ${name}`;
326
+ }
327
+ return value;
328
+ return null;
329
+ }
330
+
331
+ function executionOptions(options, timeout) {
332
+ __requireArity(arguments.length, [0, 1], "executionOptions");
333
+ options = __boundary(options);
334
+ timeout = __boundary(timeout);
335
+ if (__requireBoolean(options === null)) {
336
+ options = { };
337
+ }
338
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(options))) || __requireBoolean(__isArray(options)))) {
339
+ throw "Execution options must be a map";
340
+ }
341
+ for (const [key, value] of __entries(options)) {
342
+ if (__requireBoolean(__requireBoolean(key !== "signal") && __requireBoolean(key !== "timeoutMs"))) {
343
+ throw `Unknown execution option: ${key}`;
344
+ }
345
+ }
346
+ if (__requireBoolean(__requireBoolean(__normalize(options.signal) !== null) && __requireBoolean(!__requireBoolean(__isKindof(__normalize(options.signal), __normalize(__hostGlobal(["AbortSignal"]))))))) {
347
+ throw "Execution signal must be an AbortSignal";
348
+ }
349
+ return { signal: __normalize(options.signal), timeoutMs: limit(__normalize(options.timeoutMs), timeout, 1, 2147483647, "timeoutMs") };
350
+ return null;
351
+ }
352
+
353
+ function validateTask(task, targets) {
354
+ __requireArity(arguments.length, [0, 1], "validateTask");
355
+ task = __boundary(task);
356
+ targets = __boundary(targets);
357
+ if (__requireBoolean(__requireBoolean(__requireBoolean(task === null) || __requireBoolean(!__requireBoolean(__isObject(task)))) || __requireBoolean(__isArray(task)))) {
358
+ throw "Execution task must be a map";
359
+ }
360
+ let allowed = ["id", "path", "input", "source", "attempt"];
361
+ for (const [key, value] of __entries(task)) {
362
+ if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
363
+ throw `Unknown execution task option: ${key}`;
364
+ }
365
+ }
366
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(task.id)))) || __requireBoolean(__normalize(task.id) === "")) || __requireBoolean(task.id.length > 200))) {
367
+ throw "Execution task requires a job id";
368
+ }
369
+ let found = false;
370
+ for (const route of targets) {
371
+ if (__requireBoolean(__normalize(route.path) === __normalize(task.path))) {
372
+ found = true;
373
+ }
374
+ }
375
+ if (__requireBoolean(!__requireBoolean(found))) {
376
+ throw "Unregistered execution path";
377
+ }
378
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(task.input) === null) || __requireBoolean(!__requireBoolean(__isObject(__normalize(task.input))))) || __requireBoolean(__isArray(__normalize(task.input))))) {
379
+ throw "Execution input must be a JSON map";
380
+ }
381
+ return { id: __normalize(task.id), path: __normalize(task.path), input: __fromJson(__toJson(__normalize(task.input))), source: executionSource(__normalize(task.source)), attempt: limit(__normalize(task.attempt), 1, 1, 2147483647, "attempt") };
382
+ return null;
383
+ }
384
+ //# sourceMappingURL=executor.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.mjs","sources":["executor.lango"],"sourcesContent":["import \"./job_configuration\"\n readDatabaseConfiguration(directory)\n readJobRoutes(directory)\nimport \"./execution_identity\"\n executionSource(source)\nimport \"./queue_provider\"\n queueProvider(root, spec)\nimport \"./error_details\"\n errorDetails(error)\nimport js \"node:path\" as path\nimport js \"node:module\" as modules\nimport js \"piscina\" as piscina\nimport js global Object\nimport js global Number\nimport js global Promise\nimport js global Error\nimport js global AbortController\nimport js global AbortSignal\nimport js global setTimeout\nimport js global clearTimeout\n\nexport class Executor(directory, settings)\n methods\n start()\n execute(task, options = null)\n authorize(task, options = null)\n close()\n\nExecutor(directory, settings)\n if !is_string(directory) or directory == \"\"\n throw \"Executor requires an application directory\"\n if settings == null or !is_object(settings) or is_array(settings)\n throw \"Executor requires settings and an identity provider\"\n allowed = [\"identity\", \"workers\", \"maxQueue\", \"timeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\", \"queueProvider\"]\n for settings as key: value\n if !allowed.includes(key)\n throw \"Unknown executor option: ${key}\"\n root = path/resolve(directory)\n @definition = {\n root: root\n database: readDatabaseConfiguration(root)\n targets: readJobRoutes(root)\n identity: provider(root, settings.identity)\n queueProvider: queueProvider(root, settings.queueProvider)\n }\n @workers = limit(settings.workers, 2, 1, 128, \"workers\")\n @maxQueue = limit(settings.maxQueue, 64, 0, 2147483647, \"maxQueue\")\n @timeout = limit(settings.timeoutMs, 900000, 1, 2147483647, \"timeoutMs\")\n @startupTimeout = limit(settings.startupTimeoutMs, 30000, 1, 2147483647, \"startupTimeoutMs\")\n @shutdownTimeout = limit(settings.shutdownTimeoutMs, 30000, 1, 2147483647, \"shutdownTimeoutMs\")\n @state = \"new\"\n @pool = null\n @closing = null\n @closeError = null\n @ready = Promise/withResolvers()\n @readyIds = []\n\nprovider(root, spec)\n if spec == null or !is_object(spec) or is_array(spec)\n throw \"Executor identity provider requires module and handler\"\n for spec as key: value\n if key != \"module\" and key != \"handler\"\n throw \"Unknown identity provider option: ${key}\"\n if !is_string(spec.module) or spec.module == \"\" or spec.module.startsWith(\"node:\") or !is_string(spec.handler) or spec.handler == \"\"\n throw \"Executor identity provider requires module and handler\"\n resolver = modules/createRequire(path/join(root, \"package.json\"))\n file = resolver.resolve(spec.module)\n if !path/isAbsolute(file)\n throw \"Identity provider must resolve to an application module\"\n return {\n module: file\n handler: spec.handler\n }\n\nlimit(value, fallback, minimum, maximum, name)\n if value == null\n return fallback\n if !Number/isSafeInteger(value) or value < minimum or value > maximum\n throw \"Invalid executor option: ${name}\"\n return value\n\nExecutor:start()\n if @state != \"new\"\n return Promise/reject(\"Executor can only be started once\")\n @state = \"starting\"\n try\n resolver = modules/createRequire(path/join(@definition.root, \"package.json\"))\n entry = resolver.resolve(\"@lango-project/lango-server\")\n @pool = new piscina/Piscina({\n filename: path/join(path/dirname(entry), \"execution_worker.mjs\")\n name: \"execute\"\n minThreads: @workers\n maxThreads: @workers\n maxQueue: @maxQueue\n concurrentTasksPerWorker: 1\n closeTimeout: @shutdownTimeout\n workerData: @definition\n })\n pool = @pool\n pool.on(\"message\", (message)\n if message.kind == \"ready\" and !@readyIds.includes(message.threadId)\n @readyIds.add(message.threadId)\n if @readyIds.length == @workers\n @ready.resolve(null)\n )\n pool.on(\"error\", (error)\n @ready.reject(error)\n if @state == \"closing\"\n @closeError = error\n context log\n log.err(\"Execution worker error: ${errorDetails(error)}\")\n )\n catch error\n @state = \"closed\"\n return Promise/reject(error)\n expire = ()\n @ready.reject(\"Executor startup timed out\")\n timer = setTimeout(expire, @startupTimeout)\n return chain @ready.promise\n then(ignored)\n if @state != \"starting\"\n throw \"Executor closed during startup\"\n @state = \"running\"\n return null\n catch(error)\n return chain destroy()\n then(ignored)\n capture error\n @state = \"closed\"\n throw error\n finally\n capture timer\n clearTimeout(timer)\n\nExecutor:execute(task, options = null)\n return run(task, options, false)\n\nExecutor:authorize(task, options = null)\n return run(task, options, true)\n\nExecutor:run(task, options, authorize)\n if @state != \"running\"\n return Promise/reject(\"Executor is not running\")\n try\n task = validateTask(task, @definition.targets)\n task.authorize = authorize\n options = executionOptions(options, @timeout)\n catch error\n return Promise/reject(error)\n controller = new AbortController()\n status = {\n expired: false\n }\n signal = options.signal\n abort = ()\n capture controller\n controller.abort()\n if signal != null\n signal.addEventListener(\"abort\", abort, {\n once: true\n })\n if signal.aborted\n controller.abort()\n expire = ()\n capture controller\n capture status\n if status.expired\n return null\n status.expired = true\n controller.abort()\n timer = setTimeout(expire, options.timeoutMs)\n return chain @pool.run(task, {\n signal: controller.signal\n })\n then(result)\n if !result.ok\n error = new Error(result.message)\n error.code = result.code\n error.permanent = result.permanent\n error.logged = true\n throw error\n return result.output\n catch(error)\n capture status\n capture task\n capture signal\n if status.expired\n error = new Error(\"Execution attempt timed out\")\n error.code = \"ExecutionTimeout\"\n error.permanent = false\n else\n if signal != null and signal.aborted\n error = new Error(\"Execution attempt was aborted\")\n error.code = \"ExecutionAborted\"\n error.permanent = false\n if !is_object(error)\n error = new Error(\"${error}\")\n if error.permanent != true\n error.permanent = false\n context log\n if error.logged != true\n source = task.source\n log.err(\"[jobId=${task.id}] path=${task.path} attempt=${task.attempt} clientId=${source.clientId} userId=${source.userId} identity=${source.identity}: ${errorDetails(error)}\")\n throw error\n finally\n capture timer\n capture signal\n capture abort\n clearTimeout(timer)\n if signal != null\n signal.removeEventListener(\"abort\", abort)\n\nexecutionOptions(options, timeout)\n if options == null\n options = {}\n if !is_object(options) or is_array(options)\n throw \"Execution options must be a map\"\n for options as key: value\n if key != \"signal\" and key != \"timeoutMs\"\n throw \"Unknown execution option: ${key}\"\n if options.signal != null and !is_kindof(options.signal, AbortSignal)\n throw \"Execution signal must be an AbortSignal\"\n return {\n signal: options.signal\n timeoutMs: limit(options.timeoutMs, timeout, 1, 2147483647, \"timeoutMs\")\n }\n\nvalidateTask(task, targets)\n if task == null or !is_object(task) or is_array(task)\n throw \"Execution task must be a map\"\n allowed = [\"id\", \"path\", \"input\", \"source\", \"attempt\"]\n for task as key: value\n if !allowed.includes(key)\n throw \"Unknown execution task option: ${key}\"\n if !is_string(task.id) or task.id == \"\" or task.id.length > 200\n throw \"Execution task requires a job id\"\n found = false\n for targets as route\n if route.path == task.path\n found = true\n if !found\n throw \"Unregistered execution path\"\n if task.input == null or !is_object(task.input) or is_array(task.input)\n throw \"Execution input must be a JSON map\"\n return {\n id: task.id\n path: task.path\n input: from_json(to_json(task.input))\n source: executionSource(task.source)\n attempt: limit(task.attempt, 1, 1, 2147483647, \"attempt\")\n }\n\nExecutor:close()\n if @closing != null\n return @closing\n if @state == \"closed\" or @state == \"new\"\n @state = \"closed\"\n return Promise/resolve(null)\n state = @state\n @state = \"closing\"\n if state == \"starting\"\n @ready.reject(\"Executor closed during startup\")\n @closing = destroy()\n else\n @closing = drain()\n return @closing\n\nExecutor:drain()\n return chain @pool.close()\n then(ignored)\n if @closeError != null\n throw @closeError\n return null\n finally\n return destroy()\n finally\n @state = \"closed\"\n\nExecutor:destroy()\n if @pool == null\n return Promise/resolve(null)\n pool = @pool\n @pool = null\n return pool.destroy()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA;;;;;;;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;;;;;AA0IA;;;;;;;;;;;;;;;;;;;;;;AAeA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/group.mjs CHANGED
@@ -16,13 +16,17 @@ export class Group {
16
16
  #prefix;
17
17
  #module;
18
18
  #auth;
19
- constructor(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null) {
19
+ #policy;
20
+ #reauthSeconds;
21
+ constructor(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null) {
20
22
  __requireArity(arguments.length, [0, 1], "constructor");
21
23
  routes = __boundary(routes);
22
24
  spec = __boundary(spec);
23
25
  prefix = __boundary(prefix);
24
26
  inheritedModule = __boundary(inheritedModule);
25
27
  inheritedAuth = __boundary(inheritedAuth);
28
+ inheritedPolicy = __boundary(inheritedPolicy);
29
+ inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
26
30
  if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.path)))) || __requireBoolean(!__requireBoolean(__normalize(spec.path.startsWith("/"))))) || __requireBoolean(__normalize(spec.path.endsWith("/")))) || __requireBoolean(__normalize(spec.path.includes("*"))))) {
27
31
  throw "Group requires a path without a trailing slash or wildcard";
28
32
  }
@@ -30,11 +34,17 @@ export class Group {
30
34
  this.#prefix = `${prefix}${__normalize(spec.path)}`;
31
35
  this.#module = __normalize(spec.module);
32
36
  this.#auth = __normalize(spec.auth);
37
+ this.#policy = __normalize(spec.policy);
38
+ this.#reauthSeconds = __normalize(spec.reauthSeconds);
33
39
  if (__requireBoolean(__normalize(this.#module) === null)) {
34
40
  this.#module = inheritedModule;
35
41
  }
36
- if (__requireBoolean(__normalize(this.#auth) === null)) {
42
+ if (__requireBoolean(__requireBoolean(__normalize(this.#auth) === null) && __requireBoolean(__normalize(this.#policy) === null))) {
37
43
  this.#auth = inheritedAuth;
44
+ this.#policy = inheritedPolicy;
45
+ }
46
+ if (__requireBoolean(__normalize(this.#reauthSeconds) === null)) {
47
+ this.#reauthSeconds = inheritedReauthSeconds;
38
48
  }
39
49
  }
40
50
  endpoint(spec) {
@@ -43,26 +53,20 @@ export class Group {
43
53
  if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.path)))) || __requireBoolean(!__requireBoolean(__normalize(spec.path.startsWith("/")))))) {
44
54
  throw "Endpoint path must begin with /";
45
55
  }
46
- let mod = __normalize(spec.module);
47
- let auth = __normalize(spec.auth);
48
- if (__requireBoolean(mod === null)) {
49
- mod = __normalize(this.#module);
50
- }
51
- if (__requireBoolean(auth === null)) {
52
- auth = __normalize(this.#auth);
56
+ let item = this.#merged(spec);
57
+ item.path = `${__normalize(this.#prefix)}${__normalize(spec.path)}`;
58
+ if (__requireBoolean(__normalize(item.module) === null)) {
59
+ item.module = __normalize(this.#module);
53
60
  }
54
- __normalize(this.#routes.endpoint({ path: `${__normalize(this.#prefix)}${__normalize(spec.path)}`, module: mod, handler: __normalize(spec.handler), auth: auth, middleware: __normalize(spec.middleware) }));
61
+ __normalize(this.#routes.endpoint(item));
55
62
  return this;
56
63
  return null;
57
64
  }
58
65
  longpoll(spec) {
59
66
  __requireArity(arguments.length, [0], "longpoll");
60
67
  spec = __boundary(spec);
61
- let item = __normalize(__hostGlobal(["Object"]).assign({ }, spec));
68
+ let item = this.#merged(spec);
62
69
  item.path = `${__normalize(this.#prefix)}${__normalize(spec.path)}`;
63
- if (__requireBoolean(__normalize(item.auth) === null)) {
64
- item.auth = __normalize(this.#auth);
65
- }
66
70
  if (__requireBoolean(__normalize(item.fallback) !== null)) {
67
71
  item.fallback = `${__normalize(this.#prefix)}${__normalize(item.fallback)}`;
68
72
  }
@@ -70,10 +74,25 @@ export class Group {
70
74
  return this;
71
75
  return null;
72
76
  }
77
+ job(spec) {
78
+ __requireArity(arguments.length, [0], "job");
79
+ spec = __boundary(spec);
80
+ let item = this.#merged(spec);
81
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(item.path)))) || __requireBoolean(!__requireBoolean(__normalize(item.path.startsWith("/")))))) {
82
+ throw "Job path must begin with /";
83
+ }
84
+ item.path = `${__normalize(this.#prefix)}${__normalize(item.path)}`;
85
+ if (__requireBoolean(__normalize(item.module) === null)) {
86
+ item.module = __normalize(this.#module);
87
+ }
88
+ __normalize(this.#routes.job(item));
89
+ return this;
90
+ return null;
91
+ }
73
92
  group(spec) {
74
93
  __requireArity(arguments.length, [0], "group");
75
94
  spec = __boundary(spec);
76
- return new Group(__normalize(this.#routes), spec, __normalize(this.#prefix), __normalize(this.#module), __normalize(this.#auth));
95
+ return new Group(__normalize(this.#routes), spec, __normalize(this.#prefix), __normalize(this.#module), __normalize(this.#auth), __normalize(this.#policy), __normalize(this.#reauthSeconds));
77
96
  return null;
78
97
  }
79
98
  include(spec) {
@@ -83,9 +102,23 @@ export class Group {
83
102
  if (__requireBoolean(mount === null)) {
84
103
  mount = "/";
85
104
  }
86
- __normalize(this.#routes.include({ module: __normalize(spec.module), path: `${__normalize(this.#prefix)}${mount}` }, __normalize(this.#auth)));
105
+ __normalize(this.#routes.include({ module: __normalize(spec.module), path: `${__normalize(this.#prefix)}${mount}` }, __normalize(this.#auth), __normalize(this.#policy), __normalize(this.#reauthSeconds)));
87
106
  return this;
88
107
  return null;
89
108
  }
109
+ #merged(spec) {
110
+ __requireArity(arguments.length, [0], "merged");
111
+ spec = __boundary(spec);
112
+ let item = __normalize(__hostGlobal(["Object"]).assign({ }, spec));
113
+ if (__requireBoolean(__requireBoolean(__normalize(item.auth) === null) && __requireBoolean(__normalize(item.policy) === null))) {
114
+ item.auth = __normalize(this.#auth);
115
+ item.policy = __normalize(this.#policy);
116
+ }
117
+ if (__requireBoolean(__normalize(item.reauthSeconds) === null)) {
118
+ item.reauthSeconds = __normalize(this.#reauthSeconds);
119
+ }
120
+ return item;
121
+ return null;
122
+ }
90
123
  }
91
124
  //# sourceMappingURL=group.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"group.mjs","sources":["group.lango"],"sourcesContent":["import js global Object\n\nexport class Group(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null)\n methods\n endpoint(spec)\n longpoll(spec)\n group(spec)\n include(spec)\n\nGroup(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\") or spec.path.endsWith(\"/\") or spec.path.includes(\"*\")\n throw \"Group requires a path without a trailing slash or wildcard\"\n @routes = routes\n @prefix = \"${prefix}${spec.path}\"\n @module = spec.module\n @auth = spec.auth\n if @module == null\n @module = inheritedModule\n if @auth == null\n @auth = inheritedAuth\n\nGroup:endpoint(spec)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\")\n throw \"Endpoint path must begin with /\"\n mod = spec.module\n auth = spec.auth\n if mod == null\n mod = @module\n if auth == null\n auth = @auth\n @routes.endpoint({\n path: \"${@prefix}${spec.path}\"\n module: mod\n handler: spec.handler\n auth: auth\n middleware: spec.middleware\n })\n return @this\n\nGroup:longpoll(spec)\n item = Object/assign({}, spec)\n item.path = \"${@prefix}${spec.path}\"\n if item.auth == null\n item.auth = @auth\n if item.fallback != null\n item.fallback = \"${@prefix}${item.fallback}\"\n @routes.longpoll(item)\n return @this\n\nGroup:group(spec)\n return new Group(@routes, spec, @prefix, @module, @auth)\n\nGroup:include(spec)\n mount = spec.path\n if mount == null\n mount = \"/\"\n @routes.include({\n module: spec.module\n path: \"${@prefix}${mount}\"\n }, @auth)\n return @this\n"],"names":[],"mappings":";;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"group.mjs","sources":["group.lango"],"sourcesContent":["import js global Object\n\nexport class Group(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)\n methods\n endpoint(spec)\n job(spec)\n longpoll(spec)\n group(spec)\n include(spec)\n\nGroup(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\") or spec.path.endsWith(\"/\") or spec.path.includes(\"*\")\n throw \"Group requires a path without a trailing slash or wildcard\"\n @routes = routes\n @prefix = \"${prefix}${spec.path}\"\n @module = spec.module\n @auth = spec.auth\n @policy = spec.policy\n @reauthSeconds = spec.reauthSeconds\n if @module == null\n @module = inheritedModule\n if @auth == null and @policy == null\n @auth = inheritedAuth\n @policy = inheritedPolicy\n if @reauthSeconds == null\n @reauthSeconds = inheritedReauthSeconds\n\nGroup:endpoint(spec)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\")\n throw \"Endpoint path must begin with /\"\n item = merged(spec)\n item.path = \"${@prefix}${spec.path}\"\n if item.module == null\n item.module = @module\n @routes.endpoint(item)\n return @this\n\nGroup:longpoll(spec)\n item = merged(spec)\n item.path = \"${@prefix}${spec.path}\"\n if item.fallback != null\n item.fallback = \"${@prefix}${item.fallback}\"\n @routes.longpoll(item)\n return @this\n\nGroup:job(spec)\n item = merged(spec)\n if !is_string(item.path) or !item.path.startsWith(\"/\")\n throw \"Job path must begin with /\"\n item.path = \"${@prefix}${item.path}\"\n if item.module == null\n item.module = @module\n @routes.job(item)\n return @this\n\nGroup:group(spec)\n return new Group(@routes, spec, @prefix, @module, @auth, @policy, @reauthSeconds)\n\nGroup:include(spec)\n mount = spec.path\n if mount == null\n mount = \"/\"\n @routes.include({\n module: spec.module\n path: \"${@prefix}${mount}\"\n }, @auth, @policy, @reauthSeconds)\n return @this\n\nGroup:merged(spec)\n item = Object/assign({}, spec)\n if item.auth == null and item.policy == null\n item.auth = @auth\n item.policy = @policy\n if item.reauthSeconds == null\n item.reauthSeconds = @reauthSeconds\n return item\n"],"names":[],"mappings":";;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -15,14 +15,16 @@ import {
15
15
  } from "./.lango/runtime.mjs";
16
16
 
17
17
  export class HttpFailure {
18
+ #details;
18
19
  #status;
19
20
  #code;
20
21
  #headers;
21
- constructor(status, code, headers) {
22
+ constructor(status, code, headers, details = null) {
22
23
  __requireArity(arguments.length, [0, 1, 2], "constructor");
23
24
  status = __boundary(status);
24
25
  code = __boundary(code);
25
26
  headers = __boundary(headers);
27
+ details = __boundary(details);
26
28
  if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isNumber(status))) || __requireBoolean(__finite(status % 1) !== 0)) || __requireBoolean(status < 400)) || __requireBoolean(status > 599))) {
27
29
  throw "Rejected response requires an error status";
28
30
  }
@@ -30,6 +32,7 @@ export class HttpFailure {
30
32
  if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(code))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(code)))))) {
31
33
  throw "Rejected response requires an error code";
32
34
  }
35
+ this.#details = details;
33
36
  this.#status = status;
34
37
  this.#code = code;
35
38
  this.#headers = __fromJson(__toJson(headers));
@@ -38,7 +41,11 @@ export class HttpFailure {
38
41
  }
39
42
  }
40
43
  result() {
41
- return { status: __normalize(this.#status), headers: __fromJson(__toJson(__normalize(this.#headers))), body: __toJson({ error: __normalize(this.#code) }) };
44
+ let body = { error: __normalize(this.#code) };
45
+ if (__requireBoolean(__normalize(this.#details) !== null)) {
46
+ body.needLogin = __normalize(this.#details);
47
+ }
48
+ return { status: __normalize(this.#status), headers: __fromJson(__toJson(__normalize(this.#headers))), body: __toJson(body) };
42
49
  return null;
43
50
  }
44
51
  }
@@ -1 +1 @@
1
- {"version":3,"file":"http_failure.mjs","sources":["http_failure.lango"],"sourcesContent":["import js global RegExp\n\nexport class HttpFailure(status, code, headers)\n methods\n result()\n\nHttpFailure(status, code, headers)\n if !is_number(status) or status % 1 != 0 or status < 400 or status > 599\n throw \"Rejected response requires an error status\"\n pattern = new RegExp(\"^[A-Za-z][A-Za-z0-9_]{0,63}$\")\n if !is_string(code) or !pattern.test(code)\n throw \"Rejected response requires an error code\"\n @status = status\n @code = code\n @headers = from_json(to_json(headers))\n if status == 401\n @headers[\"www-authenticate\"] = \"Bearer\"\n\nHttpFailure:result()\n return {\n status: @status\n headers: from_json(to_json(@headers))\n body: to_json({\n error: @code\n })\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"http_failure.mjs","sources":["http_failure.lango"],"sourcesContent":["import js global RegExp\n\nexport class HttpFailure(status, code, headers, details = null)\n methods\n result()\n\nHttpFailure(status, code, headers, details = null)\n if !is_number(status) or status % 1 != 0 or status < 400 or status > 599\n throw \"Rejected response requires an error status\"\n pattern = new RegExp(\"^[A-Za-z][A-Za-z0-9_]{0,63}$\")\n if !is_string(code) or !pattern.test(code)\n throw \"Rejected response requires an error code\"\n @details = details\n @status = status\n @code = code\n @headers = from_json(to_json(headers))\n if status == 401\n @headers[\"www-authenticate\"] = \"Bearer\"\n\nHttpFailure:result()\n body = {\n error: @code\n }\n if @details != null\n body.needLogin = @details\n return {\n status: @status\n headers: from_json(to_json(@headers))\n body: to_json(body)\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,37 @@
1
+ import { readDeployment } from "./options.mjs";
2
+ import { databaseOptions } from "./database_options.mjs";
3
+ import { Routes } from "./routes.mjs";
4
+ import { policyRegistry, resolvePolicies } from "./policy.mjs";
5
+ import * as path from "node:path";
6
+ import {
7
+ hostGlobalCall as __hostGlobalCall,
8
+ requireBoolean as __requireBoolean,
9
+ normalize as __normalize,
10
+ boundary as __boundary,
11
+ requireArity as __requireArity,
12
+ } from "./.lango/runtime.mjs";
13
+
14
+ export function readDatabaseConfiguration(directory) {
15
+ __requireArity(arguments.length, [0], "readDatabaseConfiguration");
16
+ directory = __boundary(directory);
17
+ let root = __normalize(path.resolve(directory));
18
+ let settings = readDeployment(root, false);
19
+ let workers = 2;
20
+ if (__requireBoolean(__normalize(settings.workers) !== null)) {
21
+ workers = __hostGlobalCall(["Number"], __normalize(settings.workers));
22
+ }
23
+ return databaseOptions(root, __normalize(settings.database), workers);
24
+ return null;
25
+ }
26
+
27
+ export function readJobRoutes(directory) {
28
+ __requireArity(arguments.length, [0], "readJobRoutes");
29
+ directory = __boundary(directory);
30
+ let root = __normalize(path.resolve(directory));
31
+ let settings = readDeployment(root, false);
32
+ let routes = new Routes(root);
33
+ __normalize(routes.xml(__normalize(path.join(root, "routes.xml"))));
34
+ return resolvePolicies(__normalize(routes.jobs()), policyRegistry(__normalize(settings.policies)));
35
+ return null;
36
+ }
37
+ //# sourceMappingURL=job_configuration.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job_configuration.mjs","sources":["job_configuration.lango"],"sourcesContent":["import \"./options\"\n readDeployment(directory, credentials = true)\nimport \"./database_options\"\n databaseOptions(root, spec, workers)\nimport \"./routes\"\n class Routes\nimport \"./policy\"\n policyRegistry(spec)\n resolvePolicies(routes, registry)\nimport js \"node:path\" as path\nimport js global Number\n\nexport readDatabaseConfiguration(directory)\nexport readJobRoutes(directory)\n\nreadDatabaseConfiguration(directory)\n root = path/resolve(directory)\n settings = readDeployment(root, false)\n workers = 2\n if settings.workers != null\n workers = Number(settings.workers)\n return databaseOptions(root, settings.database, workers)\n\nreadJobRoutes(directory)\n root = path/resolve(directory)\n settings = readDeployment(root, false)\n routes = new Routes(root)\n routes.xml(path/join(root, \"routes.xml\"))\n return resolvePolicies(routes.jobs(), policyRegistry(settings.policies))\n"],"names":[],"mappings":";;;;;;;;;;;;;AAeA;;;;;;;;;;;;;AAQA;;;;;;;;;;;"}