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'
test('fetches wrapper jars checksums', async () => {
let validChecksums = await checksums.fetchValidChecksums(false)
const validChecksums = await checksums.fetchValidChecksums(false)
expect(validChecksums.length).toBeGreaterThan(10)
})

View file

@ -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(

View file

@ -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
)

14
dist/index.js vendored
View file

@ -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);
});
});
});
}

View file

@ -4,9 +4,15 @@ import * as fs from 'fs'
export async function sha256File(path: string): Promise<string> {
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)
})
})
}