@@ -2,7 +2,10 @@ import path from 'node:path'
22import { createReadStream , createWriteStream } from 'node:fs'
33import { realpath } from 'node:fs/promises'
44import { tmpdir } from 'node:os'
5- import { HeapSnapshotLoader } from '../../thirdparty/devtools-frontend/index.js'
5+ import {
6+ HeapSnapshotLoader ,
7+ SecondaryInitManager
8+ } from '../../thirdparty/devtools-frontend/index.js'
69import { randomUUID } from 'node:crypto'
710
811// via https://github.com/sindresorhus/temp-dir/blob/437937c/index.js#L4
@@ -54,5 +57,19 @@ export async function createHeapSnapshotModel (filename) {
5457 loader . close ( )
5558 await loader . parsingComplete
5659
57- return ( await loader . buildSnapshot ( ) )
60+ // Pattern borrowed from `createJSHeapSnapshotForTesting` in Chromium code.
61+ // https://github.com/ChromeDevTools/devtools-frontend/blob/866e7ab/front_end/entrypoints/heap_snapshot_worker/HeapSnapshot.ts#L3675-L3682
62+ // Rather than trying to make it work with two workers, we just do it all in one thread.
63+ // For context see this commit splitting the work into two workers:
64+ // https://github.com/ChromeDevTools/devtools-frontend/commit/6a523a7
65+ const channel = new MessageChannel ( )
66+ try {
67+ // eslint-disable-next-line no-new
68+ new SecondaryInitManager ( channel . port2 )
69+ return await loader . buildSnapshot ( channel . port1 )
70+ } finally {
71+ // Without this, the Node process will just hang forever
72+ channel . port1 . close ( )
73+ channel . port2 . close ( )
74+ }
5875}
0 commit comments