Skip to content

Commit 0b10323

Browse files
refactor to abstract class and use crypto.randomBytes
1 parent 10a5d09 commit 0b10323

2 files changed

Lines changed: 19 additions & 27 deletions

File tree

src/vs/workbench/contrib/debug/node/debugAdapter.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,25 @@ export abstract class StreamDebugAdapter extends AbstractDebugAdapter {
9191
}
9292
}
9393

94+
export abstract class NetworkDebugAdapter extends StreamDebugAdapter {
95+
96+
protected socket?: net.Socket;
97+
98+
abstract startSession(): Promise<void>;
99+
100+
async stopSession(): Promise<void> {
101+
await this.cancelPendingRequests();
102+
if (this.socket) {
103+
this.socket.end();
104+
this.socket = undefined;
105+
}
106+
}
107+
}
108+
94109
/**
95110
* An implementation that connects to a debug adapter via a socket.
96111
*/
97-
export class SocketDebugAdapter extends StreamDebugAdapter {
98-
99-
private socket?: net.Socket;
112+
export class SocketDebugAdapter extends NetworkDebugAdapter {
100113

101114
constructor(private adapterServer: IDebugAdapterServer) {
102115
super();
@@ -126,22 +139,12 @@ export class SocketDebugAdapter extends StreamDebugAdapter {
126139
});
127140
});
128141
}
129-
130-
async stopSession(): Promise<void> {
131-
await this.cancelPendingRequests();
132-
if (this.socket) {
133-
this.socket.end();
134-
this.socket = undefined;
135-
}
136-
}
137142
}
138143

139144
/**
140145
* An implementation that connects to a debug adapter via a NamedPipe (on Windows)/UNIX Domain Socket (on non-Windows).
141146
*/
142-
export class NamedPipeDebugAdapter extends StreamDebugAdapter {
143-
144-
private socket?: net.Socket;
147+
export class NamedPipeDebugAdapter extends NetworkDebugAdapter {
145148

146149
constructor(private adapterServer: IDebugAdapterNamedPipeServer) {
147150
super();
@@ -171,14 +174,6 @@ export class NamedPipeDebugAdapter extends StreamDebugAdapter {
171174
});
172175
});
173176
}
174-
175-
async stopSession(): Promise<void> {
176-
await this.cancelPendingRequests();
177-
if (this.socket) {
178-
this.socket.end();
179-
this.socket = undefined;
180-
}
181-
}
182177
}
183178

184179
/**

src/vs/workbench/contrib/debug/test/node/streamDebugAdapter.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as assert from 'assert';
7+
import * as crypto from 'crypto';
78
import * as net from 'net';
89
import * as platform from 'vs/base/common/platform';
910
import { tmpdir } from 'os';
@@ -16,10 +17,6 @@ function rndPort(): number {
1617
return Math.floor(Math.random() * (max - min) + min);
1718
}
1819

19-
function rndName(): string {
20-
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
21-
}
22-
2320
function sendInitializeRequest(debugAdapter: StreamDebugAdapter): Promise<DebugProtocol.Response> {
2421
return new Promise((resolve, reject) => {
2522
debugAdapter.sendRequest('initialize', { adapterID: 'test' }, (result) => {
@@ -52,7 +49,7 @@ function serverConnection(socket: net.Socket) {
5249

5350
suite('Debug - StreamDebugAdapter', () => {
5451
const port = rndPort();
55-
const pipeName = rndName();
52+
const pipeName = crypto.randomBytes(10).toString('utf8');
5653
const pipePath = platform.isWindows ? join('\\\\.\\pipe\\', pipeName) : join(tmpdir(), pipeName);
5754

5855
const testCases: { testName: string, debugAdapter: StreamDebugAdapter, connectionDetail: string | number }[] = [

0 commit comments

Comments
 (0)