chore: move assert into its own module (#6021)

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.
This commit is contained in:
Jack Franklin 2020-06-15 16:34:50 +01:00 committed by GitHub
parent f1ec6a3df0
commit e4de5f10d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 57 additions and 23 deletions

View File

@ -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';

View File

@ -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 = {

View File

@ -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 ►');

View File

@ -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';

View File

@ -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';

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
import { assert } from './helper';
import { assert } from './assert';
import { CDPSession } from './Connection';
import Protocol from './protocol';

View File

@ -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';

View File

@ -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;

View File

@ -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';

View File

@ -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 {

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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 {

24
src/assert.ts Normal file
View File

@ -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);
};

View File

@ -20,6 +20,7 @@ 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);
@ -27,10 +28,6 @@ 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;
}

View File

@ -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';