From 94a3288680a71fc92cc6c5e565d8f77bfa60ec58 Mon Sep 17 00:00:00 2001 From: Paul Merlin Date: Mon, 6 Jan 2020 11:56:42 +0100 Subject: [PATCH] Properly close file read stream Signed-off-by: Paul Merlin --- __tests__/checksums.test.ts | 2 +- __tests__/hash.test.ts | 2 +- __tests__/validate.test.ts | 2 +- dist/index.js | 14 ++++++++++---- src/hash.ts | 14 ++++++++++---- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/__tests__/checksums.test.ts b/__tests__/checksums.test.ts index f5dfbbd..aa27208 100644 --- a/__tests__/checksums.test.ts +++ b/__tests__/checksums.test.ts @@ -1,6 +1,6 @@ import * as checksums from '../src/checksums' test('fetches wrapper jars checksums', async () => { - let validChecksums = await checksums.fetchValidChecksums(false) + const validChecksums = await checksums.fetchValidChecksums(false) expect(validChecksums.length).toBeGreaterThan(10) }) diff --git a/__tests__/hash.test.ts b/__tests__/hash.test.ts index ec0c7a3..01c0675 100644 --- a/__tests__/hash.test.ts +++ b/__tests__/hash.test.ts @@ -2,7 +2,7 @@ import * as path from 'path' import * as hash from '../src/hash' test('can sha256 files', async () => { - let sha = await hash.sha256File( + const sha = await hash.sha256File( path.resolve('__tests__/data/invalid/gradle-wrapper.jar') ) expect(sha).toEqual( diff --git a/__tests__/validate.test.ts b/__tests__/validate.test.ts index e81121e..838df46 100644 --- a/__tests__/validate.test.ts +++ b/__tests__/validate.test.ts @@ -2,7 +2,7 @@ import * as path from 'path' import * as validate from '../src/validate' test('validates wrapper jars', async () => { - let invalidWrapperJars = await validate.findInvalidWrapperJars( + const invalidWrapperJars = await validate.findInvalidWrapperJars( path.resolve('.'), false ) diff --git a/dist/index.js b/dist/index.js index bdcc7f4..2772691 100644 --- a/dist/index.js +++ b/dist/index.js @@ -19546,10 +19546,16 @@ function sha256File(path) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const hash = crypto.createHash('sha256'); - fs.createReadStream(path) - .on('data', data => hash.update(data)) - .on('end', () => resolve(hash.digest('hex'))) - .on('error', error => reject(error)); + const stream = fs.createReadStream(path); + stream.on('data', data => hash.update(data)); + stream.on('end', () => { + stream.destroy(); + resolve(hash.digest('hex')); + }); + stream.on('error', error => { + stream.destroy(); + reject(error); + }); }); }); } diff --git a/src/hash.ts b/src/hash.ts index e1f76a6..101e8a3 100644 --- a/src/hash.ts +++ b/src/hash.ts @@ -4,9 +4,15 @@ import * as fs from 'fs' export async function sha256File(path: string): Promise { return new Promise((resolve, reject) => { const hash = crypto.createHash('sha256') - fs.createReadStream(path) - .on('data', data => hash.update(data)) - .on('end', () => resolve(hash.digest('hex'))) - .on('error', error => reject(error)) + const stream = fs.createReadStream(path) + stream.on('data', data => hash.update(data)) + stream.on('end', () => { + stream.destroy() + resolve(hash.digest('hex')) + }) + stream.on('error', error => { + stream.destroy() + reject(error) + }) }) }