From e4de5f10d01b5bfb0ce686b6c32a38b57d80e12c Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Mon, 15 Jun 2020 16:34:50 +0100 Subject: [PATCH] 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. --- src/Browser.ts | 3 ++- src/BrowserFetcher.ts | 4 ++-- src/Connection.ts | 2 +- src/Coverage.ts | 3 ++- src/DOMWorld.ts | 3 ++- src/Dialog.ts | 2 +- src/ExecutionContext.ts | 3 ++- src/FileChooser.ts | 2 +- src/FrameManager.ts | 3 ++- src/HTTPRequest.ts | 3 ++- src/Input.ts | 2 +- src/JSHandle.ts | 3 ++- src/Launcher.ts | 3 ++- src/LifecycleWatcher.ts | 3 ++- src/NetworkManager.ts | 3 ++- src/Page.ts | 3 ++- src/Tracing.ts | 3 ++- src/assert.ts | 24 ++++++++++++++++++++++++ src/helper.ts | 5 +---- src/launcher/BrowserRunner.ts | 3 ++- 20 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 src/assert.ts diff --git a/src/Browser.ts b/src/Browser.ts index 5ac2fec581d7f..10f7e7279a9a3 100644 --- a/src/Browser.ts +++ b/src/Browser.ts @@ -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'; diff --git a/src/BrowserFetcher.ts b/src/BrowserFetcher.ts index 4eacc6e7eef8d..3fd2d72dc9ad8 100644 --- a/src/BrowserFetcher.ts +++ b/src/BrowserFetcher.ts @@ -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 = { diff --git a/src/Connection.ts b/src/Connection.ts index 2ff5a75476f38..a2b28fa97fe61 100644 --- a/src/Connection.ts +++ b/src/Connection.ts @@ -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 ►'); diff --git a/src/Coverage.ts b/src/Coverage.ts index 0d90c5e7c3dd4..5f8bff8463076 100644 --- a/src/Coverage.ts +++ b/src/Coverage.ts @@ -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'; diff --git a/src/DOMWorld.ts b/src/DOMWorld.ts index cc30a458b0825..678ddc4300dd1 100644 --- a/src/DOMWorld.ts +++ b/src/DOMWorld.ts @@ -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'; diff --git a/src/Dialog.ts b/src/Dialog.ts index 716c467c2e85e..775d10dbb1c81 100644 --- a/src/Dialog.ts +++ b/src/Dialog.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { assert } from './helper'; +import { assert } from './assert'; import { CDPSession } from './Connection'; import Protocol from './protocol'; diff --git a/src/ExecutionContext.ts b/src/ExecutionContext.ts index c82ad8443d785..32db1a0c906a5 100644 --- a/src/ExecutionContext.ts +++ b/src/ExecutionContext.ts @@ -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'; diff --git a/src/FileChooser.ts b/src/FileChooser.ts index ed6303451c477..99a2b14281219 100644 --- a/src/FileChooser.ts +++ b/src/FileChooser.ts @@ -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; diff --git a/src/FrameManager.ts b/src/FrameManager.ts index d16ab3698ac39..3ac37085ba73e 100644 --- a/src/FrameManager.ts +++ b/src/FrameManager.ts @@ -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'; diff --git a/src/HTTPRequest.ts b/src/HTTPRequest.ts index b8ed5057800bf..7df17ee24eed8 100644 --- a/src/HTTPRequest.ts +++ b/src/HTTPRequest.ts @@ -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 { diff --git a/src/Input.ts b/src/Input.ts index 0fef07d29339a..7a49b224b122a 100644 --- a/src/Input.ts +++ b/src/Input.ts @@ -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'; diff --git a/src/JSHandle.ts b/src/JSHandle.ts index d8be11c637fca..38e814eea8438 100644 --- a/src/JSHandle.ts +++ b/src/JSHandle.ts @@ -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'; diff --git a/src/Launcher.ts b/src/Launcher.ts index 0a0e9f7cf14f4..60e977cdc3a50 100644 --- a/src/Launcher.ts +++ b/src/Launcher.ts @@ -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'; diff --git a/src/LifecycleWatcher.ts b/src/LifecycleWatcher.ts index 775453374c752..7f400911f1ee4 100644 --- a/src/LifecycleWatcher.ts +++ b/src/LifecycleWatcher.ts @@ -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'; diff --git a/src/NetworkManager.ts b/src/NetworkManager.ts index 8043744f6112d..46ed595ba81d9 100644 --- a/src/NetworkManager.ts +++ b/src/NetworkManager.ts @@ -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'; diff --git a/src/Page.ts b/src/Page.ts index be1a593df63c1..c5f51179e80bd 100644 --- a/src/Page.ts +++ b/src/Page.ts @@ -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'; diff --git a/src/Tracing.ts b/src/Tracing.ts index 8f19840a54aea..5f2c4a4880a88 100644 --- a/src/Tracing.ts +++ b/src/Tracing.ts @@ -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 { diff --git a/src/assert.ts b/src/assert.ts new file mode 100644 index 0000000000000..6ba090ce26e85 --- /dev/null +++ b/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); +}; diff --git a/src/helper.ts b/src/helper.ts index c1103e9104fde..03b4a8c634334 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -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; } diff --git a/src/launcher/BrowserRunner.ts b/src/launcher/BrowserRunner.ts index 7b571cabb26fc..03ea2becbc78b 100644 --- a/src/launcher/BrowserRunner.ts +++ b/src/launcher/BrowserRunner.ts @@ -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';