@theia/vsx-registry 1.53.0-next.55 → 1.53.0-next.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +45 -45
  2. package/package.json +10 -10
  3. package/src/browser/recommended-extensions/preference-provider-overrides.ts +99 -99
  4. package/src/browser/recommended-extensions/recommended-extensions-json-schema.ts +74 -74
  5. package/src/browser/recommended-extensions/recommended-extensions-preference-contribution.ts +68 -68
  6. package/src/browser/style/extensions.svg +4 -4
  7. package/src/browser/style/index.css +436 -436
  8. package/src/browser/vsx-extension-argument-processor.ts +32 -32
  9. package/src/browser/vsx-extension-commands.ts +68 -68
  10. package/src/browser/vsx-extension-editor-manager.ts +42 -42
  11. package/src/browser/vsx-extension-editor.tsx +96 -96
  12. package/src/browser/vsx-extension.tsx +710 -710
  13. package/src/browser/vsx-extensions-contribution.ts +373 -373
  14. package/src/browser/vsx-extensions-model.ts +456 -456
  15. package/src/browser/vsx-extensions-preferences.ts +58 -58
  16. package/src/browser/vsx-extensions-search-bar.tsx +107 -107
  17. package/src/browser/vsx-extensions-search-model.ts +61 -61
  18. package/src/browser/vsx-extensions-source.ts +83 -83
  19. package/src/browser/vsx-extensions-view-container.ts +179 -179
  20. package/src/browser/vsx-extensions-widget.tsx +165 -165
  21. package/src/browser/vsx-language-quick-pick-service.ts +112 -112
  22. package/src/browser/vsx-registry-frontend-module.ts +113 -113
  23. package/src/common/index.ts +19 -19
  24. package/src/common/ovsx-client-provider.ts +35 -35
  25. package/src/common/vsx-environment.ts +28 -28
  26. package/src/common/vsx-extension-uri.ts +20 -20
  27. package/src/common/vsx-registry-common-module.ts +85 -85
  28. package/src/node/vsx-cli-deployer-participant.ts +46 -46
  29. package/src/node/vsx-cli.ts +55 -55
  30. package/src/node/vsx-environment-impl.ts +54 -54
  31. package/src/node/vsx-extension-resolver.ts +134 -134
  32. package/src/node/vsx-registry-backend-module.ts +38 -38
  33. package/src/node/vsx-remote-cli.ts +39 -39
  34. package/src/package.spec.ts +29 -29
