From 798a22e703f20738de4b11aa3ec20dd62818d37d Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 16 Feb 2023 18:46:02 +0100 Subject: [PATCH 01/16] CoreClientConfig - use unique map instead of array for config.envs --- .../src/ClientConfigBuilder.ts | 12 +--- .../__tests__/client-config-builder.spec.ts | 72 ++++++++----------- packages/js/client/src/PolywrapClient.ts | 7 +- packages/js/core-client/README.md | 4 +- .../js/core-client/src/PolywrapCoreClient.ts | 13 ++-- packages/js/core/README.md | 24 +++---- packages/js/core/src/types/CoreClient.ts | 12 ++-- packages/js/core/src/types/Env.ts | 26 ++++--- .../js/core/src/utils/getEnvFromUriHistory.ts | 4 +- .../src/UriResolverWrapper.ts | 2 +- 10 files changed, 81 insertions(+), 95 deletions(-) diff --git a/packages/js/client-config-builder/src/ClientConfigBuilder.ts b/packages/js/client-config-builder/src/ClientConfigBuilder.ts index 844d182120..978f5dbcdd 100644 --- a/packages/js/client-config-builder/src/ClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/ClientConfigBuilder.ts @@ -5,7 +5,7 @@ import { BuilderConfig } from "./types"; import { CoreClientConfig, - Env, + Envs, InterfaceImplementations, IUriPackage, IUriRedirect, @@ -73,14 +73,8 @@ export class ClientConfigBuilder extends BaseClientConfigBuilder { return this._config; } - private buildEnvs(): Env[] { - const envs: Env[] = []; - - for (const [uri, env] of Object.entries(this._config.envs)) { - envs.push({ uri: Uri.from(uri), env }); - } - - return envs; + private buildEnvs(): Envs { + return this._config.envs; } private buildInterfaces(): InterfaceImplementations[] { diff --git a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts index 358b18528f..1d71b3fef8 100644 --- a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts +++ b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts @@ -79,7 +79,7 @@ describe("Client config builder", () => { it("should build an empty partial config", () => { const clientConfig = new ClientConfigBuilder().build(); - expect(clientConfig.envs).toStrictEqual([]); + expect(clientConfig.envs).toStrictEqual({}); expect(clientConfig.interfaces).toStrictEqual([]); }); @@ -97,12 +97,7 @@ describe("Client config builder", () => { const builderConfig = builder.config; expect(clientConfig).toBeTruthy(); - expect(clientConfig.envs).toStrictEqual( - Object.entries(testEnvs).map(([uri, env]) => ({ - uri: Uri.from(uri), - env: env, - })) - ); + expect(clientConfig.envs).toStrictEqual(testEnvs); expect(clientConfig.interfaces).toStrictEqual( Object.entries(testInterfaces).map(([uri, interfaces]) => ({ interface: Uri.from(uri), @@ -134,12 +129,10 @@ describe("Client config builder", () => { const builderConfig = builder.config; expect(clientConfig).toBeTruthy(); - expect(clientConfig.envs).toStrictEqual( - Object.entries(testEnvs).map(([uri, env]) => ({ - uri: Uri.from(uri), - env: env, - })) - ); + expect(clientConfig.envs).toStrictEqual({ + ...testEnv1, + ...testEnv2, + }); expect(clientConfig.interfaces).toStrictEqual( Object.entries(testInterfaces).map(([uri, interfaces]) => ({ interface: Uri.from(uri), @@ -167,7 +160,9 @@ describe("Client config builder", () => { expect(clientConfig).toBeTruthy(); const expectedBuilderConfig = getDefaultConfig(); - expect(JSON.stringify(builderConfig)).toBe(JSON.stringify(expectedBuilderConfig)); + expect(JSON.stringify(builderConfig)).toBe( + JSON.stringify(expectedBuilderConfig) + ); }); it("should successfully add an env", () => { @@ -181,12 +176,11 @@ describe("Client config builder", () => { const config = new ClientConfigBuilder().addEnv(envUri, env).build(); - if (!config.envs || config.envs.length !== 1) { + if (!config.envs || Object.keys(config.envs).length !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[0].uri).toStrictEqual(Uri.from(envUri)); - expect(config.envs[0].env).toStrictEqual(env); + expect(config.envs[envUri]).toEqual(env); }); it("should successfully add to an existing env", () => { @@ -207,12 +201,11 @@ describe("Client config builder", () => { const expectedEnv = { ...env1, ...env2 }; - if (!config.envs || config.envs.length !== 1) { + if (!config.envs || Object.keys(config.envs).length !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[0].uri).toStrictEqual(Uri.from(envUri)); - expect(config.envs[0].env).toStrictEqual(expectedEnv); + expect(config.envs[envUri]).toEqual(expectedEnv); }); it("should succesfully add two separate envs", () => { @@ -221,18 +214,16 @@ describe("Client config builder", () => { .addEnv(Object.keys(testEnvs)[1], Object.values(testEnvs)[1]) .build(); - if (!config.envs || config.envs.length !== 2) { + if (!config.envs || Object.keys(config.envs).length !== 2) { fail(["Expected 2 envs, received:", config.envs]); } - expect(config.envs).toContainEqual({ - uri: Uri.from(Object.keys(testEnvs)[0]), - env: Object.values(testEnvs)[0], - }); - expect(config.envs).toContainEqual({ - uri: Uri.from(Object.keys(testEnvs)[1]), - env: Object.values(testEnvs)[1], - }); + expect(config.envs[Object.keys(testEnvs)[0]]).toEqual( + Object.values(testEnvs)[0] + ); + expect(config.envs[Object.keys(testEnvs)[0]]).toEqual( + Object.values(testEnvs)[1] + ); }); it("should remove an env", () => { @@ -242,14 +233,13 @@ describe("Client config builder", () => { .removeEnv(Object.keys(testEnvs)[0]) .build(); - if (!config.envs || config.envs.length !== 1) { + if (!config.envs || Object.keys(config.envs).length !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs).toContainEqual({ - uri: Uri.from(Object.keys(testEnvs)[1]), - env: Object.values(testEnvs)[1], - }); + expect(config.envs[Object.keys(testEnvs)[1]]).toEqual( + Object.values(testEnvs)[1] + ); }); it("should set an env", () => { @@ -261,14 +251,11 @@ describe("Client config builder", () => { const config = new ClientConfigBuilder().setEnv(envUri, env).build(); - if (!config.envs || config.envs.length !== 1) { + if (!config.envs || Object.keys(config.envs).length !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[0]).toEqual({ - uri: Uri.from(envUri), - env: env, - }); + expect(config.envs[envUri]).toEqual(env); }); it("should set an env over an existing env", () => { @@ -286,14 +273,11 @@ describe("Client config builder", () => { .setEnv(envUri, env2) .build(); - if (!config.envs || config.envs.length !== 1) { + if (!config.envs || Object.keys(config.envs).length !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[0]).toEqual({ - uri: Uri.from(envUri), - env: env2, - }); + expect(config.envs[envUri]).toEqual(env2); }); it("should add an interface implementation for a non-existent interface", () => { diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index db727796f0..ae09925e23 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -3,7 +3,7 @@ import { InvokerOptions, TryResolveUriOptions } from "./types"; import { PolywrapCoreClient } from "@polywrap/core-client-js"; import { CoreClientConfig, - Env, + Envs, GetFileOptions, GetImplementationsOptions, InterfaceImplementations, @@ -15,6 +15,7 @@ import { ValidateOptions, WrapError, Wrapper, + WrapperEnv, } from "@polywrap/core-js"; import { Result, ResultErr, ResultOk } from "@polywrap/result"; import { @@ -66,7 +67,7 @@ export class PolywrapClient extends PolywrapCoreClient { } @Tracer.traceMethod("PolywrapClient: getEnvs") - public getEnvs(): readonly Env[] | undefined { + public getEnvs(): Readonly | undefined { return super.getEnvs(); } @@ -78,7 +79,7 @@ export class PolywrapClient extends PolywrapCoreClient { @Tracer.traceMethod("PolywrapClient: getEnvByUri") public getEnvByUri( uri: TUri - ): Env | undefined { + ): Readonly | undefined { return super.getEnvByUri(Uri.from(uri)); } diff --git a/packages/js/core-client/README.md b/packages/js/core-client/README.md index b5defa23fb..bf6bc85c15 100644 --- a/packages/js/core-client/README.md +++ b/packages/js/core-client/README.md @@ -86,7 +86,7 @@ Invoke a wrapper. * * @returns an array of env objects containing wrapper environmental variables */ - public getEnvs(): readonly Env[] | undefined + public getEnvs(): Readonly | undefined ``` ### getResolver @@ -107,7 +107,7 @@ Invoke a wrapper. * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - public getEnvByUri(uri: Uri): Env | undefined + public getEnvByUri(uri: Uri): Readonly | undefined ``` ### getManifest diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index e69afa98b6..6ea2d561ff 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -1,7 +1,6 @@ import { Wrapper, CoreClient, - Env, GetFileOptions, GetImplementationsOptions, InterfaceImplementations, @@ -20,6 +19,8 @@ import { CoreClientConfig, WrapError, WrapErrorCode, + Envs, + WrapperEnv, } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { @@ -65,7 +66,7 @@ export class PolywrapCoreClient implements CoreClient { * * @returns an array of env objects containing wrapper environmental variables */ - public getEnvs(): readonly Env[] | undefined /* $ */ { + public getEnvs(): Readonly | undefined /* $ */ { return this._config.envs; } @@ -86,15 +87,13 @@ export class PolywrapCoreClient implements CoreClient { * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - public getEnvByUri(uri: Uri): Env | undefined /* $ */ { - const uriUri = Uri.from(uri); - + public getEnvByUri(uri: Uri): Readonly | undefined /* $ */ { const envs = this.getEnvs(); if (!envs) { return undefined; } - return envs.find((environment) => Uri.equals(environment.uri, uriUri)); + return envs[uri.uri]; } // $start: PolywrapCoreClient-getManifest @@ -295,7 +294,7 @@ export class PolywrapCoreClient implements CoreClient { ); const invokeResult = await this.invokeWrapper({ - env: env?.env, + env: env, ...typedOptions, wrapper, }); diff --git a/packages/js/core/README.md b/packages/js/core/README.md index 2e5c527ac9..beb93eada5 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -12,11 +12,11 @@ A TypeScript / JavaScript implementation of the WRAP standard, including all fun /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { - /** set environmental variables for a wrapper */ + /** register interface implementations */ readonly interfaces?: Readonly; - /** register interface implementations */ - readonly envs?: Readonly; + /** set environmental variables for a wrapper */ + readonly envs?: Envs; /** configure URI resolution for redirects, packages, and wrappers */ readonly resolver: Readonly>; @@ -70,7 +70,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of env objects containing wrapper environmental variables */ - getEnvs(): readonly Env[] | undefined; + getEnvs(): Readonly | undefined; /** * returns an env (a set of environmental variables) from the configuration used to instantiate the client @@ -78,7 +78,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - getEnvByUri(uri: Uri): Env | undefined; + getEnvByUri(uri: Uri): Readonly | undefined; /** * returns the URI resolver from the configuration used to instantiate the client @@ -127,14 +127,14 @@ export interface CoreClient extends Invoker, UriResolverHandler { ```ts -/** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ -export interface Env { - /** Uri of wrapper */ - uri: Uri; +// /** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ +// export interface Env { +// /** Uri of wrapper */ +// uri: Uri; - /** Env variables used by the module */ - env: Record; -} +// /** Env variables used by the module */ +// env: Record; +// } ``` ### InterfaceImplementations diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index 1089f1880a..0cc41023e9 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -1,4 +1,4 @@ -import { Invoker, Uri, InterfaceImplementations, Env, WrapError } from "."; +import { Invoker, Uri, InterfaceImplementations, WrapError, Envs, WrapperEnv } from "."; import { IUriResolutionContext, IUriResolver } from "../uri-resolution"; import { UriResolverHandler } from "./UriResolver"; @@ -9,11 +9,11 @@ import { Result } from "@polywrap/result"; /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { - /** set environmental variables for a wrapper */ + /** register interface implementations */ readonly interfaces?: Readonly; - /** register interface implementations */ - readonly envs?: Readonly; + /** set environmental variables for a wrapper */ + readonly envs?: Envs; /** configure URI resolution for redirects, packages, and wrappers */ readonly resolver: Readonly>; @@ -67,7 +67,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of env objects containing wrapper environmental variables */ - getEnvs(): readonly Env[] | undefined; + getEnvs(): Readonly | undefined; /** * returns an env (a set of environmental variables) from the configuration used to instantiate the client @@ -75,7 +75,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - getEnvByUri(uri: Uri): Env | undefined; + getEnvByUri(uri: Uri): Readonly | undefined; /** * returns the URI resolver from the configuration used to instantiate the client diff --git a/packages/js/core/src/types/Env.ts b/packages/js/core/src/types/Env.ts index 0bc8af2127..733dfeff8a 100644 --- a/packages/js/core/src/types/Env.ts +++ b/packages/js/core/src/types/Env.ts @@ -1,13 +1,21 @@ -import { Uri } from "."; +// import { Uri } from "."; -// $start: Env.ts +// // $start: Env.ts -/** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ -export interface Env { - /** Uri of wrapper */ - uri: Uri; +// /** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ +// export interface Env { +// /** Uri of wrapper */ +// uri: Uri; - /** Env variables used by the module */ - env: Record; +// /** Env variables used by the module */ +// env: Record; +// } +// // $end + +export interface Envs { + readonly [k: string]: WrapperEnv; } -// $end + +export interface WrapperEnv { + readonly [k: string]: unknown; +} \ No newline at end of file diff --git a/packages/js/core/src/utils/getEnvFromUriHistory.ts b/packages/js/core/src/utils/getEnvFromUriHistory.ts index ef96d2cad4..4bf6623798 100644 --- a/packages/js/core/src/utils/getEnvFromUriHistory.ts +++ b/packages/js/core/src/utils/getEnvFromUriHistory.ts @@ -1,9 +1,9 @@ -import { Uri, CoreClient, Env } from "../types"; +import { Uri, CoreClient, WrapperEnv } from "../types"; export const getEnvFromUriHistory = ( uriHistory: Uri[], client: CoreClient -): Env | undefined => { +): Readonly | undefined => { for (const uri of uriHistory) { const env = client.getEnvByUri(uri); diff --git a/packages/js/uri-resolver-extensions/src/UriResolverWrapper.ts b/packages/js/uri-resolver-extensions/src/UriResolverWrapper.ts index b93d4152eb..41d892cbc6 100644 --- a/packages/js/uri-resolver-extensions/src/UriResolverWrapper.ts +++ b/packages/js/uri-resolver-extensions/src/UriResolverWrapper.ts @@ -126,7 +126,7 @@ const tryResolveUriWithImplementation = async ( authority: uri.authority, path: uri.path, }, - env: env?.env, + env: env, } ); From 97485f38e88a77152a638e06af9de9eace588a1a Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 22 Feb 2023 18:36:18 +0100 Subject: [PATCH 02/16] fix env tests --- .../option-parsers/option-parsers.spec.ts | 4 +-- .../src/BaseClientConfigBuilder.ts | 14 +++++++--- .../__tests__/core/wrap-features/env-case.ts | 28 ++++++++----------- packages/js/client/src/__tests__/e2e.spec.ts | 12 ++++---- .../src/__tests__/helpers/getClient.ts | 15 ++++------ 5 files changed, 35 insertions(+), 38 deletions(-) diff --git a/packages/cli/src/__tests__/unit/option-parsers/option-parsers.spec.ts b/packages/cli/src/__tests__/unit/option-parsers/option-parsers.spec.ts index e154930a6b..6307f00310 100644 --- a/packages/cli/src/__tests__/unit/option-parsers/option-parsers.spec.ts +++ b/packages/cli/src/__tests__/unit/option-parsers/option-parsers.spec.ts @@ -4,13 +4,13 @@ import { parseWrapperEnvsOption } from "../../../lib"; describe("unit tests for option-parsers", () => { describe("wrapper-envs", () => { const sampleFileEnvs = { - "ens/wraps.eth:ethereum@1.0.0": { + "wrap://ens/wraps.eth:ethereum@1.0.0": { connection: { networkNameOrChainId: "mainnet", node: "https://mainnet.infura.io/v3/some_api_key", }, }, - "ens/hello-world.polywrap.eth": { foo: "bar" }, + "wrap://ens/hello-world.polywrap.eth": { foo: "bar" }, }; it("Should return undefined when undefined is provided for wrapperEnvsPath", async () => { diff --git a/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts b/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts index 1b6dc8999f..8f27b83ed9 100644 --- a/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts @@ -1,6 +1,6 @@ import { BuilderConfig, IClientConfigBuilder } from "./types"; -import { CoreClientConfig, Wrapper, IWrapPackage } from "@polywrap/core-js"; +import { CoreClientConfig, Wrapper, IWrapPackage, Uri } from "@polywrap/core-js"; import { UriResolverLike } from "@polywrap/uri-resolvers-js"; export abstract class BaseClientConfigBuilder implements IClientConfigBuilder { @@ -92,7 +92,9 @@ export abstract class BaseClientConfigBuilder implements IClientConfigBuilder { } addEnv(uri: string, env: Record): IClientConfigBuilder { - this._config.envs[uri] = { ...this._config.envs[uri], ...env }; + const sanitizedUri = Uri.from(uri).uri; + + this._config.envs[sanitizedUri] = { ...this._config.envs[sanitizedUri], ...env }; return this; } @@ -106,13 +108,17 @@ export abstract class BaseClientConfigBuilder implements IClientConfigBuilder { } removeEnv(uri: string): IClientConfigBuilder { - delete this._config.envs[uri]; + const sanitizedUri = Uri.from(uri).uri; + + delete this._config.envs[sanitizedUri]; return this; } setEnv(uri: string, env: Record): IClientConfigBuilder { - this._config.envs[uri] = env; + const sanitizedUri = Uri.from(uri).uri; + + this._config.envs[sanitizedUri] = env; return this; } diff --git a/packages/js/client/src/__tests__/core/wrap-features/env-case.ts b/packages/js/client/src/__tests__/core/wrap-features/env-case.ts index 88864f6140..694a9bec42 100644 --- a/packages/js/client/src/__tests__/core/wrap-features/env-case.ts +++ b/packages/js/client/src/__tests__/core/wrap-features/env-case.ts @@ -175,14 +175,11 @@ export const envTestCases = (implementation: string) => { }, { from: Uri.from("ens/hello.eth"), to: implementationUri }, ]), - envs: [ - { - uri: Uri.from("ens/hello.eth"), - env: { - arg1: "10", - }, + envs: { + "wrap://ens/hello.eth": { + arg1: "10", }, - ], + }, }); const mockEnv = await client.invoke({ @@ -196,7 +193,7 @@ export const envTestCases = (implementation: string) => { }); test("inline plugin env types", async () => { - const implementationUri = Uri.from("wrap://ens/some-implementation.eth"); + const implementationUri = "wrap://ens/some-implementation.eth"; interface MockEnv extends Record { arg1: number; } @@ -204,7 +201,7 @@ export const envTestCases = (implementation: string) => { const client = new PolywrapClient({ resolver: RecursiveResolver.from([ { - uri: implementationUri, + uri: Uri.from(implementationUri), package: PluginPackage.from((module) => ({ mockEnv: (): MockEnv => { return module.env; @@ -212,18 +209,15 @@ export const envTestCases = (implementation: string) => { })), }, ]), - envs: [ - { - uri: implementationUri, - env: { - arg1: "10", - }, + envs: { + [implementationUri]: { + arg1: "10", }, - ], + }, }); const mockEnv = await client.invoke({ - uri: implementationUri, + uri: Uri.from(implementationUri), method: "mockEnv", }); diff --git a/packages/js/client/src/__tests__/e2e.spec.ts b/packages/js/client/src/__tests__/e2e.spec.ts index 09d21a5f02..bb1a51db95 100644 --- a/packages/js/client/src/__tests__/e2e.spec.ts +++ b/packages/js/client/src/__tests__/e2e.spec.ts @@ -1,15 +1,15 @@ -import { typeTestCases } from "./core/type-test-cases"; -import { subinvokeCase } from "./core/wrap-features/subinvoke-case"; +// import { typeTestCases } from "./core/type-test-cases"; +// import { subinvokeCase } from "./core/wrap-features/subinvoke-case"; import { envTestCases } from "./core/wrap-features/env-case"; -import { interfaceInvokeCase } from "./core/wrap-features/interface-implementation-case"; +// import { interfaceInvokeCase } from "./core/wrap-features/interface-implementation-case"; export const supportedImplementations = ["as", "rs"]; describe.each(supportedImplementations)( "client <-> wrappers end to end", (i) => { - typeTestCases(i); - subinvokeCase(i); + // typeTestCases(i); + // subinvokeCase(i); envTestCases(i); - interfaceInvokeCase(i); + // interfaceInvokeCase(i); } ); diff --git a/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts b/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts index 5876138cd3..1a3f7e7f68 100644 --- a/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts +++ b/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts @@ -24,16 +24,13 @@ import { Uri } from "@polywrap/core-js"; export const getClient = () => { return new PolywrapClient({ - envs: [ - { - uri: Uri.from(defaultPackages.ipfsResolver), - env: { - provider: providers.ipfs, - fallbackProviders: defaultIpfsProviders, - retries: { tryResolveUri: 1, getFile: 1 }, - }, + envs: { + [defaultPackages.ipfsResolver]: { + provider: providers.ipfs, + fallbackProviders: defaultIpfsProviders, + retries: { tryResolveUri: 1, getFile: 1 }, }, - ], + }, interfaces: [ { interface: ExtendableUriResolver.extInterfaceUri, From b7413de4c1b333f4355606a70becfd20204c1519 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 23 Feb 2023 00:29:02 +0100 Subject: [PATCH 03/16] ClientConfig - change interfaces into a map instead of array --- .../src/ClientConfigBuilder.ts | 25 ++- .../__tests__/client-config-builder.spec.ts | 169 ++++++------------ packages/js/client/src/PolywrapClient.ts | 4 +- packages/js/core-client/README.md | 2 +- .../js/core-client/src/PolywrapCoreClient.ts | 6 +- packages/js/core/README.md | 4 +- .../src/__tests__/get-implementations.spec.ts | 130 ++++++-------- .../src/algorithms/get-implementations.ts | 19 +- packages/js/core/src/types/CoreClient.ts | 13 +- .../src/types/InterfaceImplementations.ts | 4 + packages/js/uri-resolver-extensions/README.md | 25 +-- .../examples/quickstart.ts | 27 ++- 12 files changed, 170 insertions(+), 258 deletions(-) diff --git a/packages/js/client-config-builder/src/ClientConfigBuilder.ts b/packages/js/client-config-builder/src/ClientConfigBuilder.ts index e7f304f857..724fd0a693 100644 --- a/packages/js/client-config-builder/src/ClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/ClientConfigBuilder.ts @@ -5,11 +5,11 @@ import { BuildOptions, IClientConfigBuilder, BuilderConfig } from "./types"; import { CoreClientConfig, Envs, - InterfaceImplementations, IUriPackage, IUriRedirect, IUriWrapper, Uri, + InterfaceImpls, } from "@polywrap/core-js"; import { PackageToWrapperCacheResolver, @@ -67,19 +67,16 @@ export class ClientConfigBuilder extends BaseClientConfigBuilder { return this._config.envs; } - private buildInterfaces(): InterfaceImplementations[] { - const interfaces: InterfaceImplementations[] = []; - - for (const [interfaceUri, implementations] of Object.entries( - this._config.interfaces - )) { - if (implementations.size === 0) continue; - interfaces.push({ - interface: Uri.from(interfaceUri), - implementations: Array.from(implementations).map((uri) => - Uri.from(uri) - ), - }); + private buildInterfaces(): InterfaceImpls { + const interfaces: Record = {}; + + for (const iface in this._config.interfaces) { + if (this._config.interfaces[iface].size > 0) { + // Sanitize uri + const uri = Uri.from(iface).uri; + + interfaces[uri] = [...this._config.interfaces[iface]]; + } } return interfaces; diff --git a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts index 1d71b3fef8..f7ba6ba4d0 100644 --- a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts +++ b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts @@ -80,7 +80,7 @@ describe("Client config builder", () => { const clientConfig = new ClientConfigBuilder().build(); expect(clientConfig.envs).toStrictEqual({}); - expect(clientConfig.interfaces).toStrictEqual([]); + expect(clientConfig.interfaces).toStrictEqual({}); }); it("should succesfully add config object and build", () => { @@ -98,12 +98,14 @@ describe("Client config builder", () => { expect(clientConfig).toBeTruthy(); expect(clientConfig.envs).toStrictEqual(testEnvs); - expect(clientConfig.interfaces).toStrictEqual( - Object.entries(testInterfaces).map(([uri, interfaces]) => ({ - interface: Uri.from(uri), - implementations: Array.from(interfaces).map(Uri.from), - })) - ); + expect(clientConfig.interfaces).toStrictEqual({ + "wrap://ens/test-interface-1.polywrap.eth": [ + "wrap://ens/test1.polywrap.eth", + ], + "wrap://ens/test-interface-2.polywrap.eth": [ + "wrap://ens/test2.polywrap.eth", + ], + }); expect(builderConfig).toEqual({ ...emptyBuilderConfig, @@ -133,12 +135,14 @@ describe("Client config builder", () => { ...testEnv1, ...testEnv2, }); - expect(clientConfig.interfaces).toStrictEqual( - Object.entries(testInterfaces).map(([uri, interfaces]) => ({ - interface: Uri.from(uri), - implementations: Array.from(interfaces).map(Uri.from), - })) - ); + expect(clientConfig.interfaces).toStrictEqual({ + "wrap://ens/test-interface-1.polywrap.eth": [ + "wrap://ens/test1.polywrap.eth", + ], + "wrap://ens/test-interface-2.polywrap.eth": [ + "wrap://ens/test2.polywrap.eth", + ], + }); expect(clientConfig.resolver).toBeTruthy(); @@ -288,13 +292,12 @@ describe("Client config builder", () => { .addInterfaceImplementation(interfaceUri, implUri) .build(); - if (!config.interfaces || config.interfaces.length !== 1) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces[0]).toStrictEqual({ - interface: Uri.from(interfaceUri), - implementations: [Uri.from(implUri)], + expect(config.interfaces).toStrictEqual({ + [interfaceUri]: [implUri], }); }); @@ -308,19 +311,13 @@ describe("Client config builder", () => { .addInterfaceImplementation(interfaceUri, implUri2) .build(); - if (!config.interfaces || config.interfaces.length !== 1) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces[0].interface).toStrictEqual( - Uri.from(interfaceUri) - ); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri1) - ); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri2) - ); + expect(config.interfaces).toStrictEqual({ + [interfaceUri]: [implUri1, implUri2], + }); }); it("should add different implementations for different interfaces", () => { @@ -338,26 +335,14 @@ describe("Client config builder", () => { .addInterfaceImplementation(interfaceUri2, implUri4) .build(); - if (!config.interfaces || config.interfaces.length !== 2) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 2) { fail(["Expected 2 interfaces, received:", config.interfaces]); } - const interface1 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri1 - ); - const interface2 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri2 - ); - - expect(interface1).toBeDefined(); - expect(interface1?.implementations).toHaveLength(2); - expect(interface1?.implementations).toContainEqual(Uri.from(implUri1)); - expect(interface1?.implementations).toContainEqual(Uri.from(implUri3)); - - expect(interface2).toBeDefined(); - expect(interface2?.implementations).toHaveLength(2); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri2)); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri4)); + expect(config.interfaces).toStrictEqual({ + [interfaceUri1]: [implUri1, implUri3], + [interfaceUri2]: [implUri2, implUri4], + }); }); it("should add multiple implementations for a non-existent interface", () => { @@ -369,20 +354,13 @@ describe("Client config builder", () => { .addInterfaceImplementations(interfaceUri, [implUri1, implUri2]) .build(); - if (!config.interfaces || config.interfaces.length !== 1) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces[0].interface).toStrictEqual( - Uri.from(interfaceUri) - ); - expect(config.interfaces[0].implementations).toHaveLength(2); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri1) - ); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri2) - ); + expect(config.interfaces).toStrictEqual({ + [interfaceUri]: [implUri1, implUri2], + }); }); it("should add multiple implementations for an existing interface", () => { @@ -396,23 +374,13 @@ describe("Client config builder", () => { .addInterfaceImplementations(interfaceUri, [implUri2, implUri3]) .build(); - if (!config.interfaces || config.interfaces.length !== 1) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces[0].interface).toStrictEqual( - Uri.from(interfaceUri) - ); - expect(config.interfaces[0].implementations).toHaveLength(3); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri1) - ); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri2) - ); - expect(config.interfaces[0].implementations).toContainEqual( - Uri.from(implUri3) - ); + expect(config.interfaces).toStrictEqual({ + [interfaceUri]: [implUri1, implUri2, implUri3], + }); }); it("should add multiple different implementations for different interfaces", () => { @@ -432,28 +400,14 @@ describe("Client config builder", () => { .addInterfaceImplementations(interfaceUri2, [implUri4, implUri6]) .build(); - if (!config.interfaces || config.interfaces.length !== 2) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 2) { fail(["Expected 2 interfaces, received:", config.interfaces]); } - const interface1 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri1 - ); - const interface2 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri2 - ); - - expect(interface1).toBeDefined(); - expect(interface1?.implementations).toHaveLength(3); - expect(interface1?.implementations).toContainEqual(Uri.from(implUri1)); - expect(interface1?.implementations).toContainEqual(Uri.from(implUri3)); - expect(interface1?.implementations).toContainEqual(Uri.from(implUri5)); - - expect(interface2).toBeDefined(); - expect(interface2?.implementations).toHaveLength(3); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri2)); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri4)); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri6)); + expect(config.interfaces).toStrictEqual({ + [interfaceUri1]: [implUri1, implUri3, implUri5], + [interfaceUri2]: [implUri2, implUri4, implUri6], + }); }); it("should remove an interface implementation", () => { @@ -468,25 +422,14 @@ describe("Client config builder", () => { .removeInterfaceImplementation(interfaceUri1, implUri2) .build(); - if (!config.interfaces || config.interfaces.length !== 2) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 2) { fail(["Expected 2 interfaces, received:", config.interfaces]); } - const interface1 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri1 - ); - const interface2 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri2 - ); - - expect(interface1).toBeDefined(); - expect(interface1?.implementations).toHaveLength(1); - expect(interface1?.implementations).toContainEqual(Uri.from(implUri1)); - - expect(interface2).toBeDefined(); - expect(interface2?.implementations).toHaveLength(2); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri1)); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri2)); + expect(config.interfaces).toStrictEqual({ + [interfaceUri1]: [implUri1], + [interfaceUri2]: [implUri1, implUri2], + }); }); it("should completely remove an interface if there are no implementations left", () => { @@ -502,23 +445,13 @@ describe("Client config builder", () => { .removeInterfaceImplementation(interfaceUri1, implUri2) .build(); - if (!config.interfaces || config.interfaces.length !== 1) { + if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - const interface1 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri1 - ); - const interface2 = config.interfaces.find( - (x) => x.interface.uri === interfaceUri2 - ); - - expect(interface1).toBeUndefined(); - - expect(interface2).toBeDefined(); - expect(interface2?.implementations).toHaveLength(2); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri1)); - expect(interface2?.implementations).toContainEqual(Uri.from(implUri2)); + expect(config.interfaces).toStrictEqual({ + [interfaceUri2]: [implUri1, implUri2], + }); }); it("should add an uri redirect", () => { diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index f59ca659a2..ffbb4f1531 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -6,7 +6,7 @@ import { Envs, GetFileOptions, GetImplementationsOptions, - InterfaceImplementations, + InterfaceImpls, InvokeResult, IUriResolutionContext, IUriResolver, @@ -61,7 +61,7 @@ export class PolywrapClient extends PolywrapCoreClient { } @Tracer.traceMethod("PolywrapClient: getInterfaces") - public getInterfaces(): readonly InterfaceImplementations[] | undefined { + public getInterfaces(): InterfaceImpls | undefined { return super.getInterfaces(); } diff --git a/packages/js/core-client/README.md b/packages/js/core-client/README.md index bf6bc85c15..d57b11a133 100644 --- a/packages/js/core-client/README.md +++ b/packages/js/core-client/README.md @@ -75,7 +75,7 @@ Invoke a wrapper. * @returns an array of interfaces and their registered implementations */ public getInterfaces(): - | readonly InterfaceImplementations[] + | InterfaceImpls | undefined ``` diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index 6ea2d561ff..afc95a5ee9 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -3,7 +3,6 @@ import { CoreClient, GetFileOptions, GetImplementationsOptions, - InterfaceImplementations, InvokeOptions, InvokerOptions, Uri, @@ -21,6 +20,7 @@ import { WrapErrorCode, Envs, WrapperEnv, + InterfaceImpls, } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { @@ -55,7 +55,7 @@ export class PolywrapCoreClient implements CoreClient { * @returns an array of interfaces and their registered implementations */ public getInterfaces(): - | readonly InterfaceImplementations[] + | InterfaceImpls | undefined /* $ */ { return this._config.interfaces; } @@ -162,7 +162,7 @@ export class PolywrapCoreClient implements CoreClient { const getImplResult = await getImplementations( Uri.from(uri), - this.getInterfaces() ?? [], + this.getInterfaces() ?? {}, applyResolution ? this : undefined, applyResolution ? options.resolutionContext : undefined ); diff --git a/packages/js/core/README.md b/packages/js/core/README.md index beb93eada5..3cdffe38d2 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -13,7 +13,7 @@ A TypeScript / JavaScript implementation of the WRAP standard, including all fun /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { /** register interface implementations */ - readonly interfaces?: Readonly; + readonly interfaces?: InterfaceImpls; /** set environmental variables for a wrapper */ readonly envs?: Envs; @@ -63,7 +63,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of interfaces and their registered implementations */ - getInterfaces(): readonly InterfaceImplementations[] | undefined; + getInterfaces(): InterfaceImpls | undefined; /** * returns all env registrations from the configuration used to instantiate the client diff --git a/packages/js/core/src/__tests__/get-implementations.spec.ts b/packages/js/core/src/__tests__/get-implementations.spec.ts index 5d2ba04858..546e1690e2 100644 --- a/packages/js/core/src/__tests__/get-implementations.spec.ts +++ b/packages/js/core/src/__tests__/get-implementations.spec.ts @@ -4,12 +4,16 @@ import { IUriRedirect, UriPackageOrWrapper, } from "../"; -import { CoreClient, InterfaceImplementations } from "../types"; +import { CoreClient, InterfaceImplementations, InterfaceImpls } from "../types"; import { Result, ResultOk } from "@polywrap/result"; const getClient = (redirects: IUriRedirect[]): CoreClient => { return { - tryResolveUri: async ( { uri }: { uri: Uri }): Promise> => { + tryResolveUri: async ({ + uri, + }: { + uri: Uri; + }): Promise> => { while (true) { const redirect = redirects.find((x) => uri.uri === x.from.uri); if (redirect) { @@ -26,7 +30,6 @@ const getClient = (redirects: IUriRedirect[]): CoreClient => { }; describe("getImplementations", () => { - it("works with complex redirects", async () => { const interface1Uri = "wrap://ens/some-interface1.eth"; const interface2Uri = "wrap://ens/some-interface2.eth"; @@ -39,71 +42,59 @@ describe("getImplementations", () => { const redirects: IUriRedirect[] = [ { from: new Uri(interface1Uri), - to: new Uri(interface2Uri) + to: new Uri(interface2Uri), }, { from: new Uri(implementation1Uri), - to: new Uri(implementation2Uri) + to: new Uri(implementation2Uri), }, { from: new Uri(implementation2Uri), - to: new Uri(implementation3Uri) - } - ]; - - const interfaces: InterfaceImplementations[] = [ - { - interface: new Uri(interface1Uri), - implementations: [ - new Uri(implementation1Uri), - new Uri(implementation2Uri) - ] + to: new Uri(implementation3Uri), }, - { - interface: new Uri(interface2Uri), - implementations: [ - new Uri(implementation3Uri) - ] - }, - { - interface: new Uri(interface3Uri), - implementations: [ - new Uri(implementation3Uri) - ] - } ]; + const interfaces: InterfaceImpls = { + [interface1Uri]: [implementation1Uri, implementation2Uri], + [interface2Uri]: [implementation3Uri], + [interface3Uri]: [implementation3Uri], + }; + const getImplementationsResult1 = await getImplementations( - new Uri(interface1Uri), - interfaces, - getClient(redirects) - ); + new Uri(interface1Uri), + interfaces, + getClient(redirects) + ); const getImplementationsResult2 = await getImplementations( - new Uri(interface2Uri), - interfaces, - getClient(redirects) - ); + new Uri(interface2Uri), + interfaces, + getClient(redirects) + ); const getImplementationsResult3 = await getImplementations( - new Uri(interface3Uri), - interfaces, - getClient(redirects) - ); - - expect(getImplementationsResult1).toEqual(ResultOk([ - new Uri(implementation1Uri), - new Uri(implementation2Uri), - new Uri(implementation3Uri) - ])); - - expect(getImplementationsResult2).toEqual(ResultOk([ - new Uri(implementation1Uri), - new Uri(implementation2Uri), - new Uri(implementation3Uri) - ])); - - expect(getImplementationsResult3).toEqual(ResultOk([ - new Uri(implementation3Uri) - ])); + new Uri(interface3Uri), + interfaces, + getClient(redirects) + ); + + expect(getImplementationsResult1).toEqual( + ResultOk([ + new Uri(implementation1Uri), + new Uri(implementation2Uri), + new Uri(implementation3Uri), + ]) + ); + + expect(getImplementationsResult2).toEqual( + ResultOk([ + new Uri(implementation1Uri), + new Uri(implementation2Uri), + new Uri(implementation3Uri), + ]) + ); + + expect(getImplementationsResult3).toEqual( + ResultOk([new Uri(implementation3Uri)]) + ); }); it("interface implementations are not redirected", async () => { @@ -115,27 +106,20 @@ describe("getImplementations", () => { const redirects: IUriRedirect[] = [ { from: new Uri(implementation1Uri), - to: new Uri(implementation2Uri) - } + to: new Uri(implementation2Uri), + }, ]; - const interfaces: InterfaceImplementations[] = [ - { - interface: new Uri(interface1Uri), - implementations: [ - new Uri(implementation1Uri) - ] - } - ]; + const interfaces: InterfaceImpls = { + [interface1Uri]: [implementation1Uri], + }; const result = await getImplementations( - new Uri(interface1Uri), - interfaces, - getClient(redirects) - ); + new Uri(interface1Uri), + interfaces, + getClient(redirects) + ); - expect(result).toEqual(ResultOk([ - new Uri(implementation1Uri) - ])); + expect(result).toEqual(ResultOk([new Uri(implementation1Uri)])); }); }); diff --git a/packages/js/core/src/algorithms/get-implementations.ts b/packages/js/core/src/algorithms/get-implementations.ts index 7396ef358c..bc81575199 100644 --- a/packages/js/core/src/algorithms/get-implementations.ts +++ b/packages/js/core/src/algorithms/get-implementations.ts @@ -1,9 +1,9 @@ import { Uri, - InterfaceImplementations, CoreClient, WrapError, WrapErrorCode, + InterfaceImpls, } from "../types"; import { IUriResolutionContext } from "../uri-resolution"; import { applyResolution } from "./applyResolution"; @@ -12,7 +12,7 @@ import { Result, ResultErr, ResultOk } from "@polywrap/result"; export const getImplementations = async ( wrapperInterfaceUri: Uri, - interfaces: readonly InterfaceImplementations[], + interfaces: InterfaceImpls, client?: CoreClient, resolutionContext?: IUriResolutionContext ): Promise> => { @@ -26,20 +26,21 @@ export const getImplementations = async ( }; const addAllImplementationsFromImplementationsArray = async ( - implementationsArray: readonly InterfaceImplementations[], + impls: InterfaceImpls, wrapperInterfaceUri: Uri ): Promise> => { - for (const interfaceImplementations of implementationsArray) { + for (const impl in impls) { let fullyResolvedUri: Uri; + let interfaceUri = Uri.from(impl); if (client) { const redirectsResult = await applyResolution( - interfaceImplementations.interface, + interfaceUri, client, resolutionContext ); if (!redirectsResult.ok) { const error = new WrapError("Failed to resolve redirects", { - uri: interfaceImplementations.interface.uri, + uri: impl, code: WrapErrorCode.CLIENT_GET_IMPLEMENTATIONS_ERROR, cause: redirectsResult.error, }); @@ -47,12 +48,12 @@ export const getImplementations = async ( } fullyResolvedUri = redirectsResult.value; } else { - fullyResolvedUri = interfaceImplementations.interface; + fullyResolvedUri = interfaceUri; } if (Uri.equals(fullyResolvedUri, wrapperInterfaceUri)) { - for (const implementation of interfaceImplementations.implementations) { - addUniqueResult(implementation); + for (const implementation of impls[impl]) { + addUniqueResult(Uri.from(implementation)); } } } diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index 0cc41023e9..226a1cdae6 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -1,4 +1,11 @@ -import { Invoker, Uri, InterfaceImplementations, WrapError, Envs, WrapperEnv } from "."; +import { + Invoker, + Uri, + WrapError, + Envs, + WrapperEnv, + InterfaceImpls, +} from "."; import { IUriResolutionContext, IUriResolver } from "../uri-resolution"; import { UriResolverHandler } from "./UriResolver"; @@ -10,7 +17,7 @@ import { Result } from "@polywrap/result"; /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { /** register interface implementations */ - readonly interfaces?: Readonly; + readonly interfaces?: InterfaceImpls; /** set environmental variables for a wrapper */ readonly envs?: Envs; @@ -60,7 +67,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of interfaces and their registered implementations */ - getInterfaces(): readonly InterfaceImplementations[] | undefined; + getInterfaces(): InterfaceImpls | undefined; /** * returns all env registrations from the configuration used to instantiate the client diff --git a/packages/js/core/src/types/InterfaceImplementations.ts b/packages/js/core/src/types/InterfaceImplementations.ts index fd8ccc85a0..6558a238b6 100644 --- a/packages/js/core/src/types/InterfaceImplementations.ts +++ b/packages/js/core/src/types/InterfaceImplementations.ts @@ -12,3 +12,7 @@ export interface InterfaceImplementations { } // $end + +export interface InterfaceImpls { + readonly [k: string]: readonly string[]; +} diff --git a/packages/js/uri-resolver-extensions/README.md b/packages/js/uri-resolver-extensions/README.md index 6446e9d06c..16407cc9df 100644 --- a/packages/js/uri-resolver-extensions/README.md +++ b/packages/js/uri-resolver-extensions/README.md @@ -21,29 +21,22 @@ Otherwise, you must also add the `ExtendableUriResolver` to your resolver. ```ts const clientConfig: CoreClientConfig = { - interfaces: [ - { - interface: Uri.from("wrap://ens/uri-resolver.core.polywrap.eth"), - implementations: [ - Uri.from("wrap://ens/fs-resolver.polywrap.eth"), - Uri.from("wrap://ens/ipfs-resolver.polywrap.eth"), - Uri.from("wrap://ens/ens-resolver.polywrap.eth") - ] - } - ], + interfaces: { + "wrap://ens/uri-resolver.core.polywrap.eth": [ + "wrap://ens/fs-resolver.polywrap.eth", + "wrap://ens/ipfs-resolver.polywrap.eth", + "wrap://ens/ens-resolver.polywrap.eth", + ], + }, resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ - StaticResolver.from([ - ...redirects, - ...wrappers, - ...packages, - ]), + StaticResolver.from([...redirects, ...wrappers, ...packages]), new ExtendableUriResolver(), ], new WrapperCache() ) - ) + ), }; ``` diff --git a/packages/js/uri-resolver-extensions/examples/quickstart.ts b/packages/js/uri-resolver-extensions/examples/quickstart.ts index fbd9d7b64c..9862a81989 100644 --- a/packages/js/uri-resolver-extensions/examples/quickstart.ts +++ b/packages/js/uri-resolver-extensions/examples/quickstart.ts @@ -1,4 +1,4 @@ -import { CoreClientConfig, Uri } from "@polywrap/core-js"; +import { CoreClientConfig } from "@polywrap/core-js"; import { PackageResolver, PackageToWrapperCacheResolver, @@ -16,29 +16,22 @@ export function example(): CoreClientConfig { const packages: PackageResolver[] = []; // $start: quickstart-example const clientConfig: CoreClientConfig = { - interfaces: [ - { - interface: Uri.from("wrap://ens/uri-resolver.core.polywrap.eth"), - implementations: [ - Uri.from("wrap://ens/fs-resolver.polywrap.eth"), - Uri.from("wrap://ens/ipfs-resolver.polywrap.eth"), - Uri.from("wrap://ens/ens-resolver.polywrap.eth") - ] - } - ], + interfaces: { + "wrap://ens/uri-resolver.core.polywrap.eth": [ + "wrap://ens/fs-resolver.polywrap.eth", + "wrap://ens/ipfs-resolver.polywrap.eth", + "wrap://ens/ens-resolver.polywrap.eth", + ], + }, resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ - StaticResolver.from([ - ...redirects, - ...wrappers, - ...packages, - ]), + StaticResolver.from([...redirects, ...wrappers, ...packages]), new ExtendableUriResolver(), ], new WrapperCache() ) - ) + ), }; // $end From 833102c737f22546479916d1d4612215b17e68a0 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 23 Feb 2023 14:53:01 +0100 Subject: [PATCH 04/16] fix up plugin tests --- .../src/__tests__/helpers/getClient.ts | 29 +++++++------------ .../src/__tests__/helpers/getClient.ts | 11 ++++--- .../src/__tests__/helpers/getClient.ts | 11 ++++--- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts b/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts index 1a3f7e7f68..60a7dd31bc 100644 --- a/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts +++ b/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/helpers/getClient.ts @@ -31,24 +31,17 @@ export const getClient = () => { retries: { tryResolveUri: 1, getFile: 1 }, }, }, - interfaces: [ - { - interface: ExtendableUriResolver.extInterfaceUri, - implementations: [ - Uri.from(defaultPackages.ipfsResolver), - Uri.from(defaultPackages.ensResolver), - Uri.from(defaultWrappers.ensTextRecordResolver), - ], - }, - { - interface: Uri.from(defaultInterfaces.ipfsHttpClient), - implementations: [Uri.from(defaultInterfaces.ipfsHttpClient)], - }, - { - interface: Uri.from(defaultInterfaces.ethereumProvider), - implementations: [Uri.from(defaultInterfaces.ethereumProvider)], - }, - ], + interfaces: { + [ExtendableUriResolver.extInterfaceUri.uri]: [ + defaultPackages.ipfsResolver, + defaultPackages.ensResolver, + defaultWrappers.ensTextRecordResolver, + ], + [defaultInterfaces.ipfsHttpClient]: [defaultInterfaces.ipfsHttpClient], + [defaultInterfaces.ethereumProvider]: [ + defaultInterfaces.ethereumProvider, + ], + }, resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ diff --git a/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/helpers/getClient.ts b/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/helpers/getClient.ts index d1d5641902..c094d9c985 100644 --- a/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/helpers/getClient.ts +++ b/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/helpers/getClient.ts @@ -14,12 +14,11 @@ import { export const getClient = () => { return new PolywrapClient({ - interfaces: [ - { - interface: ExtendableUriResolver.extInterfaceUri, - implementations: [Uri.from(defaultPackages.fileSystemResolver)], - }, - ], + interfaces: { + [ExtendableUriResolver.extInterfaceUri.uri]: [ + defaultPackages.fileSystemResolver, + ], + }, resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ diff --git a/packages/js/plugins/uri-resolvers/http-resolver/src/__tests__/helpers/getClient.ts b/packages/js/plugins/uri-resolvers/http-resolver/src/__tests__/helpers/getClient.ts index e5f9b623a3..3a8fcb16cd 100644 --- a/packages/js/plugins/uri-resolvers/http-resolver/src/__tests__/helpers/getClient.ts +++ b/packages/js/plugins/uri-resolvers/http-resolver/src/__tests__/helpers/getClient.ts @@ -14,12 +14,11 @@ import { export const getClient = () => { return new PolywrapClient({ - interfaces: [ - { - interface: ExtendableUriResolver.extInterfaceUri, - implementations: [Uri.from(defaultPackages.httpResolver)], - }, - ], + interfaces: { + [ExtendableUriResolver.extInterfaceUri.uri]: [ + defaultPackages.httpResolver, + ], + }, resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ From 458951709f1e54ef9a1e335f89ab853a15a5469e Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 23 Feb 2023 15:20:08 +0100 Subject: [PATCH 05/16] fix core tests & lint --- .../src/BaseClientConfigBuilder.ts | 12 ++++- .../client/src/__tests__/core/sanity.spec.ts | 54 ++++++------------- .../js/core-client/src/PolywrapCoreClient.ts | 4 +- .../src/algorithms/get-implementations.ts | 2 +- packages/js/core/src/types/CoreClient.ts | 9 +--- packages/js/core/src/types/Env.ts | 2 +- 6 files changed, 31 insertions(+), 52 deletions(-) diff --git a/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts b/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts index 8f27b83ed9..1a16551900 100644 --- a/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts @@ -1,6 +1,11 @@ import { BuilderConfig, IClientConfigBuilder } from "./types"; -import { CoreClientConfig, Wrapper, IWrapPackage, Uri } from "@polywrap/core-js"; +import { + CoreClientConfig, + Wrapper, + IWrapPackage, + Uri, +} from "@polywrap/core-js"; import { UriResolverLike } from "@polywrap/uri-resolvers-js"; export abstract class BaseClientConfigBuilder implements IClientConfigBuilder { @@ -94,7 +99,10 @@ export abstract class BaseClientConfigBuilder implements IClientConfigBuilder { addEnv(uri: string, env: Record): IClientConfigBuilder { const sanitizedUri = Uri.from(uri).uri; - this._config.envs[sanitizedUri] = { ...this._config.envs[sanitizedUri], ...env }; + this._config.envs[sanitizedUri] = { + ...this._config.envs[sanitizedUri], + ...env, + }; return this; } diff --git a/packages/js/client/src/__tests__/core/sanity.spec.ts b/packages/js/client/src/__tests__/core/sanity.spec.ts index 4b3bb863ef..5ead320f7e 100644 --- a/packages/js/client/src/__tests__/core/sanity.spec.ts +++ b/packages/js/client/src/__tests__/core/sanity.spec.ts @@ -18,43 +18,23 @@ jest.setTimeout(200000); describe("sanity", () => { test("default client config", () => { const client = new PolywrapClient(); - - expect(client.getInterfaces()).toStrictEqual([ - { - interface: ExtendableUriResolver.extInterfaceUri, - implementations: [ - new Uri(defaultPackages.ipfsResolver), - new Uri(defaultPackages.ensResolver), - new Uri(defaultPackages.fileSystemResolver), - new Uri(defaultPackages.httpResolver), - new Uri(defaultWrappers.ensTextRecordResolver), - ], - }, - { - interface: new Uri(defaultInterfaces.logger), - implementations: [new Uri(defaultInterfaces.logger)], - }, - { - interface: new Uri(defaultInterfaces.concurrent), - implementations: [new Uri(defaultInterfaces.concurrent)], - }, - { - interface: new Uri(defaultInterfaces.ipfsHttpClient), - implementations: [new Uri(defaultInterfaces.ipfsHttpClient)], - }, - { - interface: new Uri(defaultInterfaces.fileSystem), - implementations: [new Uri(defaultInterfaces.fileSystem)], - }, - { - interface: new Uri(defaultInterfaces.http), - implementations: [new Uri(defaultInterfaces.http)], - }, - { - interface: new Uri(defaultInterfaces.ethereumProvider), - implementations: [new Uri(defaultInterfaces.ethereumProvider)], - }, - ]); + expect(client.getInterfaces()).toStrictEqual({ + [ExtendableUriResolver.extInterfaceUri.uri]: [ + defaultPackages.ipfsResolver, + defaultPackages.ensResolver, + defaultPackages.fileSystemResolver, + defaultPackages.httpResolver, + defaultWrappers.ensTextRecordResolver, + ], + [defaultInterfaces.logger]: [defaultInterfaces.logger], + [defaultInterfaces.concurrent]: [defaultInterfaces.concurrent], + [defaultInterfaces.ipfsHttpClient]: [defaultInterfaces.ipfsHttpClient], + [defaultInterfaces.fileSystem]: [defaultInterfaces.fileSystem], + [defaultInterfaces.http]: [defaultInterfaces.http], + [defaultInterfaces.ethereumProvider]: [ + defaultInterfaces.ethereumProvider, + ], + }); }); test("validate requested uri is available", async () => { diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index afc95a5ee9..f74ad5a8b1 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -54,9 +54,7 @@ export class PolywrapCoreClient implements CoreClient { * * @returns an array of interfaces and their registered implementations */ - public getInterfaces(): - | InterfaceImpls - | undefined /* $ */ { + public getInterfaces(): InterfaceImpls | undefined /* $ */ { return this._config.interfaces; } diff --git a/packages/js/core/src/algorithms/get-implementations.ts b/packages/js/core/src/algorithms/get-implementations.ts index bc81575199..c98630fcae 100644 --- a/packages/js/core/src/algorithms/get-implementations.ts +++ b/packages/js/core/src/algorithms/get-implementations.ts @@ -31,7 +31,7 @@ export const getImplementations = async ( ): Promise> => { for (const impl in impls) { let fullyResolvedUri: Uri; - let interfaceUri = Uri.from(impl); + const interfaceUri = Uri.from(impl); if (client) { const redirectsResult = await applyResolution( interfaceUri, diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index 226a1cdae6..b37780a978 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -1,11 +1,4 @@ -import { - Invoker, - Uri, - WrapError, - Envs, - WrapperEnv, - InterfaceImpls, -} from "."; +import { Invoker, Uri, WrapError, Envs, WrapperEnv, InterfaceImpls } from "."; import { IUriResolutionContext, IUriResolver } from "../uri-resolution"; import { UriResolverHandler } from "./UriResolver"; diff --git a/packages/js/core/src/types/Env.ts b/packages/js/core/src/types/Env.ts index 733dfeff8a..5f45fc462e 100644 --- a/packages/js/core/src/types/Env.ts +++ b/packages/js/core/src/types/Env.ts @@ -18,4 +18,4 @@ export interface Envs { export interface WrapperEnv { readonly [k: string]: unknown; -} \ No newline at end of file +} From 0a4c409229d5b5d6a58317775e7367a16e154b23 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 23 Feb 2023 23:31:03 +0100 Subject: [PATCH 06/16] remove commented/old code --- packages/js/core-client/README.md | 4 +--- packages/js/core/README.md | 23 ++++++++----------- packages/js/core/src/types/Env.ts | 16 +++---------- .../src/types/InterfaceImplementations.ts | 13 +---------- 4 files changed, 14 insertions(+), 42 deletions(-) diff --git a/packages/js/core-client/README.md b/packages/js/core-client/README.md index d57b11a133..58f244de48 100644 --- a/packages/js/core-client/README.md +++ b/packages/js/core-client/README.md @@ -74,9 +74,7 @@ Invoke a wrapper. * * @returns an array of interfaces and their registered implementations */ - public getInterfaces(): - | InterfaceImpls - | undefined + public getInterfaces(): InterfaceImpls | undefined ``` ### getEnvs diff --git a/packages/js/core/README.md b/packages/js/core/README.md index 3cdffe38d2..5455886069 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -127,14 +127,14 @@ export interface CoreClient extends Invoker, UriResolverHandler { ```ts -// /** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ -// export interface Env { -// /** Uri of wrapper */ -// uri: Uri; +/** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ +export interface Envs { + readonly [k: string]: WrapperEnv; +} -// /** Env variables used by the module */ -// env: Record; -// } +export interface WrapperEnv { + readonly [k: string]: unknown; +} ``` ### InterfaceImplementations @@ -142,14 +142,9 @@ export interface CoreClient extends Invoker, UriResolverHandler { ```ts /** An interface and a list of wrappers that implement the interface */ -export interface InterfaceImplementations { - /** Uri of interface */ - interface: Uri; - - /** Uris of implementations */ - implementations: Uri[]; +export interface InterfaceImpls { + readonly [k: string]: readonly string[]; } - ``` ### Invoke diff --git a/packages/js/core/src/types/Env.ts b/packages/js/core/src/types/Env.ts index 5f45fc462e..898881d2f5 100644 --- a/packages/js/core/src/types/Env.ts +++ b/packages/js/core/src/types/Env.ts @@ -1,17 +1,6 @@ -// import { Uri } from "."; - -// // $start: Env.ts - -// /** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ -// export interface Env { -// /** Uri of wrapper */ -// uri: Uri; - -// /** Env variables used by the module */ -// env: Record; -// } -// // $end +// $start: Env.ts +/** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ export interface Envs { readonly [k: string]: WrapperEnv; } @@ -19,3 +8,4 @@ export interface Envs { export interface WrapperEnv { readonly [k: string]: unknown; } +// $end \ No newline at end of file diff --git a/packages/js/core/src/types/InterfaceImplementations.ts b/packages/js/core/src/types/InterfaceImplementations.ts index 6558a238b6..b5b3bda5f7 100644 --- a/packages/js/core/src/types/InterfaceImplementations.ts +++ b/packages/js/core/src/types/InterfaceImplementations.ts @@ -1,18 +1,7 @@ -import { Uri } from "."; - // $start: InterfaceImplementations.ts /** An interface and a list of wrappers that implement the interface */ -export interface InterfaceImplementations { - /** Uri of interface */ - interface: Uri; - - /** Uris of implementations */ - implementations: Uri[]; -} - -// $end - export interface InterfaceImpls { readonly [k: string]: readonly string[]; } +// $end From 6fa5c1e1d934596f7f5487a754f949c2ba7975f1 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 23 Feb 2023 23:38:25 +0100 Subject: [PATCH 07/16] chore: rename types & lint --- .../src/ClientConfigBuilder.ts | 4 ++-- packages/js/client/src/PolywrapClient.ts | 4 ++-- packages/js/core-client/README.md | 2 +- packages/js/core-client/src/PolywrapCoreClient.ts | 4 ++-- packages/js/core/README.md | 6 +++--- .../core/src/__tests__/get-implementations.spec.ts | 6 +++--- .../js/core/src/algorithms/get-implementations.ts | 6 +++--- packages/js/core/src/types/CoreClient.ts | 13 ++++++++++--- packages/js/core/src/types/Env.ts | 2 +- .../js/core/src/types/InterfaceImplementations.ts | 2 +- 10 files changed, 28 insertions(+), 21 deletions(-) diff --git a/packages/js/client-config-builder/src/ClientConfigBuilder.ts b/packages/js/client-config-builder/src/ClientConfigBuilder.ts index ffd3916e3f..82458bceee 100644 --- a/packages/js/client-config-builder/src/ClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/ClientConfigBuilder.ts @@ -9,7 +9,7 @@ import { IUriRedirect, IUriWrapper, Uri, - InterfaceImpls, + InterfaceImplementations, } from "@polywrap/core-js"; import { RecursiveResolver, @@ -70,7 +70,7 @@ export class ClientConfigBuilder extends BaseClientConfigBuilder { return this._config.envs; } - private buildInterfaces(): InterfaceImpls { + private buildInterfaces(): InterfaceImplementations { const interfaces: Record = {}; for (const iface in this._config.interfaces) { diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index ffbb4f1531..74802603be 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -6,7 +6,7 @@ import { Envs, GetFileOptions, GetImplementationsOptions, - InterfaceImpls, + InterfaceImplementations, InvokeResult, IUriResolutionContext, IUriResolver, @@ -61,7 +61,7 @@ export class PolywrapClient extends PolywrapCoreClient { } @Tracer.traceMethod("PolywrapClient: getInterfaces") - public getInterfaces(): InterfaceImpls | undefined { + public getInterfaces(): InterfaceImplementations | undefined { return super.getInterfaces(); } diff --git a/packages/js/core-client/README.md b/packages/js/core-client/README.md index 58f244de48..e79b2c0add 100644 --- a/packages/js/core-client/README.md +++ b/packages/js/core-client/README.md @@ -74,7 +74,7 @@ Invoke a wrapper. * * @returns an array of interfaces and their registered implementations */ - public getInterfaces(): InterfaceImpls | undefined + public getInterfaces(): InterfaceImplementations | undefined ``` ### getEnvs diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index f74ad5a8b1..859f2792b6 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -20,7 +20,7 @@ import { WrapErrorCode, Envs, WrapperEnv, - InterfaceImpls, + InterfaceImplementations, } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { @@ -54,7 +54,7 @@ export class PolywrapCoreClient implements CoreClient { * * @returns an array of interfaces and their registered implementations */ - public getInterfaces(): InterfaceImpls | undefined /* $ */ { + public getInterfaces(): InterfaceImplementations | undefined /* $ */ { return this._config.interfaces; } diff --git a/packages/js/core/README.md b/packages/js/core/README.md index 5455886069..d7406afa79 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -13,7 +13,7 @@ A TypeScript / JavaScript implementation of the WRAP standard, including all fun /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { /** register interface implementations */ - readonly interfaces?: InterfaceImpls; + readonly interfaces?: InterfaceImplementations; /** set environmental variables for a wrapper */ readonly envs?: Envs; @@ -63,7 +63,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of interfaces and their registered implementations */ - getInterfaces(): InterfaceImpls | undefined; + getInterfaces(): InterfaceImplementations | undefined; /** * returns all env registrations from the configuration used to instantiate the client @@ -142,7 +142,7 @@ export interface WrapperEnv { ```ts /** An interface and a list of wrappers that implement the interface */ -export interface InterfaceImpls { +export interface InterfaceImplementations { readonly [k: string]: readonly string[]; } ``` diff --git a/packages/js/core/src/__tests__/get-implementations.spec.ts b/packages/js/core/src/__tests__/get-implementations.spec.ts index 546e1690e2..ece280e4d6 100644 --- a/packages/js/core/src/__tests__/get-implementations.spec.ts +++ b/packages/js/core/src/__tests__/get-implementations.spec.ts @@ -4,7 +4,7 @@ import { IUriRedirect, UriPackageOrWrapper, } from "../"; -import { CoreClient, InterfaceImplementations, InterfaceImpls } from "../types"; +import { CoreClient, InterfaceImplementations } from "../types"; import { Result, ResultOk } from "@polywrap/result"; const getClient = (redirects: IUriRedirect[]): CoreClient => { @@ -54,7 +54,7 @@ describe("getImplementations", () => { }, ]; - const interfaces: InterfaceImpls = { + const interfaces: InterfaceImplementations = { [interface1Uri]: [implementation1Uri, implementation2Uri], [interface2Uri]: [implementation3Uri], [interface3Uri]: [implementation3Uri], @@ -110,7 +110,7 @@ describe("getImplementations", () => { }, ]; - const interfaces: InterfaceImpls = { + const interfaces: InterfaceImplementations = { [interface1Uri]: [implementation1Uri], }; diff --git a/packages/js/core/src/algorithms/get-implementations.ts b/packages/js/core/src/algorithms/get-implementations.ts index c98630fcae..43a523c968 100644 --- a/packages/js/core/src/algorithms/get-implementations.ts +++ b/packages/js/core/src/algorithms/get-implementations.ts @@ -3,7 +3,7 @@ import { CoreClient, WrapError, WrapErrorCode, - InterfaceImpls, + InterfaceImplementations, } from "../types"; import { IUriResolutionContext } from "../uri-resolution"; import { applyResolution } from "./applyResolution"; @@ -12,7 +12,7 @@ import { Result, ResultErr, ResultOk } from "@polywrap/result"; export const getImplementations = async ( wrapperInterfaceUri: Uri, - interfaces: InterfaceImpls, + interfaces: InterfaceImplementations, client?: CoreClient, resolutionContext?: IUriResolutionContext ): Promise> => { @@ -26,7 +26,7 @@ export const getImplementations = async ( }; const addAllImplementationsFromImplementationsArray = async ( - impls: InterfaceImpls, + impls: InterfaceImplementations, wrapperInterfaceUri: Uri ): Promise> => { for (const impl in impls) { diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index b37780a978..89195c54f0 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -1,4 +1,11 @@ -import { Invoker, Uri, WrapError, Envs, WrapperEnv, InterfaceImpls } from "."; +import { + Invoker, + Uri, + WrapError, + Envs, + WrapperEnv, + InterfaceImplementations, +} from "."; import { IUriResolutionContext, IUriResolver } from "../uri-resolution"; import { UriResolverHandler } from "./UriResolver"; @@ -10,7 +17,7 @@ import { Result } from "@polywrap/result"; /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { /** register interface implementations */ - readonly interfaces?: InterfaceImpls; + readonly interfaces?: InterfaceImplementations; /** set environmental variables for a wrapper */ readonly envs?: Envs; @@ -60,7 +67,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of interfaces and their registered implementations */ - getInterfaces(): InterfaceImpls | undefined; + getInterfaces(): InterfaceImplementations | undefined; /** * returns all env registrations from the configuration used to instantiate the client diff --git a/packages/js/core/src/types/Env.ts b/packages/js/core/src/types/Env.ts index 898881d2f5..f2cfd502bc 100644 --- a/packages/js/core/src/types/Env.ts +++ b/packages/js/core/src/types/Env.ts @@ -8,4 +8,4 @@ export interface Envs { export interface WrapperEnv { readonly [k: string]: unknown; } -// $end \ No newline at end of file +// $end diff --git a/packages/js/core/src/types/InterfaceImplementations.ts b/packages/js/core/src/types/InterfaceImplementations.ts index b5b3bda5f7..bae760a1fe 100644 --- a/packages/js/core/src/types/InterfaceImplementations.ts +++ b/packages/js/core/src/types/InterfaceImplementations.ts @@ -1,7 +1,7 @@ // $start: InterfaceImplementations.ts /** An interface and a list of wrappers that implement the interface */ -export interface InterfaceImpls { +export interface InterfaceImplementations { readonly [k: string]: readonly string[]; } // $end From 642057b8309dfe63c317cd1f6219ae533550917a Mon Sep 17 00:00:00 2001 From: Pileks Date: Tue, 7 Mar 2023 21:37:17 +0100 Subject: [PATCH 08/16] add urimap type --- packages/js/core/src/types/UriMap.ts | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 packages/js/core/src/types/UriMap.ts diff --git a/packages/js/core/src/types/UriMap.ts b/packages/js/core/src/types/UriMap.ts new file mode 100644 index 0000000000..df0bf8c2bc --- /dev/null +++ b/packages/js/core/src/types/UriMap.ts @@ -0,0 +1,78 @@ +import { Uri } from "./Uri"; + +export class UriMap implements Map { + private map: Map; + + /** + * + */ + constructor(entries?: readonly (readonly [Uri, TValue])[]) { + this.map = new Map(entries?.map((x) => [x[0].uri, x[1]])); + } + + clear(): void { + this.map.clear(); + } + + delete(key: Uri): boolean { + return this.map.delete(key.uri); + } + + forEach( + callbackfn: (value: TValue, key: Uri, map: Map) => void, + thisArg?: any + ): void { + this.map.forEach((v, k) => callbackfn(v, Uri.from(k), this), thisArg); + throw new Error("Method not implemented."); + } + + get(key: Uri): TValue | undefined { + return this.map.get(key.uri); + } + + has(key: Uri): boolean { + return this.map.has(key.uri); + } + + set(key: Uri, value: TValue): this { + this.map.set(key.uri, value); + + return this; + } + + get size(): number { + return this.map.size; + } + + [Symbol.toStringTag] = "UriMap"; + + *entries(): IterableIterator<[Uri, TValue]> { + for (const [k, v] of this.map.entries()) { + yield [Uri.from(k), v]; + } + } + + *keys(): IterableIterator { + for (const k of this.map.keys()) { + yield Uri.from(k); + } + } + + values(): IterableIterator { + return this.map.values(); + } + + [Symbol.iterator](): IterableIterator<[Uri, TValue]> { + return this.entries(); + } +} + +export interface ReadonlyUriMap extends ReadonlyMap {} + +function foo(bar: ReadonlyUriMap) { + bar.size; +} + +const a = new UriMap(); + +foo(new UriMap([[Uri.from(""), true]])); From 0e67c7ccae3d05b4e9574aa4b8092d1ac40eedb0 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 01:04:34 +0100 Subject: [PATCH 09/16] CoreClientConfig - refactor Envs into ReadonlyUriMap --- .../src/ClientConfigBuilder.ts | 14 +++-- .../__tests__/client-config-builder.spec.ts | 51 +++++++++++-------- packages/js/client/src/PolywrapClient.ts | 6 +-- packages/js/core-client/README.md | 4 +- .../js/core-client/src/PolywrapCoreClient.ts | 8 +-- packages/js/core/README.md | 6 +-- packages/js/core/src/types/CoreClient.ts | 8 +-- packages/js/core/src/types/UriMap.ts | 15 +----- packages/js/core/src/types/index.ts | 1 + 9 files changed, 61 insertions(+), 52 deletions(-) diff --git a/packages/js/client-config-builder/src/ClientConfigBuilder.ts b/packages/js/client-config-builder/src/ClientConfigBuilder.ts index c412bb8c5d..7aacbd77aa 100644 --- a/packages/js/client-config-builder/src/ClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/ClientConfigBuilder.ts @@ -4,12 +4,14 @@ import { BuildOptions, IClientConfigBuilder, BuilderConfig } from "./types"; import { CoreClientConfig, - Envs, IUriPackage, IUriRedirect, IUriWrapper, Uri, InterfaceImplementations, + WrapperEnv, + ReadonlyUriMap, + UriMap, } from "@polywrap/core-js"; import { RecursiveResolver, @@ -66,8 +68,14 @@ export class ClientConfigBuilder extends BaseClientConfigBuilder { return this._config; } - private buildEnvs(): Envs { - return this._config.envs; + private buildEnvs(): ReadonlyUriMap { + const envs = new UriMap(); + + for (const uri in this._config.envs) { + envs.set(Uri.from(uri), this._config.envs[uri]); + } + + return envs; } private buildInterfaces(): InterfaceImplementations { diff --git a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts index 22972f7a65..5b25b8c9b8 100644 --- a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts +++ b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts @@ -4,6 +4,8 @@ import { Uri, IUriResolver, UriPackageOrWrapper, + UriMap, + WrapperEnv, } from "@polywrap/core-js"; import { Result } from "@polywrap/result"; import { DefaultBundle } from "../bundles"; @@ -79,7 +81,7 @@ describe("Client config builder", () => { it("should build an empty partial config", () => { const clientConfig = new ClientConfigBuilder().build(); - expect(clientConfig.envs).toStrictEqual({}); + expect(clientConfig.envs).toStrictEqual(new UriMap()); expect(clientConfig.interfaces).toStrictEqual({}); }); @@ -97,7 +99,12 @@ describe("Client config builder", () => { const builderConfig = builder.config; expect(clientConfig).toBeTruthy(); - expect(clientConfig.envs).toStrictEqual(testEnvs); + expect(clientConfig.envs).toStrictEqual( + new UriMap([ + [Uri.from("wrap://ens/test.plugin.one"), { test: "value" }], + [Uri.from("wrap://ens/test.plugin.two"), { test: "value" }], + ]) + ); expect(clientConfig.interfaces).toStrictEqual({ "wrap://ens/test-interface-1.polywrap.eth": [ "wrap://ens/test1.polywrap.eth", @@ -131,10 +138,12 @@ describe("Client config builder", () => { const builderConfig = builder.config; expect(clientConfig).toBeTruthy(); - expect(clientConfig.envs).toStrictEqual({ - ...testEnv1, - ...testEnv2, - }); + expect(clientConfig.envs).toStrictEqual( + new UriMap([ + [Uri.from("wrap://ens/test.plugin.one"), { test: "value" }], + [Uri.from("wrap://ens/test.plugin.two"), { test: "value" }], + ]) + ); expect(clientConfig.interfaces).toStrictEqual({ "wrap://ens/test-interface-1.polywrap.eth": [ "wrap://ens/test1.polywrap.eth", @@ -164,7 +173,9 @@ describe("Client config builder", () => { expect(clientConfig).toBeTruthy(); const expectedBuilderConfig = DefaultBundle.getConfig(); - expect(JSON.stringify(builderConfig)).toBe(JSON.stringify(expectedBuilderConfig)); + expect(JSON.stringify(builderConfig)).toBe( + JSON.stringify(expectedBuilderConfig) + ); }); it("should successfully add an env", () => { @@ -178,11 +189,11 @@ describe("Client config builder", () => { const config = new ClientConfigBuilder().addEnv(envUri, env).build(); - if (!config.envs || Object.keys(config.envs).length !== 1) { + if (!config.envs || config.envs.size !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[envUri]).toEqual(env); + expect(config.envs.get(Uri.from(envUri))).toEqual(env); }); it("should successfully add to an existing env", () => { @@ -203,11 +214,11 @@ describe("Client config builder", () => { const expectedEnv = { ...env1, ...env2 }; - if (!config.envs || Object.keys(config.envs).length !== 1) { + if (!config.envs || config.envs.size !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[envUri]).toEqual(expectedEnv); + expect(config.envs.get(Uri.from(envUri))).toEqual(expectedEnv); }); it("should succesfully add two separate envs", () => { @@ -216,14 +227,14 @@ describe("Client config builder", () => { .addEnv(Object.keys(testEnvs)[1], Object.values(testEnvs)[1]) .build(); - if (!config.envs || Object.keys(config.envs).length !== 2) { + if (!config.envs || config.envs.size !== 2) { fail(["Expected 2 envs, received:", config.envs]); } - expect(config.envs[Object.keys(testEnvs)[0]]).toEqual( + expect(config.envs.get(Uri.from(Object.keys(testEnvs)[0]))).toEqual( Object.values(testEnvs)[0] ); - expect(config.envs[Object.keys(testEnvs)[0]]).toEqual( + expect(config.envs.get(Uri.from(Object.keys(testEnvs)[0]))).toEqual( Object.values(testEnvs)[1] ); }); @@ -235,11 +246,11 @@ describe("Client config builder", () => { .removeEnv(Object.keys(testEnvs)[0]) .build(); - if (!config.envs || Object.keys(config.envs).length !== 1) { + if (!config.envs || config.envs.size !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[Object.keys(testEnvs)[1]]).toEqual( + expect(config.envs.get(Uri.from(Object.keys(testEnvs)[1]))).toEqual( Object.values(testEnvs)[1] ); }); @@ -253,11 +264,11 @@ describe("Client config builder", () => { const config = new ClientConfigBuilder().setEnv(envUri, env).build(); - if (!config.envs || Object.keys(config.envs).length !== 1) { + if (!config.envs || config.envs.size !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[envUri]).toEqual(env); + expect(config.envs.get(Uri.from(envUri))).toEqual(env); }); it("should set an env over an existing env", () => { @@ -275,11 +286,11 @@ describe("Client config builder", () => { .setEnv(envUri, env2) .build(); - if (!config.envs || Object.keys(config.envs).length !== 1) { + if (!config.envs || config.envs.size !== 1) { fail(["Expected 1 env, received:", config.envs]); } - expect(config.envs[envUri]).toEqual(env2); + expect(config.envs.get(Uri.from(envUri))).toEqual(env2); }); it("should add an interface implementation for a non-existent interface", () => { diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index 74802603be..4042fbf663 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -3,13 +3,13 @@ import { InvokerOptions, TryResolveUriOptions } from "./types"; import { PolywrapCoreClient } from "@polywrap/core-client-js"; import { CoreClientConfig, - Envs, GetFileOptions, GetImplementationsOptions, InterfaceImplementations, InvokeResult, IUriResolutionContext, IUriResolver, + ReadonlyUriMap, Uri, UriPackageOrWrapper, ValidateOptions, @@ -66,7 +66,7 @@ export class PolywrapClient extends PolywrapCoreClient { } @Tracer.traceMethod("PolywrapClient: getEnvs") - public getEnvs(): Readonly | undefined { + public getEnvs(): ReadonlyUriMap | undefined { return super.getEnvs(); } @@ -78,7 +78,7 @@ export class PolywrapClient extends PolywrapCoreClient { @Tracer.traceMethod("PolywrapClient: getEnvByUri") public getEnvByUri( uri: TUri - ): Readonly | undefined { + ): WrapperEnv | undefined { return super.getEnvByUri(Uri.from(uri)); } diff --git a/packages/js/core-client/README.md b/packages/js/core-client/README.md index e79b2c0add..8c3eebbe97 100644 --- a/packages/js/core-client/README.md +++ b/packages/js/core-client/README.md @@ -84,7 +84,7 @@ Invoke a wrapper. * * @returns an array of env objects containing wrapper environmental variables */ - public getEnvs(): Readonly | undefined + public getEnvs(): ReadonlyUriMap | undefined ``` ### getResolver @@ -105,7 +105,7 @@ Invoke a wrapper. * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - public getEnvByUri(uri: Uri): Readonly | undefined + public getEnvByUri(uri: Uri): WrapperEnv | undefined ``` ### getManifest diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index 859f2792b6..f9a57495de 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -18,9 +18,9 @@ import { CoreClientConfig, WrapError, WrapErrorCode, - Envs, WrapperEnv, InterfaceImplementations, + ReadonlyUriMap } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { @@ -64,7 +64,7 @@ export class PolywrapCoreClient implements CoreClient { * * @returns an array of env objects containing wrapper environmental variables */ - public getEnvs(): Readonly | undefined /* $ */ { + public getEnvs(): ReadonlyUriMap | undefined /* $ */ { return this._config.envs; } @@ -85,13 +85,13 @@ export class PolywrapCoreClient implements CoreClient { * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - public getEnvByUri(uri: Uri): Readonly | undefined /* $ */ { + public getEnvByUri(uri: Uri): WrapperEnv | undefined /* $ */ { const envs = this.getEnvs(); if (!envs) { return undefined; } - return envs[uri.uri]; + return envs.get(uri); } // $start: PolywrapCoreClient-getManifest diff --git a/packages/js/core/README.md b/packages/js/core/README.md index 2947be2a86..962cf622fc 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -16,7 +16,7 @@ export interface CoreClientConfig { readonly interfaces?: InterfaceImplementations; /** set environmental variables for a wrapper */ - readonly envs?: Envs; + readonly envs?: ReadonlyUriMap; /** configure URI resolution for redirects, packages, and wrappers */ readonly resolver: Readonly>; @@ -70,7 +70,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of env objects containing wrapper environmental variables */ - getEnvs(): Readonly | undefined; + getEnvs(): ReadonlyUriMap | undefined; /** * returns an env (a set of environmental variables) from the configuration used to instantiate the client @@ -78,7 +78,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - getEnvByUri(uri: Uri): Readonly | undefined; + getEnvByUri(uri: Uri): WrapperEnv | undefined; /** * returns the URI resolver from the configuration used to instantiate the client diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index 89195c54f0..0fb1be63a7 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -2,7 +2,6 @@ import { Invoker, Uri, WrapError, - Envs, WrapperEnv, InterfaceImplementations, } from "."; @@ -11,6 +10,7 @@ import { UriResolverHandler } from "./UriResolver"; import { WrapManifest } from "@polywrap/wrap-manifest-types-js"; import { Result } from "@polywrap/result"; +import { ReadonlyUriMap } from "./UriMap"; // $start: CoreClient.ts @@ -20,7 +20,7 @@ export interface CoreClientConfig { readonly interfaces?: InterfaceImplementations; /** set environmental variables for a wrapper */ - readonly envs?: Envs; + readonly envs?: ReadonlyUriMap; /** configure URI resolution for redirects, packages, and wrappers */ readonly resolver: Readonly>; @@ -74,7 +74,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * * @returns an array of env objects containing wrapper environmental variables */ - getEnvs(): Readonly | undefined; + getEnvs(): ReadonlyUriMap | undefined; /** * returns an env (a set of environmental variables) from the configuration used to instantiate the client @@ -82,7 +82,7 @@ export interface CoreClient extends Invoker, UriResolverHandler { * @param uri - the URI used to register the env * @returns an env, or undefined if an env is not found at the given URI */ - getEnvByUri(uri: Uri): Readonly | undefined; + getEnvByUri(uri: Uri): WrapperEnv | undefined; /** * returns the URI resolver from the configuration used to instantiate the client diff --git a/packages/js/core/src/types/UriMap.ts b/packages/js/core/src/types/UriMap.ts index df0bf8c2bc..f50dc11404 100644 --- a/packages/js/core/src/types/UriMap.ts +++ b/packages/js/core/src/types/UriMap.ts @@ -1,11 +1,9 @@ import { Uri } from "./Uri"; -export class UriMap implements Map { +export class UriMap + implements Map, ReadonlyUriMap { private map: Map; - /** - * - */ constructor(entries?: readonly (readonly [Uri, TValue])[]) { this.map = new Map(entries?.map((x) => [x[0].uri, x[1]])); } @@ -23,7 +21,6 @@ export class UriMap implements Map { thisArg?: any ): void { this.map.forEach((v, k) => callbackfn(v, Uri.from(k), this), thisArg); - throw new Error("Method not implemented."); } get(key: Uri): TValue | undefined { @@ -68,11 +65,3 @@ export class UriMap implements Map { } export interface ReadonlyUriMap extends ReadonlyMap {} - -function foo(bar: ReadonlyUriMap) { - bar.size; -} - -const a = new UriMap(); - -foo(new UriMap([[Uri.from(""), true]])); diff --git a/packages/js/core/src/types/index.ts b/packages/js/core/src/types/index.ts index fa09582799..93ae1fcb3a 100644 --- a/packages/js/core/src/types/index.ts +++ b/packages/js/core/src/types/index.ts @@ -11,3 +11,4 @@ export * from "./IUriRedirect"; export * from "./IUriWrapper"; export * from "./IUriPackage"; export * from "./WrapError"; +export * from "./UriMap"; \ No newline at end of file From d5bbfd55099e8422545913980383fa4cf011578a Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 12:59:59 +0100 Subject: [PATCH 10/16] fix env tests --- .../src/__tests__/core/wrap-features/env-case.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/js/client/src/__tests__/core/wrap-features/env-case.ts b/packages/js/client/src/__tests__/core/wrap-features/env-case.ts index 694a9bec42..b9147fb70c 100644 --- a/packages/js/client/src/__tests__/core/wrap-features/env-case.ts +++ b/packages/js/client/src/__tests__/core/wrap-features/env-case.ts @@ -4,7 +4,7 @@ import { GetPathToTestWrappers } from "@polywrap/test-cases"; import { PolywrapClient } from "../../../PolywrapClient"; import { mockPluginRegistration } from "../../helpers"; import { ClientConfigBuilder } from "@polywrap/client-config-builder-js"; -import { Uri } from "@polywrap/core-js"; +import { Uri, UriMap } from "@polywrap/core-js"; jest.setTimeout(200000); @@ -175,11 +175,7 @@ export const envTestCases = (implementation: string) => { }, { from: Uri.from("ens/hello.eth"), to: implementationUri }, ]), - envs: { - "wrap://ens/hello.eth": { - arg1: "10", - }, - }, + envs: new UriMap([[Uri.from("wrap://ens/hello.eth"), { arg1: "10" }]]), }); const mockEnv = await client.invoke({ @@ -209,11 +205,7 @@ export const envTestCases = (implementation: string) => { })), }, ]), - envs: { - [implementationUri]: { - arg1: "10", - }, - }, + envs: new UriMap([[Uri.from(implementationUri), { arg1: "10" }]]), }); const mockEnv = await client.invoke({ From 8d2e26ed2551a7febc8ce8653194afc715cf036b Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 15:58:22 +0100 Subject: [PATCH 11/16] Rewrite CoreClientConfig.interfaces as ReadonlyUriMap --- .../src/BaseClientConfigBuilder.ts | 4 + .../src/ClientConfigBuilder.ts | 19 ++- .../__tests__/client-config-builder.spec.ts | 135 +++++++++++------- packages/js/core/README.md | 6 +- packages/js/core/src/types/CoreClient.ts | 7 +- 5 files changed, 101 insertions(+), 70 deletions(-) diff --git a/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts b/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts index 1a16551900..f617bcd130 100644 --- a/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/BaseClientConfigBuilder.ts @@ -173,6 +173,10 @@ export abstract class BaseClientConfigBuilder implements IClientConfigBuilder { existingInterface.delete(implementationUri); + if (existingInterface.size == 0) { + delete this._config.interfaces[interfaceUri]; + } + return this; } diff --git a/packages/js/client-config-builder/src/ClientConfigBuilder.ts b/packages/js/client-config-builder/src/ClientConfigBuilder.ts index 7aacbd77aa..ee9d288c0a 100644 --- a/packages/js/client-config-builder/src/ClientConfigBuilder.ts +++ b/packages/js/client-config-builder/src/ClientConfigBuilder.ts @@ -8,7 +8,6 @@ import { IUriRedirect, IUriWrapper, Uri, - InterfaceImplementations, WrapperEnv, ReadonlyUriMap, UriMap, @@ -78,19 +77,17 @@ export class ClientConfigBuilder extends BaseClientConfigBuilder { return envs; } - private buildInterfaces(): InterfaceImplementations { - const interfaces: Record = {}; + private buildInterfaces(): ReadonlyUriMap { + const interfaceImplementations = new UriMap(); - for (const iface in this._config.interfaces) { - if (this._config.interfaces[iface].size > 0) { - // Sanitize uri - const uri = Uri.from(iface).uri; - - interfaces[uri] = [...this._config.interfaces[iface]]; - } + for (const uri in this._config.interfaces) { + const uriImpls = [...this._config.interfaces[uri]].map((x) => + Uri.from(x) + ); + interfaceImplementations.set(Uri.from(uri), uriImpls); } - return interfaces; + return interfaceImplementations; } private buildRedirects(): IUriRedirect[] { diff --git a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts index 5b25b8c9b8..4068f4448a 100644 --- a/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts +++ b/packages/js/client-config-builder/src/__tests__/client-config-builder.spec.ts @@ -82,7 +82,7 @@ describe("Client config builder", () => { const clientConfig = new ClientConfigBuilder().build(); expect(clientConfig.envs).toStrictEqual(new UriMap()); - expect(clientConfig.interfaces).toStrictEqual({}); + expect(clientConfig.interfaces).toStrictEqual(new UriMap()); }); it("should succesfully add config object and build", () => { @@ -105,14 +105,18 @@ describe("Client config builder", () => { [Uri.from("wrap://ens/test.plugin.two"), { test: "value" }], ]) ); - expect(clientConfig.interfaces).toStrictEqual({ - "wrap://ens/test-interface-1.polywrap.eth": [ - "wrap://ens/test1.polywrap.eth", - ], - "wrap://ens/test-interface-2.polywrap.eth": [ - "wrap://ens/test2.polywrap.eth", - ], - }); + expect(clientConfig.interfaces).toStrictEqual( + new UriMap([ + [ + Uri.from("wrap://ens/test-interface-1.polywrap.eth"), + [Uri.from("wrap://ens/test1.polywrap.eth")], + ], + [ + Uri.from("wrap://ens/test-interface-2.polywrap.eth"), + [Uri.from("wrap://ens/test2.polywrap.eth")], + ], + ]) + ); expect(builderConfig).toEqual({ ...emptyBuilderConfig, @@ -144,14 +148,18 @@ describe("Client config builder", () => { [Uri.from("wrap://ens/test.plugin.two"), { test: "value" }], ]) ); - expect(clientConfig.interfaces).toStrictEqual({ - "wrap://ens/test-interface-1.polywrap.eth": [ - "wrap://ens/test1.polywrap.eth", - ], - "wrap://ens/test-interface-2.polywrap.eth": [ - "wrap://ens/test2.polywrap.eth", - ], - }); + expect(clientConfig.interfaces).toStrictEqual( + new UriMap([ + [ + Uri.from("wrap://ens/test-interface-1.polywrap.eth"), + [Uri.from("wrap://ens/test1.polywrap.eth")], + ], + [ + Uri.from("wrap://ens/test-interface-2.polywrap.eth"), + [Uri.from("wrap://ens/test2.polywrap.eth")], + ], + ]) + ); expect(clientConfig.resolver).toBeTruthy(); @@ -301,13 +309,13 @@ describe("Client config builder", () => { .addInterfaceImplementation(interfaceUri, implUri) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { + if (!config.interfaces || config.interfaces.size !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri]: [implUri], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([[Uri.from(interfaceUri), [Uri.from(implUri)]]]) + ); }); it("should add an interface implementation for an interface that already exists", () => { @@ -320,13 +328,15 @@ describe("Client config builder", () => { .addInterfaceImplementation(interfaceUri, implUri2) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { + if (!config.interfaces || config.interfaces.size !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri]: [implUri1, implUri2], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [Uri.from(interfaceUri), [Uri.from(implUri1), Uri.from(implUri2)]], + ]) + ); }); it("should add different implementations for different interfaces", () => { @@ -344,14 +354,16 @@ describe("Client config builder", () => { .addInterfaceImplementation(interfaceUri2, implUri4) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 2) { + if (!config.interfaces || config.interfaces.size !== 2) { fail(["Expected 2 interfaces, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri1]: [implUri1, implUri3], - [interfaceUri2]: [implUri2, implUri4], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [Uri.from(interfaceUri1), [Uri.from(implUri1), Uri.from(implUri3)]], + [Uri.from(interfaceUri2), [Uri.from(implUri2), Uri.from(implUri4)]], + ]) + ); }); it("should add multiple implementations for a non-existent interface", () => { @@ -363,13 +375,15 @@ describe("Client config builder", () => { .addInterfaceImplementations(interfaceUri, [implUri1, implUri2]) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { + if (!config.interfaces || config.interfaces.size !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri]: [implUri1, implUri2], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [Uri.from(interfaceUri), [Uri.from(implUri1), Uri.from(implUri2)]], + ]) + ); }); it("should add multiple implementations for an existing interface", () => { @@ -383,13 +397,18 @@ describe("Client config builder", () => { .addInterfaceImplementations(interfaceUri, [implUri2, implUri3]) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { + if (!config.interfaces || config.interfaces.size !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri]: [implUri1, implUri2, implUri3], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [ + Uri.from(interfaceUri), + [Uri.from(implUri1), Uri.from(implUri2), Uri.from(implUri3)], + ], + ]) + ); }); it("should add multiple different implementations for different interfaces", () => { @@ -409,14 +428,22 @@ describe("Client config builder", () => { .addInterfaceImplementations(interfaceUri2, [implUri4, implUri6]) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 2) { + if (!config.interfaces || config.interfaces.size !== 2) { fail(["Expected 2 interfaces, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri1]: [implUri1, implUri3, implUri5], - [interfaceUri2]: [implUri2, implUri4, implUri6], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [ + Uri.from(interfaceUri1), + [Uri.from(implUri1), Uri.from(implUri3), Uri.from(implUri5)], + ], + [ + Uri.from(interfaceUri2), + [Uri.from(implUri2), Uri.from(implUri4), Uri.from(implUri6)], + ], + ]) + ); }); it("should remove an interface implementation", () => { @@ -431,14 +458,16 @@ describe("Client config builder", () => { .removeInterfaceImplementation(interfaceUri1, implUri2) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 2) { + if (!config.interfaces || config.interfaces.size !== 2) { fail(["Expected 2 interfaces, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri1]: [implUri1], - [interfaceUri2]: [implUri1, implUri2], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [Uri.from(interfaceUri1), [Uri.from(implUri1)]], + [Uri.from(interfaceUri2), [Uri.from(implUri1), Uri.from(implUri2)]], + ]) + ); }); it("should completely remove an interface if there are no implementations left", () => { @@ -454,13 +483,15 @@ describe("Client config builder", () => { .removeInterfaceImplementation(interfaceUri1, implUri2) .build(); - if (!config.interfaces || Object.keys(config.interfaces).length !== 1) { + if (!config.interfaces || config.interfaces.size !== 1) { fail(["Expected 1 interface, received:", config.interfaces]); } - expect(config.interfaces).toStrictEqual({ - [interfaceUri2]: [implUri1, implUri2], - }); + expect(config.interfaces).toStrictEqual( + new UriMap([ + [Uri.from(interfaceUri2), [Uri.from(implUri1), Uri.from(implUri2)]], + ]) + ); }); it("should add an uri redirect", () => { diff --git a/packages/js/core/README.md b/packages/js/core/README.md index 962cf622fc..4f8f6fa717 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -13,7 +13,7 @@ A TypeScript / JavaScript implementation of the WRAP standard, including all fun /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { /** register interface implementations */ - readonly interfaces?: InterfaceImplementations; + readonly interfaces?: ReadonlyUriMap; /** set environmental variables for a wrapper */ readonly envs?: ReadonlyUriMap; @@ -61,9 +61,9 @@ export interface CoreClient extends Invoker, UriResolverHandler { /** * returns all interfaces from the configuration used to instantiate the client * - * @returns an array of interfaces and their registered implementations + * @returns a Set of interfaces and their registered implementations */ - getInterfaces(): InterfaceImplementations | undefined; + getInterfaces(): ReadonlyUriMap | undefined; /** * returns all env registrations from the configuration used to instantiate the client diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index 0fb1be63a7..2a1a391ba4 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -3,7 +3,6 @@ import { Uri, WrapError, WrapperEnv, - InterfaceImplementations, } from "."; import { IUriResolutionContext, IUriResolver } from "../uri-resolution"; import { UriResolverHandler } from "./UriResolver"; @@ -17,7 +16,7 @@ import { ReadonlyUriMap } from "./UriMap"; /** Core Client configuration that can be passed to the PolywrapClient or PolywrapCoreClient constructors */ export interface CoreClientConfig { /** register interface implementations */ - readonly interfaces?: InterfaceImplementations; + readonly interfaces?: ReadonlyUriMap; /** set environmental variables for a wrapper */ readonly envs?: ReadonlyUriMap; @@ -65,9 +64,9 @@ export interface CoreClient extends Invoker, UriResolverHandler { /** * returns all interfaces from the configuration used to instantiate the client * - * @returns an array of interfaces and their registered implementations + * @returns a Set of interfaces and their registered implementations */ - getInterfaces(): InterfaceImplementations | undefined; + getInterfaces(): ReadonlyUriMap | undefined; /** * returns all env registrations from the configuration used to instantiate the client From 685156892dbe75f11a0de15f7dd09467d5b1e449 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 16:29:38 +0100 Subject: [PATCH 12/16] adjust get-implementations algorithm to new CoreClientConfig structure --- packages/js/client/src/PolywrapClient.ts | 3 +- .../interface-implementation-case.ts | 80 ++++++------------- packages/js/client/src/__tests__/e2e.spec.ts | 4 +- packages/js/core-client/README.md | 2 +- .../js/core-client/src/PolywrapCoreClient.ts | 8 +- .../src/__tests__/get-implementations.spec.ts | 21 ++--- .../src/algorithms/get-implementations.ts | 22 ++--- 7 files changed, 58 insertions(+), 82 deletions(-) diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index 4042fbf663..978bb06868 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -5,7 +5,6 @@ import { CoreClientConfig, GetFileOptions, GetImplementationsOptions, - InterfaceImplementations, InvokeResult, IUriResolutionContext, IUriResolver, @@ -61,7 +60,7 @@ export class PolywrapClient extends PolywrapCoreClient { } @Tracer.traceMethod("PolywrapClient: getInterfaces") - public getInterfaces(): InterfaceImplementations | undefined { + public getInterfaces(): ReadonlyUriMap | undefined { return super.getInterfaces(); } diff --git a/packages/js/client/src/__tests__/core/wrap-features/interface-implementation-case.ts b/packages/js/client/src/__tests__/core/wrap-features/interface-implementation-case.ts index a98b201848..bbedd059c4 100644 --- a/packages/js/client/src/__tests__/core/wrap-features/interface-implementation-case.ts +++ b/packages/js/client/src/__tests__/core/wrap-features/interface-implementation-case.ts @@ -1,4 +1,9 @@ -import { ExtendableUriResolver, Uri, PolywrapClient } from "../../../index"; +import { + ExtendableUriResolver, + Uri, + PolywrapClient, + UriMap, +} from "../../../index"; import { ClientConfigBuilder } from "@polywrap/client-config-builder-js"; import { UriResolver } from "@polywrap/uri-resolvers-js"; import { mockPluginRegistration } from "../../helpers"; @@ -52,12 +57,9 @@ export const interfaceInvokeCase = (implementation: string) => { ); const client = new PolywrapClient({ - interfaces: [ - { - interface: interfaceUri, - implementations: [implementation1Uri, implementation2Uri], - }, - ], + interfaces: new UriMap([ + [interfaceUri, [implementation1Uri, implementation2Uri]], + ]), resolver: UriResolver.from({ from: Uri.from("uri/foo"), to: Uri.from("uri/bar"), @@ -66,12 +68,9 @@ export const interfaceInvokeCase = (implementation: string) => { const interfaces = client.getInterfaces(); - expect(interfaces).toEqual([ - { - interface: interfaceUri, - implementations: [implementation1Uri, implementation2Uri], - }, - ]); + expect(interfaces).toEqual(new UriMap([ + [interfaceUri, [implementation1Uri, implementation2Uri]], + ])); const implementations = await client.getImplementations(interfaceUri, { applyResolution: false, @@ -116,20 +115,11 @@ export const interfaceInvokeCase = (implementation: string) => { }, mockPluginRegistration(implementation4Uri), ]), - interfaces: [ - { - interface: interface1Uri, - implementations: [implementation1Uri, implementation2Uri], - }, - { - interface: interface2Uri, - implementations: [implementation3Uri], - }, - { - interface: interface3Uri, - implementations: [implementation3Uri, implementation4Uri], - }, - ], + interfaces: new UriMap([ + [interface1Uri, [implementation1Uri, implementation2Uri]], + [interface2Uri, [implementation3Uri]], + [interface3Uri, [implementation3Uri, implementation4Uri]], + ]), }); const implementations1 = await client.getImplementations(interface1Uri, { @@ -178,12 +168,7 @@ export const interfaceInvokeCase = (implementation: string) => { const client = new PolywrapClient(config); - const interfaces = (client.getInterfaces() || []).filter( - (x) => x.interface.uri === interfaceUri.uri - ); - expect(interfaces.length).toEqual(1); - - const implementationUris = interfaces[0].implementations; + const implementationUris = (client.getInterfaces() || new UriMap).get(interfaceUri); expect(implementationUris).toEqual([ implementationUri1, @@ -206,20 +191,13 @@ export const interfaceInvokeCase = (implementation: string) => { const client = new PolywrapClient(config); - const interfaces = (client.getInterfaces() || []).filter( - (x) => x.interface.uri === interfaceUri.uri - ); - expect(interfaces.length).toEqual(1); - - const implementationUris = interfaces[0].implementations; + const implementationUris = (client.getInterfaces() ?? new UriMap).get(interfaceUri); const builder = new ClientConfigBuilder(); const defaultClientConfig = builder.addDefaults().build(); expect(implementationUris).toEqual([ - ...(defaultClientConfig.interfaces || []).find( - (x) => x.interface.uri === interfaceUri.uri - )!.implementations, + ...((defaultClientConfig.interfaces ?? new UriMap).get(interfaceUri) || []), implementationUri1, implementationUri2, ]); @@ -239,12 +217,9 @@ export const interfaceInvokeCase = (implementation: string) => { resolver: UriResolver.from([ mockPluginRegistration(implementation1Uri), ]), - interfaces: [ - { - interface: interfaceUri, - implementations: [implementation2Uri], - }, - ], + interfaces: new UriMap([ + [interfaceUri, [implementation2Uri]], + ]), }); const getImplementationsResult = await client.getImplementations( @@ -270,12 +245,9 @@ export const interfaceInvokeCase = (implementation: string) => { resolver: UriResolver.from([ mockPluginRegistration(implementation1Uri), ]), - interfaces: [ - { - interface: interfaceUri, - implementations: [implementation1Uri, implementation2Uri], - }, - ], + interfaces: new UriMap([ + [interfaceUri, [implementation1Uri, implementation2Uri]], + ]), }); const getImplementationsResult = await client.getImplementations( diff --git a/packages/js/client/src/__tests__/e2e.spec.ts b/packages/js/client/src/__tests__/e2e.spec.ts index bb1a51db95..41f29d98ef 100644 --- a/packages/js/client/src/__tests__/e2e.spec.ts +++ b/packages/js/client/src/__tests__/e2e.spec.ts @@ -1,7 +1,7 @@ // import { typeTestCases } from "./core/type-test-cases"; // import { subinvokeCase } from "./core/wrap-features/subinvoke-case"; import { envTestCases } from "./core/wrap-features/env-case"; -// import { interfaceInvokeCase } from "./core/wrap-features/interface-implementation-case"; +import { interfaceInvokeCase } from "./core/wrap-features/interface-implementation-case"; export const supportedImplementations = ["as", "rs"]; describe.each(supportedImplementations)( @@ -10,6 +10,6 @@ describe.each(supportedImplementations)( // typeTestCases(i); // subinvokeCase(i); envTestCases(i); - // interfaceInvokeCase(i); + interfaceInvokeCase(i); } ); diff --git a/packages/js/core-client/README.md b/packages/js/core-client/README.md index 8c3eebbe97..0f64b7e764 100644 --- a/packages/js/core-client/README.md +++ b/packages/js/core-client/README.md @@ -74,7 +74,7 @@ Invoke a wrapper. * * @returns an array of interfaces and their registered implementations */ - public getInterfaces(): InterfaceImplementations | undefined + public getInterfaces(): ReadonlyUriMap | undefined ``` ### getEnvs diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index f9a57495de..e6a7ff8782 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -19,8 +19,8 @@ import { WrapError, WrapErrorCode, WrapperEnv, - InterfaceImplementations, - ReadonlyUriMap + ReadonlyUriMap, + UriMap } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { @@ -54,7 +54,7 @@ export class PolywrapCoreClient implements CoreClient { * * @returns an array of interfaces and their registered implementations */ - public getInterfaces(): InterfaceImplementations | undefined /* $ */ { + public getInterfaces(): ReadonlyUriMap | undefined /* $ */ { return this._config.interfaces; } @@ -160,7 +160,7 @@ export class PolywrapCoreClient implements CoreClient { const getImplResult = await getImplementations( Uri.from(uri), - this.getInterfaces() ?? {}, + this.getInterfaces() ?? new UriMap(), applyResolution ? this : undefined, applyResolution ? options.resolutionContext : undefined ); diff --git a/packages/js/core/src/__tests__/get-implementations.spec.ts b/packages/js/core/src/__tests__/get-implementations.spec.ts index ece280e4d6..fe514f9822 100644 --- a/packages/js/core/src/__tests__/get-implementations.spec.ts +++ b/packages/js/core/src/__tests__/get-implementations.spec.ts @@ -4,7 +4,7 @@ import { IUriRedirect, UriPackageOrWrapper, } from "../"; -import { CoreClient, InterfaceImplementations } from "../types"; +import { CoreClient, UriMap } from "../types"; import { Result, ResultOk } from "@polywrap/result"; const getClient = (redirects: IUriRedirect[]): CoreClient => { @@ -54,11 +54,14 @@ describe("getImplementations", () => { }, ]; - const interfaces: InterfaceImplementations = { - [interface1Uri]: [implementation1Uri, implementation2Uri], - [interface2Uri]: [implementation3Uri], - [interface3Uri]: [implementation3Uri], - }; + const interfaces: UriMap = new UriMap([ + [ + Uri.from(interface1Uri), + [Uri.from(implementation1Uri), Uri.from(implementation2Uri)], + ], + [Uri.from(interface2Uri), [Uri.from(implementation3Uri)]], + [Uri.from(interface3Uri), [Uri.from(implementation3Uri)]], + ]); const getImplementationsResult1 = await getImplementations( new Uri(interface1Uri), @@ -110,9 +113,9 @@ describe("getImplementations", () => { }, ]; - const interfaces: InterfaceImplementations = { - [interface1Uri]: [implementation1Uri], - }; + const interfaces: UriMap = new UriMap([ + [Uri.from(interface1Uri), [Uri.from(implementation1Uri)]], + ]); const result = await getImplementations( new Uri(interface1Uri), diff --git a/packages/js/core/src/algorithms/get-implementations.ts b/packages/js/core/src/algorithms/get-implementations.ts index 43a523c968..9a23f9ca03 100644 --- a/packages/js/core/src/algorithms/get-implementations.ts +++ b/packages/js/core/src/algorithms/get-implementations.ts @@ -3,7 +3,7 @@ import { CoreClient, WrapError, WrapErrorCode, - InterfaceImplementations, + ReadonlyUriMap, } from "../types"; import { IUriResolutionContext } from "../uri-resolution"; import { applyResolution } from "./applyResolution"; @@ -12,7 +12,7 @@ import { Result, ResultErr, ResultOk } from "@polywrap/result"; export const getImplementations = async ( wrapperInterfaceUri: Uri, - interfaces: InterfaceImplementations, + interfaces: ReadonlyUriMap, client?: CoreClient, resolutionContext?: IUriResolutionContext ): Promise> => { @@ -26,21 +26,20 @@ export const getImplementations = async ( }; const addAllImplementationsFromImplementationsArray = async ( - impls: InterfaceImplementations, + impls: ReadonlyUriMap, wrapperInterfaceUri: Uri ): Promise> => { - for (const impl in impls) { + for (const impl of impls.keys()) { let fullyResolvedUri: Uri; - const interfaceUri = Uri.from(impl); if (client) { const redirectsResult = await applyResolution( - interfaceUri, + impl, client, resolutionContext ); if (!redirectsResult.ok) { const error = new WrapError("Failed to resolve redirects", { - uri: impl, + uri: impl.uri, code: WrapErrorCode.CLIENT_GET_IMPLEMENTATIONS_ERROR, cause: redirectsResult.error, }); @@ -48,12 +47,15 @@ export const getImplementations = async ( } fullyResolvedUri = redirectsResult.value; } else { - fullyResolvedUri = interfaceUri; + fullyResolvedUri = impl; } if (Uri.equals(fullyResolvedUri, wrapperInterfaceUri)) { - for (const implementation of impls[impl]) { - addUniqueResult(Uri.from(implementation)); + const implementations = impls.get(impl); + if (implementations) { + for (const implementation of implementations) { + addUniqueResult(Uri.from(implementation)); + } } } } From e462c809aa00d65591bd9264bb12346119e56046 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 16:31:43 +0100 Subject: [PATCH 13/16] chore: lint --- packages/js/core-client/src/PolywrapCoreClient.ts | 2 +- packages/js/core/src/types/CoreClient.ts | 9 ++------- packages/js/core/src/types/UriMap.ts | 4 ++-- packages/js/core/src/types/index.ts | 2 +- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/js/core-client/src/PolywrapCoreClient.ts b/packages/js/core-client/src/PolywrapCoreClient.ts index e6a7ff8782..90324b11ea 100644 --- a/packages/js/core-client/src/PolywrapCoreClient.ts +++ b/packages/js/core-client/src/PolywrapCoreClient.ts @@ -20,7 +20,7 @@ import { WrapErrorCode, WrapperEnv, ReadonlyUriMap, - UriMap + UriMap, } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { diff --git a/packages/js/core/src/types/CoreClient.ts b/packages/js/core/src/types/CoreClient.ts index 2a1a391ba4..da099a6b0f 100644 --- a/packages/js/core/src/types/CoreClient.ts +++ b/packages/js/core/src/types/CoreClient.ts @@ -1,15 +1,10 @@ -import { - Invoker, - Uri, - WrapError, - WrapperEnv, -} from "."; +import { Invoker, Uri, WrapError, WrapperEnv } from "."; import { IUriResolutionContext, IUriResolver } from "../uri-resolution"; import { UriResolverHandler } from "./UriResolver"; +import { ReadonlyUriMap } from "./UriMap"; import { WrapManifest } from "@polywrap/wrap-manifest-types-js"; import { Result } from "@polywrap/result"; -import { ReadonlyUriMap } from "./UriMap"; // $start: CoreClient.ts diff --git a/packages/js/core/src/types/UriMap.ts b/packages/js/core/src/types/UriMap.ts index f50dc11404..a6e218cdd6 100644 --- a/packages/js/core/src/types/UriMap.ts +++ b/packages/js/core/src/types/UriMap.ts @@ -18,7 +18,7 @@ export class UriMap forEach( callbackfn: (value: TValue, key: Uri, map: Map) => void, - thisArg?: any + thisArg?: unknown ): void { this.map.forEach((v, k) => callbackfn(v, Uri.from(k), this), thisArg); } @@ -64,4 +64,4 @@ export class UriMap } } -export interface ReadonlyUriMap extends ReadonlyMap {} +export type ReadonlyUriMap = ReadonlyMap; diff --git a/packages/js/core/src/types/index.ts b/packages/js/core/src/types/index.ts index 93ae1fcb3a..ecd50e69ab 100644 --- a/packages/js/core/src/types/index.ts +++ b/packages/js/core/src/types/index.ts @@ -11,4 +11,4 @@ export * from "./IUriRedirect"; export * from "./IUriWrapper"; export * from "./IUriPackage"; export * from "./WrapError"; -export * from "./UriMap"; \ No newline at end of file +export * from "./UriMap"; From 7ac870de36ae9e684332d92a5b395165adf5cef1 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 16:58:42 +0100 Subject: [PATCH 14/16] chore: fix example quickstart --- packages/js/uri-resolver-extensions/README.md | 15 +++++++++------ .../examples/quickstart.ts | 17 ++++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/js/uri-resolver-extensions/README.md b/packages/js/uri-resolver-extensions/README.md index c44a555e53..fa0c9932ca 100644 --- a/packages/js/uri-resolver-extensions/README.md +++ b/packages/js/uri-resolver-extensions/README.md @@ -21,13 +21,16 @@ Otherwise, you must also add the `ExtendableUriResolver` to your resolver. ```ts const clientConfig: CoreClientConfig = { - interfaces: { - "wrap://ens/uri-resolver.core.polywrap.eth": [ - "wrap://ens/fs-resolver.polywrap.eth", - "wrap://ens/ipfs-resolver.polywrap.eth", - "wrap://ens/ens-resolver.polywrap.eth", + interfaces: new UriMap([ + [ + Uri.from("wrap://ens/uri-resolver.core.polywrap.eth"), + [ + Uri.from("wrap://ens/fs-resolver.polywrap.eth"), + Uri.from("wrap://ens/ipfs-resolver.polywrap.eth"), + Uri.from("wrap://ens/ens-resolver.polywrap.eth"), + ], ], - }, + ]), resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ diff --git a/packages/js/uri-resolver-extensions/examples/quickstart.ts b/packages/js/uri-resolver-extensions/examples/quickstart.ts index 9862a81989..9217914d49 100644 --- a/packages/js/uri-resolver-extensions/examples/quickstart.ts +++ b/packages/js/uri-resolver-extensions/examples/quickstart.ts @@ -1,4 +1,4 @@ -import { CoreClientConfig } from "@polywrap/core-js"; +import { CoreClientConfig, Uri, UriMap } from "@polywrap/core-js"; import { PackageResolver, PackageToWrapperCacheResolver, @@ -16,13 +16,16 @@ export function example(): CoreClientConfig { const packages: PackageResolver[] = []; // $start: quickstart-example const clientConfig: CoreClientConfig = { - interfaces: { - "wrap://ens/uri-resolver.core.polywrap.eth": [ - "wrap://ens/fs-resolver.polywrap.eth", - "wrap://ens/ipfs-resolver.polywrap.eth", - "wrap://ens/ens-resolver.polywrap.eth", + interfaces: new UriMap([ + [ + Uri.from("wrap://ens/uri-resolver.core.polywrap.eth"), + [ + Uri.from("wrap://ens/fs-resolver.polywrap.eth"), + Uri.from("wrap://ens/ipfs-resolver.polywrap.eth"), + Uri.from("wrap://ens/ens-resolver.polywrap.eth"), + ], ], - }, + ]), resolver: RecursiveResolver.from( PackageToWrapperCacheResolver.from( [ From 8d77daeca76552a86ea423329a4598c3c977d695 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 8 Mar 2023 17:36:36 +0100 Subject: [PATCH 15/16] Remove InterfaceImplementations file --- packages/js/core/README.md | 14 -------------- packages/js/core/readme/README.md | 6 ------ packages/js/core/src/types/Env.ts | 4 ---- .../js/core/src/types/InterfaceImplementations.ts | 7 ------- packages/js/core/src/types/index.ts | 1 - 5 files changed, 32 deletions(-) delete mode 100644 packages/js/core/src/types/InterfaceImplementations.ts diff --git a/packages/js/core/README.md b/packages/js/core/README.md index 4f8f6fa717..a3d2afc75a 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -128,25 +128,11 @@ export interface CoreClient extends Invoker, UriResolverHandler { ```ts /** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ -export interface Envs { - readonly [k: string]: WrapperEnv; -} - export interface WrapperEnv { readonly [k: string]: unknown; } ``` -### InterfaceImplementations - -```ts - -/** An interface and a list of wrappers that implement the interface */ -export interface InterfaceImplementations { - readonly [k: string]: readonly string[]; -} -``` - ### Invoke ```ts diff --git a/packages/js/core/readme/README.md b/packages/js/core/readme/README.md index 27479ac1c0..7b412808b2 100644 --- a/packages/js/core/readme/README.md +++ b/packages/js/core/readme/README.md @@ -18,12 +18,6 @@ $snippet: CoreClient.ts $snippet: Env.ts ``` -### InterfaceImplementations - -```ts -$snippet: InterfaceImplementations.ts -``` - ### Invoke ```ts diff --git a/packages/js/core/src/types/Env.ts b/packages/js/core/src/types/Env.ts index f2cfd502bc..1b88c52c8b 100644 --- a/packages/js/core/src/types/Env.ts +++ b/packages/js/core/src/types/Env.ts @@ -1,10 +1,6 @@ // $start: Env.ts /** A map of string-indexed, Msgpack-serializable environmental variables associated with a wrapper */ -export interface Envs { - readonly [k: string]: WrapperEnv; -} - export interface WrapperEnv { readonly [k: string]: unknown; } diff --git a/packages/js/core/src/types/InterfaceImplementations.ts b/packages/js/core/src/types/InterfaceImplementations.ts deleted file mode 100644 index bae760a1fe..0000000000 --- a/packages/js/core/src/types/InterfaceImplementations.ts +++ /dev/null @@ -1,7 +0,0 @@ -// $start: InterfaceImplementations.ts - -/** An interface and a list of wrappers that implement the interface */ -export interface InterfaceImplementations { - readonly [k: string]: readonly string[]; -} -// $end diff --git a/packages/js/core/src/types/index.ts b/packages/js/core/src/types/index.ts index ecd50e69ab..832a906a57 100644 --- a/packages/js/core/src/types/index.ts +++ b/packages/js/core/src/types/index.ts @@ -4,7 +4,6 @@ export * from "./Invoke"; export * from "./MaybeAsync"; export * from "./Uri"; export * from "./Env"; -export * from "./InterfaceImplementations"; export * from "./UriResolver"; export * from "./IWrapPackage"; export * from "./IUriRedirect"; From b461dc4f0f8c35e4eb0f8d37815c071506e42ec9 Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Wed, 15 Mar 2023 21:01:49 -0400 Subject: [PATCH 16/16] chore: uncomment test cases --- packages/js/client/src/__tests__/e2e.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/js/client/src/__tests__/e2e.spec.ts b/packages/js/client/src/__tests__/e2e.spec.ts index 41f29d98ef..09d21a5f02 100644 --- a/packages/js/client/src/__tests__/e2e.spec.ts +++ b/packages/js/client/src/__tests__/e2e.spec.ts @@ -1,5 +1,5 @@ -// import { typeTestCases } from "./core/type-test-cases"; -// import { subinvokeCase } from "./core/wrap-features/subinvoke-case"; +import { typeTestCases } from "./core/type-test-cases"; +import { subinvokeCase } from "./core/wrap-features/subinvoke-case"; import { envTestCases } from "./core/wrap-features/env-case"; import { interfaceInvokeCase } from "./core/wrap-features/interface-implementation-case"; @@ -7,8 +7,8 @@ export const supportedImplementations = ["as", "rs"]; describe.each(supportedImplementations)( "client <-> wrappers end to end", (i) => { - // typeTestCases(i); - // subinvokeCase(i); + typeTestCases(i); + subinvokeCase(i); envTestCases(i); interfaceInvokeCase(i); }