@fabbahiense/pulsar-pino-transport 0.2.1 → 0.4.0

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.
@@ -0,0 +1,360 @@
1
+ // ../shared/src/redact.ts
2
+ var DEFAULT_REDACT_KEYS = [
3
+ "password",
4
+ "passwd",
5
+ "password_hash",
6
+ "secret",
7
+ "client_secret",
8
+ "token",
9
+ "access_token",
10
+ "refresh_token",
11
+ "id_token",
12
+ "session_id",
13
+ "api_key",
14
+ "x-api-key",
15
+ "x-auth-token",
16
+ "authorization",
17
+ "proxy-authorization",
18
+ "cookie",
19
+ "set-cookie"
20
+ ];
21
+ function normalizeKey(key) {
22
+ return String(key).toLowerCase().replace(/[-_]/g, "");
23
+ }
24
+ function redactKeyList(extra = []) {
25
+ return [...new Set([...DEFAULT_REDACT_KEYS, ...extra].map(normalizeKey))];
26
+ }
27
+ function isRedactedKey(key, redactKeys) {
28
+ return redactKeys.includes(normalizeKey(key));
29
+ }
30
+
31
+ // ../shared/src/trace.ts
32
+ import { AsyncLocalStorage } from "async_hooks";
33
+ import { createHash, randomBytes } from "crypto";
34
+ var TRACEPARENT_HEADER = "traceparent";
35
+ var TRACESTATE_HEADER = "tracestate";
36
+ var TRACE_ID_HEADER = "x-trace-id";
37
+ var MAX_TRACESTATE_CHARS = 512;
38
+ var ZERO_TRACE_ID = "0".repeat(32);
39
+ var ZERO_SPAN_ID = "0".repeat(16);
40
+ var TRACEPARENT_RE = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?$/;
41
+ var TRACE_ID_RE = /^[0-9a-f]{32}$/;
42
+ var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
43
+ function newTraceId() {
44
+ for (; ; ) {
45
+ const id = randomBytes(16).toString("hex");
46
+ if (id !== ZERO_TRACE_ID) return id;
47
+ }
48
+ }
49
+ function newSpanId() {
50
+ for (; ; ) {
51
+ const id = randomBytes(8).toString("hex");
52
+ if (id !== ZERO_SPAN_ID) return id;
53
+ }
54
+ }
55
+ function sha256Hex(value) {
56
+ return createHash("sha256").update(value, "utf8").digest("hex");
57
+ }
58
+ function toW3CTraceId(traceId) {
59
+ const lower = traceId.toLowerCase();
60
+ if (TRACE_ID_RE.test(lower) && lower !== ZERO_TRACE_ID) return lower;
61
+ if (UUID_RE.test(lower)) {
62
+ const hex = lower.replace(/-/g, "");
63
+ if (hex !== ZERO_TRACE_ID) return hex;
64
+ }
65
+ return sha256Hex(traceId).slice(0, 32);
66
+ }
67
+ function parseTraceparent(value) {
68
+ if (typeof value !== "string") return null;
69
+ const header = value.trim();
70
+ const match = TRACEPARENT_RE.exec(header);
71
+ if (!match) return null;
72
+ const [, version, traceId, parentId, flagsHex, rest] = match;
73
+ if (version === "ff") return null;
74
+ if (version === "00" && rest !== void 0) return null;
75
+ if (traceId === ZERO_TRACE_ID || parentId === ZERO_SPAN_ID) return null;
76
+ const flags = parseInt(flagsHex, 16);
77
+ return { version, traceId, parentId, flags, sampled: (flags & 1) === 1 };
78
+ }
79
+ var STORE_KEY = /* @__PURE__ */ Symbol.for("@fabbahiense/pulsar.trace-context.v1");
80
+ function sharedStore() {
81
+ const g = globalThis;
82
+ let store = g[STORE_KEY];
83
+ if (!store) {
84
+ store = new AsyncLocalStorage();
85
+ g[STORE_KEY] = store;
86
+ }
87
+ return store;
88
+ }
89
+ var traceStore = sharedStore();
90
+ function text(value) {
91
+ return typeof value === "string" && value.length > 0 ? value : void 0;
92
+ }
93
+ function completeContext(init) {
94
+ const ctx = {
95
+ traceId: text(init?.traceId) ?? newTraceId(),
96
+ spanId: text(init?.spanId) ?? newSpanId(),
97
+ sampled: init?.sampled !== false
98
+ };
99
+ const parentSpanId = text(init?.parentSpanId);
100
+ if (parentSpanId) ctx.parentSpanId = parentSpanId;
101
+ const traceState = text(init?.traceState);
102
+ if (traceState) ctx.traceState = traceState;
103
+ const method = text(init?.method);
104
+ if (method) ctx.method = method;
105
+ const url = text(init?.url);
106
+ if (url) ctx.url = url;
107
+ return ctx;
108
+ }
109
+ function childContext(parent) {
110
+ if (!parent || !text(parent.traceId)) return completeContext(null);
111
+ const local = parent;
112
+ return completeContext({
113
+ traceId: parent.traceId,
114
+ parentSpanId: parent.spanId,
115
+ sampled: parent.sampled,
116
+ traceState: parent.traceState,
117
+ method: local.method,
118
+ url: local.url
119
+ });
120
+ }
121
+ function read(carrier, name) {
122
+ let value;
123
+ if (typeof carrier.get === "function") {
124
+ value = carrier.get(name);
125
+ } else {
126
+ const record = carrier;
127
+ value = record[name];
128
+ if (value === void 0) {
129
+ for (const key of Object.keys(record)) {
130
+ if (key.toLowerCase() === name) {
131
+ value = record[key];
132
+ break;
133
+ }
134
+ }
135
+ }
136
+ }
137
+ if (Array.isArray(value)) value = value.find((v) => typeof v === "string");
138
+ return typeof value === "string" && value.length > 0 ? value : void 0;
139
+ }
140
+ function readTraceState(carrier) {
141
+ let raw;
142
+ if (typeof carrier.get === "function") {
143
+ raw = carrier.get(TRACESTATE_HEADER);
144
+ } else {
145
+ const record = carrier;
146
+ raw = record[TRACESTATE_HEADER];
147
+ if (raw === void 0) {
148
+ const key = Object.keys(record).find((k) => k.toLowerCase() === TRACESTATE_HEADER);
149
+ raw = key === void 0 ? void 0 : record[key];
150
+ }
151
+ }
152
+ const joined = Array.isArray(raw) ? raw.filter((v) => typeof v === "string").join(",") : raw;
153
+ if (typeof joined !== "string") return void 0;
154
+ const value = joined.trim();
155
+ return value.length > 0 && value.length <= MAX_TRACESTATE_CHARS ? value : void 0;
156
+ }
157
+ function extractTrace(carrier) {
158
+ if (typeof carrier !== "object" || carrier === null) return null;
159
+ try {
160
+ const source = carrier;
161
+ const legacy = read(source, TRACE_ID_HEADER);
162
+ const parent = parseTraceparent(read(source, TRACEPARENT_HEADER));
163
+ if (legacy !== void 0) {
164
+ if (parent && parent.traceId === toW3CTraceId(legacy)) {
165
+ return withTraceState({ traceId: legacy, spanId: parent.parentId, sampled: parent.sampled }, source);
166
+ }
167
+ return { traceId: legacy, sampled: true };
168
+ }
169
+ if (parent) return withTraceState({ traceId: parent.traceId, spanId: parent.parentId, sampled: parent.sampled }, source);
170
+ return null;
171
+ } catch {
172
+ return null;
173
+ }
174
+ }
175
+ function withTraceState(trace, carrier) {
176
+ const traceState = readTraceState(carrier);
177
+ if (traceState) trace.traceState = traceState;
178
+ return trace;
179
+ }
180
+
181
+ // src/middleware.ts
182
+ var DEFAULT_SKIP_URLS = ["/health", "/metrics", "/favicon.ico"];
183
+ var DEFAULT_MAX_BODY = 5e4;
184
+ function pulsarRequestLogger(options) {
185
+ const logger = options.logger;
186
+ if (!logger) throw new Error("pulsarRequestLogger: options.logger is required");
187
+ const skipUrls = options.skipUrls ?? DEFAULT_SKIP_URLS;
188
+ const redactKeys = redactKeyList(options.redactKeys ?? []);
189
+ const maxBodySize = options.maxBodySize ?? DEFAULT_MAX_BODY;
190
+ const getUserId = options.getUserId ?? ((req) => req.user?.id);
191
+ const getLevel = options.getLevel ?? defaultLevel;
192
+ return function pulsarMw(req, res, next) {
193
+ const url = req.originalUrl || req.url;
194
+ const ctx = requestContext(req, url);
195
+ if (skipUrls.some((s) => url.includes(s))) return traceStore.run(ctx, next);
196
+ const startedAt = Date.now();
197
+ const capture = new BodyCapture(maxBodySize);
198
+ const originalWrite = res.write.bind(res);
199
+ const originalEnd = res.end.bind(res);
200
+ res.write = function(chunk, ...rest) {
201
+ capture.add(chunk, rest[0]);
202
+ return originalWrite(chunk, ...rest);
203
+ };
204
+ res.end = function(chunk, ...rest) {
205
+ capture.add(chunk, rest[0]);
206
+ return originalEnd(chunk, ...rest);
207
+ };
208
+ res.on("finish", () => traceStore.run(ctx, () => {
209
+ try {
210
+ const responseTime = Date.now() - startedAt;
211
+ const status = res.statusCode;
212
+ const level = getLevel(req, res);
213
+ const responseBody = readBuffer(capture, maxBodySize, getResponseContentType(res), redactKeys);
214
+ const payload = {
215
+ // Top level: becomes columns and filters
216
+ req: { method: req.method, url, id: req.id },
217
+ res: { statusCode: status },
218
+ responseTime,
219
+ // A request id set later (pino-http registered after this middleware) still wins, as it always has.
220
+ traceId: req.id || ctx.traceId,
221
+ // The request's span-end entry.
222
+ spanId: ctx.spanId,
223
+ ...ctx.parentSpanId ? { parentSpanId: ctx.parentSpanId } : {},
224
+ spanKind: "server",
225
+ // metadata: the sections of the side panel
226
+ metadata: trimUndefined({
227
+ requestBody: redactDeep(req.body, redactKeys),
228
+ responseBody,
229
+ requestHeaders: redactHeaders(req.headers, redactKeys),
230
+ responseHeaders: redactHeaders(res.getHeaders ? res.getHeaders() : {}, redactKeys),
231
+ query: isPopulatedObject(req.query) ? req.query : void 0,
232
+ params: isPopulatedObject(req.params) ? req.params : void 0,
233
+ userId: getUserId(req)
234
+ })
235
+ };
236
+ const msg = `${req.method} ${url} ${status}`;
237
+ logger[level](payload, msg);
238
+ } catch (err) {
239
+ if (typeof console !== "undefined") console.error("[pulsarRequestLogger] failed:", err);
240
+ }
241
+ }));
242
+ traceStore.run(ctx, next);
243
+ };
244
+ }
245
+ function requestContext(req, url) {
246
+ let parent = extractTrace(req.headers);
247
+ const id = req.id;
248
+ if (id !== void 0 && id !== null && id !== "") {
249
+ const traceId = String(id);
250
+ if (!parent || parent.traceId !== traceId) parent = { traceId, sampled: parent?.sampled ?? true };
251
+ }
252
+ const ctx = childContext(parent);
253
+ ctx.method = req.method;
254
+ ctx.url = url;
255
+ return ctx;
256
+ }
257
+ function defaultLevel(_req, res) {
258
+ if (res.statusCode >= 500) return "error";
259
+ if (res.statusCode >= 400) return "warn";
260
+ return "info";
261
+ }
262
+ function toBuffer(chunk, encoding) {
263
+ if (Buffer.isBuffer(chunk)) return chunk;
264
+ if (chunk instanceof Uint8Array) return Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
265
+ if (typeof chunk === "string") {
266
+ return typeof encoding === "string" && Buffer.isEncoding(encoding) ? Buffer.from(chunk, encoding) : Buffer.from(chunk);
267
+ }
268
+ try {
269
+ return Buffer.from(JSON.stringify(chunk));
270
+ } catch {
271
+ return Buffer.alloc(0);
272
+ }
273
+ }
274
+ var BodyCapture = class {
275
+ constructor(max) {
276
+ this.max = max;
277
+ }
278
+ max;
279
+ chunks = [];
280
+ kept = 0;
281
+ total = 0;
282
+ add(chunk, encoding) {
283
+ if (!chunk || typeof chunk === "function") return;
284
+ try {
285
+ const buf = toBuffer(chunk, encoding);
286
+ this.total += buf.length;
287
+ const room = this.max + 1 - this.kept;
288
+ if (room <= 0) return;
289
+ const part = Buffer.from(buf.length > room ? buf.subarray(0, room) : buf);
290
+ this.chunks.push(part);
291
+ this.kept += part.length;
292
+ } catch {
293
+ }
294
+ }
295
+ bytes() {
296
+ return Buffer.concat(this.chunks);
297
+ }
298
+ };
299
+ function readBuffer(capture, max, contentType, redactKeys) {
300
+ if (capture.total === 0) return void 0;
301
+ const buf = capture.bytes();
302
+ if (capture.total > max) {
303
+ return `[TRUNCATED ${capture.total}B > ${max}B] ${buf.subarray(0, max).toString("utf8")}\u2026`;
304
+ }
305
+ const raw = buf.toString("utf8");
306
+ const ct = contentType.toLowerCase();
307
+ if (ct.includes("application/json")) {
308
+ try {
309
+ return redactDeep(JSON.parse(raw), redactKeys);
310
+ } catch {
311
+ return raw;
312
+ }
313
+ }
314
+ return raw;
315
+ }
316
+ function getResponseContentType(res) {
317
+ if (!res.getHeaders) return "";
318
+ const headers = res.getHeaders();
319
+ const v = headers["content-type"] ?? headers["Content-Type"];
320
+ if (typeof v === "string") return v;
321
+ if (Array.isArray(v)) return v.join(", ");
322
+ return "";
323
+ }
324
+ function redactHeaders(headers, redactKeys) {
325
+ const out = {};
326
+ for (const [k, v] of Object.entries(headers)) {
327
+ if (v === void 0 || v === null) continue;
328
+ out[k] = isRedactedKey(k, redactKeys) ? "[REDACTED]" : Array.isArray(v) ? v.join(", ") : String(v);
329
+ }
330
+ return out;
331
+ }
332
+ function redactDeep(value, redactKeys, seen = /* @__PURE__ */ new WeakSet()) {
333
+ if (value === null || value === void 0) return value;
334
+ if (typeof value !== "object") return value;
335
+ if (seen.has(value)) return "[Circular]";
336
+ seen.add(value);
337
+ try {
338
+ if (Array.isArray(value)) return value.map((v) => redactDeep(v, redactKeys, seen));
339
+ const out = {};
340
+ for (const [k, v] of Object.entries(value)) {
341
+ out[k] = isRedactedKey(k, redactKeys) ? "[REDACTED]" : redactDeep(v, redactKeys, seen);
342
+ }
343
+ return out;
344
+ } finally {
345
+ seen.delete(value);
346
+ }
347
+ }
348
+ function trimUndefined(obj) {
349
+ const out = {};
350
+ for (const [k, v] of Object.entries(obj)) {
351
+ if (v !== void 0) out[k] = v;
352
+ }
353
+ return out;
354
+ }
355
+ function isPopulatedObject(v) {
356
+ return typeof v === "object" && v !== null && !Array.isArray(v) && Object.keys(v).length > 0;
357
+ }
358
+ export {
359
+ pulsarRequestLogger
360
+ };
package/package.json CHANGED
@@ -1,32 +1,59 @@
1
1
  {
2
2
  "name": "@fabbahiense/pulsar-pino-transport",
3
- "version": "0.2.1",
4
- "description": "Pino transport for Pulsar observability platform",
3
+ "version": "0.4.0",
4
+ "description": "Pino transport for Pulsar observability platform + Express request logger middleware",
5
+ "author": "Fabrício Bahiense Santos",
5
6
  "type": "module",
6
7
  "main": "./dist/index.cjs",
7
8
  "module": "./dist/index.js",
8
9
  "types": "./dist/index.d.ts",
9
10
  "exports": {
10
11
  ".": {
11
- "import": "./dist/index.js",
12
- "require": "./dist/index.cjs",
13
- "types": "./dist/index.d.ts"
12
+ "import": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "require": {
17
+ "types": "./dist/index.d.cts",
18
+ "default": "./dist/index.cjs"
19
+ }
20
+ },
21
+ "./middleware": {
22
+ "import": {
23
+ "types": "./dist/middleware.d.ts",
24
+ "default": "./dist/middleware.js"
25
+ },
26
+ "require": {
27
+ "types": "./dist/middleware.d.cts",
28
+ "default": "./dist/middleware.cjs"
29
+ }
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "typesVersions": {
34
+ "*": {
35
+ "middleware": ["./dist/middleware.d.ts"]
14
36
  }
15
37
  },
16
- "files": ["dist"],
38
+ "files": ["dist", "CHANGELOG.md"],
39
+ "sideEffects": false,
17
40
  "scripts": {
18
41
  "build": "tsup",
19
- "dev": "tsup --watch"
42
+ "dev": "tsup --watch",
43
+ "pretest": "npm run build",
44
+ "test": "vitest run --coverage",
45
+ "prepublishOnly": "npm test"
20
46
  },
21
47
  "peerDependencies": {
22
48
  "pino-abstract-transport": ">=2.0.0"
23
49
  },
24
50
  "devDependencies": {
51
+ "pino": "10.3.1",
25
52
  "pino-abstract-transport": "^3.0.0",
26
53
  "tsup": "^8.0.0",
27
54
  "typescript": "^5.9.0"
28
55
  },
29
- "keywords": ["pino", "transport", "pulsar", "observability", "logging"],
56
+ "keywords": ["pino", "transport", "pulsar", "observability", "logging", "express", "middleware"],
30
57
  "license": "MIT",
31
58
  "engines": {
32
59
  "node": ">=18.0.0"