|
| 1 | +import { |
| 2 | + type InternalAxiosRequestConfig, |
| 3 | + type AxiosResponse, |
| 4 | + type AxiosError, |
| 5 | + isAxiosError, |
| 6 | +} from "axios"; |
| 7 | +import { Buffer } from "node:buffer"; |
| 8 | +import crypto from "node:crypto"; |
| 9 | +import type * as vscode from "vscode"; |
| 10 | +import { errToStr } from "../api-helper"; |
| 11 | + |
| 12 | +export interface RequestMeta { |
| 13 | + requestId: string; |
| 14 | + startedAt: number; |
| 15 | +} |
| 16 | + |
| 17 | +export type RequestConfigWithMeta = InternalAxiosRequestConfig & { |
| 18 | + metadata?: RequestMeta; |
| 19 | +}; |
| 20 | + |
| 21 | +function shortId(id: string): string { |
| 22 | + return id.slice(0, 8); |
| 23 | +} |
| 24 | + |
| 25 | +function sizeOf(data: unknown): number { |
| 26 | + if (data === null || data === undefined) { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + if (typeof data === "string") { |
| 30 | + return Buffer.byteLength(data); |
| 31 | + } |
| 32 | + if (Buffer.isBuffer(data)) { |
| 33 | + return data.length; |
| 34 | + } |
| 35 | + if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) { |
| 36 | + return data.byteLength; |
| 37 | + } |
| 38 | + if ( |
| 39 | + typeof data === "object" && |
| 40 | + "size" in data && |
| 41 | + typeof data.size === "number" |
| 42 | + ) { |
| 43 | + return data.size; |
| 44 | + } |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +export function createRequestMeta(): RequestMeta { |
| 49 | + return { |
| 50 | + requestId: crypto.randomUUID().replace(/-/g, ""), |
| 51 | + startedAt: Date.now(), |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +export function logRequestStart( |
| 56 | + logger: vscode.LogOutputChannel, |
| 57 | + requestId: string, |
| 58 | + config: InternalAxiosRequestConfig, |
| 59 | +): void { |
| 60 | + const method = (config.method ?? "GET").toUpperCase(); |
| 61 | + const url = config.url || ""; |
| 62 | + const len = config.headers?.["content-length"] as string | undefined; |
| 63 | + const lenStr = len ? ` (${len}b)` : ""; |
| 64 | + logger.trace(`→ ${shortId(requestId)} ${method} ${url}${lenStr}`); |
| 65 | +} |
| 66 | + |
| 67 | +export function logRequestSuccess( |
| 68 | + logger: vscode.LogOutputChannel, |
| 69 | + meta: RequestMeta, |
| 70 | + response: AxiosResponse, |
| 71 | +): void { |
| 72 | + const method = (response.config.method ?? "GET").toUpperCase(); |
| 73 | + const url = response.config.url || ""; |
| 74 | + const len = response.headers?.["content-length"] as string | undefined; |
| 75 | + const ms = Date.now() - meta.startedAt; |
| 76 | + const lenStr = len ? ` (${len}b)` : ""; |
| 77 | + logger.trace( |
| 78 | + `← ${shortId(meta.requestId)} ${response.status} ${method} ${url} ${ms}ms${lenStr}`, |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +export function logRequestError( |
| 83 | + logger: vscode.LogOutputChannel, |
| 84 | + error: AxiosError | unknown, |
| 85 | +): void { |
| 86 | + if (isAxiosError(error)) { |
| 87 | + const config = error.config as RequestConfigWithMeta | undefined; |
| 88 | + const meta = config?.metadata; |
| 89 | + const method = (config?.method ?? "GET").toUpperCase(); |
| 90 | + const url = config?.url || ""; |
| 91 | + const requestId = meta?.requestId ?? "unknown"; |
| 92 | + const ms = meta ? Date.now() - meta.startedAt : "?"; |
| 93 | + |
| 94 | + if (error.response) { |
| 95 | + // Response error (4xx, 5xx status codes) |
| 96 | + const msg = |
| 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 | + const reason = 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 | +export class WsLogger { |
| 116 | + private logger: vscode.LogOutputChannel; |
| 117 | + private url: string; |
| 118 | + private id: string; |
| 119 | + private startedAt: number; |
| 120 | + private openedAt?: number; |
| 121 | + private msgCount = 0; |
| 122 | + private byteCount = 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 | + const connectMs = 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 | + const upMs = this.openedAt ? Date.now() - this.openedAt : 0; |
| 148 | + const stats = []; |
| 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 | + const codeStr = code ? ` (${code})` : ""; |
| 160 | + const reasonStr = reason ? ` - ${reason}` : ""; |
| 161 | + const statsStr = 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 | + const ms = Date.now() - this.startedAt; |
| 170 | + const errorMsg = errToStr(error, "connection error"); |
| 171 | + this.logger.error( |
| 172 | + `✗ WS ${shortId(this.id)} error ${ms}ms - ${errorMsg}`, |
| 173 | + error, |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments