@mereb/shared-packages 0.0.31 → 0.0.33
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"splunk-transport.d.ts","sourceRoot":"","sources":["../../src/transports/splunk-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAG5C,KAAK,sBAAsB,GAAG;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;
|
|
1
|
+
{"version":3,"file":"splunk-transport.d.ts","sourceRoot":"","sources":["../../src/transports/splunk-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAG5C,KAAK,sBAAsB,GAAG;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AA0CF,wBAA8B,eAAe,CAAC,IAAI,EAAE,sBAAsB,yDAsDzE"}
|
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
import build from 'pino-abstract-transport';
|
|
2
2
|
import { randomUUID } from 'node:crypto';
|
|
3
|
+
const warnOnce = () => {
|
|
4
|
+
let warned = false;
|
|
5
|
+
return (message) => {
|
|
6
|
+
if (!warned) {
|
|
7
|
+
console.warn(message);
|
|
8
|
+
warned = true;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
const parsePayload = (chunk, onWarn) => {
|
|
13
|
+
if (chunk != null && typeof chunk === 'object') {
|
|
14
|
+
return { success: true, payload: chunk }; // already structured JSON from pino
|
|
15
|
+
}
|
|
16
|
+
if (typeof chunk === 'string') {
|
|
17
|
+
try {
|
|
18
|
+
return { success: true, payload: JSON.parse(chunk) };
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
onWarn('Splunk transport dropped log: JSON parse failed');
|
|
22
|
+
return { success: false };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const candidate = chunk?.toString?.();
|
|
26
|
+
if (typeof candidate === 'string') {
|
|
27
|
+
try {
|
|
28
|
+
return { success: true, payload: JSON.parse(candidate) };
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
onWarn('Splunk transport dropped log: JSON parse failed');
|
|
32
|
+
return { success: false };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
onWarn('Splunk transport dropped log: non-string chunk');
|
|
36
|
+
return { success: false };
|
|
37
|
+
};
|
|
3
38
|
export default async function splunkTransport(opts) {
|
|
4
39
|
const url = opts.url;
|
|
5
40
|
const token = opts.token;
|
|
@@ -11,14 +46,11 @@ export default async function splunkTransport(opts) {
|
|
|
11
46
|
throw new Error('Splunk transport requires url, token, and index');
|
|
12
47
|
}
|
|
13
48
|
return build(async (stream) => {
|
|
49
|
+
const warn = warnOnce();
|
|
14
50
|
for await (const chunk of stream) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
payload = JSON.parse(chunk);
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
51
|
+
const parsed = parsePayload(chunk, warn);
|
|
52
|
+
if (!parsed.success)
|
|
20
53
|
continue;
|
|
21
|
-
}
|
|
22
54
|
const controller = AbortSignal.timeout(timeoutMs);
|
|
23
55
|
const requestId = randomUUID();
|
|
24
56
|
const response = await fetch(url, {
|
|
@@ -29,7 +61,7 @@ export default async function splunkTransport(opts) {
|
|
|
29
61
|
'X-Splunk-Request-Channel': requestId
|
|
30
62
|
},
|
|
31
63
|
body: JSON.stringify({
|
|
32
|
-
event: payload,
|
|
64
|
+
event: parsed.payload,
|
|
33
65
|
index,
|
|
34
66
|
source,
|
|
35
67
|
sourcetype
|