@theia/vsx-registry 1.45.0 → 1.46.0-next.72

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