@lango-project/lango-server 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +431 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/runtime.mjs +683 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/body_reader.mjs +179 -0
- package/dist/body_reader.mjs.map +1 -0
- package/dist/builder.mjs +69 -0
- package/dist/builder.mjs.map +1 -0
- package/dist/cli/.lango/bignumber.mjs +3119 -0
- package/dist/cli/.lango/runtime.mjs +683 -0
- package/dist/cli/.lango/xmldom/conventions.js +429 -0
- package/dist/cli/.lango/xmldom/dom-parser.js +591 -0
- package/dist/cli/.lango/xmldom/dom.js +3759 -0
- package/dist/cli/.lango/xmldom/entities.js +2171 -0
- package/dist/cli/.lango/xmldom/errors.js +205 -0
- package/dist/cli/.lango/xmldom/grammar.js +559 -0
- package/dist/cli/.lango/xmldom/index.js +41 -0
- package/dist/cli/.lango/xmldom/package.json +1 -0
- package/dist/cli/.lango/xmldom/sax.js +983 -0
- package/dist/cli/.lango/xmldom.LICENSE.md +8 -0
- package/dist/cli/body_reader.mjs +179 -0
- package/dist/cli/body_reader.mjs.map +1 -0
- package/dist/cli/database.mjs +101 -0
- package/dist/cli/database.mjs.map +1 -0
- package/dist/cli/database_options.mjs +83 -0
- package/dist/cli/database_options.mjs.map +1 -0
- package/dist/cli/database_scope.mjs +82 -0
- package/dist/cli/database_scope.mjs.map +1 -0
- package/dist/cli/endpoint.mjs +44 -0
- package/dist/cli/endpoint.mjs.map +1 -0
- package/dist/cli/options.mjs +165 -0
- package/dist/cli/options.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +166 -0
- package/dist/cli/postgres_adapter.mjs.map +1 -0
- package/dist/cli/routes.mjs +233 -0
- package/dist/cli/routes.mjs.map +1 -0
- package/dist/cli/server.mjs +572 -0
- package/dist/cli/server.mjs.map +1 -0
- package/dist/cli/sqlite_adapter.mjs +163 -0
- package/dist/cli/sqlite_adapter.mjs.map +1 -0
- package/dist/database.mjs +101 -0
- package/dist/database.mjs.map +1 -0
- package/dist/database_options.mjs +83 -0
- package/dist/database_options.mjs.map +1 -0
- package/dist/database_scope.mjs +82 -0
- package/dist/database_scope.mjs.map +1 -0
- package/dist/endpoint.mjs +44 -0
- package/dist/endpoint.mjs.map +1 -0
- package/dist/group.mjs +75 -0
- package/dist/group.mjs.map +1 -0
- package/dist/library.mjs +6 -0
- package/dist/library.mjs.map +1 -0
- package/dist/options.mjs +165 -0
- package/dist/options.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +166 -0
- package/dist/postgres_adapter.mjs.map +1 -0
- package/dist/postgres_worker.mjs +119 -0
- package/dist/postgres_worker.mjs.map +1 -0
- package/dist/response.mjs +62 -0
- package/dist/response.mjs.map +1 -0
- package/dist/routes.mjs +233 -0
- package/dist/routes.mjs.map +1 -0
- package/dist/runtime.mjs +475 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/server.mjs +568 -0
- package/dist/server.mjs.map +1 -0
- package/dist/sqlite_adapter.mjs +163 -0
- package/dist/sqlite_adapter.mjs.map +1 -0
- package/dist/token.mjs +106 -0
- package/dist/token.mjs.map +1 -0
- package/dist/tokens.mjs +76 -0
- package/dist/tokens.mjs.map +1 -0
- package/dist/worker.mjs +57 -0
- package/dist/worker.mjs.map +1 -0
- package/package.json +39 -4
- package/src/body_reader.lango +113 -0
- package/src/builder.lango +53 -0
- package/src/database.lango +66 -0
- package/src/database_options.lango +57 -0
- package/src/database_scope.lango +59 -0
- package/src/endpoint.lango +23 -0
- package/src/group.lango +47 -0
- package/src/library.lango +10 -0
- package/src/options.lango +128 -0
- package/src/postgres_adapter.lango +138 -0
- package/src/postgres_worker.lango +114 -0
- package/src/response.lango +42 -0
- package/src/routes.lango +160 -0
- package/src/runtime.lango +320 -0
- package/src/server.lango +432 -0
- package/src/sqlite_adapter.lango +127 -0
- package/src/token.lango +70 -0
- package/src/tokens.lango +70 -0
- package/src/worker.lango +43 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { databaseOptions } from "./database_options.mjs";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
hostGlobal as __hostGlobal,
|
|
6
|
+
hostGlobalCall as __hostGlobalCall,
|
|
7
|
+
xml2obj as __xml2obj,
|
|
8
|
+
fromJson as __fromJson,
|
|
9
|
+
toJson as __toJson,
|
|
10
|
+
isNumber as __isNumber,
|
|
11
|
+
isString as __isString,
|
|
12
|
+
isBool as __isBool,
|
|
13
|
+
isObject as __isObject,
|
|
14
|
+
isArray as __isArray,
|
|
15
|
+
entries as __entries,
|
|
16
|
+
requireBoolean as __requireBoolean,
|
|
17
|
+
normalize as __normalize,
|
|
18
|
+
boundary as __boundary,
|
|
19
|
+
requireArity as __requireArity,
|
|
20
|
+
} from "./.lango/runtime.mjs";
|
|
21
|
+
|
|
22
|
+
export function attributes(tag, allowed) {
|
|
23
|
+
__requireArity(arguments.length, [0, 1], "attributes");
|
|
24
|
+
tag = __boundary(tag);
|
|
25
|
+
allowed = __boundary(allowed);
|
|
26
|
+
for (const [key, value] of __entries(__normalize(tag.attributes()))) {
|
|
27
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
28
|
+
throw `Unknown ${__normalize(tag.tag())} attribute: ${key}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function options(root, supplied = null) {
|
|
36
|
+
__requireArity(arguments.length, [0], "options");
|
|
37
|
+
root = __boundary(root);
|
|
38
|
+
supplied = __boundary(supplied);
|
|
39
|
+
let result = { root: __normalize(path.resolve(root)), host: "127.0.0.1", port: 3000, workers: 2, maxQueue: 64, bodyLimit: 1048576, 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 };
|
|
40
|
+
if (__requireBoolean(supplied !== null)) {
|
|
41
|
+
for (const [key, value] of __entries(supplied)) {
|
|
42
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(result, key)))) || __requireBoolean(key === "root"))) {
|
|
43
|
+
throw `Unknown server option: ${key}`;
|
|
44
|
+
}
|
|
45
|
+
result[key] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(result.host)))) || __requireBoolean(__normalize(result.host) === ""))) {
|
|
49
|
+
throw "Host must be a nonempty string";
|
|
50
|
+
}
|
|
51
|
+
for (const key of ["port", "workers", "maxQueue", "bodyLimit", "bodyTimeoutMs", "requestTimeoutMs", "eventTimeoutMs", "startupTimeoutMs", "shutdownTimeoutMs", "ttlSeconds"]) {
|
|
52
|
+
let value = __normalize(result[key]);
|
|
53
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isNumber(value))) || __requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(value))))) || __requireBoolean(value < 0))) {
|
|
54
|
+
throw `Invalid server option: ${key}`;
|
|
55
|
+
}
|
|
56
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(key !== "port") && __requireBoolean(key !== "maxQueue")) && __requireBoolean(value === 0))) {
|
|
57
|
+
throw `Server option must be positive: ${key}`;
|
|
58
|
+
}
|
|
59
|
+
if (__requireBoolean(__requireBoolean(__normalize(key.endsWith("Ms"))) && __requireBoolean(value > 2147483647))) {
|
|
60
|
+
throw `Timeout exceeds timer limit: ${key}`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(result.port) > 65535) || __requireBoolean(__normalize(result.workers) > 128)) || __requireBoolean(__normalize(result.ttlSeconds) > 86400))) {
|
|
64
|
+
throw "Server option exceeds supported limit";
|
|
65
|
+
}
|
|
66
|
+
if (__requireBoolean(!__requireBoolean(__isBool(__normalize(result.requestEvent))))) {
|
|
67
|
+
throw "requestEvent must be boolean";
|
|
68
|
+
}
|
|
69
|
+
for (const key of ["issuer", "audience", "activeKeyId"]) {
|
|
70
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(result[key])))) || __requireBoolean(__normalize(result[key]) === ""))) {
|
|
71
|
+
throw `Missing JWT option: ${key}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(__normalize(result.keys)))) || __requireBoolean(__isArray(__normalize(result.keys))))) {
|
|
75
|
+
throw "JWT keys must be a map";
|
|
76
|
+
}
|
|
77
|
+
let keys = __normalize(__hostGlobal(["Object"]).assign(__normalize(__hostGlobal(["Object"]).create(null)), __normalize(result.keys)));
|
|
78
|
+
if (__requireBoolean(__normalize(result.key) !== null)) {
|
|
79
|
+
keys[__normalize(result.activeKeyId)] = __normalize(result.key);
|
|
80
|
+
}
|
|
81
|
+
if (__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(keys, __normalize(result.activeKeyId)))))) {
|
|
82
|
+
throw "JWT signing key is required";
|
|
83
|
+
}
|
|
84
|
+
for (const [id, secret] of __entries(keys)) {
|
|
85
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(secret))) || __requireBoolean(__normalize(secret.length) < 32))) {
|
|
86
|
+
throw "JWT keys must contain at least 32 characters";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
result.keys = keys;
|
|
90
|
+
result.key = null;
|
|
91
|
+
result.database = databaseOptions(__normalize(result.root), __normalize(result.database), __normalize(result.workers));
|
|
92
|
+
return result;
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function deployment(directory) {
|
|
97
|
+
__requireArity(arguments.length, [0], "deployment");
|
|
98
|
+
directory = __boundary(directory);
|
|
99
|
+
let file = __normalize(path.join(directory, "deploy.xml"));
|
|
100
|
+
let root = __xml2obj(__normalize(fs.readFileSync(file, "utf8")));
|
|
101
|
+
if (__requireBoolean(__normalize(root.tag()) !== "deploy")) {
|
|
102
|
+
throw "Expected deploy root";
|
|
103
|
+
}
|
|
104
|
+
attributes(root, []);
|
|
105
|
+
let settings = { };
|
|
106
|
+
let seen = [];
|
|
107
|
+
for (const tag of __normalize(root.children())) {
|
|
108
|
+
let name = __normalize(tag.tag());
|
|
109
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(seen.includes(name))) || __requireBoolean(__normalize(__normalize(tag.children()).length) !== 0)) || __requireBoolean(__normalize(__normalize(tag.innerText()).trim()) !== ""))) {
|
|
110
|
+
throw `Duplicate or nonempty deployment section: ${name}`;
|
|
111
|
+
}
|
|
112
|
+
seen.push(name);
|
|
113
|
+
if (name === "http") {
|
|
114
|
+
attributes(tag, ["host", "port", "bodyLimit", "bodyTimeoutMs"]);
|
|
115
|
+
} else if (name === "workers") {
|
|
116
|
+
attributes(tag, ["workers", "maxQueue", "requestTimeoutMs", "startupTimeoutMs", "shutdownTimeoutMs"]);
|
|
117
|
+
} else if (name === "events") {
|
|
118
|
+
attributes(tag, ["requestEvent", "eventTimeoutMs"]);
|
|
119
|
+
} else if (name === "database") {
|
|
120
|
+
attributes(tag, ["type", "file", "connectionEnv", "busyTimeoutMs", "queryTimeoutMs", "connectTimeoutSeconds"]);
|
|
121
|
+
settings.database = __normalize(tag.attributes());
|
|
122
|
+
continue;
|
|
123
|
+
} else if (name === "jwt") {
|
|
124
|
+
attributes(tag, ["issuer", "audience", "ttlSeconds", "keyEnv", "keysFile", "activeKeyId"]);
|
|
125
|
+
} else {
|
|
126
|
+
throw `Unknown deployment section: ${name}`;
|
|
127
|
+
}
|
|
128
|
+
for (const [key, value] of __entries(__normalize(tag.attributes()))) {
|
|
129
|
+
if (__requireBoolean(key === "keyEnv")) {
|
|
130
|
+
settings.key = __normalize(__hostGlobal(["process"]).env[value]);
|
|
131
|
+
if (__requireBoolean(__normalize(settings.key) === null)) {
|
|
132
|
+
throw `JWT key environment variable is missing: ${value}`;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
if (__requireBoolean(key === "keysFile")) {
|
|
136
|
+
settings.keys = __fromJson(__normalize(fs.readFileSync(__normalize(path.resolve(directory, value)), "utf8")));
|
|
137
|
+
} else {
|
|
138
|
+
if (__requireBoolean(contains(["host", "issuer", "audience", "activeKeyId"], key))) {
|
|
139
|
+
settings[key] = value;
|
|
140
|
+
} else {
|
|
141
|
+
if (__requireBoolean(key === "requestEvent")) {
|
|
142
|
+
if (__requireBoolean(__requireBoolean(value !== "true") && __requireBoolean(value !== "false"))) {
|
|
143
|
+
throw "requestEvent must be true or false";
|
|
144
|
+
}
|
|
145
|
+
settings[key] = value === "true";
|
|
146
|
+
} else {
|
|
147
|
+
settings[key] = __hostGlobalCall(["Number"], value);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return options(directory, settings);
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function contains(items, value) {
|
|
159
|
+
__requireArity(arguments.length, [0, 1], "contains");
|
|
160
|
+
items = __boundary(items);
|
|
161
|
+
value = __boundary(value);
|
|
162
|
+
return __normalize(items.includes(value));
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=options.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.mjs","sources":["options.lango"],"sourcesContent":["import \"./database_options\"\n databaseOptions(root, spec, workers)\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: 1048576\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 }\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 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 \"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 \"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":";;;;;;;;;;;;;;;;;;;;;AAaA;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;;;;;"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import * as modules from "node:module";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import * as threads from "node:worker_threads";
|
|
4
|
+
import {
|
|
5
|
+
hostGlobal as __hostGlobal,
|
|
6
|
+
requireBoolean as __requireBoolean,
|
|
7
|
+
normalize as __normalize,
|
|
8
|
+
finite as __finite,
|
|
9
|
+
numericAdd as __numericAdd,
|
|
10
|
+
boundary as __boundary,
|
|
11
|
+
requireArity as __requireArity,
|
|
12
|
+
withArity as __withArity,
|
|
13
|
+
} from "./.lango/runtime.mjs";
|
|
14
|
+
|
|
15
|
+
export class PostgresAdapter {
|
|
16
|
+
parameterStyle;
|
|
17
|
+
#closed;
|
|
18
|
+
#broken;
|
|
19
|
+
#state;
|
|
20
|
+
#depth;
|
|
21
|
+
#rollbackOnly;
|
|
22
|
+
#timeout;
|
|
23
|
+
#signal;
|
|
24
|
+
#port;
|
|
25
|
+
#lango;
|
|
26
|
+
#worker;
|
|
27
|
+
constructor(mod, settings, root) {
|
|
28
|
+
__requireArity(arguments.length, [0, 1, 2], "constructor");
|
|
29
|
+
mod = __boundary(mod);
|
|
30
|
+
settings = __boundary(settings);
|
|
31
|
+
root = __boundary(root);
|
|
32
|
+
this.#closed = false;
|
|
33
|
+
this.#broken = false;
|
|
34
|
+
this.#state = null;
|
|
35
|
+
this.#depth = 0;
|
|
36
|
+
this.#rollbackOnly = false;
|
|
37
|
+
this.parameterStyle = "postgresql";
|
|
38
|
+
this.#timeout = __normalize(__hostGlobal(["Math"]).min(2147483647, __numericAdd(__normalize(settings.queryTimeoutMs), 1000)));
|
|
39
|
+
this.#signal = new (__hostGlobal(["Int32Array"]))(new (__hostGlobal(["SharedArrayBuffer"]))(4));
|
|
40
|
+
let channel = __normalize(__hostGlobal(["Reflect"]).construct(__normalize(mod.MessageChannel), []));
|
|
41
|
+
this.#port = __normalize(channel.port1);
|
|
42
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(root, "package.json"))));
|
|
43
|
+
let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
|
|
44
|
+
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
|
+
let worker = __normalize(this.#worker);
|
|
46
|
+
__normalize(worker.on("error", __withArity((error) => {
|
|
47
|
+
error = __boundary(error);
|
|
48
|
+
this.#broken = true;
|
|
49
|
+
return null;
|
|
50
|
+
}, [0], "closure")));
|
|
51
|
+
__normalize(this.#worker.unref());
|
|
52
|
+
__normalize(this.#port.unref());
|
|
53
|
+
try {
|
|
54
|
+
this.#exchange({ action: "connect" }, __normalize(__hostGlobal(["Math"]).min(2147483647, __numericAdd(__finite(__normalize(settings.connectTimeoutSeconds) * 1000), 1000))));
|
|
55
|
+
} catch (error) {
|
|
56
|
+
this.close();
|
|
57
|
+
throw new (__hostGlobal(["Error"]))("PostgreSQL initialization failed");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
#exchange(task, timeout) {
|
|
61
|
+
__requireArity(arguments.length, [0, 1], "exchange");
|
|
62
|
+
task = __boundary(task);
|
|
63
|
+
timeout = __boundary(timeout);
|
|
64
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#closed)) || __requireBoolean(__normalize(this.#broken)))) {
|
|
65
|
+
throw new (__hostGlobal(["Error"]))("PostgreSQL connection is unavailable");
|
|
66
|
+
}
|
|
67
|
+
__normalize(__hostGlobal(["Atomics"]).store(__normalize(this.#signal), 0, 0));
|
|
68
|
+
__normalize(this.#port.postMessage(task));
|
|
69
|
+
let waited = __normalize(__hostGlobal(["Atomics"]).wait(__normalize(this.#signal), 0, 0, timeout));
|
|
70
|
+
if (__requireBoolean(waited === "timed-out")) {
|
|
71
|
+
this.#broken = true;
|
|
72
|
+
__normalize(this.#worker.terminate());
|
|
73
|
+
throw new (__hostGlobal(["Error"]))("PostgreSQL operation timed out");
|
|
74
|
+
}
|
|
75
|
+
let reply = __normalize(threads.receiveMessageOnPort(__normalize(this.#port)));
|
|
76
|
+
if (__requireBoolean(reply === null)) {
|
|
77
|
+
this.#broken = true;
|
|
78
|
+
throw new (__hostGlobal(["Error"]))("PostgreSQL response was not received");
|
|
79
|
+
}
|
|
80
|
+
let result = __normalize(reply.message);
|
|
81
|
+
this.#state = __normalize(result.state);
|
|
82
|
+
if (__requireBoolean(__normalize(result.failed))) {
|
|
83
|
+
this.#broken = __normalize(result.dead);
|
|
84
|
+
let error = new (__hostGlobal(["Error"]))("PostgreSQL query failed");
|
|
85
|
+
error.code = __normalize(result.code);
|
|
86
|
+
error.position = __normalize(result.position);
|
|
87
|
+
error.transactionUnusable = __normalize(this.#state) === "failed";
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
execute(sql, parameters) {
|
|
94
|
+
__requireArity(arguments.length, [0, 1], "execute");
|
|
95
|
+
sql = __boundary(sql);
|
|
96
|
+
parameters = __boundary(parameters);
|
|
97
|
+
return this.#exchange({ action: "query", sql: sql, parameters: parameters }, __normalize(this.#timeout));
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
begin() {
|
|
101
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#depth) === 0) && __requireBoolean(__normalize(this.#state) === null))) {
|
|
102
|
+
this.execute("BEGIN", []);
|
|
103
|
+
}
|
|
104
|
+
this.#depth = __numericAdd(__normalize(this.#depth), 1);
|
|
105
|
+
return null;
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
commit() {
|
|
109
|
+
if (__requireBoolean(__normalize(this.#depth) === 0)) {
|
|
110
|
+
throw new (__hostGlobal(["Error"]))("NoActiveTransaction");
|
|
111
|
+
}
|
|
112
|
+
if (__requireBoolean(__normalize(this.#depth) > 1)) {
|
|
113
|
+
this.#depth = __finite(__normalize(this.#depth) - 1);
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#rollbackOnly)) || __requireBoolean(__normalize(this.#state) === "failed"))) {
|
|
117
|
+
this.rollback();
|
|
118
|
+
throw new (__hostGlobal(["Error"]))("TransactionRolledBack");
|
|
119
|
+
}
|
|
120
|
+
this.execute("COMMIT", []);
|
|
121
|
+
this.#depth = 0;
|
|
122
|
+
return null;
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
rollback() {
|
|
126
|
+
if (__requireBoolean(__normalize(this.#depth) > 1)) {
|
|
127
|
+
this.#depth = __finite(__normalize(this.#depth) - 1);
|
|
128
|
+
this.#rollbackOnly = true;
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
if (__requireBoolean(__normalize(this.#state) !== null)) {
|
|
132
|
+
this.execute("ROLLBACK", []);
|
|
133
|
+
}
|
|
134
|
+
this.#depth = 0;
|
|
135
|
+
this.#rollbackOnly = false;
|
|
136
|
+
return null;
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
transactionState() {
|
|
140
|
+
if (__requireBoolean(__normalize(this.#broken))) {
|
|
141
|
+
throw new (__hostGlobal(["Error"]))("PostgreSQL connection is unavailable");
|
|
142
|
+
}
|
|
143
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#rollbackOnly)) && __requireBoolean(__normalize(this.#state) !== "failed"))) {
|
|
144
|
+
return "rollback_only";
|
|
145
|
+
}
|
|
146
|
+
return __normalize(this.#state);
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
close() {
|
|
150
|
+
if (__requireBoolean(__normalize(this.#closed))) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
if (__requireBoolean(!__requireBoolean(__normalize(this.#broken)))) {
|
|
155
|
+
this.#exchange({ action: "close" }, __normalize(this.#timeout));
|
|
156
|
+
}
|
|
157
|
+
} finally {
|
|
158
|
+
this.#closed = true;
|
|
159
|
+
__normalize(this.#port.close());
|
|
160
|
+
__normalize(this.#worker.terminate());
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=postgres_adapter.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres_adapter.mjs","sources":["postgres_adapter.lango"],"sourcesContent":["import js \"node:module\" as modules\nimport js \"node:path\" as path\nimport js \"node:worker_threads\" as threads\nimport js global Reflect\nimport js global SharedArrayBuffer\nimport js global Int32Array\nimport js global Atomics\nimport js global Math\nimport js global Error\n\nexport class PostgresAdapter(mod, settings, root)\n properties\n parameterStyle\n methods\n execute(sql, parameters)\n begin()\n commit()\n rollback()\n transactionState()\n close()\n\nPostgresAdapter(mod, settings, root)\n @closed = false\n @broken = false\n @state = null\n @depth = 0\n @rollbackOnly = false\n @parameterStyle = \"postgresql\"\n @timeout = Math/min(2147483647, settings.queryTimeoutMs + 1000)\n @signal = new Int32Array(new SharedArrayBuffer(4))\n channel = Reflect/construct(mod.MessageChannel, [])\n @port = channel.port1\n resolver = modules/createRequire(path/join(root, \"package.json\"))\n entry = resolver.resolve(\"@lango-project/lango-server\")\n @worker = Reflect/construct(mod.Worker, [path/join(path/dirname(entry), \"postgres_worker.mjs\"), {\n workerData: {\n settings: settings\n port: channel.port2\n signal: @signal\n }\n transferList: [channel.port2]\n }])\n worker = @worker\n worker.on(\"error\", (error)\n @broken = true\n )\n @worker.unref()\n @port.unref()\n try\n exchange({\n action: \"connect\"\n }, Math/min(2147483647, settings.connectTimeoutSeconds * 1000 + 1000))\n catch error\n close()\n throw new Error(\"PostgreSQL initialization failed\")\n\nPostgresAdapter:exchange(task, timeout)\n if @closed or @broken\n throw new Error(\"PostgreSQL connection is unavailable\")\n Atomics/store(@signal, 0, 0)\n @port.postMessage(task)\n waited = Atomics/wait(@signal, 0, 0, timeout)\n if waited == \"timed-out\"\n @broken = true\n @worker.terminate()\n throw new Error(\"PostgreSQL operation timed out\")\n reply = threads/receiveMessageOnPort(@port)\n if reply == null\n @broken = true\n throw new Error(\"PostgreSQL response was not received\")\n result = reply.message\n @state = result.state\n if result.failed\n @broken = result.dead\n error = new Error(\"PostgreSQL query failed\")\n error.code = result.code\n error.position = result.position\n error.transactionUnusable = @state == \"failed\"\n throw error\n return result\n\nPostgresAdapter:execute(sql, parameters)\n return exchange({\n action: \"query\"\n sql: sql\n parameters: parameters\n }, @timeout)\n\nPostgresAdapter:begin()\n if @depth == 0 and @state == null\n execute(\"BEGIN\", [])\n @depth = @depth + 1\n return null\n\nPostgresAdapter:commit()\n if @depth == 0\n throw new Error(\"NoActiveTransaction\")\n if @depth > 1\n @depth = @depth - 1\n return null\n if @rollbackOnly or @state == \"failed\"\n rollback()\n throw new Error(\"TransactionRolledBack\")\n execute(\"COMMIT\", [])\n @depth = 0\n return null\n\nPostgresAdapter:rollback()\n if @depth > 1\n @depth = @depth - 1\n @rollbackOnly = true\n return null\n if @state != null\n execute(\"ROLLBACK\", [])\n @depth = 0\n @rollbackOnly = false\n return null\n\nPostgresAdapter:transactionState()\n if @broken\n throw new Error(\"PostgreSQL connection is unavailable\")\n if @rollbackOnly and @state != \"failed\"\n return \"rollback_only\"\n return @state\n\nPostgresAdapter:close()\n if @closed\n return null\n try\n if !@broken\n exchange({\n action: \"close\"\n }, @timeout)\n finally\n @closed = true\n @port.close()\n @worker.terminate()\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { contains } from "./options.mjs";
|
|
2
|
+
import { attributes } from "./options.mjs";
|
|
3
|
+
import { Endpoint } from "./endpoint.mjs";
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import * as modules from "node:module";
|
|
7
|
+
import {
|
|
8
|
+
xml2obj as __xml2obj,
|
|
9
|
+
fromJson as __fromJson,
|
|
10
|
+
toJson as __toJson,
|
|
11
|
+
isNumber as __isNumber,
|
|
12
|
+
isString as __isString,
|
|
13
|
+
isBool as __isBool,
|
|
14
|
+
isObject as __isObject,
|
|
15
|
+
isArray as __isArray,
|
|
16
|
+
includes as __includes,
|
|
17
|
+
add as __add,
|
|
18
|
+
requireBoolean as __requireBoolean,
|
|
19
|
+
normalize as __normalize,
|
|
20
|
+
finite as __finite,
|
|
21
|
+
boundary as __boundary,
|
|
22
|
+
requireArity as __requireArity,
|
|
23
|
+
} from "./.lango/runtime.mjs";
|
|
24
|
+
|
|
25
|
+
export class Routes {
|
|
26
|
+
#root;
|
|
27
|
+
#items;
|
|
28
|
+
#events;
|
|
29
|
+
constructor(root) {
|
|
30
|
+
__requireArity(arguments.length, [0], "constructor");
|
|
31
|
+
root = __boundary(root);
|
|
32
|
+
this.#root = __normalize(path.resolve(root));
|
|
33
|
+
this.#items = [];
|
|
34
|
+
this.#events = { };
|
|
35
|
+
}
|
|
36
|
+
endpoint(spec) {
|
|
37
|
+
__requireArity(arguments.length, [0], "endpoint");
|
|
38
|
+
spec = __boundary(spec);
|
|
39
|
+
let item = new Endpoint(spec);
|
|
40
|
+
for (const other of __normalize(this.#items)) {
|
|
41
|
+
if (__requireBoolean(__normalize(other.path) === __normalize(item.path))) {
|
|
42
|
+
throw `Duplicate endpoint: ${__normalize(item.path)}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
__add(this.#items, { path: __normalize(item.path), module: resolveModule(__normalize(item.module), __normalize(this.#root)), handler: __normalize(item.handler), auth: __normalize(item.auth) });
|
|
46
|
+
return this;
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
event(spec) {
|
|
50
|
+
__requireArity(arguments.length, [0], "event");
|
|
51
|
+
spec = __boundary(spec);
|
|
52
|
+
if (__requireBoolean(!__requireBoolean(contains(["register", "validate", "request"], __normalize(spec.name))))) {
|
|
53
|
+
throw `Unknown event: ${__normalize(spec.name)}`;
|
|
54
|
+
}
|
|
55
|
+
if (__requireBoolean(__includes(__normalize(spec.name), __normalize(this.#events)))) {
|
|
56
|
+
throw `Duplicate event: ${__normalize(spec.name)}`;
|
|
57
|
+
}
|
|
58
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler)))) || __requireBoolean(__normalize(spec.handler) === ""))) {
|
|
59
|
+
throw "Event handler is required";
|
|
60
|
+
}
|
|
61
|
+
this.#events[__normalize(spec.name)] = { module: resolveModule(__normalize(spec.module), __normalize(this.#root)), handler: __normalize(spec.handler) };
|
|
62
|
+
return this;
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
include(spec, auth = null) {
|
|
66
|
+
__requireArity(arguments.length, [0], "include");
|
|
67
|
+
spec = __boundary(spec);
|
|
68
|
+
auth = __boundary(auth);
|
|
69
|
+
this.#includeAt(spec, __normalize(this.#root), "", [], auth);
|
|
70
|
+
return this;
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
xml(file) {
|
|
74
|
+
__requireArity(arguments.length, [0], "xml");
|
|
75
|
+
file = __boundary(file);
|
|
76
|
+
this.#readXml(__normalize(path.resolve(file)), "", __normalize(this.#root), [], true);
|
|
77
|
+
return this;
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
definitions() {
|
|
81
|
+
return __fromJson(__toJson(__normalize(this.#items)));
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
events() {
|
|
85
|
+
return __fromJson(__toJson(__normalize(this.#events)));
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
#includeAt(spec, directory, prefix, stack, inheritedAuth = null) {
|
|
89
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "includeAt");
|
|
90
|
+
spec = __boundary(spec);
|
|
91
|
+
directory = __boundary(directory);
|
|
92
|
+
prefix = __boundary(prefix);
|
|
93
|
+
stack = __boundary(stack);
|
|
94
|
+
inheritedAuth = __boundary(inheritedAuth);
|
|
95
|
+
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:"))))) {
|
|
96
|
+
throw "Include requires an npm package name";
|
|
97
|
+
}
|
|
98
|
+
let mount = __normalize(spec.path);
|
|
99
|
+
if (__requireBoolean(mount === null)) {
|
|
100
|
+
mount = "/";
|
|
101
|
+
}
|
|
102
|
+
if (__requireBoolean(!__requireBoolean(__isString(mount)))) {
|
|
103
|
+
throw "Include path must be a string";
|
|
104
|
+
}
|
|
105
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(mount.includes("*"))) || __requireBoolean(__normalize(mount.includes("//")))) || __requireBoolean(__normalize(mount.includes("?")))) || __requireBoolean(__normalize(mount.includes("#")))) || __requireBoolean(__normalize(mount.includes("\\"))))) {
|
|
106
|
+
throw "Invalid include prefix";
|
|
107
|
+
}
|
|
108
|
+
if (__requireBoolean(__requireBoolean(__normalize(mount.endsWith("/"))) && __requireBoolean(mount !== "/"))) {
|
|
109
|
+
mount = __normalize(mount.slice(0, __finite(-1)));
|
|
110
|
+
}
|
|
111
|
+
let combined = joinPath(prefix, mount);
|
|
112
|
+
if (__requireBoolean(combined === "/")) {
|
|
113
|
+
combined = "";
|
|
114
|
+
}
|
|
115
|
+
let file = resolveModule(`${__normalize(spec.module)}/routes.xml`, directory);
|
|
116
|
+
this.#readXml(file, combined, __normalize(path.dirname(file)), stack, false, inheritedAuth);
|
|
117
|
+
return null;
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
#readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null) {
|
|
121
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4], "readXml");
|
|
122
|
+
file = __boundary(file);
|
|
123
|
+
prefix = __boundary(prefix);
|
|
124
|
+
directory = __boundary(directory);
|
|
125
|
+
stack = __boundary(stack);
|
|
126
|
+
allowEvents = __boundary(allowEvents);
|
|
127
|
+
inheritedAuth = __boundary(inheritedAuth);
|
|
128
|
+
let real = __normalize(fs.realpathSync(file));
|
|
129
|
+
if (__requireBoolean(__normalize(stack.includes(real)))) {
|
|
130
|
+
throw `Route include cycle: ${real}`;
|
|
131
|
+
}
|
|
132
|
+
let root = __xml2obj(__normalize(fs.readFileSync(real, "utf8")));
|
|
133
|
+
if (__requireBoolean(__normalize(root.tag()) !== "routes")) {
|
|
134
|
+
throw `Expected routes root: ${real}`;
|
|
135
|
+
}
|
|
136
|
+
attributes(root, []);
|
|
137
|
+
let nextStack = __normalize(stack.slice());
|
|
138
|
+
__add(nextStack, real);
|
|
139
|
+
this.#collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents);
|
|
140
|
+
return null;
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
#collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents) {
|
|
144
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4, 5, 6], "collect");
|
|
145
|
+
parent = __boundary(parent);
|
|
146
|
+
prefix = __boundary(prefix);
|
|
147
|
+
inheritedModule = __boundary(inheritedModule);
|
|
148
|
+
inheritedAuth = __boundary(inheritedAuth);
|
|
149
|
+
directory = __boundary(directory);
|
|
150
|
+
stack = __boundary(stack);
|
|
151
|
+
allowEvents = __boundary(allowEvents);
|
|
152
|
+
if (__requireBoolean(__normalize(__normalize(parent.innerText()).trim()) !== "")) {
|
|
153
|
+
throw "Route XML cannot contain text";
|
|
154
|
+
}
|
|
155
|
+
for (const tag of __normalize(parent.children())) {
|
|
156
|
+
let spec = __normalize(tag.attributes());
|
|
157
|
+
let mod = __normalize(spec.module);
|
|
158
|
+
let auth = __normalize(spec.auth);
|
|
159
|
+
if (__requireBoolean(mod === null)) {
|
|
160
|
+
mod = inheritedModule;
|
|
161
|
+
}
|
|
162
|
+
if (__requireBoolean(auth === null)) {
|
|
163
|
+
auth = inheritedAuth;
|
|
164
|
+
}
|
|
165
|
+
if (__normalize(tag.tag()) === "endpoint") {
|
|
166
|
+
attributes(tag, ["path", "module", "handler", "auth"]);
|
|
167
|
+
leaf(tag);
|
|
168
|
+
this.endpoint({ path: joinPath(prefix, __normalize(spec.path)), module: resolveModule(mod, directory), handler: __normalize(spec.handler), auth: auth });
|
|
169
|
+
} else if (__normalize(tag.tag()) === "group") {
|
|
170
|
+
attributes(tag, ["path", "module", "auth"]);
|
|
171
|
+
this.#collect(tag, joinPath(prefix, __normalize(spec.path)), mod, auth, directory, stack, false);
|
|
172
|
+
} else if (__normalize(tag.tag()) === "include") {
|
|
173
|
+
attributes(tag, ["module", "path"]);
|
|
174
|
+
leaf(tag);
|
|
175
|
+
this.#includeAt(spec, directory, prefix, stack, auth);
|
|
176
|
+
} else if (__normalize(tag.tag()) === "event") {
|
|
177
|
+
attributes(tag, ["name", "module", "handler"]);
|
|
178
|
+
leaf(tag);
|
|
179
|
+
if (__requireBoolean(!__requireBoolean(allowEvents))) {
|
|
180
|
+
throw "Events are only allowed in the application's root routes.xml";
|
|
181
|
+
}
|
|
182
|
+
this.event({ name: __normalize(spec.name), module: resolveModule(__normalize(spec.module), directory), handler: __normalize(spec.handler) });
|
|
183
|
+
} else {
|
|
184
|
+
throw `Unknown routes element: ${__normalize(tag.tag())}`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function resolveModule(name, directory) {
|
|
192
|
+
__requireArity(arguments.length, [0, 1], "resolveModule");
|
|
193
|
+
name = __boundary(name);
|
|
194
|
+
directory = __boundary(directory);
|
|
195
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(name === "")) || __requireBoolean(__normalize(name.startsWith("node:"))))) {
|
|
196
|
+
throw "Application module is required";
|
|
197
|
+
}
|
|
198
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(directory, "package.json"))));
|
|
199
|
+
let resolved = __normalize(resolver.resolve(name));
|
|
200
|
+
if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(resolved))))) {
|
|
201
|
+
throw `Application module must resolve to a file: ${name}`;
|
|
202
|
+
}
|
|
203
|
+
return resolved;
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function joinPath(prefix, suffix) {
|
|
208
|
+
__requireArity(arguments.length, [0, 1], "joinPath");
|
|
209
|
+
prefix = __boundary(prefix);
|
|
210
|
+
suffix = __boundary(suffix);
|
|
211
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(suffix))) || __requireBoolean(!__requireBoolean(__normalize(suffix.startsWith("/")))))) {
|
|
212
|
+
throw "Route path must begin with /";
|
|
213
|
+
}
|
|
214
|
+
if (__requireBoolean(__requireBoolean(prefix === "") || __requireBoolean(prefix === "/"))) {
|
|
215
|
+
return suffix;
|
|
216
|
+
}
|
|
217
|
+
if (__requireBoolean(suffix === "/")) {
|
|
218
|
+
return `${prefix}/`;
|
|
219
|
+
}
|
|
220
|
+
return `${prefix}${suffix}`;
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function leaf(tag) {
|
|
225
|
+
__requireArity(arguments.length, [0], "leaf");
|
|
226
|
+
tag = __boundary(tag);
|
|
227
|
+
if (__requireBoolean(__requireBoolean(__normalize(__normalize(tag.children()).length) !== 0) || __requireBoolean(__normalize(__normalize(tag.innerText()).trim()) !== ""))) {
|
|
228
|
+
throw `Expected an empty ${__normalize(tag.tag())} element`;
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=routes.mjs.map
|
|
@@ -0,0 +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 \"./endpoint\"\n class Endpoint\n\nexport class Routes(root)\n methods\n endpoint(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\nRoutes:endpoint(spec)\n item = new Endpoint(spec)\n for @items as other\n if other.path == item.path\n throw \"Duplicate endpoint: ${item.path}\"\n @items.add({\n path: item.path\n module: resolveModule(item.module, @root)\n handler: item.handler\n auth: item.auth\n })\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 \"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 \"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 throw \"Unknown routes element: ${tag.tag()}\"\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":";;;;;;;;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDA;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;AAmFA;;;;;;;;;;"}
|