mirror of
https://github.com/coder/code-server.git
synced 2026-05-08 05:17:27 +02:00
Merge commit 'be3e8236086165e5e45a5a10783823874b3f3ebd' as 'lib/vscode'
This commit is contained in:
108
lib/vscode/src/vs/platform/request/common/request.ts
Normal file
108
lib/vscode/src/vs/platform/request/common/request.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { localize } from 'vs/nls';
|
||||
import { CancellationToken } from 'vs/base/common/cancellation';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { IConfigurationRegistry, Extensions } from 'vs/platform/configuration/common/configurationRegistry';
|
||||
import { Registry } from 'vs/platform/registry/common/platform';
|
||||
import { streamToBuffer } from 'vs/base/common/buffer';
|
||||
import { IRequestOptions, IRequestContext } from 'vs/base/parts/request/common/request';
|
||||
|
||||
export const IRequestService = createDecorator<IRequestService>('requestService');
|
||||
|
||||
export interface IRequestService {
|
||||
readonly _serviceBrand: undefined;
|
||||
|
||||
request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext>;
|
||||
|
||||
resolveProxy(url: string): Promise<string | undefined>;
|
||||
}
|
||||
|
||||
export function isSuccess(context: IRequestContext): boolean {
|
||||
return (context.res.statusCode && context.res.statusCode >= 200 && context.res.statusCode < 300) || context.res.statusCode === 1223;
|
||||
}
|
||||
|
||||
function hasNoContent(context: IRequestContext): boolean {
|
||||
return context.res.statusCode === 204;
|
||||
}
|
||||
|
||||
export async function asText(context: IRequestContext): Promise<string | null> {
|
||||
if (!isSuccess(context)) {
|
||||
throw new Error('Server returned ' + context.res.statusCode);
|
||||
}
|
||||
if (hasNoContent(context)) {
|
||||
return null;
|
||||
}
|
||||
const buffer = await streamToBuffer(context.stream);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
export async function asJson<T = {}>(context: IRequestContext): Promise<T | null> {
|
||||
if (!isSuccess(context)) {
|
||||
throw new Error('Server returned ' + context.res.statusCode);
|
||||
}
|
||||
if (hasNoContent(context)) {
|
||||
return null;
|
||||
}
|
||||
const buffer = await streamToBuffer(context.stream);
|
||||
const str = buffer.toString();
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch (err) {
|
||||
err.message += ':\n' + str;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export interface IHTTPConfiguration {
|
||||
http?: {
|
||||
proxy?: string;
|
||||
proxyStrictSSL?: boolean;
|
||||
proxyAuthorization?: string;
|
||||
};
|
||||
}
|
||||
|
||||
Registry.as<IConfigurationRegistry>(Extensions.Configuration)
|
||||
.registerConfiguration({
|
||||
id: 'http',
|
||||
order: 15,
|
||||
title: localize('httpConfigurationTitle', "HTTP"),
|
||||
type: 'object',
|
||||
properties: {
|
||||
'http.proxy': {
|
||||
type: 'string',
|
||||
pattern: '^https?://([^:]*(:[^@]*)?@)?([^:]+|\\[[:0-9a-fA-F]+\\])(:\\d+)?/?$|^$',
|
||||
markdownDescription: localize('proxy', "The proxy setting to use. If not set, will be inherited from the `http_proxy` and `https_proxy` environment variables.")
|
||||
},
|
||||
'http.proxyStrictSSL': {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
description: localize('strictSSL', "Controls whether the proxy server certificate should be verified against the list of supplied CAs.")
|
||||
},
|
||||
'http.proxyAuthorization': {
|
||||
type: ['null', 'string'],
|
||||
default: null,
|
||||
markdownDescription: localize('proxyAuthorization', "The value to send as the `Proxy-Authorization` header for every network request.")
|
||||
},
|
||||
'http.proxySupport': {
|
||||
type: 'string',
|
||||
enum: ['off', 'on', 'override'],
|
||||
enumDescriptions: [
|
||||
localize('proxySupportOff', "Disable proxy support for extensions."),
|
||||
localize('proxySupportOn', "Enable proxy support for extensions."),
|
||||
localize('proxySupportOverride', "Enable proxy support for extensions, override request options."),
|
||||
],
|
||||
default: 'override',
|
||||
description: localize('proxySupport', "Use the proxy support for extensions.")
|
||||
},
|
||||
'http.systemCertificates': {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
description: localize('systemCertificates', "Controls whether CA certificates should be loaded from the OS. (On Windows and macOS a reload of the window is required after turning this off.)")
|
||||
}
|
||||
}
|
||||
});
|
||||
56
lib/vscode/src/vs/platform/request/common/requestIpc.ts
Normal file
56
lib/vscode/src/vs/platform/request/common/requestIpc.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc';
|
||||
import { Event } from 'vs/base/common/event';
|
||||
import { IRequestService } from 'vs/platform/request/common/request';
|
||||
import { IRequestOptions, IRequestContext, IHeaders } from 'vs/base/parts/request/common/request';
|
||||
import { CancellationToken } from 'vs/base/common/cancellation';
|
||||
import { VSBuffer, bufferToStream, streamToBuffer } from 'vs/base/common/buffer';
|
||||
|
||||
type RequestResponse = [
|
||||
{
|
||||
headers: IHeaders;
|
||||
statusCode?: number;
|
||||
},
|
||||
VSBuffer
|
||||
];
|
||||
|
||||
export class RequestChannel implements IServerChannel {
|
||||
|
||||
constructor(private readonly service: IRequestService) { }
|
||||
|
||||
listen(context: any, event: string): Event<any> {
|
||||
throw new Error('Invalid listen');
|
||||
}
|
||||
|
||||
call(context: any, command: string, args?: any): Promise<any> {
|
||||
switch (command) {
|
||||
case 'request': return this.service.request(args[0], CancellationToken.None)
|
||||
.then(async ({ res, stream }) => {
|
||||
const buffer = await streamToBuffer(stream);
|
||||
return <RequestResponse>[{ statusCode: res.statusCode, headers: res.headers }, buffer];
|
||||
});
|
||||
}
|
||||
throw new Error('Invalid call');
|
||||
}
|
||||
}
|
||||
|
||||
export class RequestChannelClient {
|
||||
|
||||
declare readonly _serviceBrand: undefined;
|
||||
|
||||
constructor(private readonly channel: IChannel) { }
|
||||
|
||||
async request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
|
||||
return RequestChannelClient.request(this.channel, options, token);
|
||||
}
|
||||
|
||||
static async request(channel: IChannel, options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
|
||||
const [res, buffer] = await channel.call<RequestResponse>('request', [options]);
|
||||
return { res, stream: bufferToStream(buffer) };
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user