|
| 1 | +import{ |
| 2 | +typeInternalAxiosRequestConfig, |
| 3 | +typeAxiosResponse, |
| 4 | +typeAxiosError, |
| 5 | +isAxiosError, |
| 6 | +}from"axios"; |
| 7 | +import{Buffer}from"node:buffer"; |
| 8 | +importcryptofrom"node:crypto"; |
| 9 | +importtype*asvscodefrom"vscode"; |
| 10 | +import{errToStr}from"../api-helper"; |
| 11 | + |
| 12 | +exportinterfaceRequestMeta{ |
| 13 | +requestId:string; |
| 14 | +startedAt:number; |
| 15 | +} |
| 16 | + |
| 17 | +exporttypeRequestConfigWithMeta=InternalAxiosRequestConfig&{ |
| 18 | +metadata?:RequestMeta; |
| 19 | +}; |
| 20 | + |
| 21 | +functionshortId(id:string):string{ |
| 22 | +returnid.slice(0,8); |
| 23 | +} |
| 24 | + |
| 25 | +functionsizeOf(data:unknown):number{ |
| 26 | +if(data===null||data===undefined){ |
| 27 | +return0; |
| 28 | +} |
| 29 | +if(typeofdata==="string"){ |
| 30 | +returnBuffer.byteLength(data); |
| 31 | +} |
| 32 | +if(Buffer.isBuffer(data)){ |
| 33 | +returndata.length; |
| 34 | +} |
| 35 | +if(datainstanceofArrayBuffer||ArrayBuffer.isView(data)){ |
| 36 | +returndata.byteLength; |
| 37 | +} |
| 38 | +if( |
| 39 | +typeofdata==="object"&& |
| 40 | +"size"indata&& |
| 41 | +typeofdata.size==="number" |
| 42 | +){ |
| 43 | +returndata.size; |
| 44 | +} |
| 45 | +return0; |
| 46 | +} |
| 47 | + |
| 48 | +exportfunctioncreateRequestMeta():RequestMeta{ |
| 49 | +return{ |
| 50 | +requestId:crypto.randomUUID().replace(/-/g,""), |
| 51 | +startedAt:Date.now(), |
| 52 | +}; |
| 53 | +} |
| 54 | + |
| 55 | +exportfunctionlogRequestStart( |
| 56 | +logger:vscode.LogOutputChannel, |
| 57 | +requestId:string, |
| 58 | +config:InternalAxiosRequestConfig, |
| 59 | +):void{ |
| 60 | +constmethod=(config.method??"GET").toUpperCase(); |
| 61 | +consturl=config.url||""; |
| 62 | +constlen=config.headers?.["content-length"]asstring|undefined; |
| 63 | +constlenStr=len ?` (${len}b)` :""; |
| 64 | +logger.trace(`→${shortId(requestId)}${method}${url}${lenStr}`); |
| 65 | +} |
| 66 | + |
| 67 | +exportfunctionlogRequestSuccess( |
| 68 | +logger:vscode.LogOutputChannel, |
| 69 | +meta:RequestMeta, |
| 70 | +response:AxiosResponse, |
| 71 | +):void{ |
| 72 | +constmethod=(response.config.method??"GET").toUpperCase(); |
| 73 | +consturl=response.config.url||""; |
| 74 | +constlen=response.headers?.["content-length"]asstring|undefined; |
| 75 | +constms=Date.now()-meta.startedAt; |
| 76 | +constlenStr=len ?` (${len}b)` :""; |
| 77 | +logger.trace( |
| 78 | +`←${shortId(meta.requestId)}${response.status}${method}${url}${ms}ms${lenStr}`, |
| 79 | +); |
| 80 | +} |
| 81 | + |
| 82 | +exportfunctionlogRequestError( |
| 83 | +logger:vscode.LogOutputChannel, |
| 84 | +error:AxiosError|unknown, |
| 85 | +):void{ |
| 86 | +if(isAxiosError(error)){ |
| 87 | +constconfig=error.configasRequestConfigWithMeta|undefined; |
| 88 | +constmeta=config?.metadata; |
| 89 | +constmethod=(config?.method??"GET").toUpperCase(); |
| 90 | +consturl=config?.url||""; |
| 91 | +constrequestId=meta?.requestId??"unknown"; |
| 92 | +constms=meta ?Date.now()-meta.startedAt :"?"; |
| 93 | + |
| 94 | +if(error.response){ |
| 95 | +// Response error (4xx, 5xx status codes) |
| 96 | +constmsg= |
| 97 | +error.response.statusText||String(error.response.data).slice(0,100); |
| 98 | +logger.error( |
| 99 | +`←${shortId(requestId)}${error.response.status}${method}${url}${ms}ms -${msg}`, |
| 100 | +error, |
| 101 | +); |
| 102 | +}else{ |
| 103 | +// Request error (network, timeout, etc) |
| 104 | +constreason=error.code||error.message||"Network error"; |
| 105 | +logger.error( |
| 106 | +`✗${shortId(requestId)}${method}${url}${ms}ms -${reason}`, |
| 107 | +error, |
| 108 | +); |
| 109 | +} |
| 110 | +}else{ |
| 111 | +logger.error("Request error",error); |
| 112 | +} |
| 113 | +} |
| 114 | + |
| 115 | +exportclassWsLogger{ |
| 116 | +privatelogger:vscode.LogOutputChannel; |
| 117 | +privateurl:string; |
| 118 | +privateid:string; |
| 119 | +privatestartedAt:number; |
| 120 | +privateopenedAt?:number; |
| 121 | +privatemsgCount=0; |
| 122 | +privatebyteCount=0; |
| 123 | + |
| 124 | +constructor(logger:vscode.LogOutputChannel,url:string){ |
| 125 | +this.logger=logger; |
| 126 | +this.url=url; |
| 127 | +this.id=crypto.randomUUID().replace(/-/g,""); |
| 128 | +this.startedAt=Date.now(); |
| 129 | +} |
| 130 | + |
| 131 | +logConnecting():void{ |
| 132 | +this.logger.trace(`→ WS${shortId(this.id)}${this.url}`); |
| 133 | +} |
| 134 | + |
| 135 | +logOpen():void{ |
| 136 | +this.openedAt=Date.now(); |
| 137 | +constconnectMs=this.openedAt-this.startedAt; |
| 138 | +this.logger.trace(`← WS${shortId(this.id)} connected${connectMs}ms`); |
| 139 | +} |
| 140 | + |
| 141 | +logMessage(data:unknown):void{ |
| 142 | +this.msgCount+=1; |
| 143 | +this.byteCount+=sizeOf(data); |
| 144 | +} |
| 145 | + |
| 146 | +logClose(code?:number,reason?:string):void{ |
| 147 | +constupMs=this.openedAt ?Date.now()-this.openedAt :0; |
| 148 | +conststats=[]; |
| 149 | +if(upMs>0){ |
| 150 | +stats.push(`${upMs}ms`); |
| 151 | +} |
| 152 | +if(this.msgCount>0){ |
| 153 | +stats.push(`${this.msgCount} msgs`); |
| 154 | +} |
| 155 | +if(this.byteCount>0){ |
| 156 | +stats.push(`${this.byteCount}b`); |
| 157 | +} |
| 158 | + |
| 159 | +constcodeStr=code ?` (${code})` :""; |
| 160 | +constreasonStr=reason ?` -${reason}` :""; |
| 161 | +conststatsStr=stats.length>0 ?` [${stats.join(", ")}]` :""; |
| 162 | + |
| 163 | +this.logger.trace( |
| 164 | +`✗ WS${shortId(this.id)} closed${codeStr}${reasonStr}${statsStr}`, |
| 165 | +); |
| 166 | +} |
| 167 | + |
| 168 | +logError(error:unknown):void{ |
| 169 | +constms=Date.now()-this.startedAt; |
| 170 | +consterrorMsg=errToStr(error,"connection error"); |
| 171 | +this.logger.error( |
| 172 | +`✗ WS${shortId(this.id)} error${ms}ms -${errorMsg}`, |
| 173 | +error, |
| 174 | +); |
| 175 | +} |
| 176 | +} |