Skip to content

Commit

Permalink
Merge pull request #21 from CMSgov/close-stream-when-done
Browse files Browse the repository at this point in the history
Close stream after validation to avoid exhausting memory
  • Loading branch information
shaselton-usds authored Oct 3, 2024
2 parents 4e41ae3 + 4148bda commit 912c678
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -62,21 +64,21 @@ export async function validate(
}

async function validateFile(
filename: string,
inputStream: fs.ReadStream,
version: string,
format: FileFormat,
validatorOptions: CsvValidationOptions | JsonValidatorOptions
): Promise<ValidationResult | null> {
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
)
Expand Down

0 comments on commit 912c678

Please sign in to comment.