Repository navigation
Expand file tree
/
Copy pathjavaDebugAdapterDescriptorFactory.ts
More file actions
72 lines (66 loc) · 2.96 KB
/
Copy pathjavaDebugAdapterDescriptorFactory.ts
File metadata and controls
72 lines (66 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import {
DebugAdapterDescriptor,
DebugAdapterDescriptorFactory,
DebugAdapterExecutable,
DebugAdapterInlineImplementation,
DebugAdapterServer,
DebugSession,
} from "vscode";
import * as telemetryWrapper from "vscode-extension-telemetry-wrapper";
import { AuthenticatedSocketDebugAdapter, normalizeLoopbackHost } from "./authenticatedDebugAdapter";
import { Type } from "./javaLogger";
import { IDebugSessionDescriptor, startDebugSession } from "./languageServerPlugin";
import { convertErrorToMessage, showErrorMessageWithTroubleshooting } from "./utility";
export type DebugSessionProtocol = "v2" | "v1_legacy_fallback";
export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory {
public async createDebugAdapterDescriptor(_session: DebugSession,
_executable: DebugAdapterExecutable): Promise<DebugAdapterDescriptor | undefined> {
let error: Error | undefined;
try {
const sessionDescriptor = await startDebugSession();
if (isAuthenticatedSessionDescriptor(sessionDescriptor)) {
const host = normalizeLoopbackHost(sessionDescriptor.host || "127.0.0.1");
const adapter = new AuthenticatedSocketDebugAdapter(sessionDescriptor.port, sessionDescriptor.authToken, host);
recordSessionProtocol("v2");
return new DebugAdapterInlineImplementation(adapter);
} else if (typeof sessionDescriptor === "number" && sessionDescriptor > 0) {
recordSessionProtocol("v1_legacy_fallback");
return new DebugAdapterServer(sessionDescriptor, "127.0.0.1");
} else {
// Information for diagnostic:
// tslint:disable-next-line:no-console
console.log("Cannot find a port for debugging session");
}
} catch (err) {
error = err;
}
const message = error ? convertErrorToMessage(error) : {
type: Type.EXCEPTION,
message: "Failed to start debug server.",
};
showErrorMessageWithTroubleshooting(message);
return undefined;
}
}
function recordSessionProtocol(protocol: DebugSessionProtocol): void {
try {
telemetryWrapper.sendInfo("", {
operationName: "dapSessionProtocol",
protocol,
});
} catch (_err) {
// Telemetry must never interfere with debug session startup.
}
}
function isAuthenticatedSessionDescriptor(value: unknown): value is IDebugSessionDescriptor {
if (typeof value !== "object" || value === null) {
return false;
}
const candidate = value as Partial<IDebugSessionDescriptor>;
return typeof candidate.port === "number"
&& candidate.port > 0
&& typeof candidate.authToken === "string"
&& candidate.authToken.length > 0;
}