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