2019-11-18 18:18:28 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications 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.
|
|
|
|
*/
|
2020-01-07 11:55:24 -08:00
|
|
|
|
2020-03-04 17:57:35 -08:00
|
|
|
import { assert } from '../helper';
|
2019-12-19 16:40:44 -08:00
|
|
|
import { CRSession } from './crConnection';
|
2019-11-18 18:18:28 -08:00
|
|
|
import { Protocol } from './protocol';
|
2020-01-07 11:55:24 -08:00
|
|
|
import * as platform from '../platform';
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
export function getExceptionMessage(exceptionDetails: Protocol.Runtime.ExceptionDetails): string {
|
|
|
|
if (exceptionDetails.exception)
|
|
|
|
return exceptionDetails.exception.description || exceptionDetails.exception.value;
|
|
|
|
let message = exceptionDetails.text;
|
|
|
|
if (exceptionDetails.stackTrace) {
|
|
|
|
for (const callframe of exceptionDetails.stackTrace.callFrames) {
|
|
|
|
const location = callframe.url + ':' + callframe.lineNumber + ':' + callframe.columnNumber;
|
|
|
|
const functionName = callframe.functionName || '<anonymous>';
|
|
|
|
message += `\n at ${functionName} (${location})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function valueFromRemoteObject(remoteObject: Protocol.Runtime.RemoteObject): any {
|
|
|
|
assert(!remoteObject.objectId, 'Cannot extract value when objectId is given');
|
|
|
|
if (remoteObject.unserializableValue) {
|
|
|
|
if (remoteObject.type === 'bigint' && typeof BigInt !== 'undefined')
|
|
|
|
return BigInt(remoteObject.unserializableValue.replace('n', ''));
|
|
|
|
switch (remoteObject.unserializableValue) {
|
|
|
|
case '-0':
|
|
|
|
return -0;
|
|
|
|
case 'NaN':
|
|
|
|
return NaN;
|
|
|
|
case 'Infinity':
|
|
|
|
return Infinity;
|
|
|
|
case '-Infinity':
|
|
|
|
return -Infinity;
|
|
|
|
default:
|
|
|
|
throw new Error('Unsupported unserializable value: ' + remoteObject.unserializableValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return remoteObject.value;
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:40:44 -08:00
|
|
|
export async function releaseObject(client: CRSession, remoteObject: Protocol.Runtime.RemoteObject) {
|
2019-11-18 18:18:28 -08:00
|
|
|
if (!remoteObject.objectId)
|
|
|
|
return;
|
2020-03-04 17:57:35 -08:00
|
|
|
await client.send('Runtime.releaseObject', {objectId: remoteObject.objectId}).catch(error => {});
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-07 11:55:24 -08:00
|
|
|
export async function readProtocolStream(client: CRSession, handle: string, path: string | null): Promise<platform.BufferType> {
|
2019-11-18 18:18:28 -08:00
|
|
|
let eof = false;
|
2020-01-13 13:33:25 -08:00
|
|
|
let fd: number | undefined;
|
2019-11-18 18:18:28 -08:00
|
|
|
if (path)
|
2020-01-07 11:55:24 -08:00
|
|
|
fd = await platform.openFdAsync(path, 'w');
|
2019-11-18 18:18:28 -08:00
|
|
|
const bufs = [];
|
|
|
|
while (!eof) {
|
|
|
|
const response = await client.send('IO.read', {handle});
|
|
|
|
eof = response.eof;
|
2020-01-07 11:55:24 -08:00
|
|
|
const buf = platform.Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
|
2019-11-18 18:18:28 -08:00
|
|
|
bufs.push(buf);
|
|
|
|
if (path)
|
2020-01-13 13:33:25 -08:00
|
|
|
await platform.writeFdAsync(fd!, buf);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
if (path)
|
2020-01-13 13:33:25 -08:00
|
|
|
await platform.closeFdAsync(fd!);
|
2019-11-18 18:18:28 -08:00
|
|
|
await client.send('IO.close', {handle});
|
2020-03-27 15:16:17 -07:00
|
|
|
return platform.Buffer.concat(bufs);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
export function toConsoleMessageLocation(stackTrace: Protocol.Runtime.StackTrace | undefined) {
|
|
|
|
return stackTrace && stackTrace.callFrames.length ? {
|
|
|
|
url: stackTrace.callFrames[0].url,
|
|
|
|
lineNumber: stackTrace.callFrames[0].lineNumber,
|
|
|
|
columnNumber: stackTrace.callFrames[0].columnNumber,
|
|
|
|
} : {};
|
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
export function exceptionToError(exceptionDetails: Protocol.Runtime.ExceptionDetails): Error {
|
|
|
|
const message = getExceptionMessage(exceptionDetails);
|
|
|
|
const err = new Error(message);
|
2020-02-04 19:31:57 -08:00
|
|
|
// Don't report clientside error with a node stack attached
|
|
|
|
err.stack = 'Error: ' + err.message; // Stack is supposed to contain error message as the first line.
|
2019-12-06 13:36:47 -08:00
|
|
|
return err;
|
|
|
|
}
|