|
1 |
| -import type {ConnectableObservable} from 'rxjs-compat/bundles/rxjs-compat.umd.min.js'; |
| 1 | +import type { ConnectableObservable } from "rxjs-compat/bundles/rxjs-compat.umd.min.js" |
2 | 2 |
|
3 |
| -import http from 'http'; |
4 |
| -import net from 'net'; |
5 |
| -import {Observable, Subject} from 'rxjs-compat/bundles/rxjs-compat.umd.min.js'; |
6 |
| -import {getLogger} from 'log4js'; |
7 |
| -import {sleep} from '@atom-ide-community/nuclide-commons/promise'; |
| 3 | +import http from "http" |
| 4 | +import net from "net" |
| 5 | +import { Observable, Subject } from "rxjs-compat/bundles/rxjs-compat.umd.min.js" |
| 6 | +import { getLogger } from "log4js" |
| 7 | +import { sleep } from "@atom-ide-community/nuclide-commons/promise" |
8 | 8 |
|
9 |
| -let isServerSetup = false; |
| 9 | +let isServerSetup = false |
10 | 10 |
|
11 | 11 | export type RemoteDebugCommandRequest = {
|
12 |
| - type: 'python', |
13 |
| - command: 'attach', |
| 12 | + type: "python", |
| 13 | + command: "attach", |
14 | 14 | target: PythonDebuggerAttachTarget,
|
15 |
| -}; |
| 15 | +} |
16 | 16 |
|
17 | 17 | export type PythonDebuggerAttachTarget = {
|
18 | 18 | port: number,
|
19 | 19 | localRoot: ?string,
|
20 | 20 | remoteRoot: ?string,
|
21 | 21 | debugOptions: ?Array<string>,
|
22 | 22 | id: ?string,
|
23 |
| -}; |
| 23 | +} |
24 | 24 |
|
25 |
| -const debugRequests: Subject<RemoteDebugCommandRequest> = new Subject(); |
26 |
| -const attachReady: Map<number, PythonDebuggerAttachTarget> = new Map(); |
27 |
| -const DEBUGGER_REGISTRY_PORT = 9615; |
| 25 | +const debugRequests: Subject<RemoteDebugCommandRequest> = new Subject() |
| 26 | +const attachReady: Map<number, PythonDebuggerAttachTarget> = new Map() |
| 27 | +const DEBUGGER_REGISTRY_PORT = 9615 |
28 | 28 |
|
29 |
| -export function observeRemoteDebugCommands(): ConnectableObservable< |
30 |
| - RemoteDebugCommandRequest, |
31 |
| -> { |
32 |
| - let setupStep; |
| 29 | +export function observeRemoteDebugCommands(): ConnectableObservable<RemoteDebugCommandRequest> { |
| 30 | + let setupStep |
33 | 31 | if (!isServerSetup) {
|
34 |
| - setupStep = Observable.fromPromise(setupServer()).ignoreElements(); |
| 32 | + setupStep = Observable.fromPromise(setupServer()).ignoreElements() |
35 | 33 | } else {
|
36 |
| - setupStep = Observable.empty(); |
| 34 | + setupStep = Observable.empty() |
37 | 35 | }
|
38 |
| - return setupStep.concat(debugRequests).publish(); |
| 36 | + return setupStep.concat(debugRequests).publish() |
39 | 37 | }
|
40 | 38 |
|
41 |
| -export function observeAttachDebugTargets(): ConnectableObservable< |
42 |
| - Array<PythonDebuggerAttachTarget>, |
43 |
| -> { |
| 39 | +export function observeAttachDebugTargets(): ConnectableObservable<Array<PythonDebuggerAttachTarget>> { |
44 | 40 | // Validate attach-ready values with the processes with used ports (ready to attach).
|
45 | 41 | // Note: we can't use process ids because we could be debugging processes inside containers
|
46 | 42 | // where the process ids don't map to the host running this code.
|
47 | 43 | return Observable.interval(3000)
|
48 | 44 | .startWith(0)
|
49 | 45 | .switchMap(() =>
|
50 | 46 | Promise.all(
|
51 |
| - Array.from(attachReady.values()).map(async target => { |
| 47 | + Array.from(attachReady.values()).map(async (target) => { |
52 | 48 | if (!(await isPortUsed(target.port))) {
|
53 |
| - attachReady.delete(target.port); |
| 49 | + attachReady.delete(target.port) |
54 | 50 | }
|
55 |
| - }), |
56 |
| - ), |
| 51 | + }) |
| 52 | + ) |
57 | 53 | )
|
58 | 54 | .map(() => Array.from(attachReady.values()))
|
59 |
| - .publish(); |
| 55 | + .publish() |
60 | 56 | }
|
61 | 57 |
|
62 | 58 | function isPortUsed(port: number): Promise<boolean> {
|
63 | 59 | const tryConnectPromise = new Promise((resolve, reject) => {
|
64 |
| - const client = new net.Socket(); |
| 60 | + const client = new net.Socket() |
65 | 61 | client
|
66 |
| - .once('connect', () => { |
67 |
| - cleanUp(); |
68 |
| - resolve(true); |
| 62 | + .once("connect", () => { |
| 63 | + cleanUp() |
| 64 | + resolve(true) |
| 65 | + }) |
| 66 | + .once("error", (err) => { |
| 67 | + cleanUp() |
| 68 | + resolve(err.code !== "ECONNREFUSED") |
69 | 69 | })
|
70 |
| - .once('error', err => { |
71 |
| - cleanUp(); |
72 |
| - resolve(err.code !== 'ECONNREFUSED'); |
73 |
| - }); |
74 | 70 |
|
75 | 71 | function cleanUp() {
|
76 |
| - client.removeAllListeners('connect'); |
77 |
| - client.removeAllListeners('error'); |
78 |
| - client.end(); |
79 |
| - client.destroy(); |
80 |
| - client.unref(); |
| 72 | + client.removeAllListeners("connect") |
| 73 | + client.removeAllListeners("error") |
| 74 | + client.end() |
| 75 | + client.destroy() |
| 76 | + client.unref() |
81 | 77 | }
|
82 | 78 |
|
83 |
| - client.connect({port, host: '127.0.0.1'}); |
84 |
| - }); |
| 79 | + client.connect({ port, host: "127.0.0.1" }) |
| 80 | + }) |
85 | 81 | // Trying to connect can take multiple seconds, then times out (if the server is busy).
|
86 | 82 | // Hence, we need to fallback to `true`.
|
87 |
| - const connectTimeoutPromise = sleep(1000).then(() => true); |
88 |
| - return Promise.race([tryConnectPromise, connectTimeoutPromise]); |
| 83 | + const connectTimeoutPromise = sleep(1000).then(() => true) |
| 84 | + return Promise.race([tryConnectPromise, connectTimeoutPromise]) |
89 | 85 | }
|
90 | 86 |
|
91 | 87 | function setupServer(): Promise<void> {
|
92 | 88 | return new Promise((resolve, reject) => {
|
93 | 89 | http
|
94 | 90 | .createServer((req, res) => {
|
95 |
| - if (req.method !== 'POST') { |
96 |
| - res.writeHead(500, {'Content-Type': 'text/html'}); |
97 |
| - res.end('Invalid request'); |
| 91 | + if (req.method !== "POST") { |
| 92 | + res.writeHead(500, { "Content-Type": "text/html" }) |
| 93 | + res.end("Invalid request") |
98 | 94 | } else {
|
99 |
| - let body = ''; |
100 |
| - req.on('data', data => { |
101 |
| - body += data; |
102 |
| - }); |
103 |
| - req.on('end', () => { |
104 |
| - handleJsonRequest(JSON.parse(body), res); |
105 |
| - }); |
| 95 | + let body = "" |
| 96 | + req.on("data", (data) => { |
| 97 | + body += data |
| 98 | + }) |
| 99 | + req.on("end", () => { |
| 100 | + handleJsonRequest(JSON.parse(body), res) |
| 101 | + }) |
106 | 102 | }
|
107 | 103 | })
|
108 |
| - .on('error', reject) |
| 104 | + .on("error", reject) |
109 | 105 | .listen((DEBUGGER_REGISTRY_PORT: any), () => {
|
110 |
| - isServerSetup = true; |
111 |
| - resolve(); |
112 |
| - }); |
113 |
| - }); |
| 106 | + isServerSetup = true |
| 107 | + resolve() |
| 108 | + }) |
| 109 | + }) |
114 | 110 | }
|
115 | 111 |
|
116 | 112 | function handleJsonRequest(body, res) {
|
117 |
| - res.writeHead(200, {'Content-Type': 'application/json'}); |
118 |
| - const {domain, command, type} = body; |
119 |
| - let success = false; |
120 |
| - if (domain !== 'debug' || type !== 'python') { |
121 |
| - res.end(JSON.stringify({success})); |
122 |
| - return; |
| 113 | + res.writeHead(200, { "Content-Type": "application/json" }) |
| 114 | + const { domain, command, type } = body |
| 115 | + let success = false |
| 116 | + if (domain !== "debug" || type !== "python") { |
| 117 | + res.end(JSON.stringify({ success })) |
| 118 | + return |
123 | 119 | }
|
124 |
| - if (command === 'enable-attach') { |
125 |
| - const port = Number(body.port); |
126 |
| - const {options} = body; |
| 120 | + if (command === "enable-attach") { |
| 121 | + const port = Number(body.port) |
| 122 | + const { options } = body |
127 | 123 | const target = {
|
128 | 124 | port,
|
129 | 125 | id: options.id,
|
130 | 126 | localRoot: options.localRoot,
|
131 | 127 | remoteRoot: options.remoteRoot,
|
132 | 128 | debugOptions: options.debugOptions,
|
133 |
| - }; |
134 |
| - attachReady.set(port, target); |
135 |
| - getLogger().info('Remote debug target is ready to attach', target); |
136 |
| - success = true; |
137 |
| - } else if (command === 'attach') { |
138 |
| - const port = Number(body.port); |
139 |
| - getLogger().info('Remote debug target attach request', body); |
140 |
| - const target = attachReady.get(port); |
| 129 | + } |
| 130 | + attachReady.set(port, target) |
| 131 | + getLogger().info("Remote debug target is ready to attach", target) |
| 132 | + success = true |
| 133 | + } else if (command === "attach") { |
| 134 | + const port = Number(body.port) |
| 135 | + getLogger().info("Remote debug target attach request", body) |
| 136 | + const target = attachReady.get(port) |
141 | 137 | if (target != null) {
|
142 | 138 | debugRequests.next({
|
143 | 139 | type,
|
144 | 140 | command,
|
145 | 141 | target,
|
146 |
| - }); |
147 |
| - success = true; |
| 142 | + }) |
| 143 | + success = true |
148 | 144 | }
|
149 | 145 | }
|
150 |
| - res.end(JSON.stringify({success})); |
| 146 | + res.end(JSON.stringify({ success })) |
151 | 147 | }
|
0 commit comments