add machine id header

This commit is contained in:
Sandeep Somavarapu
2021-07-20 09:37:24 +02:00
parent 8aea156be8
commit ecbab7858e
2 changed files with 58 additions and 28 deletions

View File

@@ -8,14 +8,28 @@ import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IFileService } from 'vs/platform/files/common/files';
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
import { FileAccess, Schemas } from 'vs/base/common/network';
import { IProductService } from 'vs/platform/product/common/productService';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IHeaders } from 'vs/base/parts/request/common/request';
import { getServiceMachineId } from 'vs/platform/serviceMachineId/common/serviceMachineId';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
class ExtensionResourceLoaderService implements IExtensionResourceLoaderService {
declare readonly _serviceBrand: undefined;
private readonly _extensionGalleryResourceAuthority: string | undefined;
constructor(
@IFileService private readonly _fileService: IFileService
) { }
@IFileService private readonly _fileService: IFileService,
@IProductService productService: IProductService,
@IStorageService private readonly _storageService: IStorageService,
@IEnvironmentService private readonly _environmentService: IEnvironmentService,
) {
if (productService.extensionsGallery) {
this._extensionGalleryResourceAuthority = this._getExtensionResourceAuthority(URI.parse(productService.extensionsGallery.resourceUrlTemplate));
}
}
async readExtensionResource(uri: URI): Promise<string> {
uri = FileAccess.asBrowserUri(uri);
@@ -25,13 +39,32 @@ class ExtensionResourceLoaderService implements IExtensionResourceLoaderService
return result.value.toString();
}
const response = await fetch(uri.toString(true));
let headers: IHeaders = {};
if (this._extensionGalleryResourceAuthority && this._extensionGalleryResourceAuthority === this._getExtensionResourceAuthority(uri)) {
const machineId = await this._getServiceMachineId();
headers['X-Machine-Id'] = machineId;
}
const response = await fetch(uri.toString(true), { headers });
if (response.status !== 200) {
throw new Error(response.statusText);
}
return response.text();
}
private _serviceMachineIdPromise: Promise<string> | undefined;
private _getServiceMachineId(): Promise<string> {
if (!this._serviceMachineIdPromise) {
this._serviceMachineIdPromise = getServiceMachineId(this._environmentService, this._fileService, this._storageService);
}
return this._serviceMachineIdPromise;
}
private _getExtensionResourceAuthority(uri: URI): string | undefined {
const index = uri.authority.indexOf('.');
return index !== -1 ? uri.authority.substring(index + 1) : undefined;
}
}
registerSingleton(IExtensionResourceLoaderService, ExtensionResourceLoaderService);