@lango-project/lango-server 0.1.1 → 0.2.0
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 +149 -18
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +25 -6
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -0
- package/dist/cli/.lango/localization.mjs +108 -0
- package/dist/cli/.lango/runtime.mjs +25 -6
- package/dist/cli/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/cli/.lango/vendor/i18next.mjs +2434 -0
- package/dist/cli/endpoint.mjs +2 -6
- package/dist/cli/endpoint.mjs.map +1 -1
- package/dist/cli/longpoll_options.mjs +1 -1
- package/dist/cli/longpoll_options.mjs.map +1 -1
- package/dist/cli/options.mjs +2 -2
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/route_pattern.mjs +129 -0
- package/dist/cli/route_pattern.mjs.map +1 -0
- package/dist/cli/routes.mjs +25 -13
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/static_files.mjs +121 -25
- package/dist/cli/static_files.mjs.map +1 -1
- package/dist/endpoint.mjs +2 -6
- package/dist/endpoint.mjs.map +1 -1
- package/dist/http_failure.mjs +45 -0
- package/dist/http_failure.mjs.map +1 -0
- package/dist/longpoll_options.mjs +1 -1
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/options.mjs +2 -2
- package/dist/options.mjs.map +1 -1
- package/dist/response.mjs +8 -0
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs +22 -0
- package/dist/route_info.mjs.map +1 -0
- package/dist/route_pattern.mjs +129 -0
- package/dist/route_pattern.mjs.map +1 -0
- package/dist/routes.mjs +25 -13
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +132 -22
- package/dist/runtime.mjs.map +1 -1
- package/dist/server_context.mjs +25 -2
- package/dist/server_context.mjs.map +1 -1
- package/dist/static_files.mjs +121 -25
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs +38 -13
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs +16 -1
- package/dist/tokens.mjs.map +1 -1
- package/package.json +3 -3
- package/src/endpoint.lango +3 -4
- package/src/http_failure.lango +26 -0
- package/src/longpoll_options.lango +1 -1
- package/src/options.lango +2 -2
- package/src/response.lango +6 -0
- package/src/route_info.lango +15 -0
- package/src/route_pattern.lango +86 -0
- package/src/routes.lango +20 -8
- package/src/runtime.lango +119 -19
- package/src/server_context.lango +19 -3
- package/src/static_files.lango +99 -23
- package/src/token.lango +24 -3
- package/src/tokens.lango +11 -1
package/dist/cli/endpoint.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { routePattern } from "./route_pattern.mjs";
|
|
1
2
|
import { middlewareName } from "./middleware.mjs";
|
|
2
3
|
import {
|
|
3
4
|
isNumber as __isNumber,
|
|
@@ -20,12 +21,7 @@ export class Endpoint {
|
|
|
20
21
|
constructor(spec) {
|
|
21
22
|
__requireArity(arguments.length, [0], "constructor");
|
|
22
23
|
spec = __boundary(spec);
|
|
23
|
-
|
|
24
|
-
throw "Endpoint requires an absolute URL path";
|
|
25
|
-
}
|
|
26
|
-
if (__requireBoolean(__requireBoolean(__normalize(spec.path.includes("*"))) && __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(spec.path.endsWith("/*")))) || __requireBoolean(__normalize(spec.path.indexOf("*")) !== __normalize(spec.path.lastIndexOf("*")))))) {
|
|
27
|
-
throw "Only trailing /* wildcards are supported";
|
|
28
|
-
}
|
|
24
|
+
routePattern(__normalize(spec.path));
|
|
29
25
|
for (const key of ["module", "handler"]) {
|
|
30
26
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec[key])))) || __requireBoolean(__normalize(spec[key]) === ""))) {
|
|
31
27
|
throw `Endpoint requires ${key}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./middleware\"\n middlewareName(name)\n\nexport class Endpoint(spec)\n properties\n path\n module\n handler\n auth\n middleware\n\nEndpoint(spec)\n
|
|
1
|
+
{"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./route_pattern\"\n routePattern(path)\nimport \"./middleware\"\n middlewareName(name)\n\nexport class Endpoint(spec)\n properties\n path\n module\n handler\n auth\n middleware\n\nEndpoint(spec)\n routePattern(spec.path)\n for [\"module\", \"handler\"] as key\n if !is_string(spec[key]) or spec[key] == \"\"\n throw \"Endpoint requires ${key}\"\n @path = spec.path\n @module = spec.module\n @handler = spec.handler\n @middleware = spec.middleware\n if @middleware != null\n middlewareName(@middleware)\n @auth = spec.auth\n if @auth == null\n @auth = \"login\"\n if !is_string(@auth) or @auth == \"\" or @auth.includes(\",\")\n throw \"Endpoint auth must name one permission\"\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -51,7 +51,7 @@ export function longpollOptions(spec) {
|
|
|
51
51
|
function validPath(value) {
|
|
52
52
|
__requireArity(arguments.length, [0], "validPath");
|
|
53
53
|
value = __boundary(value);
|
|
54
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(!__requireBoolean(__normalize(value.startsWith("/"))))) || __requireBoolean(__normalize(value.includes("*")))) || __requireBoolean(__normalize(value.includes("?")))) || __requireBoolean(__normalize(value.includes("#")))) || __requireBoolean(__normalize(value.includes("\\")))) || __requireBoolean(__normalize(value.includes("//"))))) {
|
|
54
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(!__requireBoolean(__normalize(value.startsWith("/"))))) || __requireBoolean(__normalize(value.includes("*")))) || __requireBoolean(__normalize(value.includes("{")))) || __requireBoolean(__normalize(value.includes("}")))) || __requireBoolean(__normalize(value.includes("?")))) || __requireBoolean(__normalize(value.includes("#")))) || __requireBoolean(__normalize(value.includes("\\")))) || __requireBoolean(__normalize(value.includes("//"))))) {
|
|
55
55
|
throw "Longpoll requires an exact absolute URL path";
|
|
56
56
|
}
|
|
57
57
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"longpoll_options.mjs","sources":["longpoll_options.lango"],"sourcesContent":["import js global Number\nimport js global Object\nimport js global process\nimport js global URL\n\nexport longpollOptions(spec)\nexport redisOptions(spec)\n\nlongpollOptions(spec)\n result = {\n kind: \"longpoll\"\n path: spec.path\n auth: \"login\"\n waitTimeoutMs: 20000\n retentionMs: 300000\n maxMessages: 1000\n fallback: null\n }\n for spec as key: value\n if !Object/hasOwn(result, key) or key == \"kind\"\n throw \"Unknown longpoll option: ${key}\"\n result[key] = value\n if result.auth == null\n result.auth = \"login\"\n validPath(result.path)\n if !is_string(result.auth) or result.auth == \"\" or result.auth.includes(\",\")\n throw \"Longpoll auth must name one permission\"\n if result.fallback != null\n validPath(result.fallback)\n limits = {\n waitTimeoutMs: 60000\n retentionMs: 86400000\n maxMessages: 10000\n }\n for limits as key: limit\n if !is_number(result[key]) and !is_string(result[key])\n throw \"Invalid longpoll option: ${key}\"\n value = Number(result[key])\n if !Number/isSafeInteger(value) or value < 1 or value > limit\n throw \"Invalid longpoll option: ${key}\"\n result[key] = value\n return result\n\nvalidPath(value)\n if !is_string(value) or !value.startsWith(\"/\") or value.includes(\"*\") or value.includes(\"?\") or value.includes(\"#\") or value.includes(\"\\\\\") or value.includes(\"//\")\n throw \"Longpoll requires an exact absolute URL path\"\n return null\n\nredisOptions(spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Redis configuration must be a map\"\n allowed = [\"connectionEnv\", \"namespace\", \"maxPendingPolls\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown Redis option: ${key}\"\n if !is_string(spec.connectionEnv) or spec.connectionEnv == \"\" or !is_string(spec.namespace) or spec.namespace == \"\" or spec.namespace.length > 128\n throw \"Redis requires connectionEnv and namespace\"\n name = spec.connectionEnv\n value = process.env[name]\n if !is_string(value) or value == \"\"\n throw \"Redis connection environment variable is missing\"\n url = new URL(value)\n protocols = [\"redis:\", \"rediss:\"]\n if !protocols.includes(url.protocol)\n throw \"Redis requires a redis:// or rediss:// URL\"\n maximum = 1024\n if spec.maxPendingPolls != null\n if !is_number(spec.maxPendingPolls) and !is_string(spec.maxPendingPolls)\n throw \"Invalid Redis maxPendingPolls\"\n maximum = Number(spec.maxPendingPolls)\n if !Number/isSafeInteger(maximum) or maximum < 1 or maximum > 10000\n throw \"Invalid Redis maxPendingPolls\"\n return {\n connectionEnv: spec.connectionEnv\n namespace: spec.namespace\n maxPendingPolls: maximum\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"longpoll_options.mjs","sources":["longpoll_options.lango"],"sourcesContent":["import js global Number\nimport js global Object\nimport js global process\nimport js global URL\n\nexport longpollOptions(spec)\nexport redisOptions(spec)\n\nlongpollOptions(spec)\n result = {\n kind: \"longpoll\"\n path: spec.path\n auth: \"login\"\n waitTimeoutMs: 20000\n retentionMs: 300000\n maxMessages: 1000\n fallback: null\n }\n for spec as key: value\n if !Object/hasOwn(result, key) or key == \"kind\"\n throw \"Unknown longpoll option: ${key}\"\n result[key] = value\n if result.auth == null\n result.auth = \"login\"\n validPath(result.path)\n if !is_string(result.auth) or result.auth == \"\" or result.auth.includes(\",\")\n throw \"Longpoll auth must name one permission\"\n if result.fallback != null\n validPath(result.fallback)\n limits = {\n waitTimeoutMs: 60000\n retentionMs: 86400000\n maxMessages: 10000\n }\n for limits as key: limit\n if !is_number(result[key]) and !is_string(result[key])\n throw \"Invalid longpoll option: ${key}\"\n value = Number(result[key])\n if !Number/isSafeInteger(value) or value < 1 or value > limit\n throw \"Invalid longpoll option: ${key}\"\n result[key] = value\n return result\n\nvalidPath(value)\n if !is_string(value) or !value.startsWith(\"/\") or value.includes(\"*\") or value.includes(\"{\") or value.includes(\"}\") or value.includes(\"?\") or value.includes(\"#\") or value.includes(\"\\\\\") or value.includes(\"//\")\n throw \"Longpoll requires an exact absolute URL path\"\n return null\n\nredisOptions(spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Redis configuration must be a map\"\n allowed = [\"connectionEnv\", \"namespace\", \"maxPendingPolls\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown Redis option: ${key}\"\n if !is_string(spec.connectionEnv) or spec.connectionEnv == \"\" or !is_string(spec.namespace) or spec.namespace == \"\" or spec.namespace.length > 128\n throw \"Redis requires connectionEnv and namespace\"\n name = spec.connectionEnv\n value = process.env[name]\n if !is_string(value) or value == \"\"\n throw \"Redis connection environment variable is missing\"\n url = new URL(value)\n protocols = [\"redis:\", \"rediss:\"]\n if !protocols.includes(url.protocol)\n throw \"Redis requires a redis:// or rediss:// URL\"\n maximum = 1024\n if spec.maxPendingPolls != null\n if !is_number(spec.maxPendingPolls) and !is_string(spec.maxPendingPolls)\n throw \"Invalid Redis maxPendingPolls\"\n maximum = Number(spec.maxPendingPolls)\n if !Number/isSafeInteger(maximum) or maximum < 1 or maximum > 10000\n throw \"Invalid Redis maxPendingPolls\"\n return {\n connectionEnv: spec.connectionEnv\n namespace: spec.namespace\n maxPendingPolls: maximum\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/cli/options.mjs
CHANGED
|
@@ -38,7 +38,7 @@ export function options(root, supplied = null) {
|
|
|
38
38
|
__requireArity(arguments.length, [0], "options");
|
|
39
39
|
root = __boundary(root);
|
|
40
40
|
supplied = __boundary(supplied);
|
|
41
|
-
let result = { root: __normalize(path.resolve(root)), host: "127.0.0.1", port: 3000, workers: 2, maxQueue: 64, bodyLimit:
|
|
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 };
|
|
42
42
|
if (__requireBoolean(supplied !== null)) {
|
|
43
43
|
for (const [key, value] of __entries(supplied)) {
|
|
44
44
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(result, key)))) || __requireBoolean(key === "root"))) {
|
|
@@ -115,7 +115,7 @@ export function deployment(directory) {
|
|
|
115
115
|
}
|
|
116
116
|
seen.push(name);
|
|
117
117
|
if (name === "static") {
|
|
118
|
-
attributes(tag, ["path", "directory", "index"]);
|
|
118
|
+
attributes(tag, ["path", "directory", "index", "fallback", "runtime"]);
|
|
119
119
|
settings.static = __normalize(tag.attributes());
|
|
120
120
|
continue;
|
|
121
121
|
} else if (name === "http") {
|
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:
|
|
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":";;;;;;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDA;;;;;;;;"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
isNumber as __isNumber,
|
|
4
|
+
isString as __isString,
|
|
5
|
+
isBool as __isBool,
|
|
6
|
+
isObject as __isObject,
|
|
7
|
+
isArray as __isArray,
|
|
8
|
+
entries as __entries,
|
|
9
|
+
range as __range,
|
|
10
|
+
requireBoolean as __requireBoolean,
|
|
11
|
+
normalize as __normalize,
|
|
12
|
+
finite as __finite,
|
|
13
|
+
boundary as __boundary,
|
|
14
|
+
requireArity as __requireArity,
|
|
15
|
+
} from "./.lango/runtime.mjs";
|
|
16
|
+
|
|
17
|
+
export function routePattern(path) {
|
|
18
|
+
__requireArity(arguments.length, [0], "routePattern");
|
|
19
|
+
path = __boundary(path);
|
|
20
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(path))) || __requireBoolean(!__requireBoolean(__normalize(path.startsWith("/"))))) || __requireBoolean(__normalize(path.includes("?")))) || __requireBoolean(__normalize(path.includes("#")))) || __requireBoolean(__normalize(path.includes("\\")))) || __requireBoolean(__normalize(path.includes("//"))))) {
|
|
21
|
+
throw "Endpoint requires an absolute URL path";
|
|
22
|
+
}
|
|
23
|
+
if (__requireBoolean(__requireBoolean(__normalize(path.includes("*"))) && __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(path.endsWith("/*")))) || __requireBoolean(__normalize(path.indexOf("*")) !== __normalize(path.lastIndexOf("*")))))) {
|
|
24
|
+
throw "Only trailing /* wildcards are supported";
|
|
25
|
+
}
|
|
26
|
+
let segments = __normalize(path.split("/"));
|
|
27
|
+
segments = __normalize(segments.slice(1));
|
|
28
|
+
let wildcard = __normalize(path.endsWith("/*"));
|
|
29
|
+
if (__requireBoolean(wildcard)) {
|
|
30
|
+
__normalize(segments.pop());
|
|
31
|
+
}
|
|
32
|
+
let names = [];
|
|
33
|
+
let shape = [];
|
|
34
|
+
let identifier = new (__hostGlobal(["RegExp"]))("^[A-Za-z_][A-Za-z0-9_]*$");
|
|
35
|
+
let reserved = ["__proto__", "constructor", "prototype"];
|
|
36
|
+
for (const segment of segments) {
|
|
37
|
+
if (__requireBoolean(__requireBoolean(__normalize(segment.includes("{"))) || __requireBoolean(__normalize(segment.includes("}"))))) {
|
|
38
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(segment.startsWith("{")))) || __requireBoolean(!__requireBoolean(__normalize(segment.endsWith("}")))))) {
|
|
39
|
+
throw "Path parameters must occupy an entire segment";
|
|
40
|
+
}
|
|
41
|
+
let name = __normalize(segment.slice(1, __finite(-1)));
|
|
42
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(identifier.test(name)))) || __requireBoolean(__normalize(reserved.includes(name))))) {
|
|
43
|
+
throw `Invalid path parameter name: ${name}`;
|
|
44
|
+
}
|
|
45
|
+
if (__requireBoolean(__normalize(names.includes(name)))) {
|
|
46
|
+
throw `Duplicate path parameter: ${name}`;
|
|
47
|
+
}
|
|
48
|
+
names.push(name);
|
|
49
|
+
shape.push("{}");
|
|
50
|
+
} else {
|
|
51
|
+
shape.push(segment);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (__requireBoolean(wildcard)) {
|
|
55
|
+
shape.push("*");
|
|
56
|
+
}
|
|
57
|
+
let key = __normalize(shape.join("/"));
|
|
58
|
+
key = `/${key}`;
|
|
59
|
+
return { segments: segments, wildcard: wildcard, key: key };
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function matchPath(pattern, path) {
|
|
64
|
+
__requireArity(arguments.length, [0, 1], "matchPath");
|
|
65
|
+
pattern = __boundary(pattern);
|
|
66
|
+
path = __boundary(path);
|
|
67
|
+
let parts = __normalize(path.split("/"));
|
|
68
|
+
if (__requireBoolean(__normalize(parts[0]) !== "")) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
parts = __normalize(parts.slice(1));
|
|
72
|
+
let count = pattern.segments.length;
|
|
73
|
+
if (__requireBoolean(__normalize(pattern.wildcard))) {
|
|
74
|
+
if (__requireBoolean(__normalize(parts.length) <= count)) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
if (__requireBoolean(__normalize(parts.length) !== count)) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
let parameters = { };
|
|
83
|
+
for (const [index, segment] of __entries(__normalize(pattern.segments))) {
|
|
84
|
+
let value = __normalize(parts[index]);
|
|
85
|
+
if (__requireBoolean(__normalize(segment.startsWith("{")))) {
|
|
86
|
+
if (__requireBoolean(value === "")) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
let name = __normalize(segment.slice(1, __finite(-1)));
|
|
90
|
+
parameters[name] = value;
|
|
91
|
+
} else {
|
|
92
|
+
if (__requireBoolean(segment !== value)) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return parameters;
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function moreSpecific(candidate, current) {
|
|
102
|
+
__requireArity(arguments.length, [0, 1], "moreSpecific");
|
|
103
|
+
candidate = __boundary(candidate);
|
|
104
|
+
current = __boundary(current);
|
|
105
|
+
if (__requireBoolean(current === null)) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
if (__requireBoolean(__normalize(candidate.wildcard) !== __normalize(current.wildcard))) {
|
|
109
|
+
return !__requireBoolean(__normalize(candidate.wildcard));
|
|
110
|
+
}
|
|
111
|
+
let first = __normalize(candidate.segments);
|
|
112
|
+
let second = __normalize(current.segments);
|
|
113
|
+
let count = __normalize(first.length);
|
|
114
|
+
if (__requireBoolean(__normalize(second.length) < count)) {
|
|
115
|
+
count = __normalize(second.length);
|
|
116
|
+
}
|
|
117
|
+
for (const index of __range(0, count)) {
|
|
118
|
+
let segment = __normalize(first[index]);
|
|
119
|
+
let alternative = __normalize(second[index]);
|
|
120
|
+
let literal = !__requireBoolean(__normalize(segment.startsWith("{")));
|
|
121
|
+
let other = !__requireBoolean(__normalize(alternative.startsWith("{")));
|
|
122
|
+
if (__requireBoolean(literal !== other)) {
|
|
123
|
+
return literal;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return __normalize(first.length) > __normalize(second.length);
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=route_pattern.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route_pattern.mjs","sources":["route_pattern.lango"],"sourcesContent":["import js global RegExp\n\nexport routePattern(path)\nexport matchPath(pattern, path)\nexport moreSpecific(candidate, current)\n\nroutePattern(path)\n if !is_string(path) or !path.startsWith(\"/\") or path.includes(\"?\") or path.includes(\"#\") or path.includes(\"\\\\\") or path.includes(\"//\")\n throw \"Endpoint requires an absolute URL path\"\n if path.includes(\"*\") and (!path.endsWith(\"/*\") or path.indexOf(\"*\") != path.lastIndexOf(\"*\"))\n throw \"Only trailing /* wildcards are supported\"\n segments = path.split(\"/\")\n segments = segments.slice(1)\n wildcard = path.endsWith(\"/*\")\n if wildcard\n segments.pop()\n names = []\n shape = []\n identifier = new RegExp(\"^[A-Za-z_][A-Za-z0-9_]*$\")\n reserved = [\"__proto__\", \"constructor\", \"prototype\"]\n for segments as segment\n if segment.includes(\"{\") or segment.includes(\"}\")\n if !segment.startsWith(\"{\") or !segment.endsWith(\"}\")\n throw \"Path parameters must occupy an entire segment\"\n name = segment.slice(1, -1)\n if !identifier.test(name) or reserved.includes(name)\n throw \"Invalid path parameter name: ${name}\"\n if names.includes(name)\n throw \"Duplicate path parameter: ${name}\"\n names.add(name)\n shape.add(\"{}\")\n else\n shape.add(segment)\n if wildcard\n shape.add(\"*\")\n key = shape.join(\"/\")\n key = \"/${key}\"\n return {\n segments: segments\n wildcard: wildcard\n key: key\n }\n\nmatchPath(pattern, path)\n parts = path.split(\"/\")\n if parts[0] != \"\"\n return null\n parts = parts.slice(1)\n count = pattern.segments.length\n if pattern.wildcard\n if parts.length <= count\n return null\n else\n if parts.length != count\n return null\n parameters = {}\n for pattern.segments as index: segment\n value = parts[index]\n if segment.startsWith(\"{\")\n if value == \"\"\n return null\n name = segment.slice(1, -1)\n parameters[name] = value\n else\n if segment != value\n return null\n return parameters\n\nmoreSpecific(candidate, current)\n if current == null\n return true\n if candidate.wildcard != current.wildcard\n return !candidate.wildcard\n first = candidate.segments\n second = current.segments\n count = first.length\n if second.length < count\n count = second.length\n for range(count) as index\n segment = first[index]\n alternative = second[index]\n literal = !segment.startsWith(\"{\")\n other = !alternative.startsWith(\"{\")\n if literal != other\n return literal\n return first.length > second.length\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/cli/routes.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { routePattern } from "./route_pattern.mjs";
|
|
1
2
|
import { contains } from "./options.mjs";
|
|
2
3
|
import { attributes } from "./options.mjs";
|
|
3
4
|
import { middlewareName } from "./middleware.mjs";
|
|
@@ -43,13 +44,25 @@ export class Routes {
|
|
|
43
44
|
__requireArity(arguments.length, [0], "endpoint");
|
|
44
45
|
spec = __boundary(spec);
|
|
45
46
|
let item = new Endpoint(spec);
|
|
47
|
+
this.#checkPath(__normalize(item.path));
|
|
48
|
+
__add(this.#items, { path: __normalize(item.path), module: resolveModule(__normalize(item.module), __normalize(this.#root)), handler: __normalize(item.handler), auth: __normalize(item.auth), middleware: __normalize(item.middleware) });
|
|
49
|
+
return this;
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
#checkPath(path) {
|
|
53
|
+
__requireArity(arguments.length, [0], "checkPath");
|
|
54
|
+
path = __boundary(path);
|
|
55
|
+
let pattern = routePattern(path);
|
|
46
56
|
for (const other of __normalize(this.#items)) {
|
|
47
|
-
if (__requireBoolean(__normalize(other.path) ===
|
|
48
|
-
throw `Duplicate endpoint: ${
|
|
57
|
+
if (__requireBoolean(__normalize(other.path) === path)) {
|
|
58
|
+
throw `Duplicate endpoint: ${path}`;
|
|
59
|
+
}
|
|
60
|
+
let existing = routePattern(__normalize(other.path));
|
|
61
|
+
if (__requireBoolean(__normalize(existing.key) === __normalize(pattern.key))) {
|
|
62
|
+
throw `Conflicting endpoint pattern: ${path}`;
|
|
49
63
|
}
|
|
50
64
|
}
|
|
51
|
-
|
|
52
|
-
return this;
|
|
65
|
+
return null;
|
|
53
66
|
return null;
|
|
54
67
|
}
|
|
55
68
|
addMiddleware(name, spec) {
|
|
@@ -64,14 +77,17 @@ export class Routes {
|
|
|
64
77
|
throw "Middleware requires a module and exported handler";
|
|
65
78
|
}
|
|
66
79
|
for (const [key, value] of __entries(spec)) {
|
|
67
|
-
if (__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler"))) {
|
|
80
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler")) && __requireBoolean(key !== "validate"))) {
|
|
68
81
|
throw `Unknown middleware option: ${key}`;
|
|
69
82
|
}
|
|
70
83
|
}
|
|
71
84
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
|
|
72
85
|
throw "Middleware handler is required";
|
|
73
86
|
}
|
|
74
|
-
|
|
87
|
+
if (__requireBoolean(__requireBoolean(__normalize(spec.validate) !== null) && __requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.validate)))) || __requireBoolean(__normalize(spec.validate) === "")))) {
|
|
88
|
+
throw "Middleware validate must name an exported function";
|
|
89
|
+
}
|
|
90
|
+
this.#middlewares[name] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler), validate: __normalize(spec.validate) };
|
|
75
91
|
return this;
|
|
76
92
|
return null;
|
|
77
93
|
}
|
|
@@ -83,11 +99,7 @@ export class Routes {
|
|
|
83
99
|
__requireArity(arguments.length, [0], "longpoll");
|
|
84
100
|
spec = __boundary(spec);
|
|
85
101
|
let item = longpollOptions(spec);
|
|
86
|
-
|
|
87
|
-
if (__requireBoolean(__normalize(other.path) === __normalize(item.path))) {
|
|
88
|
-
throw `Duplicate endpoint: ${__normalize(item.path)}`;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
102
|
+
this.#checkPath(__normalize(item.path));
|
|
91
103
|
__add(this.#items, item);
|
|
92
104
|
return this;
|
|
93
105
|
return null;
|
|
@@ -229,12 +241,12 @@ export class Routes {
|
|
|
229
241
|
leaf(tag);
|
|
230
242
|
this.#includeAt(spec, directory, prefix, stack, auth);
|
|
231
243
|
} else if (__normalize(tag.tag()) === "middleware") {
|
|
232
|
-
attributes(tag, ["name", "module", "handler"]);
|
|
244
|
+
attributes(tag, ["name", "module", "handler", "validate"]);
|
|
233
245
|
leaf(tag);
|
|
234
246
|
if (__requireBoolean(!__requireBoolean(allowEvents))) {
|
|
235
247
|
throw "Middleware declarations are only allowed in the application's root routes.xml";
|
|
236
248
|
}
|
|
237
|
-
this.addMiddleware(__normalize(spec.name), { module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
249
|
+
this.addMiddleware(__normalize(spec.name), { module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler), validate: __normalize(spec.validate) });
|
|
238
250
|
} else if (__normalize(tag.tag()) === "event") {
|
|
239
251
|
attributes(tag, ["name", "module", "handler"]);
|
|
240
252
|
leaf(tag);
|
package/dist/cli/routes.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.mjs","sources":["routes.lango"],"sourcesContent":["import \"./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
|
|
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;;;;;;;;;;"}
|
|
@@ -3,6 +3,7 @@ import * as path from "node:path";
|
|
|
3
3
|
import * as files from "serve-static";
|
|
4
4
|
import {
|
|
5
5
|
hostGlobal as __hostGlobal,
|
|
6
|
+
hostGlobalCall as __hostGlobalCall,
|
|
6
7
|
isNumber as __isNumber,
|
|
7
8
|
isString as __isString,
|
|
8
9
|
isBool as __isBool,
|
|
@@ -21,6 +22,7 @@ import {
|
|
|
21
22
|
export class StaticFiles {
|
|
22
23
|
#settings;
|
|
23
24
|
#serve;
|
|
25
|
+
#runtime;
|
|
24
26
|
constructor(settings) {
|
|
25
27
|
__requireArity(arguments.length, [0], "constructor");
|
|
26
28
|
settings = __boundary(settings);
|
|
@@ -28,7 +30,11 @@ export class StaticFiles {
|
|
|
28
30
|
if (__requireBoolean(!__requireBoolean(__normalize(__normalize(fs.statSync(__normalize(settings.directory))).isDirectory())))) {
|
|
29
31
|
throw "Static directory must be a directory";
|
|
30
32
|
}
|
|
33
|
+
if (__requireBoolean(__requireBoolean(__normalize(settings.fallback) !== null) && __requireBoolean(!__requireBoolean(__normalize(__normalize(fs.statSync(__normalize(path.join(__normalize(settings.directory), __normalize(settings.fallback))))).isFile()))))) {
|
|
34
|
+
throw "Static fallback must be a file";
|
|
35
|
+
}
|
|
31
36
|
this.#serve = __normalize(files.default(__normalize(settings.directory), { index: __normalize(settings.index), dotfiles: "deny", fallthrough: false, redirect: false }));
|
|
37
|
+
this.#runtime = __normalize(files.default(__normalize(settings.directory), { index: false, dotfiles: "allow", fallthrough: false, redirect: false }));
|
|
32
38
|
}
|
|
33
39
|
receive(req, res, failed) {
|
|
34
40
|
__requireArity(arguments.length, [0, 1, 2], "receive");
|
|
@@ -48,39 +54,88 @@ export class StaticFiles {
|
|
|
48
54
|
req.url = `/${__normalize(req.url)}`;
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
} else if (__normalize(error.status) === 404) {
|
|
70
|
-
status = 404;
|
|
57
|
+
let local = __normalize(__normalize(req.url.split("?"))[0]);
|
|
58
|
+
let decoded = null;
|
|
59
|
+
try {
|
|
60
|
+
decoded = __hostGlobalCall(["decodeURIComponent"], local);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
__call(failed, 400, "InvalidPath");
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (__requireBoolean(__requireBoolean(__normalize(decoded.includes("\\"))) || __requireBoolean(__normalize(decoded.includes(__normalize(__hostGlobal(["String"]).fromCharCode(0))))))) {
|
|
66
|
+
__call(failed, 400, "InvalidPath");
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
let segments = __normalize(decoded.split("/"));
|
|
70
|
+
let runtime = false;
|
|
71
|
+
for (const [index, segment] of __entries(segments)) {
|
|
72
|
+
if (__requireBoolean(__normalize(segment.startsWith(".")))) {
|
|
73
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(index === 1) && __requireBoolean(segment === ".lango")) && __requireBoolean(__normalize(this.#settings.runtime)))) {
|
|
74
|
+
runtime = true;
|
|
71
75
|
} else {
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
__call(failed, 403, "Forbidden");
|
|
77
|
+
return null;
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
|
-
|
|
80
|
+
}
|
|
81
|
+
let done = __withArity((error) => {
|
|
82
|
+
error = __boundary(error);
|
|
83
|
+
this.#failure(req, res, failed, error, decoded);
|
|
77
84
|
return null;
|
|
78
85
|
}, [0], "closure");
|
|
79
86
|
let serve = __normalize(this.#serve);
|
|
87
|
+
if (__requireBoolean(runtime)) {
|
|
88
|
+
serve = __normalize(this.#runtime);
|
|
89
|
+
}
|
|
80
90
|
__call(serve, req, res, done);
|
|
81
91
|
return null;
|
|
82
92
|
return null;
|
|
83
93
|
}
|
|
94
|
+
#failure(req, res, failed, error, pathname = null) {
|
|
95
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "failure");
|
|
96
|
+
req = __boundary(req);
|
|
97
|
+
res = __boundary(res);
|
|
98
|
+
failed = __boundary(failed);
|
|
99
|
+
error = __boundary(error);
|
|
100
|
+
pathname = __boundary(pathname);
|
|
101
|
+
if (__requireBoolean(__normalize(res.headersSent))) {
|
|
102
|
+
__normalize(res.destroy());
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
let status = 404;
|
|
106
|
+
let code = "NotFound";
|
|
107
|
+
if (__requireBoolean(error !== null)) {
|
|
108
|
+
if (__normalize(error.status) === 400) {
|
|
109
|
+
status = 400;
|
|
110
|
+
code = "InvalidPath";
|
|
111
|
+
} else if (__normalize(error.status) === 403) {
|
|
112
|
+
status = 403;
|
|
113
|
+
code = "Forbidden";
|
|
114
|
+
} else if (__normalize(error.status) === 416) {
|
|
115
|
+
status = 416;
|
|
116
|
+
code = "RangeNotSatisfiable";
|
|
117
|
+
} else if (__normalize(error.status) === 404) {
|
|
118
|
+
status = 404;
|
|
119
|
+
} else {
|
|
120
|
+
status = 500;
|
|
121
|
+
code = "InternalServerError";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(status === 404) && __requireBoolean(pathname !== null)) && __requireBoolean(__normalize(this.#settings.fallback) !== null)) && __requireBoolean(!__requireBoolean(__normalize(pathname.startsWith("/.lango"))))) && __requireBoolean(__normalize(path.posix.extname(pathname)) === "")) && __requireBoolean(acceptsHTML(req.headers.accept)))) {
|
|
125
|
+
req.url = `/${__normalize(this.#settings.fallback)}`;
|
|
126
|
+
let done = __withArity((error) => {
|
|
127
|
+
error = __boundary(error);
|
|
128
|
+
this.#failure(req, res, failed, error);
|
|
129
|
+
return null;
|
|
130
|
+
}, [0], "closure");
|
|
131
|
+
let serve = __normalize(this.#serve);
|
|
132
|
+
__call(serve, req, res, done);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
__call(failed, status, code);
|
|
136
|
+
return null;
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
84
139
|
}
|
|
85
140
|
export function staticOptions(root, spec) {
|
|
86
141
|
__requireArity(arguments.length, [0, 1], "staticOptions");
|
|
@@ -92,7 +147,7 @@ export function staticOptions(root, spec) {
|
|
|
92
147
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
93
148
|
throw "static must be a map";
|
|
94
149
|
}
|
|
95
|
-
let allowed = ["path", "directory", "index"];
|
|
150
|
+
let allowed = ["path", "directory", "index", "fallback", "runtime"];
|
|
96
151
|
for (const [key, value] of __entries(spec)) {
|
|
97
152
|
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
98
153
|
throw `Unknown static option: ${key}`;
|
|
@@ -119,7 +174,48 @@ export function staticOptions(root, spec) {
|
|
|
119
174
|
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(index))) || __requireBoolean(index === "")) || __requireBoolean(__normalize(index.startsWith(".")))) || __requireBoolean(__normalize(index.includes("/")))) || __requireBoolean(__normalize(index.includes("\\"))))) {
|
|
120
175
|
throw "Static index must be a file name";
|
|
121
176
|
}
|
|
122
|
-
|
|
177
|
+
let fallback = __normalize(spec.fallback);
|
|
178
|
+
if (__requireBoolean(__requireBoolean(fallback !== null) && __requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(fallback))) || __requireBoolean(fallback === "")) || __requireBoolean(__normalize(fallback.startsWith(".")))) || __requireBoolean(__normalize(fallback.includes("/")))) || __requireBoolean(__normalize(fallback.includes("\\")))))) {
|
|
179
|
+
throw "Static fallback must be a file name";
|
|
180
|
+
}
|
|
181
|
+
let runtime = __normalize(spec.runtime);
|
|
182
|
+
if (__requireBoolean(__requireBoolean(runtime === null) || __requireBoolean(runtime === "false"))) {
|
|
183
|
+
runtime = false;
|
|
184
|
+
}
|
|
185
|
+
if (__requireBoolean(runtime === "true")) {
|
|
186
|
+
runtime = true;
|
|
187
|
+
}
|
|
188
|
+
if (__requireBoolean(!__requireBoolean(__isBool(runtime)))) {
|
|
189
|
+
throw "Static runtime must be boolean";
|
|
190
|
+
}
|
|
191
|
+
return { path: mount, directory: __normalize(path.resolve(root, __normalize(spec.directory))), index: index, fallback: fallback, runtime: runtime };
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function acceptsHTML(value) {
|
|
196
|
+
__requireArity(arguments.length, [0], "acceptsHTML");
|
|
197
|
+
value = __boundary(value);
|
|
198
|
+
if (__requireBoolean(!__requireBoolean(__isString(value)))) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
let zero = new (__hostGlobal(["RegExp"]))("^q\\s*=\\s*0(?:\\.0*)?$", "i");
|
|
202
|
+
for (const range of __normalize(value.split(","))) {
|
|
203
|
+
let parts = __normalize(range.split(";"));
|
|
204
|
+
let media = __normalize(parts[0]);
|
|
205
|
+
let type = __normalize(__normalize(media.trim()).toLowerCase());
|
|
206
|
+
if (__requireBoolean(__requireBoolean(type === "text/html") || __requireBoolean(type === "application/xhtml+xml"))) {
|
|
207
|
+
let allowed = true;
|
|
208
|
+
for (const parameter of __normalize(parts.slice(1))) {
|
|
209
|
+
if (__requireBoolean(__normalize(zero.test(__normalize(parameter.trim()))))) {
|
|
210
|
+
allowed = false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (__requireBoolean(allowed)) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
123
219
|
return null;
|
|
124
220
|
}
|
|
125
221
|
//# sourceMappingURL=static_files.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n done = (error)\n capture res\n capture failed\n if res.headersSent\n
|
|
1
|
+
{"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\nimport js global decodeURIComponent\nimport js global String\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n fallback = spec.fallback\n if fallback != null and (!is_string(fallback) or fallback == \"\" or fallback.startsWith(\".\") or fallback.includes(\"/\") or fallback.includes(\"\\\\\"))\n throw \"Static fallback must be a file name\"\n runtime = spec.runtime\n if runtime == null or runtime == \"false\"\n runtime = false\n if runtime == \"true\"\n runtime = true\n if !is_bool(runtime)\n throw \"Static runtime must be boolean\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n fallback: fallback\n runtime: runtime\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()\n throw \"Static fallback must be a file\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n @runtime = files/default(settings.directory, {\n index: false\n dotfiles: \"allow\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n local = req.url.split(\"?\")[0]\n decoded = null\n try\n decoded = decodeURIComponent(local)\n catch error\n failed(400, \"InvalidPath\")\n return null\n if decoded.includes(\"\\\\\") or decoded.includes(String/fromCharCode(0))\n failed(400, \"InvalidPath\")\n return null\n segments = decoded.split(\"/\")\n runtime = false\n for segments as index: segment\n if segment.startsWith(\".\")\n if index == 1 and segment == \".lango\" and @settings.runtime\n runtime = true\n else\n failed(403, \"Forbidden\")\n return null\n done = (error)\n capture req\n capture res\n capture failed\n capture decoded\n failure(req, res, failed, error, decoded)\n serve = @serve\n if runtime\n serve = @runtime\n serve(req, res, done)\n return null\n\nStaticFiles:failure(req, res, failed, error, pathname = null)\n if res.headersSent\n res.destroy()\n return null\n status = 404\n code = \"NotFound\"\n if error != null\n switch error.status\n case 400\n status = 400\n code = \"InvalidPath\"\n case 403\n status = 403\n code = \"Forbidden\"\n case 416\n status = 416\n code = \"RangeNotSatisfiable\"\n case 404\n status = 404\n else\n status = 500\n code = \"InternalServerError\"\n if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith(\"/.lango\") and path/posix/extname(pathname) == \"\" and acceptsHTML(req.headers.accept)\n req.url = \"/${@settings.fallback}\"\n done = (error)\n capture req\n capture res\n capture failed\n failure(req, res, failed, error)\n serve = @serve\n serve(req, res, done)\n return null\n failed(status, code)\n return null\n\nacceptsHTML(value)\n if !is_string(value)\n return false\n zero = new RegExp(\"^q\\\\s*=\\\\s*0(?:\\\\.0*)?$\", \"i\")\n for value.split(\",\") as range\n parts = range.split(\";\")\n media = parts[0]\n type = media.trim().toLowerCase()\n if type == \"text/html\" or type == \"application/xhtml+xml\"\n allowed = true\n for parts.slice(1) as parameter\n if zero.test(parameter.trim())\n allowed = false\n if allowed\n return true\n return false\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0IA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|