@lango-project/lango-server 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +239 -27
- package/dist/.lango/runtime.mjs +16 -0
- package/dist/body_reader.mjs +15 -14
- package/dist/body_reader.mjs.map +1 -1
- package/dist/cli/.lango/runtime.mjs +16 -0
- package/dist/cli/body_reader.mjs +15 -14
- package/dist/cli/body_reader.mjs.map +1 -1
- package/dist/cli/client_identity.mjs +250 -0
- package/dist/cli/client_identity.mjs.map +1 -0
- package/dist/cli/database.mjs +23 -8
- package/dist/cli/database.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +11 -7
- package/dist/cli/endpoint.mjs.map +1 -1
- package/dist/cli/longpoll_broker.mjs +9 -8
- package/dist/cli/longpoll_broker.mjs.map +1 -1
- package/dist/cli/longpoll_options.mjs +8 -7
- package/dist/cli/longpoll_options.mjs.map +1 -1
- package/dist/cli/longpoll_session.mjs +11 -10
- package/dist/cli/longpoll_session.mjs.map +1 -1
- package/dist/cli/options.mjs +29 -3
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/origin.mjs +87 -0
- package/dist/cli/origin.mjs.map +1 -0
- package/dist/cli/policy.mjs +121 -0
- package/dist/cli/policy.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +3 -2
- package/dist/cli/postgres_adapter.mjs.map +1 -1
- package/dist/cli/routes.mjs +33 -17
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +84 -80
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +5 -4
- package/dist/cli/static_files.mjs.map +1 -1
- package/dist/client_identity.mjs +250 -0
- package/dist/client_identity.mjs.map +1 -0
- package/dist/database.mjs +23 -8
- package/dist/database.mjs.map +1 -1
- package/dist/endpoint.mjs +11 -7
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +34 -16
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs +9 -2
- package/dist/http_failure.mjs.map +1 -1
- package/dist/longpoll_broker.mjs +9 -8
- package/dist/longpoll_broker.mjs.map +1 -1
- package/dist/longpoll_options.mjs +8 -7
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/longpoll_session.mjs +11 -10
- package/dist/longpoll_session.mjs.map +1 -1
- package/dist/options.mjs +29 -3
- package/dist/options.mjs.map +1 -1
- package/dist/origin.mjs +87 -0
- package/dist/origin.mjs.map +1 -0
- package/dist/policy.mjs +121 -0
- package/dist/policy.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +3 -2
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +15 -14
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/response.mjs +1 -1
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs +1 -1
- package/dist/route_info.mjs.map +1 -1
- package/dist/routes.mjs +33 -17
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +227 -112
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +84 -80
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs +21 -9
- package/dist/server_context.mjs.map +1 -1
- package/dist/static_files.mjs +5 -4
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs +34 -7
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs +11 -2
- package/dist/tokens.mjs.map +1 -1
- package/dist/worker.mjs +7 -6
- package/dist/worker.mjs.map +1 -1
- package/docs/security.md +420 -0
- package/examples/security/README.md +160 -0
- package/examples/security/deploy.xml +12 -0
- package/examples/security/handlers.lango +610 -0
- package/examples/security/package.json +19 -0
- package/examples/security/passwords.lango +61 -0
- package/examples/security/routes.xml +14 -0
- package/examples/security/schema.lango +91 -0
- package/examples/security/setup.lango +30 -0
- package/package.json +12 -6
- package/src/client_identity.lango +184 -0
- package/src/database.lango +15 -4
- package/src/endpoint.lango +12 -5
- package/src/group.lango +25 -21
- package/src/http_failure.lango +9 -5
- package/src/longpoll_options.lango +11 -5
- package/src/options.lango +34 -1
- package/src/origin.lango +56 -0
- package/src/policy.lango +84 -0
- package/src/response.lango +1 -1
- package/src/route_info.lango +2 -0
- package/src/routes.lango +30 -15
- package/src/runtime.lango +131 -80
- package/src/server.lango +6 -2
- package/src/server_context.lango +10 -2
- package/src/token.lango +26 -5
- package/src/tokens.lango +10 -3
package/dist/cli/options.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { policyRegistry } from "./policy.mjs";
|
|
2
|
+
import { clientOptions } from "./client_identity.mjs";
|
|
3
|
+
import { originOptions } from "./origin.mjs";
|
|
1
4
|
import { redisOptions } from "./longpoll_options.mjs";
|
|
2
5
|
import { databaseOptions } from "./database_options.mjs";
|
|
3
6
|
import { staticOptions } from "./static_files.mjs";
|
|
@@ -38,7 +41,7 @@ export function options(root, supplied = null) {
|
|
|
38
41
|
__requireArity(arguments.length, [0], "options");
|
|
39
42
|
root = __boundary(root);
|
|
40
43
|
supplied = __boundary(supplied);
|
|
41
|
-
let result = { root: __normalize(path.resolve(root)), host: "127.0.0.1", port: 3000, workers: 2, maxQueue: 64, bodyLimit: 2097152, bodyTimeoutMs: 30000, requestTimeoutMs: 30000, eventTimeoutMs: 5000, startupTimeoutMs: 30000, shutdownTimeoutMs: 30000, issuer: "lango-server", audience: "lango-client", ttlSeconds: 900, key: null, keys: { }, activeKeyId: "current", requestEvent: false, database: null, redis: null, static: null };
|
|
44
|
+
let result = { root: __normalize(path.resolve(root)), host: "127.0.0.1", port: 3000, workers: 2, maxQueue: 64, bodyLimit: 2097152, bodyTimeoutMs: 30000, requestTimeoutMs: 30000, eventTimeoutMs: 5000, startupTimeoutMs: 30000, shutdownTimeoutMs: 30000, issuer: "lango-server", audience: "lango-client", ttlSeconds: 900, key: null, keys: { }, activeKeyId: "current", requestEvent: false, database: null, redis: null, static: null, origin: null, client: null, policies: null };
|
|
42
45
|
if (__requireBoolean(supplied !== null)) {
|
|
43
46
|
for (const [key, value] of __entries(supplied)) {
|
|
44
47
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(result, key)))) || __requireBoolean(key === "root"))) {
|
|
@@ -90,9 +93,12 @@ export function options(root, supplied = null) {
|
|
|
90
93
|
}
|
|
91
94
|
result.keys = keys;
|
|
92
95
|
result.key = null;
|
|
96
|
+
result.policies = policyRegistry(__normalize(result.policies));
|
|
93
97
|
result.database = databaseOptions(__normalize(result.root), __normalize(result.database), __normalize(result.workers));
|
|
94
98
|
result.redis = redisOptions(__normalize(result.redis));
|
|
95
99
|
result.static = staticOptions(__normalize(result.root), __normalize(result.static));
|
|
100
|
+
result.origin = originOptions(__normalize(result.origin));
|
|
101
|
+
result.client = clientOptions(__normalize(result.client), __normalize(result.database), __normalize(result.origin));
|
|
96
102
|
return result;
|
|
97
103
|
return null;
|
|
98
104
|
}
|
|
@@ -110,11 +116,31 @@ export function deployment(directory) {
|
|
|
110
116
|
let seen = [];
|
|
111
117
|
for (const tag of __normalize(root.children())) {
|
|
112
118
|
let name = __normalize(tag.tag());
|
|
113
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(seen.includes(name))) || __requireBoolean(__normalize(__normalize(tag.children()).length) !== 0)) || __requireBoolean(__normalize(__normalize(tag.innerText()).trim()) !== ""))) {
|
|
119
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(seen.includes(name))) || __requireBoolean(__requireBoolean(name !== "policies") && __requireBoolean(__normalize(__normalize(tag.children()).length) !== 0))) || __requireBoolean(__normalize(__normalize(tag.innerText()).trim()) !== ""))) {
|
|
114
120
|
throw `Duplicate or nonempty deployment section: ${name}`;
|
|
115
121
|
}
|
|
116
122
|
seen.push(name);
|
|
117
|
-
if (name === "
|
|
123
|
+
if (name === "policies") {
|
|
124
|
+
attributes(tag, ["reauthSeconds"]);
|
|
125
|
+
let items = [];
|
|
126
|
+
for (const child of __normalize(tag.children())) {
|
|
127
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(child.tag()) !== "policy") || __requireBoolean(__normalize(__normalize(child.children()).length) !== 0)) || __requireBoolean(__normalize(__normalize(child.innerText()).trim()) !== ""))) {
|
|
128
|
+
throw "Policies may contain only empty policy elements";
|
|
129
|
+
}
|
|
130
|
+
attributes(child, ["name", "reauthSeconds"]);
|
|
131
|
+
items.push(__normalize(child.attributes()));
|
|
132
|
+
}
|
|
133
|
+
settings.policies = { reauthSeconds: __normalize(tag.reauthSeconds), items: items };
|
|
134
|
+
continue;
|
|
135
|
+
} else if (name === "client") {
|
|
136
|
+
attributes(tag, ["maxAgeSeconds"]);
|
|
137
|
+
settings.client = __normalize(tag.attributes());
|
|
138
|
+
continue;
|
|
139
|
+
} else if (name === "origin") {
|
|
140
|
+
attributes(tag, ["url", "urlEnv", "required"]);
|
|
141
|
+
settings.origin = __normalize(tag.attributes());
|
|
142
|
+
continue;
|
|
143
|
+
} else if (name === "static") {
|
|
118
144
|
attributes(tag, ["path", "directory", "index", "fallback", "runtime"]);
|
|
119
145
|
settings.static = __normalize(tag.attributes());
|
|
120
146
|
continue;
|
package/dist/cli/options.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.mjs","sources":["options.lango"],"sourcesContent":["import \"./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 }\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.database = databaseOptions(result.root, result.database, result.workers)\n result.redis = redisOptions(result.redis)\n result.static = staticOptions(result.root, result.static)\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 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 \"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 options(directory, settings)\n\ncontains(items, value)\n return items.includes(value)\n"],"names":[],"mappings":"
|
|
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 options(directory, settings)\n\ncontains(items, value)\n return items.includes(value)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EA;;;;;;;;"}
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
requireBoolean as __requireBoolean,
|
|
9
|
+
normalize as __normalize,
|
|
10
|
+
boundary as __boundary,
|
|
11
|
+
requireArity as __requireArity,
|
|
12
|
+
} from "./.lango/runtime.mjs";
|
|
13
|
+
|
|
14
|
+
export function originOptions(spec) {
|
|
15
|
+
__requireArity(arguments.length, [0], "originOptions");
|
|
16
|
+
spec = __boundary(spec);
|
|
17
|
+
let envName;
|
|
18
|
+
if (__requireBoolean(spec === null)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
22
|
+
throw "Origin configuration must be an object";
|
|
23
|
+
}
|
|
24
|
+
for (const name of __normalize(__hostGlobal(["Object"]).keys(spec))) {
|
|
25
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(name !== "url") && __requireBoolean(name !== "urlEnv")) && __requireBoolean(name !== "required"))) {
|
|
26
|
+
throw `Unknown origin option: ${name}`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (__requireBoolean(__requireBoolean(__normalize(spec.url) !== null) && __requireBoolean(__normalize(spec.urlEnv) !== null))) {
|
|
30
|
+
throw "Origin requires either url or urlEnv, not both";
|
|
31
|
+
}
|
|
32
|
+
let url = __normalize(spec.url);
|
|
33
|
+
if (__requireBoolean(__normalize(spec.urlEnv) !== null)) {
|
|
34
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.urlEnv)))) || __requireBoolean(__normalize(spec.urlEnv) === ""))) {
|
|
35
|
+
throw "Origin urlEnv must name an environment variable";
|
|
36
|
+
}
|
|
37
|
+
envName = __normalize(spec.urlEnv);
|
|
38
|
+
url = __normalize(__hostGlobal(["process"]).env[envName]);
|
|
39
|
+
}
|
|
40
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(url))) || __requireBoolean(url === ""))) {
|
|
41
|
+
throw "Origin URL is required";
|
|
42
|
+
}
|
|
43
|
+
let parsed = null;
|
|
44
|
+
try {
|
|
45
|
+
parsed = new (__hostGlobal(["URL"]))(url);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw "Origin requires an exact HTTP or HTTPS origin";
|
|
48
|
+
}
|
|
49
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(parsed.protocol) !== "https:") && __requireBoolean(__normalize(parsed.protocol) !== "http:")) || __requireBoolean(__normalize(parsed.origin) !== url))) {
|
|
50
|
+
throw "Origin requires an exact HTTP or HTTPS origin without credentials, path, query or fragment";
|
|
51
|
+
}
|
|
52
|
+
let required = __normalize(spec.required);
|
|
53
|
+
if (__requireBoolean(__requireBoolean(required === null) || __requireBoolean(required === "true"))) {
|
|
54
|
+
required = true;
|
|
55
|
+
}
|
|
56
|
+
if (__requireBoolean(required === "false")) {
|
|
57
|
+
required = false;
|
|
58
|
+
}
|
|
59
|
+
if (__requireBoolean(!__requireBoolean(__isBool(required)))) {
|
|
60
|
+
throw "Origin required must be true or false";
|
|
61
|
+
}
|
|
62
|
+
return { url: url, required: required };
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function originAllowed(spec, request) {
|
|
67
|
+
__requireArity(arguments.length, [0, 1], "originAllowed");
|
|
68
|
+
spec = __boundary(spec);
|
|
69
|
+
request = __boundary(request);
|
|
70
|
+
if (__requireBoolean(spec === null)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
let headers = __normalize(request.headers);
|
|
74
|
+
let value = __normalize(headers.origin);
|
|
75
|
+
if (__requireBoolean(value === null)) {
|
|
76
|
+
if (__requireBoolean(__normalize(spec.required))) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (__requireBoolean(__requireBoolean(value !== null) && __requireBoolean(value !== __normalize(spec.url)))) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
let site = __normalize(headers["sec-fetch-site"]);
|
|
84
|
+
return __requireBoolean(__requireBoolean(site === null) || __requireBoolean(site === "same-origin")) || __requireBoolean(site === "none");
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=origin.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"origin.mjs","sources":["origin.lango"],"sourcesContent":["import js global Object\nimport js global URL\nimport js global process\n\nexport originOptions(spec)\nexport originAllowed(spec, request)\n\noriginOptions(spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Origin configuration must be an object\"\n for Object/keys(spec) as name\n if name != \"url\" and name != \"urlEnv\" and name != \"required\"\n throw \"Unknown origin option: ${name}\"\n if spec.url != null and spec.urlEnv != null\n throw \"Origin requires either url or urlEnv, not both\"\n url = spec.url\n if spec.urlEnv != null\n if !is_string(spec.urlEnv) or spec.urlEnv == \"\"\n throw \"Origin urlEnv must name an environment variable\"\n envName = spec.urlEnv\n url = process.env[envName]\n if !is_string(url) or url == \"\"\n throw \"Origin URL is required\"\n parsed = null\n try\n parsed = new URL(url)\n catch error\n throw \"Origin requires an exact HTTP or HTTPS origin\"\n if (parsed.protocol != \"https:\" and parsed.protocol != \"http:\") or parsed.origin != url\n throw \"Origin requires an exact HTTP or HTTPS origin without credentials, path, query or fragment\"\n required = spec.required\n if required == null or required == \"true\"\n required = true\n if required == \"false\"\n required = false\n if !is_bool(required)\n throw \"Origin required must be true or false\"\n return {\n url: url\n required: required\n }\n\noriginAllowed(spec, request)\n if spec == null\n return true\n headers = request.headers\n value = headers.origin\n if value == null\n if spec.required\n return false\n if value != null and value != spec.url\n return false\n site = headers[\"sec-fetch-site\"]\n return site == null or site == \"same-origin\" or site == \"none\"\n"],"names":[],"mappings":";;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
hostGlobalCall as __hostGlobalCall,
|
|
4
|
+
isNumber as __isNumber,
|
|
5
|
+
isString as __isString,
|
|
6
|
+
isBool as __isBool,
|
|
7
|
+
isObject as __isObject,
|
|
8
|
+
isArray as __isArray,
|
|
9
|
+
entries as __entries,
|
|
10
|
+
requireBoolean as __requireBoolean,
|
|
11
|
+
normalize as __normalize,
|
|
12
|
+
boundary as __boundary,
|
|
13
|
+
requireArity as __requireArity,
|
|
14
|
+
} from "./.lango/runtime.mjs";
|
|
15
|
+
|
|
16
|
+
export function policyOptions(spec) {
|
|
17
|
+
__requireArity(arguments.length, [0], "policyOptions");
|
|
18
|
+
spec = __boundary(spec);
|
|
19
|
+
let name = __normalize(spec.policy);
|
|
20
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(name !== null) && __requireBoolean(__normalize(spec.auth) !== null)) && __requireBoolean(name !== __normalize(spec.auth)))) {
|
|
21
|
+
throw "Endpoint policy and legacy auth cannot disagree";
|
|
22
|
+
}
|
|
23
|
+
let legacy = name === null;
|
|
24
|
+
if (__requireBoolean(name === null)) {
|
|
25
|
+
name = __normalize(spec.auth);
|
|
26
|
+
}
|
|
27
|
+
if (__requireBoolean(name === null)) {
|
|
28
|
+
name = "login";
|
|
29
|
+
}
|
|
30
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(__normalize(name.trim()) === "")) || __requireBoolean(__normalize(name.includes(","))))) {
|
|
31
|
+
throw "Endpoint policy must name one permission";
|
|
32
|
+
}
|
|
33
|
+
let seconds = __normalize(spec.reauthSeconds);
|
|
34
|
+
if (__requireBoolean(seconds === null)) {
|
|
35
|
+
seconds = 0;
|
|
36
|
+
}
|
|
37
|
+
if (__requireBoolean(__isString(seconds))) {
|
|
38
|
+
if (__requireBoolean(__normalize(seconds.trim()) === "")) {
|
|
39
|
+
throw "reauthSeconds cannot be empty";
|
|
40
|
+
}
|
|
41
|
+
seconds = __hostGlobalCall(["Number"], seconds);
|
|
42
|
+
}
|
|
43
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(seconds)))) || __requireBoolean(seconds < 0)) || __requireBoolean(seconds > 31536000))) {
|
|
44
|
+
throw "reauthSeconds must be an integer between 0 and 31536000";
|
|
45
|
+
}
|
|
46
|
+
if (__requireBoolean(__requireBoolean(seconds > 0) && __requireBoolean(name === "anonymous"))) {
|
|
47
|
+
throw "Anonymous policy cannot require password freshness";
|
|
48
|
+
}
|
|
49
|
+
return { name: name, reauthSeconds: seconds, explicit: __normalize(spec.reauthSeconds) !== null, legacy: legacy };
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function policyRegistry(spec) {
|
|
54
|
+
__requireArity(arguments.length, [0], "policyRegistry");
|
|
55
|
+
spec = __boundary(spec);
|
|
56
|
+
if (__requireBoolean(spec === null)) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
60
|
+
throw "Policies must be a map";
|
|
61
|
+
}
|
|
62
|
+
for (const [key, value] of __entries(spec)) {
|
|
63
|
+
if (__requireBoolean(__requireBoolean(key !== "reauthSeconds") && __requireBoolean(key !== "items"))) {
|
|
64
|
+
throw `Unknown policies option: ${key}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
let base = policyOptions({ policy: "login", reauthSeconds: __normalize(spec.reauthSeconds) });
|
|
68
|
+
let items = __normalize(spec.items);
|
|
69
|
+
if (__requireBoolean(!__requireBoolean(__isArray(items)))) {
|
|
70
|
+
throw "Policies require an items array";
|
|
71
|
+
}
|
|
72
|
+
let result = __normalize(__hostGlobal(["Object"]).create(null));
|
|
73
|
+
for (const item of items) {
|
|
74
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(item))) || __requireBoolean(__isArray(item)))) {
|
|
75
|
+
throw "Policy must be a map";
|
|
76
|
+
}
|
|
77
|
+
for (const [key, value] of __entries(item)) {
|
|
78
|
+
if (__requireBoolean(__requireBoolean(key !== "name") && __requireBoolean(key !== "reauthSeconds"))) {
|
|
79
|
+
throw `Unknown policy option: ${key}`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
let name = __normalize(item.name);
|
|
83
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(__normalize(name.trim()) !== name)) || __requireBoolean(name === "")) || __requireBoolean(name === "anonymous")) || __requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(result, name))))) {
|
|
84
|
+
throw "Invalid or duplicate policy name";
|
|
85
|
+
}
|
|
86
|
+
let seconds = __normalize(item.reauthSeconds);
|
|
87
|
+
if (__requireBoolean(seconds === null)) {
|
|
88
|
+
seconds = __normalize(base.reauthSeconds);
|
|
89
|
+
}
|
|
90
|
+
let selected = policyOptions({ policy: name, reauthSeconds: seconds });
|
|
91
|
+
result[name] = __normalize(selected.reauthSeconds);
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function resolvePolicies(routes, registry) {
|
|
98
|
+
__requireArity(arguments.length, [0, 1], "resolvePolicies");
|
|
99
|
+
routes = __boundary(routes);
|
|
100
|
+
registry = __boundary(registry);
|
|
101
|
+
if (__requireBoolean(registry === null)) {
|
|
102
|
+
return routes;
|
|
103
|
+
}
|
|
104
|
+
for (const route of routes) {
|
|
105
|
+
if (__requireBoolean(__normalize(route.policy) !== "anonymous")) {
|
|
106
|
+
if (__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(registry, __normalize(route.policy)))))) {
|
|
107
|
+
throw `Undefined route policy: ${__normalize(route.policy)}`;
|
|
108
|
+
}
|
|
109
|
+
let maximum = __normalize(registry[__normalize(route.policy)]);
|
|
110
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(route.reauthExplicit)) && __requireBoolean(maximum > 0)) && __requireBoolean(__requireBoolean(__normalize(route.reauthSeconds) === 0) || __requireBoolean(__normalize(route.reauthSeconds) > maximum)))) {
|
|
111
|
+
throw `Route cannot weaken policy freshness: ${__normalize(route.path)}`;
|
|
112
|
+
}
|
|
113
|
+
if (__requireBoolean(!__requireBoolean(__normalize(route.reauthExplicit)))) {
|
|
114
|
+
route.reauthSeconds = maximum;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return routes;
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=policy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.mjs","sources":["policy.lango"],"sourcesContent":["import js global Number\nimport js global Object\n\nexport policyOptions(spec)\nexport policyRegistry(spec)\nexport resolvePolicies(routes, registry)\n\npolicyOptions(spec)\n name = spec.policy\n if name != null and spec.auth != null and name != spec.auth\n throw \"Endpoint policy and legacy auth cannot disagree\"\n legacy = name == null\n if name == null\n name = spec.auth\n if name == null\n name = \"login\"\n if !is_string(name) or name.trim() == \"\" or name.includes(\",\")\n throw \"Endpoint policy must name one permission\"\n seconds = spec.reauthSeconds\n if seconds == null\n seconds = 0\n if is_string(seconds)\n if seconds.trim() == \"\"\n throw \"reauthSeconds cannot be empty\"\n seconds = Number(seconds)\n if !Number/isSafeInteger(seconds) or seconds < 0 or seconds > 31536000\n throw \"reauthSeconds must be an integer between 0 and 31536000\"\n if seconds > 0 and name == \"anonymous\"\n throw \"Anonymous policy cannot require password freshness\"\n return {\n name: name\n reauthSeconds: seconds\n explicit: spec.reauthSeconds != null\n legacy: legacy\n }\n\npolicyRegistry(spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Policies must be a map\"\n for spec as key: value\n if key != \"reauthSeconds\" and key != \"items\"\n throw \"Unknown policies option: ${key}\"\n base = policyOptions({\n policy: \"login\"\n reauthSeconds: spec.reauthSeconds\n })\n items = spec.items\n if !is_array(items)\n throw \"Policies require an items array\"\n result = Object/create(null)\n for items as item\n if !is_object(item) or is_array(item)\n throw \"Policy must be a map\"\n for item as key: value\n if key != \"name\" and key != \"reauthSeconds\"\n throw \"Unknown policy option: ${key}\"\n name = item.name\n if !is_string(name) or name.trim() != name or name == \"\" or name == \"anonymous\" or Object/hasOwn(result, name)\n throw \"Invalid or duplicate policy name\"\n seconds = item.reauthSeconds\n if seconds == null\n seconds = base.reauthSeconds\n selected = policyOptions({\n policy: name\n reauthSeconds: seconds\n })\n result[name] = selected.reauthSeconds\n return result\n\nresolvePolicies(routes, registry)\n if registry == null\n return routes\n for routes as route\n if route.policy != \"anonymous\"\n if !Object/hasOwn(registry, route.policy)\n throw \"Undefined route policy: ${route.policy}\"\n maximum = registry[route.policy]\n if route.reauthExplicit and maximum > 0 and (route.reauthSeconds == 0 or route.reauthSeconds > maximum)\n throw \"Route cannot weaken policy freshness: ${route.path}\"\n if !route.reauthExplicit\n route.reauthSeconds = maximum\n return routes\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -3,6 +3,7 @@ import * as path from "node:path";
|
|
|
3
3
|
import * as threads from "node:worker_threads";
|
|
4
4
|
import {
|
|
5
5
|
hostGlobal as __hostGlobal,
|
|
6
|
+
bindContexts as __bindContexts,
|
|
6
7
|
requireBoolean as __requireBoolean,
|
|
7
8
|
normalize as __normalize,
|
|
8
9
|
finite as __finite,
|
|
@@ -43,11 +44,11 @@ export class PostgresAdapter {
|
|
|
43
44
|
let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
|
|
44
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
46
|
let worker = __normalize(this.#worker);
|
|
46
|
-
__normalize(worker.on("error", __withArity((error) => {
|
|
47
|
+
__normalize(worker.on("error", __withArity(__bindContexts((error) => {
|
|
47
48
|
error = __boundary(error);
|
|
48
49
|
this.#broken = true;
|
|
49
50
|
return null;
|
|
50
|
-
}, [0], "closure")));
|
|
51
|
+
}), [0], "closure")));
|
|
51
52
|
__normalize(this.#worker.unref());
|
|
52
53
|
__normalize(this.#port.unref());
|
|
53
54
|
try {
|
|
@@ -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":"
|
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/cli/routes.mjs
CHANGED
|
@@ -45,7 +45,7 @@ export class Routes {
|
|
|
45
45
|
spec = __boundary(spec);
|
|
46
46
|
let item = new Endpoint(spec);
|
|
47
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), middleware: __normalize(item.middleware) });
|
|
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
49
|
return this;
|
|
50
50
|
return null;
|
|
51
51
|
}
|
|
@@ -120,11 +120,13 @@ export class Routes {
|
|
|
120
120
|
return this;
|
|
121
121
|
return null;
|
|
122
122
|
}
|
|
123
|
-
include(spec, auth = null) {
|
|
123
|
+
include(spec, auth = null, policy = null, reauthSeconds = null) {
|
|
124
124
|
__requireArity(arguments.length, [0], "include");
|
|
125
125
|
spec = __boundary(spec);
|
|
126
126
|
auth = __boundary(auth);
|
|
127
|
-
|
|
127
|
+
policy = __boundary(policy);
|
|
128
|
+
reauthSeconds = __boundary(reauthSeconds);
|
|
129
|
+
this.#includeAt(spec, __normalize(this.#root), "", [], auth, policy, reauthSeconds);
|
|
128
130
|
return this;
|
|
129
131
|
return null;
|
|
130
132
|
}
|
|
@@ -143,13 +145,15 @@ export class Routes {
|
|
|
143
145
|
return __fromJson(__toJson(__normalize(this.#events)));
|
|
144
146
|
return null;
|
|
145
147
|
}
|
|
146
|
-
#includeAt(spec, directory, prefix, stack, inheritedAuth = null) {
|
|
148
|
+
#includeAt(spec, directory, prefix, stack, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null) {
|
|
147
149
|
__requireArity(arguments.length, [0, 1, 2, 3], "includeAt");
|
|
148
150
|
spec = __boundary(spec);
|
|
149
151
|
directory = __boundary(directory);
|
|
150
152
|
prefix = __boundary(prefix);
|
|
151
153
|
stack = __boundary(stack);
|
|
152
154
|
inheritedAuth = __boundary(inheritedAuth);
|
|
155
|
+
inheritedPolicy = __boundary(inheritedPolicy);
|
|
156
|
+
inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
|
|
153
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:"))))) {
|
|
154
158
|
throw "Include requires an npm package name";
|
|
155
159
|
}
|
|
@@ -171,11 +175,11 @@ export class Routes {
|
|
|
171
175
|
combined = "";
|
|
172
176
|
}
|
|
173
177
|
let file = resolveModule(`${__normalize(spec.module)}/routes.xml`, directory);
|
|
174
|
-
this.#readXml(file, combined, __normalize(path.dirname(file)), stack, false, inheritedAuth);
|
|
178
|
+
this.#readXml(file, combined, __normalize(path.dirname(file)), stack, false, inheritedAuth, inheritedPolicy, inheritedReauthSeconds);
|
|
175
179
|
return null;
|
|
176
180
|
return null;
|
|
177
181
|
}
|
|
178
|
-
#readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null) {
|
|
182
|
+
#readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null) {
|
|
179
183
|
__requireArity(arguments.length, [0, 1, 2, 3, 4], "readXml");
|
|
180
184
|
file = __boundary(file);
|
|
181
185
|
prefix = __boundary(prefix);
|
|
@@ -183,6 +187,8 @@ export class Routes {
|
|
|
183
187
|
stack = __boundary(stack);
|
|
184
188
|
allowEvents = __boundary(allowEvents);
|
|
185
189
|
inheritedAuth = __boundary(inheritedAuth);
|
|
190
|
+
inheritedPolicy = __boundary(inheritedPolicy);
|
|
191
|
+
inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
|
|
186
192
|
let real = __normalize(fs.realpathSync(file));
|
|
187
193
|
if (__requireBoolean(__normalize(stack.includes(real)))) {
|
|
188
194
|
throw `Route include cycle: ${real}`;
|
|
@@ -194,11 +200,11 @@ export class Routes {
|
|
|
194
200
|
attributes(root, []);
|
|
195
201
|
let nextStack = __normalize(stack.slice());
|
|
196
202
|
__add(nextStack, real);
|
|
197
|
-
this.#collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents);
|
|
203
|
+
this.#collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents, inheritedPolicy, inheritedReauthSeconds);
|
|
198
204
|
return null;
|
|
199
205
|
return null;
|
|
200
206
|
}
|
|
201
|
-
#collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents) {
|
|
207
|
+
#collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents, inheritedPolicy = null, inheritedReauthSeconds = null) {
|
|
202
208
|
__requireArity(arguments.length, [0, 1, 2, 3, 4, 5, 6], "collect");
|
|
203
209
|
parent = __boundary(parent);
|
|
204
210
|
prefix = __boundary(prefix);
|
|
@@ -207,6 +213,8 @@ export class Routes {
|
|
|
207
213
|
directory = __boundary(directory);
|
|
208
214
|
stack = __boundary(stack);
|
|
209
215
|
allowEvents = __boundary(allowEvents);
|
|
216
|
+
inheritedPolicy = __boundary(inheritedPolicy);
|
|
217
|
+
inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
|
|
210
218
|
if (__requireBoolean(__normalize(__normalize(parent.innerText()).trim()) !== "")) {
|
|
211
219
|
throw "Route XML cannot contain text";
|
|
212
220
|
}
|
|
@@ -214,32 +222,40 @@ export class Routes {
|
|
|
214
222
|
let spec = __normalize(tag.attributes());
|
|
215
223
|
let mod = __normalize(spec.module);
|
|
216
224
|
let auth = __normalize(spec.auth);
|
|
225
|
+
let policy = __normalize(spec.policy);
|
|
226
|
+
let reauthSeconds = __normalize(spec.reauthSeconds);
|
|
217
227
|
if (__requireBoolean(mod === null)) {
|
|
218
228
|
mod = inheritedModule;
|
|
219
229
|
}
|
|
220
|
-
if (__requireBoolean(auth === null)) {
|
|
230
|
+
if (__requireBoolean(__requireBoolean(auth === null) && __requireBoolean(policy === null))) {
|
|
221
231
|
auth = inheritedAuth;
|
|
232
|
+
policy = inheritedPolicy;
|
|
233
|
+
}
|
|
234
|
+
if (__requireBoolean(reauthSeconds === null)) {
|
|
235
|
+
reauthSeconds = inheritedReauthSeconds;
|
|
222
236
|
}
|
|
223
237
|
if (__normalize(tag.tag()) === "endpoint") {
|
|
224
|
-
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
238
|
+
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"]);
|
|
225
239
|
leaf(tag);
|
|
226
|
-
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth });
|
|
240
|
+
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, policy: policy, reauthSeconds: reauthSeconds });
|
|
227
241
|
} else if (__normalize(tag.tag()) === "longpoll") {
|
|
228
|
-
attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"]);
|
|
242
|
+
attributes(tag, ["path", "auth", "policy", "reauthSeconds", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"]);
|
|
229
243
|
leaf(tag);
|
|
230
244
|
spec.path = joinPath(prefix, __normalize(spec.path));
|
|
231
245
|
spec.auth = auth;
|
|
246
|
+
spec.policy = policy;
|
|
247
|
+
spec.reauthSeconds = reauthSeconds;
|
|
232
248
|
if (__requireBoolean(__normalize(spec.fallback) !== null)) {
|
|
233
249
|
spec.fallback = joinPath(prefix, __normalize(spec.fallback));
|
|
234
250
|
}
|
|
235
251
|
this.longpoll(spec);
|
|
236
252
|
} else if (__normalize(tag.tag()) === "group") {
|
|
237
|
-
attributes(tag, ["path", "module", "auth"]);
|
|
238
|
-
this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false);
|
|
253
|
+
attributes(tag, ["path", "module", "auth", "policy", "reauthSeconds"]);
|
|
254
|
+
this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false, policy, reauthSeconds);
|
|
239
255
|
} else if (__normalize(tag.tag()) === "include") {
|
|
240
256
|
attributes(tag, ["module", "path"]);
|
|
241
257
|
leaf(tag);
|
|
242
|
-
this.#includeAt(spec, directory, prefix, stack, auth);
|
|
258
|
+
this.#includeAt(spec, directory, prefix, stack, auth, policy, reauthSeconds);
|
|
243
259
|
} else if (__normalize(tag.tag()) === "middleware") {
|
|
244
260
|
attributes(tag, ["name", "module", "handler", "validate"]);
|
|
245
261
|
leaf(tag);
|
|
@@ -255,9 +271,9 @@ export class Routes {
|
|
|
255
271
|
}
|
|
256
272
|
this.event({ name: __normalize(spec.name), module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
257
273
|
} else {
|
|
258
|
-
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
274
|
+
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"]);
|
|
259
275
|
leaf(tag);
|
|
260
|
-
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth, middleware: __normalize(tag.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()) });
|
|
261
277
|
}
|
|
262
278
|
}
|
|
263
279
|
return null;
|
package/dist/cli/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)\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 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)\n includeAt(spec, @root, \"\", [], auth)\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)\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)\n return null\n\nRoutes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = 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)\n return null\n\nRoutes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents)\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 if mod == null\n mod = inheritedModule\n if auth == null\n auth = inheritedAuth\n switch tag.tag()\n case \"endpoint\"\n attributes(tag, [\"path\", \"module\", \"handler\", \"auth\"])\n leaf(tag)\n endpoint({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n auth: auth\n })\n case \"longpoll\"\n attributes(tag, [\"path\", \"auth\", \"waitTimeoutMs\", \"retentionMs\", \"maxMessages\", \"fallback\"])\n leaf(tag)\n spec.path = joinPath(prefix, spec.path)\n spec.auth = auth\n if spec.fallback != null\n spec.fallback = joinPath(prefix, spec.fallback)\n longpoll(spec)\n case \"group\"\n attributes(tag, [\"path\", \"module\", \"auth\"])\n collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false)\n case \"include\"\n attributes(tag, [\"module\", \"path\"])\n leaf(tag)\n includeAt(spec, directory, prefix, stack, auth)\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\"])\n leaf(tag)\n endpoint({\n path: joinPath(prefix, spec.path)\n module: resolveModule(mod, directory)\n handler: spec.handler\n auth: auth\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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgGA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;AA6GA;;;;;;;;;;"}
|
|
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;;;;;;;;;;"}
|