Skip to content

Commit c65c990

Browse files
committed
Use utils.inspect instead of JSON.stringify
1 parent 1feae28 commit c65c990

File tree

2 files changed

+26
-43
lines changed

2 files changed

+26
-43
lines changed

src/logging/formatters.ts

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import util from "node:util";
12
import prettyBytes from "pretty-bytes";
23

4+
import { sizeOf } from "./utils";
5+
36
import type { InternalAxiosRequestConfig } from "axios";
47

58
const SENSITIVE_HEADERS = ["Coder-Session-Token", "Proxy-Authorization"];
@@ -32,24 +35,13 @@ export function formatContentLength(
3235
const len = headers["content-length"];
3336
if (len && typeof len === "string") {
3437
const bytes = parseInt(len, 10);
35-
return isNaN(bytes) ? "(?B)" : `(${prettyBytes(bytes)})`;
38+
return isNaN(bytes) ? "(? B)" : `(${prettyBytes(bytes)})`;
3639
}
3740

3841
// Estimate from data if no header
39-
40-
if (data === undefined || data === null) {
41-
return `(${prettyBytes(0)})`;
42-
}
43-
44-
if (Buffer.isBuffer(data)) {
45-
return `(${prettyBytes(data.byteLength)})`;
46-
}
47-
if (typeof data === "string" || typeof data === "bigint") {
48-
const bytes = Buffer.byteLength(data.toString(), "utf8");
49-
return `(${prettyBytes(bytes)})`;
50-
}
51-
if (typeof data === "number" || typeof data === "boolean") {
52-
return `(~${prettyBytes(8)})`;
42+
const size = sizeOf(data);
43+
if (size !== undefined) {
44+
return `(${prettyBytes(size)})`;
5345
}
5446

5547
if (typeof data === "object") {
@@ -60,7 +52,7 @@ export function formatContentLength(
6052
}
6153
}
6254

63-
return "(?B)";
55+
return "(? B)";
6456
}
6557

6658
export function formatUri(
@@ -93,30 +85,17 @@ export function formatBody(body: unknown): string {
9385

9486
function safeStringify(data: unknown): string | null {
9587
try {
96-
const seen = new WeakSet();
97-
return JSON.stringify(data, (_key, value) => {
98-
// Handle circular references
99-
if (typeof value === "object" && value !== null) {
100-
if (seen.has(value)) {
101-
return "[Circular]";
102-
}
103-
seen.add(value);
104-
}
105-
106-
// Handle special types that might slip through
107-
if (typeof value === "function") {
108-
return "[Function]";
109-
}
110-
if (typeof value === "symbol") {
111-
return "[Symbol]";
112-
}
113-
if (typeof value === "bigint") {
114-
return value.toString();
115-
}
116-
117-
return value;
88+
return util.inspect(data, {
89+
showHidden: false,
90+
depth: Infinity,
91+
maxArrayLength: Infinity,
92+
maxStringLength: Infinity,
93+
breakLength: Infinity,
94+
compact: true,
95+
getters: false, // avoid side-effects
11896
});
11997
} catch {
98+
// Should rarely happen but just in case
12099
return null;
121100
}
122101
}

src/logging/utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ export function sizeOf(data: unknown): number | undefined {
99
if (data === null || data === undefined) {
1010
return 0;
1111
}
12-
if (typeof data === "string") {
13-
return Buffer.byteLength(data);
12+
if (typeof data === "number" || typeof data === "boolean") {
13+
return 8;
1414
}
15-
if (Buffer.isBuffer(data)) {
16-
return data.length;
15+
if (typeof data === "string" || typeof data === "bigint") {
16+
return Buffer.byteLength(data.toString());
1717
}
18-
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
18+
if (
19+
Buffer.isBuffer(data) ||
20+
data instanceof ArrayBuffer ||
21+
ArrayBuffer.isView(data)
22+
) {
1923
return data.byteLength;
2024
}
2125
if (

0 commit comments

Comments
 (0)