Skip to content

Commit

Permalink
fix electron
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Jan 6, 2023
1 parent 88e688f commit d8793f9
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 164 deletions.
4 changes: 2 additions & 2 deletions dev-packages/ovsx-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// *****************************************************************************

export { OVSXApiFilter, OVSXApiFilterImpl } from './ovsx-api-filter';
export * from './ovsx-http-client';
export { OVSXHttpClient } from './ovsx-http-client';
export { OVSXMockClient } from './ovsx-mock-client';
export { OVSXRouterClient, OVSXRouterConfig } from './ovsx-router-client';
export { ExtensionIdMatchesFilterFactory, RequestContainsFilterFactory } from './ovsx-router-filters';
export { ExtensionIdMatchesFilterFactory, FilterFactory, RequestContainsFilterFactory } from './ovsx-router-filters';
export * from './ovsx-types';

117 changes: 0 additions & 117 deletions dev-packages/ovsx-client/src/ovsx-client.spec.ts-todo

This file was deleted.

13 changes: 7 additions & 6 deletions dev-packages/ovsx-client/src/ovsx-mock-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { OVSXClient, VSXExtensionRaw, VSXQueryOptions, VSXQueryResult, VSXSearchOptions, VSXSearchResult } from './ovsx-types';
import { ExtensionLike, OVSXClient, VSXExtensionRaw, VSXQueryOptions, VSXQueryResult, VSXSearchOptions, VSXSearchResult } from './ovsx-types';

