microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/IWDPHelper.ts
86lines · modeblame
259c018fYuri Skorokhodov5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
09f6024fHeniker4 years ago | 4 | import * as cp from "child_process"; |
259c018fYuri Skorokhodov5 years ago | 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 | | |
0d77292aJiglioNero4 years ago | 22 | public async startiOSWebkitDebugProxy( |
34472878RedMickey5 years ago | 23 | proxyPort: number, |
| 24 | proxyRangeStart: number, | |
| 25 | proxyRangeEnd: number, | |
| 26 | ): Promise<void> { | |
259c018fYuri Skorokhodov5 years ago | 27 | return new Promise((resolve, reject) => { |
| 28 | this.cleanUp(); | |
| 29 | | |
09f6024fHeniker4 years ago | 30 | const portRange = `null:${proxyPort},:${proxyRangeStart}-${proxyRangeEnd}`; |
77e86943lexie0111 years ago | 31 | this.iOSWebkitDebugProxyProcess = cp.spawn( |
| 32 | "ios_webkit_debug_proxy", | |
| 33 | ["-c", portRange], | |
| 34 | { shell: true }, | |
| 35 | ); | |
34472878RedMickey5 years ago | 36 | this.iOSWebkitDebugProxyProcess.on("error", err => { |
09f6024fHeniker4 years ago | 37 | reject(new Error(`Unable to start ios_webkit_debug_proxy: ${String(err)}`)); |
259c018fYuri Skorokhodov5 years ago | 38 | }); |
| 39 | // Allow some time for the spawned process to error out | |
09f6024fHeniker4 years ago | 40 | void PromiseUtil.delay(250).then(() => resolve()); |
259c018fYuri Skorokhodov5 years ago | 41 | }); |
| 42 | } | |
| 43 | | |
0d77292aJiglioNero4 years ago | 44 | public async getSimulatorProxyPort( |
34472878RedMickey5 years ago | 45 | attachArgs: IAttachRequestArgs, |
| 46 | ): Promise<{ targetPort: number; iOSVersion: string }> { | |
0d77292aJiglioNero4 years ago | 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 | // }] | |
09f6024fHeniker4 years ago | 56 | const endpointsList = JSON.parse(response); |
259c018fYuri Skorokhodov5 years ago | 57 | |
0d77292aJiglioNero4 years ago | 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 | } | |
259c018fYuri Skorokhodov5 years ago | 66 | |
09f6024fHeniker4 years ago | 67 | const device = devices[0]; |
0d77292aJiglioNero4 years ago | 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 | } | |
d55f3c22Yuri Skorokhodov5 years ago | 78 | } |
259c018fYuri Skorokhodov5 years ago | 79 | |
| 80 | public cleanUp(): void { | |
| 81 | if (this.iOSWebkitDebugProxyProcess) { | |
| 82 | this.iOSWebkitDebugProxyProcess.kill(); | |
| 83 | this.iOSWebkitDebugProxyProcess = null; | |
| 84 | } | |
| 85 | } | |
34472878RedMickey5 years ago | 86 | } |