@lango-project/lango-server 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/body_reader.mjs.map +1 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/server.mjs +3 -838
- package/dist/cli/server.mjs.map +1 -1
- package/dist/client_identity.mjs.map +1 -1
- package/dist/database.mjs.map +1 -1
- package/dist/database_action.mjs.map +1 -1
- package/dist/database_options.mjs.map +1 -1
- package/dist/database_scope.mjs.map +1 -1
- package/dist/endpoint.mjs.map +1 -1
- package/dist/error_details.mjs.map +1 -1
- package/dist/execution_failure.mjs.map +1 -1
- package/dist/execution_identity.mjs.map +1 -1
- package/dist/execution_log.mjs.map +1 -1
- package/dist/execution_runtime.mjs.map +1 -1
- package/dist/execution_worker.mjs.map +1 -1
- package/dist/executor.mjs.map +1 -1
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs.map +1 -1
- package/dist/job_configuration.mjs.map +1 -1
- package/dist/job_context.mjs.map +1 -1
- package/dist/library.mjs.map +1 -1
- package/dist/longpoll_broker.mjs.map +1 -1
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/longpoll_session.mjs.map +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs.map +1 -1
- package/dist/options.mjs.map +1 -1
- package/dist/origin.mjs.map +1 -1
- package/dist/policy.mjs.map +1 -1
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs.map +1 -1
- package/dist/request_identity.mjs.map +1 -1
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs.map +1 -1
- package/dist/route_pattern.mjs.map +1 -1
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs.map +1 -1
- package/dist/sqlite_adapter.mjs.map +1 -1
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs.map +1 -1
- package/dist/trace.mjs.map +1 -1
- package/dist/worker.mjs.map +1 -1
- package/examples/security/package.json +1 -1
- package/package.json +6 -4
- package/dist/cli/.lango/bignumber.mjs +0 -3119
- package/dist/cli/.lango/localization.mjs +0 -108
- package/dist/cli/.lango/runtime.mjs +0 -725
- package/dist/cli/.lango/vendor/i18next.LICENSE +0 -21
- package/dist/cli/.lango/vendor/i18next.mjs +0 -2434
- package/dist/cli/.lango/xmldom/conventions.js +0 -429
- package/dist/cli/.lango/xmldom/dom-parser.js +0 -591
- package/dist/cli/.lango/xmldom/dom.js +0 -3759
- package/dist/cli/.lango/xmldom/entities.js +0 -2171
- package/dist/cli/.lango/xmldom/errors.js +0 -205
- package/dist/cli/.lango/xmldom/grammar.js +0 -559
- package/dist/cli/.lango/xmldom/index.js +0 -41
- package/dist/cli/.lango/xmldom/package.json +0 -1
- package/dist/cli/.lango/xmldom/sax.js +0 -983
- package/dist/cli/.lango/xmldom.LICENSE.md +0 -8
- package/dist/cli/body_reader.mjs +0 -180
- package/dist/cli/body_reader.mjs.map +0 -1
- package/dist/cli/client_identity.mjs +0 -250
- package/dist/cli/client_identity.mjs.map +0 -1
- package/dist/cli/database.mjs +0 -116
- package/dist/cli/database.mjs.map +0 -1
- package/dist/cli/database_options.mjs +0 -83
- package/dist/cli/database_options.mjs.map +0 -1
- package/dist/cli/database_scope.mjs +0 -93
- package/dist/cli/database_scope.mjs.map +0 -1
- package/dist/cli/endpoint.mjs +0 -50
- package/dist/cli/endpoint.mjs.map +0 -1
- package/dist/cli/error_details.mjs +0 -45
- package/dist/cli/error_details.mjs.map +0 -1
- package/dist/cli/longpoll_broker.mjs +0 -215
- package/dist/cli/longpoll_broker.mjs.map +0 -1
- package/dist/cli/longpoll_options.mjs +0 -103
- package/dist/cli/longpoll_options.mjs.map +0 -1
- package/dist/cli/longpoll_session.mjs +0 -136
- package/dist/cli/longpoll_session.mjs.map +0 -1
- package/dist/cli/middleware.mjs +0 -25
- package/dist/cli/middleware.mjs.map +0 -1
- package/dist/cli/options.mjs +0 -214
- package/dist/cli/options.mjs.map +0 -1
- package/dist/cli/origin.mjs +0 -87
- package/dist/cli/origin.mjs.map +0 -1
- package/dist/cli/policy.mjs +0 -121
- package/dist/cli/policy.mjs.map +0 -1
- package/dist/cli/postgres_adapter.mjs +0 -178
- package/dist/cli/postgres_adapter.mjs.map +0 -1
- package/dist/cli/route_pattern.mjs +0 -129
- package/dist/cli/route_pattern.mjs.map +0 -1
- package/dist/cli/routes.mjs +0 -360
- package/dist/cli/routes.mjs.map +0 -1
- package/dist/cli/sqlite_adapter.mjs +0 -163
- package/dist/cli/sqlite_adapter.mjs.map +0 -1
- package/dist/cli/static_files.mjs +0 -224
- package/dist/cli/static_files.mjs.map +0 -1
- package/dist/cli/trace.mjs +0 -53
- package/dist/cli/trace.mjs.map +0 -1
|
@@ -1,725 +0,0 @@
|
|
|
1
|
-
import BigNumber from "./bignumber.mjs";
|
|
2
|
-
import { createI18n } from "./localization.mjs";
|
|
3
|
-
export { createI18n };
|
|
4
|
-
import { createRequire } from "node:module";
|
|
5
|
-
const require = createRequire(import.meta.url);
|
|
6
|
-
const { DOMParser } = require("./xmldom/index.js");
|
|
7
|
-
|
|
8
|
-
const Decimal = BigNumber.clone({
|
|
9
|
-
DECIMAL_PLACES: 6,
|
|
10
|
-
ROUNDING_MODE: BigNumber.ROUND_HALF_EVEN,
|
|
11
|
-
});
|
|
12
|
-
Decimal.Rounding = Object.freeze({"Up":"up","Down":"down","Ceil":"ceil","Floor":"floor","HalfUp":"half_up","HalfDown":"half_down","HalfEven":"half_even","HalfCeil":"half_ceil","HalfFloor":"half_floor"});
|
|
13
|
-
export { Decimal };
|
|
14
|
-
|
|
15
|
-
export function decimal(value) {
|
|
16
|
-
if (value instanceof Decimal) return value;
|
|
17
|
-
let result;
|
|
18
|
-
try {
|
|
19
|
-
result = new Decimal(value);
|
|
20
|
-
} catch {
|
|
21
|
-
throw new TypeError("Decimal value must be finite");
|
|
22
|
-
}
|
|
23
|
-
if (!result.isFinite())
|
|
24
|
-
throw new TypeError("Decimal value must be finite");
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function isDecimal(value) {
|
|
29
|
-
return value instanceof Decimal;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function toJsNumber(value) {
|
|
33
|
-
return new Decimal(value).toNumber();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let defaultI18n = null;
|
|
37
|
-
let output = null;
|
|
38
|
-
let database = null;
|
|
39
|
-
const failureStore = Symbol.for("lango.databaseFailures");
|
|
40
|
-
const failedTransactions = globalThis[failureStore] ?? new WeakSet();
|
|
41
|
-
globalThis[failureStore] = failedTransactions;
|
|
42
|
-
|
|
43
|
-
function sqlBindings(sql) {
|
|
44
|
-
const bindings = [];
|
|
45
|
-
for (let i = 0; i < sql.length;) {
|
|
46
|
-
if (sql.startsWith("--", i)) {
|
|
47
|
-
const end = sql.indexOf("\n", i + 2);
|
|
48
|
-
i = end < 0 ? sql.length : end + 1;
|
|
49
|
-
} else if (sql.startsWith("/*", i)) {
|
|
50
|
-
let depth = 1;
|
|
51
|
-
i += 2;
|
|
52
|
-
while (i < sql.length && depth) {
|
|
53
|
-
if (sql.startsWith("/*", i)) { depth++; i += 2; }
|
|
54
|
-
else if (sql.startsWith("*/", i)) { depth--; i += 2; }
|
|
55
|
-
else i++;
|
|
56
|
-
}
|
|
57
|
-
} else if ("'\"`[".includes(sql[i])) {
|
|
58
|
-
const start = sql[i];
|
|
59
|
-
const end = start === "[" ? "]" : start;
|
|
60
|
-
const escape = start === "'" && /[eE]/.test(sql[i - 1] ?? "") &&
|
|
61
|
-
!/[\w$]/.test(sql[i - 2] ?? "");
|
|
62
|
-
i++;
|
|
63
|
-
while (i < sql.length) {
|
|
64
|
-
if (escape && sql[i] === "\\") i += 2;
|
|
65
|
-
else if (sql[i] === end) {
|
|
66
|
-
i++;
|
|
67
|
-
if (sql[i] !== end) break;
|
|
68
|
-
i++;
|
|
69
|
-
} else i++;
|
|
70
|
-
}
|
|
71
|
-
} else if (sql[i] === "$" && !/[\w$]/.test(sql[i - 1] ?? "")) {
|
|
72
|
-
const tag = /^\$(?:[A-Za-z_]\w*)?\$/.exec(sql.slice(i))?.[0];
|
|
73
|
-
if (tag) {
|
|
74
|
-
const end = sql.indexOf(tag, i + tag.length);
|
|
75
|
-
i = end < 0 ? sql.length : end + tag.length;
|
|
76
|
-
} else i++;
|
|
77
|
-
} else if (sql[i] === ":" && sql[i - 1] !== ":" && /[A-Za-z_]/.test(sql[i + 1] ?? "")) {
|
|
78
|
-
const start = i++;
|
|
79
|
-
while (/\w/.test(sql[i] ?? "")) i++;
|
|
80
|
-
bindings.push({ name: sql.slice(start + 1, i), start, end: i });
|
|
81
|
-
} else i++;
|
|
82
|
-
}
|
|
83
|
-
return bindings;
|
|
84
|
-
}
|
|
85
|
-
function postgresQuery(sql, parameters) {
|
|
86
|
-
const positions = new Map();
|
|
87
|
-
const values = [];
|
|
88
|
-
let text = "";
|
|
89
|
-
let at = 0;
|
|
90
|
-
for (const binding of sqlBindings(sql)) {
|
|
91
|
-
if (!Object.hasOwn(parameters, binding.name))
|
|
92
|
-
throw new Error(`SqlParameterNotBound: ${binding.name}`);
|
|
93
|
-
if (!positions.has(binding.name)) {
|
|
94
|
-
values.push(parameters[binding.name]);
|
|
95
|
-
positions.set(binding.name, values.length);
|
|
96
|
-
}
|
|
97
|
-
text += sql.slice(at, binding.start) + "$" + positions.get(binding.name);
|
|
98
|
-
at = binding.end;
|
|
99
|
-
}
|
|
100
|
-
return { text: text + sql.slice(at), values };
|
|
101
|
-
}
|
|
102
|
-
export const DB = Object.freeze({ Active: "active", RollbackOnly: "rollback_only", Failed: "failed" });
|
|
103
|
-
const loggingStore = Symbol.for("lango.logging");
|
|
104
|
-
const logging = globalThis[loggingStore] ?? { verbose: false };
|
|
105
|
-
globalThis[loggingStore] = logging;
|
|
106
|
-
const contextStore = Symbol.for("lango.contexts");
|
|
107
|
-
const contexts = globalThis[contextStore] ?? new Map();
|
|
108
|
-
globalThis[contextStore] = contexts;
|
|
109
|
-
|
|
110
|
-
export class RuntimeConfigurationError extends Error {
|
|
111
|
-
constructor(message) {
|
|
112
|
-
super(message);
|
|
113
|
-
this.name = "RuntimeConfigurationError";
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function configure(options = {}) {
|
|
118
|
-
if (options === null || typeof options !== "object" || Array.isArray(options))
|
|
119
|
-
throw new RuntimeConfigurationError("runtime configuration must be an object");
|
|
120
|
-
if (options.verbose !== undefined && typeof options.verbose !== "boolean")
|
|
121
|
-
throw new RuntimeConfigurationError("runtime verbose must be boolean");
|
|
122
|
-
const nextCatalog = options.catalog ?? {};
|
|
123
|
-
const nextOutput = options.output ?? null;
|
|
124
|
-
const nextDatabase = options.database_adapter ?? null;
|
|
125
|
-
if (nextCatalog === null || typeof nextCatalog !== "object" || Array.isArray(nextCatalog))
|
|
126
|
-
throw new RuntimeConfigurationError("runtime catalog must be an object");
|
|
127
|
-
if (nextOutput !== null && typeof nextOutput !== "function")
|
|
128
|
-
throw new RuntimeConfigurationError("runtime output must be a function or null");
|
|
129
|
-
if (nextDatabase !== null && (typeof nextDatabase !== "object" && typeof nextDatabase !== "function"))
|
|
130
|
-
throw new RuntimeConfigurationError("runtime database_adapter must be an object or null");
|
|
131
|
-
let nextI18n;
|
|
132
|
-
try {
|
|
133
|
-
nextI18n = options.i18n ?? createI18n({
|
|
134
|
-
catalog: nextCatalog, catalogs: options.catalogs,
|
|
135
|
-
locale: options.locale, fallbackLocale: options.fallbackLocale,
|
|
136
|
-
timeZone: options.timeZone, currency: options.currency,
|
|
137
|
-
});
|
|
138
|
-
if (typeof nextI18n.translate !== "function" || typeof nextI18n.formatNumber !== "function" || typeof nextI18n.formatDate !== "function")
|
|
139
|
-
throw new TypeError("i18n requires translate, formatNumber and formatDate methods");
|
|
140
|
-
} catch (error) {
|
|
141
|
-
throw new RuntimeConfigurationError(error.message);
|
|
142
|
-
}
|
|
143
|
-
if (options.verbose !== undefined) logging.verbose = options.verbose;
|
|
144
|
-
defaultI18n = nextI18n;
|
|
145
|
-
output = nextOutput;
|
|
146
|
-
if (database !== null) failedTransactions.delete(database);
|
|
147
|
-
database = nextDatabase;
|
|
148
|
-
if (database !== null) failedTransactions.delete(database);
|
|
149
|
-
contexts.clear();
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function requireDatabase() {
|
|
154
|
-
const stack = contexts.get("db");
|
|
155
|
-
const adapter = stack?.length ? stack.at(-1) : database;
|
|
156
|
-
if (adapter === null || adapter === undefined) throw new Error("DatabaseNotConfigured");
|
|
157
|
-
return adapter;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function startTrans() {
|
|
161
|
-
const adapter = requireDatabase();
|
|
162
|
-
if (failedTransactions.has(adapter)) throw new Error("TransactionFailed");
|
|
163
|
-
adapter.begin();
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
export function commitTrans() {
|
|
167
|
-
const adapter = requireDatabase();
|
|
168
|
-
if (failedTransactions.has(adapter)) throw new Error("TransactionFailed");
|
|
169
|
-
adapter.commit();
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
export function rollbackTrans() {
|
|
173
|
-
const adapter = requireDatabase();
|
|
174
|
-
adapter.rollback();
|
|
175
|
-
if (adapter.transactionState() === null) failedTransactions.delete(adapter);
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
export function transState() {
|
|
179
|
-
const adapter = requireDatabase();
|
|
180
|
-
const state = adapter.transactionState();
|
|
181
|
-
return failedTransactions.has(adapter) && state !== null ? DB.Failed : state;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export class SqlError extends Error {
|
|
185
|
-
constructor(cause, query) {
|
|
186
|
-
super(cause?.message ?? "Database error");
|
|
187
|
-
this.name = "SqlError";
|
|
188
|
-
this.code = typeof cause?.code === "string" ? cause.code : null;
|
|
189
|
-
this.query = query;
|
|
190
|
-
this.position = Number.isInteger(cause?.position) ? cause.position : null;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function query(sql, parameterNames) {
|
|
195
|
-
const parameters = Object.create(null);
|
|
196
|
-
let rows = [];
|
|
197
|
-
let cursor = -1;
|
|
198
|
-
let result = { changes: null, lastInsertRowid: null };
|
|
199
|
-
const api = {
|
|
200
|
-
execute() {
|
|
201
|
-
const adapter = requireDatabase();
|
|
202
|
-
const boundParameters = Object.fromEntries(
|
|
203
|
-
Object.entries(parameters).map(([name, value]) => [
|
|
204
|
-
name,
|
|
205
|
-
isDecimal(value) ? value.toFixed() : value,
|
|
206
|
-
]),
|
|
207
|
-
);
|
|
208
|
-
try {
|
|
209
|
-
if (failedTransactions.has(adapter)) throw new Error("TransactionFailed");
|
|
210
|
-
if (adapter.parameterStyle === "postgresql") {
|
|
211
|
-
const prepared = postgresQuery(sql, boundParameters);
|
|
212
|
-
result = adapter.execute(prepared.text, prepared.values);
|
|
213
|
-
} else result = adapter.execute(sql, boundParameters);
|
|
214
|
-
if (result && typeof result.then === "function")
|
|
215
|
-
throw new Error("SynchronousDatabaseAdapterRequired");
|
|
216
|
-
} catch (error) {
|
|
217
|
-
if (error?.transactionUnusable === true && adapter.transactionState() !== null)
|
|
218
|
-
failedTransactions.add(adapter);
|
|
219
|
-
throw new SqlError(error, sql);
|
|
220
|
-
}
|
|
221
|
-
rows = result.rows;
|
|
222
|
-
cursor = -1;
|
|
223
|
-
return proxy;
|
|
224
|
-
},
|
|
225
|
-
next() {
|
|
226
|
-
if (cursor + 1 >= rows.length) {
|
|
227
|
-
cursor = rows.length;
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
cursor += 1;
|
|
231
|
-
return true;
|
|
232
|
-
},
|
|
233
|
-
next_result() { return false; },
|
|
234
|
-
};
|
|
235
|
-
const proxy = new Proxy(api, {
|
|
236
|
-
get(target, name) {
|
|
237
|
-
if (name === "affected_rows") return result.changes;
|
|
238
|
-
if (name === "generated_key") return result.lastInsertRowid;
|
|
239
|
-
if (name in target) return target[name];
|
|
240
|
-
if (cursor < 0 || cursor >= rows.length) throw new Error("SqlNoCurrentRow");
|
|
241
|
-
return rows[cursor][name] ?? null;
|
|
242
|
-
},
|
|
243
|
-
set(target, name, value) {
|
|
244
|
-
if (!parameterNames.includes(name)) throw new Error("SqlUnknownParameter");
|
|
245
|
-
parameters[name] = value;
|
|
246
|
-
return true;
|
|
247
|
-
},
|
|
248
|
-
});
|
|
249
|
-
return proxy;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export function translate(template, values, options = null) {
|
|
253
|
-
return contextGet("i18n").translate(template, values, options);
|
|
254
|
-
}
|
|
255
|
-
export function formatNumber(value, options = null) {
|
|
256
|
-
return contextGet("i18n").formatNumber(value, options);
|
|
257
|
-
}
|
|
258
|
-
export function formatDate(value, options = null) {
|
|
259
|
-
return contextGet("i18n").formatDate(value, options);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export function xmlEscape(value) {
|
|
263
|
-
return String(value)
|
|
264
|
-
.replaceAll("&", "&")
|
|
265
|
-
.replaceAll("<", "<")
|
|
266
|
-
.replaceAll(">", ">")
|
|
267
|
-
.replaceAll('"', """)
|
|
268
|
-
.replaceAll("'", "'");
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export function xml2obj(xml) {
|
|
272
|
-
if (typeof xml !== "string") throw new Error("XmlParseError: XML input must be a string");
|
|
273
|
-
const errors = [];
|
|
274
|
-
let document;
|
|
275
|
-
try {
|
|
276
|
-
document = new DOMParser({
|
|
277
|
-
onError(level, message) {
|
|
278
|
-
if (level !== "warning") errors.push(message);
|
|
279
|
-
},
|
|
280
|
-
}).parseFromString(xml, "application/xml");
|
|
281
|
-
} catch (error) {
|
|
282
|
-
throw new Error(`XmlParseError: ${error.message}`);
|
|
283
|
-
}
|
|
284
|
-
if (errors.length) throw new Error(`XmlParseError: ${errors[0]}`);
|
|
285
|
-
if (false && document.getElementsByTagName("parsererror").length)
|
|
286
|
-
throw new Error("XmlParseError: invalid XML document");
|
|
287
|
-
if (!document.documentElement)
|
|
288
|
-
throw new Error("XmlParseError: XML must contain one root tag");
|
|
289
|
-
return new Tag(document.documentElement);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export class Tag {
|
|
293
|
-
constructor(element) {
|
|
294
|
-
this.element = element;
|
|
295
|
-
return new Proxy(this, {
|
|
296
|
-
get: (target, name, receiver) => target.#property(name, receiver),
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
tag() {
|
|
301
|
-
return this.element.tagName;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
innerText() {
|
|
305
|
-
let text = "";
|
|
306
|
-
for (const child of this.element.childNodes) {
|
|
307
|
-
if (child.nodeType === 3 || child.nodeType === 4) text += child.data;
|
|
308
|
-
}
|
|
309
|
-
return text;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
children() {
|
|
313
|
-
return Array.from(this.element.childNodes)
|
|
314
|
-
.filter((child) => child.nodeType === 1)
|
|
315
|
-
.map((child) => new Tag(child));
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
attribute(name) {
|
|
319
|
-
const value = this.element.getAttribute(String(name));
|
|
320
|
-
return value === null ? null : value;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
attributes() {
|
|
324
|
-
const values = {};
|
|
325
|
-
for (const attribute of Array.from(this.element.attributes)) values[attribute.name] = attribute.value;
|
|
326
|
-
return values;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
#property(name, receiver) {
|
|
330
|
-
if (typeof name !== "string") return Reflect.get(this, name, receiver);
|
|
331
|
-
if (name === "tag" || name === "innerText" || name === "children" || name === "attribute" || name === "attributes" || name === "element")
|
|
332
|
-
return Reflect.get(this, name, receiver);
|
|
333
|
-
if (this.element.hasAttribute(name)) return this.element.getAttribute(name);
|
|
334
|
-
const matching = Array.from(this.element.childNodes)
|
|
335
|
-
.filter((child) => child.nodeType === 1 && child.tagName === name);
|
|
336
|
-
return matching.length === 1 ? new Tag(matching[0]) : undefined;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
export function println(value) {
|
|
341
|
-
if (output === null) {
|
|
342
|
-
throw new Error("Lango runtime output is not configured");
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
output(String(value));
|
|
346
|
-
return null;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const defaultLog = Object.freeze({
|
|
350
|
-
get verbose() { return logging.verbose || process.env.LANGO_VERBOSE === "1"; },
|
|
351
|
-
debug(value) { if (!defaultLog.verbose) return null; return writeLog(process.stdout, "DEBUG", value); },
|
|
352
|
-
info(value) { return writeLog(process.stdout, "INFO", value); },
|
|
353
|
-
warn(value) { return writeLog(process.stderr, "WARN", value); },
|
|
354
|
-
err(value) { return writeLog(process.stderr, "ERROR", value); },
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
function writeLog(stream, level, value) {
|
|
358
|
-
stream.write(`[${level}] ${String(value)}\n`);
|
|
359
|
-
return null;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
export function fromJson(text) {
|
|
363
|
-
try {
|
|
364
|
-
return decimalJson(JSON.parse(text));
|
|
365
|
-
} catch (error) {
|
|
366
|
-
throw new Error(`JsonParseError: ${error.message}`);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function decimalJson(value) {
|
|
371
|
-
if (Array.isArray(value)) return value.map(decimalJson);
|
|
372
|
-
if (value === null || typeof value !== "object") return value;
|
|
373
|
-
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
|
374
|
-
key,
|
|
375
|
-
key.startsWith("$") && typeof item === "number" ? decimal(item) : decimalJson(item),
|
|
376
|
-
]));
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
export function toJson(value) {
|
|
380
|
-
try {
|
|
381
|
-
return JSON.stringify(jsonValue(value));
|
|
382
|
-
} catch (error) {
|
|
383
|
-
if (error.message === "NonFiniteNumber" || error.message === "JsonCycle") {
|
|
384
|
-
throw error;
|
|
385
|
-
}
|
|
386
|
-
throw new Error(`JsonUnsupported: ${error.message}`);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
function jsonValue(value, ancestors = new Set()) {
|
|
391
|
-
if (value === undefined) return null;
|
|
392
|
-
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
393
|
-
return value;
|
|
394
|
-
}
|
|
395
|
-
if (isDecimal(value)) return JSON.rawJSON(value.toFixed());
|
|
396
|
-
if (typeof value === "number") {
|
|
397
|
-
if (!Number.isFinite(value)) throw new Error("NonFiniteNumber");
|
|
398
|
-
return value;
|
|
399
|
-
}
|
|
400
|
-
if (typeof value === "function" || typeof value !== "object") {
|
|
401
|
-
throw new Error("unsupported value");
|
|
402
|
-
}
|
|
403
|
-
if (ancestors.has(value)) throw new Error("JsonCycle");
|
|
404
|
-
|
|
405
|
-
ancestors.add(value);
|
|
406
|
-
try {
|
|
407
|
-
if (Array.isArray(value)) return value.map((item) => jsonValue(item, ancestors));
|
|
408
|
-
const prototype = Object.getPrototypeOf(value);
|
|
409
|
-
if (prototype !== Object.prototype && prototype !== null) {
|
|
410
|
-
throw new Error("unsupported value");
|
|
411
|
-
}
|
|
412
|
-
return Object.fromEntries(
|
|
413
|
-
Object.entries(value).map(([key, item]) => [key, jsonValue(item, ancestors)]),
|
|
414
|
-
);
|
|
415
|
-
} finally {
|
|
416
|
-
ancestors.delete(value);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
export function getClass(value) {
|
|
421
|
-
if (value === null) return null;
|
|
422
|
-
if (typeof value === "number") return Number;
|
|
423
|
-
if (typeof value === "string") return String;
|
|
424
|
-
if (typeof value === "boolean") return Boolean;
|
|
425
|
-
return Object.getPrototypeOf(value)?.constructor ?? Object;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
export function isKindof(value, Class) {
|
|
429
|
-
if (value === null) return false;
|
|
430
|
-
if (typeof value === "object" || typeof value === "function") return value instanceof Class;
|
|
431
|
-
return getClass(value) === Class;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
export function isNumber(value) {
|
|
435
|
-
return typeof value === "number" && Number.isFinite(value);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
export function isString(value) {
|
|
439
|
-
return typeof value === "string";
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
export function isBool(value) {
|
|
443
|
-
return typeof value === "boolean";
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
export function isObject(value) {
|
|
447
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
export function isArray(value) {
|
|
451
|
-
return Array.isArray(value);
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
export function includes(value, collection) {
|
|
455
|
-
if (Array.isArray(collection)) return collection.includes(value);
|
|
456
|
-
if (collection instanceof Set) return collection.has(value);
|
|
457
|
-
if (collection instanceof Map) return collection.has(value);
|
|
458
|
-
if (typeof collection === "string" && typeof value === "string") {
|
|
459
|
-
return collection.includes(value);
|
|
460
|
-
}
|
|
461
|
-
if (collection !== null && typeof collection === "object" && typeof value === "string") {
|
|
462
|
-
return Object.hasOwn(collection, value);
|
|
463
|
-
}
|
|
464
|
-
throw new Error("MembershipTarget");
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
export function entries(collection) {
|
|
468
|
-
if (collection instanceof Map) return collection.entries();
|
|
469
|
-
if (collection instanceof Set) return collection.entries();
|
|
470
|
-
if (Array.isArray(collection)) return collection.entries();
|
|
471
|
-
if (typeof collection === "string") return Array.from(collection).entries();
|
|
472
|
-
if (collection !== null && typeof collection?.[Symbol.iterator] === "function") {
|
|
473
|
-
return Array.from(collection).entries();
|
|
474
|
-
}
|
|
475
|
-
if (collection !== null && typeof collection === "object") {
|
|
476
|
-
return Object.entries(collection);
|
|
477
|
-
}
|
|
478
|
-
throw new Error("EntriesTarget");
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
export function* range(start, end, step = 1) {
|
|
482
|
-
if (
|
|
483
|
-
!Number.isSafeInteger(start) ||
|
|
484
|
-
!Number.isSafeInteger(end) ||
|
|
485
|
-
!Number.isSafeInteger(step) ||
|
|
486
|
-
step === 0
|
|
487
|
-
) {
|
|
488
|
-
throw new Error("RangeBounds");
|
|
489
|
-
}
|
|
490
|
-
for (
|
|
491
|
-
let value = start;
|
|
492
|
-
step > 0 ? value < end : value > end;
|
|
493
|
-
value += step
|
|
494
|
-
) yield value;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
export function add(value, ...items) {
|
|
498
|
-
if (Array.isArray(value)) return value.push(...items);
|
|
499
|
-
if (value !== null && typeof value?.add === "function") {
|
|
500
|
-
return normalize(value.add(...items));
|
|
501
|
-
}
|
|
502
|
-
throw new Error("NotAddable");
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
export function remove(value, ...items) {
|
|
506
|
-
const [item] = items;
|
|
507
|
-
if (Array.isArray(value)) {
|
|
508
|
-
const index = value.indexOf(item);
|
|
509
|
-
if (index < 0) return false;
|
|
510
|
-
value.splice(index, 1);
|
|
511
|
-
return true;
|
|
512
|
-
}
|
|
513
|
-
if (value !== null && typeof value?.remove === "function") {
|
|
514
|
-
return normalize(value.remove(...items));
|
|
515
|
-
}
|
|
516
|
-
throw new Error("NotRemovable");
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
export function remove_all(value, ...items) {
|
|
520
|
-
const [item] = items;
|
|
521
|
-
if (Array.isArray(value)) {
|
|
522
|
-
let removed = 0;
|
|
523
|
-
for (let index = value.length - 1; index >= 0; index -= 1) {
|
|
524
|
-
if (value[index] !== item) continue;
|
|
525
|
-
value.splice(index, 1);
|
|
526
|
-
removed += 1;
|
|
527
|
-
}
|
|
528
|
-
return removed;
|
|
529
|
-
}
|
|
530
|
-
if (value !== null && typeof value?.remove_all === "function") {
|
|
531
|
-
return normalize(value.remove_all(...items));
|
|
532
|
-
}
|
|
533
|
-
throw new Error("NotRemovable");
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
export function clear(value, ...items) {
|
|
537
|
-
if (Array.isArray(value)) {
|
|
538
|
-
value.length = 0;
|
|
539
|
-
return null;
|
|
540
|
-
}
|
|
541
|
-
if (value !== null && typeof value?.clear === "function") {
|
|
542
|
-
return normalize(value.clear(...items));
|
|
543
|
-
}
|
|
544
|
-
throw new Error("NotClearable");
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
export function remove_at(value, ...items) {
|
|
548
|
-
const [index] = items;
|
|
549
|
-
if (Array.isArray(value)) {
|
|
550
|
-
if (!Number.isSafeInteger(index) || index < 0 || index >= value.length)
|
|
551
|
-
return false;
|
|
552
|
-
value.splice(index, 1);
|
|
553
|
-
return true;
|
|
554
|
-
}
|
|
555
|
-
if (value !== null && typeof value?.remove_at === "function") {
|
|
556
|
-
return normalize(value.remove_at(...items));
|
|
557
|
-
}
|
|
558
|
-
throw new Error("NotRemovable");
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
export function insert_at(value, ...items) {
|
|
562
|
-
const [index, item] = items;
|
|
563
|
-
if (Array.isArray(value)) {
|
|
564
|
-
if (!Number.isSafeInteger(index) || index < 0 || index > value.length)
|
|
565
|
-
return false;
|
|
566
|
-
value.splice(index, 0, item);
|
|
567
|
-
return true;
|
|
568
|
-
}
|
|
569
|
-
if (value !== null && typeof value?.insert_at === "function") {
|
|
570
|
-
return normalize(value.insert_at(...items));
|
|
571
|
-
}
|
|
572
|
-
throw new Error("NotInsertable");
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
export function optional(value, callback) {
|
|
576
|
-
return value === null || value === undefined ? null : callback(value);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
export function requireBoolean(value) {
|
|
580
|
-
if (typeof value !== "boolean") {
|
|
581
|
-
throw new Error("BooleanRequired");
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
return value;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
export function assert(value, location) {
|
|
588
|
-
if (!value) throw new Error(`AssertionFailed at ${location}`);
|
|
589
|
-
return null;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
export function normalize(value) {
|
|
593
|
-
if (value === undefined) return null;
|
|
594
|
-
if (typeof value === "number" && !Number.isFinite(value)) {
|
|
595
|
-
throw new Error("NonFiniteNumber");
|
|
596
|
-
}
|
|
597
|
-
return value;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
export function finite(value) {
|
|
601
|
-
if (typeof value === "number" && !Number.isFinite(value)) {
|
|
602
|
-
throw new Error("NonFiniteNumber");
|
|
603
|
-
}
|
|
604
|
-
return value;
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
export function requireNumber(value) {
|
|
608
|
-
if (typeof value !== "number") throw new Error("NumberRequired");
|
|
609
|
-
return finite(value);
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
export function requireNumeric(value) {
|
|
613
|
-
if (isDecimal(value) && value.isFinite()) return value;
|
|
614
|
-
return requireNumber(value);
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
export function numericAdd(left, right) {
|
|
618
|
-
requireNumeric(left);
|
|
619
|
-
requireNumeric(right);
|
|
620
|
-
if (isDecimal(left) || isDecimal(right))
|
|
621
|
-
return decimal(left).plus(right);
|
|
622
|
-
return finite(left + right);
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
export function call(value, ...arguments_) {
|
|
626
|
-
if (typeof value !== "function") throw new Error("NotCallable");
|
|
627
|
-
return normalize(value(...arguments_));
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
export class HostGlobalUnavailable extends Error {
|
|
631
|
-
constructor(path) {
|
|
632
|
-
super("HostGlobalUnavailable: " + path.join("/"));
|
|
633
|
-
this.name = "HostGlobalUnavailable";
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
function resolveHostGlobal(path) {
|
|
638
|
-
let owner = globalThis;
|
|
639
|
-
let value = owner;
|
|
640
|
-
for (const segment of path) {
|
|
641
|
-
owner = value;
|
|
642
|
-
if (owner === null || owner === undefined || !(segment in Object(owner)))
|
|
643
|
-
throw new HostGlobalUnavailable(path);
|
|
644
|
-
value = owner[segment];
|
|
645
|
-
}
|
|
646
|
-
if (value === undefined) throw new HostGlobalUnavailable(path);
|
|
647
|
-
return { owner, value };
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
export function hostGlobal(path) {
|
|
651
|
-
return resolveHostGlobal(path).value;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
export function hostGlobalCall(path, ...arguments_) {
|
|
655
|
-
const { owner, value } = resolveHostGlobal(path);
|
|
656
|
-
if (typeof value !== "function") throw new Error("NotCallable");
|
|
657
|
-
return normalize(Reflect.apply(value, owner, arguments_));
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
export function callMethod(receiver, name, ...arguments_) {
|
|
661
|
-
const value = receiver?.[name];
|
|
662
|
-
if (typeof value !== "function") throw new Error("NotCallable");
|
|
663
|
-
return normalize(value.call(receiver, ...arguments_));
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
export function boundary(value) {
|
|
667
|
-
return finite(normalize(value));
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
export function requireArity(actual, requiredPositions, name) {
|
|
671
|
-
const missing = requiredPositions.find((position) => actual <= position);
|
|
672
|
-
if (missing !== undefined) {
|
|
673
|
-
throw new Error(`ArityError: ${name} is missing required argument ${missing + 1}`);
|
|
674
|
-
}
|
|
675
|
-
return null;
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
export function withArity(callback, requiredPositions, name) {
|
|
679
|
-
return function (...arguments_) {
|
|
680
|
-
requireArity(arguments_.length, requiredPositions, name);
|
|
681
|
-
return callback(...arguments_);
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
// Capture the environment even when context use is hidden in a downstream call.
|
|
686
|
-
// Each invocation uses fresh stack arrays and restores the caller synchronously.
|
|
687
|
-
export function bindContexts(callback) {
|
|
688
|
-
const captured = new Map([...contexts].map(([name, stack]) => [name, stack.at(-1)]));
|
|
689
|
-
return function (...args) {
|
|
690
|
-
const previous = new Map(contexts);
|
|
691
|
-
for (const [name, value] of captured) contexts.set(name, [value]);
|
|
692
|
-
try {
|
|
693
|
-
return Reflect.apply(callback, this, args);
|
|
694
|
-
} finally {
|
|
695
|
-
contexts.clear();
|
|
696
|
-
for (const [name, stack] of previous) contexts.set(name, stack);
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
export function contextGet(name) {
|
|
702
|
-
const stack = contexts.get(name);
|
|
703
|
-
if ((!stack || stack.length === 0) && name === "log") return defaultLog;
|
|
704
|
-
if ((!stack || stack.length === 0) && name === "i18n") return defaultI18n ??= createI18n();
|
|
705
|
-
if (!stack || stack.length === 0) {
|
|
706
|
-
throw new Error(`Lango context is unavailable: ${name}`);
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
return stack.at(-1);
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
export function contextSet(name, value) {
|
|
713
|
-
const stack = contexts.get(name) ?? [];
|
|
714
|
-
contexts.set(name, stack);
|
|
715
|
-
stack.push(value);
|
|
716
|
-
return name;
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
export function contextRestore(names) {
|
|
720
|
-
for (const name of names.reverse()) {
|
|
721
|
-
const stack = contexts.get(name);
|
|
722
|
-
stack.pop();
|
|
723
|
-
if (stack.length === 0) contexts.delete(name);
|
|
724
|
-
}
|
|
725
|
-
}
|