2020-06-25 16:05:36 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the 'License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { EventEmitter } from 'events';
|
2020-08-24 17:05:16 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2020-07-24 16:22:20 -07:00
|
|
|
import type { Connection } from './connection';
|
2020-08-24 17:05:16 -07:00
|
|
|
import type { Logger } from './types';
|
2020-08-22 15:13:51 -07:00
|
|
|
import { debugLogger } from '../utils/debugLogger';
|
2020-09-10 19:25:44 -07:00
|
|
|
import { rewriteErrorMessage } from '../utils/stackTrace';
|
2020-11-26 07:33:09 -08:00
|
|
|
import { createScheme, Validator, ValidationError } from '../protocol/validator';
|
2021-02-13 20:31:06 -08:00
|
|
|
import { StackFrame } from '../common/types';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
export abstract class ChannelOwner<T extends channels.Channel = channels.Channel, Initializer = {}> extends EventEmitter {
|
2020-07-10 15:11:47 -07:00
|
|
|
private _connection: Connection;
|
|
|
|
private _parent: ChannelOwner | undefined;
|
|
|
|
private _objects = new Map<string, ChannelOwner>();
|
|
|
|
|
2020-07-10 18:00:10 -07:00
|
|
|
readonly _type: string;
|
2020-07-10 15:11:47 -07:00
|
|
|
readonly _guid: string;
|
2020-06-25 16:05:36 -07:00
|
|
|
readonly _channel: T;
|
2020-06-26 12:28:27 -07:00
|
|
|
readonly _initializer: Initializer;
|
2020-08-24 17:05:16 -07:00
|
|
|
_logger: Logger | undefined;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-07-27 10:21:39 -07:00
|
|
|
constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: Initializer) {
|
2020-06-25 16:05:36 -07:00
|
|
|
super();
|
2021-02-09 09:00:00 -08:00
|
|
|
this.setMaxListeners(0);
|
2020-07-24 16:22:20 -07:00
|
|
|
this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
|
2020-07-10 18:00:10 -07:00
|
|
|
this._type = type;
|
2020-07-10 15:11:47 -07:00
|
|
|
this._guid = guid;
|
2020-07-24 16:22:20 -07:00
|
|
|
this._parent = parent instanceof ChannelOwner ? parent : undefined;
|
2020-07-10 15:11:47 -07:00
|
|
|
|
|
|
|
this._connection._objects.set(guid, this);
|
2020-07-10 18:00:10 -07:00
|
|
|
if (this._parent) {
|
2020-07-10 15:11:47 -07:00
|
|
|
this._parent._objects.set(guid, this);
|
2020-07-10 18:00:10 -07:00
|
|
|
this._logger = this._parent._logger;
|
|
|
|
}
|
2020-07-10 15:11:47 -07:00
|
|
|
|
2021-02-19 18:12:33 -08:00
|
|
|
this._channel = this._createChannel(new EventEmitter(), '');
|
2020-06-26 12:28:27 -07:00
|
|
|
this._initializer = initializer;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-07-10 15:11:47 -07:00
|
|
|
|
|
|
|
_dispose() {
|
|
|
|
// Clean up from parent and connection.
|
|
|
|
if (this._parent)
|
|
|
|
this._parent._objects.delete(this._guid);
|
|
|
|
this._connection._objects.delete(this._guid);
|
|
|
|
|
|
|
|
// Dispose all children.
|
2020-07-27 10:21:39 -07:00
|
|
|
for (const object of [...this._objects.values()])
|
|
|
|
object._dispose();
|
2020-07-10 15:11:47 -07:00
|
|
|
this._objects.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
_debugScopeState(): any {
|
|
|
|
return {
|
|
|
|
_guid: this._guid,
|
2020-07-27 10:21:39 -07:00
|
|
|
objects: Array.from(this._objects.values()).map(o => o._debugScopeState()),
|
2020-07-10 15:11:47 -07:00
|
|
|
};
|
|
|
|
}
|
2020-07-15 14:04:39 -07:00
|
|
|
|
2021-02-19 18:12:33 -08:00
|
|
|
_createChannel(base: Object, apiName: string): T {
|
|
|
|
const channel = new Proxy(base, {
|
|
|
|
get: (obj: any, prop) => {
|
|
|
|
if (prop === 'debugScopeState')
|
|
|
|
return (params: any) => this._connection.sendMessageToServer(this._guid, prop, params, apiName);
|
|
|
|
if (typeof prop === 'string') {
|
|
|
|
const validator = scheme[paramsName(this._type, prop)];
|
|
|
|
if (validator)
|
|
|
|
return (params: any) => this._connection.sendMessageToServer(this._guid, prop, validator(params, ''), apiName);
|
|
|
|
}
|
|
|
|
return obj[prop];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
(channel as any)._object = this;
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _wrapApiCall<R, C extends channels.Channel>(apiName: string, func: (channel: C) => Promise<R>, logger?: Logger): Promise<R> {
|
2020-07-15 14:04:39 -07:00
|
|
|
logger = logger || this._logger;
|
|
|
|
try {
|
2020-07-16 14:32:21 -07:00
|
|
|
logApiCall(logger, `=> ${apiName} started`);
|
2021-02-19 18:12:33 -08:00
|
|
|
const channel = this._createChannel({}, apiName);
|
|
|
|
const result = await func(channel as any);
|
2020-07-16 14:32:21 -07:00
|
|
|
logApiCall(logger, `<= ${apiName} succeeded`);
|
2020-07-15 14:04:39 -07:00
|
|
|
return result;
|
|
|
|
} catch (e) {
|
2020-07-16 14:32:21 -07:00
|
|
|
logApiCall(logger, `<= ${apiName} failed`);
|
2020-09-10 19:25:44 -07:00
|
|
|
rewriteErrorMessage(e, `${apiName}: ` + e.message);
|
2020-07-15 14:04:39 -07:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 14:41:33 -07:00
|
|
|
|
2021-04-18 17:02:34 -10:00
|
|
|
_waitForEventInfoBefore(waitId: string, apiName: string, stack: StackFrame[]) {
|
|
|
|
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { apiName, waitId, phase: 'before', stack } }, undefined).catch(() => {});
|
2021-02-13 20:31:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_waitForEventInfoAfter(waitId: string, error?: string) {
|
2021-02-19 18:12:33 -08:00
|
|
|
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { waitId, phase: 'after', error } }, undefined).catch(() => {});
|
2021-02-13 20:31:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_waitForEventInfoLog(waitId: string, message: string) {
|
2021-02-19 18:12:33 -08:00
|
|
|
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { waitId, phase: 'log', message } }, undefined).catch(() => {});
|
2021-02-13 20:31:06 -08:00
|
|
|
}
|
|
|
|
|
2020-08-16 14:41:33 -07:00
|
|
|
private toJSON() {
|
|
|
|
// Jest's expect library tries to print objects sometimes.
|
|
|
|
// RPC objects can contain links to lots of other objects,
|
|
|
|
// which can cause jest to crash. Let's help it out
|
|
|
|
// by just returning the important values.
|
|
|
|
return {
|
|
|
|
_type: this._type,
|
|
|
|
_guid: this._guid,
|
|
|
|
};
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-07-16 14:32:21 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
function logApiCall(logger: Logger | undefined, message: string) {
|
2020-07-16 14:32:21 -07:00
|
|
|
if (logger && logger.isEnabled('api', 'info'))
|
|
|
|
logger.log('api', 'info', message, [], { color: 'cyan' });
|
2020-08-17 14:12:31 -07:00
|
|
|
debugLogger.log('api', message);
|
2020-07-16 14:32:21 -07:00
|
|
|
}
|
2020-11-26 07:33:09 -08:00
|
|
|
|
|
|
|
function paramsName(type: string, method: string) {
|
|
|
|
return type + method[0].toUpperCase() + method.substring(1) + 'Params';
|
|
|
|
}
|
|
|
|
|
|
|
|
const tChannel = (name: string): Validator => {
|
|
|
|
return (arg: any, path: string) => {
|
|
|
|
if (arg._object instanceof ChannelOwner && (name === '*' || arg._object._type === name))
|
|
|
|
return { guid: arg._object._guid };
|
|
|
|
throw new ValidationError(`${path}: expected ${name}`);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const scheme = createScheme(tChannel);
|