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
|
|
|
|
2019-12-19 16:40:44 -08:00
|
|
|
import { CRSession } from './crConnection';
|
2019-11-18 18:18:28 -08:00
|
|
|
import { Protocol } from './protocol';
|
2021-02-11 06:36:15 -08:00
|
|
|
import fs from 'fs';
|
2020-08-24 06:51:51 -07:00
|
|
|
import * as types from '../types';
|
2020-08-22 15:46:42 -07:00
|
|
|
import { mkdirIfNeeded } from '../../utils/utils';
|
2021-04-19 22:37:38 +02:00
|
|
|
import { splitErrorMessage } from '../../utils/stackTrace';
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
export function getExceptionMessage(exceptionDetails: Protocol.Runtime.ExceptionDetails): string {
|
|
|
|
if (exceptionDetails.exception)
|
2020-05-19 15:20:49 -07:00
|
|
|
return exceptionDetails.exception.description || String(exceptionDetails.exception.value);
|
2019-11-18 18:18:28 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
export async function releaseObject(client: CRSession, objectId: string) {
|
|
|
|
await client.send('Runtime.releaseObject', { objectId }).catch(error => {});
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
export async function readProtocolStream(client: CRSession, handle: string, path: string | null): Promise<Buffer> {
|
2019-11-18 18:18:28 -08:00
|
|
|
let eof = false;
|
2021-06-03 09:55:33 -07:00
|
|
|
let fd: fs.promises.FileHandle | undefined;
|
2020-08-17 16:53:19 -07:00
|
|
|
if (path) {
|
|
|
|
await mkdirIfNeeded(path);
|
2021-06-03 09:55:33 -07:00
|
|
|
fd = await fs.promises.open(path, 'w');
|
2020-08-17 16:53:19 -07:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
const bufs = [];
|
|
|
|
while (!eof) {
|
|
|
|
const response = await client.send('IO.read', {handle});
|
|
|
|
eof = response.eof;
|
2020-04-01 14:42:47 -07:00
|
|
|
const buf = Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
|
2019-11-18 18:18:28 -08:00
|
|
|
bufs.push(buf);
|
2021-06-03 09:55:33 -07:00
|
|
|
if (fd)
|
|
|
|
await fd.write(buf);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2021-06-03 09:55:33 -07:00
|
|
|
if (fd)
|
|
|
|
await fd.close();
|
2019-11-18 18:18:28 -08:00
|
|
|
await client.send('IO.close', {handle});
|
2020-04-01 14:42:47 -07:00
|
|
|
return Buffer.concat(bufs);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-08-05 22:25:56 -07:00
|
|
|
export function toConsoleMessageLocation(stackTrace: Protocol.Runtime.StackTrace | undefined): types.ConsoleMessageLocation {
|
2019-12-06 13:36:47 -08:00
|
|
|
return stackTrace && stackTrace.callFrames.length ? {
|
|
|
|
url: stackTrace.callFrames[0].url,
|
|
|
|
lineNumber: stackTrace.callFrames[0].lineNumber,
|
|
|
|
columnNumber: stackTrace.callFrames[0].columnNumber,
|
2020-08-05 22:25:56 -07:00
|
|
|
} : { url: '', lineNumber: 0, columnNumber: 0 };
|
2019-12-06 13:36:47 -08:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
export function exceptionToError(exceptionDetails: Protocol.Runtime.ExceptionDetails): Error {
|
2020-04-20 11:37:02 -07:00
|
|
|
const messageWithStack = getExceptionMessage(exceptionDetails);
|
|
|
|
const lines = messageWithStack.split('\n');
|
|
|
|
const firstStackTraceLine = lines.findIndex(line => line.startsWith(' at'));
|
2021-04-19 22:37:38 +02:00
|
|
|
let messageWithName = '';
|
2020-04-20 11:37:02 -07:00
|
|
|
let stack = '';
|
|
|
|
if (firstStackTraceLine === -1) {
|
2021-04-19 22:37:38 +02:00
|
|
|
messageWithName = messageWithStack;
|
2020-04-20 11:37:02 -07:00
|
|
|
} else {
|
2021-04-19 22:37:38 +02:00
|
|
|
messageWithName = lines.slice(0, firstStackTraceLine).join('\n');
|
2020-04-20 11:37:02 -07:00
|
|
|
stack = messageWithStack;
|
|
|
|
}
|
2021-04-19 22:37:38 +02:00
|
|
|
const {name, message} = splitErrorMessage(messageWithName);
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
const err = new Error(message);
|
2020-04-20 11:37:02 -07:00
|
|
|
err.stack = stack;
|
2021-04-19 22:37:38 +02:00
|
|
|
err.name = name;
|
2019-12-06 13:36:47 -08:00
|
|
|
return err;
|
|
|
|
}
|