Skip to content

Commit

Permalink
chore: migrate src/NetworkManager to TypeScript (#5774)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfranklin committed Apr 30, 2020
1 parent 862eea8 commit 8654d63
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 302 deletions.
14 changes: 7 additions & 7 deletions src/FrameManager.ts
Expand Up @@ -20,7 +20,7 @@ import {Events} from './Events';
import {ExecutionContext, EVALUATION_SCRIPT_URL} from './ExecutionContext';
import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher';
import {DOMWorld, WaitForSelectorOptions} from './DOMWorld';
import {NetworkManager} from './NetworkManager';
import {NetworkManager, Response} from './NetworkManager';
import {TimeoutSettings} from './TimeoutSettings';
import {CDPSession} from './Connection';
import {JSHandle, ElementHandle} from './JSHandle';
Expand All @@ -31,7 +31,7 @@ const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
export class FrameManager extends EventEmitter {
_client: CDPSession;
_page: Puppeteer.Page;
_networkManager: Puppeteer.NetworkManager;
_networkManager: NetworkManager;
_timeoutSettings: TimeoutSettings;
_frames = new Map<string, Frame>();
_contextIdToContext = new Map<number, ExecutionContext>();
Expand Down Expand Up @@ -70,11 +70,11 @@ export class FrameManager extends EventEmitter {
]);
}

networkManager(): Puppeteer.NetworkManager {
networkManager(): NetworkManager {
return this._networkManager;
}

async navigateFrame(frame: Frame, url: string, options: {referer?: string; timeout?: number; waitUntil?: PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]} = {}): Promise<Puppeteer.Response | null> {
async navigateFrame(frame: Frame, url: string, options: {referer?: string; timeout?: number; waitUntil?: PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]} = {}): Promise<Response | null> {
assertNoLegacyNavigationOptions(options);
const {
referer = this._networkManager.extraHTTPHeaders()['referer'],
Expand Down Expand Up @@ -110,7 +110,7 @@ export class FrameManager extends EventEmitter {
}
}

async waitForFrameNavigation(frame: Frame, options: {timeout?: number; waitUntil?: PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]} = {}): Promise<Puppeteer.Response | null> {
async waitForFrameNavigation(frame: Frame, options: {timeout?: number; waitUntil?: PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]} = {}): Promise<Response | null> {
assertNoLegacyNavigationOptions(options);
const {
waitUntil = ['load'],
Expand Down Expand Up @@ -333,11 +333,11 @@ export class Frame {
this._parentFrame._childFrames.add(this);
}

async goto(url: string, options: {referer?: string; timeout?: number; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]}): Promise<Puppeteer.Response | null> {
async goto(url: string, options: {referer?: string; timeout?: number; waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]}): Promise<Response | null> {
return await this._frameManager.navigateFrame(this, url, options);
}

async waitForNavigation(options: {timeout?: number; waitUntil?: PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]}): Promise<Puppeteer.Response | null> {
async waitForNavigation(options: {timeout?: number; waitUntil?: PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]}): Promise<Response | null> {
return await this._frameManager.waitForFrameNavigation(this, options);
}

Expand Down
8 changes: 4 additions & 4 deletions src/LifecycleWatcher.ts
Expand Up @@ -18,6 +18,7 @@ import {helper, assert, PuppeteerEventListener} from './helper';
import {Events} from './Events';
import {TimeoutError} from './Errors';
import {FrameManager, Frame} from './FrameManager';
import {Request, Response} from './NetworkManager';

export type PuppeteerLifeCycleEvent = 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2';
type ProtocolLifeCycleEvent = 'load' | 'DOMContentLoaded' | 'networkIdle' | 'networkAlmostIdle';
Expand All @@ -35,7 +36,7 @@ export class LifecycleWatcher {
_frameManager: FrameManager;
_frame: Frame;
_timeout: number;
_navigationRequest?: Puppeteer.Request;
_navigationRequest?: Request;
_eventListeners: PuppeteerEventListener[];
_initialLoaderId: string;

Expand Down Expand Up @@ -72,7 +73,6 @@ export class LifecycleWatcher {
this._frame = frame;
this._initialLoaderId = frame._loaderId;
this._timeout = timeout;
/** @type {?Puppeteer.Request} */
this._navigationRequest = null;
this._eventListeners = [
helper.addEventListener(frameManager._client, Events.CDPSession.Disconnected, () => this._terminate(new Error('Navigation failed because browser has disconnected!'))),
Expand Down Expand Up @@ -101,7 +101,7 @@ export class LifecycleWatcher {
this._checkLifecycleComplete();
}

_onRequest(request: Puppeteer.Request): void {
_onRequest(request: Request): void {
if (request.frame() !== this._frame || !request.isNavigationRequest())
return;
this._navigationRequest = request;
Expand All @@ -115,7 +115,7 @@ export class LifecycleWatcher {
this._checkLifecycleComplete();
}

navigationResponse(): Puppeteer.Response | null {
navigationResponse(): Response | null {
return this._navigationRequest ? this._navigationRequest.response() : null;
}

Expand Down

0 comments on commit 8654d63

Please sign in to comment.