chore: refactor init to make sure it works with paused targets (#8710)

This commit is contained in:
Alex Rudenko 2022-07-29 12:42:25 +02:00 committed by GitHub
parent 7580a1be01
commit 8477105e09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,7 +22,12 @@ import {Frame} from './FrameManager.js';
import {HTTPRequest} from './HTTPRequest.js'; import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js'; import {HTTPResponse} from './HTTPResponse.js';
import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js'; import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js';
import {debugError, isString} from './util.js'; import {
debugError,
isString,
createDeferredPromiseWithTimer,
DeferredPromise,
} from './util.js';
/** /**
* @public * @public
@ -95,6 +100,7 @@ export class NetworkManager extends EventEmitter {
download: -1, download: -1,
latency: 0, latency: 0,
}; };
#deferredInitPromise?: DeferredPromise<void>;
constructor( constructor(
client: CDPSession, client: CDPSession,
@ -131,13 +137,34 @@ export class NetworkManager extends EventEmitter {
); );
} }
async initialize(): Promise<void> { /**
await this.#client.send('Network.enable'); * Initialize calls should avoid async dependencies between CDP calls as those
if (this.#ignoreHTTPSErrors) { * might not resolve until after the target is resumed causing a deadlock.
await this.#client.send('Security.setIgnoreCertificateErrors', { */
ignore: true, initialize(): Promise<void> {
}); if (this.#deferredInitPromise) {
return this.#deferredInitPromise.promise;
} }
this.#deferredInitPromise = createDeferredPromiseWithTimer<void>(
'NetworkManager initialization timed out',
30000
);
const init = Promise.all([
this.#ignoreHTTPSErrors
? this.#client.send('Security.setIgnoreCertificateErrors', {
ignore: true,
})
: null,
this.#client.send('Network.enable'),
]);
init
.then(() => {
this.#deferredInitPromise?.resolve();
})
.catch(err => {
return this.#deferredInitPromise?.reject(err);
});
return this.#deferredInitPromise.promise;
} }
async authenticate(credentials?: Credentials): Promise<void> { async authenticate(credentials?: Credentials): Promise<void> {