UNPKG

@nitedani/inspector-api

Version:

A simple node module to access V8 inspector + some tools to export and read the data.

56 lines (55 loc) 1.95 kB
import * as utils from "./utils"; import { PassThrough } from "stream"; export default class Heap { session; constructor(session) { this.session = session; } async enable() { await utils.invokeFunction(this.session, "HeapProfiler.enable"); } async disable() { await utils.invokeFunction(this.session, "HeapProfiler.disable"); } async startSampling() { await utils.invokeFunction(this.session, "HeapProfiler.startSampling"); } async stopSampling() { return utils.invokeStop("HeapProfiler.stopSampling", this.session); } async startTimeline() { await utils.invokeFunction(this.session, "HeapProfiler.startTrackingHeapObjects", { trackAllocations: true }); } stopTimeline() { const stream = new PassThrough(); const getChunk = (m) => { stream.push(m.params.chunk); }; this.session.on("HeapProfiler.addHeapSnapshotChunk", getChunk); this.session.post("HeapProfiler.stopTrackingHeapObjects", (err) => { this.session.removeListener("HeapProfiler.addHeapSnapshotChunk", getChunk); stream.emit("finish"); stream.emit("end"); stream.end(); if (err) throw err; }); return stream; } takeSnapshot() { const stream = new PassThrough(); const getChunk = (m) => { stream.push(m.params.chunk); }; this.session.on("HeapProfiler.addHeapSnapshotChunk", getChunk); this.session.post("HeapProfiler.takeHeapSnapshot", (err, _r) => { this.session.removeListener("HeapProfiler.addHeapSnapshotChunk", getChunk); stream.emit("finish"); stream.emit("end"); stream.end(); if (err) throw err; }); return stream; } }