microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.7.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/IWDPHelper.ts

82lines · 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
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 {
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 async startiOSWebkitDebugProxy(
23 proxyPort: number,
24 proxyRangeStart: number,
25 proxyRangeEnd: number,
26 ): Promise<void> {
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]);
32 this.iOSWebkitDebugProxyProcess.on("error", err => {
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
40 public async getSimulatorProxyPort(
41 attachArgs: IAttachRequestArgs,
42 ): Promise<{ targetPort: number; iOSVersion: string }> {
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);
53
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 }
62
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 }
74 }
75
76 public cleanUp(): void {
77 if (this.iOSWebkitDebugProxyProcess) {
78 this.iOSWebkitDebugProxyProcess.kill();
79 this.iOSWebkitDebugProxyProcess = null;
80 }
81 }
82}
83