repoburg 1.3.324 → 1.3.326
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/agent-pane/backend/dist/index.js +18 -0
- package/agent-pane/backend/dist/server.js +18 -0
- package/agent-pane/frontend/dist/assets/{TerminalView-DQaRNqT-.js → TerminalView-DhSqcUfd.js} +1 -1
- package/agent-pane/frontend/dist/assets/{index-kBNaARow.js → index-7TAUiBlb.js} +97 -97
- package/agent-pane/frontend/dist/index.html +1 -1
- package/daemon/dist/index.js +10 -2
- package/daemon/dist/telemetry/clientRoute.js +44 -0
- package/daemon/dist/telemetry/index.js +182 -0
- package/daemon/dist/telemetry/inflight.js +69 -0
- package/daemon/dist/telemetry/logCapture.js +95 -0
- package/daemon/dist/telemetry/pm2Logs.js +160 -0
- package/daemon/dist/telemetry/redact.js +40 -0
- package/daemon/dist/telemetry/reporter.js +214 -0
- package/daemon/dist/telemetry/requestWatch.js +105 -0
- package/daemon/dist/telemetry/telemetry.spec.js +356 -0
- package/package.json +1 -1
- package/tabtape/extension/dist/build-info.json +3 -3
- package/tabtape/extension/dist/manifest.json +1 -1
- package/tabtape/ui/dist/build-info.json +3 -3
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;450;510;590;600&family=JetBrains+Mono:wght@400;500&display=swap"
|
|
11
11
|
rel="stylesheet"
|
|
12
12
|
/>
|
|
13
|
-
<script type="module" crossorigin src="/assets/index-
|
|
13
|
+
<script type="module" crossorigin src="/assets/index-7TAUiBlb.js"></script>
|
|
14
14
|
<link rel="stylesheet" crossorigin href="/assets/index-CUuZmtId.css">
|
|
15
15
|
</head>
|
|
16
16
|
<body>
|
package/daemon/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ const checkRemoteAuth_1 = require("./middleware/checkRemoteAuth");
|
|
|
20
20
|
const staticServing_1 = require("./staticServing");
|
|
21
21
|
const router_1 = require("./recorder/router");
|
|
22
22
|
const tabtape_1 = require("./tabtape");
|
|
23
|
+
const telemetry_1 = require("./telemetry");
|
|
23
24
|
// REPOBURG_DAEMON_PORT is for checks that start a second daemon next to the
|
|
24
25
|
// running one (runtime/smoke-test.sh); everything else expects 9998.
|
|
25
26
|
const PORT = Number(process.env.REPOBURG_DAEMON_PORT) || 9998;
|
|
@@ -46,6 +47,8 @@ function loadTabtape() {
|
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
49
|
async function main() {
|
|
50
|
+
// First, so errors of the start itself are reported (telemetry/index.ts).
|
|
51
|
+
const telemetry = (0, telemetry_1.startTelemetry)();
|
|
49
52
|
await serviceManager_1.serviceManager.connect();
|
|
50
53
|
// Embedded agent-pane backend — served on THIS port (no second listener).
|
|
51
54
|
// Failure is non-fatal: the daemon keeps managing services, /api/* -> 503.
|
|
@@ -81,7 +84,9 @@ async function main() {
|
|
|
81
84
|
// every route. Local requests pass. See middleware/checkRemoteAuth.ts.
|
|
82
85
|
app.use(checkRemoteAuth_1.checkRemoteAuth);
|
|
83
86
|
// Embedded agent-pane API. MUST precede express.json() — Fastify parses its
|
|
84
|
-
// own request bodies; the stream can only be consumed once.
|
|
87
|
+
// own request bodies; the stream can only be consumed once. Telemetry
|
|
88
|
+
// watches it for requests that hang or fail.
|
|
89
|
+
app.use('/api', telemetry.paneRequests);
|
|
85
90
|
app.all('/api/*', (0, agentPane_1.createPaneDelegate)(pane));
|
|
86
91
|
// Same-origin VS Code web proxy (/vscode/* in the embedded app). Route-style
|
|
87
92
|
// mount (not app.use) keeps req.url intact — serve-web serves under the
|
|
@@ -92,8 +97,10 @@ async function main() {
|
|
|
92
97
|
// Tunnel lifecycle API — local-only; the CF ingress edge blocks /remote/*
|
|
93
98
|
// for tunnel traffic (see account-server CloudflareService.setIngress).
|
|
94
99
|
app.use('/remote', (0, routes_1.createRemoteRouter)());
|
|
100
|
+
// The pane frontend's errors (agent-pane/frontend/src/utils/clientErrors.ts).
|
|
101
|
+
app.use('/telemetry', telemetry.clientRouter);
|
|
95
102
|
// API Routers
|
|
96
|
-
app.use('/services', services_1.default);
|
|
103
|
+
app.use('/services', telemetry.daemonRequests, services_1.default);
|
|
97
104
|
// Running agents and "Quit and Stop Agents" (the app's Quit row).
|
|
98
105
|
app.use('/agents', (0, agents_1.createAgentsRouter)(pane));
|
|
99
106
|
app.use('/auth', (0, auth_1.default)(wss));
|
|
@@ -181,6 +188,7 @@ async function main() {
|
|
|
181
188
|
console.error('[tabtape] shutdown failed:', err);
|
|
182
189
|
}
|
|
183
190
|
}
|
|
191
|
+
await telemetry.stop();
|
|
184
192
|
process.exit(0);
|
|
185
193
|
};
|
|
186
194
|
process.on('SIGINT', shutdown);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createClientErrorRouter = createClientErrorRouter;
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const PER_MINUTE = 30;
|
|
6
|
+
/**
|
|
7
|
+
* POST /telemetry/client: errors of the pane frontend (window errors,
|
|
8
|
+
* unhandled rejections), sent by agent-pane/frontend/src/utils/clientErrors.ts.
|
|
9
|
+
* Needs express.json() before it. Answers 204 whatever happens.
|
|
10
|
+
*/
|
|
11
|
+
function createClientErrorRouter(report) {
|
|
12
|
+
const router = (0, express_1.Router)();
|
|
13
|
+
let windowStart = Date.now();
|
|
14
|
+
let count = 0;
|
|
15
|
+
router.post('/client', (req, res) => {
|
|
16
|
+
res.status(204).end();
|
|
17
|
+
if (Date.now() - windowStart > 60000) {
|
|
18
|
+
windowStart = Date.now();
|
|
19
|
+
count = 0;
|
|
20
|
+
}
|
|
21
|
+
if (++count > PER_MINUTE)
|
|
22
|
+
return;
|
|
23
|
+
const body = (req.body ?? {});
|
|
24
|
+
if (typeof body.message !== 'string' || !body.message)
|
|
25
|
+
return;
|
|
26
|
+
const text = (value, max) => typeof value === 'string' && value ? value.slice(0, max) : undefined;
|
|
27
|
+
const stack = text(body.stack, 8000);
|
|
28
|
+
const page = text(body.page, 300);
|
|
29
|
+
report({
|
|
30
|
+
source: 'frontend',
|
|
31
|
+
kind: 'client_error',
|
|
32
|
+
level: 'error',
|
|
33
|
+
message: body.message.slice(0, 2000),
|
|
34
|
+
...(stack ? { stack } : {}),
|
|
35
|
+
// The frontend's own recent actions, not the daemon's output.
|
|
36
|
+
context: text(body.context, 8000) ?? '',
|
|
37
|
+
extra: {
|
|
38
|
+
...(page ? { page } : {}),
|
|
39
|
+
...(typeof body.type === 'string' ? { type: body.type.slice(0, 40) } : {}),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
return router;
|
|
44
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.telemetryEnabled = telemetryEnabled;
|
|
7
|
+
exports.channelOf = channelOf;
|
|
8
|
+
exports.startTelemetry = startTelemetry;
|
|
9
|
+
/**
|
|
10
|
+
* Field telemetry: the daemon sends its errors, the backends' error-log
|
|
11
|
+
* lines, slow or failed pane requests and the pane frontend's errors to the
|
|
12
|
+
* account server (POST <account API>/telemetry/events), without the user
|
|
13
|
+
* reporting anything. The fix-field-errors skill reads them from there.
|
|
14
|
+
*
|
|
15
|
+
* What leaves the machine is redacted first (redact.ts): the home folder
|
|
16
|
+
* becomes ~, emails, tokens and keys go. No prompts, files or model output
|
|
17
|
+
* are sent; only error messages, stacks and the log lines before them.
|
|
18
|
+
*
|
|
19
|
+
* Off: REPOBURG_TELEMETRY=0, or "telemetry": false in ~/.repoburg/config.json.
|
|
20
|
+
* Never on under jest. A daemon that runs from a git checkout reports on the
|
|
21
|
+
* `dev` channel, which the skill leaves out by default.
|
|
22
|
+
*/
|
|
23
|
+
const child_process_1 = require("child_process");
|
|
24
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
25
|
+
const fs_1 = __importDefault(require("fs"));
|
|
26
|
+
const os_1 = __importDefault(require("os"));
|
|
27
|
+
const path_1 = __importDefault(require("path"));
|
|
28
|
+
const accountServer_1 = require("../auth/accountServer");
|
|
29
|
+
const clientRoute_1 = require("./clientRoute");
|
|
30
|
+
const inflight_1 = require("./inflight");
|
|
31
|
+
const logCapture_1 = require("./logCapture");
|
|
32
|
+
const pm2Logs_1 = require("./pm2Logs");
|
|
33
|
+
const requestWatch_1 = require("./requestWatch");
|
|
34
|
+
const reporter_1 = require("./reporter");
|
|
35
|
+
const FLUSH_MS = 60000;
|
|
36
|
+
const PM2_SCAN_MS = 30000;
|
|
37
|
+
const HEARTBEAT_MS = 24 * 60 * 60000;
|
|
38
|
+
const passThrough = (_req, _res, next) => next();
|
|
39
|
+
/** Whether telemetry runs: on unless turned off or under tests. */
|
|
40
|
+
function telemetryEnabled(env = process.env, home = os_1.default.homedir()) {
|
|
41
|
+
if (env.REPOBURG_TELEMETRY === '0' || env.REPOBURG_TELEMETRY === 'false')
|
|
42
|
+
return false;
|
|
43
|
+
if (env.JEST_WORKER_ID !== undefined || env.NODE_ENV === 'test')
|
|
44
|
+
return false;
|
|
45
|
+
try {
|
|
46
|
+
const config = JSON.parse(fs_1.default.readFileSync(path_1.default.join(home, '.repoburg', 'config.json'), 'utf8'));
|
|
47
|
+
if (config.telemetry === false)
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// No config yet: on.
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
/** The root of the installed app (package.json with the version). */
|
|
56
|
+
function appRoot() {
|
|
57
|
+
// dist/telemetry (built) and src/telemetry (ts-node) are both two levels down.
|
|
58
|
+
return path_1.default.resolve(__dirname, '..', '..', '..');
|
|
59
|
+
}
|
|
60
|
+
function readJson(file) {
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(fs_1.default.readFileSync(file, 'utf8'));
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** `release` for an installed runtime or npm package, `dev` for a checkout. */
|
|
69
|
+
function channelOf(root, env = process.env) {
|
|
70
|
+
if (env.REPOBURG_TELEMETRY_CHANNEL === 'release' || env.REPOBURG_TELEMETRY_CHANNEL === 'dev') {
|
|
71
|
+
return env.REPOBURG_TELEMETRY_CHANNEL;
|
|
72
|
+
}
|
|
73
|
+
return fs_1.default.existsSync(path_1.default.join(root, '.git')) ? 'dev' : 'release';
|
|
74
|
+
}
|
|
75
|
+
function osVersion() {
|
|
76
|
+
if (process.platform === 'darwin') {
|
|
77
|
+
try {
|
|
78
|
+
return (0, child_process_1.execFileSync)('/usr/bin/sw_vers', ['-productVersion'], { encoding: 'utf8', timeout: 2000 }).trim();
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return os_1.default.release();
|
|
85
|
+
}
|
|
86
|
+
function installId(dir) {
|
|
87
|
+
const file = path_1.default.join(dir, 'install-id');
|
|
88
|
+
try {
|
|
89
|
+
const id = fs_1.default.readFileSync(file, 'utf8').trim();
|
|
90
|
+
if (/^[0-9a-f-]{36}$/i.test(id))
|
|
91
|
+
return id;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// First run.
|
|
95
|
+
}
|
|
96
|
+
const id = crypto_1.default.randomUUID();
|
|
97
|
+
try {
|
|
98
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
99
|
+
fs_1.default.writeFileSync(file, id);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// A new id on every start: still works, counts machines too often.
|
|
103
|
+
}
|
|
104
|
+
return id;
|
|
105
|
+
}
|
|
106
|
+
/** Starts telemetry for this daemon process; a no-op object when it is off. */
|
|
107
|
+
function startTelemetry(env = process.env) {
|
|
108
|
+
const home = os_1.default.homedir();
|
|
109
|
+
if (!telemetryEnabled(env, home)) {
|
|
110
|
+
return {
|
|
111
|
+
enabled: false,
|
|
112
|
+
report: () => undefined,
|
|
113
|
+
paneRequests: passThrough,
|
|
114
|
+
daemonRequests: passThrough,
|
|
115
|
+
clientRouter: (0, clientRoute_1.createClientErrorRouter)(() => undefined),
|
|
116
|
+
stop: async () => undefined,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const dir = path_1.default.join(home, '.repoburg', 'telemetry');
|
|
120
|
+
const root = appRoot();
|
|
121
|
+
const runtime = readJson(path_1.default.join(root, '..', 'runtime.json'));
|
|
122
|
+
const install = {
|
|
123
|
+
id: installId(dir),
|
|
124
|
+
channel: channelOf(root, env),
|
|
125
|
+
version: readJson(path_1.default.join(root, 'package.json'))?.version ?? null,
|
|
126
|
+
runtimeVersion: runtime?.version ?? null,
|
|
127
|
+
platform: process.platform,
|
|
128
|
+
arch: process.arch,
|
|
129
|
+
osVersion: osVersion(),
|
|
130
|
+
nodeVersion: process.versions.node,
|
|
131
|
+
};
|
|
132
|
+
const buffer = new logCapture_1.LineBuffer();
|
|
133
|
+
const reporter = new reporter_1.TelemetryReporter({
|
|
134
|
+
endpoint: env.REPOBURG_TELEMETRY_URL || `${(0, accountServer_1.accountServerUrl)()}/telemetry/events`,
|
|
135
|
+
install,
|
|
136
|
+
dir,
|
|
137
|
+
tokenPath: path_1.default.join(home, '.repoburg', 'token.jwt'),
|
|
138
|
+
recentLines: () => buffer.recent(),
|
|
139
|
+
home,
|
|
140
|
+
});
|
|
141
|
+
const report = (event) => reporter.capture(event);
|
|
142
|
+
// After the reporter took the original fetch: its own sends are not tracked.
|
|
143
|
+
const fetches = (0, inflight_1.trackFetches)((url) => url.includes('/telemetry/events'));
|
|
144
|
+
const undoConsole = (0, logCapture_1.captureConsole)(buffer, report);
|
|
145
|
+
const undoCrashes = (0, logCapture_1.captureCrashes)((event) => reporter.captureSync(event));
|
|
146
|
+
const pm2Home = env.PM2_HOME || path_1.default.join(home, '.pm2');
|
|
147
|
+
const pm2Logs = new pm2Logs_1.Pm2LogWatcher(path_1.default.join(pm2Home, 'logs'), path_1.default.join(dir, 'pm2-offsets.json'), report);
|
|
148
|
+
const timers = [
|
|
149
|
+
setInterval(() => void reporter.flush(), FLUSH_MS),
|
|
150
|
+
setInterval(() => pm2Logs.scan(), PM2_SCAN_MS),
|
|
151
|
+
setInterval(() => void reporter.heartbeat(), HEARTBEAT_MS),
|
|
152
|
+
];
|
|
153
|
+
for (const timer of timers)
|
|
154
|
+
timer.unref();
|
|
155
|
+
// First scan sets the offsets; a first send shortly after start brings
|
|
156
|
+
// what a crash spooled and tells the server which version runs here.
|
|
157
|
+
pm2Logs.scan();
|
|
158
|
+
const first = setTimeout(() => {
|
|
159
|
+
void reporter.heartbeat();
|
|
160
|
+
void reporter.flush();
|
|
161
|
+
}, 15000);
|
|
162
|
+
first.unref();
|
|
163
|
+
console.log(`[telemetry] errors are sent to Repoburg (install ${install.id.slice(0, 8)}, ${install.channel}). ` +
|
|
164
|
+
'Turn off: REPOBURG_TELEMETRY=0 or "telemetry": false in ~/.repoburg/config.json. What is sent: https://repoburg.com/privacy');
|
|
165
|
+
return {
|
|
166
|
+
enabled: true,
|
|
167
|
+
report,
|
|
168
|
+
paneRequests: (0, requestWatch_1.requestWatch)(report, { source: 'pane', pending: fetches.pending }),
|
|
169
|
+
daemonRequests: (0, requestWatch_1.requestWatch)(report, { source: 'daemon', pending: fetches.pending }),
|
|
170
|
+
clientRouter: (0, clientRoute_1.createClientErrorRouter)(report),
|
|
171
|
+
stop: async () => {
|
|
172
|
+
for (const timer of timers)
|
|
173
|
+
clearInterval(timer);
|
|
174
|
+
clearTimeout(first);
|
|
175
|
+
pm2Logs.scan();
|
|
176
|
+
await reporter.close();
|
|
177
|
+
undoConsole();
|
|
178
|
+
undoCrashes();
|
|
179
|
+
fetches.undo();
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.trackFetches = trackFetches;
|
|
4
|
+
const requestWatch_1 = require("./requestWatch");
|
|
5
|
+
/**
|
|
6
|
+
* Tracks the outgoing fetch() calls of this process (the embedded pane calls
|
|
7
|
+
* the workspace backends and the daemon with it). A slow pane request is
|
|
8
|
+
* reported with the calls still waiting at that moment: they show where it
|
|
9
|
+
* hangs. Returns the list getter and the undo.
|
|
10
|
+
*/
|
|
11
|
+
function trackFetches(skip = () => false) {
|
|
12
|
+
const original = globalThis.fetch;
|
|
13
|
+
const active = new Map();
|
|
14
|
+
let next = 0;
|
|
15
|
+
const patched = function (input, init) {
|
|
16
|
+
let url = '';
|
|
17
|
+
let method = init?.method ?? 'GET';
|
|
18
|
+
try {
|
|
19
|
+
if (typeof input === 'string')
|
|
20
|
+
url = input;
|
|
21
|
+
else if (input instanceof URL)
|
|
22
|
+
url = input.href;
|
|
23
|
+
else {
|
|
24
|
+
url = input.url;
|
|
25
|
+
method = init?.method ?? input.method;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
url = '';
|
|
30
|
+
}
|
|
31
|
+
if (!url || skip(url))
|
|
32
|
+
return original(input, init);
|
|
33
|
+
let target = url;
|
|
34
|
+
try {
|
|
35
|
+
const parsed = new URL(url);
|
|
36
|
+
target = `${parsed.origin}${(0, requestWatch_1.routePattern)(parsed.pathname)}`;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Keep it as given.
|
|
40
|
+
}
|
|
41
|
+
const id = next++;
|
|
42
|
+
active.set(id, { method: method.toUpperCase(), target, started: Date.now() });
|
|
43
|
+
const done = () => active.delete(id);
|
|
44
|
+
try {
|
|
45
|
+
const result = original(input, init);
|
|
46
|
+
result.then(done, done);
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
done();
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
globalThis.fetch = patched;
|
|
55
|
+
return {
|
|
56
|
+
pending: () => [...active.values()]
|
|
57
|
+
.map((call) => ({
|
|
58
|
+
method: call.method,
|
|
59
|
+
target: call.target,
|
|
60
|
+
seconds: Math.round((Date.now() - call.started) / 1000),
|
|
61
|
+
}))
|
|
62
|
+
.sort((a, b) => b.seconds - a.seconds)
|
|
63
|
+
.slice(0, 10),
|
|
64
|
+
undo: () => {
|
|
65
|
+
if (globalThis.fetch === patched)
|
|
66
|
+
globalThis.fetch = original;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LineBuffer = void 0;
|
|
7
|
+
exports.captureConsole = captureConsole;
|
|
8
|
+
exports.captureCrashes = captureCrashes;
|
|
9
|
+
const util_1 = __importDefault(require("util"));
|
|
10
|
+
/** The last lines the process printed, the context of an event. */
|
|
11
|
+
class LineBuffer {
|
|
12
|
+
constructor(size = 300) {
|
|
13
|
+
this.size = size;
|
|
14
|
+
this.lines = [];
|
|
15
|
+
}
|
|
16
|
+
push(line) {
|
|
17
|
+
for (const part of line.split('\n')) {
|
|
18
|
+
this.lines.push(part.length > 1000 ? `${part.slice(0, 1000)}…` : part);
|
|
19
|
+
}
|
|
20
|
+
if (this.lines.length > this.size)
|
|
21
|
+
this.lines.splice(0, this.lines.length - this.size);
|
|
22
|
+
}
|
|
23
|
+
/** The last `count` lines. */
|
|
24
|
+
recent(count = 60) {
|
|
25
|
+
return this.lines.slice(-count).join('\n');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.LineBuffer = LineBuffer;
|
|
29
|
+
const LEVELS = ['log', 'info', 'warn', 'error', 'debug'];
|
|
30
|
+
/**
|
|
31
|
+
* Wraps console.*: every line goes to the buffer, console.error also becomes
|
|
32
|
+
* an event (source `daemon`: the embedded agent pane prints through the same
|
|
33
|
+
* console). The original console still prints. Returns the undo.
|
|
34
|
+
*/
|
|
35
|
+
function captureConsole(buffer, onError, now = () => new Date()) {
|
|
36
|
+
const originals = new Map();
|
|
37
|
+
let inside = false;
|
|
38
|
+
for (const level of LEVELS) {
|
|
39
|
+
const original = console[level].bind(console);
|
|
40
|
+
originals.set(level, original);
|
|
41
|
+
console[level] = (...args) => {
|
|
42
|
+
original(...args);
|
|
43
|
+
// A reporter that logs must not report its own lines.
|
|
44
|
+
if (inside)
|
|
45
|
+
return;
|
|
46
|
+
inside = true;
|
|
47
|
+
try {
|
|
48
|
+
const text = util_1.default.format(...args);
|
|
49
|
+
buffer.push(`${now().toISOString()} ${level.toUpperCase()} ${text}`);
|
|
50
|
+
if (level === 'error') {
|
|
51
|
+
const error = args.find((arg) => arg instanceof Error);
|
|
52
|
+
onError({
|
|
53
|
+
source: 'daemon',
|
|
54
|
+
kind: 'error',
|
|
55
|
+
level: 'error',
|
|
56
|
+
// The text before the error object (its prefix) plus the error.
|
|
57
|
+
message: error
|
|
58
|
+
? `${util_1.default.format(...args.filter((arg) => arg !== error))} ${error.message}`.trim()
|
|
59
|
+
: text,
|
|
60
|
+
...(error?.stack ? { stack: error.stack } : {}),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Logging must keep working.
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
inside = false;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return () => {
|
|
73
|
+
for (const [level, original] of originals)
|
|
74
|
+
console[level] = original;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Uncaught exceptions and unhandled rejections that end the process. A
|
|
79
|
+
* monitor listener does not change Node's behaviour (the process still
|
|
80
|
+
* crashes); the event is written to disk at once and sent at the next start.
|
|
81
|
+
*/
|
|
82
|
+
function captureCrashes(onCrash) {
|
|
83
|
+
const listener = (error, origin) => {
|
|
84
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
85
|
+
onCrash({
|
|
86
|
+
source: 'daemon',
|
|
87
|
+
kind: origin === 'unhandledRejection' ? 'rejection' : 'uncaught',
|
|
88
|
+
level: 'fatal',
|
|
89
|
+
message: err.message || String(error),
|
|
90
|
+
...(err.stack ? { stack: err.stack } : {}),
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
process.on('uncaughtExceptionMonitor', listener);
|
|
94
|
+
return () => process.off('uncaughtExceptionMonitor', listener);
|
|
95
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Pm2LogWatcher = void 0;
|
|
7
|
+
exports.splitEntries = splitEntries;
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
/** pm2 prefixes each write with this (log_date_format in serviceManager.ts). */
|
|
11
|
+
const DATE_PREFIX = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?: [+-]\d{2}:?\d{2})?: ?/;
|
|
12
|
+
/** Lines of Node.js itself that say nothing about Repoburg. */
|
|
13
|
+
const NOISE = [
|
|
14
|
+
/ExperimentalWarning/,
|
|
15
|
+
/DeprecationWarning/,
|
|
16
|
+
/MODULE_TYPELESS_PACKAGE_JSON/,
|
|
17
|
+
/Reparsing as ES module/,
|
|
18
|
+
/To eliminate this warning/,
|
|
19
|
+
/node --trace-warnings/,
|
|
20
|
+
/--trace-deprecation/,
|
|
21
|
+
];
|
|
22
|
+
const MAX_READ_BYTES = 256 * 1024;
|
|
23
|
+
/**
|
|
24
|
+
* Splits new error-log text into entries. A line with pm2's date prefix
|
|
25
|
+
* starts an entry, unless it is a stack frame (`at …`); lines without the
|
|
26
|
+
* prefix continue the entry before them.
|
|
27
|
+
*/
|
|
28
|
+
function splitEntries(text) {
|
|
29
|
+
const entries = [];
|
|
30
|
+
for (const raw of text.split('\n')) {
|
|
31
|
+
if (!raw.trim())
|
|
32
|
+
continue;
|
|
33
|
+
const prefixed = DATE_PREFIX.test(raw);
|
|
34
|
+
const line = raw.replace(DATE_PREFIX, '');
|
|
35
|
+
const continues = !prefixed || /^\s+at\s/.test(line) || /^\s/.test(line);
|
|
36
|
+
if (continues && entries.length > 0)
|
|
37
|
+
entries[entries.length - 1].lines.push(line);
|
|
38
|
+
else
|
|
39
|
+
entries.push({ lines: [line] });
|
|
40
|
+
}
|
|
41
|
+
return entries
|
|
42
|
+
.map(({ lines }) => ({ message: lines[0].trim(), rest: lines.slice(1).join('\n') }))
|
|
43
|
+
.filter((entry) => !NOISE.some((pattern) => pattern.test(entry.message)));
|
|
44
|
+
}
|
|
45
|
+
/** The last `bytes` of a file as text ('' when it cannot be read). */
|
|
46
|
+
function readTail(file, bytes) {
|
|
47
|
+
try {
|
|
48
|
+
const size = fs_1.default.statSync(file).size;
|
|
49
|
+
const start = Math.max(0, size - bytes);
|
|
50
|
+
const fd = fs_1.default.openSync(file, 'r');
|
|
51
|
+
try {
|
|
52
|
+
const buffer = Buffer.alloc(size - start);
|
|
53
|
+
fs_1.default.readSync(fd, buffer, 0, buffer.length, start);
|
|
54
|
+
return buffer.toString('utf8');
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
fs_1.default.closeSync(fd);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Follows the backends' pm2 error logs (`<PM2_HOME>/logs/<service>-error.log`)
|
|
66
|
+
* and reports what they print. Offsets are kept in `offsetsPath`, so a daemon
|
|
67
|
+
* restart does not report old lines again; a log seen for the first time is
|
|
68
|
+
* followed from its end.
|
|
69
|
+
*/
|
|
70
|
+
class Pm2LogWatcher {
|
|
71
|
+
constructor(logsDir, offsetsPath, onEntry) {
|
|
72
|
+
this.logsDir = logsDir;
|
|
73
|
+
this.offsetsPath = offsetsPath;
|
|
74
|
+
this.onEntry = onEntry;
|
|
75
|
+
this.offsets = {};
|
|
76
|
+
try {
|
|
77
|
+
this.offsets = JSON.parse(fs_1.default.readFileSync(offsetsPath, 'utf8'));
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
this.offsets = {};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Reads what the error logs got since the last scan. */
|
|
84
|
+
scan() {
|
|
85
|
+
let files;
|
|
86
|
+
try {
|
|
87
|
+
files = fs_1.default.readdirSync(this.logsDir).filter((name) => name.endsWith('-error.log'));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
let changed = false;
|
|
93
|
+
for (const name of files) {
|
|
94
|
+
const file = path_1.default.join(this.logsDir, name);
|
|
95
|
+
let size;
|
|
96
|
+
try {
|
|
97
|
+
size = fs_1.default.statSync(file).size;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const known = this.offsets[name];
|
|
103
|
+
if (known === undefined || size < known) {
|
|
104
|
+
// New log: start at its end. Truncated (pm2 flush): start again from 0.
|
|
105
|
+
this.offsets[name] = known === undefined ? size : 0;
|
|
106
|
+
changed = true;
|
|
107
|
+
if (known === undefined)
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const from = this.offsets[name];
|
|
111
|
+
if (size === from)
|
|
112
|
+
continue;
|
|
113
|
+
const length = Math.min(size - from, MAX_READ_BYTES);
|
|
114
|
+
let text = '';
|
|
115
|
+
try {
|
|
116
|
+
const fd = fs_1.default.openSync(file, 'r');
|
|
117
|
+
try {
|
|
118
|
+
const buffer = Buffer.alloc(length);
|
|
119
|
+
fs_1.default.readSync(fd, buffer, 0, length, size - length);
|
|
120
|
+
text = buffer.toString('utf8');
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
fs_1.default.closeSync(fd);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
this.offsets[name] = size;
|
|
130
|
+
changed = true;
|
|
131
|
+
const service = name.replace(/-error\.log$/, '');
|
|
132
|
+
const context = readTail(path_1.default.join(this.logsDir, `${service}-out.log`), 4096)
|
|
133
|
+
.split('\n')
|
|
134
|
+
.slice(-30)
|
|
135
|
+
.map((line) => line.replace(DATE_PREFIX, ''))
|
|
136
|
+
.join('\n');
|
|
137
|
+
for (const entry of splitEntries(text)) {
|
|
138
|
+
this.onEntry({
|
|
139
|
+
source: 'backend',
|
|
140
|
+
kind: 'log',
|
|
141
|
+
level: 'error',
|
|
142
|
+
message: entry.message,
|
|
143
|
+
...(entry.rest ? { stack: entry.rest } : {}),
|
|
144
|
+
context,
|
|
145
|
+
extra: { service },
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (changed) {
|
|
150
|
+
try {
|
|
151
|
+
fs_1.default.mkdirSync(path_1.default.dirname(this.offsetsPath), { recursive: true });
|
|
152
|
+
fs_1.default.writeFileSync(this.offsetsPath, JSON.stringify(this.offsets));
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// Worst case: lines are reported again after a restart.
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.Pm2LogWatcher = Pm2LogWatcher;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.redact = redact;
|
|
7
|
+
exports.tail = tail;
|
|
8
|
+
exports.head = head;
|
|
9
|
+
const os_1 = __importDefault(require("os"));
|
|
10
|
+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
11
|
+
/**
|
|
12
|
+
* Removes what must not leave the machine before an event is sent: the home
|
|
13
|
+
* folder (it holds the user name), other users' folders, emails, tokens and
|
|
14
|
+
* keys, URL query strings. Paths under the home folder stay as `~/…`: they
|
|
15
|
+
* are what tells which workspace or file failed.
|
|
16
|
+
*/
|
|
17
|
+
function redact(text, home = os_1.default.homedir()) {
|
|
18
|
+
let out = text;
|
|
19
|
+
if (home && home !== '/')
|
|
20
|
+
out = out.replace(new RegExp(escapeRegExp(home), 'g'), '~');
|
|
21
|
+
return (out
|
|
22
|
+
.replace(/\/(Users|home)\/[^/\s'"`]+/g, '/$1/<user>')
|
|
23
|
+
.replace(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g, '<email>')
|
|
24
|
+
.replace(/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g, '<jwt>')
|
|
25
|
+
.replace(/\b(Bearer|Basic)\s+[^\s'"`,;]+/gi, '$1 <redacted>')
|
|
26
|
+
.replace(/((?:api[_-]?key|apikey|access[_-]?token|refresh[_-]?token|token|secret|password|passwd|authorization|cookie)["']?\s*[:=]\s*["']?)(?!<|Bearer\b|Basic\b)[^\s"'`&,;})]+/gi, '$1<redacted>')
|
|
27
|
+
.replace(/\b(?:sk-[A-Za-z0-9_-]{8,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,})/g, '<secret>')
|
|
28
|
+
.replace(/(https?:\/\/[^\s?#'"`]+)\?[^\s'"`)]*/g, '$1?<redacted>')
|
|
29
|
+
// Long opaque runs (keys of unknown shape, hashes). No `/` in the class,
|
|
30
|
+
// so long paths survive.
|
|
31
|
+
.replace(/[A-Za-z0-9+_-]{40,}={0,2}/g, '<blob>'));
|
|
32
|
+
}
|
|
33
|
+
/** At most `max` characters, cut at the start (the end is the newest). */
|
|
34
|
+
function tail(text, max) {
|
|
35
|
+
return text.length > max ? `…${text.slice(text.length - max)}` : text;
|
|
36
|
+
}
|
|
37
|
+
/** At most `max` characters, cut at the end. */
|
|
38
|
+
function head(text, max) {
|
|
39
|
+
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
40
|
+
}
|