Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/processors/HeapSnapshotManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export type HeapQueryOptions =
export type HeapEdgesQueryOptions =
DevTools.HeapSnapshotModel.HeapSnapshotModel.HeapEdgesQueryOptions;

const VALID_EXTENSIONS: readonly string[] = ['.heapsnapshot', '.heaptimeline'];

function hasValidHeapSnapshotExtension(filePath: string): boolean {
return VALID_EXTENSIONS.some(ext => filePath.endsWith(ext));
}

export class HeapSnapshotManager {
#snapshotIdGenerator = createIdGenerator();
#snapshots = new Map<
Expand All @@ -65,6 +71,11 @@ export class HeapSnapshotManager {
async getSnapshot(
filePath: string,
): Promise<DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy> {
if (!hasValidHeapSnapshotExtension(filePath)) {
throw new Error(
`File ${filePath} must have a .heapsnapshot or .heaptimeline extension.`,
);
}
const absolutePath = path.resolve(filePath);
const cached = this.#snapshots.get(absolutePath);
if (cached) {
Expand Down
9 changes: 9 additions & 0 deletions tests/processors/HeapSnapshotManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ describe('HeapSnapshotManager', () => {
sinon.restore();
});

it('rejects when a file without .heapsnapshot or .heaptimeline extension is passed', async () => {
const manager = new HeapSnapshotManager();

await assert.rejects(
manager.getSnapshot('tests/fixtures/snapshot_diffs.js'),
/must have a \.heapsnapshot or \.heaptimeline extension/,
);
});

it('disposes the worker when snapshot loading fails', async () => {
const disposeSpy = sinon.spy(
DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy
Expand Down
Loading