-
Notifications
You must be signed in to change notification settings - Fork 279
/
portAttributesProvider.ts
55 lines (47 loc) · 1.83 KB
/
portAttributesProvider.ts
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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { inject, injectable } from 'inversify';
import { ExtensionContext, PortAttributesProvider, PortAutoForwardAction, workspace } from 'vscode';
import { IPortLeaseTracker } from '../adapter/portLeaseTracker';
import { DefaultJsDebugPorts } from '../common/findOpenPort';
import { IExtensionContribution } from '../ioc-extras';
@injectable()
export class JsDebugPortAttributesProvider
implements IExtensionContribution, PortAttributesProvider
{
/** Cache of used ports (#1092) */
private cachedResolutions: string[] = new Array(16).fill('');
/** Index counter for the next cached resolution index in the list */
private cachedResolutionIndex = 0;
constructor(@inject(IPortLeaseTracker) private readonly leaseTracker: IPortLeaseTracker) {}
/**
* @inheritdoc
*/
public register(context: ExtensionContext) {
if (typeof workspace.registerPortAttributesProvider === 'function') {
context.subscriptions.push(
workspace.registerPortAttributesProvider(
{ portRange: [DefaultJsDebugPorts.Min, DefaultJsDebugPorts.Max] },
this,
),
);
}
}
/**
* @inheritdoc
*/
public async providePortAttributes(port: number, pid: number | undefined) {
if (pid && this.cachedResolutions.includes(`${port}:${pid}`)) {
return { port, autoForwardAction: PortAutoForwardAction.Ignore };
}
if (!(await this.leaseTracker.isRegistered(port))) {
return undefined;
}
if (pid) {
const index = this.cachedResolutionIndex++ % this.cachedResolutions.length;
this.cachedResolutions[index] = `${port}:${pid}`;
}
return { port, autoForwardAction: PortAutoForwardAction.Ignore };
}
}