observe-node 1.0.3 → 1.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "observe-node",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -41,12 +41,41 @@ function expressMiddleware(observe, opts = {}) {
41
41
  return _json(body);
42
42
  };
43
43
 
44
- const _send = res.send.bind(res);
45
- res.send = (body) => {
46
- // if body is string/buffer, avoid logging full
47
- capturedResponse = { truncated: true, bytes: Buffer.isBuffer(body) ? body.length : (typeof body === "string" ? Buffer.byteLength(body) : null) };
44
+ const _send = res.send.bind(res);
45
+ res.send = (body) => {
46
+ try {
47
+ // If body is a JSON string, parse it and store full object
48
+ if (typeof body === "string") {
49
+ const trimmed = body.trim();
50
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
51
+ const parsed = JSON.parse(trimmed);
52
+ capturedResponse = pickResponseBody(parsed, maxResponseBytes);
53
+ return _send(body);
54
+ }
55
+ // non-json string => don't log full
56
+ capturedResponse = { truncated: true, bytes: Buffer.byteLength(body, "utf8") };
48
57
  return _send(body);
49
- };
58
+ }
59
+
60
+ // If Buffer, don't log full
61
+ if (Buffer.isBuffer(body)) {
62
+ capturedResponse = { truncated: true, bytes: body.length };
63
+ return _send(body);
64
+ }
65
+
66
+ // If object (sometimes people do res.send({}))
67
+ if (typeof body === "object" && body !== null) {
68
+ capturedResponse = pickResponseBody(body, maxResponseBytes);
69
+ return _send(body);
70
+ }
71
+
72
+ capturedResponse = { truncated: true, bytes: null };
73
+ return _send(body);
74
+ } catch (e) {
75
+ capturedResponse = { truncated: true, bytes: null, parse_error: true };
76
+ return _send(body);
77
+ }
78
+ };
50
79
 
51
80
  res.on("finish", () => {
52
81
  const duration_ms = Date.now() - start;