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+ 'use strict' ;
7+
8+ import * as fs from 'fs' ;
9+ import * as crypto from 'crypto' ;
10+ import * as stream from 'stream' ;
11+ import { TPromise } from 'vs/base/common/winjs.base' ;
12+ import { once } from 'vs/base/common/async' ;
13+
14+ export function checksum ( path : string , sha1hash : string ) : TPromise < void > {
15+ const promise = new TPromise < string > ( ( c , e ) => {
16+ const input = fs . createReadStream ( path ) ;
17+ const hash = crypto . createHash ( 'sha1' ) ;
18+ const hashStream = hash as any as stream . PassThrough ;
19+ input . pipe ( hashStream ) ;
20+
21+ const done = once ( ( err ?: Error , result ?: string ) => {
22+ input . removeAllListeners ( ) ;
23+ hashStream . removeAllListeners ( ) ;
24+
25+ if ( err ) {
26+ e ( err ) ;
27+ } else {
28+ c ( result ) ;
29+ }
30+ } ) ;
31+
32+ input . once ( 'error' , done ) ;
33+ input . once ( 'end' , done ) ;
34+ hashStream . once ( 'error' , done ) ;
35+ hashStream . once ( 'data' , data => done ( null , data . toString ( 'hex' ) ) ) ;
36+ } ) ;
37+
38+ return promise . then ( hash => {
39+ if ( hash !== sha1hash ) {
40+ return TPromise . wrapError < void > ( new Error ( 'Hash mismatch' ) ) ;
41+ }
42+
43+ return TPromise . as ( null ) ;
44+ } ) ;
45+ }
0 commit comments