microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/IWDPHelper.ts
86lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as cp from "child_process"; |
| 5 | import { PromiseUtil } from "../../common/node/promise"; |
| 6 | import { Request } from "../../common/node/request"; |
| 7 | import { IAttachRequestArgs } from "../debugSessionBase"; |
| 8 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 9 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 10 | |
| 11 | /** |
| 12 | * Helper class to control [ios-webkit-debug-proxy](https://github.com/google/ios-webkit-debug-proxy) |
| 13 | */ |
| 14 | export class IWDPHelper { |
| 15 | private iOSWebkitDebugProxyProcess: cp.ChildProcess | null; |
| 16 | public static readonly iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT: number = 9221; |
| 17 | |
| 18 | constructor() { |
| 19 | this.iOSWebkitDebugProxyProcess = null; |
| 20 | } |
| 21 | |
| 22 | public async startiOSWebkitDebugProxy( |
| 23 | proxyPort: number, |
| 24 | proxyRangeStart: number, |
| 25 | proxyRangeEnd: number, |
| 26 | ): Promise<void> { |
| 27 | return new Promise((resolve, reject) => { |
| 28 | this.cleanUp(); |
| 29 | |
| 30 | const portRange = `null:${proxyPort},:${proxyRangeStart}-${proxyRangeEnd}`; |
| 31 | this.iOSWebkitDebugProxyProcess = cp.spawn( |
| 32 | "ios_webkit_debug_proxy", |
| 33 | ["-c", portRange], |
| 34 | { shell: true }, |
| 35 | ); |
| 36 | this.iOSWebkitDebugProxyProcess.on("error", err => { |
| 37 | reject(new Error(`Unable to start ios_webkit_debug_proxy: ${String(err)}`)); |
| 38 | }); |
| 39 | // Allow some time for the spawned process to error out |
| 40 | void PromiseUtil.delay(250).then(() => resolve()); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | public async getSimulatorProxyPort( |
| 45 | attachArgs: IAttachRequestArgs, |
| 46 | ): Promise<{ targetPort: number; iOSVersion: string }> { |
| 47 | const response = await Request.request(`http://localhost:${attachArgs.port}/json`, true); |
| 48 | try { |
| 49 | // An example of a json response from IWDP |
| 50 | // [{ |
| 51 | // "deviceId": "00008020-XXXXXXXXXXXXXXXX", |
| 52 | // "deviceName": "iPhone name", |
| 53 | // "deviceOSVersion": "13.4.1", |
| 54 | // "url": "localhost:9223" |
| 55 | // }] |
| 56 | const endpointsList = JSON.parse(response); |
| 57 | |
| 58 | let devices = endpointsList; |
| 59 | if (attachArgs.target) { |
| 60 | devices = endpointsList.filter((entry: { deviceId: string }) => |
| 61 | attachArgs.target?.toLowerCase() === "device" |
| 62 | ? entry.deviceId !== "SIMULATOR" |
| 63 | : entry.deviceId === "SIMULATOR", |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | const device = devices[0]; |
| 68 | // device.url is of the form 'localhost:port' |
| 69 | return { |
| 70 | targetPort: parseInt(device.url.split(":")[1], 10), |
| 71 | iOSVersion: device.deviceOSVersion, |
| 72 | }; |
| 73 | } catch (e) { |
| 74 | throw ErrorHelper.getInternalError( |
| 75 | InternalErrorCode.IOSCouldNotFoundDeviceForDirectDebugging, |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public cleanUp(): void { |
| 81 | if (this.iOSWebkitDebugProxyProcess) { |
| 82 | this.iOSWebkitDebugProxyProcess.kill(); |
| 83 | this.iOSWebkitDebugProxyProcess = null; |
| 84 | } |
| 85 | } |
| 86 | } |