microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/IWDPHelper.ts
82lines · 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 | | |
| 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 | | |
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 | | |
| 30 | let portRange = `null:${proxyPort},:${proxyRangeStart}-${proxyRangeEnd}`; | |
| 31 | this.iOSWebkitDebugProxyProcess = cp.spawn("ios_webkit_debug_proxy", ["-c", portRange]); | |
34472878RedMickey5 years ago | 32 | this.iOSWebkitDebugProxyProcess.on("error", err => { |
259c018fYuri Skorokhodov5 years ago | 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 | | |
0d77292aJiglioNero4 years ago | 40 | public async getSimulatorProxyPort( |
34472878RedMickey5 years ago | 41 | attachArgs: IAttachRequestArgs, |
| 42 | ): Promise<{ targetPort: number; iOSVersion: string }> { | |
0d77292aJiglioNero4 years ago | 43 | const response = await Request.request(`http://localhost:${attachArgs.port}/json`, true); |
| 44 | try { | |
| 45 | // An example of a json response from IWDP | |
| 46 | // [{ | |
| 47 | // "deviceId": "00008020-XXXXXXXXXXXXXXXX", | |
| 48 | // "deviceName": "iPhone name", | |
| 49 | // "deviceOSVersion": "13.4.1", | |
| 50 | // "url": "localhost:9223" | |
| 51 | // }] | |
| 52 | let endpointsList = JSON.parse(response); | |
259c018fYuri Skorokhodov5 years ago | 53 | |
0d77292aJiglioNero4 years ago | 54 | let devices = endpointsList; |
| 55 | if (attachArgs.target) { | |
| 56 | devices = endpointsList.filter((entry: { deviceId: string }) => | |
| 57 | attachArgs.target?.toLowerCase() === "device" | |
| 58 | ? entry.deviceId !== "SIMULATOR" | |
| 59 | : entry.deviceId === "SIMULATOR", | |
| 60 | ); | |
| 61 | } | |
259c018fYuri Skorokhodov5 years ago | 62 | |
0d77292aJiglioNero4 years ago | 63 | let device = devices[0]; |
| 64 | // device.url is of the form 'localhost:port' | |
| 65 | return { | |
| 66 | targetPort: parseInt(device.url.split(":")[1], 10), | |
| 67 | iOSVersion: device.deviceOSVersion, | |
| 68 | }; | |
| 69 | } catch (e) { | |
| 70 | throw ErrorHelper.getInternalError( | |
| 71 | InternalErrorCode.IOSCouldNotFoundDeviceForDirectDebugging, | |
| 72 | ); | |
| 73 | } | |
d55f3c22Yuri Skorokhodov5 years ago | 74 | } |
259c018fYuri Skorokhodov5 years ago | 75 | |
| 76 | public cleanUp(): void { | |
| 77 | if (this.iOSWebkitDebugProxyProcess) { | |
| 78 | this.iOSWebkitDebugProxyProcess.kill(); | |
| 79 | this.iOSWebkitDebugProxyProcess = null; | |
| 80 | } | |
| 81 | } | |
34472878RedMickey5 years ago | 82 | } |