|
| 1 | +import { APIConnectionTimeoutError, APIUserAbortError, KernelError } from '../core/error'; |
| 2 | +import type { RequestOptions } from '../internal/request-options'; |
| 3 | +import type { AuditLogExportChunkParams } from '../resources/audit-logs'; |
| 4 | + |
| 5 | +const DEFAULT_MAX_TRANSFER_RETRIES = 6; |
| 6 | +const MAX_CHUNK_ROWS = 50_000; |
| 7 | +const MAX_RETRY_DELAY_MS = 8_000; |
| 8 | + |
| 9 | +export class AuditLogDownloadError extends KernelError {} |
| 10 | + |
| 11 | +export type AuditLogDownloadParams = Omit<AuditLogExportChunkParams, 'cursor' | 'format'>; |
| 12 | + |
| 13 | +export interface AuditLogDownloadResult { |
| 14 | + bytesWritten: number; |
| 15 | + chunks: number; |
| 16 | + rows: number; |
| 17 | +} |
| 18 | + |
| 19 | +export interface AuditLogDownloadProgress extends AuditLogDownloadResult { |
| 20 | + chunkRows: number; |
| 21 | +} |
| 22 | + |
| 23 | +export type AuditLogDownloadWriteResult = void | number | { bytesWritten: number }; |
| 24 | + |
| 25 | +export interface AuditLogDownloadDestination { |
| 26 | + write(chunk: Uint8Array): AuditLogDownloadWriteResult | Promise<AuditLogDownloadWriteResult>; |
| 27 | +} |
| 28 | + |
| 29 | +export interface AuditLogDownloadOptions |
| 30 | + extends Omit< |
| 31 | + RequestOptions, |
| 32 | + 'method' | 'path' | 'query' | 'body' | 'stream' | '__binaryResponse' | '__streamClass' |
| 33 | + > { |
| 34 | + onProgress?(progress: AuditLogDownloadProgress): void | Promise<void>; |
| 35 | + maxTransferRetries?: number; |
| 36 | +} |
| 37 | + |
| 38 | +type FetchChunk = (query: AuditLogExportChunkParams, options?: RequestOptions) => Promise<Response>; |
| 39 | + |
| 40 | +export async function downloadAuditLogs( |
| 41 | + fetchChunk: FetchChunk, |
| 42 | + query: AuditLogDownloadParams, |
| 43 | + destination: AuditLogDownloadDestination, |
| 44 | + defaultTimeout: number, |
| 45 | + options: AuditLogDownloadOptions = {}, |
| 46 | +): Promise<AuditLogDownloadResult> { |
| 47 | + if (!destination || typeof destination.write !== 'function') { |
| 48 | + throw new TypeError('audit log download destination must provide write()'); |
| 49 | + } |
| 50 | + |
| 51 | + const { onProgress, maxTransferRetries = DEFAULT_MAX_TRANSFER_RETRIES, ...requestOptions } = options; |
| 52 | + if (!Number.isInteger(maxTransferRetries) || maxTransferRetries < 0) { |
| 53 | + throw new TypeError('maxTransferRetries must be a non-negative integer'); |
| 54 | + } |
| 55 | + const timeout = requestOptions.timeout ?? defaultTimeout; |
| 56 | + let cursor: string | undefined; |
| 57 | + const result: AuditLogDownloadResult = { bytesWritten: 0, chunks: 0, rows: 0 }; |
| 58 | + const seenCursors = new Set<string>(); |
| 59 | + |
| 60 | + while (true) { |
| 61 | + const chunk = await fetchVerifiedChunk( |
| 62 | + fetchChunk, |
| 63 | + cursor ? { ...query, cursor } : query, |
| 64 | + requestOptions, |
| 65 | + maxTransferRetries, |
| 66 | + timeout, |
| 67 | + ); |
| 68 | + const { nextCursor, hasMore, rows } = parseChunkHeaders(chunk.headers, cursor); |
| 69 | + if (hasMore && nextCursor) { |
| 70 | + if (seenCursors.has(nextCursor)) { |
| 71 | + throw new AuditLogDownloadError('response repeated X-Next-Cursor header'); |
| 72 | + } |
| 73 | + seenCursors.add(nextCursor); |
| 74 | + } |
| 75 | + await writeChunk(destination, chunk.body); |
| 76 | + |
| 77 | + cursor = nextCursor; |
| 78 | + result.bytesWritten += chunk.body.byteLength; |
| 79 | + result.chunks += 1; |
| 80 | + result.rows += rows; |
| 81 | + if (onProgress) { |
| 82 | + await onProgress({ ...result, chunkRows: rows }); |
| 83 | + } |
| 84 | + if (!hasMore) { |
| 85 | + return result; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +async function fetchVerifiedChunk( |
| 91 | + fetchChunk: FetchChunk, |
| 92 | + query: AuditLogExportChunkParams, |
| 93 | + options: RequestOptions, |
| 94 | + maxTransferRetries: number, |
| 95 | + timeout: number, |
| 96 | +): Promise<{ body: Uint8Array; headers: Headers }> { |
| 97 | + for (let retries = 0; ; retries += 1) { |
| 98 | + const controller = new AbortController(); |
| 99 | + const onAbort = () => controller.abort(); |
| 100 | + if (options.signal?.aborted) { |
| 101 | + controller.abort(); |
| 102 | + } else { |
| 103 | + options.signal?.addEventListener('abort', onAbort, { once: true }); |
| 104 | + } |
| 105 | + |
| 106 | + let response: Response; |
| 107 | + try { |
| 108 | + response = await fetchChunk(query, { ...options, signal: controller.signal }); |
| 109 | + } catch (error) { |
| 110 | + options.signal?.removeEventListener('abort', onAbort); |
| 111 | + throw error; |
| 112 | + } |
| 113 | + |
| 114 | + let bodyTimedOut = false; |
| 115 | + const timer = setTimeout(() => { |
| 116 | + bodyTimedOut = true; |
| 117 | + controller.abort(); |
| 118 | + }, timeout); |
| 119 | + try { |
| 120 | + const body = new Uint8Array(await response.arrayBuffer()); |
| 121 | + clearTimeout(timer); |
| 122 | + if (options.signal?.aborted) { |
| 123 | + throw new APIUserAbortError(); |
| 124 | + } |
| 125 | + if (bodyTimedOut) { |
| 126 | + throw new APIConnectionTimeoutError(); |
| 127 | + } |
| 128 | + const expected = response.headers.get('x-content-sha256'); |
| 129 | + if (!expected) { |
| 130 | + throw new AuditLogDownloadError('response missing X-Content-Sha256 header'); |
| 131 | + } |
| 132 | + const actual = await sha256Hex(body); |
| 133 | + if (options.signal?.aborted) { |
| 134 | + throw new APIUserAbortError(); |
| 135 | + } |
| 136 | + if (actual !== expected) { |
| 137 | + throw new AuditLogDownloadError( |
| 138 | + `audit log chunk checksum mismatch (got ${actual}, want ${expected})`, |
| 139 | + ); |
| 140 | + } |
| 141 | + return { body, headers: response.headers }; |
| 142 | + } catch (error) { |
| 143 | + if (options.signal?.aborted && !(error instanceof APIUserAbortError)) { |
| 144 | + error = new APIUserAbortError(); |
| 145 | + } else if (bodyTimedOut) { |
| 146 | + error = new APIConnectionTimeoutError(); |
| 147 | + } |
| 148 | + if (retries === maxTransferRetries || error instanceof APIUserAbortError) { |
| 149 | + throw error; |
| 150 | + } |
| 151 | + await retryDelay(retries + 1, options.signal); |
| 152 | + } finally { |
| 153 | + clearTimeout(timer); |
| 154 | + options.signal?.removeEventListener('abort', onAbort); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +function parseChunkHeaders( |
| 160 | + headers: Headers, |
| 161 | + currentCursor: string | undefined, |
| 162 | +): { rows: number; nextCursor: string | undefined; hasMore: boolean } { |
| 163 | + const hasMoreValue = headers.get('x-has-more'); |
| 164 | + if (hasMoreValue !== 'true' && hasMoreValue !== 'false') { |
| 165 | + throw new AuditLogDownloadError('response missing or invalid X-Has-More header'); |
| 166 | + } |
| 167 | + const hasMore = hasMoreValue === 'true'; |
| 168 | + |
| 169 | + const rowCount = headers.get('x-row-count'); |
| 170 | + if (rowCount === null || !/^[0-9]+$/.test(rowCount)) { |
| 171 | + throw new AuditLogDownloadError('response missing or invalid X-Row-Count header'); |
| 172 | + } |
| 173 | + const rows = Number(rowCount); |
| 174 | + if (!Number.isSafeInteger(rows) || rows > MAX_CHUNK_ROWS) { |
| 175 | + throw new AuditLogDownloadError('response missing or invalid X-Row-Count header'); |
| 176 | + } |
| 177 | + |
| 178 | + const nextCursor = headers.get('x-next-cursor') || undefined; |
| 179 | + if (hasMore && (!nextCursor || nextCursor === currentCursor)) { |
| 180 | + throw new AuditLogDownloadError('response has invalid X-Next-Cursor header'); |
| 181 | + } |
| 182 | + if (!hasMore && nextCursor) { |
| 183 | + throw new AuditLogDownloadError('response returned a cursor after the final chunk'); |
| 184 | + } |
| 185 | + return { rows, nextCursor, hasMore }; |
| 186 | +} |
| 187 | + |
| 188 | +async function sha256Hex(body: Uint8Array): Promise<string> { |
| 189 | + const digest = await globalThis.crypto.subtle.digest('SHA-256', body); |
| 190 | + return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join(''); |
| 191 | +} |
| 192 | + |
| 193 | +async function retryDelay(attempt: number, signal: AbortSignal | null | undefined): Promise<void> { |
| 194 | + const delay = Math.min(1_000 * 2 ** (attempt - 1), MAX_RETRY_DELAY_MS); |
| 195 | + await new Promise<void>((resolve, reject) => { |
| 196 | + if (signal?.aborted) { |
| 197 | + reject(new APIUserAbortError()); |
| 198 | + return; |
| 199 | + } |
| 200 | + const onAbort = () => { |
| 201 | + clearTimeout(timer); |
| 202 | + reject(new APIUserAbortError()); |
| 203 | + }; |
| 204 | + const timer = setTimeout(() => { |
| 205 | + signal?.removeEventListener('abort', onAbort); |
| 206 | + resolve(); |
| 207 | + }, delay); |
| 208 | + signal?.addEventListener('abort', onAbort, { once: true }); |
| 209 | + }); |
| 210 | +} |
| 211 | + |
| 212 | +async function writeChunk(destination: AuditLogDownloadDestination, body: Uint8Array): Promise<void> { |
| 213 | + let offset = 0; |
| 214 | + while (offset < body.byteLength) { |
| 215 | + const result = await destination.write(offset === 0 ? body : body.subarray(offset)); |
| 216 | + if (typeof result === 'number') { |
| 217 | + offset += validateWriteCount(result, body.byteLength - offset); |
| 218 | + continue; |
| 219 | + } |
| 220 | + if (result && typeof result === 'object' && 'bytesWritten' in result) { |
| 221 | + const bytesWritten = (result as { bytesWritten: number }).bytesWritten; |
| 222 | + offset += validateWriteCount(bytesWritten, body.byteLength - offset); |
| 223 | + continue; |
| 224 | + } |
| 225 | + return; |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +function validateWriteCount(value: number, remaining: number): number { |
| 230 | + if (!Number.isSafeInteger(value) || value <= 0 || value > remaining) { |
| 231 | + throw new AuditLogDownloadError('audit log download destination performed a short write'); |
| 232 | + } |
| 233 | + return value; |
| 234 | +} |
0 commit comments