diff --git a/src/apkSignature/ApkSignatureProvider.ts b/src/apkSignature/ApkSignatureProvider.ts index 29a59f79f6..175ae1ea09 100644 --- a/src/apkSignature/ApkSignatureProvider.ts +++ b/src/apkSignature/ApkSignatureProvider.ts @@ -8,14 +8,12 @@ import type { ApkSignaturePort, } from "../ports/apkSignature/ApkSignaturePort.js"; import { + closeArtifactResources, withStableArtifactHandle, - ArtifactReaderFailure, assertArtifactReadActive, - bindArtifactFileCleanup, ArtifactResourceScope, type StableArtifactHandle, } from "../resources/index.js"; -import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import type { AnalysisError } from "../domain/analysisErrorBase.js"; import { APK_BLOCK_ID, @@ -370,24 +368,12 @@ export class ApkSignatureProvider implements ApkSignaturePort { /** Owns the selected file's descriptor, retaining it when closing fails. */ readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - if ( - !(cause instanceof ArtifactReaderFailure) || - cause.cleanup === undefined - ) - throw cause; - const error = new ProviderCleanupError( - APK_SIGNATURE_PROVIDER_IDENTITY.id, - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { cause, operation: OPERATION }, - ); - bindArtifactFileCleanup(cause, error); - throw error; - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + APK_SIGNATURE_PROVIDER_IDENTITY, + OPERATION, + ); } async inspect( diff --git a/src/artifacts/ReleaseExposureReader.ts b/src/artifacts/ReleaseExposureReader.ts index 4c465eceae..859e8da673 100644 --- a/src/artifacts/ReleaseExposureReader.ts +++ b/src/artifacts/ReleaseExposureReader.ts @@ -30,7 +30,7 @@ import { readStableArtifact, withStableArtifactHandle, type StableArtifactHandle, - projectArtifactCloseFailure, + closeArtifactResources, readerIntegrityFailure, hashStableRootArtifactHandle, createAsarHeaderSignatureProbe, @@ -240,16 +240,12 @@ export class ReleaseExposureReader implements ReleaseExposurePort { private readonly asarHeaderAdmission: AsarHeaderAdmission = DEFAULT_ASAR_HEADER_ADMISSION, ) {} - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - RELEASE_EXPOSURE_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + RELEASE_EXPOSURE_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/erlang/BeamProvider.ts b/src/erlang/BeamProvider.ts index c7a30224d3..9da7d4deb5 100644 --- a/src/erlang/BeamProvider.ts +++ b/src/erlang/BeamProvider.ts @@ -1,7 +1,7 @@ import { ERLANG_BEAM_PROVIDER_IDENTITY } from "./ErlangBeamProviderIdentity.js"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, ArtifactReaderFailure, withStableArtifactHandle, artifactFilePrimaryFacts, @@ -38,16 +38,12 @@ export class BeamProvider implements BeamPort { readonly identity = ERLANG_BEAM_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - ERLANG_BEAM_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + ERLANG_BEAM_PROVIDER_IDENTITY, + OPERATION, + ); } async inspect(input: InspectErlangBeamInput, options?: ExecutionOptions) { diff --git a/src/firmware/devicetree/DevicetreeProvider.ts b/src/firmware/devicetree/DevicetreeProvider.ts index 6839287f6f..42226aa886 100644 --- a/src/firmware/devicetree/DevicetreeProvider.ts +++ b/src/firmware/devicetree/DevicetreeProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/parseFailureAnalysisError.js"; import { artifactFilePrimaryFacts, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, ArtifactReaderFailure, type StableArtifactSnapshot, @@ -34,16 +34,12 @@ export class DevicetreeProvider implements DevicetreePort { readonly identity = DEVICETREE_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - DEVICETREE_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + DEVICETREE_PROVIDER, + OPERATION, + ); } async inspect(input: InspectDevicetreeInput, options?: ExecutionOptions) { diff --git a/src/go/GoBinaryProvider.ts b/src/go/GoBinaryProvider.ts index 53ac2b01a4..53769b7ee0 100644 --- a/src/go/GoBinaryProvider.ts +++ b/src/go/GoBinaryProvider.ts @@ -5,7 +5,7 @@ import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; import { GO_BINARY_PROVIDER_IDENTITY } from "./GoBinaryProviderIdentity.js"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, artifactFilePrimaryFacts, assertArtifactReadActive, @@ -280,16 +280,12 @@ export class GoBinaryProvider implements GoBinaryPort { readonly identity = GO_BINARY_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - GO_BINARY_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + GO_BINARY_PROVIDER_IDENTITY, + OPERATION, + ); } /** Read complete compiler/module metadata while preserving original byte identity and source ranges. */ diff --git a/src/il2cpp/Il2cppMetadataProvider.ts b/src/il2cpp/Il2cppMetadataProvider.ts index 8f26d9436d..188d6177c0 100644 --- a/src/il2cpp/Il2cppMetadataProvider.ts +++ b/src/il2cpp/Il2cppMetadataProvider.ts @@ -19,6 +19,7 @@ import type { AnalysisPartialObservation } from "../domain/analysisErrorBase.js" import { jsonValueSchema } from "../domain/jsonValue.js"; import { setImmediate } from "node:timers/promises"; import { + closeArtifactResources, readStableArtifact, type StableArtifactSnapshot, ArtifactResourceScope, @@ -421,25 +422,15 @@ export class Il2cppMetadataProvider implements Il2cppMetadataPort { private readonly yieldControl: () => Promise = setImmediate, ) {} - async close(): Promise { - this.#admissionStopped = true; - try { - await this.#resources.close(); - } catch (cause: unknown) { - if ( - !(cause instanceof ArtifactReaderFailure) || - cause.cleanup === undefined - ) - throw cause; - const error = new ProviderCleanupError( - IL2CPP_METADATA_PROVIDER_IDENTITY.id, - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { cause, operation: OPERATION }, - ); - bindArtifactFileCleanup(cause, error); - throw error; - } + close(): Promise { + return closeArtifactResources( + () => { + this.#admissionStopped = true; + return this.#resources.close(); + }, + IL2CPP_METADATA_PROVIDER_IDENTITY, + OPERATION, + ); } /** Read every image, type and method name from one stable file snapshot. */ diff --git a/src/java/MavenGradleMetadataProvider.ts b/src/java/MavenGradleMetadataProvider.ts index 2ae10ccabc..89b256fd37 100644 --- a/src/java/MavenGradleMetadataProvider.ts +++ b/src/java/MavenGradleMetadataProvider.ts @@ -2,7 +2,7 @@ import { ArtifactReaderFailure, ArtifactResourceScope, artifactFilePrimaryFacts, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, readStableArtifact, translateArtifactFailure, @@ -42,16 +42,12 @@ export class MavenGradleMetadataProvider { readonly identity = MAVEN_GRADLE_METADATA_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - MAVEN_GRADLE_METADATA_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + MAVEN_GRADLE_METADATA_PROVIDER_IDENTITY, + OPERATION, + ); } async inspect( diff --git a/src/native/apple/AppleIpsProvider.ts b/src/native/apple/AppleIpsProvider.ts index 57cbba37ca..de8e036851 100644 --- a/src/native/apple/AppleIpsProvider.ts +++ b/src/native/apple/AppleIpsProvider.ts @@ -2,7 +2,7 @@ import { completedStableArtifactObservation, type StableArtifactSnapshot, projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, readStableArtifact, withStableArtifactHandle, artifactFileCompletion, @@ -54,16 +54,12 @@ export class AppleIpsProvider implements AppleIpsPort { readonly identity = APPLE_IPS_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - APPLE_IPS_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + APPLE_IPS_PROVIDER, + OPERATION, + ); } async inspect(input: InspectAppleIpsInput, options?: ExecutionOptions) { diff --git a/src/native/dwarf/DwarfUnitsProvider.ts b/src/native/dwarf/DwarfUnitsProvider.ts index 549dd29e69..4cd13c1490 100644 --- a/src/native/dwarf/DwarfUnitsProvider.ts +++ b/src/native/dwarf/DwarfUnitsProvider.ts @@ -1,5 +1,5 @@ import { - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, withStableArtifactHandle, yieldArtifactRead, @@ -56,16 +56,12 @@ export class DwarfUnitsProvider { readonly identity = DWARF_UNITS_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - DWARF_UNITS_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + DWARF_UNITS_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/native/elf/ElfDebugLinkProvider.ts b/src/native/elf/ElfDebugLinkProvider.ts index 8a30e1c621..b7ddeeea8a 100644 --- a/src/native/elf/ElfDebugLinkProvider.ts +++ b/src/native/elf/ElfDebugLinkProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/parseFailureAnalysisError.js"; import { decodeLimit } from "../../domain/parseFailure.js"; import { - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, ArtifactReaderFailure, completedStableArtifactStream, @@ -59,16 +59,12 @@ export class ElfDebugLinkProvider implements ElfDebugLinkPort { readonly identity = ELF_DEBUG_LINK_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - ELF_DEBUG_LINK_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + ELF_DEBUG_LINK_PROVIDER, + OPERATION, + ); } async inspect(input: InspectElfDebugLinkInput, options?: ExecutionOptions) { diff --git a/src/native/elf/ElfDependenciesProvider.ts b/src/native/elf/ElfDependenciesProvider.ts index 3072b8193b..531c1a4260 100644 --- a/src/native/elf/ElfDependenciesProvider.ts +++ b/src/native/elf/ElfDependenciesProvider.ts @@ -5,7 +5,7 @@ import { } from "../../domain/parseFailureAnalysisError.js"; import { decodeLimit } from "../../domain/parseFailure.js"; import { - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, withStableArtifactHandle, artifactFilePrimaryFacts, @@ -44,16 +44,12 @@ export class ElfDependenciesProvider { readonly identity = ELF_DEPENDENCIES_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - ELF_DEPENDENCIES_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + ELF_DEPENDENCIES_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/native/elf/ElfNotesProvider.ts b/src/native/elf/ElfNotesProvider.ts index 730c6366c4..010a484d65 100644 --- a/src/native/elf/ElfNotesProvider.ts +++ b/src/native/elf/ElfNotesProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/parseFailureAnalysisError.js"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, yieldArtifactRead, artifactFilePrimaryFacts, @@ -44,16 +44,12 @@ export class ElfNotesProvider implements ElfNotesPort { readonly identity = ELF_NOTES_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - ELF_NOTES_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + ELF_NOTES_PROVIDER, + OPERATION, + ); } async inspect(input: InspectElfNotesInput, options?: ExecutionOptions) { diff --git a/src/native/elf/ElfUnwindRangesProvider.ts b/src/native/elf/ElfUnwindRangesProvider.ts index e1002d6221..1d935ee413 100644 --- a/src/native/elf/ElfUnwindRangesProvider.ts +++ b/src/native/elf/ElfUnwindRangesProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/parseFailureAnalysisError.js"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, artifactFilePrimaryFacts, ArtifactResourceScope, @@ -41,16 +41,12 @@ export class ElfUnwindRangesProvider implements ElfUnwindRangesPort { readonly identity = ELF_UNWIND_RANGES_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - ELF_UNWIND_RANGES_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + ELF_UNWIND_RANGES_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/native/elf/KernelBuildConfigProvider.ts b/src/native/elf/KernelBuildConfigProvider.ts index e220b658b0..4cdef758d2 100644 --- a/src/native/elf/KernelBuildConfigProvider.ts +++ b/src/native/elf/KernelBuildConfigProvider.ts @@ -1,6 +1,6 @@ import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, ArtifactReaderFailure, withStableArtifactHandle, artifactFilePrimaryFacts, @@ -35,16 +35,12 @@ export class KernelBuildConfigProvider implements KernelBuildConfigPort { readonly identity = KERNEL_BUILD_CONFIG_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - KERNEL_BUILD_CONFIG_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + KERNEL_BUILD_CONFIG_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/native/elf/KernelModuleProvider.ts b/src/native/elf/KernelModuleProvider.ts index 1322825090..b3f50dc6ca 100644 --- a/src/native/elf/KernelModuleProvider.ts +++ b/src/native/elf/KernelModuleProvider.ts @@ -1,7 +1,7 @@ import { parseLimitConstraint } from "../../domain/parseFailureAnalysisError.js"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, ArtifactReaderFailure, withStableArtifactHandle, artifactFilePrimaryFacts, @@ -35,16 +35,12 @@ export class KernelModuleProvider implements KernelModulePort { readonly identity = KERNEL_MODULE_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - KERNEL_MODULE_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + KERNEL_MODULE_PROVIDER, + OPERATION, + ); } async inspect(input: InspectKernelModuleInput, options?: ExecutionOptions) { diff --git a/src/native/exports/NativeExportsProvider.ts b/src/native/exports/NativeExportsProvider.ts index b06916f9a0..20b1be9649 100644 --- a/src/native/exports/NativeExportsProvider.ts +++ b/src/native/exports/NativeExportsProvider.ts @@ -4,7 +4,7 @@ import { artifactFileCeilingConstraint, artifactReadBudgetConstraint, projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, artifactFilePrimaryFacts, assertArtifactReadActive, @@ -47,16 +47,12 @@ export class NativeExportsProvider implements NativeExportsPort { readonly identity = NATIVE_EXPORTS_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - NATIVE_EXPORTS_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + NATIVE_EXPORTS_PROVIDER, + OPERATION, + ); } async inspect(input: InspectNativeExportsInput, options?: ExecutionOptions) { diff --git a/src/native/hardening/BinaryHardeningProvider.ts b/src/native/hardening/BinaryHardeningProvider.ts index 9303e02313..0702c284de 100644 --- a/src/native/hardening/BinaryHardeningProvider.ts +++ b/src/native/hardening/BinaryHardeningProvider.ts @@ -5,7 +5,7 @@ import { artifactFileCeilingConstraint, artifactReadBudgetConstraint, projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, artifactFilePrimaryFacts, assertArtifactReadActive, @@ -43,16 +43,12 @@ export class BinaryHardeningProvider implements BinaryHardeningPort { readonly identity = BINARY_HARDENING_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - BINARY_HARDENING_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + BINARY_HARDENING_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/native/initializers/NativeInitializersProvider.ts b/src/native/initializers/NativeInitializersProvider.ts index 2bb6906f02..a9aa246467 100644 --- a/src/native/initializers/NativeInitializersProvider.ts +++ b/src/native/initializers/NativeInitializersProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/parseFailureAnalysisError.js"; import { imageFileSizeRemediation, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, ArtifactReaderFailure, readStableArtifact, @@ -59,16 +59,12 @@ export class NativeInitializersProvider implements NativeInitializersPort { readonly identity = NATIVE_INITIALIZERS_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - NATIVE_INITIALIZERS_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + NATIVE_INITIALIZERS_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/native/pe/PeCertificatesProvider.ts b/src/native/pe/PeCertificatesProvider.ts index 2e2ea0d31d..26b2c86c16 100644 --- a/src/native/pe/PeCertificatesProvider.ts +++ b/src/native/pe/PeCertificatesProvider.ts @@ -6,7 +6,7 @@ import { artifactFileCeilingConstraint, artifactReadBudgetConstraint, artifactFilePrimaryFacts, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, ArtifactReaderFailure, withStableArtifactHandle, @@ -39,16 +39,12 @@ export class PeCertificatesProvider implements PeCertificatesPort { readonly identity = PE_CERTIFICATES_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - PE_CERTIFICATES_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + PE_CERTIFICATES_PROVIDER, + OPERATION, + ); } async inspect(input: InspectPeCertificatesInput, options?: ExecutionOptions) { diff --git a/src/native/pe/PeImportsProvider.ts b/src/native/pe/PeImportsProvider.ts index 4340839454..e72897397d 100644 --- a/src/native/pe/PeImportsProvider.ts +++ b/src/native/pe/PeImportsProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/artifactReadBudget.js"; import { projectPeBinaryFailure } from "./PeBinaryFailure.js"; import { - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, assertArtifactReadActive, ArtifactResourceScope, @@ -40,16 +40,12 @@ export class PeImportsProvider implements PeImportsPort { readonly identity = PE_IMPORTS_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - PE_IMPORTS_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + PE_IMPORTS_PROVIDER, + OPERATION, + ); } async inspect(input: InspectPeImportsInput, options?: ExecutionOptions) { diff --git a/src/native/pe/PeLoadConfigProvider.ts b/src/native/pe/PeLoadConfigProvider.ts index b0163aadeb..a0caced67e 100644 --- a/src/native/pe/PeLoadConfigProvider.ts +++ b/src/native/pe/PeLoadConfigProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/artifactReadBudget.js"; import { projectPeBinaryFailure } from "./PeBinaryFailure.js"; import { - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, assertArtifactReadActive, ArtifactResourceScope, @@ -38,16 +38,12 @@ export class PeLoadConfigProvider implements PeLoadConfigPort { readonly identity = PE_LOAD_CONFIG_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - PE_LOAD_CONFIG_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + PE_LOAD_CONFIG_PROVIDER, + OPERATION, + ); } async inspect(input: InspectPeLoadConfigInput, options?: ExecutionOptions) { diff --git a/src/native/pe/PeResourcesProvider.ts b/src/native/pe/PeResourcesProvider.ts index a27e108bda..a32b809a9c 100644 --- a/src/native/pe/PeResourcesProvider.ts +++ b/src/native/pe/PeResourcesProvider.ts @@ -6,7 +6,7 @@ import { artifactFileCeilingConstraint, artifactReadBudgetConstraint, projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, ArtifactReaderFailure, withStableArtifactHandle, artifactFilePrimaryFacts, @@ -42,16 +42,12 @@ export class PeResourcesProvider implements PeResourcesPort { readonly identity = PE_RESOURCES_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - PE_RESOURCES_PROVIDER.id, - "inspect_pe_resources", - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + PE_RESOURCES_PROVIDER, + "inspect_pe_resources", + ); } async inspect(input: InspectPeResourcesInput, options?: ExecutionOptions) { diff --git a/src/native/pe/PeUnwindRangesProvider.ts b/src/native/pe/PeUnwindRangesProvider.ts index 4da0d809a4..f617c93f4d 100644 --- a/src/native/pe/PeUnwindRangesProvider.ts +++ b/src/native/pe/PeUnwindRangesProvider.ts @@ -4,7 +4,7 @@ import { } from "../../domain/artifactReadBudget.js"; import { projectPeBinaryFailure } from "./PeBinaryFailure.js"; import { - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, assertArtifactReadActive, ArtifactResourceScope, @@ -29,16 +29,12 @@ export class PeUnwindRangesProvider implements PeUnwindRangesPort { readonly identity = PE_UNWIND_RANGES_PROVIDER; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - PE_UNWIND_RANGES_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + PE_UNWIND_RANGES_PROVIDER, + OPERATION, + ); } async inspect(input: InspectPeUnwindRangesInput, options?: ExecutionOptions) { diff --git a/src/native/windows/WindowsShortcutProvider.ts b/src/native/windows/WindowsShortcutProvider.ts index 943d7ee42e..2f32c4ad2e 100644 --- a/src/native/windows/WindowsShortcutProvider.ts +++ b/src/native/windows/WindowsShortcutProvider.ts @@ -3,7 +3,7 @@ import { parseLimitConstraint } from "../../domain/parseFailureAnalysisError.js" import { lstat, open } from "node:fs/promises"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, readStableArtifact, type StableArtifactFileSystem, type StableArtifactSnapshot, @@ -143,16 +143,12 @@ export class WindowsShortcutProvider implements WindowsShortcutPort { private readonly platform: NodeJS.Platform = HOST_PLATFORM.platform, ) {} - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - WINDOWS_SHORTCUT_PROVIDER.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + WINDOWS_SHORTCUT_PROVIDER, + OPERATION, + ); } async inspect( diff --git a/src/nuget/NugetNupkgProvider.ts b/src/nuget/NugetNupkgProvider.ts index 135bbd2baa..e0549cf39a 100644 --- a/src/nuget/NugetNupkgProvider.ts +++ b/src/nuget/NugetNupkgProvider.ts @@ -3,7 +3,7 @@ import { ArtifactReaderFailure, ArtifactResourceScope, artifactFilePrimaryFacts, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, translateArtifactFailure, withStableArtifactHandle, @@ -145,16 +145,12 @@ export class NugetNupkgProvider { readonly identity = NUGET_NUPKG_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - NUGET_NUPKG_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + NUGET_NUPKG_PROVIDER_IDENTITY, + OPERATION, + ); } async inspect( diff --git a/src/python/PythonPycProvider.ts b/src/python/PythonPycProvider.ts index d0a9ddf4c4..d0dfee76ce 100644 --- a/src/python/PythonPycProvider.ts +++ b/src/python/PythonPycProvider.ts @@ -1,7 +1,7 @@ import { PYTHON_PYC_PROVIDER_IDENTITY } from "./PythonPycProviderIdentity.js"; import { artifactFilePrimaryFacts, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, type StableArtifactSnapshot, readStableArtifact, @@ -108,16 +108,12 @@ export class PythonPycProvider implements PythonPycPort { readonly identity = PYTHON_PYC_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - PYTHON_PYC_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + PYTHON_PYC_PROVIDER_IDENTITY, + OPERATION, + ); } async inspect(input: InspectPythonPycInput, options?: ExecutionOptions) { diff --git a/src/resources/StableArtifactFailure.ts b/src/resources/StableArtifactFailure.ts index a898f79ef1..89fb446674 100644 --- a/src/resources/StableArtifactFailure.ts +++ b/src/resources/StableArtifactFailure.ts @@ -193,6 +193,19 @@ export const projectArtifactCloseFailure = ( return error; }; +/** Close a provider's descriptor scope and preserve its cleanup leases on failure. */ +export const closeArtifactResources = async ( + close: () => Promise, + provider: { readonly id: string }, + operation: string, +): Promise => { + try { + await close(); + } catch (cause: unknown) { + throw projectArtifactCloseFailure(cause, provider.id, operation); + } +}; + /** Preserve completed physical identity and exact cleanup ownership around a provider's primary failure. */ export const projectStableArtifactFailure = ( cause: unknown, diff --git a/src/resources/index.ts b/src/resources/index.ts index aff96e0973..d40a1310a8 100644 --- a/src/resources/index.ts +++ b/src/resources/index.ts @@ -97,6 +97,7 @@ export type { } from "./ArtifactResourceScope.js"; export { imageFileSizeRemediation, + closeArtifactResources, projectArtifactCloseFailure, projectOwnedArtifactIdentityFailure, projectOwnedArtifactInputFailure, diff --git a/src/rust/RustBinaryProvider.ts b/src/rust/RustBinaryProvider.ts index 4044891ed1..4cebc9109f 100644 --- a/src/rust/RustBinaryProvider.ts +++ b/src/rust/RustBinaryProvider.ts @@ -3,7 +3,7 @@ import { producerLimitControl } from "../domain/producerResourceLimits.js"; import { RUST_BINARY_PROVIDER_IDENTITY } from "./RustBinaryProviderIdentity.js"; import { projectStableArtifactFailure, - projectArtifactCloseFailure, + closeArtifactResources, withStableArtifactHandle, artifactFilePrimaryFacts, assertArtifactReadActive, @@ -66,16 +66,12 @@ export class RustBinaryProvider implements RustBinaryPort { readonly identity = RUST_BINARY_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - RUST_BINARY_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + RUST_BINARY_PROVIDER_IDENTITY, + OPERATION, + ); } /** Read one dependency section and any inferred rustc commit from the selected file. */ diff --git a/src/v8/V8CodeCacheProvider.ts b/src/v8/V8CodeCacheProvider.ts index 6dabdfce8e..a37394caf1 100644 --- a/src/v8/V8CodeCacheProvider.ts +++ b/src/v8/V8CodeCacheProvider.ts @@ -7,7 +7,7 @@ import { ArtifactReaderFailure, withStableArtifactHandle, artifactFilePrimaryFacts, - projectArtifactCloseFailure, + closeArtifactResources, projectStableArtifactFailure, } from "../resources/index.js"; import type { ExecutionOptions } from "../ports/AnalysisProvider.js"; @@ -35,16 +35,12 @@ export class V8CodeCacheProvider implements V8CodeCachePort { readonly identity = V8_CODE_CACHE_PROVIDER_IDENTITY; readonly #resources = new ArtifactResourceScope(); - async close(): Promise { - try { - await this.#resources.close(); - } catch (cause: unknown) { - throw projectArtifactCloseFailure( - cause, - V8_CODE_CACHE_PROVIDER_IDENTITY.id, - OPERATION, - ); - } + close(): Promise { + return closeArtifactResources( + () => this.#resources.close(), + V8_CODE_CACHE_PROVIDER_IDENTITY, + OPERATION, + ); } async inspect(input: InspectV8CodeCacheInput, options?: ExecutionOptions) {