microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
78c2aaee56e1edfea55cdebbdccbf5541dff7beb

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/direct/IWDPHelper.ts

82lines · modeblame

259c018fYuri Skorokhodov5 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import { PromiseUtil } from "../../common/node/promise";
5import { Request } from "../../common/node/request";
6import { IAttachRequestArgs } from "../debugSessionBase";
7import * as cp from "child_process";
8import { ErrorHelper } from "../../common/error/errorHelper";
9import { 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*/
14export class IWDPHelper {
15private iOSWebkitDebugProxyProcess: cp.ChildProcess | null;
16public static readonly iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT: number = 9221;
17
18constructor() {
19this.iOSWebkitDebugProxyProcess = null;
20}
21
0d77292aJiglioNero4 years ago22public async startiOSWebkitDebugProxy(
34472878RedMickey5 years ago23proxyPort: number,
24proxyRangeStart: number,
25proxyRangeEnd: number,
26): Promise<void> {
259c018fYuri Skorokhodov5 years ago27return new Promise((resolve, reject) => {
28this.cleanUp();
29
30let portRange = `null:${proxyPort},:${proxyRangeStart}-${proxyRangeEnd}`;
31this.iOSWebkitDebugProxyProcess = cp.spawn("ios_webkit_debug_proxy", ["-c", portRange]);
34472878RedMickey5 years ago32this.iOSWebkitDebugProxyProcess.on("error", err => {
259c018fYuri Skorokhodov5 years ago33reject(new Error("Unable to start ios_webkit_debug_proxy: " + err));
34});
35// Allow some time for the spawned process to error out
36PromiseUtil.delay(250).then(() => resolve());
37});
38}
39
0d77292aJiglioNero4 years ago40public async getSimulatorProxyPort(
34472878RedMickey5 years ago41attachArgs: IAttachRequestArgs,
42): Promise<{ targetPort: number; iOSVersion: string }> {
0d77292aJiglioNero4 years ago43const response = await Request.request(`http://localhost:${attachArgs.port}/json`, true);
44try {
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// }]
52let endpointsList = JSON.parse(response);
259c018fYuri Skorokhodov5 years ago53
0d77292aJiglioNero4 years ago54let devices = endpointsList;
55if (attachArgs.target) {
56devices = endpointsList.filter((entry: { deviceId: string }) =>
57attachArgs.target?.toLowerCase() === "device"
58? entry.deviceId !== "SIMULATOR"
59: entry.deviceId === "SIMULATOR",
60);
61}
259c018fYuri Skorokhodov5 years ago62
0d77292aJiglioNero4 years ago63let device = devices[0];
64// device.url is of the form 'localhost:port'
65return {
66targetPort: parseInt(device.url.split(":")[1], 10),
67iOSVersion: device.deviceOSVersion,
68};
69} catch (e) {
70throw ErrorHelper.getInternalError(
71InternalErrorCode.IOSCouldNotFoundDeviceForDirectDebugging,
72);
73}
d55f3c22Yuri Skorokhodov5 years ago74}
259c018fYuri Skorokhodov5 years ago75
76public cleanUp(): void {
77if (this.iOSWebkitDebugProxyProcess) {
78this.iOSWebkitDebugProxyProcess.kill();
79this.iOSWebkitDebugProxyProcess = null;
80}
81}
34472878RedMickey5 years ago82}