@@ -1,456 +1,456 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2020 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
18
- import debounce from 'p-debounce';
19
- import * as markdownit from '@theia/core/shared/markdown-it';
20
- import * as DOMPurify from '@theia/core/shared/dompurify';
21
- import { Emitter, Event } from '@theia/core/lib/common/event';
22
- import { CancellationToken, CancellationTokenSource } from '@theia/core/lib/common/cancellation';
23
- import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin';
24
- import { VSXExtension, VSXExtensionFactory } from './vsx-extension';
25
- import { ProgressService } from '@theia/core/lib/common/progress-service';
26
- import { VSXExtensionsSearchModel } from './vsx-extensions-search-model';
27
- import { PreferenceInspectionScope, PreferenceService } from '@theia/core/lib/browser';
28
- import { WorkspaceService } from '@theia/workspace/lib/browser';
29
- import { RecommendedExtensions } from './recommended-extensions/recommended-extensions-preference-contribution';
30
- import URI from '@theia/core/lib/common/uri';
31
- import { OVSXClient, VSXAllVersions, VSXExtensionRaw, VSXResponseError, VSXSearchEntry, VSXSearchOptions, VSXTargetPlatform } from '@theia/ovsx-client/lib/ovsx-types';
32
- import { OVSXClientProvider } from '../common/ovsx-client-provider';
33
- import { RequestContext, RequestService } from '@theia/core/shared/@theia/request';
34
- import { OVSXApiFilterProvider } from '@theia/ovsx-client';
35
- import { ApplicationServer } from '@theia/core/lib/common/application-protocol';
36
-
37
- @injectable()
38
- export class VSXExtensionsModel {
39
-
40
- protected initialized: Promise<void>;
41
- /**
42
- * Single source for all extensions
43
- */
44
- protected readonly extensions = new Map<string, VSXExtension>();
45
- protected readonly onDidChangeEmitter = new Emitter<void>();
46
- protected _installed = new Set<string>();
47
- protected _recommended = new Set<string>();
48
- protected _searchResult = new Set<string>();
49
- protected _searchError?: string;
50
-
51
- protected searchCancellationTokenSource = new CancellationTokenSource();
52
- protected updateSearchResult = debounce(async () => {
53
- const { token } = this.resetSearchCancellationTokenSource();
54
- await this.doUpdateSearchResult({ query: this.search.query, includeAllVersions: true }, token);
55
- }, 500);
56
-
57
- @inject(OVSXClientProvider)
58
- protected clientProvider: OVSXClientProvider;
59
-
60
- @inject(HostedPluginSupport)
61
- protected readonly pluginSupport: HostedPluginSupport;
62
-
63
- @inject(VSXExtensionFactory)
64
- protected readonly extensionFactory: VSXExtensionFactory;
65
-
66
- @inject(ProgressService)
67
- protected readonly progressService: ProgressService;
68
-
69
- @inject(PreferenceService)
70
- protected readonly preferences: PreferenceService;
71
-
72
- @inject(WorkspaceService)
73
- protected readonly workspaceService: WorkspaceService;
74
-
75
- @inject(VSXExtensionsSearchModel)
76
- readonly search: VSXExtensionsSearchModel;
77
-
78
- @inject(RequestService)
79
- protected request: RequestService;
80
-
81
- @inject(OVSXApiFilterProvider)
82
- protected vsxApiFilter: OVSXApiFilterProvider;
83
-
84
- @inject(ApplicationServer)
85
- protected readonly applicationServer: ApplicationServer;
86
-
87
- @postConstruct()
88
- protected init(): void {
89
- this.initialized = this.doInit().catch(console.error);
90
- }
91
-
92
- protected async doInit(): Promise<void> {
93
- await Promise.all([
94
- this.initInstalled(),
95
- this.initSearchResult(),
96
- this.initRecommended(),
97
- ]);
98
- }
99
-
100
- get onDidChange(): Event<void> {
101
- return this.onDidChangeEmitter.event;
102
- }
103
-
104
- get installed(): IterableIterator<string> {
105
- return this._installed.values();
106
- }
107
-
108
- get searchError(): string | undefined {
109
- return this._searchError;
110
- }
111
-
112
- get searchResult(): IterableIterator<string> {
113
- return this._searchResult.values();
114
- }
115
-
116
- get recommended(): IterableIterator<string> {
117
- return this._recommended.values();
118
- }
119
-
120
- setOnlyShowVerifiedExtensions(bool: boolean): void {
121
- if (this.preferences.get('extensions.onlyShowVerifiedExtensions') !== bool) {
122
- this.preferences.updateValue('extensions.onlyShowVerifiedExtensions', bool);
123
- }
124
- this.updateSearchResult();
125
- }
126
-
127
- isInstalled(id: string): boolean {
128
- return this._installed.has(id);
129
- }
130
-
131
- getExtension(id: string): VSXExtension | undefined {
132
- return this.extensions.get(id);
133
- }
134
-
135
- resolve(id: string): Promise<VSXExtension> {
136
- return this.doChange(async () => {
137
- await this.initialized;
138
- const extension = await this.refresh(id);
139
- if (!extension) {
140
- throw new Error(`Failed to resolve ${id} extension.`);
141
- }
142
- if (extension.readmeUrl) {
143
- try {
144
- const rawReadme = RequestContext.asText(await this.request.request({ url: extension.readmeUrl }));
145
- const readme = this.compileReadme(rawReadme);
146
- extension.update({ readme });
147
- } catch (e) {
148
- if (!VSXResponseError.is(e) || e.statusCode !== 404) {
149
- console.error(`[${id}]: failed to compile readme, reason:`, e);
150
- }
151
- }
152
- }
153
- return extension;
154
- });
155
- }
156
-
157
- protected async initInstalled(): Promise<void> {
158
- await this.pluginSupport.willStart;
159
- this.pluginSupport.onDidChangePlugins(() => this.updateInstalled());
160
- try {
161
- await this.updateInstalled();
162
- } catch (e) {
163
- console.error(e);
164
- }
165
- }
166
-
167
- protected async initSearchResult(): Promise<void> {
168
- this.search.onDidChangeQuery(() => this.updateSearchResult());
169
- try {
170
- await this.updateSearchResult();
171
- } catch (e) {
172
- console.error(e);
173
- }
174
- }
175
-
176
- protected async initRecommended(): Promise<void> {
177
- this.preferences.onPreferenceChanged(change => {
178
- if (change.preferenceName === 'extensions') {
179
- this.updateRecommended();
180
- }
181
- });
182
- await this.preferences.ready;
183
- try {
184
- await this.updateRecommended();
185
- } catch (e) {
186
- console.error(e);
187
- }
188
- }
189
-
190
- protected resetSearchCancellationTokenSource(): CancellationTokenSource {
191
- this.searchCancellationTokenSource.cancel();
192
- return this.searchCancellationTokenSource = new CancellationTokenSource();
193
- }
194
-
195
- protected setExtension(id: string): VSXExtension {
196
- let extension = this.extensions.get(id);
197
- if (!extension) {
198
- extension = this.extensionFactory({ id });
199
- this.extensions.set(id, extension);
200
- }
201
- return extension;
202
- }
203
-
204
- protected doChange<T>(task: () => Promise<T>): Promise<T>;
205
- protected doChange<T>(task: () => Promise<T>, token: CancellationToken): Promise<T | undefined>;
206
- protected doChange<T>(task: () => Promise<T>, token: CancellationToken = CancellationToken.None): Promise<T | undefined> {
207
- return this.progressService.withProgress('', 'extensions', async () => {
208
- if (token && token.isCancellationRequested) {
209
- return;
210
- }
211
- const result = await task();
212
- if (token && token.isCancellationRequested) {
213
- return;
214
- }
215
- this.onDidChangeEmitter.fire();
216
- return result;
217
- });
218
- }
219
-
220
- protected doUpdateSearchResult(param: VSXSearchOptions, token: CancellationToken): Promise<void> {
221
- return this.doChange(async () => {
222
- this._searchResult = new Set<string>();
223
- if (!param.query) {
224
- return;
225
- }
226
- const client = await this.clientProvider();
227
- const filter = await this.vsxApiFilter();
228
- try {
229
- const result = await client.search(param);
230
-
231
- if (token.isCancellationRequested) {
232
- return;
233
- }
234
- for (const data of result.extensions) {
235
- const id = data.namespace.toLowerCase() + '.' + data.name.toLowerCase();
236
- const allVersions = filter.getLatestCompatibleVersion(data);
237
- if (!allVersions) {
238
- continue;
239
- }
240
- if (this.preferences.get('extensions.onlyShowVerifiedExtensions')) {
241
- this.fetchVerifiedStatus(id, client, allVersions).then(verified => {
242
- this.doChange(() => {
243
- this.addExtensions(data, id, allVersions, !!verified);
244
- return Promise.resolve();
245
- });
246
- });
247
- } else {
248
- this.addExtensions(data, id, allVersions);
249
- this.fetchVerifiedStatus(id, client, allVersions).then(verified => {
250
- this.doChange(() => {
251
- let extension = this.getExtension(id);
252
- extension = this.setExtension(id);
253
- extension.update(Object.assign({
254
- verified: verified
255
- }));
256
- return Promise.resolve();
257
- });
258
- });
259
- }
260
- }
261
- } catch (error) {
262
- this._searchError = error?.message || String(error);
263
- }
264
-
265
- }, token);
266
- }
267
-
268
- protected async fetchVerifiedStatus(id: string, client: OVSXClient, allVersions: VSXAllVersions): Promise<boolean | undefined> {
269
- try {
270
- const res = await client.query({ extensionId: id, extensionVersion: allVersions.version, includeAllVersions: true });
271
- const extension = res.extensions?.[0];
272
- let verified = extension?.verified;
273
- if (!verified && extension?.publishedBy.loginName === 'open-vsx') {
274
- verified = true;
275
- }
276
- return verified;
277
- } catch (error) {
278
- console.error(error);
279
- return false;
280
- }
281
- }
282
-
283
- protected addExtensions(data: VSXSearchEntry, id: string, allVersions: VSXAllVersions, verified?: boolean): void {
284
- if (!this.preferences.get('extensions.onlyShowVerifiedExtensions') || verified) {
285
- const extension = this.setExtension(id);
286
- extension.update(Object.assign(data, {
287
- publisher: data.namespace,
288
- downloadUrl: data.files.download,
289
- iconUrl: data.files.icon,
290
- readmeUrl: data.files.readme,
291
- licenseUrl: data.files.license,
292
- version: allVersions.version,
293
- verified: verified
294
- }));
295
- this._searchResult.add(id);
296
- }
297
- }
298
-
299
- protected async updateInstalled(): Promise<void> {
300
- const prevInstalled = this._installed;
301
- return this.doChange(async () => {
302
- const plugins = this.pluginSupport.plugins;
303
- const currInstalled = new Set<string>();
304
- const refreshing = [];
305
- for (const plugin of plugins) {
306
- if (plugin.model.engine.type === 'vscode') {
307
- const version = plugin.model.version;
308
- const id = plugin.model.id;
309
- this._installed.delete(id);
310
- const extension = this.setExtension(id);
311
- currInstalled.add(extension.id);
312
- refreshing.push(this.refresh(id, version));
313
- }
314
- }
315
- for (const id of this._installed) {
316
- const extension = this.getExtension(id);
317
- if (!extension) { continue; }
318
- refreshing.push(this.refresh(id, extension.version));
319
- }
320
- const installed = new Set([...prevInstalled, ...currInstalled]);
321
- const installedSorted = Array.from(installed).sort((a, b) => this.compareExtensions(a, b));
322
- this._installed = new Set(installedSorted.values());
323
- await Promise.all(refreshing);
324
- });
325
- }
326
-
327
- protected updateRecommended(): Promise<Array<VSXExtension | undefined>> {
328
- return this.doChange<Array<VSXExtension | undefined>>(async () => {
329
- const allRecommendations = new Set<string>();
330
- const allUnwantedRecommendations = new Set<string>();
331
-
332
- const updateRecommendationsForScope = (scope: PreferenceInspectionScope, root?: URI) => {
333
- const { recommendations, unwantedRecommendations } = this.getRecommendationsForScope(scope, root);
334
- recommendations.forEach(recommendation => allRecommendations.add(recommendation));
335
- unwantedRecommendations.forEach(unwantedRecommendation => allUnwantedRecommendations.add(unwantedRecommendation));
336
- };
337
-
338
- updateRecommendationsForScope('defaultValue'); // In case there are application-default recommendations.
339
- const roots = await this.workspaceService.roots;
340
- for (const root of roots) {
341
- updateRecommendationsForScope('workspaceFolderValue', root.resource);
342
- }
343
- if (this.workspaceService.saved) {
344
- updateRecommendationsForScope('workspaceValue');
345
- }
346
- const recommendedSorted = new Set(Array.from(allRecommendations).sort((a, b) => this.compareExtensions(a, b)));
347
- allUnwantedRecommendations.forEach(unwantedRecommendation => recommendedSorted.delete(unwantedRecommendation));
348
- this._recommended = recommendedSorted;
349
- return Promise.all(Array.from(recommendedSorted, plugin => this.refresh(plugin)));
350
- });
351
- }
352
-
353
- protected getRecommendationsForScope(scope: PreferenceInspectionScope, root?: URI): Required<RecommendedExtensions> {
354
- const configuredValue = this.preferences.inspect<Required<RecommendedExtensions>>('extensions', root?.toString())?.[scope];
355
- return {
356
- recommendations: configuredValue?.recommendations ?? [],
357
- unwantedRecommendations: configuredValue?.unwantedRecommendations ?? [],
358
- };
359
- }
360
-
361
- protected compileReadme(readmeMarkdown: string): string {
362
- const readmeHtml = markdownit({ html: true }).render(readmeMarkdown);
363
- return DOMPurify.sanitize(readmeHtml);
364
- }
365
-
366
- protected async refresh(id: string, version?: string): Promise<VSXExtension | undefined> {
367
- try {
368
- let extension = this.getExtension(id);
369
- if (!this.shouldRefresh(extension)) {
370
- return extension;
371
- }
372
- const filter = await this.vsxApiFilter();
373
- const targetPlatform = await this.applicationServer.getApplicationPlatform() as VSXTargetPlatform;
374
- let data: VSXExtensionRaw | undefined;
375
- if (version === undefined) {
376
- data = await filter.findLatestCompatibleExtension({
377
- extensionId: id,
378
- includeAllVersions: true,
379
- targetPlatform
380
- });
381
- } else {
382
- data = await filter.findLatestCompatibleExtension({
383
- extensionId: id,
384
- extensionVersion: version,
385
- includeAllVersions: true,
386
- targetPlatform
387
- });
388
- }
389
- if (!data) {
390
- return;
391
- }
392
- if (data.error) {
393
- return this.onDidFailRefresh(id, data.error);
394
- }
395
- if (!data.verified) {
396
- if (data.publishedBy.loginName === 'open-vsx') {
397
- data.verified = true;
398
- }
399
- }
400
- extension = this.setExtension(id);
401
- extension.update(Object.assign(data, {
402
- publisher: data.namespace,
403
- downloadUrl: data.files.download,
404
- iconUrl: data.files.icon,
405
- readmeUrl: data.files.readme,
406
- licenseUrl: data.files.license,
407
- version: data.version,
408
- verified: data.verified
409
- }));
410
- return extension;
411
- } catch (e) {
412
- return this.onDidFailRefresh(id, e);
413
- }
414
- }
415
-
416
- /**
417
- * Determines if the given extension should be refreshed.
418
- * @param extension the extension to refresh.
419
- */
420
- protected shouldRefresh(extension?: VSXExtension): boolean {
421
- if (extension === undefined) {
422
- return true;
423
- }
424
- return !extension.builtin;
425
- }
426
-
427
- protected onDidFailRefresh(id: string, error: unknown): VSXExtension | undefined {
428
- const cached = this.getExtension(id);
429
- if (cached && cached.installed) {
430
- return cached;
431
- }
432
- console.error(`[${id}]: failed to refresh, reason:`, error);
433
- return undefined;
434
- }
435
-
436
- /**
437
- * Compare two extensions based on their display name, and publisher if applicable.
438
- * @param a the first extension id for comparison.
439
- * @param b the second extension id for comparison.
440
- */
441
- protected compareExtensions(a: string, b: string): number {
442
- const extensionA = this.getExtension(a);
443
- const extensionB = this.getExtension(b);
444
- if (!extensionA || !extensionB) {
445
- return 0;
446
- }
447
- if (extensionA.displayName && extensionB.displayName) {
448
- return extensionA.displayName.localeCompare(extensionB.displayName);
449
- }
450
- if (extensionA.publisher && extensionB.publisher) {
451
- return extensionA.publisher.localeCompare(extensionB.publisher);
452
- }
453
- return 0;
454
- }
455
-
456
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2020 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
18
+ import debounce from 'p-debounce';
19
+ import * as markdownit from '@theia/core/shared/markdown-it';
20
+ import * as DOMPurify from '@theia/core/shared/dompurify';
21
+ import { Emitter, Event } from '@theia/core/lib/common/event';
22
+ import { CancellationToken, CancellationTokenSource } from '@theia/core/lib/common/cancellation';
23
+ import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin';
24
+ import { VSXExtension, VSXExtensionFactory } from './vsx-extension';
25
+ import { ProgressService } from '@theia/core/lib/common/progress-service';
26
+ import { VSXExtensionsSearchModel } from './vsx-extensions-search-model';
27
+ import { PreferenceInspectionScope, PreferenceService } from '@theia/core/lib/browser';
28
+ import { WorkspaceService } from '@theia/workspace/lib/browser';
29
+ import { RecommendedExtensions } from './recommended-extensions/recommended-extensions-preference-contribution';
30
+ import URI from '@theia/core/lib/common/uri';
31
+ import { OVSXClient, VSXAllVersions, VSXExtensionRaw, VSXResponseError, VSXSearchEntry, VSXSearchOptions, VSXTargetPlatform } from '@theia/ovsx-client/lib/ovsx-types';
32
+ import { OVSXClientProvider } from '../common/ovsx-client-provider';
33
+ import { RequestContext, RequestService } from '@theia/core/shared/@theia/request';
34
+ import { OVSXApiFilterProvider } from '@theia/ovsx-client';
35
+ import { ApplicationServer } from '@theia/core/lib/common/application-protocol';
36
+
37
+ @injectable()
38
+ export class VSXExtensionsModel {
39
+
40
+ protected initialized: Promise<void>;
41
+ /**
42
+ * Single source for all extensions
43
+ */
44
+ protected readonly extensions = new Map<string, VSXExtension>();
45
+ protected readonly onDidChangeEmitter = new Emitter<void>();
46
+ protected _installed = new Set<string>();
47
+ protected _recommended = new Set<string>();
48
+ protected _searchResult = new Set<string>();
49
+ protected _searchError?: string;
50
+
51
+ protected searchCancellationTokenSource = new CancellationTokenSource();
52
+ protected updateSearchResult = debounce(async () => {
53
+ const { token } = this.resetSearchCancellationTokenSource();
54
+ await this.doUpdateSearchResult({ query: this.search.query, includeAllVersions: true }, token);
55
+ }, 500);
56
+
57
+ @inject(OVSXClientProvider)
58
+ protected clientProvider: OVSXClientProvider;
59
+
60
+ @inject(HostedPluginSupport)
61
+ protected readonly pluginSupport: HostedPluginSupport;
62
+
63
+ @inject(VSXExtensionFactory)
64
+ protected readonly extensionFactory: VSXExtensionFactory;
65
+
66
+ @inject(ProgressService)
67
+ protected readonly progressService: ProgressService;
68
+
69
+ @inject(PreferenceService)
70
+ protected readonly preferences: PreferenceService;
71
+
72
+ @inject(WorkspaceService)
73
+ protected readonly workspaceService: WorkspaceService;
74
+
75
+ @inject(VSXExtensionsSearchModel)
76
+ readonly search: VSXExtensionsSearchModel;
77
+
78
+ @inject(RequestService)
79
+ protected request: RequestService;
80
+
81
+ @inject(OVSXApiFilterProvider)
82
+ protected vsxApiFilter: OVSXApiFilterProvider;
83
+
84
+ @inject(ApplicationServer)
85
+ protected readonly applicationServer: ApplicationServer;
86
+
87
+ @postConstruct()
88
+ protected init(): void {
89
+ this.initialized = this.doInit().catch(console.error);
90
+ }
91
+
92
+ protected async doInit(): Promise<void> {
93
+ await Promise.all([
94
+ this.initInstalled(),
95
+ this.initSearchResult(),
96
+ this.initRecommended(),
97
+ ]);
98
+ }
99
+
100
+ get onDidChange(): Event<void> {
101
+ return this.onDidChangeEmitter.event;
102
+ }
103
+
104
+ get installed(): IterableIterator<string> {
105
+ return this._installed.values();
106
+ }
107
+
108
+ get searchError(): string | undefined {
109
+ return this._searchError;
110
+ }
111
+
112
+ get searchResult(): IterableIterator<string> {
113
+ return this._searchResult.values();
114
+ }
115
+
116
+ get recommended(): IterableIterator<string> {
117
+ return this._recommended.values();
118
+ }
119
+
120
+ setOnlyShowVerifiedExtensions(bool: boolean): void {
121
+ if (this.preferences.get('extensions.onlyShowVerifiedExtensions') !== bool) {
122
+ this.preferences.updateValue('extensions.onlyShowVerifiedExtensions', bool);
123
+ }
124
+ this.updateSearchResult();
125
+ }
126
+
127
+ isInstalled(id: string): boolean {
128
+ return this._installed.has(id);
129
+ }
130
+
131
+ getExtension(id: string): VSXExtension | undefined {
132
+ return this.extensions.get(id);
133
+ }
134
+
135
+ resolve(id: string): Promise<VSXExtension> {
136
+ return this.doChange(async () => {
137
+ await this.initialized;
138
+ const extension = await this.refresh(id);
139
+ if (!extension) {
140
+ throw new Error(`Failed to resolve ${id} extension.`);
141
+ }
142
+ if (extension.readmeUrl) {
143
+ try {
144
+ const rawReadme = RequestContext.asText(await this.request.request({ url: extension.readmeUrl }));
145
+ const readme = this.compileReadme(rawReadme);
146
+ extension.update({ readme });
147
+ } catch (e) {
148
+ if (!VSXResponseError.is(e) || e.statusCode !== 404) {
149
+ console.error(`[${id}]: failed to compile readme, reason:`, e);
150
+ }
151
+ }
152
+ }
153
+ return extension;
154
+ });
155
+ }
156
+
157
+ protected async initInstalled(): Promise<void> {
158
+ await this.pluginSupport.willStart;
159
+ this.pluginSupport.onDidChangePlugins(() => this.updateInstalled());
160
+ try {
161
+ await this.updateInstalled();
162
+ } catch (e) {
163
+ console.error(e);
164
+ }
165
+ }
166
+
167
+ protected async initSearchResult(): Promise<void> {
168
+ this.search.onDidChangeQuery(() => this.updateSearchResult());
169
+ try {
170
+ await this.updateSearchResult();
171
+ } catch (e) {
172
+ console.error(e);
173
+ }
174
+ }
175
+
176
+ protected async initRecommended(): Promise<void> {
177
+ this.preferences.onPreferenceChanged(change => {
178
+ if (change.preferenceName === 'extensions') {
179
+ this.updateRecommended();
180
+ }
181
+ });
182
+ await this.preferences.ready;
183
+ try {
184
+ await this.updateRecommended();
185
+ } catch (e) {
186
+ console.error(e);
187
+ }
188
+ }
189
+
190
+ protected resetSearchCancellationTokenSource(): CancellationTokenSource {
191
+ this.searchCancellationTokenSource.cancel();
192
+ return this.searchCancellationTokenSource = new CancellationTokenSource();
193
+ }
194
+
195
+ protected setExtension(id: string): VSXExtension {
196
+ let extension = this.extensions.get(id);
197
+ if (!extension) {
198
+ extension = this.extensionFactory({ id });
199
+ this.extensions.set(id, extension);
200
+ }
201
+ return extension;
202
+ }
203
+
204
+ protected doChange<T>(task: () => Promise<T>): Promise<T>;
205
+ protected doChange<T>(task: () => Promise<T>, token: CancellationToken): Promise<T | undefined>;
206
+ protected doChange<T>(task: () => Promise<T>, token: CancellationToken = CancellationToken.None): Promise<T | undefined> {
207
+ return this.progressService.withProgress('', 'extensions', async () => {
208
+ if (token && token.isCancellationRequested) {
209
+ return;
210
+ }
211
+ const result = await task();
212
+ if (token && token.isCancellationRequested) {
213
+ return;
214
+ }
215
+ this.onDidChangeEmitter.fire();
216
+ return result;
217
+ });
218
+ }
219
+
220
+ protected doUpdateSearchResult(param: VSXSearchOptions, token: CancellationToken): Promise<void> {
221
+ return this.doChange(async () => {
222
+ this._searchResult = new Set<string>();
223
+ if (!param.query) {
224
+ return;
225
+ }
226
+ const client = await this.clientProvider();
227
+ const filter = await this.vsxApiFilter();
228
+ try {
229
+ const result = await client.search(param);
230
+
231
+ if (token.isCancellationRequested) {
232
+ return;
233
+ }
234
+ for (const data of result.extensions) {
235
+ const id = data.namespace.toLowerCase() + '.' + data.name.toLowerCase();
236
+ const allVersions = filter.getLatestCompatibleVersion(data);
237
+ if (!allVersions) {
238
+ continue;
239
+ }
240
+ if (this.preferences.get('extensions.onlyShowVerifiedExtensions')) {
241
+ this.fetchVerifiedStatus(id, client, allVersions).then(verified => {
242
+ this.doChange(() => {
243
+ this.addExtensions(data, id, allVersions, !!verified);
244
+ return Promise.resolve();
245
+ });
246
+ });
247
+ } else {
248
+ this.addExtensions(data, id, allVersions);
249
+ this.fetchVerifiedStatus(id, client, allVersions).then(verified => {
250
+ this.doChange(() => {
251
+ let extension = this.getExtension(id);
252
+ extension = this.setExtension(id);
253
+ extension.update(Object.assign({
254
+ verified: verified
255
+ }));
256
+ return Promise.resolve();
257
+ });
258
+ });
259
+ }
260
+ }
261
+ } catch (error) {
262
+ this._searchError = error?.message || String(error);
263
+ }
264
+
265
+ }, token);
266
+ }
267
+
268
+ protected async fetchVerifiedStatus(id: string, client: OVSXClient, allVersions: VSXAllVersions): Promise<boolean | undefined> {
269
+ try {
270
+ const res = await client.query({ extensionId: id, extensionVersion: allVersions.version, includeAllVersions: true });
271
+ const extension = res.extensions?.[0];
272
+ let verified = extension?.verified;
273
+ if (!verified && extension?.publishedBy.loginName === 'open-vsx') {
274
+ verified = true;
275
+ }
276
+ return verified;
277
+ } catch (error) {
278
+ console.error(error);
279
+ return false;
280
+ }
281
+ }
282
+
283
+ protected addExtensions(data: VSXSearchEntry, id: string, allVersions: VSXAllVersions, verified?: boolean): void {
284
+ if (!this.preferences.get('extensions.onlyShowVerifiedExtensions') || verified) {
285
+ const extension = this.setExtension(id);
286
+ extension.update(Object.assign(data, {
287
+ publisher: data.namespace,
288
+ downloadUrl: data.files.download,
289
+ iconUrl: data.files.icon,
290
+ readmeUrl: data.files.readme,
291
+ licenseUrl: data.files.license,
292
+ version: allVersions.version,
293
+ verified: verified
294
+ }));
295
+ this._searchResult.add(id);
296
+ }
297
+ }
298
+
299
+ protected async updateInstalled(): Promise<void> {
300
+ const prevInstalled = this._installed;
301
+ return this.doChange(async () => {
302
+ const plugins = this.pluginSupport.plugins;
303
+ const currInstalled = new Set<string>();
304
+ const refreshing = [];
305
+ for (const plugin of plugins) {
306
+ if (plugin.model.engine.type === 'vscode') {
307
+ const version = plugin.model.version;
308
+ const id = plugin.model.id;
309
+ this._installed.delete(id);
310
+ const extension = this.setExtension(id);
311
+ currInstalled.add(extension.id);
312
+ refreshing.push(this.refresh(id, version));
313
+ }
314
+ }
315
+ for (const id of this._installed) {
316
+ const extension = this.getExtension(id);
317
+ if (!extension) { continue; }
318
+ refreshing.push(this.refresh(id, extension.version));
319
+ }
320
+ const installed = new Set([...prevInstalled, ...currInstalled]);
321
+ const installedSorted = Array.from(installed).sort((a, b) => this.compareExtensions(a, b));
322
+ this._installed = new Set(installedSorted.values());
323
+ await Promise.all(refreshing);
324
+ });
325
+ }
326
+
327
+ protected updateRecommended(): Promise<Array<VSXExtension | undefined>> {
328
+ return this.doChange<Array<VSXExtension | undefined>>(async () => {
329
+ const allRecommendations = new Set<string>();
330
+ const allUnwantedRecommendations = new Set<string>();
331
+
332
+ const updateRecommendationsForScope = (scope: PreferenceInspectionScope, root?: URI) => {
333
+ const { recommendations, unwantedRecommendations } = this.getRecommendationsForScope(scope, root);
334
+ recommendations.forEach(recommendation => allRecommendations.add(recommendation));
335
+ unwantedRecommendations.forEach(unwantedRecommendation => allUnwantedRecommendations.add(unwantedRecommendation));
336
+ };
337
+
338
+ updateRecommendationsForScope('defaultValue'); // In case there are application-default recommendations.
339
+ const roots = await this.workspaceService.roots;
340
+ for (const root of roots) {
341
+ updateRecommendationsForScope('workspaceFolderValue', root.resource);
342
+ }
343
+ if (this.workspaceService.saved) {
344
+ updateRecommendationsForScope('workspaceValue');
345
+ }
346
+ const recommendedSorted = new Set(Array.from(allRecommendations).sort((a, b) => this.compareExtensions(a, b)));
347
+ allUnwantedRecommendations.forEach(unwantedRecommendation => recommendedSorted.delete(unwantedRecommendation));
348
+ this._recommended = recommendedSorted;
349
+ return Promise.all(Array.from(recommendedSorted, plugin => this.refresh(plugin)));
350
+ });
351
+ }
352
+
353
+ protected getRecommendationsForScope(scope: PreferenceInspectionScope, root?: URI): Required<RecommendedExtensions> {
354
+ const configuredValue = this.preferences.inspect<Required<RecommendedExtensions>>('extensions', root?.toString())?.[scope];
355
+ return {
356
+ recommendations: configuredValue?.recommendations ?? [],
357
+ unwantedRecommendations: configuredValue?.unwantedRecommendations ?? [],
358
+ };
359
+ }
360
+
361
+ protected compileReadme(readmeMarkdown: string): string {
362
+ const readmeHtml = markdownit({ html: true }).render(readmeMarkdown);
363
+ return DOMPurify.sanitize(readmeHtml);
364
+ }
365
+
366
+ protected async refresh(id: string, version?: string): Promise<VSXExtension | undefined> {
367
+ try {
368
+ let extension = this.getExtension(id);
369
+ if (!this.shouldRefresh(extension)) {
370
+ return extension;
371
+ }
372
+ const filter = await this.vsxApiFilter();
373
+ const targetPlatform = await this.applicationServer.getApplicationPlatform() as VSXTargetPlatform;
374
+ let data: VSXExtensionRaw | undefined;
375
+ if (version === undefined) {
376
+ data = await filter.findLatestCompatibleExtension({
377
+ extensionId: id,
378
+ includeAllVersions: true,
379
+ targetPlatform
380
+ });
381
+ } else {
382
+ data = await filter.findLatestCompatibleExtension({
383
+ extensionId: id,
384
+ extensionVersion: version,
385
+ includeAllVersions: true,
386
+ targetPlatform
387
+ });
388
+ }
389
+ if (!data) {
390
+ return;
391
+ }
392
+ if (data.error) {
393
+ return this.onDidFailRefresh(id, data.error);
394
+ }
395
+ if (!data.verified) {
396
+ if (data.publishedBy.loginName === 'open-vsx') {
397
+ data.verified = true;
398
+ }
399
+ }
400
+ extension = this.setExtension(id);
401
+ extension.update(Object.assign(data, {
402
+ publisher: data.namespace,
403
+ downloadUrl: data.files.download,
404
+ iconUrl: data.files.icon,
405
+ readmeUrl: data.files.readme,
406
+ licenseUrl: data.files.license,
407
+ version: data.version,
408
+ verified: data.verified
409
+ }));
410
+ return extension;
411
+ } catch (e) {
412
+ return this.onDidFailRefresh(id, e);
413
+ }
414
+ }
415
+
416
+ /**
417
+ * Determines if the given extension should be refreshed.
418
+ * @param extension the extension to refresh.
419
+ */
420
+ protected shouldRefresh(extension?: VSXExtension): boolean {
421
+ if (extension === undefined) {
422
+ return true;
423
+ }
424
+ return !extension.builtin;
425
+ }
426
+
427
+ protected onDidFailRefresh(id: string, error: unknown): VSXExtension | undefined {
428
+ const cached = this.getExtension(id);
429
+ if (cached && cached.installed) {
430
+ return cached;
431
+ }
432
+ console.error(`[${id}]: failed to refresh, reason:`, error);
433
+ return undefined;
434
+ }
435
+
436
+ /**
437
+ * Compare two extensions based on their display name, and publisher if applicable.
438
+ * @param a the first extension id for comparison.
439
+ * @param b the second extension id for comparison.
440
+ */
441
+ protected compareExtensions(a: string, b: string): number {
442
+ const extensionA = this.getExtension(a);
443
+ const extensionB = this.getExtension(b);
444
+ if (!extensionA || !extensionB) {
445
+ return 0;
446
+ }
447
+ if (extensionA.displayName && extensionB.displayName) {
448
+ return extensionA.displayName.localeCompare(extensionB.displayName);
449
+ }
450
+ if (extensionA.publisher && extensionB.publisher) {
451
+ return extensionA.publisher.localeCompare(extensionB.publisher);
452
+ }
453
+ return 0;
454
+ }
455
+
456
+ }