dsh-log 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/dist/host.js ADDED
@@ -0,0 +1,55 @@
1
+ // AUTO-GENERATED by node packages/dsh-log/build.mjs — DO NOT EDIT. Source: packages/dsh-log/src/host.ts
2
+ import {
3
+ buildPhoneNames
4
+ } from "./config.js";
5
+ import { createLogStore } from "./store.js";
6
+ import { buildPhoneName, buildPhoneNames as buildPhoneNames2, resolveHostLogConfig } from "./config.js";
7
+ import { parseEventListManifest, checkEventFields, checkEventCounts } from "./config.js";
8
+ import {
9
+ LOG_DEBOUNCE_MS,
10
+ LOG_DIR_NAME,
11
+ LOG_SWITCH_FILE,
12
+ formatDailyFileName,
13
+ formatLogFileName,
14
+ createLogStore as createLogStore2
15
+ } from "./store.js";
16
+ function createHostLog(deps, configInput) {
17
+ const store = createLogStore(deps, configInput);
18
+ const phoneNames = buildPhoneNames(store.config.prefix);
19
+ return { store, phoneNames };
20
+ }
21
+ function registerHostLogPhones(registry, hostLog) {
22
+ const store = hostLog.store;
23
+ const handlers = {
24
+ logBatch: (args) => store.handleLogBatch(args),
25
+ logExport: (args) => store.handleLogExport(args),
26
+ logClear: (args) => store.handleLogClear(args),
27
+ logGetSwitch: () => store.handleLogGetSwitch(),
28
+ logSetSwitch: (args) => store.handleLogSetSwitch(args)
29
+ };
30
+ const actions = ["logBatch", "logExport", "logClear", "logGetSwitch", "logSetSwitch"];
31
+ for (const action of actions) {
32
+ const name = hostLog.phoneNames[action];
33
+ if (registry.has(name)) {
34
+ throw new Error("[dsh-log] \u7535\u8BDD\u540D\u5DF2\u88AB\u6CE8\u518C\uFF0C\u4E0D\u8986\u76D6\u65E7\u7684\uFF1A" + name);
35
+ }
36
+ registry.set(name, handlers[action]);
37
+ }
38
+ return hostLog.phoneNames;
39
+ }
40
+ export {
41
+ LOG_DEBOUNCE_MS,
42
+ LOG_DIR_NAME,
43
+ LOG_SWITCH_FILE,
44
+ buildPhoneName,
45
+ buildPhoneNames2 as buildPhoneNames,
46
+ checkEventCounts,
47
+ checkEventFields,
48
+ createHostLog,
49
+ createLogStore2 as createLogStore,
50
+ formatDailyFileName,
51
+ formatLogFileName,
52
+ parseEventListManifest,
53
+ registerHostLogPhones,
54
+ resolveHostLogConfig
55
+ };
package/dist/node.js ADDED
@@ -0,0 +1,124 @@
1
+ // AUTO-GENERATED by node packages/dsh-log/build.mjs — DO NOT EDIT. Source: packages/dsh-log/src/node.ts
2
+ import { readFile, readdir, mkdir, unlink, writeFile } from "node:fs/promises";
3
+ import { dirname, join } from "node:path";
4
+ import { createHostLog } from "./host.js";
5
+ import { logFileNamePattern } from "./config.js";
6
+ function resolvePath(pathStr) {
7
+ return Promise.resolve({ path: String(pathStr) });
8
+ }
9
+ function targetPath(target) {
10
+ if (target && typeof target === "object" && typeof target.path === "string") {
11
+ return target.path;
12
+ }
13
+ return String(target);
14
+ }
15
+ function createNodeFileService() {
16
+ return {
17
+ resolve: resolvePath,
18
+ async readText(target) {
19
+ try {
20
+ return await readFile(targetPath(target), "utf8");
21
+ } catch (e) {
22
+ if (e && e.code === "ENOENT") return "";
23
+ throw e;
24
+ }
25
+ },
26
+ async writeText(target, text) {
27
+ const target2 = targetPath(target);
28
+ await mkdir(dirname(target2), { recursive: true });
29
+ await writeFile(target2, text, "utf8");
30
+ },
31
+ async mkdir(dir) {
32
+ await mkdir(String(dir), { recursive: true });
33
+ },
34
+ async unlink(target) {
35
+ try {
36
+ await unlink(targetPath(target));
37
+ } catch (e) {
38
+ if (e && e.code === "ENOENT") return;
39
+ throw e;
40
+ }
41
+ },
42
+ async listDir(target) {
43
+ try {
44
+ return await readdir(targetPath(target));
45
+ } catch (e) {
46
+ if (e && e.code === "ENOENT") return [];
47
+ throw e;
48
+ }
49
+ }
50
+ };
51
+ }
52
+ function createNodeTimer() {
53
+ return {
54
+ timeout: (fn, ms) => setTimeout(fn, ms)
55
+ };
56
+ }
57
+ function delay(ms) {
58
+ return new Promise((resolve) => {
59
+ setTimeout(() => resolve(), ms);
60
+ });
61
+ }
62
+ async function markerLanded(store, cacheDir, marker) {
63
+ try {
64
+ const logDir = join(cacheDir, store.config.logDirName);
65
+ const pattern = logFileNamePattern(store.config);
66
+ const names = await readdir(logDir);
67
+ for (const name of names) {
68
+ if (!pattern.test(name)) continue;
69
+ const text = await readFile(join(logDir, name), "utf8");
70
+ if (text.includes(`"event":"${marker}"`)) return true;
71
+ }
72
+ return false;
73
+ } catch (e) {
74
+ void e;
75
+ return false;
76
+ }
77
+ }
78
+ async function createNodeHostLog(deps, configInput) {
79
+ if (!deps || typeof deps.cacheDir !== "string" || deps.cacheDir === "") {
80
+ throw new Error("[dsh-log] \u5EFA Node \u65E5\u5FD7\u5E93\u5FC5\u987B\u7ED9 cacheDir\uFF1A\u65E5\u5FD7\u5199\u5230\u54EA\u7531\u8C03\u7528\u65B9\u51B3\u5B9A\uFF0C\u672C\u5165\u53E3\u4E0D\u731C\u4F4D\u7F6E");
81
+ }
82
+ const cacheDir = deps.cacheDir;
83
+ const fs = deps.fs ?? createNodeFileService();
84
+ const timer = deps.timer ?? createNodeTimer();
85
+ const joinPath = deps.joinPath ?? ((...parts) => join(...parts));
86
+ await mkdir(cacheDir, { recursive: true });
87
+ const hostLog = createHostLog(
88
+ { fs, timer, getCacheDir: () => cacheDir, DEFAULT_CWD: cacheDir },
89
+ configInput
90
+ );
91
+ const store = hostLog.store;
92
+ try {
93
+ await store.loadSwitch();
94
+ } catch (e) {
95
+ void e;
96
+ }
97
+ const ready = (async () => {
98
+ const marker = "log.flush.marker";
99
+ let flushed = false;
100
+ try {
101
+ store.log("debug", marker, {});
102
+ flushed = true;
103
+ } catch (e) {
104
+ void e;
105
+ }
106
+ for (let pass = 0; pass < 6; pass++) {
107
+ try {
108
+ if (typeof store.flushNow === "function") await store.flushNow();
109
+ else store.flush();
110
+ } catch (e) {
111
+ void e;
112
+ }
113
+ if (!flushed) return;
114
+ if (await markerLanded(hostLog.store, cacheDir, marker)) return;
115
+ await delay(10);
116
+ }
117
+ })();
118
+ return { store: hostLog.store, phoneNames: hostLog.phoneNames, ready };
119
+ }
120
+ export {
121
+ createNodeFileService,
122
+ createNodeHostLog,
123
+ createNodeTimer
124
+ };
package/dist/phones.js ADDED
@@ -0,0 +1,266 @@
1
+ // AUTO-GENERATED by node packages/dsh-log/build.mjs — DO NOT EDIT. Source: packages/dsh-log/src/phones.ts
2
+ import {
3
+ formatDailyFileName,
4
+ resolveLogFileName,
5
+ matchExportFileName,
6
+ logFileNamePattern
7
+ } from "./config.js";
8
+ function hash8(s) {
9
+ try {
10
+ const t = String(s || "");
11
+ let h = 5381;
12
+ for (let i = 0; i < t.length; i++) h = (h << 5) + h + t.charCodeAt(i) >>> 0;
13
+ return ("0000000" + h.toString(16)).slice(-8);
14
+ } catch (e) {
15
+ void e;
16
+ return "00000000";
17
+ }
18
+ }
19
+ function targetToPath(t, fb) {
20
+ if (typeof t === "string") return t;
21
+ if (t && typeof t === "object") {
22
+ const c = t.displayPath || t.path;
23
+ if (typeof c === "string" && c) return c;
24
+ }
25
+ return typeof fb === "string" && fb ? fb : "";
26
+ }
27
+ async function listFileNames(ctx, logDir) {
28
+ const { fs, getPlatform, resolveTarget } = ctx;
29
+ try {
30
+ const platform = typeof getPlatform === "function" ? await getPlatform() : null;
31
+ const listFn = platform && platform.fs && typeof platform.fs.listDir === "function" ? platform.fs.listDir.bind(platform.fs) : fs && typeof fs.listDir === "function" ? fs.listDir.bind(fs) : null;
32
+ if (!listFn) return [];
33
+ const dirTarget = await resolveTarget(logDir);
34
+ const entries = await listFn(dirTarget);
35
+ if (!Array.isArray(entries)) return [];
36
+ return entries.map((x) => typeof x === "string" ? x : x && typeof x.name === "string" ? x.name : "");
37
+ } catch (e) {
38
+ void e;
39
+ return [];
40
+ }
41
+ }
42
+ function createLogPhones(ctx) {
43
+ const config = ctx.config;
44
+ const filePattern = logFileNamePattern(config);
45
+ const pidOf = () => {
46
+ try {
47
+ if (typeof ctx.getPid === "function") return ctx.getPid();
48
+ const header = typeof ctx.getHeaderInfo === "function" ? ctx.getHeaderInfo() : null;
49
+ if (header && typeof header.pid === "number") return header.pid;
50
+ const g = globalThis;
51
+ return (g.process && typeof g.process.pid === "number" ? g.process.pid : 0) || 0;
52
+ } catch (e) {
53
+ void e;
54
+ return 0;
55
+ }
56
+ };
57
+ const startedAtOf = () => {
58
+ try {
59
+ if (typeof ctx.getStartedAt === "function") return ctx.getStartedAt();
60
+ const header = typeof ctx.getHeaderInfo === "function" ? ctx.getHeaderInfo() : null;
61
+ return header && header.startedAt || "";
62
+ } catch (e) {
63
+ void e;
64
+ return "";
65
+ }
66
+ };
67
+ async function handleLogExport(args) {
68
+ const headerInfo = typeof ctx.getHeaderInfo === "function" ? ctx.getHeaderInfo() : null;
69
+ const now = /* @__PURE__ */ new Date();
70
+ const fallbackFileName = resolveLogFileName(config, now, pidOf(), startedAtOf());
71
+ const want = args && args.date ? String(args.date) : formatDailyFileName(now).replace(/\.log$/, "");
72
+ void (args && args.format);
73
+ try {
74
+ const dir = typeof ctx.getCacheDir === "function" ? await ctx.getCacheDir() : null;
75
+ if (!dir) {
76
+ try {
77
+ ctx.log("warn", "host.call.fail", { method: config.prefix + ".logExport", kind: "export", errorHash: hash8("no-dir") });
78
+ } catch (eL) {
79
+ void eL;
80
+ }
81
+ }
82
+ const baseDir = dir || headerInfo && headerInfo.dir || ctx.DEFAULT_CWD || "";
83
+ const logDir = baseDir ? await ctx.joinLogPath(baseDir, config.logDirName) : "";
84
+ let fileName = fallbackFileName;
85
+ if (config.fileNamePolicy === "four-segment" && /^\d{4}-\d{2}-\d{2}$/.test(want) && logDir) {
86
+ const candidates = await listFileNames(ctx, logDir);
87
+ fileName = matchExportFileName(config, candidates, want, fallbackFileName);
88
+ } else if (config.fileNamePolicy !== "four-segment") {
89
+ fileName = /^\d{4}-\d{2}-\d{2}$/.test(want) ? want + ".log" : fallbackFileName;
90
+ }
91
+ let text = "";
92
+ try {
93
+ const target = await ctx.resolveTarget(await ctx.joinLogPath(logDir, fileName));
94
+ text = await ctx.readTarget(target);
95
+ } catch (e) {
96
+ void e;
97
+ text = "";
98
+ }
99
+ let osName = "";
100
+ try {
101
+ const platform = typeof ctx.getPlatform === "function" ? await ctx.getPlatform() : null;
102
+ const g = globalThis;
103
+ osName = platform && platform.os || (typeof g.process !== "undefined" ? String(g.process.platform) : "") || "";
104
+ } catch (e2) {
105
+ void e2;
106
+ }
107
+ let cwdNow = ctx.DEFAULT_CWD || "";
108
+ try {
109
+ const g = globalThis;
110
+ cwdNow = g.process && g.process.cwd ? g.process.cwd() : ctx.DEFAULT_CWD || "";
111
+ } catch (e3) {
112
+ void e3;
113
+ }
114
+ const summary = {
115
+ pluginVersion: "unknown",
116
+ os: osName,
117
+ cwd: cwdNow,
118
+ logSwitch: ctx.getSwitchState(),
119
+ header: headerInfo
120
+ };
121
+ let dirOut = logDir;
122
+ let pathOut = "";
123
+ try {
124
+ dirOut = targetToPath(await ctx.resolveTarget(logDir), logDir);
125
+ pathOut = targetToPath(await ctx.resolveTarget(await ctx.joinLogPath(logDir, fileName)), ctx.joinPath(logDir, fileName));
126
+ } catch (e4) {
127
+ void e4;
128
+ try {
129
+ pathOut = ctx.joinPath(logDir, fileName);
130
+ } catch (e5) {
131
+ void e5;
132
+ pathOut = "";
133
+ }
134
+ }
135
+ if (!dirOut && !pathOut && baseDir) {
136
+ try {
137
+ dirOut = ctx.joinPath(baseDir, config.logDirName);
138
+ pathOut = ctx.joinPath(dirOut, fileName);
139
+ } catch (e6) {
140
+ void e6;
141
+ }
142
+ }
143
+ return {
144
+ ok: true,
145
+ fileName,
146
+ bytes: String(text || "").length,
147
+ fallback: true,
148
+ text: String(text || ""),
149
+ summary,
150
+ dir: dirOut,
151
+ path: pathOut
152
+ };
153
+ } catch (e) {
154
+ try {
155
+ ctx.log("warn", "host.call.fail", {
156
+ method: config.prefix + ".logExport",
157
+ kind: "export",
158
+ errorHash: hash8(String(e && e.message || e))
159
+ });
160
+ } catch (eL) {
161
+ void eL;
162
+ }
163
+ return { ok: false, fileName: fallbackFileName, bytes: 0, fallback: true };
164
+ }
165
+ }
166
+ async function handleLogClear(args) {
167
+ const want = args && args.date ? String(args.date) : "";
168
+ try {
169
+ const dir = typeof ctx.getCacheDir === "function" ? await ctx.getCacheDir() : null;
170
+ if (!dir) return { ok: true, removed: 0 };
171
+ const logDir = await ctx.joinLogPath(dir, config.logDirName);
172
+ if (want === "all") {
173
+ let removedAll = 0;
174
+ const names = await listFileNames(ctx, logDir);
175
+ for (let i = 0; i < names.length; i++) {
176
+ if (!filePattern.test(names[i])) continue;
177
+ if (await deleteOneFile(logDir, names[i])) removedAll += 1;
178
+ }
179
+ return { ok: true, removed: removedAll };
180
+ }
181
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(want)) {
182
+ try {
183
+ ctx.log("warn", "host.call.fail", { method: config.prefix + ".logClear", kind: "clear", errorHash: hash8("bad-date") });
184
+ } catch (eL) {
185
+ void eL;
186
+ }
187
+ return { ok: false, removed: 0 };
188
+ }
189
+ if (config.fileNamePolicy === "four-segment") {
190
+ const names = await listFileNames(ctx, logDir);
191
+ const hits = names.filter((name) => filePattern.test(name) && name.indexOf(want + ".") === 0).sort();
192
+ let removed = 0;
193
+ for (const name of hits) {
194
+ if (await deleteOneFile(logDir, name)) removed += 1;
195
+ }
196
+ return { ok: true, removed };
197
+ }
198
+ const done = await deleteOneFile(logDir, want + ".log");
199
+ return { ok: true, removed: done ? 1 : 0 };
200
+ } catch (e) {
201
+ try {
202
+ ctx.log("warn", "host.call.fail", {
203
+ method: config.prefix + ".logClear",
204
+ kind: "clear",
205
+ errorHash: hash8(String(e && e.message || e))
206
+ });
207
+ } catch (eL) {
208
+ void eL;
209
+ }
210
+ return { ok: false, removed: 0 };
211
+ }
212
+ }
213
+ async function deleteOneFile(logDir, name) {
214
+ const { fs, getPlatform } = ctx;
215
+ try {
216
+ const target = await ctx.resolveTarget(await ctx.joinLogPath(logDir, name));
217
+ try {
218
+ if (fs && typeof fs.unlink === "function") {
219
+ await fs.unlink(target);
220
+ return true;
221
+ }
222
+ } catch (e) {
223
+ void e;
224
+ }
225
+ try {
226
+ const platform = typeof getPlatform === "function" ? await getPlatform() : null;
227
+ if (platform && platform.fs && typeof platform.fs.unlink === "function") {
228
+ await platform.fs.unlink(target);
229
+ return true;
230
+ }
231
+ } catch (e2) {
232
+ void e2;
233
+ }
234
+ try {
235
+ await ctx.writeTarget(target, "");
236
+ return true;
237
+ } catch (e3) {
238
+ void e3;
239
+ return false;
240
+ }
241
+ } catch (e) {
242
+ void e;
243
+ return false;
244
+ }
245
+ }
246
+ async function handleLogGetSwitch() {
247
+ const state = await ctx.loadSwitch();
248
+ return { ok: true, enabled: state.enabled, sampleRate: state.sampleRate };
249
+ }
250
+ async function handleLogSetSwitch(args) {
251
+ const enabled = !!(args && args.enabled);
252
+ const fallbackRate = typeof ctx.getSwitchState === "function" ? ctx.getSwitchState().sampleRate : void 0;
253
+ const sampleRate = args && typeof args.sampleRate === "number" ? args.sampleRate : fallbackRate;
254
+ const state = await ctx.setSwitch(enabled, sampleRate);
255
+ return { ok: true, enabled: state.enabled };
256
+ }
257
+ return {
258
+ handleLogExport,
259
+ handleLogClear,
260
+ handleLogGetSwitch,
261
+ handleLogSetSwitch
262
+ };
263
+ }
264
+ export {
265
+ createLogPhones
266
+ };