src/vs/workbench/api/test/common/testRPCProtocol.ts
173 LOC · 132 covered · 41 uncovered · 13 ranges · 64 concepts · 3 introducers · 34 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
testRPCProtocol.ts ×9
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isThenable } from '../../../../base/common/async.js';
import { CharCode } from '../../../../base/common/charCode.js';
import { IExtHostRpcService } from '../../common/extHostRpcService.js';
import { IExtHostContext } from '../../../services/extensions/common/extHostCustomers.js';
import { ExtensionHostKind } from '../../../services/extensions/common/extensionHostKind.js';
import { Proxied, ProxyIdentifier, SerializableObjectWithBuffers } from '../../../services/extensions/common/proxyIdentifier.js';
import { parseJsonAndRestoreBufferRefs, stringifyJsonWithBufferRefs } from '../../../services/extensions/common/rpcProtocol.js';
export function SingleProxyRPCProtocol(thing: any): IExtHostContext & IExtHostRpcService {
return {
_serviceBrand: undefined,
remoteAuthority: null!,
getProxy<T>(): T {
return thing;
},
set<T, R extends T>(identifier: ProxyIdentifier<T>, value: R): R {
return value;
},
dispose: undefined!,
assertRegistered: undefined!,
drain: undefined!,
extensionHostKind: ExtensionHostKind.LocalProcess
};
}
/** Makes a fake {@link SingleProxyRPCProtocol} on which any method can be called */
export function AnyCallRPCProtocol<T>(useCalls?: { [K in keyof T]: T[K] }) {
return SingleProxyRPCProtocol(new Proxy({}, {
get(_target, prop: string) {
if (useCalls && prop in useCalls) {
// eslint-disable-next-line local/code-no-any-casts
return (useCalls as any)[prop];
}
return () => Promise.resolve(undefined);
}
}));
}
export class TestRPCProtocol implements IExtHostContext, IExtHostRpcService {
public _serviceBrand: undefined;
public remoteAuthority = null!;
public extensionHostKind = ExtensionHostKind.LocalProcess;
private _callCountValue: number = 0;
private _idle?: Promise<any>;
private _completeIdle?: Function;
private readonly _locals: { [id: string]: any };
private readonly _proxies: { [id: string]: any };
constructor() {
this._locals = Object.create(null);
this._proxies = Object.create(null);
}
drain(): Promise<void> {
return Promise.resolve();
}
private get _callCount(): number {
return this._callCountValue;
}
private set _callCount(value: number) {
this._callCountValue = value;
if (this._callCountValue === 0) {
this._completeIdle?.();
this._idle = undefined;
}
}
sync(): Promise<any> {
setTimeout(c, 0);
}).then(() => {
if (this._callCount === 0) {
return undefined;
}
if (!this._idle) {
this._idle = new Promise<any>((c, e) => {
this._completeIdle = c;
});
}
return this._idle;
}
public getProxy<T>(identifier: ProxyIdentifier<T>): Proxied<T> {
if (!this._proxies[identifier.sid]) {
this._proxies[identifier.sid] = this._createProxy(identifier.sid);
}
return this._proxies[identifier.sid];
}
private _createProxy<T>(proxyId: string): T {
const handler = {
get: (target: any, name: PropertyKey) => {
if (typeof name === 'string' && !target[name] && name.charCodeAt(0) === CharCode.DollarSign) {
target[name] = (...myArgs: any[]) => {
return this._remoteCall(proxyId, name, myArgs);
};
}
return target[name];
}
};
return new Proxy(Object.create(null), handler);
}
public set<T, R extends T>(identifier: ProxyIdentifier<T>, value: R): R {
this._locals[identifier.sid] = value;
return value;
}
protected _remoteCall(proxyId: string, path: string, args: any[]): Promise<any> {
this._callCount++;
return new Promise<any>((c) => {
setTimeout(c, 0);
}).then(() => {
const instance = this._locals[proxyId];
// pretend the args went over the wire... (invoke .toJSON on objects...)
const wireArgs = simulateWireTransfer(args);
let p: Promise<any>;
try {
const result = (<Function>instance[path]).apply(instance, wireArgs);
p = isThenable(result) ? result : Promise.resolve(result);
} catch (err) {
p = Promise.reject(err);
}
return p.then(result => {
this._callCount--;
// pretend the result went over the wire... (invoke .toJSON on objects...)
const wireResult = simulateWireTransfer(result);
return wireResult;
}, err => {
this._callCount--;
return Promise.reject(err);
});
}
public dispose() { }
public assertRegistered(identifiers: ProxyIdentifier<any>[]): void {
throw new Error('Not implemented!');
}
function simulateWireTransfer<T>(obj: T): T {
if (!obj) {
return obj;
}
if (Array.isArray(obj)) {
// eslint-disable-next-line local/code-no-any-casts
return obj.map(simulateWireTransfer) as any;
}
if (obj instanceof SerializableObjectWithBuffers) {
const { jsonString, referencedBuffers } = stringifyJsonWithBufferRefs(obj);
return parseJsonAndRestoreBufferRefs(jsonString, referencedBuffers, null);
return JSON.parse(JSON.stringify(obj));
}