deepdebug-local-agent 1.0.22 → 1.0.23
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/README.md +81 -0
- package/docs/legacy-connectors.md +268 -0
- package/package.json +4 -1
- package/scripts/sync-fixtures.mjs +246 -0
- package/src/adapters/as400/as400-parser.js +12 -0
- package/src/adapters/as400/index.js +134 -0
- package/src/adapters/cobol/cobol-parser.js +37 -0
- package/src/adapters/cobol/index.js +151 -0
- package/src/adapters/ibmmq/ibmmq-parser.js +23 -0
- package/src/adapters/ibmmq/index.js +137 -0
- package/src/adapters/index.js +79 -0
- package/src/adapters/oracle-forms/index.js +145 -0
- package/src/adapters/oracle-forms/oracle-forms-parser.js +11 -0
- package/src/adapters/sap/index.js +146 -0
- package/src/adapters/sap/sap-parser.js +41 -0
- package/src/server.js +46 -2
- package/test/adapters/as400/as400.test.js +121 -0
- package/test/adapters/cobol/cobol.test.js +217 -0
- package/test/adapters/ibmmq/ibmmq.test.js +122 -0
- package/test/adapters/oracle-forms/oracle-forms.test.js +180 -0
- package/test/adapters/registry.test.js +101 -0
- package/test/adapters/sap/sap.test.js +142 -0
- package/src/local-agent-source.tar.gz +0 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// src/adapters/as400/index.js
|
|
2
|
+
// AS/400 (IBM i) log adapter — ssh2 Client polls DSPLOG output.
|
|
3
|
+
// T-2-01 mitigation: pollInterval default 30000 ms (30 s — distinct from chokidar 5 s default per AD-3 + Constraints).
|
|
4
|
+
// T-2-02 mitigation: ssh2 keepaliveInterval=30 s + keepaliveCountMax=3; 'close' triggers reconnect with
|
|
5
|
+
// exponential backoff [1, 2, 4, 8, 16, 32, 60] s (capped at 60 s); backoff resets to base after a successful auth.
|
|
6
|
+
import { EventEmitter } from 'node:events';
|
|
7
|
+
import { Client as Ssh2Client } from 'ssh2';
|
|
8
|
+
import { extractMessageId, DSPLOG_COMMAND } from './as400-parser.js';
|
|
9
|
+
|
|
10
|
+
const ADAPTER = 'as400';
|
|
11
|
+
const DEFAULT_BACKOFF = [1000, 2000, 4000, 8000, 16000, 32000, 60000];
|
|
12
|
+
|
|
13
|
+
export class As400LogAdapter extends EventEmitter {
|
|
14
|
+
constructor(config = {}) {
|
|
15
|
+
super();
|
|
16
|
+
this.host = config.host || 'as400-prd-01';
|
|
17
|
+
this.port = config.port ?? 22;
|
|
18
|
+
this.username = config.username || 'TESTUSER01';
|
|
19
|
+
this.password = config.password;
|
|
20
|
+
this.privateKey = config.privateKey;
|
|
21
|
+
this.pollInterval = config.pollInterval ?? 30000;
|
|
22
|
+
this.backoffSchedule = config.backoffSchedule ?? DEFAULT_BACKOFF;
|
|
23
|
+
this._sshOpts = {
|
|
24
|
+
host: this.host,
|
|
25
|
+
port: this.port,
|
|
26
|
+
username: this.username,
|
|
27
|
+
...(this.password ? { password: this.password } : {}),
|
|
28
|
+
...(this.privateKey ? { privateKey: this.privateKey } : {}),
|
|
29
|
+
keepaliveInterval: 30000,
|
|
30
|
+
keepaliveCountMax: 3,
|
|
31
|
+
readyTimeout: 10000,
|
|
32
|
+
};
|
|
33
|
+
this._sshClientFactory = config.sshClientFactory || (() => new Ssh2Client());
|
|
34
|
+
this._client = null;
|
|
35
|
+
this._isRunning = false;
|
|
36
|
+
this._pollTimer = null;
|
|
37
|
+
this._backoffIdx = 0;
|
|
38
|
+
this._reconnectTimer = null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get isRunning() { return this._isRunning; }
|
|
42
|
+
|
|
43
|
+
async start() {
|
|
44
|
+
if (this._isRunning) return;
|
|
45
|
+
this._isRunning = true;
|
|
46
|
+
this._connect();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async stop() {
|
|
50
|
+
if (!this._isRunning) return;
|
|
51
|
+
this._isRunning = false;
|
|
52
|
+
if (this._reconnectTimer) { clearTimeout(this._reconnectTimer); this._reconnectTimer = null; }
|
|
53
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
54
|
+
if (this._client) {
|
|
55
|
+
try { this._client.end(); } catch { /* swallow */ }
|
|
56
|
+
this._client = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_connect() {
|
|
61
|
+
if (!this._isRunning) return;
|
|
62
|
+
const client = this._sshClientFactory();
|
|
63
|
+
this._client = client;
|
|
64
|
+
|
|
65
|
+
client.on('ready', () => {
|
|
66
|
+
this._backoffIdx = 0; // reset on successful auth
|
|
67
|
+
this._startPolling();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
client.on('close', () => {
|
|
71
|
+
if (!this._isRunning) return;
|
|
72
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
73
|
+
this._scheduleReconnect();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
client.on('error', () => {
|
|
77
|
+
// ssh2 surfaces auth/network errors as 'error' then 'close'.
|
|
78
|
+
// 'close' handler schedules reconnect; nothing to do here.
|
|
79
|
+
if (!this._isRunning) return;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
client.connect(this._sshOpts);
|
|
84
|
+
} catch {
|
|
85
|
+
this._scheduleReconnect();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_scheduleReconnect() {
|
|
90
|
+
if (!this._isRunning) return;
|
|
91
|
+
const delay = this.backoffSchedule[Math.min(this._backoffIdx, this.backoffSchedule.length - 1)];
|
|
92
|
+
this._backoffIdx = Math.min(this._backoffIdx + 1, this.backoffSchedule.length - 1);
|
|
93
|
+
this._reconnectTimer = setTimeout(() => this._connect(), delay);
|
|
94
|
+
// Do not block process exit on a pending reconnect — `stop()` clears it explicitly.
|
|
95
|
+
this._reconnectTimer.unref?.();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
_startPolling() {
|
|
99
|
+
const tick = () => {
|
|
100
|
+
if (!this._isRunning || !this._client) return;
|
|
101
|
+
this._client.exec(DSPLOG_COMMAND, (err, stream) => {
|
|
102
|
+
if (err) return; // close handler covers reconnect
|
|
103
|
+
let buf = '';
|
|
104
|
+
stream.on('data', (chunk) => { buf += chunk.toString('utf8'); });
|
|
105
|
+
stream.on('close', () => this._processOutput(buf));
|
|
106
|
+
stream.stderr?.on('data', () => { /* discard */ });
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
tick();
|
|
110
|
+
this._pollTimer = setInterval(tick, this.pollInterval);
|
|
111
|
+
// Do not block process exit on the poll cadence — `stop()` clears it explicitly.
|
|
112
|
+
this._pollTimer.unref?.();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_processOutput(text) {
|
|
116
|
+
const lines = text.split(/\r?\n/);
|
|
117
|
+
for (const line of lines) {
|
|
118
|
+
if (line.trim().length === 0) continue;
|
|
119
|
+
const code = extractMessageId(line);
|
|
120
|
+
if (!code) continue;
|
|
121
|
+
const event = Object.freeze({
|
|
122
|
+
adapter: ADAPTER,
|
|
123
|
+
errorCode: code,
|
|
124
|
+
file: `ssh://${this.host}/DSPLOG`,
|
|
125
|
+
message: line,
|
|
126
|
+
source: `ssh://${this.host}`,
|
|
127
|
+
timestamp: new Date().toISOString(),
|
|
128
|
+
});
|
|
129
|
+
this.emit('error', event);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export default As400LogAdapter;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/adapters/cobol/cobol-parser.js
|
|
2
|
+
// Multi-pattern parser surface for COBOL/JES2 spool exports. Each regex is
|
|
3
|
+
// exported as a named const for isolated unit-testing (T-2-03-equivalent
|
|
4
|
+
// mitigation surface). The `extractCobolCode` helper picks the first matching
|
|
5
|
+
// pattern in priority order.
|
|
6
|
+
//
|
|
7
|
+
// Priority (highest first): SYSTEM COMPLETION CODE → SQLCODE → FILE STATUS →
|
|
8
|
+
// ABEND CODE → IEF info code. The first three are hard error conditions; IEF
|
|
9
|
+
// info codes are the lowest priority because the same line often carries
|
|
10
|
+
// the actual completion code (e.g. `IEF450I ... ABEND=S0C7`) and the
|
|
11
|
+
// system-completion match is more specific.
|
|
12
|
+
|
|
13
|
+
export const COBOL_COMPLETION_REGEX = /SYSTEM COMPLETION CODE=([SU]?\w{3,4})/;
|
|
14
|
+
export const COBOL_SQLCODE_REGEX = /SQLCODE\s*=?\s*(-?\d+)/;
|
|
15
|
+
export const COBOL_IEF_REGEX = /\b(IEF\d{3}I)\b/;
|
|
16
|
+
export const COBOL_FILE_STATUS_REGEX = /FILE STATUS\s+(\d+)/;
|
|
17
|
+
export const COBOL_ABEND_CODE_REGEX = /ABEND CODE=(\w+)/;
|
|
18
|
+
|
|
19
|
+
export function extractCobolCode(line) {
|
|
20
|
+
let m = line.match(COBOL_COMPLETION_REGEX);
|
|
21
|
+
if (m) {
|
|
22
|
+
const raw = m[1];
|
|
23
|
+
// System codes start with 'S' (system) or 'U' (user). 0C7 → S0C7
|
|
24
|
+
// normalization per IBM convention.
|
|
25
|
+
if (/^[SU]/.test(raw)) return raw;
|
|
26
|
+
return `S${raw}`;
|
|
27
|
+
}
|
|
28
|
+
m = line.match(COBOL_SQLCODE_REGEX);
|
|
29
|
+
if (m) return m[1]; // includes leading '-' for negative codes
|
|
30
|
+
m = line.match(COBOL_FILE_STATUS_REGEX);
|
|
31
|
+
if (m) return `FILE_STATUS_${m[1]}`;
|
|
32
|
+
m = line.match(COBOL_ABEND_CODE_REGEX);
|
|
33
|
+
if (m) return m[1];
|
|
34
|
+
m = line.match(COBOL_IEF_REGEX);
|
|
35
|
+
if (m) return m[1];
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// src/adapters/cobol/index.js
|
|
2
|
+
// CobolLogAdapter — watches arbitrary files in the configured JES2 spool
|
|
3
|
+
// directory; emits 6-key error events. AD-1 lifecycle, AD-3 polling, AD-4
|
|
4
|
+
// byte-position dedup with TAIL-on-startup default, AD-5 per-file ordering,
|
|
5
|
+
// AD-6 CRLF tolerance + 80-col fixed-width column-1 preservation (z/OS spool
|
|
6
|
+
// trap). T-2-01 mitigation: default polling interval 5000 ms.
|
|
7
|
+
//
|
|
8
|
+
// The glob is `*` (any file) — JES2 spool exports have arbitrary names.
|
|
9
|
+
// This is broader than sibling adapters but the parser regexes are narrow
|
|
10
|
+
// enough (SYSTEM COMPLETION CODE, SQLCODE, IEF, FILE STATUS, ABEND CODE)
|
|
11
|
+
// that lines without any match are silently dropped.
|
|
12
|
+
|
|
13
|
+
import { EventEmitter } from 'node:events';
|
|
14
|
+
import { stat, open } from 'node:fs/promises';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import chokidar from 'chokidar';
|
|
17
|
+
import { extractCobolCode } from './cobol-parser.js';
|
|
18
|
+
|
|
19
|
+
const ADAPTER = 'cobol';
|
|
20
|
+
|
|
21
|
+
export class CobolLogAdapter extends EventEmitter {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
super();
|
|
24
|
+
this.spoolDir = config.spoolDir || '/var/spool/jes2';
|
|
25
|
+
this.interval = config.interval ?? 5000;
|
|
26
|
+
this.replayOnStart = config.replayOnStart === true;
|
|
27
|
+
this._watcher = null;
|
|
28
|
+
this._offsets = new Map(); // filePath → byte offset
|
|
29
|
+
this._inodes = new Map(); // filePath → inode
|
|
30
|
+
this._isRunning = false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get isRunning() {
|
|
34
|
+
return this._isRunning;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async start() {
|
|
38
|
+
if (this._isRunning) return;
|
|
39
|
+
this._isRunning = true;
|
|
40
|
+
|
|
41
|
+
// Glob bound to any file in spool dir. depth:0 prevents recursion.
|
|
42
|
+
const glob = join(this.spoolDir, '*');
|
|
43
|
+
|
|
44
|
+
this._watcher = chokidar.watch(glob, {
|
|
45
|
+
usePolling: true,
|
|
46
|
+
interval: this.interval,
|
|
47
|
+
ignoreInitial: false, // we handle initial ourselves via TAIL seek
|
|
48
|
+
awaitWriteFinish: false,
|
|
49
|
+
depth: 0, // do not recurse into subdirs
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const onAdd = async (filePath) => {
|
|
53
|
+
if (this.replayOnStart) {
|
|
54
|
+
this._offsets.set(filePath, 0);
|
|
55
|
+
// fall through to _readNew below
|
|
56
|
+
} else {
|
|
57
|
+
try {
|
|
58
|
+
const st = await stat(filePath);
|
|
59
|
+
this._offsets.set(filePath, st.size);
|
|
60
|
+
this._inodes.set(filePath, st.ino);
|
|
61
|
+
return; // TAIL mode — don't emit pre-existing content
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
this._readNew(filePath).catch(() => { /* ENOENT silence */ });
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
this._watcher.on('add', onAdd);
|
|
70
|
+
this._watcher.on('change', (fp) => {
|
|
71
|
+
this._readNew(fp).catch(() => { /* ENOENT silence */ });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Swallow chokidar's own bubbled errors (e.g., watch-path ENOENT) —
|
|
75
|
+
// dual-layer suppression with the per-read try/catch ensures full
|
|
76
|
+
// ENOENT silence (canonical pattern from Plan 02-07).
|
|
77
|
+
this._watcher.on('error', () => {});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async stop() {
|
|
81
|
+
if (!this._isRunning) return;
|
|
82
|
+
this._isRunning = false;
|
|
83
|
+
if (this._watcher) {
|
|
84
|
+
await this._watcher.close().catch(() => {});
|
|
85
|
+
this._watcher = null;
|
|
86
|
+
}
|
|
87
|
+
this._offsets.clear();
|
|
88
|
+
this._inodes.clear();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async _readNew(filePath) {
|
|
92
|
+
let st;
|
|
93
|
+
try {
|
|
94
|
+
st = await stat(filePath);
|
|
95
|
+
} catch {
|
|
96
|
+
return; // ENOENT silence
|
|
97
|
+
}
|
|
98
|
+
if (!st.isFile()) return; // skip dirs that may surface from broad `*` glob
|
|
99
|
+
|
|
100
|
+
const lastOffset = this._offsets.get(filePath) ?? 0;
|
|
101
|
+
const lastInode = this._inodes.get(filePath);
|
|
102
|
+
|
|
103
|
+
// Rotation: inode changed OR size shrunk → reset to 0
|
|
104
|
+
if ((lastInode !== undefined && st.ino !== lastInode) || st.size < lastOffset) {
|
|
105
|
+
this._offsets.set(filePath, 0);
|
|
106
|
+
this._inodes.set(filePath, st.ino);
|
|
107
|
+
return this._readNew(filePath); // re-enter with offset=0
|
|
108
|
+
}
|
|
109
|
+
this._inodes.set(filePath, st.ino);
|
|
110
|
+
|
|
111
|
+
if (st.size <= lastOffset) return;
|
|
112
|
+
|
|
113
|
+
const fh = await open(filePath, 'r');
|
|
114
|
+
try {
|
|
115
|
+
const len = st.size - lastOffset;
|
|
116
|
+
const buf = Buffer.alloc(len);
|
|
117
|
+
await fh.read(buf, 0, len, lastOffset);
|
|
118
|
+
this._offsets.set(filePath, st.size);
|
|
119
|
+
const text = buf.toString('utf8');
|
|
120
|
+
// CRLF-tolerant split: \r?\n splits on either ending. The trailing
|
|
121
|
+
// \r is consumed by the regex so event.message is clean of artifacts
|
|
122
|
+
// (AD-6 z/OS spool trap).
|
|
123
|
+
const lines = text.split(/\r?\n/);
|
|
124
|
+
const sourceDir = filePath.split('/').slice(0, -1).join('/');
|
|
125
|
+
for (const line of lines) {
|
|
126
|
+
// CRITICAL: do NOT trim. Column-1 in z/OS spool = print control char
|
|
127
|
+
// (0/1/space) and is semantically meaningful (page eject markers,
|
|
128
|
+
// ABEND headers, etc.) — AD-6 fixed-width trap. Skip only truly
|
|
129
|
+
// empty lines; lines that are pure whitespace (column-1 padding)
|
|
130
|
+
// are also skipped because no parser regex would match them.
|
|
131
|
+
if (line.length === 0) continue;
|
|
132
|
+
const code = extractCobolCode(line);
|
|
133
|
+
if (!code) continue;
|
|
134
|
+
// 6-key frozen event payload (AD-4 contract — extra keys forbidden)
|
|
135
|
+
const event = Object.freeze({
|
|
136
|
+
adapter: ADAPTER,
|
|
137
|
+
errorCode: code,
|
|
138
|
+
file: filePath,
|
|
139
|
+
message: line,
|
|
140
|
+
source: sourceDir,
|
|
141
|
+
timestamp: new Date().toISOString(),
|
|
142
|
+
});
|
|
143
|
+
this.emit('error', event);
|
|
144
|
+
}
|
|
145
|
+
} finally {
|
|
146
|
+
await fh.close();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default CobolLogAdapter;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/adapters/ibmmq/ibmmq-parser.js
|
|
2
|
+
// T-2-03 isolation surface: regexes exported as named constants for unit testing.
|
|
3
|
+
// Adapter binds chokidar to AMQERR0[1-3].LOG glob only; these regexes never
|
|
4
|
+
// run against arbitrary text by the adapter itself.
|
|
5
|
+
|
|
6
|
+
// Pattern: AMQ followed by 4 digits, optional suffix letter (E error / W warn / I info).
|
|
7
|
+
// IBM MQ V9 message-code shape per Knowledge Center.
|
|
8
|
+
export const IBMMQ_AMQ_REGEX = /\b(AMQ\d{4}[EWI]?)\b/;
|
|
9
|
+
|
|
10
|
+
// Secondary capture: MQRC reason code from "ArithInsert1(NNNN)" (formal SLOG)
|
|
11
|
+
// or "returned NNNN" (free-form prose). Group 1 = ArithInsert1 form, group 2 = "returned" form.
|
|
12
|
+
export const IBMMQ_MQRC_REGEX = /(?:ArithInsert1\((\d+)\)|returned\s+(\d+))/;
|
|
13
|
+
|
|
14
|
+
export function extractAmqCode(line) {
|
|
15
|
+
const m = line.match(IBMMQ_AMQ_REGEX);
|
|
16
|
+
return m ? m[1] : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function extractMqrc(line) {
|
|
20
|
+
const m = line.match(IBMMQ_MQRC_REGEX);
|
|
21
|
+
if (!m) return null;
|
|
22
|
+
return m[1] || m[2] || null;
|
|
23
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// src/adapters/ibmmq/index.js
|
|
2
|
+
// IbmMqLogAdapter — watches AMQERR0[1-3].LOG in the configured MQ errors dir;
|
|
3
|
+
// emits 6-key error events. AD-1 lifecycle, AD-3 polling, AD-4 byte-position
|
|
4
|
+
// dedup with TAIL-on-startup default, AD-5 per-file ordering.
|
|
5
|
+
// T-2-01 mitigation: default polling interval 5000 ms.
|
|
6
|
+
|
|
7
|
+
import { EventEmitter } from 'node:events';
|
|
8
|
+
import { stat, open } from 'node:fs/promises';
|
|
9
|
+
import { basename, join } from 'node:path';
|
|
10
|
+
import chokidar from 'chokidar';
|
|
11
|
+
import { extractAmqCode } from './ibmmq-parser.js';
|
|
12
|
+
|
|
13
|
+
const ADAPTER = 'ibmmq';
|
|
14
|
+
|
|
15
|
+
// T-2-03 anchor: only AMQERR01.LOG / AMQERR02.LOG / AMQERR03.LOG are accepted.
|
|
16
|
+
// Equivalent to the glob AMQERR0[1-3].LOG, but enforced post-watch in the handler
|
|
17
|
+
// because chokidar usePolling:true does not engage with bracket-class globs on
|
|
18
|
+
// macOS/Node 24 (verified empirically: AMQERR*.LOG fires events; AMQERR0[1-3].LOG
|
|
19
|
+
// fires nothing). The watcher uses AMQERR*.LOG; this regex narrows back to 01..03.
|
|
20
|
+
const IBMMQ_FILENAME_ANCHOR = /^AMQERR0[1-3]\.LOG$/;
|
|
21
|
+
|
|
22
|
+
export class IbmMqLogAdapter extends EventEmitter {
|
|
23
|
+
constructor(config = {}) {
|
|
24
|
+
super();
|
|
25
|
+
this.errorsDir = config.errorsDir || '/var/mqm/qmgrs/QM_PRD01/errors';
|
|
26
|
+
this.interval = config.interval ?? 5000;
|
|
27
|
+
this.replayOnStart = config.replayOnStart === true;
|
|
28
|
+
this._watcher = null;
|
|
29
|
+
this._offsets = new Map(); // filePath → byte offset
|
|
30
|
+
this._inodes = new Map(); // filePath → inode
|
|
31
|
+
this._isRunning = false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get isRunning() { return this._isRunning; }
|
|
35
|
+
|
|
36
|
+
async start() {
|
|
37
|
+
if (this._isRunning) return;
|
|
38
|
+
this._isRunning = true;
|
|
39
|
+
|
|
40
|
+
// Watch glob = AMQERR*.LOG (chokidar polling-compatible); the strict anchor
|
|
41
|
+
// AMQERR0[1-3].LOG is enforced by IBMMQ_FILENAME_ANCHOR on every event.
|
|
42
|
+
const glob = join(this.errorsDir, 'AMQERR*.LOG');
|
|
43
|
+
|
|
44
|
+
this._watcher = chokidar.watch(glob, {
|
|
45
|
+
usePolling: true,
|
|
46
|
+
interval: this.interval,
|
|
47
|
+
ignoreInitial: false, // we handle initial ourselves via TAIL seek
|
|
48
|
+
awaitWriteFinish: false,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const isAnchoredFile = (fp) => IBMMQ_FILENAME_ANCHOR.test(basename(fp));
|
|
52
|
+
|
|
53
|
+
const onAdd = async (filePath) => {
|
|
54
|
+
if (!isAnchoredFile(filePath)) return; // T-2-03: drop non-anchored
|
|
55
|
+
if (this.replayOnStart) {
|
|
56
|
+
this._offsets.set(filePath, 0);
|
|
57
|
+
// fall through to _readNew below
|
|
58
|
+
} else {
|
|
59
|
+
try {
|
|
60
|
+
const st = await stat(filePath);
|
|
61
|
+
this._offsets.set(filePath, st.size);
|
|
62
|
+
this._inodes.set(filePath, st.ino);
|
|
63
|
+
return; // TAIL mode — don't emit pre-existing content
|
|
64
|
+
} catch { return; }
|
|
65
|
+
}
|
|
66
|
+
this._readNew(filePath).catch(() => { /* ENOENT silence */ });
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
this._watcher.on('add', onAdd);
|
|
70
|
+
this._watcher.on('change', (fp) => {
|
|
71
|
+
if (!isAnchoredFile(fp)) return; // T-2-03: drop non-anchored
|
|
72
|
+
this._readNew(fp).catch(() => { /* ENOENT silence */ });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Swallow chokidar's own bubbled errors (e.g., watch-path ENOENT).
|
|
76
|
+
this._watcher.on('error', () => {});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async stop() {
|
|
80
|
+
if (!this._isRunning) return;
|
|
81
|
+
this._isRunning = false;
|
|
82
|
+
if (this._watcher) {
|
|
83
|
+
await this._watcher.close().catch(() => {});
|
|
84
|
+
this._watcher = null;
|
|
85
|
+
}
|
|
86
|
+
this._offsets.clear();
|
|
87
|
+
this._inodes.clear();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async _readNew(filePath) {
|
|
91
|
+
let st;
|
|
92
|
+
try { st = await stat(filePath); } catch { return; } // ENOENT silence
|
|
93
|
+
|
|
94
|
+
const lastOffset = this._offsets.get(filePath) ?? 0;
|
|
95
|
+
const lastInode = this._inodes.get(filePath);
|
|
96
|
+
|
|
97
|
+
// Rotation: inode changed OR size shrunk → reset to 0
|
|
98
|
+
if ((lastInode !== undefined && st.ino !== lastInode) || st.size < lastOffset) {
|
|
99
|
+
this._offsets.set(filePath, 0);
|
|
100
|
+
this._inodes.set(filePath, st.ino);
|
|
101
|
+
return this._readNew(filePath); // re-enter with offset=0
|
|
102
|
+
}
|
|
103
|
+
this._inodes.set(filePath, st.ino);
|
|
104
|
+
|
|
105
|
+
if (st.size <= lastOffset) return;
|
|
106
|
+
|
|
107
|
+
const fh = await open(filePath, 'r');
|
|
108
|
+
try {
|
|
109
|
+
const len = st.size - lastOffset;
|
|
110
|
+
const buf = Buffer.alloc(len);
|
|
111
|
+
await fh.read(buf, 0, len, lastOffset);
|
|
112
|
+
this._offsets.set(filePath, st.size);
|
|
113
|
+
const text = buf.toString('utf8');
|
|
114
|
+
const lines = text.split(/\r?\n/);
|
|
115
|
+
const sourceDir = filePath.split('/').slice(0, -1).join('/');
|
|
116
|
+
for (const line of lines) {
|
|
117
|
+
if (line.trim().length === 0) continue;
|
|
118
|
+
const code = extractAmqCode(line);
|
|
119
|
+
if (!code) continue;
|
|
120
|
+
// 6-key frozen event payload (AD-4 contract — extra keys forbidden)
|
|
121
|
+
const event = Object.freeze({
|
|
122
|
+
adapter: ADAPTER,
|
|
123
|
+
errorCode: code,
|
|
124
|
+
file: filePath,
|
|
125
|
+
message: line,
|
|
126
|
+
source: sourceDir,
|
|
127
|
+
timestamp: new Date().toISOString(),
|
|
128
|
+
});
|
|
129
|
+
this.emit('error', event);
|
|
130
|
+
}
|
|
131
|
+
} finally {
|
|
132
|
+
await fh.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export default IbmMqLogAdapter;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/adapters/index.js
|
|
2
|
+
// Phase 2 — Legacy connectors registry. AD-1: returns unstarted instances; caller starts.
|
|
3
|
+
//
|
|
4
|
+
// Public API:
|
|
5
|
+
// loadAdapters(config) → unstarted Adapter[]
|
|
6
|
+
// stopAll(adapters, timeoutMs?) → Promise<void>, never throws
|
|
7
|
+
// ALLOWED_STACK_LIST → frozen string[] of valid stack names
|
|
8
|
+
|
|
9
|
+
import { SapLogAdapter } from './sap/index.js';
|
|
10
|
+
import { IbmMqLogAdapter } from './ibmmq/index.js';
|
|
11
|
+
import { As400LogAdapter } from './as400/index.js';
|
|
12
|
+
import { OracleFormsLogAdapter } from './oracle-forms/index.js';
|
|
13
|
+
import { CobolLogAdapter } from './cobol/index.js';
|
|
14
|
+
|
|
15
|
+
const ALLOWED_STACKS = Object.freeze(['sap', 'ibmmq', 'as400', 'oracle-forms', 'cobol']);
|
|
16
|
+
|
|
17
|
+
const FACTORIES = Object.freeze({
|
|
18
|
+
'sap': (cfg) => new SapLogAdapter(cfg),
|
|
19
|
+
'ibmmq': (cfg) => new IbmMqLogAdapter(cfg),
|
|
20
|
+
'as400': (cfg) => new As400LogAdapter(cfg),
|
|
21
|
+
'oracle-forms': (cfg) => new OracleFormsLogAdapter(cfg),
|
|
22
|
+
'cobol': (cfg) => new CobolLogAdapter(cfg),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Load configured adapter instances WITHOUT starting them (AD-1).
|
|
27
|
+
*
|
|
28
|
+
* @param {object} config
|
|
29
|
+
* @param {string[]} config.legacyStacks subset of ['sap','ibmmq','as400','oracle-forms','cobol']
|
|
30
|
+
* @param {object} [config.sap] per-adapter config object
|
|
31
|
+
* @param {object} [config.ibmmq]
|
|
32
|
+
* @param {object} [config.as400]
|
|
33
|
+
* @param {object} [config['oracle-forms']]
|
|
34
|
+
* @param {object} [config.cobol]
|
|
35
|
+
* @returns {Array} unstarted adapters; caller invokes .start() on each
|
|
36
|
+
* @throws {Error} on unknown stack name (message names the offender)
|
|
37
|
+
*/
|
|
38
|
+
export function loadAdapters(config = {}) {
|
|
39
|
+
const stacks = Array.isArray(config.legacyStacks) ? config.legacyStacks : [];
|
|
40
|
+
for (const s of stacks) {
|
|
41
|
+
if (!ALLOWED_STACKS.includes(s)) {
|
|
42
|
+
throw new Error(`loadAdapters: unknown stack '${s}'. Allowed: ${ALLOWED_STACKS.join(', ')}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return stacks.map((s) => FACTORIES[s](config[s] || {}));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Stop all adapters in parallel with overall timeout (AC-11).
|
|
50
|
+
* Used by SIGTERM handler. Never throws — failures are swallowed so the
|
|
51
|
+
* shutdown path always returns and the event loop drains.
|
|
52
|
+
*
|
|
53
|
+
* @param {Array} adapters
|
|
54
|
+
* @param {number} [timeoutMs=5000]
|
|
55
|
+
* @returns {Promise<void>}
|
|
56
|
+
*/
|
|
57
|
+
export async function stopAll(adapters, timeoutMs = 5000) {
|
|
58
|
+
if (!Array.isArray(adapters) || adapters.length === 0) return;
|
|
59
|
+
const stopPromises = adapters.map((a) =>
|
|
60
|
+
Promise.resolve()
|
|
61
|
+
.then(() => (typeof a?.stop === 'function' ? a.stop() : undefined))
|
|
62
|
+
.catch(() => { /* swallow per never-throw contract */ })
|
|
63
|
+
);
|
|
64
|
+
// Bounded timer so a non-resolving stop() cannot block process exit.
|
|
65
|
+
let timer;
|
|
66
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
67
|
+
timer = setTimeout(resolve, timeoutMs);
|
|
68
|
+
// Per project convention, unref the timer so it never holds the loop open
|
|
69
|
+
// beyond the rest of the work (AD-3 timer hygiene).
|
|
70
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
71
|
+
});
|
|
72
|
+
try {
|
|
73
|
+
await Promise.race([Promise.all(stopPromises), timeoutPromise]);
|
|
74
|
+
} finally {
|
|
75
|
+
if (timer) clearTimeout(timer);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const ALLOWED_STACK_LIST = ALLOWED_STACKS;
|