| 10 | * and disposes the tunnels when the session ends. |
| 11 | */ |
| 12 | export class DebugSessionTunnels implements IDisposable { |
| 13 | private readonly tunnels = new Map<string, vscode.Tunnel>(); |
| 14 | private readonly disposable = new DisposableList(); |
| 15 | |
| 16 | constructor() { |
| 17 | this.disposable.push( |
| 18 | vscode.debug.onDidTerminateDebugSession(session => this.destroySession(session.id)), |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @inheritdoc |
| 24 | */ |
| 25 | public dispose() { |
| 26 | return this.disposable.dispose(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Removes a session tunnel if it exists. |
| 31 | */ |
| 32 | public destroySession(sessionId: string) { |
| 33 | const tunnel = this.tunnels.get(sessionId); |
| 34 | if (tunnel) { |
| 35 | tunnel.dispose(); |
| 36 | this.tunnels.delete(sessionId); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Requests a tunnel. Note that if a tunnel was previously created for the |
| 42 | * session, it'll be returned regardless of the localPort/remotePort. |
| 43 | */ |
| 44 | public async request( |
| 45 | sessionId: string, |
| 46 | opts: { |
| 47 | label: string; |
| 48 | localPort?: number; |
| 49 | remotePort: number; |
| 50 | }, |
| 51 | ) { |
| 52 | let tunnel = this.tunnels.get(sessionId); |
| 53 | if (!tunnel) { |
| 54 | tunnel = await vscode.workspace.openTunnel({ |
| 55 | remoteAddress: { port: opts.remotePort, host: 'localhost' }, |
| 56 | localAddressPort: opts.localPort ?? opts.remotePort, |
| 57 | label: opts.label, |
| 58 | }); |
| 59 | this.tunnels.set(sessionId, tunnel); |
| 60 | } |
| 61 | |
| 62 | let localAddress: { host: string; port: number }; |
| 63 | if (typeof tunnel.localAddress === 'string') { |
| 64 | const [host, port] = tunnel.localAddress.split(':'); |
| 65 | localAddress = { host, port: Number(port) }; |
| 66 | } else { |
| 67 | localAddress = tunnel.localAddress; |
| 68 | } |
| 69 |
nothing calls this directly
no outgoing calls
no test coverage detected