* Write heap snapshot to a file. * Uses pipeline() which handles stream cleanup automatically on errors.
(filepath: string)
| 282 | * Uses pipeline() which handles stream cleanup automatically on errors. |
| 283 | */ |
| 284 | async function writeHeapSnapshot(filepath: string): Promise<void> { |
| 285 | if (typeof Bun !== 'undefined') { |
| 286 | // In Bun, heapsnapshots are currently not streaming. |
| 287 | // Use synchronous I/O despite potentially large filesize so that we avoid cloning the string for cross-thread usage. |
| 288 | // |
| 289 | /* eslint-disable custom-rules/no-sync-fs -- intentionally sync to avoid cloning large heap snapshot string for cross-thread usage */ |
| 290 | // @ts-expect-error 2nd argument is in the next version of Bun |
| 291 | writeFileSync(filepath, Bun.generateHeapSnapshot('v8', 'arraybuffer'), { |
| 292 | mode: 0o600, |
| 293 | }) |
| 294 | /* eslint-enable custom-rules/no-sync-fs */ |
| 295 | |
| 296 | // Force GC to try to free that heap snapshot sooner. |
| 297 | Bun.gc(true) |
| 298 | return |
| 299 | } |
| 300 | const writeStream = createWriteStream(filepath, { mode: 0o600 }) |
| 301 | const heapSnapshotStream = getHeapSnapshot() |
| 302 | await pipeline(heapSnapshotStream, writeStream) |
| 303 | } |
| 304 |
no test coverage detected