Skip to content

Commit

Permalink
chore: migrate src/helpers.ts to ESM (#5699)
Browse files Browse the repository at this point in the history
* chore: migrate src/helpers.ts to ESM

Doing this means we can avoid the global `types.d.ts` file and export
the interface via ESM instead.

I would ideally like to rewrite the helper module so that it doesn't
export all the functions under the `helper` namespace, but I'll leave
that for a separate PR to keep mechanical changes to one per PR and
easier to review.
  • Loading branch information
jackfranklin committed Apr 21, 2020
1 parent f13c30a commit 3600f2f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 52 deletions.
3 changes: 1 addition & 2 deletions src/Connection.ts
Expand Up @@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import helper = require('./helper');
const {assert} = helper;
import {assert} from './helper';

import EventsModule = require('./Events');
const {Events} = EventsModule;
Expand Down
4 changes: 1 addition & 3 deletions src/Dialog.ts
Expand Up @@ -14,11 +14,9 @@
* limitations under the License.
*/

import helpers = require('./helper');
import {assert} from './helper';
import {CDPSession} from './Connection';

const {assert} = helpers;

enum DialogType {
Alert = 'alert',
BeforeUnload = 'beforeunload',
Expand Down
3 changes: 1 addition & 2 deletions src/PipeTransport.ts
Expand Up @@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import helperUtils = require('./helper');
const {helper, debugError} = helperUtils;
import {helper, debugError, PuppeteerEventListener} from './helper';

class PipeTransport implements Puppeteer.ConnectionTransport {
_pipeWrite: NodeJS.WritableStream;
Expand Down
42 changes: 22 additions & 20 deletions src/helper.ts
Expand Up @@ -16,7 +16,6 @@
import Errors = require('./Errors');

import debug = require('debug');
const debugError = debug('puppeteer:error');
import fs = require('fs');
import promisify = require('./promisify');
import {CDPSession} from './Connection';
Expand All @@ -27,7 +26,9 @@ const openAsync = promisify(fs.open);
const writeAsync = promisify(fs.write);
const closeAsync = promisify(fs.close);

function assert(value: unknown, message?: string): void {
export const debugError = debug('puppeteer:error');

export function assert(value: unknown, message?: string): void {
if (!value)
throw new Error(message);
}
Expand Down Expand Up @@ -103,6 +104,11 @@ function installAsyncStackHooks(classType: AnyClass): void {
}
}

export interface PuppeteerEventListener {
emitter: NodeJS.EventEmitter;
eventName: string | symbol;
handler: (...args: any[]) => void;
}

function addEventListener(emitter: NodeJS.EventEmitter, eventName: string|symbol, handler: (...args: any[]) => void): PuppeteerEventListener {
emitter.on(eventName, handler);
Expand Down Expand Up @@ -212,22 +218,18 @@ async function readProtocolStream(client: CDPSession, handle: string, path?: str
}


export = {
helper: {
promisify,
evaluationString,
readProtocolStream,
waitWithTimeout,
waitForEvent,
isString,
isNumber,
addEventListener,
removeEventListeners,
valueFromRemoteObject,
installAsyncStackHooks,
getExceptionMessage,
releaseObject,
},
assert,
debugError
export const helper = {
promisify,
evaluationString,
readProtocolStream,
waitWithTimeout,
waitForEvent,
isString,
isNumber,
addEventListener,
removeEventListeners,
valueFromRemoteObject,
installAsyncStackHooks,
getExceptionMessage,
releaseObject,
};
25 changes: 0 additions & 25 deletions src/types.d.ts

This file was deleted.

0 comments on commit 3600f2f

Please sign in to comment.