sussudio
Version:
An unofficial VS Code Internal API
40 lines (39 loc) • 1.6 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 "../../../base/common/buffer.mjs";
import { CancellationToken } from "../../../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]);
}
}