Skip to content

Use SHA256 hash for project info telemetry #24099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,11 @@ namespace ts {
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
getModifiedTime?(path: string): Date;
/**
* This should be cryptographically secure.
* A good implementation is node.js' `crypto.createHash`. (https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm)
*/
createHash?(data: string): string;
/** This must be cryptographically secure. Only implement this method using `crypto.createHash("sha256")`. */
createSHA256Hash?(data: string): string;
getMemoryUsage?(): number;
exit(exitCode?: number): void;
realpath?(path: string): string;
Expand Down Expand Up @@ -527,7 +528,7 @@ namespace ts {
const _path = require("path");
const _os = require("os");
// crypto can be absent on reduced node installations
let _crypto: any;
let _crypto: typeof import("crypto");
try {
_crypto = require("crypto");
}
Expand Down Expand Up @@ -590,6 +591,7 @@ namespace ts {
readDirectory,
getModifiedTime,
createHash: _crypto ? createMD5HashUsingNativeCrypto : generateDjb2Hash,
createSHA256Hash: _crypto ? createSHA256Hash : undefined,
getMemoryUsage() {
if (global.gc) {
global.gc();
Expand Down Expand Up @@ -1072,11 +1074,17 @@ namespace ts {
return `${chars.reduce((prev, curr) => ((prev << 5) + prev) + curr, 5381)}`;
}

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

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

function getChakraSystem(): System {
Expand Down
9 changes: 3 additions & 6 deletions src/harness/unittests/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ namespace ts.projectSystem {

// TODO: Apparently compilerOptions is mutated, so have to repeat it here!
et.assertProjectInfoTelemetryEvent({
projectId: Harness.mockHash("/hunter2/foo.csproj"),
compilerOptions: { strict: true },
compileOnSave: true,
// These properties can't be present for an external project, so they are undefined instead of false.
Expand All @@ -69,7 +68,7 @@ namespace ts.projectSystem {
exclude: undefined,
configFileName: "other",
projectType: "external",
});
}, "/hunter2/foo.csproj");

// Also test that opening an external project only sends an event once.

Expand Down Expand Up @@ -202,7 +201,6 @@ namespace ts.projectSystem {
const et = new TestServerEventManager([jsconfig, file]);
et.service.openClientFile(file.path);
et.assertProjectInfoTelemetryEvent({
projectId: Harness.mockHash("/jsconfig.json"),
fileStats: fileStats({ js: 1 }),
compilerOptions: autoJsCompilerOptions,
typeAcquisition: {
Expand All @@ -211,7 +209,7 @@ namespace ts.projectSystem {
exclude: false,
},
configFileName: "jsconfig.json",
});
}, "/jsconfig.json");
});

it("detects whether language service was disabled", () => {
Expand All @@ -222,7 +220,6 @@ namespace ts.projectSystem {
et.service.openClientFile(file.path);
et.getEvent<server.ProjectLanguageServiceStateEvent>(server.ProjectLanguageServiceStateEvent);
et.assertProjectInfoTelemetryEvent({
projectId: Harness.mockHash("/jsconfig.json"),
fileStats: fileStats({ js: 1 }),
compilerOptions: autoJsCompilerOptions,
configFileName: "jsconfig.json",
Expand All @@ -232,7 +229,7 @@ namespace ts.projectSystem {
exclude: false,
},
languageServiceEnabled: false,
});
}, "/jsconfig.json");
});

describe("open files telemetry", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ namespace ts.projectSystem {
assert.equal(eventData.triggerFile, triggerFile);
}

assertProjectInfoTelemetryEvent(partial: Partial<server.ProjectInfoTelemetryEventData>, configFile?: string): void {
assertProjectInfoTelemetryEvent(partial: Partial<server.ProjectInfoTelemetryEventData>, configFile = "/tsconfig.json"): void {
assert.deepEqual<server.ProjectInfoTelemetryEventData>(this.getEvent<server.ProjectInfoTelemetryEvent>(server.ProjectInfoTelemetryEvent), {
projectId: Harness.mockHash(configFile || "/tsconfig.json"),
projectId: sys.createSHA256Hash(configFile),
fileStats: fileStats({ ts: 1 }),
compilerOptions: {},
extends: false,
Expand Down
4 changes: 4 additions & 0 deletions src/harness/virtualFileSystemWithWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ interface Array<T> {}`
return Harness.mockHash(s);
}

createSHA256Hash(s: string): string {
return sys.createSHA256Hash(s);
}

watchFile(fileName: string, cb: FileWatcherCallback, pollingInterval: number) {
if (this.dynamicPriorityWatchFile) {
return this.dynamicPriorityWatchFile(fileName, cb, pollingInterval);
Expand Down
4 changes: 2 additions & 2 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,12 +1429,12 @@ namespace ts.server {
}
this.seenProjects.set(projectKey, true);

if (!this.eventHandler) {
if (!this.eventHandler || !this.host.createSHA256Hash) {
return;
}

const data: ProjectInfoTelemetryEventData = {
projectId: this.host.createHash(projectKey),
projectId: this.host.createSHA256Hash(projectKey),
fileStats: countEachFileTypes(project.getScriptInfos()),
compilerOptions: convertCompilerOptionsForTelemetry(project.getCompilationSettings()),
typeAcquisition: convertTypeAcquisition(project.getTypeAcquisition()),
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2980,10 +2980,11 @@ declare namespace ts {
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
getModifiedTime?(path: string): Date;
/**
* This should be cryptographically secure.
* A good implementation is node.js' `crypto.createHash`. (https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm)
*/
createHash?(data: string): string;
/** This must be cryptographically secure. Only implement this method using `crypto.createHash("sha256")`. */
createSHA256Hash?(data: string): string;
getMemoryUsage?(): number;
exit(exitCode?: number): void;
realpath?(path: string): string;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2980,10 +2980,11 @@ declare namespace ts {
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
getModifiedTime?(path: string): Date;
/**
* This should be cryptographically secure.
* A good implementation is node.js' `crypto.createHash`. (https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm)
*/
createHash?(data: string): string;
/** This must be cryptographically secure. Only implement this method using `crypto.createHash("sha256")`. */
createSHA256Hash?(data: string): string;
getMemoryUsage?(): number;
exit(exitCode?: number): void;
realpath?(path: string): string;
Expand Down