microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.5.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/IWDPHelper.ts

85lines · 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
34472878RedMickey5 years ago22public startiOSWebkitDebugProxy(
23proxyPort: 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
34472878RedMickey5 years ago40public getSimulatorProxyPort(
41attachArgs: IAttachRequestArgs,
42): Promise<{ targetPort: number; iOSVersion: string }> {
43return Request.request(`http://localhost:${attachArgs.port}/json`, true).then(
44(response: string) => {
259c018fYuri Skorokhodov5 years ago45try {
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// }]
53let endpointsList = JSON.parse(response);
54
55let devices = endpointsList;
56if (attachArgs.target) {
57devices = endpointsList.filter((entry: { deviceId: string }) =>
34472878RedMickey5 years ago58attachArgs.target?.toLowerCase() === "device"
59? entry.deviceId !== "SIMULATOR"
60: entry.deviceId === "SIMULATOR",
259c018fYuri Skorokhodov5 years ago61);
62}
63
64let device = devices[0];
65// device.url is of the form 'localhost:port'
66return Promise.resolve({
67targetPort: parseInt(device.url.split(":")[1], 10),
68iOSVersion: device.deviceOSVersion,
69});
70} catch (e) {
34472878RedMickey5 years ago71throw ErrorHelper.getInternalError(
72InternalErrorCode.IOSCouldNotFoundDeviceForDirectDebugging,
73);
259c018fYuri Skorokhodov5 years ago74}
34472878RedMickey5 years ago75},
76);
d55f3c22Yuri Skorokhodov5 years ago77}
259c018fYuri Skorokhodov5 years ago78
79public cleanUp(): void {
80if (this.iOSWebkitDebugProxyProcess) {
81this.iOSWebkitDebugProxyProcess.kill();
82this.iOSWebkitDebugProxyProcess = null;
83}
84}
34472878RedMickey5 years ago85}