@theia/ovsx-client 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.
package/README.md CHANGED
@@ -1,61 +1,61 @@
1
- <div align='center'>
2
-
3
- <br />
4
-
5
- <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
-
7
- <h2>ECLIPSE THEIA - OVSX CLIENT</h2>
8
-
9
- <hr />
10
-
11
- </div>
12
-
13
- ## Description
14
-
15
- The `@theia/ovsx-client` package is used to interact with `open-vsx` through its REST APIs.
16
- The package allows clients to fetch extensions and their metadata, search the registry, and
17
- includes the necessary logic to determine compatibility based on a provided supported API version.
18
-
19
- Note that this client only supports a subset of the whole OpenVSX API, only what's relevant to
20
- clients like Theia applications.
21
-
22
- ### `OVSXRouterClient`
23
-
24
- This class is an `OVSXClient` that can delegate requests to sub-clients based on some configuration (`OVSXRouterConfig`).
25
-
26
- ```jsonc
27
- {
28
- "registries": {
29
- // `[Alias]: URL` pairs to avoid copy pasting URLs down the config
30
- },
31
- "use": [
32
- // List of aliases/URLs to use when no filtering was applied.
33
- ],
34
- "rules": [
35
- {
36
- "ifRequestContains": "regex matched against various fields in requests",
37
- "ifExtensionIdMatches": "regex matched against the extension id (without version)",
38
- "use": [/*
39
- List of registries to forward the request to when all the
40
- conditions are matched.
41
-
42
- `null` or `[]` means to not forward the request anywhere.
43
- */]
44
- }
45
- ]
46
- }
47
- ```
48
-
49
- ## Additional Information
50
-
51
- - [Theia - GitHub](https://github.com/eclipse-theia/theia)
52
- - [Theia - Website](https://theia-ide.org/)
53
-
54
- ## License
55
-
56
- - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
57
- - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
58
-
59
- ## Trademark
60
- "Theia" is a trademark of the Eclipse Foundation
61
- https://www.eclipse.org/theia
1
+ <div align='center'>
2
+
3
+ <br />
4
+
5
+ <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
+
7
+ <h2>ECLIPSE THEIA - OVSX CLIENT</h2>
8
+
9
+ <hr />
10
+
11
+ </div>
12
+
13
+ ## Description
14
+
15
+ The `@theia/ovsx-client` package is used to interact with `open-vsx` through its REST APIs.
16
+ The package allows clients to fetch extensions and their metadata, search the registry, and
17
+ includes the necessary logic to determine compatibility based on a provided supported API version.
18
+
19
+ Note that this client only supports a subset of the whole OpenVSX API, only what's relevant to
20
+ clients like Theia applications.
21
+
22
+ ### `OVSXRouterClient`
23
+
24
+ This class is an `OVSXClient` that can delegate requests to sub-clients based on some configuration (`OVSXRouterConfig`).
25
+
26
+ ```jsonc
27
+ {
28
+ "registries": {
29
+ // `[Alias]: URL` pairs to avoid copy pasting URLs down the config
30
+ },
31
+ "use": [
32
+ // List of aliases/URLs to use when no filtering was applied.
33
+ ],
34
+ "rules": [
35
+ {
36
+ "ifRequestContains": "regex matched against various fields in requests",
37
+ "ifExtensionIdMatches": "regex matched against the extension id (without version)",
38
+ "use": [/*
39
+ List of registries to forward the request to when all the
40
+ conditions are matched.
41
+
42
+ `null` or `[]` means to not forward the request anywhere.
43
+ */]
44
+ }
45
+ ]
46
+ }
47
+ ```
48
+
49
+ ## Additional Information
50
+
51
+ - [Theia - GitHub](https://github.com/eclipse-theia/theia)
52
+ - [Theia - Website](https://theia-ide.org/)
53
+
54
+ ## License
55
+
56
+ - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
57
+ - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
58
+
59
+ ## Trademark
60
+ "Theia" is a trademark of the Eclipse Foundation
61
+ https://www.eclipse.org/theia
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theia/ovsx-client",
3
- "version": "1.53.0-next.55+d1a989a68c",
3
+ "version": "1.53.0-next.64+23b351d26",
4
4
  "description": "Theia Open-VSX Client",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -29,10 +29,10 @@
29
29
  "watch": "theiaext watch"
30
30
  },
31
31
  "dependencies": {
32
- "@theia/request": "1.53.0-next.55+d1a989a68c",
32
+ "@theia/request": "1.53.0-next.64+23b351d26",
33
33
  "limiter": "^2.1.0",
34
34
  "semver": "^7.5.4",
35
35
  "tslib": "^2.6.2"
36
36
  },
37
- "gitHead": "d1a989a68c1b5ec1f9098e9126653c6346844769"
37
+ "gitHead": "23b351d26346a2b5d6aca3ee81fba59c056132f7"
38
38
  }
package/src/index.ts CHANGED
@@ -1,22 +1,22 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2021 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
- export { OVSXApiFilter, OVSXApiFilterImpl, OVSXApiFilterProvider } from './ovsx-api-filter';
18
- export { OVSXHttpClient, OVSX_RATE_LIMIT } from './ovsx-http-client';
19
- export { OVSXMockClient } from './ovsx-mock-client';
20
- export { OVSXRouterClient, OVSXRouterConfig, OVSXRouterFilterFactory as FilterFactory } from './ovsx-router-client';
21
- export * from './ovsx-router-filters';
22
- export * from './ovsx-types';
1
+ // *****************************************************************************
2
+ // Copyright (C) 2021 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
+ export { OVSXApiFilter, OVSXApiFilterImpl, OVSXApiFilterProvider } from './ovsx-api-filter';
18
+ export { OVSXHttpClient, OVSX_RATE_LIMIT } from './ovsx-http-client';
19
+ export { OVSXMockClient } from './ovsx-mock-client';
20
+ export { OVSXRouterClient, OVSXRouterConfig, OVSXRouterFilterFactory as FilterFactory } from './ovsx-router-client';
21
+ export * from './ovsx-router-filters';
22
+ export * from './ovsx-types';
@@ -1,140 +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 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
+ // *****************************************************************************
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,89 +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
- 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
- }
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
+ }