playwright/src/chromium/crProtocolHelper.ts

112 lines
4.0 KiB
TypeScript
Raw Normal View History

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.
*/
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-04-01 14:42:47 -07:00
import * as fs from 'fs';
import * as util from 'util';
2019-11-18 18:18:28 -08:00
export function getExceptionMessage(exceptionDetails: Protocol.Runtime.ExceptionDetails): string {
if (exceptionDetails.exception)
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;
}
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;
await client.send('Runtime.releaseObject', {objectId: remoteObject.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;
let fd: number | undefined;
2019-11-18 18:18:28 -08:00
if (path)
2020-04-01 14:42:47 -07:00
fd = await util.promisify(fs.open)(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-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);
if (path)
2020-04-01 14:42:47 -07:00
await util.promisify(fs.write)(fd!, buf);
2019-11-18 18:18:28 -08:00
}
if (path)
2020-04-01 14:42:47 -07:00
await util.promisify(fs.close)(fd!);
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
}
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
export function exceptionToError(exceptionDetails: Protocol.Runtime.ExceptionDetails): Error {
const messageWithStack = getExceptionMessage(exceptionDetails);
const lines = messageWithStack.split('\n');
const firstStackTraceLine = lines.findIndex(line => line.startsWith(' at'));
let message = '';
let stack = '';
if (firstStackTraceLine === -1) {
message = messageWithStack;
} else {
message = lines.slice(0, firstStackTraceLine).join('\n');
stack = messageWithStack;
}
const match = message.match(/^[a-zA-Z0-0_]*Error: (.*)$/);
if (match)
message = match[1];
const err = new Error(message);
err.stack = stack;
return err;
}