[Init] Initial commit - NetMesh terminal manager
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
This commit is contained in:
138
electron/bridges/terminalPerformanceDiagnostics.cjs
Normal file
138
electron/bridges/terminalPerformanceDiagnostics.cjs
Normal file
@@ -0,0 +1,138 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const DEBUG_ENV_KEYS = [
|
||||
"NETCATTY_TERMINAL_PERF_DEBUG",
|
||||
"NETCATTY_TERMINAL_DEBUG",
|
||||
];
|
||||
|
||||
let nextPerfId = 0;
|
||||
let perfLogDir = null;
|
||||
let configuredUserDataPath = null;
|
||||
|
||||
function isTerminalPerformanceDebugEnabled() {
|
||||
return DEBUG_ENV_KEYS.some((key) => process.env[key] === "1");
|
||||
}
|
||||
|
||||
function countLineFeeds(data) {
|
||||
let count = 0;
|
||||
for (let index = 0; index < data.length; index += 1) {
|
||||
if (data[index] === "\n") count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
function safeJson(value) {
|
||||
return JSON.stringify(value, (_key, nested) => {
|
||||
if (typeof nested === "bigint") return nested.toString();
|
||||
if (typeof nested === "function") return "[function]";
|
||||
return nested;
|
||||
});
|
||||
}
|
||||
|
||||
function getUserDataPathFromArgv() {
|
||||
for (let index = 0; index < process.argv.length; index += 1) {
|
||||
const arg = process.argv[index];
|
||||
if (arg.startsWith("--user-data-dir=")) {
|
||||
return arg.slice("--user-data-dir=".length) || null;
|
||||
}
|
||||
if (arg === "--user-data-dir") {
|
||||
return process.argv[index + 1] || null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getPerfLogDir() {
|
||||
if (perfLogDir) return perfLogDir;
|
||||
try {
|
||||
const explicitLogDir = process.env.NETCATTY_TERMINAL_PERF_LOG_DIR;
|
||||
if (explicitLogDir) {
|
||||
perfLogDir = explicitLogDir;
|
||||
fs.mkdirSync(perfLogDir, { recursive: true });
|
||||
return perfLogDir;
|
||||
}
|
||||
|
||||
let userDataPath = configuredUserDataPath || getUserDataPathFromArgv();
|
||||
if (!userDataPath) {
|
||||
const { app } = require("electron");
|
||||
userDataPath = app?.getPath?.("userData") || null;
|
||||
}
|
||||
if (!userDataPath) return null;
|
||||
perfLogDir = path.join(userDataPath, "terminal-perf");
|
||||
fs.mkdirSync(perfLogDir, { recursive: true });
|
||||
return perfLogDir;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function todayFileName() {
|
||||
const d = new Date();
|
||||
const ymd = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
||||
return `terminal-perf-${ymd}.log`;
|
||||
}
|
||||
|
||||
function configureTerminalPerformanceDiagnostics(options = {}) {
|
||||
if (typeof options.userDataPath === "string" && options.userDataPath.trim()) {
|
||||
configuredUserDataPath = options.userDataPath;
|
||||
perfLogDir = null;
|
||||
}
|
||||
}
|
||||
|
||||
function appendTerminalPerfLogLine(message) {
|
||||
try {
|
||||
const dir = getPerfLogDir();
|
||||
if (!dir) return;
|
||||
fs.appendFileSync(path.join(dir, todayFileName()), message + "\n", "utf-8");
|
||||
} catch {
|
||||
// Diagnostics must never affect terminal output.
|
||||
}
|
||||
}
|
||||
|
||||
function createTerminalOutputPerfMeta(sessionId, data) {
|
||||
if (!isTerminalPerformanceDebugEnabled() || !data) return undefined;
|
||||
nextPerfId += 1;
|
||||
return {
|
||||
id: `termout-${Date.now().toString(36)}-${nextPerfId.toString(36)}`,
|
||||
emittedAt: Date.now(),
|
||||
sessionId,
|
||||
chars: typeof data === "string" ? data.length : 0,
|
||||
lineFeeds: typeof data === "string" ? countLineFeeds(data) : 0,
|
||||
};
|
||||
}
|
||||
|
||||
function attachTerminalOutputPerfMeta(meta, terminalPerf) {
|
||||
if (!terminalPerf) return meta;
|
||||
return {
|
||||
...(meta || {}),
|
||||
terminalPerf,
|
||||
};
|
||||
}
|
||||
|
||||
function logTerminalOutputPerf(event, details = {}) {
|
||||
if (!isTerminalPerformanceDebugEnabled()) return;
|
||||
const payload = {
|
||||
event,
|
||||
at: Date.now(),
|
||||
...details,
|
||||
};
|
||||
try {
|
||||
const message = `[Netcatty Terminal Perf] ${safeJson(payload)}`;
|
||||
console.info(message);
|
||||
appendTerminalPerfLogLine(message);
|
||||
} catch {
|
||||
// Diagnostics must never affect terminal output.
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
appendTerminalPerfLogLine,
|
||||
attachTerminalOutputPerfMeta,
|
||||
configureTerminalPerformanceDiagnostics,
|
||||
createTerminalOutputPerfMeta,
|
||||
isTerminalPerformanceDebugEnabled,
|
||||
logTerminalOutputPerf,
|
||||
};
|
||||
Reference in New Issue
Block a user