@@ -19,6 +19,7 @@ import Database from 'better-sqlite3';
1919import path from 'path' ;
2020import fs from 'fs' ;
2121import crypto from 'crypto' ;
22+ import os from 'os' ;
2223import { SnapValidator } from '../validation/snapValidator' ;
2324import { ValidationResult } from '../validation/validationTypes' ;
2425import { ProcessorInput } from '../utils/io' ;
@@ -104,13 +105,20 @@ class SnapProcessor extends BaseProcessor {
104105 async loadIntoTree ( filePathOrBuffer : ProcessorInput ) : Promise < AACTree > {
105106 await Promise . resolve ( ) ;
106107 const tree = new AACTree ( ) ;
108+ let tempDir : string | null = null ;
107109 const filePath =
108- typeof filePathOrBuffer === 'string'
110+ typeof filePathOrBuffer !== 'string'
111+ ? ( ( ) => {
112+ tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'snap-' ) ) ;
113+ return path . join ( tempDir , 'input.spb' ) ;
114+ } ) ( )
115+ : filePathOrBuffer ;
116+
117+ if ( typeof filePathOrBuffer !== 'string' ) {
118+ const buffer = Buffer . isBuffer ( filePathOrBuffer )
109119 ? filePathOrBuffer
110- : path . join ( process . cwd ( ) , 'temp.spb' ) ;
111-
112- if ( Buffer . isBuffer ( filePathOrBuffer ) ) {
113- fs . writeFileSync ( filePath , filePathOrBuffer ) ;
120+ : Buffer . from ( filePathOrBuffer ) ;
121+ fs . writeFileSync ( filePath , buffer ) ;
114122 }
115123
116124 let db : any = null ;
@@ -722,11 +730,11 @@ class SnapProcessor extends BaseProcessor {
722730 }
723731
724732 // Clean up temporary file if created from buffer
725- if ( Buffer . isBuffer ( filePathOrBuffer ) && fs . existsSync ( filePath ) ) {
733+ if ( tempDir && fs . existsSync ( tempDir ) ) {
726734 try {
727- fs . unlinkSync ( filePath ) ;
735+ fs . rmSync ( tempDir , { recursive : true , force : true } ) ;
728736 } catch ( e ) {
729- console . warn ( 'Failed to clean up temporary file :' , e ) ;
737+ console . warn ( 'Failed to clean up temporary files :' , e ) ;
730738 }
731739 }
732740 }
0 commit comments