Skip to content

Commit

Permalink
Implement fallback hashing algorithm when crypto module is not availa…
Browse files Browse the repository at this point in the history
…ble (#19941)

* Implement fallback hashing algorithm when crypto module is not available

* Fix lint errors

* Expose method internally and use in watch.ts

* Simplify syntax; Remove fallback from watch.ts
  • Loading branch information
bttf authored and mhegazy committed Jan 26, 2018
1 parent b80081d commit 9677b06
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,32 @@ namespace ts {
const _fs = require("fs");
const _path = require("path");
const _os = require("os");
const _crypto = require("crypto");
// crypto can be absent on reduced node installations
let _crypto: any;
try {
_crypto = require("crypto");
}
catch {
_crypto = undefined;
}

const useNonPollingWatchers = process.env.TSC_NONPOLLING_WATCHER;

/**
* djb2 hashing algorithm
* http://www.cse.yorku.ca/~oz/hash.html
*/
function generateDjb2Hash(data: string): string {
const chars = data.split("").map(str => str.charCodeAt(0));
return `${chars.reduce((prev, curr) => ((prev << 5) + prev) + curr, 5381)}`;
}

function createMD5HashUsingNativeCrypto(data: string) {
const hash = _crypto.createHash("md5");
hash.update(data);
return hash.digest("hex");
}

function createWatchedFileSet() {
const dirWatchers = createMap<DirectoryWatcher>();
// One file can have multiple watchers
Expand Down Expand Up @@ -493,11 +515,7 @@ namespace ts {
return undefined;
}
},
createHash(data) {
const hash = _crypto.createHash("md5");
hash.update(data);
return hash.digest("hex");
},
createHash: _crypto ? createMD5HashUsingNativeCrypto : generateDjb2Hash,
getMemoryUsage() {
if (global.gc) {
global.gc();
Expand Down

0 comments on commit 9677b06

Please sign in to comment.