提交 baab2e68 编写于 作者: S Sandeep Somavarapu

fix #74887

上级 97ce6386
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export function createSHA1(content: string): Thenable<string> {
if (typeof require !== 'undefined') {
const _crypto: typeof crypto = require.__$__nodeRequire('crypto');
return Promise.resolve(_crypto['createHash']('sha1').update(content).digest('hex'));
}
return crypto.subtle.digest('SHA-1', new TextEncoder().encode(content)).then(buffer => {
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#Converting_a_digest_to_a_hex_string
return Array.prototype.map.call(new Uint8Array(buffer), (value: number) => `00${value.toString(16)}`.slice(-2)).join('');
});
}
\ No newline at end of file
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { createSHA1 } from 'vs/base/browser/hash';
suite('Hash', () => {
test('computeSHA1Hash', async () => {
assert.equal(await createSHA1(''), 'da39a3ee5e6b4b0d3255bfef95601890afd80709');
assert.equal(await createSHA1('hello world'), '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed');
assert.equal(await createSHA1('da39a3ee5e6b4b0d3255bfef95601890afd80709'), '10a34637ad661d98ba3344717656fcc76209c2f8');
assert.equal(await createSHA1('2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'), 'd6b0d82cea4269b51572b8fab43adcee9fc3cf9a');
assert.equal(await createSHA1('öäü_?ß()<>ÖÄÜ'), 'b64beaeff9e317b0193c8e40a2431b210388eba9');
});
});
\ No newline at end of file
......@@ -21,8 +21,8 @@ import { extname, join } from 'vs/base/common/path';
import { equals } from 'vs/base/common/objects';
import { Schemas } from 'vs/base/common/network';
import { IConfigurationModel, compare } from 'vs/platform/configuration/common/configuration';
import { createSHA1 } from 'vs/base/browser/hash';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
import { hash } from 'vs/base/common/hash';
export class RemoteUserConfiguration extends Disposable {
......@@ -672,7 +672,7 @@ class CachedFolderConfiguration extends Disposable implements IFolderConfigurati
readonly onDidChange: Event<void> = this._onDidChange.event;
private configurationModel: ConfigurationModel;
private readonly key: Thenable<ConfigurationKey>;
private readonly key: ConfigurationKey;
constructor(
folder: URI,
......@@ -680,14 +680,13 @@ class CachedFolderConfiguration extends Disposable implements IFolderConfigurati
private readonly configurationCache: IConfigurationCache
) {
super();
this.key = createSHA1(join(folder.path, configFolderRelativePath)).then(key => ({ type: 'folder', key }));
this.key = { type: 'folder', key: hash(join(folder.path, configFolderRelativePath)).toString(16) };
this.configurationModel = new ConfigurationModel();
}
async loadConfiguration(): Promise<ConfigurationModel> {
try {
const key = await this.key;
const contents = await this.configurationCache.read(key);
const contents = await this.configurationCache.read(this.key);
const parsed: IConfigurationModel = JSON.parse(contents.toString());
this.configurationModel = new ConfigurationModel(parsed.contents, parsed.keys, parsed.overrides);
} catch (e) {
......@@ -696,11 +695,10 @@ class CachedFolderConfiguration extends Disposable implements IFolderConfigurati
}
async updateConfiguration(configurationModel: ConfigurationModel): Promise<void> {
const key = await this.key;
if (configurationModel.keys.length) {
await this.configurationCache.write(key, JSON.stringify(configurationModel.toJSON()));
await this.configurationCache.write(this.key, JSON.stringify(configurationModel.toJSON()));
} else {
await this.configurationCache.remove(key);
await this.configurationCache.remove(this.key);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册