@lango-project/lango-server 0.2.3 → 0.2.5

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 (150) hide show
  1. package/README.md +22 -3
  2. package/dist/body_reader.mjs.map +1 -1
  3. package/dist/builder.mjs +26 -2
  4. package/dist/builder.mjs.map +1 -1
  5. package/dist/cli/server.mjs +3 -835
  6. package/dist/cli/server.mjs.map +1 -1
  7. package/dist/client_identity.mjs.map +1 -1
  8. package/dist/database.mjs.map +1 -1
  9. package/dist/database_action.mjs +71 -0
  10. package/dist/database_action.mjs.map +1 -0
  11. package/dist/database_options.mjs.map +1 -1
  12. package/dist/database_scope.mjs +11 -0
  13. package/dist/database_scope.mjs.map +1 -1
  14. package/dist/endpoint.mjs.map +1 -1
  15. package/dist/error_details.mjs.map +1 -1
  16. package/dist/execution_failure.mjs +30 -0
  17. package/dist/execution_failure.mjs.map +1 -0
  18. package/dist/execution_identity.mjs +103 -0
  19. package/dist/execution_identity.mjs.map +1 -0
  20. package/dist/execution_log.mjs +47 -0
  21. package/dist/execution_log.mjs.map +1 -0
  22. package/dist/execution_runtime.mjs +201 -0
  23. package/dist/execution_runtime.mjs.map +1 -0
  24. package/dist/execution_worker.mjs +41 -0
  25. package/dist/execution_worker.mjs.map +1 -0
  26. package/dist/executor.mjs +384 -0
  27. package/dist/executor.mjs.map +1 -0
  28. package/dist/group.mjs +15 -0
  29. package/dist/group.mjs.map +1 -1
  30. package/dist/http_failure.mjs.map +1 -1
  31. package/dist/job_configuration.mjs +37 -0
  32. package/dist/job_configuration.mjs.map +1 -0
  33. package/dist/job_context.mjs +61 -0
  34. package/dist/job_context.mjs.map +1 -0
  35. package/dist/library.mjs +31 -0
  36. package/dist/library.mjs.map +1 -1
  37. package/dist/longpoll_broker.mjs.map +1 -1
  38. package/dist/longpoll_options.mjs.map +1 -1
  39. package/dist/longpoll_session.mjs.map +1 -1
  40. package/dist/middleware.mjs +1 -1
  41. package/dist/middleware.mjs.map +1 -1
  42. package/dist/module_loader.mjs +44 -0
  43. package/dist/module_loader.mjs.map +1 -0
  44. package/dist/options.mjs +12 -1
  45. package/dist/options.mjs.map +1 -1
  46. package/dist/origin.mjs.map +1 -1
  47. package/dist/policy.mjs.map +1 -1
  48. package/dist/postgres_adapter.mjs +12 -1
  49. package/dist/postgres_adapter.mjs.map +1 -1
  50. package/dist/postgres_worker.mjs +67 -5
  51. package/dist/postgres_worker.mjs.map +1 -1
  52. package/dist/queue_provider.mjs +52 -0
  53. package/dist/queue_provider.mjs.map +1 -0
  54. package/dist/request_identity.mjs.map +1 -1
  55. package/dist/response.mjs.map +1 -1
  56. package/dist/route_info.mjs.map +1 -1
  57. package/dist/route_pattern.mjs.map +1 -1
  58. package/dist/routes.mjs +36 -1
  59. package/dist/routes.mjs.map +1 -1
  60. package/dist/runtime.mjs +65 -25
  61. package/dist/runtime.mjs.map +1 -1
  62. package/dist/server.mjs +4 -1
  63. package/dist/server.mjs.map +1 -1
  64. package/dist/server_context.mjs.map +1 -1
  65. package/dist/sqlite_adapter.mjs.map +1 -1
  66. package/dist/static_files.mjs.map +1 -1
  67. package/dist/token.mjs.map +1 -1
  68. package/dist/tokens.mjs.map +1 -1
  69. package/dist/trace.mjs.map +1 -1
  70. package/dist/worker.mjs.map +1 -1
  71. package/docs/jobs.md +123 -0
  72. package/examples/security/package.json +1 -1
  73. package/package.json +10 -6
  74. package/src/builder.lango +22 -0
  75. package/src/database_action.lango +30 -0
  76. package/src/database_scope.lango +7 -0
  77. package/src/execution_failure.lango +14 -0
  78. package/src/execution_identity.lango +71 -0
  79. package/src/execution_log.lango +28 -0
  80. package/src/execution_runtime.lango +135 -0
  81. package/src/execution_worker.lango +32 -0
  82. package/src/executor.lango +284 -0
  83. package/src/group.lango +11 -0
  84. package/src/job_configuration.lango +29 -0
  85. package/src/job_context.lango +44 -0
  86. package/src/library.lango +21 -0
  87. package/src/middleware.lango +1 -1
  88. package/src/module_loader.lango +25 -0
  89. package/src/options.lango +7 -1
  90. package/src/postgres_adapter.lango +12 -0
  91. package/src/postgres_worker.lango +54 -3
  92. package/src/queue_provider.lango +30 -0
  93. package/src/routes.lango +44 -0
  94. package/src/runtime.lango +45 -17
  95. package/src/server.lango +3 -0
  96. package/dist/cli/.lango/bignumber.mjs +0 -3119
  97. package/dist/cli/.lango/localization.mjs +0 -108
  98. package/dist/cli/.lango/runtime.mjs +0 -725
  99. package/dist/cli/.lango/vendor/i18next.LICENSE +0 -21
  100. package/dist/cli/.lango/vendor/i18next.mjs +0 -2434
  101. package/dist/cli/.lango/xmldom/conventions.js +0 -429
  102. package/dist/cli/.lango/xmldom/dom-parser.js +0 -591
  103. package/dist/cli/.lango/xmldom/dom.js +0 -3759
  104. package/dist/cli/.lango/xmldom/entities.js +0 -2171
  105. package/dist/cli/.lango/xmldom/errors.js +0 -205
  106. package/dist/cli/.lango/xmldom/grammar.js +0 -559
  107. package/dist/cli/.lango/xmldom/index.js +0 -41
  108. package/dist/cli/.lango/xmldom/package.json +0 -1
  109. package/dist/cli/.lango/xmldom/sax.js +0 -983
  110. package/dist/cli/.lango/xmldom.LICENSE.md +0 -8
  111. package/dist/cli/body_reader.mjs +0 -180
  112. package/dist/cli/body_reader.mjs.map +0 -1
  113. package/dist/cli/client_identity.mjs +0 -250
  114. package/dist/cli/client_identity.mjs.map +0 -1
  115. package/dist/cli/database.mjs +0 -116
  116. package/dist/cli/database.mjs.map +0 -1
  117. package/dist/cli/database_options.mjs +0 -83
  118. package/dist/cli/database_options.mjs.map +0 -1
  119. package/dist/cli/database_scope.mjs +0 -82
  120. package/dist/cli/database_scope.mjs.map +0 -1
  121. package/dist/cli/endpoint.mjs +0 -50
  122. package/dist/cli/endpoint.mjs.map +0 -1
  123. package/dist/cli/error_details.mjs +0 -45
  124. package/dist/cli/error_details.mjs.map +0 -1
  125. package/dist/cli/longpoll_broker.mjs +0 -215
  126. package/dist/cli/longpoll_broker.mjs.map +0 -1
  127. package/dist/cli/longpoll_options.mjs +0 -103
  128. package/dist/cli/longpoll_options.mjs.map +0 -1
  129. package/dist/cli/longpoll_session.mjs +0 -136
  130. package/dist/cli/longpoll_session.mjs.map +0 -1
  131. package/dist/cli/middleware.mjs +0 -25
  132. package/dist/cli/middleware.mjs.map +0 -1
  133. package/dist/cli/options.mjs +0 -203
  134. package/dist/cli/options.mjs.map +0 -1
  135. package/dist/cli/origin.mjs +0 -87
  136. package/dist/cli/origin.mjs.map +0 -1
  137. package/dist/cli/policy.mjs +0 -121
  138. package/dist/cli/policy.mjs.map +0 -1
  139. package/dist/cli/postgres_adapter.mjs +0 -167
  140. package/dist/cli/postgres_adapter.mjs.map +0 -1
  141. package/dist/cli/route_pattern.mjs +0 -129
  142. package/dist/cli/route_pattern.mjs.map +0 -1
  143. package/dist/cli/routes.mjs +0 -325
  144. package/dist/cli/routes.mjs.map +0 -1
  145. package/dist/cli/sqlite_adapter.mjs +0 -163
  146. package/dist/cli/sqlite_adapter.mjs.map +0 -1
  147. package/dist/cli/static_files.mjs +0 -224
  148. package/dist/cli/static_files.mjs.map +0 -1
  149. package/dist/cli/trace.mjs +0 -53
  150. package/dist/cli/trace.mjs.map +0 -1
