@mereb/shared-packages 0.0.30 → 0.0.32
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;
|
|
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;AAgCF,wBAA8B,eAAe,CAAC,IAAI,EAAE,sBAAsB,yDAsDzE"}
|
|
@@ -1,4 +1,34 @@
|
|
|
1
1
|
import build from 'pino-abstract-transport';
|
|
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 normalizeChunk = (chunk) => {
|
|
13
|
+
if (typeof chunk === 'string')
|
|
14
|
+
return chunk;
|
|
15
|
+
const candidate = chunk?.toString?.();
|
|
16
|
+
return typeof candidate === 'string' ? candidate : null;
|
|
17
|
+
};
|
|
18
|
+
const parsePayload = (chunk, onWarn) => {
|
|
19
|
+
const line = normalizeChunk(chunk);
|
|
20
|
+
if (!line) {
|
|
21
|
+
onWarn('Splunk transport dropped log: non-string chunk');
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(line);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
onWarn('Splunk transport dropped log: JSON parse failed');
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
2
32
|
export default async function splunkTransport(opts) {
|
|
3
33
|
const url = opts.url;
|
|
4
34
|
const token = opts.token;
|
|
@@ -10,20 +40,19 @@ export default async function splunkTransport(opts) {
|
|
|
10
40
|
throw new Error('Splunk transport requires url, token, and index');
|
|
11
41
|
}
|
|
12
42
|
return build(async (stream) => {
|
|
43
|
+
const warn = warnOnce();
|
|
13
44
|
for await (const chunk of stream) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
payload = JSON.parse(chunk);
|
|
17
|
-
}
|
|
18
|
-
catch {
|
|
45
|
+
const payload = parsePayload(chunk, warn);
|
|
46
|
+
if (payload == null)
|
|
19
47
|
continue;
|
|
20
|
-
}
|
|
21
48
|
const controller = AbortSignal.timeout(timeoutMs);
|
|
49
|
+
const requestId = randomUUID();
|
|
22
50
|
const response = await fetch(url, {
|
|
23
51
|
method: 'POST',
|
|
24
52
|
headers: {
|
|
25
53
|
Authorization: `Splunk ${token}`,
|
|
26
|
-
'Content-Type': 'application/json'
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
'X-Splunk-Request-Channel': requestId
|
|
27
56
|
},
|
|
28
57
|
body: JSON.stringify({
|
|
29
58
|
event: payload,
|