From 4148bda5c2261c53d7ceed482b99ff46c167b8d1 Mon Sep 17 00:00:00 2001 From: Mint Thompson Date: Tue, 10 Sep 2024 11:49:57 -0400 Subject: [PATCH] close stream after validation to avoid exhausting memory --- src/commands.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index c1e7479..212dcca 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -24,14 +24,16 @@ export async function validate( ) } + const inputStream = fs.createReadStream(filepath, "utf-8") const validationResult = await validateFile( - filepath, + inputStream, version, format as FileFormat, { maxErrors: options.errorLimit as number, } ) + inputStream.close() if (!validationResult) return const errors = validationResult.errors.filter(({ warning }) => !warning) @@ -62,7 +64,7 @@ export async function validate( } async function validateFile( - filename: string, + inputStream: fs.ReadStream, version: string, format: FileFormat, validatorOptions: CsvValidationOptions | JsonValidatorOptions @@ -70,13 +72,13 @@ async function validateFile( const schemaVersion = version as "v1.1" | "v2.0" if (format === "csv") { return await validateCsv( - fs.createReadStream(filename, "utf-8"), + inputStream, schemaVersion, validatorOptions as CsvValidationOptions ) } else if (format === "json") { return await validateJson( - fs.createReadStream(filename, "utf-8"), + inputStream, schemaVersion, validatorOptions as JsonValidatorOptions )