chore: migrate src/helpers.ts to ESM (#5699)

* 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.
This commit is contained in:
Jack Franklin 2020-04-21 10:22:20 +01:00 committed by GitHub
parent f13c30a9ec
commit 3600f2f99b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 52 deletions

View File

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

View File

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

View File

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

View File

@ -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';
@ -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);
}
@ -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);
@ -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
src/types.d.ts vendored
View File

@ -1,25 +0,0 @@
/**
* 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.
*/
/* These types exist here until we migrate over to ESM where we can
* import / export them properly from modules - TS doesn't support
* exposing interfaces in CommonJS land.
*/
interface PuppeteerEventListener {
emitter: NodeJS.EventEmitter;
eventName: string | symbol;
handler: (...args: any[]) => void;
}