| 288 | } |
| 289 | |
| 290 | export async function fetchFileAsText(url: URL): Promise<string> { |
| 291 | if (typeof window === 'undefined') { |
| 292 | // @ts-expect-error no node types here. |
| 293 | const fs = await import('node:fs/promises'); |
| 294 | // @ts-expect-error no node types here. |
| 295 | const {fileURLToPath} = await import('node:url'); |
| 296 | const path = fileURLToPath(url); |
| 297 | const buffer = await fs.readFile(path); |
| 298 | const contents = await Common.Gzip.arrayBufferToString(buffer); |
| 299 | return contents; |
| 300 | } |
| 301 | const response = await fetch(url); |
| 302 | if (response.status !== 200) { |
| 303 | throw new Error(`Unable to load ${url}`); |
| 304 | } |
| 305 | |
| 306 | const buffer = await response.arrayBuffer(); |
| 307 | const contents = await Common.Gzip.arrayBufferToString(buffer); |
| 308 | return contents; |
| 309 | } |
| 310 | |
| 311 | // Below this point are private methods used in the TraceLoader class. These |
| 312 | // are purposefully not exported, you should use one of the static methods |