microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/IWDPHelper.ts
85lines · 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 { PromiseUtil } from "../../common/node/promise"; |
| 5 | import { Request } from "../../common/node/request"; |
| 6 | import { IAttachRequestArgs } from "../debugSessionBase"; |
| 7 | import * as cp from "child_process"; |
| 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 startiOSWebkitDebugProxy( |
| 23 | proxyPort: number, |
| 24 | proxyRangeStart: number, |
| 25 | proxyRangeEnd: number, |
| 26 | ): Promise<void> { |
| 27 | return new Promise((resolve, reject) => { |
| 28 | this.cleanUp(); |
| 29 | |
| 30 | let portRange = `null:${proxyPort},:${proxyRangeStart}-${proxyRangeEnd}`; |
| 31 | this.iOSWebkitDebugProxyProcess = cp.spawn("ios_webkit_debug_proxy", ["-c", portRange]); |
| 32 | this.iOSWebkitDebugProxyProcess.on("error", err => { |
| 33 | reject(new Error("Unable to start ios_webkit_debug_proxy: " + err)); |
| 34 | }); |
| 35 | // Allow some time for the spawned process to error out |
| 36 | PromiseUtil.delay(250).then(() => resolve()); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | public getSimulatorProxyPort( |
| 41 | attachArgs: IAttachRequestArgs, |
| 42 | ): Promise<{ targetPort: number; iOSVersion: string }> { |
| 43 | return Request.request(`http://localhost:${attachArgs.port}/json`, true).then( |
| 44 | (response: string) => { |
| 45 | try { |
| 46 | // An example of a json response from IWDP |
| 47 | // [{ |
| 48 | // "deviceId": "00008020-XXXXXXXXXXXXXXXX", |
| 49 | // "deviceName": "iPhone name", |
| 50 | // "deviceOSVersion": "13.4.1", |
| 51 | // "url": "localhost:9223" |
| 52 | // }] |
| 53 | let endpointsList = JSON.parse(response); |
| 54 | |
| 55 | let devices = endpointsList; |
| 56 | if (attachArgs.target) { |
| 57 | devices = endpointsList.filter((entry: { deviceId: string }) => |
| 58 | attachArgs.target?.toLowerCase() === "device" |
| 59 | ? entry.deviceId !== "SIMULATOR" |
| 60 | : entry.deviceId === "SIMULATOR", |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | let device = devices[0]; |
| 65 | // device.url is of the form 'localhost:port' |
| 66 | return Promise.resolve({ |
| 67 | targetPort: parseInt(device.url.split(":")[1], 10), |
| 68 | iOSVersion: device.deviceOSVersion, |
| 69 | }); |
| 70 | } catch (e) { |
| 71 | throw ErrorHelper.getInternalError( |
| 72 | InternalErrorCode.IOSCouldNotFoundDeviceForDirectDebugging, |
| 73 | ); |
| 74 | } |
| 75 | }, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | public cleanUp(): void { |
| 80 | if (this.iOSWebkitDebugProxyProcess) { |
| 81 | this.iOSWebkitDebugProxyProcess.kill(); |
| 82 | this.iOSWebkitDebugProxyProcess = null; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |