| 150 | } |
| 151 | |
| 152 | export async function fetchWithReport( |
| 153 | input: Parameters<typeof fetch>[0], |
| 154 | init?: Parameters<typeof fetch>[1], |
| 155 | options?: FetchWithReportOptions, |
| 156 | ): Promise<Response> { |
| 157 | const fetchImpl = options?.fetchImpl ?? fetch; |
| 158 | const method = fetchMethod(input, init); |
| 159 | const url = fetchUrl(input); |
| 160 | const path = options?.path ?? pathFromUrl(url); |
| 161 | const startedAt = Date.now(); |
| 162 | let response: Response; |
| 163 | try { |
| 164 | response = await fetchImpl(input, init); |
| 165 | } catch (error) { |
| 166 | recordReportEvent( |
| 167 | { |
| 168 | kind: 'http', |
| 169 | method, |
| 170 | path, |
| 171 | url, |
| 172 | durationMs: Date.now() - startedAt, |
| 173 | request: requestForFetchReport(input, init), |
| 174 | error: errorForReport(error), |
| 175 | }, |
| 176 | { reportDir: options?.reportDir }, |
| 177 | ); |
| 178 | throw error; |
| 179 | } |
| 180 | |
| 181 | const text = await response.clone().text(); |
| 182 | recordReportEvent( |
| 183 | { |
| 184 | kind: 'http', |
| 185 | method, |
| 186 | path, |
| 187 | url, |
| 188 | status: response.status, |
| 189 | durationMs: Date.now() - startedAt, |
| 190 | request: requestForFetchReport(input, init), |
| 191 | response: responseForReport(text), |
| 192 | }, |
| 193 | { reportDir: options?.reportDir }, |
| 194 | ); |
| 195 | return response; |
| 196 | } |
| 197 | |
| 198 | export function defaultReportDir(): string { |
| 199 | return resolve(process.env['KIMI_SERVER_E2E_REPORT_DIR'] ?? join(process.cwd(), 'reports', 'latest')); |