@lango-project/lango-boss 0.0.0-stage → 0.1.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 +201 -0
- package/NOTICE +2 -0
- package/README.md +118 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/entries/cli.mjs +139 -0
- package/dist/.lango/entries/cli.mjs.map +1 -0
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +725 -0
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -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/boss.mjs +136 -0
- package/dist/boss.mjs.map +1 -0
- package/dist/claim_watch.mjs +99 -0
- package/dist/claim_watch.mjs.map +1 -0
- package/dist/cli.mjs +7 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/configuration.mjs +58 -0
- package/dist/configuration.mjs.map +1 -0
- package/dist/dispatcher.mjs +108 -0
- package/dist/dispatcher.mjs.map +1 -0
- package/dist/driver.mjs +98 -0
- package/dist/driver.mjs.map +1 -0
- package/dist/envelope.mjs +92 -0
- package/dist/envelope.mjs.map +1 -0
- package/dist/library.mjs +139 -0
- package/dist/library.mjs.map +1 -0
- package/dist/management.mjs +360 -0
- package/dist/management.mjs.map +1 -0
- package/dist/management_transaction.mjs +26 -0
- package/dist/management_transaction.mjs.map +1 -0
- package/dist/namespace.mjs +27 -0
- package/dist/namespace.mjs.map +1 -0
- package/dist/queue.mjs +161 -0
- package/dist/queue.mjs.map +1 -0
- package/dist/reads.mjs +359 -0
- package/dist/reads.mjs.map +1 -0
- package/dist/retention.mjs +133 -0
- package/dist/retention.mjs.map +1 -0
- package/dist/schedule_sync.mjs +222 -0
- package/dist/schedule_sync.mjs.map +1 -0
- package/dist/schedule_transaction.mjs +24 -0
- package/dist/schedule_transaction.mjs.map +1 -0
- package/dist/schedules.mjs +201 -0
- package/dist/schedules.mjs.map +1 -0
- package/dist/transaction.mjs +16 -0
- package/dist/transaction.mjs.map +1 -0
- package/dist/validation.mjs +90 -0
- package/dist/validation.mjs.map +1 -0
- package/dist/worker.mjs +316 -0
- package/dist/worker.mjs.map +1 -0
- package/docs/guide.md +88 -0
- package/docs/management.md +93 -0
- package/docs/producer.md +115 -0
- package/docs/reads.md +107 -0
- package/docs/reference.md +77 -0
- package/docs/release.md +103 -0
- package/docs/retention.md +47 -0
- package/docs/scheduling.md +79 -0
- package/docs/worker.md +90 -0
- package/examples/reports/README.md +118 -0
- package/examples/reports/app.lango +133 -0
- package/examples/reports/deploy.xml +6 -0
- package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
- package/examples/reports/dist/.lango/entries/app.mjs +133 -0
- package/examples/reports/dist/.lango/entries/app.mjs.map +1 -0
- package/examples/reports/dist/.lango/localization.mjs +108 -0
- package/examples/reports/dist/.lango/runtime.mjs +725 -0
- package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
- package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
- package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
- package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
- package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
- package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
- package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
- package/examples/reports/dist/.lango/xmldom/index.js +41 -0
- package/examples/reports/dist/.lango/xmldom/package.json +1 -0
- package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
- package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
- package/examples/reports/dist/app.mjs +7 -0
- package/examples/reports/dist/app.mjs.map +1 -0
- package/examples/reports/dist/handlers.mjs +202 -0
- package/examples/reports/dist/handlers.mjs.map +1 -0
- package/examples/reports/dist/setup.mjs +14 -0
- package/examples/reports/dist/setup.mjs.map +1 -0
- package/examples/reports/handlers.lango +153 -0
- package/examples/reports/jobs.xml +5 -0
- package/examples/reports/package.json +23 -0
- package/examples/reports/routes.xml +4 -0
- package/examples/reports/setup.lango +17 -0
- package/package.json +62 -4
- package/src/boss.lango +92 -0
- package/src/claim_watch.lango +58 -0
- package/src/cli.lango +103 -0
- package/src/configuration.lango +43 -0
- package/src/dispatcher.lango +75 -0
- package/src/driver.lango +72 -0
- package/src/envelope.lango +56 -0
- package/src/library.lango +66 -0
- package/src/management.lango +302 -0
- package/src/management_transaction.lango +28 -0
- package/src/namespace.lango +15 -0
- package/src/queue.lango +115 -0
- package/src/reads.lango +277 -0
- package/src/retention.lango +107 -0
- package/src/schedule_sync.lango +190 -0
- package/src/schedule_transaction.lango +22 -0
- package/src/schedules.lango +165 -0
- package/src/transaction.lango +23 -0
- package/src/validation.lango +45 -0
- package/src/worker.lango +230 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { initialize } from "../../setup.mjs";
|
|
2
|
+
import * as boss from "@lango-project/lango-boss";
|
|
3
|
+
import * as server from "@lango-project/lango-server";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as crypto from "node:crypto";
|
|
6
|
+
import {
|
|
7
|
+
hostGlobal as __hostGlobal,
|
|
8
|
+
println as __println,
|
|
9
|
+
bindContexts as __bindContexts,
|
|
10
|
+
fromJson as __fromJson,
|
|
11
|
+
toJson as __toJson,
|
|
12
|
+
requireBoolean as __requireBoolean,
|
|
13
|
+
normalize as __normalize,
|
|
14
|
+
boundary as __boundary,
|
|
15
|
+
requireArity as __requireArity,
|
|
16
|
+
withArity as __withArity,
|
|
17
|
+
} from "../runtime.mjs";
|
|
18
|
+
|
|
19
|
+
export function main(args) {
|
|
20
|
+
__requireArity(arguments.length, [0], "main");
|
|
21
|
+
args = __boundary(args);
|
|
22
|
+
let directory = __normalize(__hostGlobal(["process"]).cwd());
|
|
23
|
+
if (__requireBoolean(__requireBoolean(__normalize(args.length) === 1) && __requireBoolean(__normalize(args[0]) === "setup"))) {
|
|
24
|
+
return __normalize(server.withDatabase(directory, initialize));
|
|
25
|
+
}
|
|
26
|
+
if (__requireBoolean(__requireBoolean(__normalize(args.length) === 1) && __requireBoolean(__normalize(args[0]) === "server"))) {
|
|
27
|
+
return serve(directory);
|
|
28
|
+
}
|
|
29
|
+
if (__requireBoolean(__requireBoolean(__normalize(args.length) >= 2) && __requireBoolean(__normalize(args[0]) === "operator"))) {
|
|
30
|
+
return operate(directory, args);
|
|
31
|
+
}
|
|
32
|
+
throw "Usage: node dist/app.mjs setup|server|operator <action> [target]";
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function serve(directory) {
|
|
37
|
+
__requireArity(arguments.length, [0], "serve");
|
|
38
|
+
directory = __boundary(directory);
|
|
39
|
+
let producer = new boss.Boss(directory);
|
|
40
|
+
let state = { http: null, stopping: false };
|
|
41
|
+
let stop = __bindContexts(() => {
|
|
42
|
+
if (__requireBoolean(__normalize(state.stopping))) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
state.stopping = true;
|
|
46
|
+
return __normalize(state.http.close()).finally(__bindContexts(() => {
|
|
47
|
+
return __normalize(producer.close());
|
|
48
|
+
return null;
|
|
49
|
+
})).catch(__withArity(__bindContexts((error) => {
|
|
50
|
+
error = __boundary(error);
|
|
51
|
+
__normalize(__hostGlobal(["process"]).stderr.write(`Report server shutdown failed: ${error}\n`));
|
|
52
|
+
__hostGlobal(["process"]).exitCode = 1;
|
|
53
|
+
return null;
|
|
54
|
+
}), [0], "closure"));
|
|
55
|
+
return null;
|
|
56
|
+
});
|
|
57
|
+
return __normalize(producer.start()).then(__withArity(__bindContexts((ignored) => {
|
|
58
|
+
ignored = __boundary(ignored);
|
|
59
|
+
let builder = new server.Builder(directory);
|
|
60
|
+
__normalize(builder.loadDeployment());
|
|
61
|
+
__normalize(builder.addMiddleware("client", { module: "./dist/handlers.mjs", handler: "guard" }));
|
|
62
|
+
__normalize(builder.event({ name: "register", module: "./dist/handlers.mjs", handler: "register" }));
|
|
63
|
+
for (const handler of ["submit", "status", "whoami"]) {
|
|
64
|
+
__normalize(builder.endpoint({ path: `/${handler}`, module: "./dist/handlers.mjs", handler: handler, policy: "anonymous", middleware: "client" }));
|
|
65
|
+
}
|
|
66
|
+
__normalize(producer.configure(builder));
|
|
67
|
+
state.http = __normalize(builder.build());
|
|
68
|
+
return __normalize(state.http.start());
|
|
69
|
+
return null;
|
|
70
|
+
}), [0], "closure")).then(__withArity(__bindContexts((address) => {
|
|
71
|
+
address = __boundary(address);
|
|
72
|
+
__println(`Listening on ${__normalize(address.address)}:${__normalize(address.port)}`);
|
|
73
|
+
__normalize(__hostGlobal(["process"]).once("SIGTERM", stop));
|
|
74
|
+
__normalize(__hostGlobal(["process"]).once("SIGINT", stop));
|
|
75
|
+
return null;
|
|
76
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
77
|
+
error = __boundary(error);
|
|
78
|
+
return __normalize(producer.close()).finally(__bindContexts(() => {
|
|
79
|
+
if (__requireBoolean(__normalize(state.http) !== null)) {
|
|
80
|
+
return __normalize(state.http.close());
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
return null;
|
|
84
|
+
})).finally(__bindContexts(() => {
|
|
85
|
+
throw error;
|
|
86
|
+
return null;
|
|
87
|
+
}));
|
|
88
|
+
return null;
|
|
89
|
+
}), [0], "closure"));
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function operate(directory, args) {
|
|
94
|
+
__requireArity(arguments.length, [0, 1], "operate");
|
|
95
|
+
directory = __boundary(directory);
|
|
96
|
+
args = __boundary(args);
|
|
97
|
+
let action = __normalize(args[1]);
|
|
98
|
+
let read = ["jobs", "queues", "schedules"];
|
|
99
|
+
let changes = ["job", "retry", "cancel", "pauseQueue", "resumeQueue", "pauseSchedule", "resumeSchedule"];
|
|
100
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(read.includes(action)))) && __requireBoolean(!__requireBoolean(__normalize(changes.includes(action))))) || __requireBoolean(__normalize(args.length) > 3))) {
|
|
101
|
+
throw "Unknown operator action";
|
|
102
|
+
}
|
|
103
|
+
if (__requireBoolean(__requireBoolean(__normalize(changes.includes(action))) && __requireBoolean(__normalize(args.length) !== 3))) {
|
|
104
|
+
throw "Operator action requires a target";
|
|
105
|
+
}
|
|
106
|
+
let producer = new boss.Boss(directory);
|
|
107
|
+
let state = { executor: null };
|
|
108
|
+
return __normalize(producer.start()).then(__withArity(__bindContexts((ignored) => {
|
|
109
|
+
ignored = __boundary(ignored);
|
|
110
|
+
state.executor = __normalize(producer.executor({ identity: { module: "./dist/handlers.mjs", handler: "identity" }, workers: 1 }));
|
|
111
|
+
return __normalize(state.executor.start());
|
|
112
|
+
return null;
|
|
113
|
+
}), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
|
|
114
|
+
ignored = __boundary(ignored);
|
|
115
|
+
return __normalize(state.executor.execute({ id: `operator:${__normalize(crypto.randomUUID())}`, path: "/operator", input: { action: action, target: __normalize(args[2]) }, source: { kind: "system", identity: "operator" } }));
|
|
116
|
+
return null;
|
|
117
|
+
}), [0], "closure")).then(__withArity(__bindContexts((result) => {
|
|
118
|
+
result = __boundary(result);
|
|
119
|
+
__println(__toJson(result));
|
|
120
|
+
return null;
|
|
121
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
122
|
+
if (__requireBoolean(__normalize(state.executor) !== null)) {
|
|
123
|
+
return __normalize(state.executor.close());
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
return null;
|
|
127
|
+
})).finally(__bindContexts(() => {
|
|
128
|
+
return __normalize(producer.close());
|
|
129
|
+
return null;
|
|
130
|
+
}));
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=app.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.mjs","sources":["app.lango"],"sourcesContent":["import js \"@lango-project/lango-boss\" as boss\nimport js \"@lango-project/lango-server\" as server\nimport js \"node:path\" as path\nimport js \"node:crypto\" as crypto\nimport js global process\nimport \"./setup\"\n initialize()\n\nexport main(args)\n\nmain(args)\n directory = process.cwd()\n if args.length == 1 and args[0] == \"setup\"\n return server/withDatabase(directory, initialize)\n if args.length == 1 and args[0] == \"server\"\n return serve(directory)\n if args.length >= 2 and args[0] == \"operator\"\n return operate(directory, args)\n throw \"Usage: node dist/app.mjs setup|server|operator <action> [target]\"\n\nserve(directory)\n producer = new boss/Boss(directory)\n state = {\n http: null\n stopping: false\n }\n stop = ()\n capture state\n if state.stopping\n return null\n state.stopping = true\n return chain state.http.close()\n finally\n capture producer\n return producer.close()\n catch(error)\n process.stderr.write(\"Report server shutdown failed: ${error}\\n\")\n process.exitCode = 1\n return chain producer.start()\n then(ignored)\n capture producer\n capture state\n capture directory\n builder = new server/Builder(directory)\n builder.loadDeployment()\n builder.addMiddleware(\"client\", {\n module: \"./dist/handlers.mjs\"\n handler: \"guard\"\n })\n builder.event({\n name: \"register\"\n module: \"./dist/handlers.mjs\"\n handler: \"register\"\n })\n for [\"submit\", \"status\", \"whoami\"] as handler\n builder.endpoint({\n path: \"/${handler}\"\n module: \"./dist/handlers.mjs\"\n handler: handler\n policy: \"anonymous\"\n middleware: \"client\"\n })\n producer.configure(builder)\n state.http = builder.build()\n return state.http.start()\n then(address)\n capture stop\n println(\"Listening on ${address.address}:${address.port}\")\n process.once(\"SIGTERM\", stop)\n process.once(\"SIGINT\", stop)\n catch(error)\n capture producer\n return chain producer.close()\n finally\n capture state\n if state.http != null\n return state.http.close()\n return null\n finally\n capture error\n throw error\n\noperate(directory, args)\n action = args[1]\n read = [\"jobs\", \"queues\", \"schedules\"]\n changes = [\"job\", \"retry\", \"cancel\", \"pauseQueue\", \"resumeQueue\", \"pauseSchedule\", \"resumeSchedule\"]\n if (!read.includes(action) and !changes.includes(action)) or args.length > 3\n throw \"Unknown operator action\"\n if changes.includes(action) and args.length != 3\n throw \"Operator action requires a target\"\n producer = new boss/Boss(directory)\n state = {\n executor: null\n }\n return chain producer.start()\n then(ignored)\n capture producer\n capture state\n state.executor = producer.executor({\n identity: {\n module: \"./dist/handlers.mjs\"\n handler: \"identity\"\n }\n workers: 1\n })\n return state.executor.start()\n then(ignored)\n capture state\n capture args\n capture action\n capture crypto\n return state.executor.execute({\n id: \"operator:${crypto/randomUUID()}\"\n path: \"/operator\"\n input: {\n action: action\n target: args[2]\n }\n source: {\n kind: \"system\"\n identity: \"operator\"\n }\n })\n then(result)\n println(to_json(result))\n finally\n capture state\n if state.executor != null\n return state.executor.close()\n return null\n finally\n capture producer\n return producer.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","x_lango_build":{"fingerprint":"de6b6a15cef4dc3eee18ce9cb8904e17b2afc2697f2922b1220aa635acf2ffd6","outputHash":"c9b275c255d58e3f2b865104be7e5e7bc35101cb27b1791ee74859a95a2ad1be","mapHash":"a4d0df324d6a280200c4a9c987adb560e1da39a27d42324c21fd6d95b35e6be4"}}
|
|
@@ -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
|
+
}
|