Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit485b5ae

Browse files
committed
Use utils.inspect instead of JSON.stringify
1 parent055bf83 commit485b5ae

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+
importutilfrom"node:util";
12
importprettyBytesfrom"pretty-bytes";
23

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

58
constSENSITIVE_HEADERS=["Coder-Session-Token","Proxy-Authorization"];
@@ -32,24 +35,13 @@ export function formatContentLength(
3235
constlen=headers["content-length"];
3336
if(len&&typeoflen==="string"){
3437
constbytes=parseInt(len,10);
35-
returnisNaN(bytes) ?"(?B)" :`(${prettyBytes(bytes)})`;
38+
returnisNaN(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(typeofdata==="string"||typeofdata==="bigint"){
48-
constbytes=Buffer.byteLength(data.toString(),"utf8");
49-
return`(${prettyBytes(bytes)})`;
50-
}
51-
if(typeofdata==="number"||typeofdata==="boolean"){
52-
return`(~${prettyBytes(8)})`;
42+
constsize=sizeOf(data);
43+
if(size!==undefined){
44+
return`(${prettyBytes(size)})`;
5345
}
5446

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

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

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

9486
functionsafeStringify(data:unknown):string|null{
9587
try{
96-
constseen=newWeakSet();
97-
returnJSON.stringify(data,(_key,value)=>{
98-
// Handle circular references
99-
if(typeofvalue==="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(typeofvalue==="function"){
108-
return"[Function]";
109-
}
110-
if(typeofvalue==="symbol"){
111-
return"[Symbol]";
112-
}
113-
if(typeofvalue==="bigint"){
114-
returnvalue.toString();
115-
}
116-
117-
returnvalue;
88+
returnutil.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
returnnull;
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
return0;
1111
}
12-
if(typeofdata==="string"){
13-
returnBuffer.byteLength(data);
12+
if(typeofdata==="number"||typeofdata==="boolean"){
13+
return8;
1414
}
15-
if(Buffer.isBuffer(data)){
16-
returndata.length;
15+
if(typeofdata==="string"||typeofdata==="bigint"){
16+
returnBuffer.byteLength(data.toString());
1717
}
18-
if(datainstanceofArrayBuffer||ArrayBuffer.isView(data)){
18+
if(
19+
Buffer.isBuffer(data)||
20+
datainstanceofArrayBuffer||
21+
ArrayBuffer.isView(data)
22+
){
1923
returndata.byteLength;
2024
}
2125
if(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp