From c94285f5b28d5b0d71398617d90578d547831ec7 Mon Sep 17 00:00:00 2001 From: cgombauld Date: Mon, 23 Jun 2025 10:26:04 +0200 Subject: [PATCH] refactor(TempDirectory): used Symbol.asyncDisposable for cleanup --- .changeset/bright-insects-cough.md | 5 +++++ workspaces/scanner/src/class/TempDirectory.class.ts | 4 ++++ workspaces/scanner/src/depWalker.ts | 3 +-- workspaces/scanner/src/index.ts | 3 +-- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 .changeset/bright-insects-cough.md diff --git a/.changeset/bright-insects-cough.md b/.changeset/bright-insects-cough.md new file mode 100644 index 00000000..59196531 --- /dev/null +++ b/.changeset/bright-insects-cough.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/scanner": minor +--- + +refactor(TempDirectory): use Symbol.asyncDispose for automatic async cleanup diff --git a/workspaces/scanner/src/class/TempDirectory.class.ts b/workspaces/scanner/src/class/TempDirectory.class.ts index f54d8a3d..319f5eac 100644 --- a/workspaces/scanner/src/class/TempDirectory.class.ts +++ b/workspaces/scanner/src/class/TempDirectory.class.ts @@ -34,4 +34,8 @@ export class TempDirectory { return this; } + + async [Symbol.asyncDispose]() { + await this.clear(); + } } diff --git a/workspaces/scanner/src/depWalker.ts b/workspaces/scanner/src/depWalker.ts index eb3dabaa..69c3ce9c 100644 --- a/workspaces/scanner/src/depWalker.ts +++ b/workspaces/scanner/src/depWalker.ts @@ -94,7 +94,7 @@ export async function depWalker( registry } = options; - const tempDir = await TempDirectory.create(); + await using tempDir = await TempDirectory.create(); const payload: Partial = { id: tempDir.id, @@ -284,7 +284,6 @@ export async function depWalker( } finally { await timers.setImmediate(); - await tempDir.clear(); logger.emit(ScannerLoggerEvents.done); } diff --git a/workspaces/scanner/src/index.ts b/workspaces/scanner/src/index.ts index f25b0514..738404cf 100644 --- a/workspaces/scanner/src/index.ts +++ b/workspaces/scanner/src/index.ts @@ -88,7 +88,7 @@ export async function verify( return tarball.scanPackage(process.cwd()); } - const tempDir = await TempDirectory.create(); + await using tempDir = await TempDirectory.create(); try { const mama = await tarball.extractAndResolve(tempDir.location, { @@ -101,7 +101,6 @@ export async function verify( } finally { await timers.setImmediate(); - await tempDir.clear(); } }