/
ioc.ts
325 lines (290 loc) · 13.4 KB
/
ioc.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import execa from 'execa';
import { promises as fsPromises } from 'fs';
import { Container, interfaces } from 'inversify';
import 'reflect-metadata';
import * as vscode from 'vscode';
import {
BrowserFinderCtor,
ChromeBrowserFinder,
EdgeBrowserFinder,
} from 'vscode-js-debug-browsers';
import {
BreakpointPredictorDelegate,
BreakpointsPredictor,
IBreakpointsPredictor,
} from './adapter/breakpointPredictor';
import { BreakpointManager } from './adapter/breakpoints';
import {
BreakpointConditionFactory,
IBreakpointConditionFactory,
} from './adapter/breakpoints/conditions';
import { LogPointCompiler } from './adapter/breakpoints/conditions/logPoint';
import { Completions, ICompletions } from './adapter/completions';
import { IConsole } from './adapter/console';
import { Console } from './adapter/console/console';
import { Evaluator, IEvaluator } from './adapter/evaluator';
import { IPerformanceProvider, PerformanceProviderFactory } from './adapter/performance';
import { IProfileController, ProfileController } from './adapter/profileController';
import { IProfilerFactory, ProfilerFactory } from './adapter/profiling';
import { BasicCpuProfiler } from './adapter/profiling/basicCpuProfiler';
import { IResourceProvider } from './adapter/resourceProvider';
import { ResourceProviderState } from './adapter/resourceProvider/resourceProviderState';
import { StatefulResourceProvider } from './adapter/resourceProvider/statefulResourceProvider';
import { ScriptSkipper } from './adapter/scriptSkipper/implementation';
import { IScriptSkipper } from './adapter/scriptSkipper/scriptSkipper';
import { SourceContainer } from './adapter/sources';
import { IVueFileMapper, VueFileMapper } from './adapter/vueFileMapper';
import Cdp from './cdp/api';
import { ICdpApi } from './cdp/connection';
import { ObservableMap } from './common/datastructure/observableMap';
import { DefaultBrowserProvider, IDefaultBrowserProvider } from './common/defaultBrowserProvider';
import { OutFiles, VueComponentPaths } from './common/fileGlobList';
import { FSUtils, LocalAndRemoteFsUtils, LocalFsUtils } from './common/fsUtils';
import { ILogger } from './common/logging';
import { Logger } from './common/logging/logger';
import { CodeSearchStrategy } from './common/sourceMaps/codeSearchStrategy';
import { CachingSourceMapFactory, ISourceMapFactory } from './common/sourceMaps/sourceMapFactory';
import { ISearchStrategy } from './common/sourceMaps/sourceMapRepository';
import { ISourcePathResolver } from './common/sourcePathResolver';
import { AnyLaunchConfiguration } from './configuration';
import Dap from './dap/api';
import { IDapApi } from './dap/connection';
import {
BrowserFinder,
Execa,
ExtensionContext,
ExtensionLocation,
FS,
IContainer,
IsVSCode,
ProcessEnv,
SessionSubStates,
StoragePath,
trackDispose,
VSCodeApi,
} from './ioc-extras';
import { BrowserAttacher } from './targets/browser/browserAttacher';
import { ChromeLauncher } from './targets/browser/chromeLauncher';
import { EdgeLauncher } from './targets/browser/edgeLauncher';
import { RemoteBrowserAttacher } from './targets/browser/remoteBrowserAttacher';
import { RemoteBrowserHelper } from './targets/browser/remoteBrowserHelper';
import { RemoteBrowserLauncher } from './targets/browser/remoteBrowserLauncher';
import { VSCodeRendererAttacher } from './targets/browser/vscodeRendererAttacher';
import { DelegateLauncherFactory } from './targets/delegate/delegateLauncherFactory';
import { ExtensionHostAttacher } from './targets/node/extensionHostAttacher';
import { ExtensionHostLauncher } from './targets/node/extensionHostLauncher';
import { NodeAttacher } from './targets/node/nodeAttacher';
import { INodeBinaryProvider, NodeBinaryProvider } from './targets/node/nodeBinaryProvider';
import { NodeLauncher } from './targets/node/nodeLauncher';
import { INvmResolver, NvmResolver } from './targets/node/nvmResolver';
import { IProgramLauncher } from './targets/node/processLauncher';
import { RestartPolicyFactory } from './targets/node/restartPolicy';
import { SubprocessProgramLauncher } from './targets/node/subprocessProgramLauncher';
import { TerminalProgramLauncher } from './targets/node/terminalProgramLauncher';
import { SourcePathResolverFactory } from './targets/sourcePathResolverFactory';
import { ITargetOrigin } from './targets/targetOrigin';
import { ILauncher, ITarget } from './targets/targets';
import { DapTelemetryReporter } from './telemetry/dapTelemetryReporter';
import { NullTelemetryReporter } from './telemetry/nullTelemetryReporter';
import { ITelemetryReporter } from './telemetry/telemetryReporter';
/**
* Contains IOC container factories for the extension. We use Inverisfy, which
* supports nested IOC containers. We have one global container, containing
* the base extension information (like temp storage path) and delegate
* launcher.
*
* For each new top-level session, we create a corresponding top-level
* container this contains shared information, such as the logger instance,
* common launcher implementations, etc.
*
* Then, for each target we receive, we create child containers. The containers
* contain the relevant ITarget, DAP and CDP APIs, any session-specific
* services. For some services, like the script skipper (todo), it may
* communicate with the instance in its parent container.
*/
/**
* Gets the container for a single target within a session.
*/
export const createTargetContainer = (
parent: Container,
target: ITarget,
dap: Dap.Api,
cdp: Cdp.Api,
) => {
const container = new Container();
container.parent = parent;
container.bind(AnyLaunchConfiguration).toConstantValue(target.launchConfig);
container.bind(IContainer).toConstantValue(container);
container.bind(IDapApi).toConstantValue(dap);
container.bind(ICdpApi).toConstantValue(cdp);
container.bind(ITarget).toConstantValue(target);
container.bind(ITargetOrigin).toConstantValue(target.targetOrigin());
container.bind(ISourcePathResolver).toConstantValue(target.sourcePathResolver());
container.bind(IResourceProvider).to(StatefulResourceProvider).inSingletonScope();
container.bind(IBreakpointConditionFactory).to(BreakpointConditionFactory).inSingletonScope();
container.bind(LogPointCompiler).toSelf().inSingletonScope();
container.bind(PerformanceProviderFactory).toSelf();
container
.bind(IPerformanceProvider)
.toDynamicValue(ctx => ctx.container.get(PerformanceProviderFactory).create())
.inSingletonScope();
container.bind(BreakpointPredictorDelegate).toSelf().inSingletonScope();
container
.bind(IBreakpointsPredictor)
.toDynamicValue(() => parent.get<BreakpointPredictorDelegate>(IBreakpointsPredictor).getChild())
.inSingletonScope()
.onActivation(trackDispose);
container
.bind(ITelemetryReporter)
.to(process.env.DA_TEST_DISABLE_TELEMETRY ? NullTelemetryReporter : DapTelemetryReporter)
.inSingletonScope()
.onActivation(trackDispose);
container.bind(BreakpointManager).toSelf().inSingletonScope();
container.bind(SourceContainer).toSelf().inSingletonScope();
container.bind(IScriptSkipper).to(ScriptSkipper).inSingletonScope();
container.bind(ICompletions).to(Completions).inSingletonScope();
container.bind(IEvaluator).to(Evaluator).inSingletonScope();
container.bind(IConsole).to(Console).inSingletonScope(); // dispose is handled by the Thread
container.bind(BasicCpuProfiler).toSelf();
container.bind(IProfilerFactory).to(ProfilerFactory).inSingletonScope();
container.bind(IProfileController).to(ProfileController).inSingletonScope();
return container;
};
export interface IRootOptions {
delegateLauncher: DelegateLauncherFactory;
}
/**
* Creates a container for the top-level "virtual" debug session, containing
* shared/global services.
*/
export const createTopLevelSessionContainer = (parent: Container) => {
const container = new Container();
container.parent = parent;
container.bind(IContainer).toConstantValue(container);
// Core services:
container.bind(ILogger).to(Logger).inSingletonScope().onActivation(trackDispose);
container.bind(ResourceProviderState).toSelf().inSingletonScope();
container.bind(IResourceProvider).to(StatefulResourceProvider).inSingletonScope();
container
.bind(ITelemetryReporter)
.to(process.env.DA_TEST_DISABLE_TELEMETRY ? NullTelemetryReporter : DapTelemetryReporter)
.inSingletonScope()
.onActivation(trackDispose);
// Source handling:
container
.bind(ISourceMapFactory)
.to(CachingSourceMapFactory)
.inSingletonScope()
.onActivation(trackDispose);
container.bind(BreakpointsPredictor).toSelf();
container
.bind(IBreakpointsPredictor)
.toDynamicValue(
ctx =>
new BreakpointPredictorDelegate(ctx.container.get(ISourceMapFactory), () =>
ctx.container.get(BreakpointsPredictor),
),
)
.inSingletonScope();
container.bind(OutFiles).to(OutFiles).inSingletonScope();
container.bind(VueComponentPaths).to(VueComponentPaths).inSingletonScope();
container.bind(IVueFileMapper).to(VueFileMapper).inSingletonScope();
container
.bind(ISearchStrategy)
.toDynamicValue(ctx => CodeSearchStrategy.createOrFallback(ctx.container.get<ILogger>(ILogger)))
.inSingletonScope();
container.bind(INodeBinaryProvider).to(NodeBinaryProvider);
container.bind(RemoteBrowserHelper).toSelf().inSingletonScope().onActivation(trackDispose);
// Launcher logic:
container.bind(RestartPolicyFactory).toSelf();
container.bind(ILauncher).to(VSCodeRendererAttacher).onActivation(trackDispose);
container.bind(ILauncher).to(ExtensionHostAttacher).onActivation(trackDispose);
container.bind(ILauncher).to(ExtensionHostLauncher).onActivation(trackDispose);
container.bind(ILauncher).to(NodeLauncher).onActivation(trackDispose);
container.bind(IProgramLauncher).to(SubprocessProgramLauncher);
container.bind(IProgramLauncher).to(TerminalProgramLauncher);
if (parent.get(IsVSCode)) {
// dynamic require to not break the debug server
container
.bind(ILauncher)
.to(require('./targets/node/terminalNodeLauncher').TerminalNodeLauncher)
.onActivation(trackDispose);
}
container.bind(ILauncher).to(NodeAttacher).onActivation(trackDispose);
container.bind(ChromeLauncher).toSelf().inSingletonScope().onActivation(trackDispose);
container.bind(ILauncher).toService(ChromeLauncher);
container.bind(ILauncher).to(EdgeLauncher).inSingletonScope().onActivation(trackDispose);
container.bind(ILauncher).to(RemoteBrowserLauncher).inSingletonScope().onActivation(trackDispose);
container.bind(ILauncher).to(RemoteBrowserAttacher).inSingletonScope().onActivation(trackDispose);
container.bind(ILauncher).to(BrowserAttacher).onActivation(trackDispose);
container
.bind(ILauncher)
.toDynamicValue(() =>
parent.get(DelegateLauncherFactory).createLauncher(container.get(ILogger)),
)
.inSingletonScope();
const browserFinderFactory = (ctor: BrowserFinderCtor) => (ctx: interfaces.Context) =>
new ctor(ctx.container.get(ProcessEnv), ctx.container.get(FS), ctx.container.get(Execa));
container
.bind(BrowserFinder)
.toDynamicValue(browserFinderFactory(ChromeBrowserFinder))
.inSingletonScope()
.whenTargetTagged('browser', 'chrome');
container
.bind(BrowserFinder)
.toDynamicValue(browserFinderFactory(EdgeBrowserFinder))
.inSingletonScope()
.whenTargetTagged('browser', 'edge');
return container;
};
export const createGlobalContainer = (options: {
storagePath: string;
isVsCode: boolean;
isRemote?: boolean;
context?: vscode.ExtensionContext;
}) => {
const container = new Container();
container.bind(IContainer).toConstantValue(container);
container.bind(DelegateLauncherFactory).toSelf().inSingletonScope();
container.bind(SessionSubStates).toConstantValue(new ObservableMap());
container.bind(IDefaultBrowserProvider).to(DefaultBrowserProvider).inSingletonScope();
container.bind(StoragePath).toConstantValue(options.storagePath);
container.bind(IsVSCode).toConstantValue(options.isVsCode);
container.bind(INvmResolver).to(NvmResolver);
container.bind(ProcessEnv).toConstantValue(process.env);
container.bind(Execa).toConstantValue(execa);
container.bind(FS).toConstantValue(fsPromises);
container.bind(FSUtils).toConstantValue(new LocalFsUtils(fsPromises));
container
.bind<ExtensionLocation>(ExtensionLocation)
.toConstantValue(options.isRemote ? 'remote' : 'local');
if (options.context) {
container.bind(ExtensionContext).toConstantValue(options.context);
}
// Dependency that pull from the vscode global--aren't safe to require at
// a top level (e.g. in the debug server)
if (options.isVsCode) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
container.bind(VSCodeApi).toConstantValue(require('vscode'));
require('./ui/ui-ioc').registerUiComponents(container);
}
return container;
};
export const provideLaunchParams = (
container: Container,
params: AnyLaunchConfiguration,
dap: Dap.Api,
) => {
container.bind(AnyLaunchConfiguration).toConstantValue(params);
container.bind(SourcePathResolverFactory).toSelf().inSingletonScope();
container
.bind(ISourcePathResolver)
.toDynamicValue(ctx => ctx.container.get(SourcePathResolverFactory).create(params))
.inSingletonScope();
container
.bind(FSUtils)
.toConstantValue(LocalAndRemoteFsUtils.create(params.__remoteFilePrefix, fsPromises, dap));
};