protected async attachRequest()

in src/debugger/direct/directDebugSession.ts [103:235]


    protected async attachRequest(
        response: DebugProtocol.AttachResponse,
        attachArgs: IAttachRequestArgs,
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        request?: DebugProtocol.Request,
    ): Promise<void> {
        let extProps = {
            platform: {
                value: attachArgs.platform,
                isPii: false,
            },
            isDirect: {
                value: true,
                isPii: false,
            },
        };

        attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223;
        attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322;

        this.previousAttachArgs = attachArgs;

        try {
            await this.initializeSettings(attachArgs);

            const packager = this.appLauncher.getPackager();
            const args: Parameters<typeof packager.forMessage> = [
                // message indicates that another debugger has connected
                "Already connected:",
                {
                    type: "client_log",
                    level: "warn",
                    mode: "BRIDGE",
                },
            ];

            void packager.forMessage(...args).then(
                () => {
                    this.showError(
                        ErrorHelper.getInternalError(
                            InternalErrorCode.AnotherDebuggerConnectedToPackager,
                        ),
                        response,
                    );
                },
                () => {},
            );

            logger.log("Attaching to the application");
            logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`);

            const versions = await ProjectVersionHelper.getReactNativeVersions(
                this.projectRootPath,
                ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
            );
            extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
                attachArgs,
                versions,
                extProps,
            );

            // eslint-disable-next-line @typescript-eslint/no-unused-vars
            await TelemetryHelper.generate("attach", extProps, async generator => {
                const port = attachArgs.useHermesEngine
                    ? attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd)
                    : attachArgs.platform === PlatformType.iOS
                    ? attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT
                    : null;
                if (port === null) {
                    throw ErrorHelper.getInternalError(
                        InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
                        attachArgs.platform,
                    );
                }
                attachArgs.port = port;
                logger.log(`Connecting to ${attachArgs.port} port`);
                await this.appLauncher.getRnCdpProxy().stopServer();

                const cdpMessageHandler: BaseCDPMessageHandler | null = attachArgs.useHermesEngine
                    ? new HermesCDPMessageHandler()
                    : attachArgs.platform === PlatformType.iOS
                    ? new IOSDirectCDPMessageHandler()
                    : null;

                if (!cdpMessageHandler) {
                    throw ErrorHelper.getInternalError(
                        InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
                        attachArgs.platform,
                    );
                }
                await this.appLauncher
                    .getRnCdpProxy()
                    .initializeServer(
                        cdpMessageHandler,
                        this.cdpProxyLogLevel,
                        this.cancellationTokenSource.token,
                    );

                if (!attachArgs.useHermesEngine && attachArgs.platform === PlatformType.iOS) {
                    await this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy(
                        attachArgs.port,
                        attachArgs.webkitRangeMin,
                        attachArgs.webkitRangeMax,
                    );
                    const results = await this.iOSWKDebugProxyHelper.getSimulatorProxyPort(
                        attachArgs,
                    );
                    attachArgs.port = results.targetPort;
                }

                if (attachArgs.request === "attach") {
                    await this.preparePackagerBeforeAttach(attachArgs, versions);
                }

                const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
                    `http://localhost:${attachArgs.port}`,
                    90,
                    this.cancellationTokenSource.token,
                    attachArgs.useHermesEngine,
                );
                this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
                await this.establishDebugSession(attachArgs);
            });
        } catch (error) {
            this.showError(
                ErrorHelper.getInternalError(
                    InternalErrorCode.CouldNotAttachToDebugger,
                    error.message || error,
                ),
                response,
            );
        }
    }