From ce5461d0d5277a95d2371ab532610bd134744b69 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:59:00 +0200 Subject: [PATCH] chore: don't use Object.freeze for internal properties (#10945) --- packages/puppeteer-core/src/bidi/Page.ts | 6 +++--- packages/puppeteer-core/src/cdp/AriaQueryHandler.ts | 3 +-- packages/puppeteer-core/src/cdp/NetworkManager.ts | 4 ++-- packages/puppeteer-core/src/cdp/Page.ts | 12 ++++++------ .../puppeteer-core/src/common/GetQueryHandler.ts | 9 +++------ 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/puppeteer-core/src/bidi/Page.ts b/packages/puppeteer-core/src/bidi/Page.ts index e44345c3a8a..cadcd009f7b 100644 --- a/packages/puppeteer-core/src/bidi/Page.ts +++ b/packages/puppeteer-core/src/bidi/Page.ts @@ -104,7 +104,7 @@ export class BidiPage extends Page { ], ['browsingContext.userPromptOpened', this.#onDialog.bind(this)], ]); - #networkManagerEvents = Object.freeze([ + readonly #networkManagerEvents = [ [ NetworkManagerEvent.Request, (request: BidiHTTPRequest) => { @@ -135,9 +135,9 @@ export class BidiPage extends Page { this.emit(PageEvent.Response, response); }, ], - ] as const); + ] as const; - #browsingContextEvents = new Map>([ + readonly #browsingContextEvents = new Map>([ [BrowsingContextEvent.Created, this.#onContextCreated.bind(this)], [BrowsingContextEvent.Destroyed, this.#onContextDestroyed.bind(this)], ]); diff --git a/packages/puppeteer-core/src/cdp/AriaQueryHandler.ts b/packages/puppeteer-core/src/cdp/AriaQueryHandler.ts index fba1147d256..7091dbf873a 100644 --- a/packages/puppeteer-core/src/cdp/AriaQueryHandler.ts +++ b/packages/puppeteer-core/src/cdp/AriaQueryHandler.ts @@ -44,11 +44,10 @@ interface ARIASelector { role?: string; } -const KNOWN_ATTRIBUTES = Object.freeze(['name', 'role']); const isKnownAttribute = ( attribute: string ): attribute is keyof ARIASelector => { - return KNOWN_ATTRIBUTES.includes(attribute); + return ['name', 'role'].includes(attribute); }; const normalizeValue = (value: string): string => { diff --git a/packages/puppeteer-core/src/cdp/NetworkManager.ts b/packages/puppeteer-core/src/cdp/NetworkManager.ts index 689e4376645..61e61017393 100644 --- a/packages/puppeteer-core/src/cdp/NetworkManager.ts +++ b/packages/puppeteer-core/src/cdp/NetworkManager.ts @@ -113,7 +113,7 @@ export class NetworkManager extends EventEmitter { #userAgent?: string; #userAgentMetadata?: Protocol.Emulation.UserAgentMetadata; - #handlers = Object.freeze([ + readonly #handlers = [ ['Fetch.requestPaused', this.#onRequestPaused], ['Fetch.authRequired', this.#onAuthRequired], ['Network.requestWillBeSent', this.#onRequestWillBeSent], @@ -123,7 +123,7 @@ export class NetworkManager extends EventEmitter { ['Network.loadingFailed', this.#onLoadingFailed], ['Network.responseReceivedExtraInfo', this.#onResponseReceivedExtraInfo], [CDPSessionEvent.Disconnected, this.#removeClient], - ] as const); + ] as const; #clients = new Map(); diff --git a/packages/puppeteer-core/src/cdp/Page.ts b/packages/puppeteer-core/src/cdp/Page.ts index b4bf27c9f9a..df89ae6327e 100644 --- a/packages/puppeteer-core/src/cdp/Page.ts +++ b/packages/puppeteer-core/src/cdp/Page.ts @@ -143,7 +143,7 @@ export class CdpPage extends Page { #serviceWorkerBypassed = false; #userDragInterceptionEnabled = false; - #frameManagerHandlers = Object.freeze([ + readonly #frameManagerHandlers = [ [ FrameManagerEvent.FrameAttached, (frame: CdpFrame) => { @@ -162,9 +162,9 @@ export class CdpPage extends Page { this.emit(PageEvent.FrameNavigated, frame); }, ], - ] as const); + ] as const; - #networkManagerHandlers = Object.freeze([ + readonly #networkManagerHandlers = [ [ NetworkManagerEvent.Request, (request: HTTPRequest) => { @@ -195,9 +195,9 @@ export class CdpPage extends Page { this.emit(PageEvent.RequestFinished, request); }, ], - ] as const); + ] as const; - #sessionHandlers = Object.freeze([ + readonly #sessionHandlers = [ [ CDPSessionEvent.Disconnected, () => { @@ -226,7 +226,7 @@ export class CdpPage extends Page { ['Performance.metrics', this.#emitMetrics.bind(this)], ['Log.entryAdded', this.#onLogEntryAdded.bind(this)], ['Page.fileChooserOpened', this.#onFileChooser.bind(this)], - ] as const); + ] as const; constructor( client: CDPSession, diff --git a/packages/puppeteer-core/src/common/GetQueryHandler.ts b/packages/puppeteer-core/src/common/GetQueryHandler.ts index 1b599aeb3db..84ac8b930fe 100644 --- a/packages/puppeteer-core/src/common/GetQueryHandler.ts +++ b/packages/puppeteer-core/src/common/GetQueryHandler.ts @@ -23,15 +23,12 @@ import type {QueryHandler} from './QueryHandler.js'; import {TextQueryHandler} from './TextQueryHandler.js'; import {XPathQueryHandler} from './XPathQueryHandler.js'; -/** - * @internal - */ -export const BUILTIN_QUERY_HANDLERS = Object.freeze({ +const BUILTIN_QUERY_HANDLERS = { aria: ARIAQueryHandler, pierce: PierceQueryHandler, xpath: XPathQueryHandler, text: TextQueryHandler, -}); +} as const; const QUERY_SEPARATORS = ['=', '/']; @@ -42,7 +39,7 @@ export function getQueryHandlerByName( name: string ): typeof QueryHandler | undefined { if (name in BUILTIN_QUERY_HANDLERS) { - return BUILTIN_QUERY_HANDLERS[name as 'aria']; + return BUILTIN_QUERY_HANDLERS[name as keyof typeof BUILTIN_QUERY_HANDLERS]; } return customQueryHandlers.get(name); }