@lango-project/lango-client 0.2.1 → 0.4.1
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 +1 -0
- package/NOTICE +5 -1
- package/README.md +603 -906
- package/cli/.lango/localization.mjs +108 -0
- package/{dist/node → cli}/.lango/runtime.mjs +120 -20
- package/cli/.lango/vendor/i18next.LICENSE +21 -0
- package/cli/.lango/vendor/i18next.mjs +2434 -0
- package/cli/create.mjs +62 -0
- package/cli/create.mjs.map +1 -0
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +120 -20
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -0
- package/dist/accordion.mjs +277 -0
- package/dist/accordion.mjs.map +1 -0
- package/dist/activities.mjs +185 -0
- package/dist/activities.mjs.map +1 -0
- package/dist/app.mjs +671 -0
- package/dist/app.mjs.map +1 -0
- package/dist/boot.mjs +24 -0
- package/dist/boot.mjs.map +1 -0
- package/dist/bootstrap-LICENSE +21 -0
- package/dist/bootstrap.min.css +6 -0
- package/dist/bootstrap.min.css.map +1 -0
- package/dist/button.mjs +77 -0
- package/dist/button.mjs.map +1 -0
- package/dist/chip.mjs +134 -0
- package/dist/chip.mjs.map +1 -0
- package/dist/client.css +102 -0
- package/dist/collections.mjs +77 -0
- package/dist/collections.mjs.map +1 -0
- package/dist/combo_box.mjs +408 -0
- package/dist/combo_box.mjs.map +1 -0
- package/dist/content.mjs +123 -0
- package/dist/content.mjs.map +1 -0
- package/dist/control.mjs +141 -0
- package/dist/control.mjs.map +1 -0
- package/dist/date_input.mjs +242 -0
- package/dist/date_input.mjs.map +1 -0
- package/dist/dimensions.mjs +102 -0
- package/dist/dimensions.mjs.map +1 -0
- package/dist/error_reporter.mjs +136 -0
- package/dist/error_reporter.mjs.map +1 -0
- package/dist/fields.mjs +454 -0
- package/dist/fields.mjs.map +1 -0
- package/dist/file_input.mjs +227 -0
- package/dist/file_input.mjs.map +1 -0
- package/dist/form.mjs +272 -0
- package/dist/form.mjs.map +1 -0
- package/dist/heartbeat.mjs +167 -254
- package/dist/heartbeat.mjs.map +1 -1
- package/dist/index.html +18 -0
- package/dist/indicators.mjs +127 -0
- package/dist/indicators.mjs.map +1 -0
- package/dist/input_changes.mjs +58 -0
- package/dist/input_changes.mjs.map +1 -0
- package/dist/input_group.mjs +38 -0
- package/dist/input_group.mjs.map +1 -0
- package/dist/layouts.mjs +195 -0
- package/dist/layouts.mjs.map +1 -0
- package/dist/library.mjs +130 -807
- package/dist/library.mjs.map +1 -1
- package/dist/longpoll.mjs +254 -0
- package/dist/longpoll.mjs.map +1 -0
- package/dist/main.mjs +147 -0
- package/dist/main.mjs.map +1 -0
- package/dist/markdown.mjs +61 -0
- package/dist/markdown.mjs.map +1 -0
- package/dist/menus.mjs +468 -0
- package/dist/menus.mjs.map +1 -0
- package/dist/navigation.mjs +260 -410
- package/dist/navigation.mjs.map +1 -1
- package/dist/numeric_input.mjs +181 -0
- package/dist/numeric_input.mjs.map +1 -0
- package/dist/otp_input.mjs +113 -0
- package/dist/otp_input.mjs.map +1 -0
- package/dist/presentations.mjs +852 -0
- package/dist/presentations.mjs.map +1 -0
- package/dist/renderer.mjs +295 -0
- package/dist/renderer.mjs.map +1 -0
- package/dist/rich_content.mjs +99 -0
- package/dist/rich_content.mjs.map +1 -0
- package/dist/table_scope.mjs +166 -0
- package/dist/table_scope.mjs.map +1 -0
- package/dist/tables.mjs +1052 -0
- package/dist/tables.mjs.map +1 -0
- package/dist/tabs.mjs +205 -0
- package/dist/tabs.mjs.map +1 -0
- package/dist/templates.mjs +46 -0
- package/dist/templates.mjs.map +1 -0
- package/dist/vendor/DOMPurify-LICENSE +202 -0
- package/dist/vendor/dompurify.mjs +2454 -0
- package/dist/vendor/flatpickr-LICENSE.md +21 -0
- package/dist/vendor/flatpickr.min.css +13 -0
- package/dist/vendor/flatpickr.mjs +2340 -0
- package/dist/vendor/flatpickr.mjs.map +7 -0
- package/dist/vendor/marked-LICENSE.md +44 -0
- package/dist/vendor/marked.esm.js.map +7 -0
- package/dist/vendor/marked.mjs +85 -0
- package/dist/vendor/purify.es.mjs.map +1 -0
- package/dist/widgets.mjs +197 -0
- package/dist/widgets.mjs.map +1 -0
- package/docs/implementation.md +93 -0
- package/package.json +25 -43
- package/src/accordion.lango +198 -0
- package/src/activities.lango +132 -0
- package/src/app.lango +540 -0
- package/src/boot.lango +12 -0
- package/src/button.lango +49 -0
- package/src/chip.lango +94 -0
- package/src/collections.lango +46 -0
- package/src/combo_box.lango +298 -0
- package/src/content.lango +75 -0
- package/src/control.lango +103 -0
- package/src/create.lango +42 -0
- package/src/date_input.lango +215 -0
- package/src/dimensions.lango +65 -0
- package/src/error_reporter.lango +95 -0
- package/src/fields.lango +327 -0
- package/src/file_input.lango +175 -0
- package/src/form.lango +195 -0
- package/src/heartbeat.lango +136 -199
- package/src/indicators.lango +94 -0
- package/src/input_changes.lango +33 -0
- package/src/input_group.lango +22 -0
- package/src/layouts.lango +137 -0
- package/src/library.lango +194 -599
- package/src/longpoll.lango +187 -0
- package/src/main.lango +118 -0
- package/src/markdown.lango +48 -0
- package/src/menus.lango +336 -0
- package/src/navigation.lango +209 -299
- package/src/numeric_input.lango +131 -0
- package/src/otp_input.lango +83 -0
- package/src/presentations.lango +617 -0
- package/src/renderer.lango +296 -0
- package/src/rich_content.lango +66 -0
- package/src/table_scope.lango +119 -0
- package/src/tables.lango +786 -0
- package/src/tabs.lango +144 -0
- package/src/templates.lango +29 -0
- package/src/widgets.lango +141 -0
- package/web/client.css +102 -0
- package/web/index.html +18 -0
- package/dist/application.mjs +0 -429
- package/dist/application.mjs.map +0 -1
- package/dist/coordinator.mjs +0 -91
- package/dist/coordinator.mjs.map +0 -1
- package/dist/demo.mjs +0 -110
- package/dist/demo.mjs.map +0 -1
- package/dist/demo_controls.mjs +0 -59
- package/dist/demo_controls.mjs.map +0 -1
- package/dist/errors.mjs +0 -201
- package/dist/errors.mjs.map +0 -1
- package/dist/form_state.mjs +0 -266
- package/dist/form_state.mjs.map +0 -1
- package/dist/inheritance.mjs +0 -240
- package/dist/inheritance.mjs.map +0 -1
- package/dist/input_values.mjs +0 -65
- package/dist/input_values.mjs.map +0 -1
- package/dist/node/.lango/xmldom/conventions.js +0 -429
- package/dist/node/.lango/xmldom/dom-parser.js +0 -591
- package/dist/node/.lango/xmldom/dom.js +0 -3759
- package/dist/node/.lango/xmldom/entities.js +0 -2171
- package/dist/node/.lango/xmldom/errors.js +0 -205
- package/dist/node/.lango/xmldom/grammar.js +0 -559
- package/dist/node/.lango/xmldom/index.js +0 -41
- package/dist/node/.lango/xmldom/package.json +0 -1
- package/dist/node/.lango/xmldom/sax.js +0 -983
- package/dist/node/.lango/xmldom.LICENSE.md +0 -8
- package/dist/node/browser_host.mjs +0 -351
- package/dist/node/browser_host.mjs.map +0 -1
- package/dist/node/create_app.mjs +0 -118
- package/dist/node/create_app.mjs.map +0 -1
- package/dist/presentation.mjs +0 -89
- package/dist/presentation.mjs.map +0 -1
- package/dist/protocol.mjs +0 -706
- package/dist/protocol.mjs.map +0 -1
- package/dist/session.mjs +0 -471
- package/dist/session.mjs.map +0 -1
- package/dist/tokens.mjs +0 -298
- package/dist/tokens.mjs.map +0 -1
- package/examples/contacts.lango +0 -293
- package/examples/contacts_flow.lango +0 -174
- package/examples/contacts_store.lango +0 -109
- package/examples/contacts_store_test.lango +0 -65
- package/examples/demo.css +0 -87
- package/examples/demo_test.lango +0 -356
- package/examples/index.html +0 -32
- package/examples/pages.lango +0 -174
- package/examples/server.lango +0 -189
- package/examples/worker.lango +0 -142
- package/src/application.lango +0 -327
- package/src/browser_host.lango +0 -276
- package/src/coordinator.lango +0 -57
- package/src/demo.lango +0 -110
- package/src/demo_controls.lango +0 -53
- package/src/errors.lango +0 -149
- package/src/errors_test.lango +0 -157
- package/src/form_state.lango +0 -168
- package/src/heartbeat_test.lango +0 -479
- package/src/inheritance.lango +0 -167
- package/src/input_values.lango +0 -37
- package/src/logout_test.lango +0 -564
- package/src/navigation_test.lango +0 -556
- package/src/presentation.lango +0 -55
- package/src/protocol.lango +0 -787
- package/src/renderer_test.lango +0 -2047
- package/src/session.lango +0 -343
- package/src/session_test.lango +0 -455
- package/src/tabs_test.lango +0 -216
- package/src/tokens.lango +0 -215
- package/template/.vscode/extensions.json +0 -3
- package/template/.vscode/settings.json +0 -9
- package/template/README.md +0 -44
- package/template/gitignore +0 -4
- package/template/package.json +0 -22
- package/template/public/app.css +0 -10
- package/template/public/index.html +0 -24
- package/template/src/client.lango +0 -78
- package/template/src/pages.lango +0 -55
- package/template/src/server.lango +0 -24
- package/template/src/worker.lango +0 -77
- package/template/test/app.lango +0 -9
- package/tools/browser_runner.lango +0 -32
- package/tools/browser_test.lango +0 -343
- package/tools/create_app.lango +0 -88
- package/tools/create_app_test.lango +0 -43
- package/tools/starter_test.lango +0 -227
- /package/{dist/node → cli}/.lango/bignumber.mjs +0 -0
- /package/{dist → cli}/.lango/xmldom/conventions.js +0 -0
- /package/{dist → cli}/.lango/xmldom/dom-parser.js +0 -0
- /package/{dist → cli}/.lango/xmldom/dom.js +0 -0
- /package/{dist → cli}/.lango/xmldom/entities.js +0 -0
- /package/{dist → cli}/.lango/xmldom/errors.js +0 -0
- /package/{dist → cli}/.lango/xmldom/grammar.js +0 -0
- /package/{dist → cli}/.lango/xmldom/index.js +0 -0
- /package/{dist → cli}/.lango/xmldom/package.json +0 -0
- /package/{dist → cli}/.lango/xmldom/sax.js +0 -0
- /package/{dist → cli}/.lango/xmldom.LICENSE.md +0 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import i18next from "./vendor/i18next.mjs";
|
|
2
|
+
|
|
3
|
+
function record(value, label) {
|
|
4
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
5
|
+
throw new TypeError(`${label} must be a map`);
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function language(value) {
|
|
10
|
+
if (typeof value !== "string" || value === "") throw new TypeError("locale must be a language tag");
|
|
11
|
+
return Intl.getCanonicalLocales(value)[0];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function numeric(value) {
|
|
15
|
+
// Decimal instances from independently generated runtimes share this backend marker.
|
|
16
|
+
if (value?._isBigNumber === true && typeof value.toFixed === "function") {
|
|
17
|
+
if (!value.isFinite()) throw new TypeError("formatNumber requires a finite value");
|
|
18
|
+
return value.toFixed();
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === "bigint") return value;
|
|
21
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
22
|
+
throw new TypeError("formatNumber requires a finite number or Decimal");
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function dateValue(value) {
|
|
27
|
+
if (typeof value === "string") {
|
|
28
|
+
const parts = /^(\d{4})-(\d{2})-(\d{2})(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2}))?$/.exec(value);
|
|
29
|
+
if (!parts) throw new TypeError("formatDate requires an ISO date or an instant with a time-zone offset");
|
|
30
|
+
const calendar = new Date(0);
|
|
31
|
+
calendar.setUTCFullYear(Number(parts[1]), Number(parts[2]) - 1, Number(parts[3]));
|
|
32
|
+
if (calendar.getUTCMonth() !== Number(parts[2]) - 1 || calendar.getUTCDate() !== Number(parts[3]))
|
|
33
|
+
throw new TypeError("formatDate requires a valid date");
|
|
34
|
+
}
|
|
35
|
+
if (!(value instanceof Date) && typeof value !== "string" && typeof value !== "number")
|
|
36
|
+
throw new TypeError("formatDate requires a Date, ISO string or epoch milliseconds");
|
|
37
|
+
const date = new Date(value);
|
|
38
|
+
if (!Number.isFinite(date.getTime())) throw new TypeError("formatDate requires a valid date");
|
|
39
|
+
return date;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Create an immutable, synchronous translator; catalogs are application-owned data. */
|
|
43
|
+
export function createI18n(options = {}) {
|
|
44
|
+
record(options, "i18n options");
|
|
45
|
+
const locale = language(options.locale ?? "en");
|
|
46
|
+
const fallbackLocale = language(options.fallbackLocale ?? "en");
|
|
47
|
+
const timeZone = new Intl.DateTimeFormat("en", { timeZone: options.timeZone ?? "UTC" }).resolvedOptions().timeZone;
|
|
48
|
+
const currency = options.currency == null ? null : options.currency;
|
|
49
|
+
if (currency !== null) new Intl.NumberFormat("en", { style: "currency", currency });
|
|
50
|
+
const resources = Object.create(null);
|
|
51
|
+
const catalogs = { ...record(options.catalogs ?? {}, "catalogs") };
|
|
52
|
+
if (options.catalog != null) catalogs[locale] = { ...catalogs[locale], ...record(options.catalog, "catalog") };
|
|
53
|
+
for (const [tag, catalog] of Object.entries(catalogs)) {
|
|
54
|
+
const messages = record(catalog, "translation catalog");
|
|
55
|
+
if (Object.values(messages).some(value => typeof value !== "string"))
|
|
56
|
+
throw new TypeError("translation catalog entries must be strings");
|
|
57
|
+
resources[language(tag)] = { translation: { ...messages } };
|
|
58
|
+
}
|
|
59
|
+
const engine = i18next.createInstance();
|
|
60
|
+
// All resources are in memory: no async backend, network lookup, or global language mutation.
|
|
61
|
+
engine.init({
|
|
62
|
+
lng: locale, fallbackLng: fallbackLocale, resources, initAsync: false,
|
|
63
|
+
keySeparator: false, nsSeparator: false, returnNull: false,
|
|
64
|
+
interpolation: { escapeValue: false },
|
|
65
|
+
});
|
|
66
|
+
const fixed = engine.getFixedT(locale);
|
|
67
|
+
const formatNumber = (value, options = null) => {
|
|
68
|
+
const settings = { ...record(options ?? {}, "number format options") };
|
|
69
|
+
if (settings.style === "currency" && settings.currency == null && currency !== null)
|
|
70
|
+
settings.currency = currency;
|
|
71
|
+
return new Intl.NumberFormat(locale, settings).format(numeric(value));
|
|
72
|
+
};
|
|
73
|
+
const formatDate = (value, options = null) => {
|
|
74
|
+
const settings = { timeZone, ...record(options ?? {}, "date format options") };
|
|
75
|
+
return new Intl.DateTimeFormat(locale, settings).format(dateValue(value));
|
|
76
|
+
};
|
|
77
|
+
const translate = (template, values = [], options = null) => {
|
|
78
|
+
if (typeof template !== "string" || !Array.isArray(values))
|
|
79
|
+
throw new TypeError("translation requires a template and a values array");
|
|
80
|
+
const settings = record(options ?? {}, "translation options");
|
|
81
|
+
if (settings.count != null && (typeof settings.count !== "number" || !Number.isFinite(settings.count)))
|
|
82
|
+
throw new TypeError("translation count must be a finite number");
|
|
83
|
+
if (settings.context != null && typeof settings.context !== "string")
|
|
84
|
+
throw new TypeError("translation context must be a string");
|
|
85
|
+
const params = record(settings.formatParams ?? {}, "formatParams");
|
|
86
|
+
const message = fixed(template, {
|
|
87
|
+
defaultValue: template, skipInterpolation: true,
|
|
88
|
+
...(settings.count == null ? {} : { count: settings.count }),
|
|
89
|
+
...(settings.context == null ? {} : { context: settings.context }),
|
|
90
|
+
});
|
|
91
|
+
// Render once: values are data and can never introduce further placeholders or code.
|
|
92
|
+
return message.replace(/{{|}}|{(\d+)(?:,\s*(number|currency|percent|date|datetime))?}/g, (token, index, format) => {
|
|
93
|
+
if (token === "{{") return "{";
|
|
94
|
+
if (token === "}}") return "}";
|
|
95
|
+
if (Number(index) >= values.length) throw new RangeError(`missing translation parameter {${index}}`);
|
|
96
|
+
const value = values[index];
|
|
97
|
+
const options = record(params[index] ?? {}, "parameter format options");
|
|
98
|
+
switch (format) {
|
|
99
|
+
case "number": return formatNumber(value, options);
|
|
100
|
+
case "currency": return formatNumber(value, { ...options, style: "currency" });
|
|
101
|
+
case "percent": return formatNumber(value, { ...options, style: "percent" });
|
|
102
|
+
case "date": case "datetime": return formatDate(value, options);
|
|
103
|
+
default: return String(value);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
return Object.freeze({ locale, fallbackLocale, timeZone, currency, translate, formatNumber, formatDate });
|
|
108
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import BigNumber from "./bignumber.mjs";
|
|
2
|
+
import { createI18n } from "./localization.mjs";
|
|
3
|
+
export { createI18n };
|
|
2
4
|
import { createRequire } from "node:module";
|
|
3
5
|
const require = createRequire(import.meta.url);
|
|
4
6
|
const { DOMParser } = require("./xmldom/index.js");
|
|
@@ -31,11 +33,76 @@ export function toJsNumber(value) {
|
|
|
31
33
|
return new Decimal(value).toNumber();
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
let
|
|
36
|
+
let defaultI18n = null;
|
|
35
37
|
let output = null;
|
|
36
38
|
let database = null;
|
|
37
|
-
|
|
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
|
+
}
|
|
38
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;
|
|
39
106
|
const contextStore = Symbol.for("lango.contexts");
|
|
40
107
|
const contexts = globalThis[contextStore] ?? new Map();
|
|
41
108
|
globalThis[contextStore] = contexts;
|
|
@@ -50,6 +117,8 @@ export class RuntimeConfigurationError extends Error {
|
|
|
50
117
|
export function configure(options = {}) {
|
|
51
118
|
if (options === null || typeof options !== "object" || Array.isArray(options))
|
|
52
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");
|
|
53
122
|
const nextCatalog = options.catalog ?? {};
|
|
54
123
|
const nextOutput = options.output ?? null;
|
|
55
124
|
const nextDatabase = options.database_adapter ?? null;
|
|
@@ -59,38 +128,57 @@ export function configure(options = {}) {
|
|
|
59
128
|
throw new RuntimeConfigurationError("runtime output must be a function or null");
|
|
60
129
|
if (nextDatabase !== null && (typeof nextDatabase !== "object" && typeof nextDatabase !== "function"))
|
|
61
130
|
throw new RuntimeConfigurationError("runtime database_adapter must be an object or null");
|
|
62
|
-
|
|
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;
|
|
63
145
|
output = nextOutput;
|
|
146
|
+
if (database !== null) failedTransactions.delete(database);
|
|
64
147
|
database = nextDatabase;
|
|
65
|
-
|
|
148
|
+
if (database !== null) failedTransactions.delete(database);
|
|
66
149
|
contexts.clear();
|
|
67
150
|
return null;
|
|
68
151
|
}
|
|
69
152
|
|
|
70
153
|
function requireDatabase() {
|
|
71
|
-
|
|
72
|
-
|
|
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;
|
|
73
158
|
}
|
|
74
159
|
|
|
75
160
|
export function startTrans() {
|
|
76
|
-
|
|
77
|
-
|
|
161
|
+
const adapter = requireDatabase();
|
|
162
|
+
if (failedTransactions.has(adapter)) throw new Error("TransactionFailed");
|
|
163
|
+
adapter.begin();
|
|
78
164
|
return null;
|
|
79
165
|
}
|
|
80
166
|
export function commitTrans() {
|
|
81
|
-
|
|
82
|
-
|
|
167
|
+
const adapter = requireDatabase();
|
|
168
|
+
if (failedTransactions.has(adapter)) throw new Error("TransactionFailed");
|
|
169
|
+
adapter.commit();
|
|
83
170
|
return null;
|
|
84
171
|
}
|
|
85
172
|
export function rollbackTrans() {
|
|
86
173
|
const adapter = requireDatabase();
|
|
87
174
|
adapter.rollback();
|
|
88
|
-
if (adapter.transactionState() === null)
|
|
175
|
+
if (adapter.transactionState() === null) failedTransactions.delete(adapter);
|
|
89
176
|
return null;
|
|
90
177
|
}
|
|
91
178
|
export function transState() {
|
|
92
|
-
const
|
|
93
|
-
|
|
179
|
+
const adapter = requireDatabase();
|
|
180
|
+
const state = adapter.transactionState();
|
|
181
|
+
return failedTransactions.has(adapter) && state !== null ? DB.Failed : state;
|
|
94
182
|
}
|
|
95
183
|
|
|
96
184
|
export class SqlError extends Error {
|
|
@@ -118,10 +206,16 @@ export function query(sql, parameterNames) {
|
|
|
118
206
|
]),
|
|
119
207
|
);
|
|
120
208
|
try {
|
|
121
|
-
|
|
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");
|
|
122
216
|
} catch (error) {
|
|
123
217
|
if (error?.transactionUnusable === true && adapter.transactionState() !== null)
|
|
124
|
-
|
|
218
|
+
failedTransactions.add(adapter);
|
|
125
219
|
throw new SqlError(error, sql);
|
|
126
220
|
}
|
|
127
221
|
rows = result.rows;
|
|
@@ -155,10 +249,14 @@ export function query(sql, parameterNames) {
|
|
|
155
249
|
return proxy;
|
|
156
250
|
}
|
|
157
251
|
|
|
158
|
-
export function translate(template, values) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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);
|
|
162
260
|
}
|
|
163
261
|
|
|
164
262
|
export function xmlEscape(value) {
|
|
@@ -249,7 +347,8 @@ export function println(value) {
|
|
|
249
347
|
}
|
|
250
348
|
|
|
251
349
|
const defaultLog = Object.freeze({
|
|
252
|
-
|
|
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); },
|
|
253
352
|
info(value) { return writeLog(process.stdout, "INFO", value); },
|
|
254
353
|
warn(value) { return writeLog(process.stderr, "WARN", value); },
|
|
255
354
|
err(value) { return writeLog(process.stderr, "ERROR", value); },
|
|
@@ -586,6 +685,7 @@ export function withArity(callback, requiredPositions, name) {
|
|
|
586
685
|
export function contextGet(name) {
|
|
587
686
|
const stack = contexts.get(name);
|
|
588
687
|
if ((!stack || stack.length === 0) && name === "log") return defaultLog;
|
|
688
|
+
if ((!stack || stack.length === 0) && name === "i18n") return defaultI18n ??= createI18n();
|
|
589
689
|
if (!stack || stack.length === 0) {
|
|
590
690
|
throw new Error(`Lango context is unavailable: ${name}`);
|
|
591
691
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2011-present i18next
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|