@sussudio/platform
Version:
Internal APIs for VS Code's service injection the base services.
41 lines (40 loc) • 1.45 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { bufferToStream, streamToBuffer } from '@sussudio/base/common/buffer.mjs';
import { CancellationToken } from '@sussudio/base/common/cancellation.mjs';
export class RequestChannel {
service;
constructor(service) {
this.service = service;
}
listen(context, event) {
throw new Error('Invalid listen');
}
call(context, command, args, token = CancellationToken.None) {
switch (command) {
case 'request':
return this.service.request(args[0], token).then(async ({ res, stream }) => {
const buffer = await streamToBuffer(stream);
return [{ statusCode: res.statusCode, headers: res.headers }, buffer];
});
case 'resolveProxy':
return this.service.resolveProxy(args[0]);
}
throw new Error('Invalid call');
}
}
export class RequestChannelClient {
channel;
constructor(channel) {
this.channel = channel;
}
async request(options, token) {
const [res, buffer] = await this.channel.call('request', [options], token);
return { res, stream: bufferToStream(buffer) };
}
async resolveProxy(url) {
return this.channel.call('resolveProxy', [url]);
}
}