@watchtowerobservability/activity-render 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/README.md +43 -4
- package/dist/format.d.cts +16 -0
- package/dist/format.d.ts +16 -0
- package/dist/index.cjs +151 -32
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +149 -32
- package/dist/vectors.d.cts +33 -0
- package/dist/vectors.d.ts +33 -0
- package/package.json +7 -4
- package/vectors.json +3127 -0
package/dist/index.js
CHANGED
|
@@ -1,7 +1,138 @@
|
|
|
1
|
+
// src/format.ts
|
|
2
|
+
function decompose(n) {
|
|
3
|
+
const view = new DataView(new ArrayBuffer(8));
|
|
4
|
+
view.setFloat64(0, n);
|
|
5
|
+
const hi = view.getUint32(0);
|
|
6
|
+
const mantissa = BigInt(hi & 1048575) << 32n | BigInt(view.getUint32(4));
|
|
7
|
+
const biased = hi >>> 20 & 2047;
|
|
8
|
+
return biased === 0 ? [mantissa, -1074] : [mantissa | 1n << 52n, biased - 1075];
|
|
9
|
+
}
|
|
10
|
+
function fixed(n, dec) {
|
|
11
|
+
const [m, e] = decompose(n);
|
|
12
|
+
const scale = 10n ** BigInt(dec);
|
|
13
|
+
let q;
|
|
14
|
+
if (e >= 0) {
|
|
15
|
+
q = (m << BigInt(e)) * scale;
|
|
16
|
+
} else {
|
|
17
|
+
const num = m * scale;
|
|
18
|
+
const den = 1n << BigInt(-e);
|
|
19
|
+
q = num / den;
|
|
20
|
+
const twice = num % den * 2n;
|
|
21
|
+
if (twice > den || twice === den && (q & 1n) === 1n) q += 1n;
|
|
22
|
+
}
|
|
23
|
+
const s = q.toString().padStart(dec + 1, "0");
|
|
24
|
+
return dec > 0 ? `${s.slice(0, s.length - dec)}.${s.slice(s.length - dec)}` : s;
|
|
25
|
+
}
|
|
26
|
+
function ryu(n) {
|
|
27
|
+
if (n === 0) return Object.is(n, -0) ? "-0.0" : "0.0";
|
|
28
|
+
const [mant, ex] = Math.abs(n).toExponential().split("e");
|
|
29
|
+
const digits = mant.replace(".", "");
|
|
30
|
+
const kk = Number(ex) + 1;
|
|
31
|
+
const pad = kk - digits.length;
|
|
32
|
+
let out;
|
|
33
|
+
if (pad >= 0 && kk <= 16) out = `${digits}${"0".repeat(pad)}.0`;
|
|
34
|
+
else if (kk > 0 && kk <= 16) out = `${digits.slice(0, kk)}.${digits.slice(kk)}`;
|
|
35
|
+
else if (kk > -5 && kk <= 0) out = `0.${"0".repeat(-kk)}${digits}`;
|
|
36
|
+
else {
|
|
37
|
+
const e = kk - 1 > 0 ? `e+${kk - 1}` : `e${kk - 1}`;
|
|
38
|
+
out = digits.length === 1 ? `${digits}${e}` : `${digits.slice(0, 1)}.${digits.slice(1)}${e}`;
|
|
39
|
+
}
|
|
40
|
+
return n < 0 ? `-${out}` : out;
|
|
41
|
+
}
|
|
42
|
+
function numberText(n) {
|
|
43
|
+
if (!Number.isFinite(n)) return String(n);
|
|
44
|
+
if (Object.is(n, -0)) return "-0.0";
|
|
45
|
+
if (Number.isInteger(n) && n >= -(2 ** 63) && n < 2 ** 64) return BigInt(n).toString();
|
|
46
|
+
return ryu(n);
|
|
47
|
+
}
|
|
48
|
+
var ISO = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2})?/;
|
|
49
|
+
var STAMP = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})$/;
|
|
50
|
+
function asWritten(v, kind) {
|
|
51
|
+
const m = ISO.exec(v);
|
|
52
|
+
const cut = (n) => m && m[0].length >= n ? v.slice(0, n) : v;
|
|
53
|
+
if (kind === "date") return cut(10);
|
|
54
|
+
if (kind === "datetime") return cut(16).split("T").join(" ");
|
|
55
|
+
const t = m?.[1];
|
|
56
|
+
return t !== void 0 ? t.slice(1) : v;
|
|
57
|
+
}
|
|
58
|
+
var zones = /* @__PURE__ */ new Map();
|
|
59
|
+
function zoneClock(zone) {
|
|
60
|
+
let f = zones.get(zone);
|
|
61
|
+
if (f === void 0) {
|
|
62
|
+
try {
|
|
63
|
+
f = new Intl.DateTimeFormat("en-US", {
|
|
64
|
+
timeZone: zone,
|
|
65
|
+
hourCycle: "h23",
|
|
66
|
+
year: "numeric",
|
|
67
|
+
month: "numeric",
|
|
68
|
+
day: "numeric",
|
|
69
|
+
hour: "numeric",
|
|
70
|
+
minute: "numeric",
|
|
71
|
+
second: "numeric"
|
|
72
|
+
});
|
|
73
|
+
} catch {
|
|
74
|
+
f = null;
|
|
75
|
+
}
|
|
76
|
+
zones.set(zone, f);
|
|
77
|
+
}
|
|
78
|
+
return f;
|
|
79
|
+
}
|
|
80
|
+
function supportsTimeZone(zone) {
|
|
81
|
+
return zoneClock(zone) !== null;
|
|
82
|
+
}
|
|
83
|
+
function offsetSeconds(clock, utcMs) {
|
|
84
|
+
const p = {};
|
|
85
|
+
for (const part of clock.formatToParts(new Date(utcMs))) if (part.type !== "literal") p[part.type] = Number(part.value);
|
|
86
|
+
const wall = Date.UTC(p.year, p.month - 1, p.day, p.hour % 24, p.minute, p.second);
|
|
87
|
+
return (wall - utcMs) / 1e3;
|
|
88
|
+
}
|
|
89
|
+
var two = (n) => String(n).padStart(2, "0");
|
|
90
|
+
function inZone(v, kind, zone) {
|
|
91
|
+
const clock = zoneClock(zone);
|
|
92
|
+
const m = STAMP.exec(v);
|
|
93
|
+
if (clock === null || m === null) return void 0;
|
|
94
|
+
const [year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0] = [1, 2, 3, 4, 5, 6].map((i) => Number(m[i]));
|
|
95
|
+
const stamp = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
96
|
+
const check = new Date(stamp);
|
|
97
|
+
if (check.getUTCFullYear() !== year || check.getUTCMonth() + 1 !== month || check.getUTCDate() !== day || hour > 23 || minute > 59 || second > 59) return void 0;
|
|
98
|
+
const zoneText = m[7];
|
|
99
|
+
let shift = 0;
|
|
100
|
+
if (zoneText !== "Z") {
|
|
101
|
+
const [oh, om] = [Number(zoneText.slice(1, 3)), Number(zoneText.slice(4, 6))];
|
|
102
|
+
if (oh > 23 || om > 59) return void 0;
|
|
103
|
+
shift = (zoneText.startsWith("-") ? -1 : 1) * (oh * 3600 + om * 60);
|
|
104
|
+
}
|
|
105
|
+
const utcMs = stamp - shift * 1e3;
|
|
106
|
+
const utcYear = new Date(utcMs).getUTCFullYear();
|
|
107
|
+
if (utcYear < 1970 || utcYear > 2037) return void 0;
|
|
108
|
+
const local = new Date(utcMs + offsetSeconds(clock, utcMs) * 1e3);
|
|
109
|
+
const ymd = `${String(local.getUTCFullYear()).padStart(4, "0")}-${two(local.getUTCMonth() + 1)}-${two(local.getUTCDate())}`;
|
|
110
|
+
const hm = `${two(local.getUTCHours())}:${two(local.getUTCMinutes())}`;
|
|
111
|
+
return kind === "date" ? ymd : kind === "time" ? hm : `${ymd} ${hm}`;
|
|
112
|
+
}
|
|
113
|
+
function formatTime(v, kind, zone) {
|
|
114
|
+
return (zone ? inZone(v, kind, zone) : void 0) ?? asWritten(v, kind);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/vectors.ts
|
|
118
|
+
function runVectors(render2, vectors) {
|
|
119
|
+
const failures = [];
|
|
120
|
+
for (const c of vectors.cases) {
|
|
121
|
+
const expected = JSON.stringify(c.display);
|
|
122
|
+
let actual;
|
|
123
|
+
try {
|
|
124
|
+
actual = JSON.stringify(render2(c.event, c.theme, c.options));
|
|
125
|
+
} catch (e) {
|
|
126
|
+
actual = `threw ${String(e)}`;
|
|
127
|
+
}
|
|
128
|
+
if (actual !== expected) failures.push({ name: c.name, expected, actual });
|
|
129
|
+
}
|
|
130
|
+
return failures;
|
|
131
|
+
}
|
|
132
|
+
|
|
1
133
|
// src/index.ts
|
|
2
134
|
var MAX_VALUE_CHARS = 200;
|
|
3
135
|
var HOLE = /\{([A-Za-z0-9_.\-]+)((?:\|[a-z]+(?::[A-Za-z0-9_ .\-]*)?)*)\}/g;
|
|
4
|
-
var ISO = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2})?/;
|
|
5
136
|
var NUMBER = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
6
137
|
var CONTROL = /^[\u0000-\u001f\u007f-\u009f]$/;
|
|
7
138
|
var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
@@ -12,7 +143,8 @@ function actionParts(action) {
|
|
|
12
143
|
}
|
|
13
144
|
function text(v) {
|
|
14
145
|
if (typeof v === "string") return v;
|
|
15
|
-
if (typeof v === "number"
|
|
146
|
+
if (typeof v === "number") return numberText(v);
|
|
147
|
+
if (typeof v === "boolean") return String(v);
|
|
16
148
|
return void 0;
|
|
17
149
|
}
|
|
18
150
|
function lookup(e, path) {
|
|
@@ -56,18 +188,6 @@ function parseNumber(s) {
|
|
|
56
188
|
const n = Number(s);
|
|
57
189
|
return Number.isFinite(n) ? n : void 0;
|
|
58
190
|
}
|
|
59
|
-
function fixed(n, dec) {
|
|
60
|
-
if (n >= 1e21) return BigInt(n).toString() + (dec > 0 ? "." + "0".repeat(dec) : "");
|
|
61
|
-
const full = n.toFixed(100);
|
|
62
|
-
const dot = full.indexOf(".");
|
|
63
|
-
const frac = full.slice(dot + 1);
|
|
64
|
-
const tie = frac[dec] === "5" && /^0*$/.test(frac.slice(dec + 1));
|
|
65
|
-
if (!tie) return n.toFixed(dec);
|
|
66
|
-
let digits = BigInt(full.slice(0, dot) + frac.slice(0, dec));
|
|
67
|
-
if (digits % 2n === 1n) digits += 1n;
|
|
68
|
-
const s = digits.toString().padStart(dec + 1, "0");
|
|
69
|
-
return dec > 0 ? `${s.slice(0, s.length - dec)}.${s.slice(s.length - dec)}` : s;
|
|
70
|
-
}
|
|
71
191
|
function group(s) {
|
|
72
192
|
const neg = s.startsWith("-");
|
|
73
193
|
const body = neg ? s.slice(1) : s;
|
|
@@ -95,11 +215,7 @@ function count(v) {
|
|
|
95
215
|
if (n === void 0) return v;
|
|
96
216
|
return group(`${n < 0 || Object.is(n, -0) ? "-" : ""}${fixed(Math.abs(n), 0)}`);
|
|
97
217
|
}
|
|
98
|
-
function
|
|
99
|
-
const m = ISO.exec(v);
|
|
100
|
-
return m && m[0].length >= n ? v.slice(0, n) : v;
|
|
101
|
-
}
|
|
102
|
-
function interpolate(tpl, event) {
|
|
218
|
+
function interpolate(tpl, event, zone) {
|
|
103
219
|
return tpl.replace(HOLE, (_m, path, fmts) => {
|
|
104
220
|
let v = lookup(event, path) ?? "";
|
|
105
221
|
for (const f of fmts.split("|")) {
|
|
@@ -109,8 +225,7 @@ function interpolate(tpl, event) {
|
|
|
109
225
|
const arg = c >= 0 ? f.slice(c + 1) : "";
|
|
110
226
|
if (name === "money") v = money(v, arg);
|
|
111
227
|
else if (name === "count") v = count(v);
|
|
112
|
-
else if (name === "date") v =
|
|
113
|
-
else if (name === "datetime") v = date(v, 16).split("T").join(" ");
|
|
228
|
+
else if (name === "date" || name === "datetime" || name === "time") v = formatTime(v, name, zone);
|
|
114
229
|
}
|
|
115
230
|
return clean(v);
|
|
116
231
|
});
|
|
@@ -139,7 +254,7 @@ function fallbackTitle(e) {
|
|
|
139
254
|
function tags(locale) {
|
|
140
255
|
return locale == null ? [] : [locale, locale.split("-")[0] ?? locale];
|
|
141
256
|
}
|
|
142
|
-
function renderTemplate(tpl, event, locale) {
|
|
257
|
+
function renderTemplate(tpl, event, locale, zone) {
|
|
143
258
|
if (!tpl) return { title: fallbackTitle(event) };
|
|
144
259
|
const chain = [];
|
|
145
260
|
for (const k of tags(locale)) if (tpl.locales && has(tpl.locales, k)) chain.push(tpl.locales[k]);
|
|
@@ -153,17 +268,17 @@ function renderTemplate(tpl, event, locale) {
|
|
|
153
268
|
break;
|
|
154
269
|
}
|
|
155
270
|
}
|
|
156
|
-
const out = { title: label === void 0 ? fallbackTitle(event) : interpolate(label, event) };
|
|
271
|
+
const out = { title: label === void 0 ? fallbackTitle(event) : interpolate(label, event, zone) };
|
|
157
272
|
const subtitle = chain.find((l) => l.subtitle != null)?.subtitle;
|
|
158
|
-
if (subtitle != null) out.subtitle = interpolate(subtitle, event);
|
|
273
|
+
if (subtitle != null) out.subtitle = interpolate(subtitle, event, zone);
|
|
159
274
|
const fields = (chain.find((l) => l.fields != null)?.fields ?? []).map((f) => ({
|
|
160
|
-
label: interpolate(f.label, event),
|
|
161
|
-
value: interpolate(f.value, event)
|
|
275
|
+
label: interpolate(f.label, event, zone),
|
|
276
|
+
value: interpolate(f.value, event, zone)
|
|
162
277
|
}));
|
|
163
278
|
if (fields.length > 0) out.fields = fields;
|
|
164
279
|
return out;
|
|
165
280
|
}
|
|
166
|
-
function failureReason(theme, event, locale) {
|
|
281
|
+
function failureReason(theme, event, locale, zone) {
|
|
167
282
|
if (!isObj(event) || event.outcome !== "failure") return void 0;
|
|
168
283
|
const path = theme.failureReasonPath ?? theme.failure_reason_path;
|
|
169
284
|
const reasons = theme.failureReasons ?? theme.failure_reasons;
|
|
@@ -179,18 +294,20 @@ function failureReason(theme, event, locale) {
|
|
|
179
294
|
for (const k of tags(locale)) if (r.locales && has(r.locales, k)) chain.push(r.locales[k]);
|
|
180
295
|
const t = chain.find((l) => l.text != null)?.text ?? r.text;
|
|
181
296
|
const a = chain.find((l) => l.action != null)?.action ?? r.action;
|
|
182
|
-
const out = { code, text: interpolate(t, event) };
|
|
183
|
-
if (a != null) out.action = { label: interpolate(a.label, event), url: a.url };
|
|
297
|
+
const out = { code, text: interpolate(t, event, zone) };
|
|
298
|
+
if (a != null) out.action = { label: interpolate(a.label, event, zone), url: a.url };
|
|
184
299
|
return out;
|
|
185
300
|
}
|
|
186
301
|
function render(event, theme, options = {}) {
|
|
187
302
|
const action = isObj(event) && typeof event.action === "string" ? event.action : void 0;
|
|
188
303
|
const found = action !== void 0 && theme.actions ? templateFor(theme.actions, action) : void 0;
|
|
189
|
-
const out = renderTemplate(found ?? theme.fallbackTemplate ?? theme.fallback_template ?? void 0, event, options.locale);
|
|
190
|
-
const reason = failureReason(theme, event, options.locale);
|
|
304
|
+
const out = renderTemplate(found ?? theme.fallbackTemplate ?? theme.fallback_template ?? void 0, event, options.locale, options.timeZone);
|
|
305
|
+
const reason = failureReason(theme, event, options.locale, options.timeZone);
|
|
191
306
|
if (reason) out.failure_reason = reason;
|
|
192
307
|
return out;
|
|
193
308
|
}
|
|
194
309
|
export {
|
|
195
|
-
render
|
|
310
|
+
render,
|
|
311
|
+
runVectors,
|
|
312
|
+
supportsTimeZone
|
|
196
313
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** One case: render `event` with `theme` and `options` and the result must be exactly `display`. */
|
|
2
|
+
export interface VectorCase {
|
|
3
|
+
name: string;
|
|
4
|
+
event: unknown;
|
|
5
|
+
theme: unknown;
|
|
6
|
+
options: {
|
|
7
|
+
locale?: string | null;
|
|
8
|
+
timeZone?: string | null;
|
|
9
|
+
};
|
|
10
|
+
/** The `display` object of the API: compared as JSON text, so key order and omitted keys count. */
|
|
11
|
+
display: unknown;
|
|
12
|
+
/** `true` when the Rust renderer is known to produce `display` (a CI run passed on it); `false` when it was traced by hand. */
|
|
13
|
+
verified: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** The shape of `vectors.json`. */
|
|
16
|
+
export interface VectorFile {
|
|
17
|
+
about?: string;
|
|
18
|
+
format?: string;
|
|
19
|
+
cases: VectorCase[];
|
|
20
|
+
}
|
|
21
|
+
export interface VectorFailure {
|
|
22
|
+
name: string;
|
|
23
|
+
expected: string;
|
|
24
|
+
actual: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Run `vectors` (the parsed `vectors.json`) through `render` and return the cases that differ: `[]` means the renderer conforms.
|
|
28
|
+
* A renderer that throws fails its case with the error text as `actual`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function runVectors(render: (event: unknown, theme: never, options?: {
|
|
31
|
+
locale?: string | null;
|
|
32
|
+
timeZone?: string | null;
|
|
33
|
+
}) => unknown, vectors: VectorFile): VectorFailure[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** One case: render `event` with `theme` and `options` and the result must be exactly `display`. */
|
|
2
|
+
export interface VectorCase {
|
|
3
|
+
name: string;
|
|
4
|
+
event: unknown;
|
|
5
|
+
theme: unknown;
|
|
6
|
+
options: {
|
|
7
|
+
locale?: string | null;
|
|
8
|
+
timeZone?: string | null;
|
|
9
|
+
};
|
|
10
|
+
/** The `display` object of the API: compared as JSON text, so key order and omitted keys count. */
|
|
11
|
+
display: unknown;
|
|
12
|
+
/** `true` when the Rust renderer is known to produce `display` (a CI run passed on it); `false` when it was traced by hand. */
|
|
13
|
+
verified: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** The shape of `vectors.json`. */
|
|
16
|
+
export interface VectorFile {
|
|
17
|
+
about?: string;
|
|
18
|
+
format?: string;
|
|
19
|
+
cases: VectorCase[];
|
|
20
|
+
}
|
|
21
|
+
export interface VectorFailure {
|
|
22
|
+
name: string;
|
|
23
|
+
expected: string;
|
|
24
|
+
actual: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Run `vectors` (the parsed `vectors.json`) through `render` and return the cases that differ: `[]` means the renderer conforms.
|
|
28
|
+
* A renderer that throws fails its case with the error text as `actual`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function runVectors(render: (event: unknown, theme: never, options?: {
|
|
31
|
+
locale?: string | null;
|
|
32
|
+
timeZone?: string | null;
|
|
33
|
+
}) => unknown, vectors: VectorFile): VectorFailure[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@watchtowerobservability/activity-render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Watchtower activity renderer: the same display the API returns (templates, formatters, locales, failure reasons), client side, zero dependencies",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -18,10 +18,13 @@
|
|
|
18
18
|
"types": "./dist/index.d.cts",
|
|
19
19
|
"default": "./dist/index.cjs"
|
|
20
20
|
}
|
|
21
|
-
}
|
|
21
|
+
},
|
|
22
|
+
"./vectors.json": "./vectors.json"
|
|
22
23
|
},
|
|
23
24
|
"files": [
|
|
24
|
-
"dist"
|
|
25
|
+
"dist",
|
|
26
|
+
"vectors.json",
|
|
27
|
+
"CHANGELOG.md"
|
|
25
28
|
],
|
|
26
29
|
"engines": {
|
|
27
30
|
"node": ">=18"
|
|
@@ -30,7 +33,7 @@
|
|
|
30
33
|
"build": "node scripts/build.mjs",
|
|
31
34
|
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
32
35
|
"test": "vitest run",
|
|
33
|
-
"prepack": "
|
|
36
|
+
"prepack": "node scripts/prepack.mjs"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
39
|
"@types/node": "22.15.0",
|