Skip to content

Commit

Permalink
chore: remove web mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Apr 1, 2020
1 parent a7b61a0 commit 9c7208d
Show file tree
Hide file tree
Showing 42 changed files with 266 additions and 942 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"extract-zip": "^1.6.6",
"https-proxy-agent": "^3.0.0",
"jpeg-js": "^0.3.6",
"mime": "^2.4.4",
"pngjs": "^3.4.0",
"progress": "^2.0.3",
"proxy-from-env": "^1.1.0",
Expand All @@ -56,6 +57,7 @@
"devDependencies": {
"@types/debug": "0.0.31",
"@types/extract-zip": "^1.6.2",
"@types/mime": "^2.0.1",
"@types/node": "^8.10.34",
"@types/pngjs": "^3.4.0",
"@types/proxy-from-env": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import { BrowserContext, BrowserContextOptions } from './browserContext';
import * as platform from './platform';
import { Page } from './page';
import { EventEmitter } from 'events';

export interface Browser extends platform.EventEmitterType {
export interface Browser extends EventEmitter {
newContext(options?: BrowserContextOptions): Promise<BrowserContext>;
contexts(): BrowserContext[];
newPage(options?: BrowserContextOptions): Promise<Page>;
Expand Down
8 changes: 4 additions & 4 deletions src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { Events as CommonEvents } from '../events';
import { assert, debugError, helper } from '../helper';
import * as network from '../network';
import { Page, PageBinding, Worker } from '../page';
import * as platform from '../platform';
import { ConnectionTransport, SlowMoTransport } from '../transport';
import * as types from '../types';
import { ConnectionEvents, CRConnection, CRSession } from './crConnection';
Expand All @@ -30,8 +29,9 @@ import { readProtocolStream } from './crProtocolHelper';
import { Events } from './events';
import { Protocol } from './protocol';
import { CRExecutionContext } from './crExecutionContext';
import { EventEmitter } from 'events';

export class CRBrowser extends platform.EventEmitter implements Browser {
export class CRBrowser extends EventEmitter implements Browser {
readonly _connection: CRConnection;
_session: CRSession;
private _clientRootSessionPromise: Promise<CRSession> | null = null;
Expand Down Expand Up @@ -221,7 +221,7 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
});
}

async stopTracing(): Promise<platform.BufferType> {
async stopTracing(): Promise<Buffer> {
assert(this._tracingClient, 'Tracing was not started.');
const [event] = await Promise.all([
new Promise(f => this._tracingClient!.once('Tracing.tracingComplete', f)),
Expand All @@ -242,7 +242,7 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
return this._clientRootSessionPromise;
}

_setDebugFunction(debugFunction: platform.DebuggerType) {
_setDebugFunction(debugFunction: debug.IDebugger) {
this._connection._debugProtocol = debugFunction;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/chromium/crConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/

import { assert } from '../helper';
import * as platform from '../platform';
import * as debug from 'debug';
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import { Protocol } from './protocol';
import { EventEmitter } from 'events';

export const ConnectionEvents = {
Disconnected: Symbol('ConnectionEvents.Disconnected')
Expand All @@ -28,13 +29,13 @@ export const ConnectionEvents = {
// should ignore.
export const kBrowserCloseMessageId = -9999;

export class CRConnection extends platform.EventEmitter {
export class CRConnection extends EventEmitter {
private _lastId = 0;
private readonly _transport: ConnectionTransport;
private readonly _sessions = new Map<string, CRSession>();
readonly rootSession: CRSession;
_closed = false;
_debugProtocol: platform.DebuggerType;
_debugProtocol: debug.IDebugger;

constructor(transport: ConnectionTransport) {
super();
Expand All @@ -43,7 +44,7 @@ export class CRConnection extends platform.EventEmitter {
this._transport.onclose = this._onClose.bind(this);
this.rootSession = new CRSession(this, '', 'browser', '');
this._sessions.set('', this.rootSession);
this._debugProtocol = platform.debug('pw:protocol');
this._debugProtocol = debug('pw:protocol');
(this._debugProtocol as any).color = '34';
}

Expand Down Expand Up @@ -118,7 +119,7 @@ export const CRSessionEvents = {
Disconnected: Symbol('Events.CDPSession.Disconnected')
};

export class CRSession extends platform.EventEmitter {
export class CRSession extends EventEmitter {
_connection: CRConnection | null;
private readonly _callbacks = new Map<number, {resolve: (o: any) => void, reject: (e: Error) => void, error: Error, method: string}>();
private readonly _targetType: string;
Expand Down
7 changes: 3 additions & 4 deletions src/chromium/crNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { assert, debugError, helper, RegisteredListener } from '../helper';
import { Protocol } from './protocol';
import * as network from '../network';
import * as frames from '../frames';
import * as platform from '../platform';
import { Credentials } from '../types';
import { CRPage } from './crPage';

Expand Down Expand Up @@ -191,7 +190,7 @@ export class CRNetworkManager {
_createResponse(request: InterceptableRequest, responsePayload: Protocol.Network.Response): network.Response {
const getResponseBody = async () => {
const response = await this._client.send('Network.getResponseBody', { requestId: request._requestId });
return platform.Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
};
return new network.Response(request.request, responsePayload.status, responsePayload.statusText, headersObject(responsePayload.headers), getResponseBody);
}
Expand Down Expand Up @@ -281,7 +280,7 @@ class InterceptableRequest implements network.RouteDelegate {
}

async fulfill(response: network.FulfillResponse) {
const responseBody = response.body && helper.isString(response.body) ? platform.Buffer.from(response.body) : (response.body || null);
const responseBody = response.body && helper.isString(response.body) ? Buffer.from(response.body) : (response.body || null);

const responseHeaders: { [s: string]: string; } = {};
if (response.headers) {
Expand All @@ -291,7 +290,7 @@ class InterceptableRequest implements network.RouteDelegate {
if (response.contentType)
responseHeaders['content-type'] = response.contentType;
if (responseBody && !('content-length' in responseHeaders))
responseHeaders['content-length'] = String(platform.Buffer.byteLength(responseBody));
responseHeaders['content-length'] = String(Buffer.byteLength(responseBody));

await this._client.send('Fetch.fulfillRequest', {
requestId: this._interceptionId!,
Expand Down
7 changes: 3 additions & 4 deletions src/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { CRPDF } from './crPdf';
import { CRBrowserContext } from './crBrowser';
import * as types from '../types';
import { ConsoleMessage } from '../console';
import * as platform from '../platform';

const UTILITY_WORLD_NAME = '__playwright_utility_world__';

Expand Down Expand Up @@ -455,7 +454,7 @@ export class CRPage implements PageDelegate {
await this._client.send('Emulation.setDefaultBackgroundColorOverride', { color });
}

async takeScreenshot(format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined): Promise<platform.BufferType> {
async takeScreenshot(format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined): Promise<Buffer> {
const { visualViewport } = await this._client.send('Page.getLayoutMetrics');
if (!documentRect) {
documentRect = {
Expand All @@ -472,7 +471,7 @@ export class CRPage implements PageDelegate {
// ignore current page scale.
const clip = { ...documentRect, scale: viewportRect ? visualViewport.scale : 1 };
const result = await this._client.send('Page.captureScreenshot', { format, quality, clip });
return platform.Buffer.from(result.data, 'base64');
return Buffer.from(result.data, 'base64');
}

async resetViewport(): Promise<void> {
Expand Down Expand Up @@ -587,7 +586,7 @@ export class CRPage implements PageDelegate {
await this._client.send('Page.enable').catch(e => {});
}

async pdf(options?: types.PDFOptions): Promise<platform.BufferType> {
async pdf(options?: types.PDFOptions): Promise<Buffer> {
return this._pdf.generate(options);
}

Expand Down
3 changes: 1 addition & 2 deletions src/chromium/crPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

import { assert, helper } from '../helper';
import * as platform from '../platform';
import * as types from '../types';
import { CRSession } from './crConnection';
import { readProtocolStream } from './crProtocolHelper';
Expand Down Expand Up @@ -77,7 +76,7 @@ export class CRPDF {
this._client = client;
}

async generate(options: types.PDFOptions = {}): Promise<platform.BufferType> {
async generate(options: types.PDFOptions = {}): Promise<Buffer> {
const {
scale = 1,
displayHeaderFooter = false,
Expand Down
15 changes: 8 additions & 7 deletions src/chromium/crProtocolHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import { assert } from '../helper';
import { CRSession } from './crConnection';
import { Protocol } from './protocol';
import * as platform from '../platform';
import * as fs from 'fs';
import * as util from 'util';

export function getExceptionMessage(exceptionDetails: Protocol.Runtime.ExceptionDetails): string {
if (exceptionDetails.exception)
Expand Down Expand Up @@ -61,24 +62,24 @@ export async function releaseObject(client: CRSession, remoteObject: Protocol.Ru
await client.send('Runtime.releaseObject', {objectId: remoteObject.objectId}).catch(error => {});
}

export async function readProtocolStream(client: CRSession, handle: string, path: string | null): Promise<platform.BufferType> {
export async function readProtocolStream(client: CRSession, handle: string, path: string | null): Promise<Buffer> {
let eof = false;
let fd: number | undefined;
if (path)
fd = await platform.openFdAsync(path, 'w');
fd = await util.promisify(fs.open)(path, 'w');
const bufs = [];
while (!eof) {
const response = await client.send('IO.read', {handle});
eof = response.eof;
const buf = platform.Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
const buf = Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
bufs.push(buf);
if (path)
await platform.writeFdAsync(fd!, buf);
await util.promisify(fs.write)(fd!, buf);
}
if (path)
await platform.closeFdAsync(fd!);
await util.promisify(fs.close)(fd!);
await client.send('IO.close', {handle});
return platform.Buffer.concat(bufs);
return Buffer.concat(bufs);
}

export function toConsoleMessageLocation(stackTrace: Protocol.Runtime.StackTrace | undefined) {
Expand Down
19 changes: 11 additions & 8 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* limitations under the License.
*/

import * as fs from 'fs';
import * as mime from 'mime';
import * as path from 'path';
import * as util from 'util';
import * as frames from './frames';
import { assert, debugError, helper } from './helper';
import Injected from './injected/injected';
import * as input from './input';
import * as js from './javascript';
import * as types from './types';
import { assert, helper, debugError } from './helper';
import Injected from './injected/injected';
import { Page } from './page';
import * as platform from './platform';
import { selectors } from './selectors';
import * as types from './types';

export type PointerActionOptions = {
modifiers?: input.Modifier[];
Expand Down Expand Up @@ -257,9 +260,9 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
for (const item of ff) {
if (typeof item === 'string') {
const file: types.FilePayload = {
name: platform.basename(item),
type: platform.getMimeType(item),
data: await platform.readFileAsync(item, 'base64')
name: path.basename(item),
type: mime.getType(item) || 'application/octet-stream',
data: await util.promisify(fs.readFile)(item, 'base64')
};
filePayloads.push(file);
} else {
Expand Down Expand Up @@ -316,7 +319,7 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return this._page._delegate.getBoundingBox(this);
}

async screenshot(options?: types.ElementScreenshotOptions): Promise<platform.BufferType> {
async screenshot(options?: types.ElementScreenshotOptions): Promise<Buffer> {
return this._page._screenshotter.screenshotElement(this, options);
}

Expand Down
2 changes: 1 addition & 1 deletion src/extendedEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { EventEmitter } from './platform';
import { EventEmitter } from 'events';
import { helper } from './helper';

export class ExtendedEventEmitter extends EventEmitter {
Expand Down
6 changes: 3 additions & 3 deletions src/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import { Events } from '../events';
import { assert, helper, RegisteredListener } from '../helper';
import * as network from '../network';
import { Page, PageBinding } from '../page';
import * as platform from '../platform';
import { ConnectionTransport, SlowMoTransport } from '../transport';
import * as types from '../types';
import { ConnectionEvents, FFConnection } from './ffConnection';
import { headersArray } from './ffNetworkManager';
import { FFPage } from './ffPage';
import { Protocol } from './protocol';
import { EventEmitter } from 'events';

export class FFBrowser extends platform.EventEmitter implements Browser {
export class FFBrowser extends EventEmitter implements Browser {
_connection: FFConnection;
readonly _ffPages: Map<string, FFPage>;
readonly _defaultContext: FFBrowserContext;
Expand Down Expand Up @@ -149,7 +149,7 @@ export class FFBrowser extends platform.EventEmitter implements Browser {
await disconnected;
}

_setDebugFunction(debugFunction: platform.DebuggerType) {
_setDebugFunction(debugFunction: debug.IDebugger) {
this._connection._debugProtocol = debugFunction;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/firefox/ffConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

import {assert} from '../helper';
import * as platform from '../platform';
import * as debug from 'debug';
import { EventEmitter } from 'events';
import { assert } from '../helper';
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import { Protocol } from './protocol';

Expand All @@ -28,12 +29,12 @@ export const ConnectionEvents = {
// should ignore.
export const kBrowserCloseMessageId = -9999;

export class FFConnection extends platform.EventEmitter {
export class FFConnection extends EventEmitter {
private _lastId: number;
private _callbacks: Map<number, {resolve: Function, reject: Function, error: Error, method: string}>;
private _transport: ConnectionTransport;
readonly _sessions: Map<string, FFSession>;
_debugProtocol: platform.DebuggerType = platform.debug('pw:protocol');
_debugProtocol = debug('pw:protocol');
_closed: boolean;

on: <T extends keyof Protocol.Events | symbol>(event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this;
Expand Down Expand Up @@ -135,7 +136,7 @@ export const FFSessionEvents = {
Disconnected: Symbol('Disconnected')
};

export class FFSession extends platform.EventEmitter {
export class FFSession extends EventEmitter {
_connection: FFConnection;
_disposed = false;
private _callbacks: Map<number, {resolve: Function, reject: Function, error: Error, method: string}>;
Expand Down
7 changes: 3 additions & 4 deletions src/firefox/ffNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { FFSession } from './ffConnection';
import { Page } from '../page';
import * as network from '../network';
import * as frames from '../frames';
import * as platform from '../platform';
import { Protocol } from './protocol';

export class FFNetworkManager {
Expand Down Expand Up @@ -73,7 +72,7 @@ export class FFNetworkManager {
});
if (response.evicted)
throw new Error(`Response body for ${request.request.method()} ${request.request.url()} was evicted!`);
return platform.Buffer.from(response.base64body, 'base64');
return Buffer.from(response.base64body, 'base64');
};
const headers: network.Headers = {};
for (const {name, value} of event.headers)
Expand Down Expand Up @@ -171,7 +170,7 @@ class InterceptableRequest implements network.RouteDelegate {
}

async fulfill(response: network.FulfillResponse) {
const responseBody = response.body && helper.isString(response.body) ? platform.Buffer.from(response.body) : (response.body || null);
const responseBody = response.body && helper.isString(response.body) ? Buffer.from(response.body) : (response.body || null);

const responseHeaders: { [s: string]: string; } = {};
if (response.headers) {
Expand All @@ -181,7 +180,7 @@ class InterceptableRequest implements network.RouteDelegate {
if (response.contentType)
responseHeaders['content-type'] = response.contentType;
if (responseBody && !('content-length' in responseHeaders))
responseHeaders['content-length'] = String(platform.Buffer.byteLength(responseBody));
responseHeaders['content-length'] = String(Buffer.byteLength(responseBody));

await this._session.send('Network.fulfillInterceptedRequest', {
requestId: this._id,
Expand Down
Loading

0 comments on commit 9c7208d

Please sign in to comment.