2020-06-26 01:05:36 +02: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-25 02:05:16 +02:00
|
|
|
import * as channels from '../protocol/channels';
|
2021-04-22 19:05:37 +02:00
|
|
|
import { createScheme, ValidationError, Validator } from '../protocol/validator';
|
2020-08-23 00:13:51 +02:00
|
|
|
import { debugLogger } from '../utils/debugLogger';
|
2021-06-28 22:27:38 +02:00
|
|
|
import { captureStackTrace, ParsedStackTrace } from '../utils/stackTrace';
|
|
|
|
|
import { isUnderTest } from '../utils/utils';
|
2021-04-22 19:05:37 +02:00
|
|
|
import type { Connection } from './connection';
|
2021-09-14 03:07:15 +02:00
|
|
|
import type { ClientSideInstrumentation, Logger } from './types';
|
2021-07-31 01:07:02 +02:00
|
|
|
|
2020-08-25 02:05:16 +02:00
|
|
|
export abstract class ChannelOwner<T extends channels.Channel = channels.Channel, Initializer = {}> extends EventEmitter {
|
2021-10-26 03:56:57 +02:00
|
|
|
readonly _connection: Connection;
|
2020-07-11 00:11:47 +02:00
|
|
|
private _parent: ChannelOwner | undefined;
|
|
|
|
|
private _objects = new Map<string, ChannelOwner>();
|
|
|
|
|
|
2020-07-11 03:00:10 +02:00
|
|
|
readonly _type: string;
|
2020-07-11 00:11:47 +02:00
|
|
|
readonly _guid: string;
|
2020-06-26 01:05:36 +02:00
|
|
|
readonly _channel: T;
|
2020-06-26 21:28:27 +02:00
|
|
|
readonly _initializer: Initializer;
|
2020-08-25 02:05:16 +02:00
|
|
|
_logger: Logger | undefined;
|
2021-07-31 01:07:02 +02:00
|
|
|
_csi: ClientSideInstrumentation | undefined;
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2020-07-27 19:21:39 +02:00
|
|
|
constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: Initializer) {
|
2020-06-26 01:05:36 +02:00
|
|
|
super();
|
2021-02-09 18:00:00 +01:00
|
|
|
this.setMaxListeners(0);
|
2020-07-25 01:22:20 +02:00
|
|
|
this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
|
2020-07-11 03:00:10 +02:00
|
|
|
this._type = type;
|
2020-07-11 00:11:47 +02:00
|
|
|
this._guid = guid;
|
2020-07-25 01:22:20 +02:00
|
|
|
this._parent = parent instanceof ChannelOwner ? parent : undefined;
|
2020-07-11 00:11:47 +02:00
|
|
|
|
|
|
|
|
this._connection._objects.set(guid, this);
|
2020-07-11 03:00:10 +02:00
|
|
|
if (this._parent) {
|
2020-07-11 00:11:47 +02:00
|
|
|
this._parent._objects.set(guid, this);
|
2020-07-11 03:00:10 +02:00
|
|
|
this._logger = this._parent._logger;
|
|
|
|
|
}
|
2020-07-11 00:11:47 +02:00
|
|
|
|
2021-06-28 22:27:38 +02:00
|
|
|
this._channel = this._createChannel(new EventEmitter(), null);
|
2020-06-26 21:28:27 +02:00
|
|
|
this._initializer = initializer;
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
2020-07-11 00:11:47 +02: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 19:21:39 +02:00
|
|
|
for (const object of [...this._objects.values()])
|
|
|
|
|
object._dispose();
|
2020-07-11 00:11:47 +02:00
|
|
|
this._objects.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_debugScopeState(): any {
|
|
|
|
|
return {
|
|
|
|
|
_guid: this._guid,
|
2020-07-27 19:21:39 +02:00
|
|
|
objects: Array.from(this._objects.values()).map(o => o._debugScopeState()),
|
2020-07-11 00:11:47 +02:00
|
|
|
};
|
|
|
|
|
}
|
2020-07-15 23:04:39 +02:00
|
|
|
|
2021-09-15 20:34:23 +02:00
|
|
|
private _createChannel(base: Object, stackTrace: ParsedStackTrace | null, csi?: ClientSideInstrumentation, callCookie?: { userObject: any }): T {
|
2021-02-20 03:12:33 +01:00
|
|
|
const channel = new Proxy(base, {
|
|
|
|
|
get: (obj: any, prop) => {
|
|
|
|
|
if (prop === 'debugScopeState')
|
2021-09-14 03:07:15 +02:00
|
|
|
return (params: any) => this._connection.sendMessageToServer(this, prop, params, stackTrace);
|
2021-02-20 03:12:33 +01:00
|
|
|
if (typeof prop === 'string') {
|
|
|
|
|
const validator = scheme[paramsName(this._type, prop)];
|
2021-09-15 20:34:23 +02:00
|
|
|
if (validator) {
|
|
|
|
|
return (params: any) => {
|
|
|
|
|
if (callCookie && csi) {
|
2021-10-19 06:06:18 +02:00
|
|
|
callCookie.userObject = csi.onApiCallBegin(renderCallWithParams(stackTrace!.apiName!, params), stackTrace).userObject;
|
2021-09-15 20:34:23 +02:00
|
|
|
csi = undefined;
|
|
|
|
|
}
|
|
|
|
|
return this._connection.sendMessageToServer(this, prop, validator(params, ''), stackTrace);
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-02-20 03:12:33 +01:00
|
|
|
}
|
|
|
|
|
return obj[prop];
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
(channel as any)._object = this;
|
|
|
|
|
return channel;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 00:22:49 +02:00
|
|
|
async _wrapApiCall<R, C extends channels.Channel = T>(func: (channel: C, stackTrace: ParsedStackTrace) => Promise<R>, logger?: Logger, isInternal?: boolean): Promise<R> {
|
2020-07-15 23:04:39 +02:00
|
|
|
logger = logger || this._logger;
|
2021-06-28 22:27:38 +02:00
|
|
|
const stackTrace = captureStackTrace();
|
|
|
|
|
const { apiName, frameTexts } = stackTrace;
|
2021-08-10 03:09:11 +02:00
|
|
|
|
|
|
|
|
let ancestorWithCSI: ChannelOwner<any> = this;
|
|
|
|
|
while (!ancestorWithCSI._csi && ancestorWithCSI._parent)
|
|
|
|
|
ancestorWithCSI = ancestorWithCSI._parent;
|
2021-09-15 20:34:23 +02:00
|
|
|
|
|
|
|
|
// Do not report nested async calls to _wrapApiCall.
|
2021-10-16 00:22:49 +02:00
|
|
|
isInternal = isInternal || stackTrace.allFrames.filter(f => f.function?.includes('_wrapApiCall')).length > 1;
|
|
|
|
|
if (isInternal)
|
|
|
|
|
delete stackTrace.apiName;
|
|
|
|
|
const csi = isInternal ? undefined : ancestorWithCSI._csi;
|
2021-09-15 20:34:23 +02:00
|
|
|
const callCookie: { userObject: any } = { userObject: null };
|
2021-08-10 03:09:11 +02:00
|
|
|
|
2020-07-15 23:04:39 +02:00
|
|
|
try {
|
2021-10-16 00:22:49 +02:00
|
|
|
logApiCall(logger, `=> ${apiName} started`, isInternal);
|
2021-09-15 20:34:23 +02:00
|
|
|
const channel = this._createChannel({}, stackTrace, csi, callCookie);
|
2021-06-28 22:27:38 +02:00
|
|
|
const result = await func(channel as any, stackTrace);
|
2021-09-15 20:34:23 +02:00
|
|
|
csi?.onApiCallEnd(callCookie);
|
2021-10-16 00:22:49 +02:00
|
|
|
logApiCall(logger, `<= ${apiName} succeeded`, isInternal);
|
2020-07-15 23:04:39 +02:00
|
|
|
return result;
|
|
|
|
|
} catch (e) {
|
2021-06-28 22:27:38 +02:00
|
|
|
const innerError = ((process.env.PWDEBUGIMPL || isUnderTest()) && e.stack) ? '\n<inner error>\n' + e.stack : '';
|
|
|
|
|
e.message = apiName + ': ' + e.message;
|
|
|
|
|
e.stack = e.message + '\n' + frameTexts.join('\n') + innerError;
|
2021-09-15 20:34:23 +02:00
|
|
|
csi?.onApiCallEnd(callCookie, e);
|
2021-10-16 00:22:49 +02:00
|
|
|
logApiCall(logger, `<= ${apiName} failed`, isInternal);
|
2020-07-15 23:04:39 +02:00
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-16 23:41:33 +02: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-26 01:05:36 +02:00
|
|
|
}
|
2020-07-16 23:32:21 +02:00
|
|
|
|
2021-09-15 20:34:23 +02:00
|
|
|
function logApiCall(logger: Logger | undefined, message: string, isNested: boolean) {
|
|
|
|
|
if (isNested)
|
|
|
|
|
return;
|
2020-07-16 23:32:21 +02:00
|
|
|
if (logger && logger.isEnabled('api', 'info'))
|
|
|
|
|
logger.log('api', 'info', message, [], { color: 'cyan' });
|
2020-08-17 23:12:31 +02:00
|
|
|
debugLogger.log('api', message);
|
2020-07-16 23:32:21 +02:00
|
|
|
}
|
2020-11-26 16:33:09 +01:00
|
|
|
|
|
|
|
|
function paramsName(type: string, method: string) {
|
|
|
|
|
return type + method[0].toUpperCase() + method.substring(1) + 'Params';
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 20:34:23 +02:00
|
|
|
const paramsToRender = ['url', 'selector', 'text', 'key'];
|
|
|
|
|
export function renderCallWithParams(apiName: string, params: any) {
|
|
|
|
|
const paramsArray = [];
|
|
|
|
|
if (params) {
|
|
|
|
|
for (const name of paramsToRender) {
|
|
|
|
|
if (params[name])
|
|
|
|
|
paramsArray.push(params[name]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const paramsText = paramsArray.length ? '(' + paramsArray.join(', ') + ')' : '';
|
|
|
|
|
return apiName + paramsText;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 16:33:09 +01:00
|
|
|
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);
|