-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionHostService.ts
More file actions
211 lines (183 loc) · 5.6 KB
/
ExtensionHostService.ts
File metadata and controls
211 lines (183 loc) · 5.6 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
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
/**
* @module ExtensionHostService
* @description
* Manages the lifecycle of extensions.
* Provides the extension runtime environment (Module interception + API injection).
*/
import { Effect, Layer } from "effect";
import { IExtensionHostService } from "../Interfaces/IExtensionHostService.js";
import { IModuleInterceptorService } from "../Interfaces/IModuleInterceptorService.js";
import { IAPIFactoryService } from "../Services/APIFactoryService.js";
// Types matching VSCode patterns
interface IExtensionDescription {
identifier: string;
extensionLocation: string;
main?: string;
activationEvents: string[];
}
interface ActivatedExtension {
activationTimes: {
codeLoadingTime: number;
activateCallTime: number;
activateResolvedTime: number;
};
exports?: any;
}
/**
* ExtensionHostService implementation
*/
export class ExtensionHostService implements IExtensionHostService {
readonly _serviceBrand: undefined;
// Extensions registry
private activatedExtensions: Map<string, ActivatedExtension> = new Map();
constructor(
private moduleInterceptor: IModuleInterceptorService,
private apiFactory: IAPIFactoryService,
) {}
/**
* Activate an extension
*/
async activateExtension(
extensionId: string,
activationEvent: string,
): Promise<void> {
if (this.activatedExtensions.has(extensionId)) {
return;
}
console.log(
`[ExtensionHost] Activating extension: ${extensionId} (Event: ${activationEvent})`,
);
try {
const startTime = Date.now();
// 1. Prepare API instance for this extension
const vscodeAPI = this.apiFactory.createAPI();
// 2. Register with module interceptor
// When the extension requires 'vscode', it gets our proxy
this.moduleInterceptor.registerAPI(extensionId, vscodeAPI);
// 3. Mock extension description (In real app, fetch from Registry)
const extension: IExtensionDescription = {
identifier: extensionId,
extensionLocation: `/extensions/${extensionId}`,
main: "extension.js",
activationEvents: [activationEvent],
};
// 4. Load the extension module
const moduleLoadStart = Date.now();
const extensionModule = await this._loadExtensionModule(extension);
const codeLoadingTime = Date.now() - moduleLoadStart;
// 5. Activate
const activateCallStart = Date.now();
const exports = await this._callActivate(
extensionModule,
extension,
);
const activateCallTime = Date.now() - activateCallStart;
const activateResolvedTime = Date.now() - startTime;
this.activatedExtensions.set(extensionId, {
activationTimes: {
codeLoadingTime,
activateCallTime,
activateResolvedTime,
},
exports,
});
console.log(
`[ExtensionHost] ${extensionId} activated successfully in ${activateResolvedTime}ms`,
);
} catch (error) {
console.error(
`[ExtensionHost] Failed to activate ${extensionId}:`,
error,
);
throw error;
}
}
/**
* Load extension module with advanced interception
*/
private async _loadExtensionModule(
extension: IExtensionDescription,
): Promise<any> {
if (!extension.main) {
// Fallback for no-code extensions (e.g. themes)
return { activate: () => {} };
}
const modulePath = `${extension.extensionLocation}/${extension.main}`;
console.log(`[ExtensionHost] Loading module: ${modulePath}`);
// Advanced module loading with security interception
try {
// Resolve module path using interceptor
const resolvedPath = this.moduleInterceptor.resolveModule(
modulePath,
extension.extensionLocation,
);
// Load module with security interception
// Note: interceptRequire would be synchronous in Node, but we simulate it here
const extensionModule = this.moduleInterceptor.interceptRequire(
resolvedPath,
extension.extensionLocation,
);
return extensionModule;
} catch (error) {
console.error(
`[ExtensionHost] Failed to load module ${modulePath}:`,
error,
);
// Fallback: If module interceptor fails (e.g. file not found in real FS),
// we simulate a dummy module for development continuity
console.warn(
`[ExtensionHost] Using dummy module for ${extension.identifier}`,
);
return {
activate: (context: any) => {
console.log(`[${extension.identifier}] activate() called`);
},
deactivate: () => {},
};
}
}
/**
* Call extension's activate function
*/
private async _callActivate(
extensionModule: any,
extension: IExtensionDescription,
): Promise<any> {
if (typeof extensionModule.activate !== "function") {
// Allow extensions without activate (declarative only)
return undefined;
}
// Create extension context
// We use a simplified context here, delegating complex parts to the API Factory if needed
const context = {
subscriptions: [],
extensionPath: extension.extensionLocation,
globalState: { get: () => {}, update: () => {} },
workspaceState: { get: () => {}, update: () => {} },
secrets: { get: () => {}, store: () => {}, delete: () => {} },
};
// Call activate function
return await extensionModule.activate(context);
}
/**
* Deactivate an extension
*/
async deactivateExtension(extensionId: string): Promise<void> {
if (!this.activatedExtensions.has(extensionId)) {
return;
}
console.log(`[ExtensionHost] Deactivating extension: ${extensionId}`);
this.activatedExtensions.delete(extensionId);
}
}
/**
* Service Layer
*/
export const ExtensionHostLayer = Layer.effect(
IExtensionHostService,
Effect.gen(function* () {
const moduleInterceptor = yield* IModuleInterceptorService;
const apiFactory = yield* IAPIFactoryService;
return new ExtensionHostService(moduleInterceptor, apiFactory);
}),
);