Skip to content

Commit

Permalink
chore: move assert into its own module (#6021)
Browse files Browse the repository at this point in the history
A lot of the helpers in `helpers.ts` are heavily bound to NodeJS and at
the moment we're trying to make the `Connection` class be able to run in
multiple environments. Its only remaining Node dependency was its
reliance on `helpers.ts`, which it only needed for `assert`.

This is a useful change also because `helpers.ts` is quite large and
full of functions that do different things; I think we can name them
better and move them into modules with a specific purpose rather than a
generic `"helpers"` dumping ground.

Once this change lands `Connection` should be usable in the browser.
  • Loading branch information
jackfranklin committed Jun 15, 2020
1 parent f1ec6a3 commit e4de5f1
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/Browser.ts
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { helper, assert } from './helper';
import { assert } from './assert';
import { helper } from './helper';
import { Target } from './Target';
import { EventEmitter } from './EventEmitter';
import { Events } from './Events';
Expand Down
4 changes: 2 additions & 2 deletions src/BrowserFetcher.ts
Expand Up @@ -28,8 +28,8 @@ import removeRecursive from 'rimraf';
import * as URL from 'url';
import ProxyAgent from 'https-proxy-agent';
import { getProxyForUrl } from 'proxy-from-env';

import { helper, assert } from './helper';
import { assert } from './assert';
import { helper } from './helper';
const debugFetcher = debug(`puppeteer:fetcher`);

const downloadURLs = {
Expand Down
2 changes: 1 addition & 1 deletion src/Connection.ts
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from './helper';
import { assert } from './assert';
import { Events } from './Events';
import debug from 'debug';
const debugProtocolSend = debug('puppeteer:protocol:SEND ►');
Expand Down
3 changes: 2 additions & 1 deletion src/Coverage.ts
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { helper, debugError, assert, PuppeteerEventListener } from './helper';
import { assert } from './assert';
import { helper, debugError, PuppeteerEventListener } from './helper';
import Protocol from './protocol';
import { CDPSession } from './Connection';

Expand Down
3 changes: 2 additions & 1 deletion src/DOMWorld.ts
Expand Up @@ -15,7 +15,8 @@
*/

import * as fs from 'fs';
import { helper, assert } from './helper';
import { assert } from './assert';
import { helper } from './helper';
import { LifecycleWatcher, PuppeteerLifeCycleEvent } from './LifecycleWatcher';
import { TimeoutError } from './Errors';
import { JSHandle, ElementHandle } from './JSHandle';
Expand Down
2 changes: 1 addition & 1 deletion src/Dialog.ts
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { assert } from './helper';
import { assert } from './assert';
import { CDPSession } from './Connection';
import Protocol from './protocol';

Expand Down
3 changes: 2 additions & 1 deletion src/ExecutionContext.ts
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { helper, assert } from './helper';
import { assert } from './assert';
import { helper } from './helper';
import { createJSHandle, JSHandle, ElementHandle } from './JSHandle';
import { CDPSession } from './Connection';
import { DOMWorld } from './DOMWorld';
Expand Down
2 changes: 1 addition & 1 deletion src/FileChooser.ts
Expand Up @@ -16,7 +16,7 @@

import { ElementHandle } from './JSHandle';
import Protocol from './protocol';
import { assert } from './helper';
import { assert } from './assert';

export class FileChooser {
private _element: ElementHandle;
Expand Down
3 changes: 2 additions & 1 deletion src/FrameManager.ts
Expand Up @@ -15,7 +15,8 @@
*/

import { EventEmitter } from './EventEmitter';
import { helper, assert, debugError } from './helper';
import { assert } from './assert';
import { helper, debugError } from './helper';
import { Events } from './Events';
import { ExecutionContext, EVALUATION_SCRIPT_URL } from './ExecutionContext';
import { LifecycleWatcher, PuppeteerLifeCycleEvent } from './LifecycleWatcher';
Expand Down
3 changes: 2 additions & 1 deletion src/HTTPRequest.ts
Expand Up @@ -16,7 +16,8 @@
import { CDPSession } from './Connection';
import { Frame } from './FrameManager';
import { HTTPResponse } from './HTTPResponse';
import { helper, assert, debugError } from './helper';
import { assert } from './assert';
import { helper, debugError } from './helper';
import Protocol from './protocol';

export class HTTPRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/Input.ts
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { assert } from './helper';
import { assert } from './assert';
import { CDPSession } from './Connection';
import { keyDefinitions, KeyDefinition, KeyInput } from './USKeyboardLayout';

Expand Down
3 changes: 2 additions & 1 deletion src/JSHandle.ts
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { helper, assert, debugError } from './helper';
import { assert } from './assert';
import { helper, debugError } from './helper';
import { ExecutionContext } from './ExecutionContext';
import { Page } from './Page';
import { CDPSession } from './Connection';
Expand Down
3 changes: 2 additions & 1 deletion src/Launcher.ts
Expand Up @@ -23,7 +23,8 @@ import * as fs from 'fs';
import { BrowserFetcher } from './BrowserFetcher';
import { Connection } from './Connection';
import { Browser } from './Browser';
import { helper, assert, debugError } from './helper';
import { assert } from './assert';
import { helper, debugError } from './helper';
import { ConnectionTransport } from './ConnectionTransport';
import { WebSocketTransport } from './WebSocketTransport';
import { BrowserRunner } from './launcher/BrowserRunner';
Expand Down
3 changes: 2 additions & 1 deletion src/LifecycleWatcher.ts
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { helper, assert, PuppeteerEventListener } from './helper';
import { assert } from './assert';
import { helper, PuppeteerEventListener } from './helper';
import { Events } from './Events';
import { TimeoutError } from './Errors';
import { FrameManager, Frame } from './FrameManager';
Expand Down
3 changes: 2 additions & 1 deletion src/NetworkManager.ts
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/
import { EventEmitter } from './EventEmitter';
import { helper, assert, debugError } from './helper';
import { assert } from './assert';
import { helper, debugError } from './helper';
import Protocol from './protocol';
import { Events } from './Events';
import { CDPSession } from './Connection';
Expand Down
3 changes: 2 additions & 1 deletion src/Page.ts
Expand Up @@ -24,7 +24,8 @@ import { EmulationManager } from './EmulationManager';
import { Frame, FrameManager } from './FrameManager';
import { Keyboard, Mouse, Touchscreen, MouseButtonInput } from './Input';
import { Tracing } from './Tracing';
import { helper, debugError, assert } from './helper';
import { assert } from './assert';
import { helper, debugError } from './helper';
import { Coverage } from './Coverage';
import { WebWorker } from './WebWorker';
import { Browser, BrowserContext } from './Browser';
Expand Down
3 changes: 2 additions & 1 deletion src/Tracing.ts
Expand Up @@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { helper, assert } from './helper';
import { assert } from './assert';
import { helper } from './helper';
import { CDPSession } from './Connection';

interface TracingOptions {
Expand Down
24 changes: 24 additions & 0 deletions src/assert.ts
@@ -0,0 +1,24 @@
/**
* Copyright 2020 Google Inc. All rights reserved.
*
* 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.
*/

/**
* Asserts that the given value is truthy.
* @param value
* @param message - the error message to throw if the value is not truthy.
*/
export const assert = (value: unknown, message?: string): void => {
if (!value) throw new Error(message);
};
5 changes: 1 addition & 4 deletions src/helper.ts
Expand Up @@ -20,17 +20,14 @@ import { CDPSession } from './Connection';
import { promisify } from 'util';
import Protocol from './protocol';
import { CommonEventEmitter } from './EventEmitter';
import { assert } from './assert';

const openAsync = promisify(fs.open);
const writeAsync = promisify(fs.write);
const closeAsync = promisify(fs.close);

export const debugError = debug('puppeteer:error');

export function assert(value: unknown, message?: string): void {
if (!value) throw new Error(message);
}

interface AnyClass {
prototype: object;
}
Expand Down
3 changes: 2 additions & 1 deletion src/launcher/BrowserRunner.ts
Expand Up @@ -18,7 +18,8 @@ import debug from 'debug';

import removeFolder from 'rimraf';
import * as childProcess from 'child_process';
import { helper, assert, debugError } from '../helper';
import { assert } from '../assert';
import { helper, debugError } from '../helper';
import { LaunchOptions } from './LaunchOptions';
import { Connection } from '../Connection';
import { WebSocketTransport } from '../WebSocketTransport';
Expand Down

0 comments on commit e4de5f1

Please sign in to comment.