1
+ import util from "node:util" ;
1
2
import prettyBytes from "pretty-bytes" ;
2
3
4
+ import { sizeOf } from "./utils" ;
5
+
3
6
import type { InternalAxiosRequestConfig } from "axios" ;
4
7
5
8
const SENSITIVE_HEADERS = [ "Coder-Session-Token" , "Proxy-Authorization" ] ;
@@ -32,24 +35,13 @@ export function formatContentLength(
32
35
const len = headers [ "content-length" ] ;
33
36
if ( len && typeof len === "string" ) {
34
37
const bytes = parseInt ( len , 10 ) ;
35
- return isNaN ( bytes ) ? "(?B)" : `(${ prettyBytes ( bytes ) } )` ;
38
+ return isNaN ( bytes ) ? "(? B)" : `(${ prettyBytes ( bytes ) } )` ;
36
39
}
37
40
38
41
// 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 ) } )` ;
53
45
}
54
46
55
47
if ( typeof data === "object" ) {
@@ -60,7 +52,7 @@ export function formatContentLength(
60
52
}
61
53
}
62
54
63
- return "(?B)" ;
55
+ return "(? B)" ;
64
56
}
65
57
66
58
export function formatUri (
@@ -93,30 +85,17 @@ export function formatBody(body: unknown): string {
93
85
94
86
function safeStringify ( data : unknown ) : string | null {
95
87
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
118
96
} ) ;
119
97
} catch {
98
+ // Should rarely happen but just in case
120
99
return null ;
121
100
}
122
101
}
0 commit comments