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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import helper = require('./helper'); import {assert} from './helper';
const {assert} = helper;
import EventsModule = require('./Events'); import EventsModule = require('./Events');
const {Events} = EventsModule; const {Events} = EventsModule;

View File

@ -14,11 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
import helpers = require('./helper'); import {assert} from './helper';
import {CDPSession} from './Connection'; import {CDPSession} from './Connection';
const {assert} = helpers;
enum DialogType { enum DialogType {
Alert = 'alert', Alert = 'alert',
BeforeUnload = 'beforeunload', BeforeUnload = 'beforeunload',

View File

@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import helperUtils = require('./helper'); import {helper, debugError, PuppeteerEventListener} from './helper';
const {helper, debugError} = helperUtils;
class PipeTransport implements Puppeteer.ConnectionTransport { class PipeTransport implements Puppeteer.ConnectionTransport {
_pipeWrite: NodeJS.WritableStream; _pipeWrite: NodeJS.WritableStream;

View File

@ -16,7 +16,6 @@
import Errors = require('./Errors'); import Errors = require('./Errors');
import debug = require('debug'); import debug = require('debug');
const debugError = debug('puppeteer:error');
import fs = require('fs'); import fs = require('fs');
import promisify = require('./promisify'); import promisify = require('./promisify');
import {CDPSession} from './Connection'; import {CDPSession} from './Connection';
@ -27,7 +26,9 @@ const openAsync = promisify(fs.open);
const writeAsync = promisify(fs.write); const writeAsync = promisify(fs.write);
const closeAsync = promisify(fs.close); 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) if (!value)
throw new Error(message); 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 { function addEventListener(emitter: NodeJS.EventEmitter, eventName: string|symbol, handler: (...args: any[]) => void): PuppeteerEventListener {
emitter.on(eventName, handler); emitter.on(eventName, handler);
@ -212,22 +218,18 @@ async function readProtocolStream(client: CDPSession, handle: string, path?: str
} }
export = { export const helper = {
helper: { promisify,
promisify, evaluationString,
evaluationString, readProtocolStream,
readProtocolStream, waitWithTimeout,
waitWithTimeout, waitForEvent,
waitForEvent, isString,
isString, isNumber,
isNumber, addEventListener,
addEventListener, removeEventListeners,
removeEventListeners, valueFromRemoteObject,
valueFromRemoteObject, installAsyncStackHooks,
installAsyncStackHooks, getExceptionMessage,
getExceptionMessage, releaseObject,
releaseObject,
},
assert,
debugError
}; };

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