Skip to content

Commit c6cada9

Browse files
author
Benjamin Pasero
committed
crypto - asyncify checkum method and add tests
1 parent 1562816 commit c6cada9

3 files changed

Lines changed: 35 additions & 38 deletions

File tree

src/vs/base/node/crypto.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import * as fs from 'fs';
77
import * as crypto from 'crypto';
88
import { once } from 'vs/base/common/functional';
99

10-
export function checksum(path: string, sha1hash: string | undefined): Promise<void> {
11-
const promise = new Promise<string | undefined>((c, e) => {
10+
export async function checksum(path: string, sha1hash: string | undefined): Promise<void> {
11+
const checksumPromise = new Promise<string | undefined>((resolve, reject) => {
1212
const input = fs.createReadStream(path);
1313
const hash = crypto.createHash('sha1');
1414
input.pipe(hash);
@@ -18,9 +18,9 @@ export function checksum(path: string, sha1hash: string | undefined): Promise<vo
1818
hash.removeAllListeners();
1919

2020
if (err) {
21-
e(err);
21+
reject(err);
2222
} else {
23-
c(result);
23+
resolve(result);
2424
}
2525
});
2626

@@ -30,11 +30,9 @@ export function checksum(path: string, sha1hash: string | undefined): Promise<vo
3030
hash.once('data', (data: Buffer) => done(undefined, data.toString('hex')));
3131
});
3232

33-
return promise.then(hash => {
34-
if (hash !== sha1hash) {
35-
return Promise.reject(new Error('Hash mismatch'));
36-
}
33+
const hash = await checksumPromise;
3734

38-
return Promise.resolve();
39-
});
35+
if (hash !== sha1hash) {
36+
throw new Error('Hash mismatch');
37+
}
4038
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { checksum } from 'vs/base/node/crypto';
7+
import { generateUuid } from 'vs/base/common/uuid';
8+
import { join } from 'vs/base/common/path';
9+
import { tmpdir } from 'os';
10+
import { mkdirp, rimraf, RimRafMode, writeFile } from 'vs/base/node/pfs';
11+
12+
suite('Crypto', () => {
13+
14+
test('checksum', async () => {
15+
const id = generateUuid();
16+
const testDir = join(tmpdir(), 'vsctests', id);
17+
const testFile = join(testDir, 'checksum.txt');
18+
19+
await mkdirp(testDir);
20+
21+
await writeFile(testFile, 'Hello World');
22+
23+
await checksum(testFile, '0a4d55a8d778e5022fab701977c5d840bbc486d0');
24+
25+
await rimraf(testDir, RimRafMode.MOVE);
26+
});
27+
});

src/vs/base/test/node/utils.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)