Properly close file read stream

Signed-off-by: Paul Merlin <paul@gradle.com>
This commit is contained in:
Paul Merlin 2020-01-06 11:56:42 +01:00
parent bdf490fd65
commit 94a3288680
5 changed files with 23 additions and 11 deletions

View file

@ -1,6 +1,6 @@
import * as checksums from '../src/checksums' import * as checksums from '../src/checksums'
test('fetches wrapper jars checksums', async () => { test('fetches wrapper jars checksums', async () => {
let validChecksums = await checksums.fetchValidChecksums(false) const validChecksums = await checksums.fetchValidChecksums(false)
expect(validChecksums.length).toBeGreaterThan(10) expect(validChecksums.length).toBeGreaterThan(10)
}) })

View file

@ -2,7 +2,7 @@ import * as path from 'path'
import * as hash from '../src/hash' import * as hash from '../src/hash'
test('can sha256 files', async () => { test('can sha256 files', async () => {
let sha = await hash.sha256File( const sha = await hash.sha256File(
path.resolve('__tests__/data/invalid/gradle-wrapper.jar') path.resolve('__tests__/data/invalid/gradle-wrapper.jar')
) )
expect(sha).toEqual( expect(sha).toEqual(

View file

@ -2,7 +2,7 @@ import * as path from 'path'
import * as validate from '../src/validate' import * as validate from '../src/validate'
test('validates wrapper jars', async () => { test('validates wrapper jars', async () => {
let invalidWrapperJars = await validate.findInvalidWrapperJars( const invalidWrapperJars = await validate.findInvalidWrapperJars(
path.resolve('.'), path.resolve('.'),
false false
) )

14
dist/index.js vendored
View file

@ -19546,10 +19546,16 @@ function sha256File(path) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256'); const hash = crypto.createHash('sha256');
fs.createReadStream(path) const stream = fs.createReadStream(path);
.on('data', data => hash.update(data)) stream.on('data', data => hash.update(data));
.on('end', () => resolve(hash.digest('hex'))) stream.on('end', () => {
.on('error', error => reject(error)); stream.destroy();
resolve(hash.digest('hex'));
});
stream.on('error', error => {
stream.destroy();
reject(error);
});
}); });
}); });
} }

View file

@ -4,9 +4,15 @@ import * as fs from 'fs'
export async function sha256File(path: string): Promise<string> { export async function sha256File(path: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256') const hash = crypto.createHash('sha256')
fs.createReadStream(path) const stream = fs.createReadStream(path)
.on('data', data => hash.update(data)) stream.on('data', data => hash.update(data))
.on('end', () => resolve(hash.digest('hex'))) stream.on('end', () => {
.on('error', error => reject(error)) stream.destroy()
resolve(hash.digest('hex'))
})
stream.on('error', error => {
stream.destroy()
reject(error)
})
}) })
} }