@theia/ovsx-client 1.53.0-next.4 → 1.53.0-next.55

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.
@@ -1,137 +1,140 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2023 Ericsson 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 * as semver from 'semver';
18
- import { OVSXClient, VSXAllVersions, VSXBuiltinNamespaces, VSXExtensionRaw, VSXQueryOptions, VSXSearchEntry } from './ovsx-types';
19
-
20
- export const OVSXApiFilterProvider = Symbol('OVSXApiFilterProvider');
21
-
22
- export type OVSXApiFilterProvider = () => Promise<OVSXApiFilter>;
23
-
24
- export const OVSXApiFilter = Symbol('OVSXApiFilter');
25
- /**
26
- * Filter various data types based on a pre-defined supported VS Code API version.
27
- */
28
- export interface OVSXApiFilter {
29
- supportedApiVersion: string;
30
- findLatestCompatibleExtension(query: VSXQueryOptions): Promise<VSXExtensionRaw | undefined>;
31
- /**
32
- * Get the latest compatible extension version:
33
- * - A builtin extension is fetched based on the extension version which matches the API.
34
- * - An extension satisfies compatibility if its `engines.vscode` version is supported.
35
- *
36
- * @param extensionId the extension id.
37
- * @returns the data for the latest compatible extension version if available, else `undefined`.
38
- */
39
- getLatestCompatibleExtension(extensions: VSXExtensionRaw[]): VSXExtensionRaw | undefined;
40
- getLatestCompatibleVersion(searchEntry: VSXSearchEntry): VSXAllVersions | undefined;
41
- }
42
-
43
- export class OVSXApiFilterImpl implements OVSXApiFilter {
44
-
45
- constructor(
46
- public client: OVSXClient,
47
- public supportedApiVersion: string
48
- ) { }
49
-
50
- async findLatestCompatibleExtension(query: VSXQueryOptions): Promise<VSXExtensionRaw | undefined> {
51
- const targetPlatform = query.targetPlatform;
52
- if (!targetPlatform) {
53
- return this.queryLatestCompatibleExtension(query);
54
- }
55
- const latestWithTargetPlatform = await this.queryLatestCompatibleExtension(query);
56
- let latestUniversal: VSXExtensionRaw | undefined;
57
- if (targetPlatform !== 'universal' && targetPlatform !== 'web') {
58
- // Additionally query the universal version, as there might be a newer one available
59
- latestUniversal = await this.queryLatestCompatibleExtension({ ...query, targetPlatform: 'universal' });
60
- }
61
- if (latestWithTargetPlatform && latestUniversal) {
62
- // Prefer the version with the target platform if it's greater or equal to the universal version
63
- return this.versionGreaterThanOrEqualTo(latestWithTargetPlatform.version, latestUniversal.version) ? latestWithTargetPlatform : latestUniversal;
64
- }
65
- return latestWithTargetPlatform ?? latestUniversal;
66
- }
67
-
68
- protected async queryLatestCompatibleExtension(query: VSXQueryOptions): Promise<VSXExtensionRaw | undefined> {
69
- let offset = 0;
70
- let loop = true;
71
- while (loop) {
72
- const queryOptions: VSXQueryOptions = {
73
- ...query,
74
- offset,
75
- size: 5 // there is a great chance that the newest version will work
76
- };
77
- const results = await this.client.query(queryOptions);
78
- const compatibleExtension = this.getLatestCompatibleExtension(results.extensions);
79
- if (compatibleExtension) {
80
- return compatibleExtension;
81
- }
82
- // Adjust offset by the amount of returned extensions
83
- offset += results.extensions.length;
84
- // Continue querying if there are more extensions available
85
- loop = results.totalSize > offset;
86
- }
87
- return undefined;
88
- }
89
-
90
- getLatestCompatibleExtension(extensions: VSXExtensionRaw[]): VSXExtensionRaw | undefined {
91
- if (extensions.length === 0) {
92
- return;
93
- } else if (this.isBuiltinNamespace(extensions[0].namespace.toLowerCase())) {
94
- return extensions.find(extension => this.versionGreaterThanOrEqualTo(this.supportedApiVersion, extension.version));
95
- } else {
96
- return extensions.find(extension => this.supportedVscodeApiSatisfies(extension.engines?.vscode ?? '*'));
97
- }
98
- }
99
-
100
- getLatestCompatibleVersion(searchEntry: VSXSearchEntry): VSXAllVersions | undefined {
101
- function getLatestCompatibleVersion(predicate: (allVersions: VSXAllVersions) => boolean): VSXAllVersions | undefined {
102
- if (searchEntry.allVersions) {
103
- return searchEntry.allVersions.find(predicate);
104
- }
105
- // If the allVersions field is missing then try to use the
106
- // searchEntry as VSXAllVersions and check if it's compatible:
107
- if (predicate(searchEntry)) {
108
- return searchEntry;
109
- }
110
- }
111
- if (this.isBuiltinNamespace(searchEntry.namespace)) {
112
- return getLatestCompatibleVersion(allVersions => this.versionGreaterThanOrEqualTo(this.supportedApiVersion, allVersions.version));
113
- } else {
114
- return getLatestCompatibleVersion(allVersions => this.supportedVscodeApiSatisfies(allVersions.engines?.vscode ?? '*'));
115
- }
116
- }
117
-
118
- protected isBuiltinNamespace(namespace: string): boolean {
119
- return VSXBuiltinNamespaces.is(namespace);
120
- }
121
-
122
- /**
123
- * @returns `a >= b`
124
- */
125
- protected versionGreaterThanOrEqualTo(a: string, b: string): boolean {
126
- const versionA = semver.clean(a);
127
- const versionB = semver.clean(b);
128
- if (!versionA || !versionB) {
129
- return false;
130
- }
131
- return semver.gte(versionA, versionB);
132
- }
133
-
134
- protected supportedVscodeApiSatisfies(vscodeApiRange: string): boolean {
135
- return semver.satisfies(this.supportedApiVersion, vscodeApiRange);
136
- }
137
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 Ericsson 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 * as semver from 'semver';
18
+ import { OVSXClient, VSXAllVersions, VSXBuiltinNamespaces, VSXExtensionRaw, VSXQueryOptions, VSXSearchEntry } from './ovsx-types';
19
+
20
+ export const OVSXApiFilterProvider = Symbol('OVSXApiFilterProvider');
21
+
22
+ export type OVSXApiFilterProvider = () => Promise<OVSXApiFilter>;
23
+
24
+ export const OVSXApiFilter = Symbol('OVSXApiFilter');
25
+ /**
26
+ * Filter various data types based on a pre-defined supported VS Code API version.
27
+ */
28
+ export interface OVSXApiFilter {
29
+ supportedApiVersion: string;
30
+ findLatestCompatibleExtension(query: VSXQueryOptions): Promise<VSXExtensionRaw | undefined>;
31
+ /**
32
+ * Get the latest compatible extension version:
33
+ * - A builtin extension is fetched based on the extension version which matches the API.
34
+ * - An extension satisfies compatibility if its `engines.vscode` version is supported.
35
+ *
36
+ * @param extensionId the extension id.
37
+ * @returns the data for the latest compatible extension version if available, else `undefined`.
38
+ */
39
+ getLatestCompatibleExtension(extensions: VSXExtensionRaw[]): VSXExtensionRaw | undefined;
40
+ getLatestCompatibleVersion(searchEntry: VSXSearchEntry): VSXAllVersions | undefined;
41
+ }
42
+
43
+ export class OVSXApiFilterImpl implements OVSXApiFilter {
44
+
45
+ constructor(
46
+ public client: OVSXClient,
47
+ public supportedApiVersion: string
48
+ ) { }
49
+
50
+ async findLatestCompatibleExtension(query: VSXQueryOptions): Promise<VSXExtensionRaw | undefined> {
51
+ const targetPlatform = query.targetPlatform;
52
+ if (!targetPlatform) {
53
+ return this.queryLatestCompatibleExtension(query);
54
+ }
55
+ const latestWithTargetPlatform = await this.queryLatestCompatibleExtension(query);
56
+ let latestUniversal: VSXExtensionRaw | undefined;
57
+ if (targetPlatform !== 'universal' && targetPlatform !== 'web') {
58
+ // Additionally query the universal version, as there might be a newer one available
59
+ latestUniversal = await this.queryLatestCompatibleExtension({ ...query, targetPlatform: 'universal' });
60
+ }
61
+ if (latestWithTargetPlatform && latestUniversal) {
62
+ // Prefer the version with the target platform if it's greater or equal to the universal version
63
+ return this.versionGreaterThanOrEqualTo(latestWithTargetPlatform.version, latestUniversal.version) ? latestWithTargetPlatform : latestUniversal;
64
+ }
65
+ return latestWithTargetPlatform ?? latestUniversal;
66
+ }
67
+
68
+ protected async queryLatestCompatibleExtension(query: VSXQueryOptions): Promise<VSXExtensionRaw | undefined> {
69
+ let offset = 0;
70
+ let size = 5;
71
+ let loop = true;
72
+ while (loop) {
73
+ const queryOptions: VSXQueryOptions = {
74
+ ...query,
75
+ offset,
76
+ size // there is a great chance that the newest version will work
77
+ };
78
+ const results = await this.client.query(queryOptions);
79
+ const compatibleExtension = this.getLatestCompatibleExtension(results.extensions);
80
+ if (compatibleExtension) {
81
+ return compatibleExtension;
82
+ }
83
+ // Adjust offset by the amount of returned extensions
84
+ offset += results.extensions.length;
85
+ // Continue querying if there are more extensions available
86
+ loop = results.totalSize > offset;
87
+ // Adjust the size to fetch more extensions next time
88
+ size = Math.min(size * 2, 100);
89
+ }
90
+ return undefined;
91
+ }
92
+
93
+ getLatestCompatibleExtension(extensions: VSXExtensionRaw[]): VSXExtensionRaw | undefined {
94
+ if (extensions.length === 0) {
95
+ return;
96
+ } else if (this.isBuiltinNamespace(extensions[0].namespace.toLowerCase())) {
97
+ return extensions.find(extension => this.versionGreaterThanOrEqualTo(this.supportedApiVersion, extension.version));
98
+ } else {
99
+ return extensions.find(extension => this.supportedVscodeApiSatisfies(extension.engines?.vscode ?? '*'));
100
+ }
101
+ }
102
+
103
+ getLatestCompatibleVersion(searchEntry: VSXSearchEntry): VSXAllVersions | undefined {
104
+ function getLatestCompatibleVersion(predicate: (allVersions: VSXAllVersions) => boolean): VSXAllVersions | undefined {
105
+ if (searchEntry.allVersions) {
106
+ return searchEntry.allVersions.find(predicate);
107
+ }
108
+ // If the allVersions field is missing then try to use the
109
+ // searchEntry as VSXAllVersions and check if it's compatible:
110
+ if (predicate(searchEntry)) {
111
+ return searchEntry;
112
+ }
113
+ }
114
+ if (this.isBuiltinNamespace(searchEntry.namespace)) {
115
+ return getLatestCompatibleVersion(allVersions => this.versionGreaterThanOrEqualTo(this.supportedApiVersion, allVersions.version));
116
+ } else {
117
+ return getLatestCompatibleVersion(allVersions => this.supportedVscodeApiSatisfies(allVersions.engines?.vscode ?? '*'));
118
+ }
119
+ }
120
+
121
+ protected isBuiltinNamespace(namespace: string): boolean {
122
+ return VSXBuiltinNamespaces.is(namespace);
123
+ }
124
+
125
+ /**
126
+ * @returns `a >= b`
127
+ */
128
+ protected versionGreaterThanOrEqualTo(a: string, b: string): boolean {
129
+ const versionA = semver.clean(a);
130
+ const versionB = semver.clean(b);
131
+ if (!versionA || !versionB) {
132
+ return false;
133
+ }
134
+ return semver.gte(versionA, versionB);
135
+ }
136
+
137
+ protected supportedVscodeApiSatisfies(vscodeApiRange: string): boolean {
138
+ return semver.satisfies(this.supportedApiVersion, vscodeApiRange);
139
+ }
140
+ }
@@ -1,70 +1,89 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2023 Ericsson 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 { OVSXClient, VSXQueryOptions, VSXQueryResult, VSXSearchOptions, VSXSearchResult } from './ovsx-types';
18
- import { RequestContext, RequestService } from '@theia/request';
19
-
20
- export class OVSXHttpClient implements OVSXClient {
21
-
22
- /**
23
- * @param requestService
24
- * @returns factory that will cache clients based on the requested input URL.
25
- */
26
- static createClientFactory(requestService: RequestService): (url: string) => OVSXClient {
27
- // eslint-disable-next-line no-null/no-null
28
- const cachedClients: Record<string, OVSXClient> = Object.create(null);
29
- return url => cachedClients[url] ??= new this(url, requestService);
30
- }
31
-
32
- constructor(
33
- protected vsxRegistryUrl: string,
34
- protected requestService: RequestService
35
- ) { }
36
-
37
- search(searchOptions?: VSXSearchOptions): Promise<VSXSearchResult> {
38
- return this.requestJson(this.buildUrl('api/-/search', searchOptions));
39
- }
40
-
41
- query(queryOptions?: VSXQueryOptions): Promise<VSXQueryResult> {
42
- return this.requestJson(this.buildUrl('api/v2/-/query', queryOptions));
43
- }
44
-
45
- protected async requestJson<R>(url: string): Promise<R> {
46
- return RequestContext.asJson<R>(await this.requestService.request({
47
- url,
48
- headers: { 'Accept': 'application/json' }
49
- }));
50
- }
51
-
52
- protected buildUrl(url: string, query?: object): string {
53
- return new URL(`${url}${this.buildQueryString(query)}`, this.vsxRegistryUrl).toString();
54
- }
55
-
56
- protected buildQueryString(searchQuery?: object): string {
57
- if (!searchQuery) {
58
- return '';
59
- }
60
- let queryString = '';
61
- for (const [key, value] of Object.entries(searchQuery)) {
62
- if (typeof value === 'string') {
63
- queryString += `&${key}=${encodeURIComponent(value)}`;
64
- } else if (typeof value === 'boolean' || typeof value === 'number') {
65
- queryString += `&${key}=${value}`;
66
- }
67
- }
68
- return queryString && '?' + queryString.slice(1);
69
- }
70
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 Ericsson 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 { OVSXClient, VSXQueryOptions, VSXQueryResult, VSXSearchOptions, VSXSearchResult } from './ovsx-types';
18
+ import { RequestContext, RequestService } from '@theia/request';
19
+ import { RateLimiter } from 'limiter';
20
+
21
+ export const OVSX_RATE_LIMIT = 15;
22
+
23
+ export class OVSXHttpClient implements OVSXClient {
24
+
25
+ /**
26
+ * @param requestService
27
+ * @returns factory that will cache clients based on the requested input URL.
28
+ */
29
+ static createClientFactory(requestService: RequestService, rateLimiter?: RateLimiter): (url: string) => OVSXClient {
30
+ // eslint-disable-next-line no-null/no-null
31
+ const cachedClients: Record<string, OVSXClient> = Object.create(null);
32
+ return url => cachedClients[url] ??= new this(url, requestService, rateLimiter);
33
+ }
34
+
35
+ constructor(
36
+ protected vsxRegistryUrl: string,
37
+ protected requestService: RequestService,
38
+ protected rateLimiter = new RateLimiter({ tokensPerInterval: OVSX_RATE_LIMIT, interval: 'second' })
39
+ ) { }
40
+
41
+ search(searchOptions?: VSXSearchOptions): Promise<VSXSearchResult> {
42
+ return this.requestJson(this.buildUrl('api/-/search', searchOptions));
43
+ }
44
+
45
+ query(queryOptions?: VSXQueryOptions): Promise<VSXQueryResult> {
46
+ return this.requestJson(this.buildUrl('api/v2/-/query', queryOptions));
47
+ }
48
+
49
+ protected async requestJson<R>(url: string): Promise<R> {
50
+ const attempts = 5;
51
+ for (let i = 0; i < attempts; i++) {
52
+ // Use 1, 2, 4, 8, 16 tokens for each attempt
53
+ const tokenCount = Math.pow(2, i);
54
+ await this.rateLimiter.removeTokens(tokenCount);
55
+ const context = await this.requestService.request({
56
+ url,
57
+ headers: { 'Accept': 'application/json' }
58
+ });
59
+ if (context.res.statusCode === 429) {
60
+ console.warn('OVSX rate limit exceeded. Consider reducing the rate limit.');
61
+ // If there are still more attempts left, retry the request with a higher token count
62
+ if (i < attempts - 1) {
63
+ continue;
64
+ }
65
+ }
66
+ return RequestContext.asJson<R>(context);
67
+ }
68
+ throw new Error('Failed to fetch data from OVSX.');
69
+ }
70
+
71
+ protected buildUrl(url: string, query?: object): string {
72
+ return new URL(`${url}${this.buildQueryString(query)}`, this.vsxRegistryUrl).toString();
73
+ }
74
+
75
+ protected buildQueryString(searchQuery?: object): string {
76
+ if (!searchQuery) {
77
+ return '';
78
+ }
79
+ let queryString = '';
80
+ for (const [key, value] of Object.entries(searchQuery)) {
81
+ if (typeof value === 'string') {
82
+ queryString += `&${key}=${encodeURIComponent(value)}`;
83
+ } else if (typeof value === 'boolean' || typeof value === 'number') {
84
+ queryString += `&${key}=${value}`;
85
+ }
86
+ }
87
+ return queryString && '?' + queryString.slice(1);
88
+ }
89
+ }