@@ -1,167 +0,0 @@
1
- import * as modules from "node:module";
2
- import * as path from "node:path";
3
- import * as threads from "node:worker_threads";
4
- import {
5
- hostGlobal as __hostGlobal,
6
- bindContexts as __bindContexts,
7
- requireBoolean as __requireBoolean,
8
- normalize as __normalize,
9
- finite as __finite,
10
- numericAdd as __numericAdd,
11
- boundary as __boundary,
12
- requireArity as __requireArity,
13
- withArity as __withArity,
14
- } from "./.lango/runtime.mjs";
15
-
16
- export class PostgresAdapter {
17
- parameterStyle;
18
- #closed;
19
- #broken;
20
- #state;
21
- #depth;
22
- #rollbackOnly;
23
- #timeout;
24
- #signal;
25
- #port;
26
- #lango;
27
- #worker;
28
- constructor(mod, settings, root) {
29
- __requireArity(arguments.length, [0, 1, 2], "constructor");
30
- mod = __boundary(mod);
31
- settings = __boundary(settings);
32
- root = __boundary(root);
33
- this.#closed = false;
34
- this.#broken = false;
35
- this.#state = null;
36
- this.#depth = 0;
37
- this.#rollbackOnly = false;
38
- this.parameterStyle = "postgresql";
39
- this.#timeout = __normalize(__hostGlobal(["Math"]).min(2147483647, __numericAdd(__normalize(settings.queryTimeoutMs), 1000)));
40
- this.#signal = new (__hostGlobal(["Int32Array"]))(new (__hostGlobal(["SharedArrayBuffer"]))(4));
41
- let channel = __normalize(__hostGlobal(["Reflect"]).construct(__normalize(mod.MessageChannel), []));
42
- this.#port = __normalize(channel.port1);
43
- let resolver = __normalize(modules.createRequire(__normalize(path.join(root, "package.json"))));
44
- let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
45
- this.#worker = __normalize(__hostGlobal(["Reflect"]).construct(__normalize(mod.Worker), [__normalize(path.join(__normalize(path.dirname(entry)), "postgres_worker.mjs")), { workerData: { settings: settings, port: __normalize(channel.port2), signal: __normalize(this.#signal) }, transferList: [__normalize(channel.port2)] }]));
46
- let worker = __normalize(this.#worker);
47
- __normalize(worker.on("error", __withArity(__bindContexts((error) => {
48
- error = __boundary(error);
49
- this.#broken = true;
50
- return null;
51
- }), [0], "closure")));
52
- __normalize(this.#worker.unref());
53
- __normalize(this.#port.unref());
54
- try {
55
- this.#exchange({ action: "connect" }, __normalize(__hostGlobal(["Math"]).min(2147483647, __numericAdd(__finite(__normalize(settings.connectTimeoutSeconds) * 1000), 1000))));
56
- } catch (error) {
57
- this.close();
58
- throw new (__hostGlobal(["Error"]))("PostgreSQL initialization failed");
59
- }
60
- }
61
- #exchange(task, timeout) {
62
- __requireArity(arguments.length, [0, 1], "exchange");
63
- task = __boundary(task);
64
- timeout = __boundary(timeout);
65
- if (__requireBoolean(__requireBoolean(__normalize(this.#closed)) || __requireBoolean(__normalize(this.#broken)))) {
66
- throw new (__hostGlobal(["Error"]))("PostgreSQL connection is unavailable");
67
- }
68
- __normalize(__hostGlobal(["Atomics"]).store(__normalize(this.#signal), 0, 0));
69
- __normalize(this.#port.postMessage(task));
70
- let waited = __normalize(__hostGlobal(["Atomics"]).wait(__normalize(this.#signal), 0, 0, timeout));
71
- if (__requireBoolean(waited === "timed-out")) {
72
- this.#broken = true;
73
- __normalize(this.#worker.terminate());
74
- throw new (__hostGlobal(["Error"]))("PostgreSQL operation timed out");
75
- }
76
- let reply = __normalize(threads.receiveMessageOnPort(__normalize(this.#port)));
77
- if (__requireBoolean(reply === null)) {
78
- this.#broken = true;
79
- throw new (__hostGlobal(["Error"]))("PostgreSQL response was not received");
80
- }
81
- let result = __normalize(reply.message);
82
- this.#state = __normalize(result.state);
83
- if (__requireBoolean(__normalize(result.failed))) {
84
- this.#broken = __normalize(result.dead);
85
- let error = new (__hostGlobal(["Error"]))("PostgreSQL query failed");
86
- error.code = __normalize(result.code);
87
- error.position = __normalize(result.position);
88
- error.transactionUnusable = __normalize(this.#state) === "failed";
89
- throw error;
90
- }
91
- return result;
92
- return null;
93
- }
94
- execute(sql, parameters) {
95
- __requireArity(arguments.length, [0, 1], "execute");
96
- sql = __boundary(sql);
97
- parameters = __boundary(parameters);
98
- return this.#exchange({ action: "query", sql: sql, parameters: parameters }, __normalize(this.#timeout));
99
- return null;
100
- }
101
- begin() {
102
- if (__requireBoolean(__requireBoolean(__normalize(this.#depth) === 0) && __requireBoolean(__normalize(this.#state) === null))) {
103
- this.execute("BEGIN", []);
104
- }
105
- this.#depth = __numericAdd(__normalize(this.#depth), 1);
106
- return null;
107
- return null;
108
- }
109
- commit() {
110
- if (__requireBoolean(__normalize(this.#depth) === 0)) {
111
- throw new (__hostGlobal(["Error"]))("NoActiveTransaction");
112
- }
113
- if (__requireBoolean(__normalize(this.#depth) > 1)) {
114
- this.#depth = __finite(__normalize(this.#depth) - 1);
115
- return null;
116
- }
117
- if (__requireBoolean(__requireBoolean(__normalize(this.#rollbackOnly)) || __requireBoolean(__normalize(this.#state) === "failed"))) {
118
- this.rollback();
119
- throw new (__hostGlobal(["Error"]))("TransactionRolledBack");
120
- }
121
- this.execute("COMMIT", []);
122
- this.#depth = 0;
123
- return null;
124
- return null;
125
- }
126
- rollback() {
127
- if (__requireBoolean(__normalize(this.#depth) > 1)) {
128
- this.#depth = __finite(__normalize(this.#depth) - 1);
129
- this.#rollbackOnly = true;
130
- return null;
131
- }
132
- if (__requireBoolean(__normalize(this.#state) !== null)) {
133
- this.execute("ROLLBACK", []);
134
- }
135
- this.#depth = 0;
136
- this.#rollbackOnly = false;
137
- return null;
138
- return null;
139
- }
140
- transactionState() {
141
- if (__requireBoolean(__normalize(this.#broken))) {
142
- throw new (__hostGlobal(["Error"]))("PostgreSQL connection is unavailable");
143
- }
144
- if (__requireBoolean(__requireBoolean(__normalize(this.#rollbackOnly)) && __requireBoolean(__normalize(this.#state) !== "failed"))) {
145
- return "rollback_only";
146
- }
147
- return __normalize(this.#state);
148
- return null;
149
- }
150
- close() {
151
- if (__requireBoolean(__normalize(this.#closed))) {
152
- return null;
153
- }
154
- try {
155
- if (__requireBoolean(!__requireBoolean(__normalize(this.#broken)))) {
156
- this.#exchange({ action: "close" }, __normalize(this.#timeout));
157
- }
158
- } finally {
159
- this.#closed = true;
160
- __normalize(this.#port.close());
161
- __normalize(this.#worker.terminate());
162
- }
163
- return null;
164
- return null;
165
- }
166
- }
167
- //# sourceMappingURL=postgres_adapter.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"postgres_adapter.mjs","sources":["postgres_adapter.lango"],"sourcesContent":["import js \"node:module\" as modules\nimport js \"node:path\" as path\nimport js \"node:worker_threads\" as threads\nimport js global Reflect\nimport js global SharedArrayBuffer\nimport js global Int32Array\nimport js global Atomics\nimport js global Math\nimport js global Error\n\nexport class PostgresAdapter(mod, settings, root)\n properties\n parameterStyle\n methods\n execute(sql, parameters)\n begin()\n commit()\n rollback()\n transactionState()\n close()\n\nPostgresAdapter(mod, settings, root)\n @closed = false\n @broken = false\n @state = null\n @depth = 0\n @rollbackOnly = false\n @parameterStyle = \"postgresql\"\n @timeout = Math/min(2147483647, settings.queryTimeoutMs + 1000)\n @signal = new Int32Array(new SharedArrayBuffer(4))\n channel = Reflect/construct(mod.MessageChannel, [])\n @port = channel.port1\n resolver = modules/createRequire(path/join(root, \"package.json\"))\n entry = resolver.resolve(\"@lango-project/lango-server\")\n @worker = Reflect/construct(mod.Worker, [path/join(path/dirname(entry), \"postgres_worker.mjs\"), {\n workerData: {\n settings: settings\n port: channel.port2\n signal: @signal\n }\n transferList: [channel.port2]\n }])\n worker = @worker\n worker.on(\"error\", (error)\n @broken = true\n )\n @worker.unref()\n @port.unref()\n try\n exchange({\n action: \"connect\"\n }, Math/min(2147483647, settings.connectTimeoutSeconds * 1000 + 1000))\n catch error\n close()\n throw new Error(\"PostgreSQL initialization failed\")\n\nPostgresAdapter:exchange(task, timeout)\n if @closed or @broken\n throw new Error(\"PostgreSQL connection is unavailable\")\n Atomics/store(@signal, 0, 0)\n @port.postMessage(task)\n waited = Atomics/wait(@signal, 0, 0, timeout)\n if waited == \"timed-out\"\n @broken = true\n @worker.terminate()\n throw new Error(\"PostgreSQL operation timed out\")\n reply = threads/receiveMessageOnPort(@port)\n if reply == null\n @broken = true\n throw new Error(\"PostgreSQL response was not received\")\n result = reply.message\n @state = result.state\n if result.failed\n @broken = result.dead\n error = new Error(\"PostgreSQL query failed\")\n error.code = result.code\n error.position = result.position\n error.transactionUnusable = @state == \"failed\"\n throw error\n return result\n\nPostgresAdapter:execute(sql, parameters)\n return exchange({\n action: \"query\"\n sql: sql\n parameters: parameters\n }, @timeout)\n\nPostgresAdapter:begin()\n if @depth == 0 and @state == null\n execute(\"BEGIN\", [])\n @depth = @depth + 1\n return null\n\nPostgresAdapter:commit()\n if @depth == 0\n throw new Error(\"NoActiveTransaction\")\n if @depth > 1\n @depth = @depth - 1\n return null\n if @rollbackOnly or @state == \"failed\"\n rollback()\n throw new Error(\"TransactionRolledBack\")\n execute(\"COMMIT\", [])\n @depth = 0\n return null\n\nPostgresAdapter:rollback()\n if @depth > 1\n @depth = @depth - 1\n @rollbackOnly = true\n return null\n if @state != null\n execute(\"ROLLBACK\", [])\n @depth = 0\n @rollbackOnly = false\n return null\n\nPostgresAdapter:transactionState()\n if @broken\n throw new Error(\"PostgreSQL connection is unavailable\")\n if @rollbackOnly and @state != \"failed\"\n return \"rollback_only\"\n return @state\n\nPostgresAdapter:close()\n if @closed\n return null\n try\n if !@broken\n exchange({\n action: \"close\"\n }, @timeout)\n finally\n @closed = true\n @port.close()\n @worker.terminate()\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,129 +0,0 @@
1
- import {
2
- hostGlobal as __hostGlobal,
3
- isNumber as __isNumber,
4
- isString as __isString,
5
- isBool as __isBool,
6
- isObject as __isObject,
7
- isArray as __isArray,
8
- entries as __entries,
9
- range as __range,
10
- requireBoolean as __requireBoolean,
11
- normalize as __normalize,
12
- finite as __finite,
13
- boundary as __boundary,
14
- requireArity as __requireArity,
15
- } from "./.lango/runtime.mjs";
16
-
17
- export function routePattern(path) {
18
- __requireArity(arguments.length, [0], "routePattern");
19
- path = __boundary(path);
20
- if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(path))) || __requireBoolean(!__requireBoolean(__normalize(path.startsWith("/"))))) || __requireBoolean(__normalize(path.includes("?")))) || __requireBoolean(__normalize(path.includes("#")))) || __requireBoolean(__normalize(path.includes("\\")))) || __requireBoolean(__normalize(path.includes("//"))))) {
21
- throw "Endpoint requires an absolute URL path";
22
- }
23
- if (__requireBoolean(__requireBoolean(__normalize(path.includes("*"))) && __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(path.endsWith("/*")))) || __requireBoolean(__normalize(path.indexOf("*")) !== __normalize(path.lastIndexOf("*")))))) {
24
- throw "Only trailing /* wildcards are supported";
25
- }
26
- let segments = __normalize(path.split("/"));
27
- segments = __normalize(segments.slice(1));
28
- let wildcard = __normalize(path.endsWith("/*"));
29
- if (__requireBoolean(wildcard)) {
30
- __normalize(segments.pop());
31
- }
32
- let names = [];
33
- let shape = [];
34
- let identifier = new (__hostGlobal(["RegExp"]))("^[A-Za-z_][A-Za-z0-9_]*$");
35
- let reserved = ["__proto__", "constructor", "prototype"];
36
- for (const segment of segments) {
37
- if (__requireBoolean(__requireBoolean(__normalize(segment.includes("{"))) || __requireBoolean(__normalize(segment.includes("}"))))) {
38
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(segment.startsWith("{")))) || __requireBoolean(!__requireBoolean(__normalize(segment.endsWith("}")))))) {
39
- throw "Path parameters must occupy an entire segment";
40
- }
41
- let name = __normalize(segment.slice(1, __finite(-1)));
42
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(identifier.test(name)))) || __requireBoolean(__normalize(reserved.includes(name))))) {
43
- throw `Invalid path parameter name: ${name}`;
44
- }
45
- if (__requireBoolean(__normalize(names.includes(name)))) {
46
- throw `Duplicate path parameter: ${name}`;
47
- }
48
- names.push(name);
49
- shape.push("{}");
50
- } else {
51
- shape.push(segment);
52
- }
53
- }
54
- if (__requireBoolean(wildcard)) {
55
- shape.push("*");
56
- }
57
- let key = __normalize(shape.join("/"));
58
- key = `/${key}`;
59
- return { segments: segments, wildcard: wildcard, key: key };
60
- return null;
61
- }
62
-
63
- export function matchPath(pattern, path) {
64
- __requireArity(arguments.length, [0, 1], "matchPath");
65
- pattern = __boundary(pattern);
66
- path = __boundary(path);
67
- let parts = __normalize(path.split("/"));
68
- if (__requireBoolean(__normalize(parts[0]) !== "")) {
69
- return null;
70
- }
71
- parts = __normalize(parts.slice(1));
72
- let count = pattern.segments.length;
73
- if (__requireBoolean(__normalize(pattern.wildcard))) {
74
- if (__requireBoolean(__normalize(parts.length) <= count)) {
75
- return null;
76
- }
77
- } else {
78
- if (__requireBoolean(__normalize(parts.length) !== count)) {
79
- return null;
80
- }
81
- }
82
- let parameters = { };
83
- for (const [index, segment] of __entries(__normalize(pattern.segments))) {
84
- let value = __normalize(parts[index]);
85
- if (__requireBoolean(__normalize(segment.startsWith("{")))) {
86
- if (__requireBoolean(value === "")) {
87
- return null;
88
- }
89
- let name = __normalize(segment.slice(1, __finite(-1)));
90
- parameters[name] = value;
91
- } else {
92
- if (__requireBoolean(segment !== value)) {
93
- return null;
94
- }
95
- }
96
- }
97
- return parameters;
98
- return null;
99
- }
100
-
101
- export function moreSpecific(candidate, current) {
102
- __requireArity(arguments.length, [0, 1], "moreSpecific");
103
- candidate = __boundary(candidate);
104
- current = __boundary(current);
105
- if (__requireBoolean(current === null)) {
106
- return true;
107
- }
108
- if (__requireBoolean(__normalize(candidate.wildcard) !== __normalize(current.wildcard))) {
109
- return !__requireBoolean(__normalize(candidate.wildcard));
110
- }
111
- let first = __normalize(candidate.segments);
112
- let second = __normalize(current.segments);
113
- let count = __normalize(first.length);
114
- if (__requireBoolean(__normalize(second.length) < count)) {
115
- count = __normalize(second.length);
116
- }
117
- for (const index of __range(0, count)) {
118
- let segment = __normalize(first[index]);
119
- let alternative = __normalize(second[index]);
120
- let literal = !__requireBoolean(__normalize(segment.startsWith("{")));
121
- let other = !__requireBoolean(__normalize(alternative.startsWith("{")));
122
- if (__requireBoolean(literal !== other)) {
123
- return literal;
124
- }
125
- }
126
- return __normalize(first.length) > __normalize(second.length);
127
- return null;
128
- }
129
- //# sourceMappingURL=route_pattern.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"route_pattern.mjs","sources":["route_pattern.lango"],"sourcesContent":["import js global RegExp\n\nexport routePattern(path)\nexport matchPath(pattern, path)\nexport moreSpecific(candidate, current)\n\nroutePattern(path)\n if !is_string(path) or !path.startsWith(\"/\") or path.includes(\"?\") or path.includes(\"#\") or path.includes(\"\\\\\") or path.includes(\"//\")\n throw \"Endpoint requires an absolute URL path\"\n if path.includes(\"*\") and (!path.endsWith(\"/*\") or path.indexOf(\"*\") != path.lastIndexOf(\"*\"))\n throw \"Only trailing /* wildcards are supported\"\n segments = path.split(\"/\")\n segments = segments.slice(1)\n wildcard = path.endsWith(\"/*\")\n if wildcard\n segments.pop()\n names = []\n shape = []\n identifier = new RegExp(\"^[A-Za-z_][A-Za-z0-9_]*$\")\n reserved = [\"__proto__\", \"constructor\", \"prototype\"]\n for segments as segment\n if segment.includes(\"{\") or segment.includes(\"}\")\n if !segment.startsWith(\"{\") or !segment.endsWith(\"}\")\n throw \"Path parameters must occupy an entire segment\"\n name = segment.slice(1, -1)\n if !identifier.test(name) or reserved.includes(name)\n throw \"Invalid path parameter name: ${name}\"\n if names.includes(name)\n throw \"Duplicate path parameter: ${name}\"\n names.add(name)\n shape.add(\"{}\")\n else\n shape.add(segment)\n if wildcard\n shape.add(\"*\")\n key = shape.join(\"/\")\n key = \"/${key}\"\n return {\n segments: segments\n wildcard: wildcard\n key: key\n }\n\nmatchPath(pattern, path)\n parts = path.split(\"/\")\n if parts[0] != \"\"\n return null\n parts = parts.slice(1)\n count = pattern.segments.length\n if pattern.wildcard\n if parts.length <= count\n return null\n else\n if parts.length != count\n return null\n parameters = {}\n for pattern.segments as index: segment\n value = parts[index]\n if segment.startsWith(\"{\")\n if value == \"\"\n return null\n name = segment.slice(1, -1)\n parameters[name] = value\n else\n if segment != value\n return null\n return parameters\n\nmoreSpecific(candidate, current)\n if current == null\n return true\n if candidate.wildcard != current.wildcard\n return !candidate.wildcard\n first = candidate.segments\n second = current.segments\n count = first.length\n if second.length < count\n count = second.length\n for range(count) as index\n segment = first[index]\n alternative = second[index]\n literal = !segment.startsWith(\"{\")\n other = !alternative.startsWith(\"{\")\n if literal != other\n return literal\n return first.length > second.length\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,325 +0,0 @@
1
- import { routePattern } from "./route_pattern.mjs";
2
- import { contains } from "./options.mjs";
3
- import { attributes } from "./options.mjs";
4
- import { middlewareName } from "./middleware.mjs";
5
- import { Endpoint } from "./endpoint.mjs";
6
- import { longpollOptions } from "./longpoll_options.mjs";
7
- import * as fs from "node:fs";
8
- import * as path from "node:path";
9
- import * as modules from "node:module";
10
- import {
11
- hostGlobal as __hostGlobal,
12
- xml2obj as __xml2obj,
13
- fromJson as __fromJson,
14
- toJson as __toJson,
15
- isNumber as __isNumber,
16
- isString as __isString,
17
- isBool as __isBool,
18
- isObject as __isObject,
19
- isArray as __isArray,
20
- includes as __includes,
21
- entries as __entries,
22
- add as __add,
23
- requireBoolean as __requireBoolean,
24
- normalize as __normalize,
25
- finite as __finite,
26
- boundary as __boundary,
27
- requireArity as __requireArity,
28
- } from "./.lango/runtime.mjs";
29
-
30
- export class Routes {
31
- #root;
32
- #items;
33
- #events;
34
- #middlewares;
35
- constructor(root) {
36
- __requireArity(arguments.length, [0], "constructor");
37
- root = __boundary(root);
38
- this.#root = __normalize(path.resolve(root));
39
- this.#items = [];
40
- this.#events = { };
41
- this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
42
- }
43
- endpoint(spec) {
44
- __requireArity(arguments.length, [0], "endpoint");
45
- spec = __boundary(spec);
46
- let item = new Endpoint(spec);
47
- this.#checkPath(__normalize(item.path));
48
- __add(this.#items, { path: __normalize(item.path), module: resolveModule(__normalize(item.module), __normalize(this.#root)), handler: __normalize(item.handler), auth: __normalize(item.auth), policy: __normalize(item.policy), reauthSeconds: __normalize(item.reauthSeconds), reauthExplicit: __normalize(item.reauthExplicit), legacyAuth: __normalize(item.legacyAuth), middleware: __normalize(item.middleware) });
49
- return this;
50
- return null;
51
- }
52
- #checkPath(path) {
53
- __requireArity(arguments.length, [0], "checkPath");
54
- path = __boundary(path);
55
- let pattern = routePattern(path);
56
- for (const other of __normalize(this.#items)) {
57
- if (__requireBoolean(__normalize(other.path) === path)) {
58
- throw `Duplicate endpoint: ${path}`;
59
- }
60
- let existing = routePattern(__normalize(other.path));
61
- if (__requireBoolean(__normalize(existing.key) === __normalize(pattern.key))) {
62
- throw `Conflicting endpoint pattern: ${path}`;
63
- }
64
- }
65
- return null;
66
- return null;
67
- }
68
- addMiddleware(name, spec) {
69
- __requireArity(arguments.length, [0, 1], "addMiddleware");
70
- name = __boundary(name);
71
- spec = __boundary(spec);
72
- middlewareName(name);
73
- if (__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#middlewares), name)))) {
74
- throw `Duplicate middleware: ${name}`;
75
- }
76
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
77
- throw "Middleware requires a module and exported handler";
78
- }
79
- for (const [key, value] of __entries(spec)) {
80
- if (__requireBoolean(__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler")) && __requireBoolean(key !== "validate"))) {
81
- throw `Unknown middleware option: ${key}`;
82
- }
83
- }
84
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
85
- throw "Middleware handler is required";
86
- }
87
- if (__requireBoolean(__requireBoolean(__normalize(spec.validate) !== null) && __requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.validate)))) || __requireBoolean(__normalize(spec.validate) === "")))) {
88
- throw "Middleware validate must name an exported function";
89
- }
90
- this.#middlewares[name] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler), validate: __normalize(spec.validate) };
91
- return this;
92
- return null;
93
- }
94
- middlewares() {
95
- return __fromJson(__toJson(__normalize(this.#middlewares)));
96
- return null;
97
- }
98
- longpoll(spec) {
99
- __requireArity(arguments.length, [0], "longpoll");
100
- spec = __boundary(spec);
101
- let item = longpollOptions(spec);
102
- this.#checkPath(__normalize(item.path));
103
- __add(this.#items, item);
104
- return this;
105
- return null;
106
- }
107
- event(spec) {
108
- __requireArity(arguments.length, [0], "event");
109
- spec = __boundary(spec);
110
- if (__requireBoolean(!__requireBoolean(contains(["register", "validate", "request"], __normalize(spec.name))))) {
111
- throw `Unknown event: ${__normalize(spec.name)}`;
112
- }
113
- if (__requireBoolean(__includes(__normalize(spec.name), __normalize(this.#events)))) {
114
- throw `Duplicate event: ${__normalize(spec.name)}`;
115
- }
116
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
117
- throw "Event handler is required";
118
- }
119
- this.#events[__normalize(spec.name)] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler) };
120
- return this;
121
- return null;
122
- }
123
- include(spec, auth = null, policy = null, reauthSeconds = null) {
124
- __requireArity(arguments.length, [0], "include");
125
- spec = __boundary(spec);
126
- auth = __boundary(auth);
127
- policy = __boundary(policy);
128
- reauthSeconds = __boundary(reauthSeconds);
129
- this.#includeAt(spec, __normalize(this.#root), "", [], auth, policy, reauthSeconds);
130
- return this;
131
- return null;
132
- }
133
- xml(file) {
134
- __requireArity(arguments.length, [0], "xml");
135
- file = __boundary(file);
136
- this.#readXml(__normalize(path.resolve(file)), "", __normalize(this.#root), [], true);
137
- return this;
138
- return null;
139
- }
140
- definitions() {
141
- return __fromJson(__toJson(__normalize(this.#items)));
142
- return null;
143
- }
144
- events() {
145
- return __fromJson(__toJson(__normalize(this.#events)));
146
- return null;
147
- }
148
- #includeAt(spec, directory, prefix, stack, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null) {
149
- __requireArity(arguments.length, [0, 1, 2, 3], "includeAt");
150
- spec = __boundary(spec);
151
- directory = __boundary(directory);
152
- prefix = __boundary(prefix);
153
- stack = __boundary(stack);
154
- inheritedAuth = __boundary(inheritedAuth);
155
- inheritedPolicy = __boundary(inheritedPolicy);
156
- inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
157
- if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.module)))) || __requireBoolean(__normalize(spec.module) === "")) || __requireBoolean(__normalize(spec.module.startsWith(".")))) || __requireBoolean(__normalize(spec.module.startsWith("/")))) || __requireBoolean(__normalize(spec.module.startsWith("node:"))))) {
158
- throw "Include requires an npm package name";
159
- }
160
- let mount = __normalize(spec.path);
161
- if (__requireBoolean(mount === null)) {
162
- mount = "/";
163
- }
164
- if (__requireBoolean(!__requireBoolean(__isString(mount)))) {
165
- throw "Include path must be a string";
166
- }
167
- if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(mount.includes("*"))) || __requireBoolean(__normalize(mount.includes("//")))) || __requireBoolean(__normalize(mount.includes("?")))) || __requireBoolean(__normalize(mount.includes("#")))) || __requireBoolean(__normalize(mount.includes("\\"))))) {
168
- throw "Invalid include prefix";
169
- }
170
- if (__requireBoolean(__requireBoolean(__normalize(mount.endsWith("/"))) && __requireBoolean(mount !== "/"))) {
171
- mount = __normalize(mount.slice(0, __finite(-1)));
172
- }
173
- let combined = joinPath(prefix, mount);
174
- if (__requireBoolean(combined === "/")) {
175
- combined = "";
176
- }
177
- let file = resolveModule(`${__normalize(spec.module)}/routes.xml`, directory);
178
- this.#readXml(file, combined, __normalize(path.dirname(file)), stack, false, inheritedAuth, inheritedPolicy, inheritedReauthSeconds);
179
- return null;
180
- return null;
181
- }
182
- #readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null) {
183
- __requireArity(arguments.length, [0, 1, 2, 3, 4], "readXml");
184
- file = __boundary(file);
185
- prefix = __boundary(prefix);
186
- directory = __boundary(directory);
187
- stack = __boundary(stack);
188
- allowEvents = __boundary(allowEvents);
189
- inheritedAuth = __boundary(inheritedAuth);
190
- inheritedPolicy = __boundary(inheritedPolicy);
191
- inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
192
- let real = __normalize(fs.realpathSync(file));
193
- if (__requireBoolean(__normalize(stack.includes(real)))) {
194
- throw `Route include cycle: ${real}`;
195
- }
196
- let root = __xml2obj(__normalize(fs.readFileSync(real, "utf8")));
197
- if (__requireBoolean(__normalize(root.tag()) !== "routes")) {
198
- throw `Expected routes root: ${real}`;
199
- }
200
- attributes(root, []);
201
- let nextStack = __normalize(stack.slice());
202
- __add(nextStack, real);
203
- this.#collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents, inheritedPolicy, inheritedReauthSeconds);
204
- return null;
205
- return null;
206
- }
207
- #collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents, inheritedPolicy = null, inheritedReauthSeconds = null) {
208
- __requireArity(arguments.length, [0, 1, 2, 3, 4, 5, 6], "collect");
209
- parent = __boundary(parent);
210
- prefix = __boundary(prefix);
211
- inheritedModule = __boundary(inheritedModule);
212
- inheritedAuth = __boundary(inheritedAuth);
213
- directory = __boundary(directory);
214
- stack = __boundary(stack);
215
- allowEvents = __boundary(allowEvents);
216
- inheritedPolicy = __boundary(inheritedPolicy);
217
- inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
218
- if (__requireBoolean(__normalize(__normalize(parent.innerText()).trim()) !== "")) {
219
- throw "Route XML cannot contain text";
220
- }
221
- for (const tag of __normalize(parent.children())) {
222
- let spec = __normalize(tag.attributes());
223
- let mod = __normalize(spec.module);
224
- let auth = __normalize(spec.auth);
225
- let policy = __normalize(spec.policy);
226
- let reauthSeconds = __normalize(spec.reauthSeconds);
227
- if (__requireBoolean(mod === null)) {
228
- mod = inheritedModule;
229
- }
230
- if (__requireBoolean(__requireBoolean(auth === null) && __requireBoolean(policy === null))) {
231
- auth = inheritedAuth;
232
- policy = inheritedPolicy;
233
- }
234
- if (__requireBoolean(reauthSeconds === null)) {
235
- reauthSeconds = inheritedReauthSeconds;
236
- }
237
- if (__normalize(tag.tag()) === "endpoint") {
238
- attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"]);
239
- leaf(tag);
240
- this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, policy: policy, reauthSeconds: reauthSeconds });
241
- } else if (__normalize(tag.tag()) === "longpoll") {
242
- attributes(tag, ["path", "auth", "policy", "reauthSeconds", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"]);
243
- leaf(tag);
244
- spec.path = joinPath(prefix, __normalize(spec.path));
245
- spec.auth = auth;
246
- spec.policy = policy;
247
- spec.reauthSeconds = reauthSeconds;
248
- if (__requireBoolean(__normalize(spec.fallback) !== null)) {
249
- spec.fallback = joinPath(prefix, __normalize(spec.fallback));
250
- }
251
- this.longpoll(spec);
252
- } else if (__normalize(tag.tag()) === "group") {
253
- attributes(tag, ["path", "module", "auth", "policy", "reauthSeconds"]);
254
- this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false, policy, reauthSeconds);
255
- } else if (__normalize(tag.tag()) === "include") {
256
- attributes(tag, ["module", "path"]);
257
- leaf(tag);
258
- this.#includeAt(spec, directory, prefix, stack, auth, policy, reauthSeconds);
259
- } else if (__normalize(tag.tag()) === "middleware") {
260
- attributes(tag, ["name", "module", "handler", "validate"]);
261
- leaf(tag);
262
- if (__requireBoolean(!__requireBoolean(allowEvents))) {
263
- throw "Middleware declarations are only allowed in the application's root routes.xml";
264
- }
265
- this.addMiddleware(__normalize(spec.name), { module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler), validate: __normalize(spec.validate) });
266
- } else if (__normalize(tag.tag()) === "event") {
267
- attributes(tag, ["name", "module", "handler"]);
268
- leaf(tag);
269
- if (__requireBoolean(!__requireBoolean(allowEvents))) {
270
- throw "Events are only allowed in the application's root routes.xml";
271
- }
272
- this.event({ name: __normalize(spec.name), module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
273
- } else {
274
- attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"]);
275
- leaf(tag);
276
- this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, policy: policy, reauthSeconds: reauthSeconds, middleware: __normalize(tag.tag()) });
277
- }
278
- }
279
- return null;
280
- return null;
281
- }
282
- }
283
- function resolveModule(name, directory) {
284
- __requireArity(arguments.length, [0, 1], "resolveModule");
285
- name = __boundary(name);
286
- directory = __boundary(directory);
287
- if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(name === "")) || __requireBoolean(__normalize(name.startsWith("node:"))))) {
288
- throw "Application module is required";
289
- }
290
- let resolver = __normalize(modules.createRequire(__normalize(path.join(directory, "package.json"))));
291
- let resolved = __normalize(resolver.resolve(name));
292
- if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(resolved))))) {
293
- throw `Application module must resolve to a file: ${name}`;
294
- }
295
- return resolved;
296
- return null;
297
- }
298
-
299
- function joinPath(prefix, suffix) {
300
- __requireArity(arguments.length, [0, 1], "joinPath");
301
- prefix = __boundary(prefix);
302
- suffix = __boundary(suffix);
303
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(suffix))) || __requireBoolean(!__requireBoolean(__normalize(suffix.startsWith("/")))))) {
304
- throw "Route path must begin with /";
305
- }
306
- if (__requireBoolean(__requireBoolean(prefix === "") || __requireBoolean(prefix === "/"))) {
307
- return suffix;
308
- }
309
- if (__requireBoolean(suffix === "/")) {
310
- return `${prefix}/`;
311
- }
312
- return `${prefix}${suffix}`;
313
- return null;
314
- }
315
-
316
- function leaf(tag) {
317
- __requireArity(arguments.length, [0], "leaf");
318
- tag = __boundary(tag);
319
- if (__requireBoolean(__requireBoolean(__normalize(__normalize(tag.children()).length) !== 0) || __requireBoolean(__normalize(__normalize(tag.innerText()).trim()) !== ""))) {
320
- throw `Expected an empty ${__normalize(tag.tag())} element`;
321
- }
322
- return null;
323
- return null;
324
- }
325
- //# sourceMappingURL=routes.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"routes.mjs","sources":["routes.lango"],"sourcesContent":["import \"./route_pattern\"\n routePattern(path)\nimport \"./options\"\n contains(items, value)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"node:module\" as modules\nimport \"./options\"\n attributes(tag, allowed)\nimport js global Object\nimport \"./middleware\"\n middlewareName(name)\nimport \"./endpoint\"\n class Endpoint\n\nimport \"./longpoll_options\"\n longpollOptions(spec)\n\nexport class Routes(root)\n methods\n endpoint(spec)\n addMiddleware(name, spec)\n middlewares()\n longpoll(spec)\n include(spec, auth = null, policy = null, reauthSeconds = null)\n xml(file)\n definitions()\n events()\n event(spec)\n\nRoutes(root)\n @root = path/resolve(root)\n @items = []\n @events = {}\n @middlewares = Object/create(null)\n\nRoutes:endpoint(spec)\n item = new Endpoint(spec)\n checkPath(item.path)\n @items.add({\n path: item.path\n module: resolveModule(item.module, @root)\n handler: item.handler\n auth: item.auth\n policy: item.policy\n reauthSeconds: item.reauthSeconds\n reauthExplicit: item.reauthExplicit\n legacyAuth: item.legacyAuth\n middleware: item.middleware\n })\n return @this\n\nRoutes:checkPath(path)\n pattern = routePattern(path)\n for @items as other\n if other.path == path\n throw \"Duplicate endpoint: ${path}\"\n existing = routePattern(other.path)\n if existing.key == pattern.key\n throw \"Conflicting endpoint pattern: ${path}\"\n return null\n\nRoutes:addMiddleware(name, spec)\n middlewareName(name)\n if Object/hasOwn(@middlewares, name)\n throw \"Duplicate middleware: ${name}\"\n if !is_object(spec) or is_array(spec)\n throw \"Middleware requires a module and exported handler\"\n for spec as key: value\n if key != \"module\" and key != \"handler\" and key != \"validate\"\n throw \"Unknown middleware option: ${key}\"\n if !is_string(spec.handler) or spec.handler == \"\"\n throw \"Middleware handler is required\"\n if spec.validate != null and (!is_string(spec.validate) or spec.validate == \"\")\n throw \"Middleware validate must name an exported function\"\n @middlewares[name] = {\n module: resolveModule(spec.module, @root)\n handler: spec.handler\n validate: spec.validate\n }\n return @this\n\nRoutes:middlewares()\n return from_json(to_json(@middlewares))\n\nRoutes:longpoll(spec)\n item = longpollOptions(spec)\n checkPath(item.path)\n @items.add(item)\n return @this\n\nRoutes:event(spec)\n if !contains([\"register\", \"validate\", \"request\"], spec.name)\n throw \"Unknown event: ${spec.name}\"\n if spec.name in @events\n throw \"Duplicate event: ${spec.name}\"\n if !is_string(spec.handler) or spec.handler == \"\"\n throw \"Event handler is required\"\n @events[spec.name] = {\n module: resolveModule(spec.module, @root)\n handler: spec.handler\n }\n return @this\n\nRoutes:include(spec, auth = null, policy = null, reauthSeconds = null)\n includeAt(spec, @root, \"\", [], auth, policy, reauthSeconds)\n return @this\n\nRoutes:xml(file)\n readXml(path/resolve(file), \"\", @root, [], true)\n return @this\n\nRoutes:definitions()\n return from_json(to_json(@items))\n\nRoutes:events()\n return from_json(to_json(@events))\n\nresolveModule(name, directory)\n if !is_string(name) or name == \"\" or name.startsWith(\"node:\")\n throw \"Application module is required\"\n resolver = modules/createRequire(path/join(directory, \"package.json\"))\n resolved = resolver.resolve(name)\n if !path/isAbsolute(resolved)\n throw \"Application module must resolve to a file: ${name}\"\n return resolved\n\njoinPath(prefix, suffix)\n if !is_string(suffix) or !suffix.startsWith(\"/\")\n throw \"Route path must begin with /\"\n if prefix == \"\" or prefix == \"/\"\n return suffix\n if suffix == \"/\"\n return \"${prefix}/\"\n return \"${prefix}${suffix}\"\n\nRoutes:includeAt(spec, directory, prefix, stack, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)\n if !is_string(spec.module) or spec.module == \"\" or spec.module.startsWith(\".\") or spec.module.startsWith(\"/\") or spec.module.startsWith(\"node:\")\n throw \"Include requires an npm package name\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n if !is_string(mount)\n throw \"Include path must be a string\"\n if mount.includes(\"*\") or mount.includes(\"//\") or mount.includes(\"?\") or mount.includes(\"#\") or mount.includes(\"\\\\\")\n throw \"Invalid include prefix\"\n if mount.endsWith(\"/\") and mount != \"/\"\n mount = mount.slice(0, -1)\n combined = joinPath(prefix, mount)\n if combined == \"/\"\n combined = \"\"\n file = resolveModule(\"${spec.module}/routes.xml\", directory)\n readXml(file, combined, path/dirname(file), stack, false, inheritedAuth, inheritedPolicy, inheritedReauthSeconds)\n return null\n\nRoutes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)\n real = fs/realpathSync(file)\n if stack.includes(real)\n throw \"Route include cycle: ${real}\"\n root = xml2obj(fs/readFileSync(real, \"utf8\"))\n if root.tag() != \"routes\"\n throw \"Expected routes root: ${real}\"\n attributes(root, [])\n nextStack = stack.slice()\n nextStack.add(real)\n collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents, inheritedPolicy, inheritedReauthSeconds)\n return null\n\nRoutes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents, inheritedPolicy = null, inheritedReauthSeconds = null)\n if parent.innerText().trim() != \"\"\n throw \"Route XML cannot contain text\"\n for parent.children() as tag\n spec = tag.attributes()\n mod = spec.module\n auth = spec.auth\n policy = spec.policy\n reauthSeconds = spec.reauthSeconds\n if mod == null\n mod = inheritedModule\n if auth == null and policy == null\n auth = inheritedAuth\n policy = inheritedPolicy\n if reauthSeconds == null\n reauthSeconds = inheritedReauthSeconds\n switch tag.tag()\n case \"endpoint\"\n attributes(tag, [\"path\", \"module\", \"handler\", \"auth\", \"policy\", \"reauthSeconds\"])\n leaf(tag)\n endpoint({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n auth: auth\n policy: policy\n reauthSeconds: reauthSeconds\n })\n case \"longpoll\"\n attributes(tag, [\"path\", \"auth\", \"policy\", \"reauthSeconds\", \"waitTimeoutMs\", \"retentionMs\", \"maxMessages\", \"fallback\"])\n leaf(tag)\n spec.path = joinPath(prefix, spec.path)\n spec.auth = auth\n spec.policy = policy\n spec.reauthSeconds = reauthSeconds\n if spec.fallback != null\n spec.fallback = joinPath(prefix, spec.fallback)\n longpoll(spec)\n case \"group\"\n attributes(tag, [\"path\", \"module\", \"auth\", \"policy\", \"reauthSeconds\"])\n collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false, policy, reauthSeconds)\n case \"include\"\n attributes(tag, [\"module\", \"path\"])\n leaf(tag)\n includeAt(spec, directory, prefix, stack, auth, policy, reauthSeconds)\n case \"middleware\"\n attributes(tag, [\"name\", \"module\", \"handler\", \"validate\"])\n leaf(tag)\n if !allowEvents\n throw \"Middleware declarations are only allowed in the application's root routes.xml\"\n addMiddleware(spec.name, {\n module: resolveModule(spec.module, directory)\n handler: spec.handler\n validate: spec.validate\n })\n case \"event\"\n attributes(tag, [\"name\", \"module\", \"handler\"])\n leaf(tag)\n if !allowEvents\n throw \"Events are only allowed in the application's root routes.xml\"\n event({\n name: spec.name\n module: resolveModule(spec.module, directory)\n handler: spec.handler\n })\n else\n attributes(tag, [\"path\", \"module\", \"handler\", \"auth\", \"policy\", \"reauthSeconds\"])\n leaf(tag)\n endpoint({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n auth: auth\n policy: policy\n reauthSeconds: reauthSeconds\n middleware: tag.tag()\n })\n return null\n\nleaf(tag)\n if tag.children().length != 0 or tag.innerText().trim() != \"\"\n throw \"Expected an empty ${tag.tag()} element\"\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoGA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;AAwHA;;;;;;;;;;"}