@olares/lares-core 0.15.9 → 0.15.10
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/log/report.js +46 -0
- package/package.json +2 -1
package/log/report.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Client diagnostics. One record shape, whether it is written here or posted in. */
|
|
2
|
+
|
|
3
|
+
export const REPORT_PATH = "/api/lares/log";
|
|
4
|
+
|
|
5
|
+
const SCOPE = /^[a-z][a-z0-9-]{0,31}$/;
|
|
6
|
+
const EVENT = /^[a-z][a-z0-9-]{0,39}$/;
|
|
7
|
+
const FIELD = /^[a-z][a-zA-Z0-9]{0,31}$/;
|
|
8
|
+
|
|
9
|
+
export function reportRecord(input = {}) {
|
|
10
|
+
const source = input && typeof input === "object" ? input : {};
|
|
11
|
+
const scope = typeof source.scope === "string" && SCOPE.test(source.scope) ? source.scope : "client";
|
|
12
|
+
const event = typeof source.event === "string" && EVENT.test(source.event) ? source.event : "event";
|
|
13
|
+
const record = { scope, event };
|
|
14
|
+
for (const [key, value] of Object.entries(source)) {
|
|
15
|
+
if (key === "scope" || key === "event" || !FIELD.test(key)) continue;
|
|
16
|
+
if (typeof value === "string") record[key] = value.slice(0, 180);
|
|
17
|
+
else if (typeof value === "number" && Number.isFinite(value)) record[key] = value;
|
|
18
|
+
else if (typeof value === "boolean") record[key] = value;
|
|
19
|
+
}
|
|
20
|
+
return record;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function writeReport(input) {
|
|
24
|
+
const record = reportRecord(input);
|
|
25
|
+
console.log(`[lares:${record.scope}]`, JSON.stringify(record));
|
|
26
|
+
return record;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Fire-and-forget. `request` is the host port when the page has no cookie.
|
|
31
|
+
* A rejected port does not reject the caller.
|
|
32
|
+
*/
|
|
33
|
+
export function postReport(input, options = {}) {
|
|
34
|
+
const body = reportRecord(input);
|
|
35
|
+
const url = options.url ?? REPORT_PATH;
|
|
36
|
+
const send = options.request;
|
|
37
|
+
const pending = typeof send === "function"
|
|
38
|
+
? send(url, { method: "POST", headers: { "content-type": "application/json" }, body })
|
|
39
|
+
: fetch(url, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
credentials: "include",
|
|
42
|
+
headers: { "content-type": "application/json" },
|
|
43
|
+
body: JSON.stringify(body),
|
|
44
|
+
});
|
|
45
|
+
return Promise.resolve(pending).then(() => true, () => false);
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olares/lares-core",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.10",
|
|
4
4
|
"description": "Lares 共享业务核心:Router、workspace、文件/媒体、Olares 身份(无 UI、无平台壳)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"i18n",
|
|
21
21
|
"icons",
|
|
22
22
|
"larepass",
|
|
23
|
+
"log",
|
|
23
24
|
"media",
|
|
24
25
|
"mcp",
|
|
25
26
|
"olares",
|