fix: require .heapsnapshot (or .heaptimeline) as file extension (#2579)

This makes it harder to pass wrong arguments to the CLI tools.
This commit is contained in:
Dominik Inführ
2026-08-17 09:08:46 +00:00
committed by GitHub
parent 1c8ee68cd0
commit b7501682e4
2 changed files with 20 additions and 0 deletions
+11
View File
@@ -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<
@@ -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) {
@@ -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