@lango-project/lango-server 0.2.3 → 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.
- package/README.md +14 -3
- package/dist/builder.mjs +26 -2
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/database_scope.mjs +11 -0
- package/dist/cli/database_scope.mjs.map +1 -1
- package/dist/cli/middleware.mjs +1 -1
- package/dist/cli/middleware.mjs.map +1 -1
- package/dist/cli/options.mjs +12 -1
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/postgres_adapter.mjs +12 -1
- package/dist/cli/postgres_adapter.mjs.map +1 -1
- package/dist/cli/routes.mjs +36 -1
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +4 -1
- package/dist/cli/server.mjs.map +1 -1
- package/dist/database_action.mjs +71 -0
- package/dist/database_action.mjs.map +1 -0
- package/dist/database_scope.mjs +11 -0
- package/dist/database_scope.mjs.map +1 -1
- package/dist/execution_failure.mjs +30 -0
- package/dist/execution_failure.mjs.map +1 -0
- package/dist/execution_identity.mjs +103 -0
- package/dist/execution_identity.mjs.map +1 -0
- package/dist/execution_log.mjs +47 -0
- package/dist/execution_log.mjs.map +1 -0
- package/dist/execution_runtime.mjs +201 -0
- package/dist/execution_runtime.mjs.map +1 -0
- package/dist/execution_worker.mjs +41 -0
- package/dist/execution_worker.mjs.map +1 -0
- package/dist/executor.mjs +384 -0
- package/dist/executor.mjs.map +1 -0
- package/dist/group.mjs +15 -0
- package/dist/group.mjs.map +1 -1
- package/dist/job_configuration.mjs +37 -0
- package/dist/job_configuration.mjs.map +1 -0
- package/dist/job_context.mjs +61 -0
- package/dist/job_context.mjs.map +1 -0
- package/dist/library.mjs +31 -0
- package/dist/library.mjs.map +1 -1
- package/dist/middleware.mjs +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs +44 -0
- package/dist/module_loader.mjs.map +1 -0
- package/dist/options.mjs +12 -1
- package/dist/options.mjs.map +1 -1
- package/dist/postgres_adapter.mjs +12 -1
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +67 -5
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs +52 -0
- package/dist/queue_provider.mjs.map +1 -0
- package/dist/routes.mjs +36 -1
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +65 -25
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +4 -1
- package/dist/server.mjs.map +1 -1
- package/docs/jobs.md +123 -0
- package/package.json +6 -4
- package/src/builder.lango +22 -0
- package/src/database_action.lango +30 -0
- package/src/database_scope.lango +7 -0
- package/src/execution_failure.lango +14 -0
- package/src/execution_identity.lango +71 -0
- package/src/execution_log.lango +28 -0
- package/src/execution_runtime.lango +135 -0
- package/src/execution_worker.lango +32 -0
- package/src/executor.lango +284 -0
- package/src/group.lango +11 -0
- package/src/job_configuration.lango +29 -0
- package/src/job_context.lango +44 -0
- package/src/library.lango +21 -0
- package/src/middleware.lango +1 -1
- package/src/module_loader.lango +25 -0
- package/src/options.lango +7 -1
- package/src/postgres_adapter.lango +12 -0
- package/src/postgres_worker.lango +54 -3
- package/src/queue_provider.lango +30 -0
- package/src/routes.lango +44 -0
- package/src/runtime.lango +45 -17
- package/src/server.lango +3 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
bindContexts as __bindContexts,
|
|
4
|
+
getClass as __getClass,
|
|
5
|
+
isKindof as __isKindof,
|
|
6
|
+
requireBoolean as __requireBoolean,
|
|
7
|
+
normalize as __normalize,
|
|
8
|
+
boundary as __boundary,
|
|
9
|
+
requireArity as __requireArity,
|
|
10
|
+
withArity as __withArity,
|
|
11
|
+
} from "./.lango/runtime.mjs";
|
|
12
|
+
|
|
13
|
+
export class ModuleLoader {
|
|
14
|
+
#modules;
|
|
15
|
+
constructor() {
|
|
16
|
+
this.#modules = __normalize(__hostGlobal(["Object"]).create(null));
|
|
17
|
+
}
|
|
18
|
+
target(spec) {
|
|
19
|
+
__requireArity(arguments.length, [0], "target");
|
|
20
|
+
spec = __boundary(spec);
|
|
21
|
+
let file = __normalize(spec.module);
|
|
22
|
+
if (__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#modules), file)))) {
|
|
23
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(exported(__normalize(this.#modules[file]), __normalize(spec.handler))));
|
|
24
|
+
}
|
|
25
|
+
return import(file).then(__withArity(__bindContexts((mod) => {
|
|
26
|
+
mod = __boundary(mod);
|
|
27
|
+
this.#modules[file] = mod;
|
|
28
|
+
return exported(mod, __normalize(spec.handler));
|
|
29
|
+
return null;
|
|
30
|
+
}), [0], "closure"));
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function exported(mod, name) {
|
|
35
|
+
__requireArity(arguments.length, [0, 1], "exported");
|
|
36
|
+
mod = __boundary(mod);
|
|
37
|
+
name = __boundary(name);
|
|
38
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(mod, name)))) || __requireBoolean(!__requireBoolean(__isKindof(__normalize(mod[name]), Function))))) {
|
|
39
|
+
throw `Handler is not exported: ${name}`;
|
|
40
|
+
}
|
|
41
|
+
return __normalize(mod[name]);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=module_loader.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module_loader.mjs","sources":["module_loader.lango"],"sourcesContent":["import js global Object\nimport js global Promise\n\nexport class ModuleLoader()\n methods\n target(spec)\n\nModuleLoader()\n @modules = Object/create(null)\n\nModuleLoader:target(spec)\n file = spec.module\n if Object/hasOwn(@modules, file)\n return Promise/resolve(exported(@modules[file], spec.handler))\n return chain import_module(file)\n then(mod)\n capture file\n capture spec\n @modules[file] = mod\n return exported(mod, spec.handler)\n\nexported(mod, name)\n if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)\n throw \"Handler is not exported: ${name}\"\n return mod[name]\n"],"names":[],"mappings":";;;;;;;;;;;;AAGA;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;"}
|
package/dist/options.mjs
CHANGED
|
@@ -106,6 +106,14 @@ export function options(root, supplied = null) {
|
|
|
106
106
|
export function deployment(directory) {
|
|
107
107
|
__requireArity(arguments.length, [0], "deployment");
|
|
108
108
|
directory = __boundary(directory);
|
|
109
|
+
return options(directory, readDeployment(directory));
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function readDeployment(directory, credentials = true) {
|
|
114
|
+
__requireArity(arguments.length, [0], "readDeployment");
|
|
115
|
+
directory = __boundary(directory);
|
|
116
|
+
credentials = __boundary(credentials);
|
|
109
117
|
let file = __normalize(path.join(directory, "deploy.xml"));
|
|
110
118
|
let root = __xml2obj(__normalize(fs.readFileSync(file, "utf8")));
|
|
111
119
|
if (__requireBoolean(__normalize(root.tag()) !== "deploy")) {
|
|
@@ -160,6 +168,9 @@ export function deployment(directory) {
|
|
|
160
168
|
continue;
|
|
161
169
|
} else if (name === "jwt") {
|
|
162
170
|
attributes(tag, ["issuer", "audience", "ttlSeconds", "keyEnv", "keysFile", "activeKeyId"]);
|
|
171
|
+
if (__requireBoolean(!__requireBoolean(credentials))) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
163
174
|
} else {
|
|
164
175
|
throw `Unknown deployment section: ${name}`;
|
|
165
176
|
}
|
|
@@ -189,7 +200,7 @@ export function deployment(directory) {
|
|
|
189
200
|
}
|
|
190
201
|
}
|
|
191
202
|
}
|
|
192
|
-
return
|
|
203
|
+
return settings;
|
|
193
204
|
return null;
|
|
194
205
|
}
|
|
195
206
|
|
package/dist/options.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.mjs","sources":["options.lango"],"sourcesContent":["import \"./policy\"\n policyRegistry(spec)\nimport \"./client_identity\"\n clientOptions(spec, database, origin)\nimport \"./origin\"\n originOptions(spec)\nimport \"./longpoll_options\"\n redisOptions(spec)\nimport \"./database_options\"\n databaseOptions(root, spec, workers)\nimport \"./static_files\"\n staticOptions(root, spec)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js global Object\nimport js global Number\nimport js global process\n\nexport contains(items, value)\nexport options(root, supplied = null)\nexport deployment(directory)\nexport attributes(tag, allowed)\n\nattributes(tag, allowed)\n for tag.attributes() as key: value\n if !allowed.includes(key)\n throw \"Unknown ${tag.tag()} attribute: ${key}\"\n return null\n\noptions(root, supplied = null)\n result = {\n root: path/resolve(root)\n host: \"127.0.0.1\"\n port: 3000\n workers: 2\n maxQueue: 64\n bodyLimit: 2097152\n bodyTimeoutMs: 30000\n requestTimeoutMs: 30000\n eventTimeoutMs: 5000\n startupTimeoutMs: 30000\n shutdownTimeoutMs: 30000\n issuer: \"lango-server\"\n audience: \"lango-client\"\n ttlSeconds: 900\n key: null\n keys: {}\n activeKeyId: \"current\"\n requestEvent: false\n database: null\n redis: null\n static: null\n origin: null\n client: null\n policies: null\n }\n if supplied != null\n for supplied as key: value\n if !Object/hasOwn(result, key) or key == \"root\"\n throw \"Unknown server option: ${key}\"\n result[key] = value\n if !is_string(result.host) or result.host == \"\"\n throw \"Host must be a nonempty string\"\n for [\"port\", \"workers\", \"maxQueue\", \"bodyLimit\", \"bodyTimeoutMs\", \"requestTimeoutMs\", \"eventTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\", \"ttlSeconds\"] as key\n value = result[key]\n if !is_number(value) or !Number/isSafeInteger(value) or value < 0\n throw \"Invalid server option: ${key}\"\n if key != \"port\" and key != \"maxQueue\" and value == 0\n throw \"Server option must be positive: ${key}\"\n if key.endsWith(\"Ms\") and value > 2147483647\n throw \"Timeout exceeds timer limit: ${key}\"\n if result.port > 65535 or result.workers > 128 or result.ttlSeconds > 86400\n throw \"Server option exceeds supported limit\"\n if !is_bool(result.requestEvent)\n throw \"requestEvent must be boolean\"\n for [\"issuer\", \"audience\", \"activeKeyId\"] as key\n if !is_string(result[key]) or result[key] == \"\"\n throw \"Missing JWT option: ${key}\"\n if !is_object(result.keys) or is_array(result.keys)\n throw \"JWT keys must be a map\"\n keys = Object/assign(Object/create(null), result.keys)\n if result.key != null\n keys[result.activeKeyId] = result.key\n if !Object/hasOwn(keys, result.activeKeyId)\n throw \"JWT signing key is required\"\n for keys as id: secret\n if !is_string(secret) or secret.length < 32\n throw \"JWT keys must contain at least 32 characters\"\n result.keys = keys\n result.key = null\n result.policies = policyRegistry(result.policies)\n result.database = databaseOptions(result.root, result.database, result.workers)\n result.redis = redisOptions(result.redis)\n result.static = staticOptions(result.root, result.static)\n result.origin = originOptions(result.origin)\n result.client = clientOptions(result.client, result.database, result.origin)\n return result\n\ndeployment(directory)\n file = path/join(directory, \"deploy.xml\")\n root = xml2obj(fs/readFileSync(file, \"utf8\"))\n if root.tag() != \"deploy\"\n throw \"Expected deploy root\"\n attributes(root, [])\n settings = {}\n seen = []\n for root.children() as tag\n name = tag.tag()\n if seen.includes(name) or (name != \"policies\" and tag.children().length != 0) or tag.innerText().trim() != \"\"\n throw \"Duplicate or nonempty deployment section: ${name}\"\n seen.add(name)\n switch name\n case \"policies\"\n attributes(tag, [\"reauthSeconds\"])\n items = []\n for tag.children() as child\n if child.tag() != \"policy\" or child.children().length != 0 or child.innerText().trim() != \"\"\n throw \"Policies may contain only empty policy elements\"\n attributes(child, [\"name\", \"reauthSeconds\"])\n items.add(child.attributes())\n settings.policies = {\n reauthSeconds: tag.reauthSeconds\n items: items\n }\n continue\n case \"client\"\n attributes(tag, [\"maxAgeSeconds\"])\n settings.client = tag.attributes()\n continue\n case \"origin\"\n attributes(tag, [\"url\", \"urlEnv\", \"required\"])\n settings.origin = tag.attributes()\n continue\n case \"static\"\n attributes(tag, [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"])\n settings.static = tag.attributes()\n continue\n case \"http\"\n attributes(tag, [\"host\", \"port\", \"bodyLimit\", \"bodyTimeoutMs\"])\n case \"workers\"\n attributes(tag, [\"workers\", \"maxQueue\", \"requestTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\"])\n case \"events\"\n attributes(tag, [\"requestEvent\", \"eventTimeoutMs\"])\n case \"redis\"\n attributes(tag, [\"connectionEnv\", \"namespace\", \"maxPendingPolls\"])\n settings.redis = tag.attributes()\n continue\n case \"database\"\n attributes(tag, [\"type\", \"file\", \"connectionEnv\", \"busyTimeoutMs\", \"queryTimeoutMs\", \"connectTimeoutSeconds\"])\n settings.database = tag.attributes()\n continue\n case \"jwt\"\n attributes(tag, [\"issuer\", \"audience\", \"ttlSeconds\", \"keyEnv\", \"keysFile\", \"activeKeyId\"])\n else\n throw \"Unknown deployment section: ${name}\"\n for tag.attributes() as key: value\n if key == \"keyEnv\"\n settings.key = process.env[value]\n if settings.key == null\n throw \"JWT key environment variable is missing: ${value}\"\n else\n if key == \"keysFile\"\n settings.keys = from_json(fs/readFileSync(path/resolve(directory, value), \"utf8\"))\n else\n if contains([\"host\", \"issuer\", \"audience\", \"activeKeyId\"], key)\n settings[key] = value\n else\n if key == \"requestEvent\"\n if value != \"true\" and value != \"false\"\n throw \"requestEvent must be true or false\"\n settings[key] = value == \"true\"\n else\n settings[key] = Number(value)\n return
|
|
1
|
+
{"version":3,"file":"options.mjs","sources":["options.lango"],"sourcesContent":["import \"./policy\"\n policyRegistry(spec)\nimport \"./client_identity\"\n clientOptions(spec, database, origin)\nimport \"./origin\"\n originOptions(spec)\nimport \"./longpoll_options\"\n redisOptions(spec)\nimport \"./database_options\"\n databaseOptions(root, spec, workers)\nimport \"./static_files\"\n staticOptions(root, spec)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js global Object\nimport js global Number\nimport js global process\n\nexport contains(items, value)\nexport options(root, supplied = null)\nexport deployment(directory)\nexport attributes(tag, allowed)\nexport readDeployment(directory, credentials = true)\n\nattributes(tag, allowed)\n for tag.attributes() as key: value\n if !allowed.includes(key)\n throw \"Unknown ${tag.tag()} attribute: ${key}\"\n return null\n\noptions(root, supplied = null)\n result = {\n root: path/resolve(root)\n host: \"127.0.0.1\"\n port: 3000\n workers: 2\n maxQueue: 64\n bodyLimit: 2097152\n bodyTimeoutMs: 30000\n requestTimeoutMs: 30000\n eventTimeoutMs: 5000\n startupTimeoutMs: 30000\n shutdownTimeoutMs: 30000\n issuer: \"lango-server\"\n audience: \"lango-client\"\n ttlSeconds: 900\n key: null\n keys: {}\n activeKeyId: \"current\"\n requestEvent: false\n database: null\n redis: null\n static: null\n origin: null\n client: null\n policies: null\n }\n if supplied != null\n for supplied as key: value\n if !Object/hasOwn(result, key) or key == \"root\"\n throw \"Unknown server option: ${key}\"\n result[key] = value\n if !is_string(result.host) or result.host == \"\"\n throw \"Host must be a nonempty string\"\n for [\"port\", \"workers\", \"maxQueue\", \"bodyLimit\", \"bodyTimeoutMs\", \"requestTimeoutMs\", \"eventTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\", \"ttlSeconds\"] as key\n value = result[key]\n if !is_number(value) or !Number/isSafeInteger(value) or value < 0\n throw \"Invalid server option: ${key}\"\n if key != \"port\" and key != \"maxQueue\" and value == 0\n throw \"Server option must be positive: ${key}\"\n if key.endsWith(\"Ms\") and value > 2147483647\n throw \"Timeout exceeds timer limit: ${key}\"\n if result.port > 65535 or result.workers > 128 or result.ttlSeconds > 86400\n throw \"Server option exceeds supported limit\"\n if !is_bool(result.requestEvent)\n throw \"requestEvent must be boolean\"\n for [\"issuer\", \"audience\", \"activeKeyId\"] as key\n if !is_string(result[key]) or result[key] == \"\"\n throw \"Missing JWT option: ${key}\"\n if !is_object(result.keys) or is_array(result.keys)\n throw \"JWT keys must be a map\"\n keys = Object/assign(Object/create(null), result.keys)\n if result.key != null\n keys[result.activeKeyId] = result.key\n if !Object/hasOwn(keys, result.activeKeyId)\n throw \"JWT signing key is required\"\n for keys as id: secret\n if !is_string(secret) or secret.length < 32\n throw \"JWT keys must contain at least 32 characters\"\n result.keys = keys\n result.key = null\n result.policies = policyRegistry(result.policies)\n result.database = databaseOptions(result.root, result.database, result.workers)\n result.redis = redisOptions(result.redis)\n result.static = staticOptions(result.root, result.static)\n result.origin = originOptions(result.origin)\n result.client = clientOptions(result.client, result.database, result.origin)\n return result\n\ndeployment(directory)\n return options(directory, readDeployment(directory))\n\nreadDeployment(directory, credentials = true)\n file = path/join(directory, \"deploy.xml\")\n root = xml2obj(fs/readFileSync(file, \"utf8\"))\n if root.tag() != \"deploy\"\n throw \"Expected deploy root\"\n attributes(root, [])\n settings = {}\n seen = []\n for root.children() as tag\n name = tag.tag()\n if seen.includes(name) or (name != \"policies\" and tag.children().length != 0) or tag.innerText().trim() != \"\"\n throw \"Duplicate or nonempty deployment section: ${name}\"\n seen.add(name)\n switch name\n case \"policies\"\n attributes(tag, [\"reauthSeconds\"])\n items = []\n for tag.children() as child\n if child.tag() != \"policy\" or child.children().length != 0 or child.innerText().trim() != \"\"\n throw \"Policies may contain only empty policy elements\"\n attributes(child, [\"name\", \"reauthSeconds\"])\n items.add(child.attributes())\n settings.policies = {\n reauthSeconds: tag.reauthSeconds\n items: items\n }\n continue\n case \"client\"\n attributes(tag, [\"maxAgeSeconds\"])\n settings.client = tag.attributes()\n continue\n case \"origin\"\n attributes(tag, [\"url\", \"urlEnv\", \"required\"])\n settings.origin = tag.attributes()\n continue\n case \"static\"\n attributes(tag, [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"])\n settings.static = tag.attributes()\n continue\n case \"http\"\n attributes(tag, [\"host\", \"port\", \"bodyLimit\", \"bodyTimeoutMs\"])\n case \"workers\"\n attributes(tag, [\"workers\", \"maxQueue\", \"requestTimeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\"])\n case \"events\"\n attributes(tag, [\"requestEvent\", \"eventTimeoutMs\"])\n case \"redis\"\n attributes(tag, [\"connectionEnv\", \"namespace\", \"maxPendingPolls\"])\n settings.redis = tag.attributes()\n continue\n case \"database\"\n attributes(tag, [\"type\", \"file\", \"connectionEnv\", \"busyTimeoutMs\", \"queryTimeoutMs\", \"connectTimeoutSeconds\"])\n settings.database = tag.attributes()\n continue\n case \"jwt\"\n attributes(tag, [\"issuer\", \"audience\", \"ttlSeconds\", \"keyEnv\", \"keysFile\", \"activeKeyId\"])\n if !credentials\n continue\n else\n throw \"Unknown deployment section: ${name}\"\n for tag.attributes() as key: value\n if key == \"keyEnv\"\n settings.key = process.env[value]\n if settings.key == null\n throw \"JWT key environment variable is missing: ${value}\"\n else\n if key == \"keysFile\"\n settings.keys = from_json(fs/readFileSync(path/resolve(directory, value), \"utf8\"))\n else\n if contains([\"host\", \"issuer\", \"audience\", \"activeKeyId\"], key)\n settings[key] = value\n else\n if key == \"requestEvent\"\n if value != \"true\" and value != \"false\"\n throw \"requestEvent must be true or false\"\n settings[key] = value == \"true\"\n else\n settings[key] = Number(value)\n return settings\n\ncontains(items, value)\n return items.includes(value)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqEA;;;;;;;AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+EA;;;;;;;;"}
|
|
@@ -42,7 +42,7 @@ export class PostgresAdapter {
|
|
|
42
42
|
this.#port = __normalize(channel.port1);
|
|
43
43
|
let resolver = __normalize(modules.createRequire(__normalize(path.join(root, "package.json"))));
|
|
44
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)] }]));
|
|
45
|
+
this.#worker = __normalize(__hostGlobal(["Reflect"]).construct(__normalize(mod.Worker), [__normalize(path.join(__normalize(path.dirname(entry)), "postgres_worker.mjs")), { workerData: { root: root, settings: settings, port: __normalize(channel.port2), signal: __normalize(this.#signal) }, transferList: [__normalize(channel.port2)] }]));
|
|
46
46
|
let worker = __normalize(this.#worker);
|
|
47
47
|
__normalize(worker.on("error", __withArity(__bindContexts((error) => {
|
|
48
48
|
error = __boundary(error);
|
|
@@ -83,6 +83,9 @@ export class PostgresAdapter {
|
|
|
83
83
|
if (__requireBoolean(__normalize(result.failed))) {
|
|
84
84
|
this.#broken = __normalize(result.dead);
|
|
85
85
|
let error = new (__hostGlobal(["Error"]))("PostgreSQL query failed");
|
|
86
|
+
if (__requireBoolean(__normalize(result.detail) !== null)) {
|
|
87
|
+
error = new (__hostGlobal(["Error"]))(`PostgreSQL extension failed: ${__normalize(result.detail)}`);
|
|
88
|
+
}
|
|
86
89
|
error.code = __normalize(result.code);
|
|
87
90
|
error.position = __normalize(result.position);
|
|
88
91
|
error.transactionUnusable = __normalize(this.#state) === "failed";
|
|
@@ -98,6 +101,14 @@ export class PostgresAdapter {
|
|
|
98
101
|
return this.#exchange({ action: "query", sql: sql, parameters: parameters }, __normalize(this.#timeout));
|
|
99
102
|
return null;
|
|
100
103
|
}
|
|
104
|
+
invoke(spec, input) {
|
|
105
|
+
__requireArity(arguments.length, [0, 1], "invoke");
|
|
106
|
+
spec = __boundary(spec);
|
|
107
|
+
input = __boundary(input);
|
|
108
|
+
let result = this.#exchange({ action: "invoke", spec: spec, input: input }, __normalize(this.#timeout));
|
|
109
|
+
return __normalize(result.value);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
101
112
|
begin() {
|
|
102
113
|
if (__requireBoolean(__requireBoolean(__normalize(this.#depth) === 0) && __requireBoolean(__normalize(this.#state) === null))) {
|
|
103
114
|
this.execute("BEGIN", []);
|
|
@@ -1 +1 @@
|
|
|
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
|
+
{"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 invoke(spec, input)\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 root: root\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 if result.detail != null\n error = new Error(\"PostgreSQL extension failed: ${result.detail}\")\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:invoke(spec, input)\n result = exchange({\n action: \"invoke\"\n spec: spec\n input: input\n }, @timeout)\n return result.value\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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/postgres_worker.mjs
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
+
import { ModuleLoader } from "./module_loader.mjs";
|
|
2
|
+
import { errorDetails } from "./error_details.mjs";
|
|
1
3
|
import * as pg from "pg";
|
|
2
4
|
import * as threads from "node:worker_threads";
|
|
5
|
+
import * as modules from "node:module";
|
|
6
|
+
import * as path from "node:path";
|
|
3
7
|
import {
|
|
4
8
|
hostGlobal as __hostGlobal,
|
|
5
9
|
bindContexts as __bindContexts,
|
|
10
|
+
isNumber as __isNumber,
|
|
11
|
+
isString as __isString,
|
|
12
|
+
isBool as __isBool,
|
|
13
|
+
isObject as __isObject,
|
|
14
|
+
isArray as __isArray,
|
|
6
15
|
requireBoolean as __requireBoolean,
|
|
7
16
|
normalize as __normalize,
|
|
8
17
|
finite as __finite,
|
|
18
|
+
call as __call,
|
|
9
19
|
boundary as __boundary,
|
|
10
20
|
requireArity as __requireArity,
|
|
11
21
|
withArity as __withArity,
|
|
@@ -13,6 +23,8 @@ import {
|
|
|
13
23
|
|
|
14
24
|
let state = initialState();
|
|
15
25
|
|
|
26
|
+
let loader = new ModuleLoader();
|
|
27
|
+
|
|
16
28
|
let installed = install();
|
|
17
29
|
function install() {
|
|
18
30
|
let port = threads.workerData.port;
|
|
@@ -29,17 +41,25 @@ function install() {
|
|
|
29
41
|
return null;
|
|
30
42
|
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
31
43
|
error = __boundary(error);
|
|
32
|
-
|
|
44
|
+
let sqlError = __requireBoolean(__isString(__normalize(error.code))) && __requireBoolean(error.code.length === 5);
|
|
45
|
+
if (__requireBoolean(__requireBoolean(__normalize(state.transaction) !== null) && __requireBoolean(__requireBoolean(__requireBoolean(__normalize(task.action) !== "invoke") || __requireBoolean(sqlError)) || __requireBoolean(__normalize(state.dead))))) {
|
|
33
46
|
state.transaction = "failed";
|
|
34
47
|
}
|
|
35
|
-
if (__requireBoolean(__requireBoolean(
|
|
48
|
+
if (__requireBoolean(__requireBoolean(__normalize(task.action) !== "invoke") && __requireBoolean(!__requireBoolean(sqlError)))) {
|
|
49
|
+
state.dead = true;
|
|
50
|
+
}
|
|
51
|
+
if (__requireBoolean(__requireBoolean(sqlError) && __requireBoolean(__normalize(error.code.startsWith("08"))))) {
|
|
36
52
|
state.dead = true;
|
|
37
53
|
}
|
|
38
54
|
let position = null;
|
|
55
|
+
let detail = null;
|
|
56
|
+
if (__requireBoolean(__requireBoolean(__normalize(task.action) === "invoke") && __requireBoolean(!__requireBoolean(sqlError)))) {
|
|
57
|
+
detail = errorDetails(error);
|
|
58
|
+
}
|
|
39
59
|
if (__requireBoolean(__normalize(error.position) !== null)) {
|
|
40
60
|
position = __normalize(__hostGlobal(["Number"]).parseInt(__normalize(error.position), 10));
|
|
41
61
|
}
|
|
42
|
-
reply({ failed: true, dead: __normalize(state.dead), code: __normalize(error.code), position: position, state: __normalize(state.transaction) });
|
|
62
|
+
reply({ failed: true, dead: __normalize(state.dead), code: __normalize(error.code), position: position, detail: detail, state: __normalize(state.transaction) });
|
|
43
63
|
return null;
|
|
44
64
|
return null;
|
|
45
65
|
}), [0], "closure"));
|
|
@@ -81,6 +101,8 @@ function execute(task) {
|
|
|
81
101
|
return success(__normalize(result.rows), changes);
|
|
82
102
|
return null;
|
|
83
103
|
}), [0], "closure"));
|
|
104
|
+
} else if (__normalize(task.action) === "invoke") {
|
|
105
|
+
return invoke(task);
|
|
84
106
|
} else if (__normalize(task.action) === "close") {
|
|
85
107
|
return __normalize(state.client.end()).then(__withArity(__bindContexts((ignored) => {
|
|
86
108
|
ignored = __boundary(ignored);
|
|
@@ -93,11 +115,51 @@ function execute(task) {
|
|
|
93
115
|
return null;
|
|
94
116
|
}
|
|
95
117
|
|
|
96
|
-
function
|
|
118
|
+
function invoke(task) {
|
|
119
|
+
__requireArity(arguments.length, [0], "invoke");
|
|
120
|
+
task = __boundary(task);
|
|
121
|
+
let spec = __normalize(task.spec);
|
|
122
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(spec === null) || __requireBoolean(!__requireBoolean(__isString(__normalize(spec.module))))) || __requireBoolean(__normalize(spec.module.startsWith("node:")))) || __requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))))) {
|
|
123
|
+
throw "Database extension requires an application module and exported handler";
|
|
124
|
+
}
|
|
125
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(threads.workerData.root, "package.json"))));
|
|
126
|
+
let file = __normalize(resolver.resolve(__normalize(spec.module)));
|
|
127
|
+
if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(file))))) {
|
|
128
|
+
throw "Database extension must resolve to an application file";
|
|
129
|
+
}
|
|
130
|
+
return __normalize(loader.target({ module: file, handler: __normalize(spec.handler) })).then(__withArity(__bindContexts((handler) => {
|
|
131
|
+
handler = __boundary(handler);
|
|
132
|
+
let client = __normalize(state.client);
|
|
133
|
+
let executeSql = __withArity(__bindContexts((sql, parameters) => {
|
|
134
|
+
sql = __boundary(sql);
|
|
135
|
+
parameters = __boundary(parameters);
|
|
136
|
+
return __normalize(client.query(sql, parameters)).catch(__withArity(__bindContexts((error) => {
|
|
137
|
+
error = __boundary(error);
|
|
138
|
+
if (__requireBoolean(__normalize(state.transaction) !== null)) {
|
|
139
|
+
state.transaction = "failed";
|
|
140
|
+
}
|
|
141
|
+
throw error;
|
|
142
|
+
return null;
|
|
143
|
+
}), [0], "closure"));
|
|
144
|
+
return null;
|
|
145
|
+
}), [0, 1], "closure");
|
|
146
|
+
let adapter = { executeSql: executeSql };
|
|
147
|
+
return __call(handler, adapter, __normalize(task.input));
|
|
148
|
+
return null;
|
|
149
|
+
}), [0], "closure")).then(__withArity(__bindContexts((value) => {
|
|
150
|
+
value = __boundary(value);
|
|
151
|
+
return success([], null, value);
|
|
152
|
+
return null;
|
|
153
|
+
}), [0], "closure"));
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function success(rows, changes, value = null) {
|
|
97
158
|
__requireArity(arguments.length, [0, 1], "success");
|
|
98
159
|
rows = __boundary(rows);
|
|
99
160
|
changes = __boundary(changes);
|
|
100
|
-
|
|
161
|
+
value = __boundary(value);
|
|
162
|
+
return { failed: false, dead: false, state: __normalize(state.transaction), rows: rows, changes: changes, lastInsertRowid: null, value: value };
|
|
101
163
|
return null;
|
|
102
164
|
}
|
|
103
165
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres_worker.mjs","sources":["postgres_worker.lango"],"sourcesContent":["import js \"pg\" as pg\nimport js \"node:worker_threads\" as threads\nimport js global Atomics\nimport js global Math\nimport js global Number\nimport js global Promise\n\n\nglobal state = initialState()\nglobal installed = install()\n\ninstall()\n port = threads.workerData.port\n port.on(\"message\", (task)\n return chain Promise/resolve(null)\n then(ignored)\n capture task\n return execute(task)\n then(result)\n reply(result)\n return null\n catch(error)\n global state\n if state.transaction != null\n state.transaction = \"failed\"\n if
|
|
1
|
+
{"version":3,"file":"postgres_worker.mjs","sources":["postgres_worker.lango"],"sourcesContent":["import js \"pg\" as pg\nimport js \"node:worker_threads\" as threads\nimport js \"node:module\" as modules\nimport js \"node:path\" as path\nimport \"./module_loader\"\n class ModuleLoader\nimport \"./error_details\"\n errorDetails(error)\nimport js global Atomics\nimport js global Math\nimport js global Number\nimport js global Promise\n\n\nglobal state = initialState()\nglobal loader = new ModuleLoader()\nglobal installed = install()\n\ninstall()\n port = threads.workerData.port\n port.on(\"message\", (task)\n return chain Promise/resolve(null)\n then(ignored)\n capture task\n return execute(task)\n then(result)\n reply(result)\n return null\n catch(error)\n capture task\n global state\n sqlError = is_string(error.code) and error.code.length == 5\n if state.transaction != null and (task.action != \"invoke\" or sqlError or state.dead)\n state.transaction = \"failed\"\n if task.action != \"invoke\" and !sqlError\n state.dead = true\n if sqlError and error.code.startsWith(\"08\")\n state.dead = true\n position = null\n detail = null\n if task.action == \"invoke\" and !sqlError\n detail = errorDetails(error)\n if error.position != null\n position = Number/parseInt(error.position, 10)\n reply({\n failed: true\n dead: state.dead\n code: error.code\n position: position\n detail: detail\n state: state.transaction\n })\n return null\n )\n return null\n\nexecute(task)\n global state\n settings = threads.workerData.settings\n switch task.action\n case \"connect\"\n state.client = new pg/Client({\n connectionString: settings.connection\n connectionTimeoutMillis: Math/min(2147483647, settings.connectTimeoutSeconds * 1000)\n statement_timeout: settings.queryTimeoutMs\n application_name: \"lango-server\"\n })\n client = state.client\n client.on(\"error\", (error)\n global state\n state.dead = true\n )\n return chain client.connect()\n then(ignored)\n return success([], null)\n case \"query\"\n return chain state.client.query({\n text: task.sql\n values: task.parameters\n queryMode: \"extended\"\n })\n then(result)\n global state\n switch result.command\n case \"BEGIN\"\n state.transaction = \"active\"\n case \"COMMIT\"\n case \"ROLLBACK\"\n state.transaction = null\n changes = null\n switch result.command\n case \"INSERT\"\n case \"UPDATE\"\n case \"DELETE\"\n case \"MERGE\"\n changes = result.rowCount\n return success(result.rows, changes)\n case \"invoke\"\n return invoke(task)\n case \"close\"\n return chain state.client.end()\n then(ignored)\n return success([], null)\n else\n throw \"Unknown PostgreSQL operation\"\n\ninvoke(task)\n global state\n global loader\n spec = task.spec\n if spec == null or !is_string(spec.module) or spec.module.startsWith(\"node:\") or !is_string(spec.handler)\n throw \"Database extension requires an application module and exported handler\"\n resolver = modules/createRequire(path/join(threads.workerData.root, \"package.json\"))\n file = resolver.resolve(spec.module)\n if !path/isAbsolute(file)\n throw \"Database extension must resolve to an application file\"\n return chain loader.target({\n module: file\n handler: spec.handler\n })\n then(handler)\n capture task\n global state\n client = state.client\n executeSql = (sql, parameters)\n capture client\n return chain client.query(sql, parameters)\n catch(error)\n global state\n if state.transaction != null\n state.transaction = \"failed\"\n throw error\n adapter = {\n executeSql: executeSql\n }\n return handler(adapter, task.input)\n then(value)\n return success([], null, value)\n\nsuccess(rows, changes, value = null)\n global state\n return {\n failed: false\n dead: false\n state: state.transaction\n rows: rows\n changes: changes\n lastInsertRowid: null\n value: value\n }\n\nreply(result)\n port = threads.workerData.port\n signal = threads.workerData.signal\n port.postMessage(result)\n Atomics/store(signal, 0, 1)\n Atomics/notify(signal, 0)\n return null\n\ninitialState()\n return {\n client: null\n transaction: null\n dead: false\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAcA;;AACA;;AACA;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA;;;;;;;;;AAYA;;;;;;;;;;;;AAQA;;;;;"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as modules from "node:module";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
fromJson as __fromJson,
|
|
5
|
+
toJson as __toJson,
|
|
6
|
+
isNumber as __isNumber,
|
|
7
|
+
isString as __isString,
|
|
8
|
+
isBool as __isBool,
|
|
9
|
+
isObject as __isObject,
|
|
10
|
+
isArray as __isArray,
|
|
11
|
+
entries as __entries,
|
|
12
|
+
requireBoolean as __requireBoolean,
|
|
13
|
+
normalize as __normalize,
|
|
14
|
+
boundary as __boundary,
|
|
15
|
+
requireArity as __requireArity,
|
|
16
|
+
} from "./.lango/runtime.mjs";
|
|
17
|
+
|
|
18
|
+
export function queueProvider(root, spec) {
|
|
19
|
+
__requireArity(arguments.length, [0, 1], "queueProvider");
|
|
20
|
+
root = __boundary(root);
|
|
21
|
+
spec = __boundary(spec);
|
|
22
|
+
if (__requireBoolean(spec === null)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
26
|
+
throw "Queue provider must be a map";
|
|
27
|
+
}
|
|
28
|
+
let allowed = ["module", "handler", "options"];
|
|
29
|
+
for (const [key, value] of __entries(spec)) {
|
|
30
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
31
|
+
throw `Unknown queue provider option: ${key}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
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) === ""))) {
|
|
35
|
+
throw "Queue provider requires a module and exported handler";
|
|
36
|
+
}
|
|
37
|
+
let settings = __normalize(spec.options);
|
|
38
|
+
if (__requireBoolean(settings === null)) {
|
|
39
|
+
settings = { };
|
|
40
|
+
}
|
|
41
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(settings))) || __requireBoolean(__isArray(settings)))) {
|
|
42
|
+
throw "Queue provider options must be a map";
|
|
43
|
+
}
|
|
44
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(root, "package.json"))));
|
|
45
|
+
let file = __normalize(resolver.resolve(__normalize(spec.module)));
|
|
46
|
+
if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(file))))) {
|
|
47
|
+
throw "Queue provider must resolve to an application file";
|
|
48
|
+
}
|
|
49
|
+
return { module: file, handler: __normalize(spec.handler), options: __fromJson(__toJson(settings)) };
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=queue_provider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue_provider.mjs","sources":["queue_provider.lango"],"sourcesContent":["import js \"node:module\" as modules\nimport js \"node:path\" as path\n\nexport queueProvider(root, spec)\n\nqueueProvider(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Queue provider must be a map\"\n allowed = [\"module\", \"handler\", \"options\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown queue 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 \"Queue provider requires a module and exported handler\"\n settings = spec.options\n if settings == null\n settings = {}\n if !is_object(settings) or is_array(settings)\n throw \"Queue provider options must be a map\"\n resolver = modules/createRequire(path/join(root, \"package.json\"))\n file = resolver.resolve(spec.module)\n if !path/isAbsolute(file)\n throw \"Queue provider must resolve to an application file\"\n return {\n module: file\n handler: spec.handler\n options: from_json(to_json(settings))\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/routes.mjs
CHANGED
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
export class Routes {
|
|
31
31
|
#root;
|
|
32
32
|
#items;
|
|
33
|
+
#jobs;
|
|
33
34
|
#events;
|
|
34
35
|
#middlewares;
|
|
35
36
|
constructor(root) {
|
|
@@ -37,6 +38,7 @@ export class Routes {
|
|
|
37
38
|
root = __boundary(root);
|
|
38
39
|
this.#root = __normalize(path.resolve(root));
|
|
39
40
|
this.#items = [];
|
|
41
|
+
this.#jobs = [];
|
|
40
42
|
this.#events = { };
|
|
41
43
|
this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
|
|
42
44
|
}
|
|
@@ -65,6 +67,35 @@ export class Routes {
|
|
|
65
67
|
return null;
|
|
66
68
|
return null;
|
|
67
69
|
}
|
|
70
|
+
job(spec) {
|
|
71
|
+
__requireArity(arguments.length, [0], "job");
|
|
72
|
+
spec = __boundary(spec);
|
|
73
|
+
let allowed = ["path", "module", "handler", "queue", "policy", "auth", "reauthSeconds"];
|
|
74
|
+
for (const [key, value] of __entries(spec)) {
|
|
75
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
76
|
+
throw `Unknown job route option: ${key}`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
let item = new Endpoint(spec);
|
|
80
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(item.path.includes("*"))) || __requireBoolean(__normalize(item.path.includes("{")))) || __requireBoolean(__normalize(item.path.includes("}"))))) {
|
|
81
|
+
throw "Job routes require an exact path";
|
|
82
|
+
}
|
|
83
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.queue)))) || __requireBoolean(__normalize(spec.queue.trim()) !== __normalize(spec.queue))) || __requireBoolean(__normalize(spec.queue) === ""))) {
|
|
84
|
+
throw "Job route requires a queue name";
|
|
85
|
+
}
|
|
86
|
+
for (const other of __normalize(this.#jobs)) {
|
|
87
|
+
if (__requireBoolean(__normalize(other.path) === __normalize(item.path))) {
|
|
88
|
+
throw `Duplicate job route: ${__normalize(item.path)}`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
__add(this.#jobs, { path: __normalize(item.path), module: resolveModule(__normalize(item.module), __normalize(this.#root)), handler: __normalize(item.handler), queue: __normalize(spec.queue), policy: __normalize(item.policy), auth: __normalize(item.auth), reauthSeconds: __normalize(item.reauthSeconds), reauthExplicit: __normalize(item.reauthExplicit), legacyAuth: __normalize(item.legacyAuth) });
|
|
92
|
+
return this;
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
jobs() {
|
|
96
|
+
return __fromJson(__toJson(__normalize(this.#jobs)));
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
68
99
|
addMiddleware(name, spec) {
|
|
69
100
|
__requireArity(arguments.length, [0, 1], "addMiddleware");
|
|
70
101
|
name = __boundary(name);
|
|
@@ -234,7 +265,11 @@ export class Routes {
|
|
|
234
265
|
if (__requireBoolean(reauthSeconds === null)) {
|
|
235
266
|
reauthSeconds = inheritedReauthSeconds;
|
|
236
267
|
}
|
|
237
|
-
if (__normalize(tag.tag()) === "
|
|
268
|
+
if (__normalize(tag.tag()) === "job") {
|
|
269
|
+
attributes(tag, ["path", "module", "handler", "queue", "auth", "policy", "reauthSeconds"]);
|
|
270
|
+
leaf(tag);
|
|
271
|
+
this.job({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), queue: __normalize(spec.queue), auth: auth, policy: policy, reauthSeconds: reauthSeconds });
|
|
272
|
+
} else if (__normalize(tag.tag()) === "endpoint") {
|
|
238
273
|
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"]);
|
|
239
274
|
leaf(tag);
|
|
240
275
|
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, policy: policy, reauthSeconds: reauthSeconds });
|
package/dist/routes.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;;;;;;;;;;"}
|
|
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 job(spec)\n jobs()\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 @jobs = []\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:job(spec)\n allowed = [\"path\", \"module\", \"handler\", \"queue\", \"policy\", \"auth\", \"reauthSeconds\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown job route option: ${key}\"\n item = new Endpoint(spec)\n if item.path.includes(\"*\") or item.path.includes(\"{\") or item.path.includes(\"}\")\n throw \"Job routes require an exact path\"\n if !is_string(spec.queue) or spec.queue.trim() != spec.queue or spec.queue == \"\"\n throw \"Job route requires a queue name\"\n for @jobs as other\n if other.path == item.path\n throw \"Duplicate job route: ${item.path}\"\n @jobs.add({\n path: item.path\n module: resolveModule(item.module, @root)\n handler: item.handler\n queue: spec.queue\n policy: item.policy\n auth: item.auth\n reauthSeconds: item.reauthSeconds\n reauthExplicit: item.reauthExplicit\n legacyAuth: item.legacyAuth\n })\n return @this\n\nRoutes:jobs()\n return from_json(to_json(@jobs))\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 \"job\"\n attributes(tag, [\"path\", \"module\", \"handler\", \"queue\", \"auth\", \"policy\", \"reauthSeconds\"])\n leaf(tag)\n job({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n queue: spec.queue\n auth: auth\n policy: policy\n reauthSeconds: reauthSeconds\n })\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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoIA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;AAoIA;;;;;;;;;;"}
|