/**
* Querying will only find exact matches.
Expand Down Expand Up @@ -82,17 +82,18 @@ export class OVSXMockClient implements OVSXClient {
async search(searchOptions?: VSXSearchOptions): Promise<VSXSearchResult> {
const query = searchOptions?.query;
const offset = searchOptions?.offset ?? 0;
const size = searchOptions?.size ?? Infinity;
const size = searchOptions?.size ?? 18;
const end = offset + size;
return {
offset: 0,
offset,
extensions: this.extensions
.filter(extension => typeof query === 'string' && (
.filter(extension => typeof query !== 'string' || (
this.includes(query, this.id(extension)) ||
this.includes(query, extension.description) ||
this.includes(query, extension.displayName)
))
.sort((a, b) => this.sort(a, b, searchOptions))
.filter((extension, i) => i >= offset && i < size)
.filter((extension, i) => i >= offset && i < end)
.map(extension => ({
downloadCount: extension.downloadCount,
files: extension.files,
Expand All @@ -105,7 +106,7 @@ export class OVSXMockClient implements OVSXClient {
};
}

protected id(extension: { name: string, namespace: string }): string {
protected id(extension: ExtensionLike): string {
return `${extension.namespace}.${extension.name}`;
}

Expand Down
33 changes: 12 additions & 21 deletions dev-packages/ovsx-client/src/ovsx-router-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// *****************************************************************************

import type { FilterFactory, OVSXRequestFilter, OVSXResultFilter } from './ovsx-router-filters';
import { OVSXClient, VSXQueryOptions, VSXQueryResult, VSXSearchOptions, VSXSearchResult } from './ovsx-types';
import { OVSXClient, OVSXClientFactory, VSXQueryOptions, VSXQueryResult, VSXSearchOptions, VSXSearchResult } from './ovsx-types';
import type { MaybePromise } from './types';

export interface OVSXRouterConfig {
Expand All @@ -30,15 +30,15 @@ export interface OVSXRouterConfig {
*/
use: string | string[]
/**
* todo
* Filters for the different phases of interfacing with a registry.
*/
filters?: {
/**
* todo
* Filter requests.
*/
requests?: OVSXRouterRule[]
/**
* todo
* Filter results such as returned extensions.
*/
results?: OVSXRouterRule[]
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export class OVSXRouterClient implements OVSXClient {

static async FromConfig(
routerConfig: OVSXRouterConfig,
getClient: (uri: string) => OVSXClient,
getClient: OVSXClientFactory,
requestFilterFactories: FilterFactory<OVSXRequestFilter>[] = [],
resultFilterFactories: FilterFactory<OVSXResultFilter>[] = []
): Promise<OVSXRouterClient> {
Expand Down Expand Up @@ -114,7 +114,7 @@ export class OVSXRouterClient implements OVSXClient {

constructor(
protected useDefault: string[],
protected getClient: (uri: string) => OVSXClient,
protected getClient: OVSXClientFactory,
protected requestFilters: OVSXParsedRule<OVSXRequestFilter>[],
protected resultFilters: OVSXParsedRule<OVSXResultFilter>[]
) { }
Expand Down Expand Up @@ -153,20 +153,11 @@ export class OVSXRouterClient implements OVSXClient {
}

protected async mergedQuery(registries: string[], queryOptions?: VSXQueryOptions): Promise<VSXQueryResult> {
return this.mergeQueryResults(await createMapping(registries, registry => this.getClient(registry).query(queryOptions)));
return this.mergeQueryResults(await createMapping(registries, async registry => (await this.getClient(registry)).query(queryOptions)));
}

protected async mergedSearch(registries: string[], searchOptions?: VSXSearchOptions): Promise<VSXSearchResult> {
// do not mutate the original value passed as parameter, if any
searchOptions &&= { ...searchOptions };
if (typeof searchOptions?.size === 'number') {
searchOptions.size = Math.min(1, Math.floor(searchOptions.size / registries.length));
}
const result = await this.mergeSearchResults(await createMapping(registries, registry => this.getClient(registry).search(searchOptions)));
if (typeof searchOptions?.size === 'number') {
result.extensions = result.extensions.slice(0, searchOptions.size);
}
return result;
return this.mergeSearchResults(await createMapping(registries, async registry => (await this.getClient(registry)).search(searchOptions)));
}

protected async mergeSearchResults(results: Map<string, VSXSearchResult>): Promise<VSXSearchResult> {
Expand All @@ -187,7 +178,7 @@ export class OVSXRouterClient implements OVSXClient {
));
return {
extensions,
offset: 0
offset: Math.min(...Array.from(results.values(), result => result.offset))
};
}

Expand Down Expand Up @@ -217,15 +208,15 @@ export class OVSXRouterClient implements OVSXClient {
* Create a map where the keys are each element from {@link values} and the
* values are the result of a mapping function applied on the key.
*/
async function createMapping<T, U>(values: T[], map: (value: T) => MaybePromise<U>, thisArg?: unknown): Promise<Map<T, U>> {
return new Map(await Promise.all(values.map(async value => [value, await map.call(thisArg, value)] as [T, U])));
async function createMapping<T, U>(values: T[], map: (value: T, index: number) => MaybePromise<U>, thisArg?: unknown): Promise<Map<T, U>> {
return new Map(await Promise.all(values.map(async (value, index) => [value, await map.call(thisArg, value, index)] as [T, U])));
}

/**
* Asynchronously map the {@link values} array using the {@link map}
* function, then remove all null elements.
*/
async function mapNonNull<T, U>(values: T[], map: (value: T) => MaybePromise<U>, thisArg?: unknown): Promise<NonNullable<U>[]> {
async function mapNonNull<T, U>(values: T[], map: (value: T, index: number) => MaybePromise<U>, thisArg?: unknown): Promise<NonNullable<U>[]> {
return (await Promise.all(values.map(map, thisArg))).filter(nonNullable);
}

Expand Down
4 changes: 4 additions & 0 deletions dev-packages/ovsx-client/src/ovsx-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { MaybePromise } from './types';

export interface ExtensionLike {
name: string
namespace: string
Expand Down Expand Up @@ -252,3 +254,5 @@ export namespace VSXBuiltinNamespaces {
|| namespace === THEIA;
}
}

export type OVSXClientFactory = (uri: string) => MaybePromise<OVSXClient>;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { SampleAppInfo } from '../../common/vsx/sample-app-info';
@injectable()
export class SampleFrontendAppInfo implements SampleAppInfo {

getSelfOrigin(): string {
async getSelfOrigin(): Promise<string> {
return new Endpoint().origin;
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/api-samples/src/common/vsx/sample-app-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ import { interfaces } from '@theia/core/shared/inversify';

export const SampleAppInfo = Symbol('SampleAppInfo') as symbol & interfaces.Abstract<SampleAppInfo>;
export interface SampleAppInfo {
getSelfOrigin(): string;
getSelfOrigin(): Promise<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { RequestService } from '@theia/core/shared/@theia/request';
import { interfaces } from '@theia/core/shared/inversify';
import { OVSXHttpClient } from '@theia/ovsx-client';
import { OVSXClientFactory } from '@theia/vsx-registry/lib/common/ovsx-client-provider';
import { OVSXClientFactory } from '@theia/vsx-registry/lib/common';
import { SampleAppInfo } from './sample-app-info';

export function rebindOVSXClientFactory(rebind: interfaces.Rebind): void {
Expand All @@ -28,7 +28,7 @@ export function rebindOVSXClientFactory(rebind: interfaces.Rebind): void {
const clientFactory = OVSXHttpClient.createClientFactory(requestService);
const appInfo = ctx.container.get(SampleAppInfo);
const selfOrigin = appInfo.getSelfOrigin();
return url => clientFactory(url.replace('${self}', selfOrigin));
return async url => clientFactory(url.replace('${self}', await selfOrigin));
})
.inSingletonScope();
}
4 changes: 3 additions & 1 deletion examples/api-samples/src/node/api-samples-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { rebindOVSXClientFactory } from '../common/vsx/sample-ovsx-client-factor

export default new ContainerModule((bind, unbind, isBound, rebind) => {
rebindOVSXClientFactory(rebind);
bind(SampleAppInfo).to(SampleBackendAppInfo).inSingletonScope();
bind(SampleBackendAppInfo).toSelf().inSingletonScope();
bind(SampleAppInfo).toService(SampleBackendAppInfo);
bind(BackendApplicationContribution).toService(SampleBackendAppInfo);
// bind a mock/sample OpenVSX registry:
bind(BackendApplicationContribution).to(SampleMockOpenVsxServer).inSingletonScope();
if (process.env.SAMPLE_BACKEND_APPLICATION_SERVER) {
Expand Down
28 changes: 24 additions & 4 deletions examples/api-samples/src/node/sample-backend-app-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,39 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { Deferred, environment } from '@theia/core/lib/common';
import { BackendApplicationCliContribution, BackendApplicationContribution } from '@theia/core/lib/node';
import { inject, injectable } from '@theia/core/shared/inversify';
import { BackendApplicationCliContribution } from '@theia/core/lib/node';
import * as net from 'net';
import { SampleAppInfo } from '../common/vsx/sample-app-info';

@injectable()
export class SampleBackendAppInfo implements SampleAppInfo {
export class SampleBackendAppInfo implements SampleAppInfo, BackendApplicationContribution {

protected addressDeferred = new Deferred<net.AddressInfo>();

@inject(BackendApplicationCliContribution)
protected backendCli: BackendApplicationCliContribution;

getSelfOrigin(): string {
const { hostname, port, ssl } = this.backendCli;
onStart(server: net.Server): void {
const address = server.address();
// eslint-disable-next-line no-null/no-null
if (typeof address === 'object' && address !== null) {
this.addressDeferred.resolve(address);
} else {
this.addressDeferred.resolve({
address: '127.0.0.1',
port: 3000,
family: '4'
});
}
}

async getSelfOrigin(): Promise<string> {
const { ssl } = this.backendCli;
const protocol = ssl ? 'https' : 'http';
const { address, port } = await this.addressDeferred.promise;
const hostname = environment.electron.is() ? 'localhost' : address;
return `${protocol}://${hostname}:${port}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class SampleMockOpenVsxServer implements BackendApplicationContribution {
}

async configure(app: express.Application): Promise<void> {
const selfOrigin = this.appInfo.getSelfOrigin();
const selfOrigin = await this.appInfo.getSelfOrigin();
const baseUrl = `${selfOrigin}${this.mockServerPath}`;
const pluginsDb = await this.findMockPlugins(this.pluginsDbPath, baseUrl);
const staticFileHandlers = new Map(Array.from(pluginsDb.entries(), ([key, value]) => [key, express.static(value.path)]));
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ export * from './contribution-filter';
export * from './nls';
export * from './numbers';
export * from './performance';

import { environment } from '@theia/application-package/lib/environment';
export { environment };
export { environment } from '@theia/application-package/lib/environment';
export { Deferred } from './promise-util';
Loading

0 comments on commit d8793f9

Please sign in to comment.