From f82b5fa575e0bbd8a572a227c8102200b728b36d Mon Sep 17 00:00:00 2001 From: Pileks Date: Mon, 18 Jul 2022 17:28:30 +0200 Subject: [PATCH 01/18] exploratory --- packages/js/plugins/ipfs/src/index.ts | 5 +++++ packages/js/plugins/ipfs/src/schema.graphql | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index d0db1d7530..1cc4b6dc60 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -13,6 +13,11 @@ import { execSimple, execFallbacks } from "./utils/exec"; import { Client, PluginFactory } from "@polywrap/core-js"; +//options: +// timeout +// disableParallelRequests +// provider + // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/naming-convention const createIpfsClient = require("@dorgjelli-test/ipfs-http-client-lite"); diff --git a/packages/js/plugins/ipfs/src/schema.graphql b/packages/js/plugins/ipfs/src/schema.graphql index 61085a4a7e..b381d5360b 100644 --- a/packages/js/plugins/ipfs/src/schema.graphql +++ b/packages/js/plugins/ipfs/src/schema.graphql @@ -1,10 +1,20 @@ #import { Module } into Ipfs from "ens/ipfs.polywrap.eth" +type Timeouts { + addFile: UInt32, + cat: UInt32, + resolve: UInt32, +} + type Env { """ Disable querying providers in parallel when resolving URIs """ - disableParallelRequests: Boolean + disableParallelRequests: Boolean, + """ + Global timeout for IPFS actions + """ + timeouts: Timeouts } type Module implements Ipfs_Module { From 793e4642cb4a2dfb831e4f7bac494178609dcab5 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 20 Jul 2022 11:01:23 +0200 Subject: [PATCH 02/18] Add basic env options to ipfs plugin --- packages/js/plugins/ipfs/src/index.ts | 72 +++++++++++++++------ packages/js/plugins/ipfs/src/schema.graphql | 6 +- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index 1cc4b6dc60..80f33e3795 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -5,7 +5,6 @@ import { Args_cat, Ipfs_Options, Ipfs_ResolveResult, - Env, manifest, } from "./wrap"; import { IpfsClient } from "./utils/IpfsClient"; @@ -13,27 +12,33 @@ import { execSimple, execFallbacks } from "./utils/exec"; import { Client, PluginFactory } from "@polywrap/core-js"; -//options: -// timeout -// disableParallelRequests -// provider - // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/naming-convention const createIpfsClient = require("@dorgjelli-test/ipfs-http-client-lite"); +const isNullOrUndefined = (arg: unknown) => { + return arg === undefined || arg === null; +}; + const getOptions = ( args: Ipfs_Options | undefined | null, - env: Env + timeout: number | null | undefined, + disableParallelRequests: boolean | null | undefined, + provider: string | null | undefined ): Ipfs_Options => { const options = args || {}; - if ( - options.disableParallelRequests === undefined || - options.disableParallelRequests === null - ) { - options.disableParallelRequests = env.disableParallelRequests; + if (isNullOrUndefined(options.disableParallelRequests)) { + options.disableParallelRequests = disableParallelRequests; } + if (isNullOrUndefined(options.timeout)) { + options.timeout = timeout; + } + + if (isNullOrUndefined(options.provider)) { + options.provider = provider; + } + return options; }; @@ -53,12 +58,19 @@ export class IpfsPlugin extends Module { } public async cat(args: Args_cat, _client: Client): Promise { + const options = getOptions( + args.options, + this.env.timeouts?.cat, + this.env.disableParallelRequests, + this.env.provider + ); + return await this._execWithOptions( "cat", (ipfs: IpfsClient, _provider: string, options: unknown) => { return ipfs.cat(args.cid, options); }, - args.options ?? undefined + options ); } @@ -66,7 +78,12 @@ export class IpfsPlugin extends Module { args: Args_resolve, _client: Client ): Promise { - const options = getOptions(args.options, this.env); + const options = getOptions( + args.options, + this.env.timeouts?.resolve, + this.env.disableParallelRequests, + this.env.provider + ); return await this._execWithOptions( "resolve", async (ipfs: IpfsClient, provider: string, options: unknown) => { @@ -81,15 +98,28 @@ export class IpfsPlugin extends Module { } public async addFile(args: Args_addFile): Promise { - const result = await this._ipfs.add(new Uint8Array(args.data)); + const options = getOptions( + null, + this.env.timeouts?.addFile, + this.env.disableParallelRequests, + this.env.provider + ); - if (result.length === 0) { - throw Error( - `IpfsPlugin:add failed to add contents. Result of length 0 returned.` - ); - } + return await this._execWithOptions( + "add", + async (ipfs: IpfsClient, provider: string, options: unknown) => { + const result = await ipfs.add(new Uint8Array(args.data), options); - return result[0].hash.toString(); + if (result.length === 0) { + throw Error( + `IpfsPlugin:add failed to add contents. Result of length 0 returned.` + ); + } + + return result[0].hash.toString(); + }, + options + ); } private async _execWithOptions( diff --git a/packages/js/plugins/ipfs/src/schema.graphql b/packages/js/plugins/ipfs/src/schema.graphql index b381d5360b..8415acff03 100644 --- a/packages/js/plugins/ipfs/src/schema.graphql +++ b/packages/js/plugins/ipfs/src/schema.graphql @@ -14,7 +14,11 @@ type Env { """ Global timeout for IPFS actions """ - timeouts: Timeouts + timeouts: Timeouts, + """ + Default provider + """ + provider: String } type Module implements Ipfs_Module { From d20a142464d7cd9949d34fa22fefa4abf5251852 Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 20 Jul 2022 20:14:25 +0200 Subject: [PATCH 03/18] remove ipfs plugin config --- packages/cli/src/lib/helpers/client.ts | 13 +++- .../cli/src/lib/test-env/client-config.ts | 17 +++-- .../js/client/scripts/extractPluginConfigs.ts | 12 ---- .../js/client/src/default-client-config.ts | 13 ++-- packages/js/client/src/pluginConfigs/index.ts | 4 -- .../js/plugins/ipfs/src/__tests__/e2e.spec.ts | 10 ++- packages/js/plugins/ipfs/src/index.ts | 71 ++++++------------- packages/js/plugins/ipfs/src/schema.graphql | 14 ++-- .../ipfs-resolver/src/__tests__/e2e.spec.ts | 37 +++++++--- .../uri-resolvers/ipfs-resolver/src/index.ts | 37 +++++----- .../ipfs-resolver/src/schema.graphql | 17 +++++ 11 files changed, 129 insertions(+), 116 deletions(-) diff --git a/packages/cli/src/lib/helpers/client.ts b/packages/cli/src/lib/helpers/client.ts index 41df7b184b..b22be2a041 100644 --- a/packages/cli/src/lib/helpers/client.ts +++ b/packages/cli/src/lib/helpers/client.ts @@ -1,4 +1,4 @@ -import { PluginRegistration } from "@polywrap/core-js"; +import { PluginRegistration, Env } from "@polywrap/core-js"; import { ensResolverPlugin } from "@polywrap/ens-resolver-plugin-js"; import { ethereumPlugin } from "@polywrap/ethereum-plugin-js"; import { ipfsPlugin } from "@polywrap/ipfs-plugin-js"; @@ -13,6 +13,8 @@ interface SimpleClientConfig { export function getSimpleClient(config: SimpleClientConfig): PolywrapClient { const { ensAddress, ethProvider, ipfsProvider } = config; const plugins: PluginRegistration[] = []; + const envs: Env[] = []; + if (ensAddress) { plugins.push({ uri: "wrap://ens/ens-resolver.polywrap.eth", @@ -38,10 +40,15 @@ export function getSimpleClient(config: SimpleClientConfig): PolywrapClient { if (ipfsProvider) { plugins.push({ uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ + plugin: ipfsPlugin({}), + }); + + envs.push({ + uri: "wrap://ens/ipfs.polywrap.eth", + env: { provider: ipfsProvider, fallbackProviders: defaultIpfsProviders, - }), + }, }); } return new PolywrapClient({ plugins }); diff --git a/packages/cli/src/lib/test-env/client-config.ts b/packages/cli/src/lib/test-env/client-config.ts index 06ccae1fdd..ae2e6f095c 100644 --- a/packages/cli/src/lib/test-env/client-config.ts +++ b/packages/cli/src/lib/test-env/client-config.ts @@ -9,6 +9,7 @@ import { ensResolverPlugin } from "@polywrap/ens-resolver-plugin-js"; import { ethereumPlugin } from "@polywrap/ethereum-plugin-js"; import { ipfsPlugin } from "@polywrap/ipfs-plugin-js"; import { ensAddresses } from "@polywrap/test-env-js"; +import { Env } from "@polywrap/core-js"; export async function getTestEnvClientConfig(): Promise< Partial @@ -38,10 +39,7 @@ export async function getTestEnvClientConfig(): Promise< }, { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ - provider: ipfsProvider, - fallbackProviders: defaultIpfsProviders, - }), + plugin: ipfsPlugin({}), }, { uri: "wrap://ens/ens-resolver.polywrap.eth", @@ -53,7 +51,18 @@ export async function getTestEnvClientConfig(): Promise< }, ]; + const envs : Env[] = [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: ipfsProvider, + fallbackProviders: defaultIpfsProviders, + } + } + ]; + return { plugins, + envs }; } diff --git a/packages/js/client/scripts/extractPluginConfigs.ts b/packages/js/client/scripts/extractPluginConfigs.ts index 344b0b012f..bdebfe9feb 100644 --- a/packages/js/client/scripts/extractPluginConfigs.ts +++ b/packages/js/client/scripts/extractPluginConfigs.ts @@ -19,18 +19,6 @@ interface PluginConfigSource { } const plugins: PluginConfigSource[] = [ - { - name: "Ipfs", - module: "@polywrap/ipfs-plugin-js", - uri: "wrap://ens/ipfs.polywrap.eth", - config: "IpfsPluginConfig", - files: [ - { - name: "build/index.d.ts", - interfaces: ["IpfsPluginConfig"], - }, - ], - }, { name: "Ethereum", module: "@polywrap/ethereum-plugin-js", diff --git a/packages/js/client/src/default-client-config.ts b/packages/js/client/src/default-client-config.ts index ca82bd7bbf..c562929667 100644 --- a/packages/js/client/src/default-client-config.ts +++ b/packages/js/client/src/default-client-config.ts @@ -29,16 +29,19 @@ export const getDefaultClientConfig = Tracer.traceFunc( "client-js: getDefaultClientConfig", (): ClientConfig => { return { - envs: [], + envs: [{ + uri: new Uri("wrap://ens/ipfs.polywrap.eth"), + env: { + provider: defaultIpfsProviders[0], + fallbackProviders: defaultIpfsProviders.slice(1), + } + }], redirects: [], plugins: [ // IPFS is required for downloading Polywrap packages { uri: new Uri("wrap://ens/ipfs.polywrap.eth"), - plugin: ipfsPlugin({ - provider: defaultIpfsProviders[0], - fallbackProviders: defaultIpfsProviders.slice(1), - }), + plugin: ipfsPlugin({}), }, // ENS is required for resolving domain to IPFS hashes { diff --git a/packages/js/client/src/pluginConfigs/index.ts b/packages/js/client/src/pluginConfigs/index.ts index e4887885fa..042e4ff673 100644 --- a/packages/js/client/src/pluginConfigs/index.ts +++ b/packages/js/client/src/pluginConfigs/index.ts @@ -2,24 +2,20 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable prettier/prettier */ -import { IpfsPluginConfig } from "./Ipfs"; import { EthereumPluginConfig } from "./Ethereum"; import { EnsResolverPluginConfig } from "./Ens"; interface PluginConfigs { - ipfs?: IpfsPluginConfig; ethereum?: EthereumPluginConfig; ens?: EnsResolverPluginConfig; } const modules: Record = { - ipfs: "@polywrap/ipfs-plugin-js", ethereum: "@polywrap/ethereum-plugin-js", ens: "@polywrap/ens-resolver-plugin-js", }; const uris: Record = { - ipfs: "wrap://ens/ipfs.polywrap.eth", ethereum: "wrap://ens/ethereum.polywrap.eth", ens: "wrap://ens/ens-resolver.polywrap.eth", }; diff --git a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts index f732bc6955..2fb8b822d3 100644 --- a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts @@ -29,9 +29,15 @@ describe("IPFS Plugin", () => { plugins: [ { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ + plugin: ipfsPlugin({}), + }, + ], + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { provider: providers.ipfs, - }), + }, }, ], }); diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index 80f33e3795..17a3f1c192 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -6,10 +6,10 @@ import { Ipfs_Options, Ipfs_ResolveResult, manifest, + Env, } from "./wrap"; import { IpfsClient } from "./utils/IpfsClient"; import { execSimple, execFallbacks } from "./utils/exec"; - import { Client, PluginFactory } from "@polywrap/core-js"; // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/naming-convention @@ -21,49 +21,30 @@ const isNullOrUndefined = (arg: unknown) => { const getOptions = ( args: Ipfs_Options | undefined | null, - timeout: number | null | undefined, - disableParallelRequests: boolean | null | undefined, - provider: string | null | undefined + env: Env ): Ipfs_Options => { const options = args || {}; if (isNullOrUndefined(options.disableParallelRequests)) { - options.disableParallelRequests = disableParallelRequests; + options.disableParallelRequests = env.disableParallelRequests; } if (isNullOrUndefined(options.timeout)) { - options.timeout = timeout; + options.timeout = env.timeout; } - + if (isNullOrUndefined(options.provider)) { - options.provider = provider; + options.provider = env.provider; } - + return options; }; -export interface IpfsPluginConfig { - provider: string; - fallbackProviders?: string[]; -} - -export class IpfsPlugin extends Module { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore: initialized within setProvider - private _ipfs: IpfsClient; - - constructor(config: IpfsPluginConfig) { - super(config); - this._ipfs = createIpfsClient(this.config.provider); - } +type NoConfig = Record; +export class IpfsPlugin extends Module { public async cat(args: Args_cat, _client: Client): Promise { - const options = getOptions( - args.options, - this.env.timeouts?.cat, - this.env.disableParallelRequests, - this.env.provider - ); + const options = getOptions(args.options, this.env); return await this._execWithOptions( "cat", @@ -78,12 +59,8 @@ export class IpfsPlugin extends Module { args: Args_resolve, _client: Client ): Promise { - const options = getOptions( - args.options, - this.env.timeouts?.resolve, - this.env.disableParallelRequests, - this.env.provider - ); + const options = getOptions(args.options, this.env); + return await this._execWithOptions( "resolve", async (ipfs: IpfsClient, provider: string, options: unknown) => { @@ -98,12 +75,7 @@ export class IpfsPlugin extends Module { } public async addFile(args: Args_addFile): Promise { - const options = getOptions( - null, - this.env.timeouts?.addFile, - this.env.disableParallelRequests, - this.env.provider - ); + const options = getOptions(null, this.env); return await this._execWithOptions( "add", @@ -131,11 +103,13 @@ export class IpfsPlugin extends Module { ) => Promise, options?: Ipfs_Options ): Promise { + const defaultIpfsClient = createIpfsClient(this.env.provider); + if (!options) { // Default behavior if no options are provided return await execSimple( operation, - this._ipfs, + defaultIpfsClient, this.config.provider, 0, func @@ -144,12 +118,9 @@ export class IpfsPlugin extends Module { const timeout = options.timeout || 0; - let providers = [ - this.config.provider, - ...(this.config.fallbackProviders || []), - ]; - let ipfs = this._ipfs; - let defaultProvider = this.config.provider; + let providers = [this.env.provider, ...(this.env.fallbackProviders || [])]; + let ipfs = defaultIpfsClient; + let defaultProvider = this.env.provider; // Use the provider default override specified if (options.provider) { @@ -172,9 +143,7 @@ export class IpfsPlugin extends Module { } } -export const ipfsPlugin: PluginFactory = ( - config: IpfsPluginConfig -) => { +export const ipfsPlugin: PluginFactory = (config: NoConfig) => { return { factory: () => new IpfsPlugin(config), manifest, diff --git a/packages/js/plugins/ipfs/src/schema.graphql b/packages/js/plugins/ipfs/src/schema.graphql index 8415acff03..b2146cd4d5 100644 --- a/packages/js/plugins/ipfs/src/schema.graphql +++ b/packages/js/plugins/ipfs/src/schema.graphql @@ -1,11 +1,5 @@ #import { Module } into Ipfs from "ens/ipfs.polywrap.eth" -type Timeouts { - addFile: UInt32, - cat: UInt32, - resolve: UInt32, -} - type Env { """ Disable querying providers in parallel when resolving URIs @@ -14,11 +8,15 @@ type Env { """ Global timeout for IPFS actions """ - timeouts: Timeouts, + timeout: UInt32, """ Default provider """ - provider: String + provider: String!, + """ + Fallback providers + """ + fallbackProviders: [String!] } type Module implements Ipfs_Module { diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts index 708eaf4534..866975f5bc 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts @@ -1,6 +1,11 @@ import { PolywrapClient } from "@polywrap/client-js"; import { GetPathToTestWrappers } from "@polywrap/test-cases"; -import { buildAndDeployWrapper, initTestEnvironment, providers, stopTestEnvironment } from "@polywrap/test-env-js"; +import { + buildAndDeployWrapper, + initTestEnvironment, + providers, + stopTestEnvironment, +} from "@polywrap/test-env-js"; import { ipfsResolverPlugin } from ".."; import { ipfsPlugin } from "@polywrap/ipfs-plugin-js"; @@ -9,7 +14,7 @@ import { createIpfsClient } from "./helpers/createIpfsClient"; jest.setTimeout(300000); -describe("IPFS Plugin", () => { +describe("IPFS Resolver Plugin", () => { let client: PolywrapClient; let ipfs: IpfsClient; @@ -24,7 +29,7 @@ describe("IPFS Plugin", () => { wrapperAbsPath: `${GetPathToTestWrappers()}/wasm-as/simple-storage`, ipfsProvider: providers.ipfs, ethereumProvider: providers.ethereum, - ensName: "cool.wrapper.eth" + ensName: "cool.wrapper.eth", }); wrapperIpfsCid = ipfsCid; @@ -33,15 +38,27 @@ describe("IPFS Plugin", () => { plugins: [ { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ - provider: providers.ipfs - }) + plugin: ipfsPlugin({}), }, { - uri: "wrap://ens/ipfs-uri-resolver.polywrap.eth", - plugin: ipfsResolverPlugin({}) - } - ] + uri: "wrap://ens/ipfs-resolver.polywrap.eth", + plugin: ipfsResolverPlugin({}), + }, + ], + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: providers.ipfs, + }, + }, + { + uri: "wrap://ens/ipfs-resolver.polywrap.eth", + env: { + skipCheckIfExistsBeforeGetFile: false, + }, + }, + ], }); }); diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts index 0a1d7a2ec1..f4b9cd2692 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts @@ -40,7 +40,7 @@ export class IpfsResolverPlugin extends Module { { cid: `${args.path}/${manifestSearchPattern}`, options: { - timeout: 5000, + timeout: this.env.timeouts?.tryResolveUri, }, }, _client @@ -64,28 +64,31 @@ export class IpfsResolverPlugin extends Module { client: Client ): Promise { try { - const resolveResult = await Ipfs_Module.resolve( - { - cid: args.path, - options: { - timeout: 5000, + if(!this.env.skipCheckIfExistsBeforeGetFile){ + const resolveResult = await Ipfs_Module.resolve( + { + cid: args.path, + options: { + timeout: this.env.timeouts?.checkIfExists, + }, }, - }, - client - ); - - const result = resolveResult.data; - - if (!result) { - return null; + client + ); + + const result = resolveResult.data; + + if (!result) { + return null; + } } const catResult = await Ipfs_Module.cat( { - cid: result.cid, + cid: args.path, options: { - provider: result.provider, - timeout: 20000, + //provider: result.provider, + //TODO - JureG: Provider! + timeout: this.env.timeouts?.getFile, disableParallelRequests: true, }, }, diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql index 2ba8f5de0f..fae13d8b6e 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql @@ -1,5 +1,22 @@ #import { Module, MaybeUriOrManifest } into UriResolver from "ens/uri-resolver.core.polywrap.eth" #import { Module } into Ipfs from "ens/ipfs.polywrap.eth" +type Timeouts { + getFile: UInt32, + checkIfExists: UInt32, + tryResolveUri: UInt32, +} + +type Env { + """ + Determines whether the plugin should try to resolve a file (check for its existence) or immediately get the file + """ + skipCheckIfExistsBeforeGetFile: Boolean, + """ + Timeouts for methods + """ + timeouts: Timeouts +} + type Module implements UriResolver_Module { } \ No newline at end of file From f2199f8d50878fc46202d48572f1b9fcb3e64bbb Mon Sep 17 00:00:00 2001 From: Pileks Date: Wed, 20 Jul 2022 20:34:28 +0200 Subject: [PATCH 04/18] use provider when checking for file existence --- .../plugins/uri-resolvers/ipfs-resolver/src/index.ts | 11 ++++++++--- .../uri-resolvers/ipfs-resolver/src/schema.graphql | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts index f4b9cd2692..31118d5c1d 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts @@ -41,6 +41,7 @@ export class IpfsResolverPlugin extends Module { cid: `${args.path}/${manifestSearchPattern}`, options: { timeout: this.env.timeouts?.tryResolveUri, + disableParallelRequests: this.env.disableParallelRequests }, }, _client @@ -64,12 +65,15 @@ export class IpfsResolverPlugin extends Module { client: Client ): Promise { try { + let provider: string | undefined = undefined; + if(!this.env.skipCheckIfExistsBeforeGetFile){ const resolveResult = await Ipfs_Module.resolve( { cid: args.path, options: { timeout: this.env.timeouts?.checkIfExists, + disableParallelRequests: this.env.disableParallelRequests }, }, client @@ -80,16 +84,17 @@ export class IpfsResolverPlugin extends Module { if (!result) { return null; } + + provider = result.provider; } const catResult = await Ipfs_Module.cat( { cid: args.path, options: { - //provider: result.provider, - //TODO - JureG: Provider! + provider: provider, timeout: this.env.timeouts?.getFile, - disableParallelRequests: true, + disableParallelRequests: this.env.disableParallelRequests, }, }, client diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql index fae13d8b6e..c9ce7dc1e5 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql @@ -15,7 +15,11 @@ type Env { """ Timeouts for methods """ - timeouts: Timeouts + timeouts: Timeouts, + """ + Disable parallel requests + """ + disableParallelRequests: Boolean } type Module implements UriResolver_Module { From 622d08a85b77572d559d64f6c77986109db26bd1 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 10:46:49 +0200 Subject: [PATCH 05/18] fix broken tests, add envs where needed --- .../cli/src/lib/test-env/client-config.ts | 8 +- .../js/client/scripts/extractPluginConfigs.ts | 12 + .../src/__tests__/core/resolveUri.spec.ts | 2 +- .../src/__tests__/core/wasm-wrapper.spec.ts | 24 +- .../client/src/__tests__/e2e/wasm-as.spec.ts | 245 +++++++++--------- .../client/src/__tests__/e2e/workflow.spec.ts | 28 +- .../js/client/src/default-client-config.ts | 16 +- packages/js/client/src/pluginConfigs/Ipfs.ts | 5 +- packages/js/client/src/pluginConfigs/index.ts | 4 + packages/js/plugins/ipfs/src/index.ts | 2 +- .../uri-resolvers/ipfs-resolver/src/index.ts | 10 +- 11 files changed, 194 insertions(+), 162 deletions(-) diff --git a/packages/cli/src/lib/test-env/client-config.ts b/packages/cli/src/lib/test-env/client-config.ts index ae2e6f095c..34c17ffd63 100644 --- a/packages/cli/src/lib/test-env/client-config.ts +++ b/packages/cli/src/lib/test-env/client-config.ts @@ -51,18 +51,18 @@ export async function getTestEnvClientConfig(): Promise< }, ]; - const envs : Env[] = [ + const envs: Env[] = [ { uri: "wrap://ens/ipfs.polywrap.eth", env: { provider: ipfsProvider, fallbackProviders: defaultIpfsProviders, - } - } + }, + }, ]; return { plugins, - envs + envs, }; } diff --git a/packages/js/client/scripts/extractPluginConfigs.ts b/packages/js/client/scripts/extractPluginConfigs.ts index bdebfe9feb..58a724468a 100644 --- a/packages/js/client/scripts/extractPluginConfigs.ts +++ b/packages/js/client/scripts/extractPluginConfigs.ts @@ -19,6 +19,18 @@ interface PluginConfigSource { } const plugins: PluginConfigSource[] = [ + { + name: "Ipfs", + module: "@polywrap/ipfs-plugin-js", + uri: "wrap://ens/ipfs.polywrap.eth", + config: "NoConfig", + files: [ + { + name: "build/index.d.ts", + types: ["NoConfig"] + }, + ], + }, { name: "Ethereum", module: "@polywrap/ethereum-plugin-js", diff --git a/packages/js/client/src/__tests__/core/resolveUri.spec.ts b/packages/js/client/src/__tests__/core/resolveUri.spec.ts index 006325602d..a28241fa1d 100644 --- a/packages/js/client/src/__tests__/core/resolveUri.spec.ts +++ b/packages/js/client/src/__tests__/core/resolveUri.spec.ts @@ -42,7 +42,7 @@ describe("resolveUri", () => { }, }, }, - ipfs: { provider: ipfsProvider }, + ipfs: {}, ens: { addresses: { testnet: ensAddresses.ensAddress, diff --git a/packages/js/client/src/__tests__/core/wasm-wrapper.spec.ts b/packages/js/client/src/__tests__/core/wasm-wrapper.spec.ts index 25e4c57c00..3d139671cb 100644 --- a/packages/js/client/src/__tests__/core/wasm-wrapper.spec.ts +++ b/packages/js/client/src/__tests__/core/wasm-wrapper.spec.ts @@ -3,15 +3,15 @@ import { ensAddresses, initTestEnvironment, stopTestEnvironment, - providers + providers, } from "@polywrap/test-env-js"; -import { msgpackDecode } from "@polywrap/msgpack-js" +import { msgpackDecode } from "@polywrap/msgpack-js"; import { Uri, createPolywrapClient, PolywrapClientConfig, PluginModule, - Subscription + Subscription, } from "../.."; import { GetPathToTestWrappers } from "@polywrap/test-cases"; import fs from "fs"; @@ -49,14 +49,24 @@ describe("wasm-wrapper", () => { }, }, }, - ipfs: { provider: ipfsProvider }, + ipfs: {}, ens: { addresses: { testnet: ensAddress, }, }, }, - config + { + ...config, + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: ipfsProvider, + }, + }, + ], + } ); }; @@ -247,7 +257,7 @@ describe("wasm-wrapper", () => { })) as Uint8Array; const decoder = new TextDecoder("utf8"); const text = decoder.decode(fileBuffer); - + expect(text).toEqual(expectedSchema); await expect(() => @@ -417,4 +427,4 @@ describe("wasm-wrapper", () => { expect(results).toContain(0); expect(results).not.toContain(2); }); -}); \ No newline at end of file +}); diff --git a/packages/js/client/src/__tests__/e2e/wasm-as.spec.ts b/packages/js/client/src/__tests__/e2e/wasm-as.spec.ts index c41723afeb..e354e6e148 100644 --- a/packages/js/client/src/__tests__/e2e/wasm-as.spec.ts +++ b/packages/js/client/src/__tests__/e2e/wasm-as.spec.ts @@ -1,7 +1,4 @@ -import { - PolywrapClientConfig, - createPolywrapClient, -} from "../../"; +import { PolywrapClientConfig, createPolywrapClient } from "../../"; import * as TestCases from "./test-cases"; import { makeMemoryStoragePlugin } from "./memory-storage"; import { @@ -10,7 +7,7 @@ import { stopTestEnvironment, runCLI, ensAddresses, - providers + providers, } from "@polywrap/test-env-js"; import { GetPathToTestWrappers } from "@polywrap/test-cases"; @@ -33,102 +30,106 @@ describe("wasm-as test cases", () => { }); const getClient = async (config?: Partial) => { - return createPolywrapClient({ - ethereum: { - networks: { - testnet: { - provider: ethProvider, + return createPolywrapClient( + { + ethereum: { + networks: { + testnet: { + provider: ethProvider, + }, }, }, - }, - ipfs: { provider: ipfsProvider }, - ens: { - addresses: { - testnet: ensAddress, + ipfs: {}, + ens: { + addresses: { + testnet: ensAddress, + }, }, }, - }, config); - } + { + ...config, + envs: [ + ...(config?.envs ?? []), + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: ipfsProvider, + }, + }, + ], + } + ); + }; it("asyncify", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/asyncify` - const wrapperUri = `fs/${wrapperPath}/build` + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/asyncify`; + const wrapperUri = `fs/${wrapperPath}/build`; await buildWrapper(wrapperPath); const client = await getClient({ - plugins: [{ - uri: "wrap://ens/memory-storage.polywrap.eth", - plugin: makeMemoryStoragePlugin({}), - }] - }) - - await TestCases.runAsyncifyTest( - client, wrapperUri - ); + plugins: [ + { + uri: "wrap://ens/memory-storage.polywrap.eth", + plugin: makeMemoryStoragePlugin({}), + }, + ], + }); + + await TestCases.runAsyncifyTest(client, wrapperUri); }); it("bigint-type", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/bigint-type` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/bigint-type`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runBigIntTypeTest( - await getClient(), wrapperUri - ); + await TestCases.runBigIntTypeTest(await getClient(), wrapperUri); }); it("bignumber-type", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/bignumber-type` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/bignumber-type`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runBigNumberTypeTest( - await getClient(), wrapperUri - ); + await TestCases.runBigNumberTypeTest(await getClient(), wrapperUri); }); it("bytes-type", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/bytes-type` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/bytes-type`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runBytesTypeTest( - await getClient(), wrapperUri - ); + await TestCases.runBytesTypeTest(await getClient(), wrapperUri); }); it("enum-types", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/enum-types` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/enum-types`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runEnumTypesTest( - await getClient(), wrapperUri - ); + await TestCases.runEnumTypesTest(await getClient(), wrapperUri); }); it("map-type", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/map-type` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/map-type`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runMapTypeTest( - await getClient(), wrapperUri - ); + await TestCases.runMapTypeTest(await getClient(), wrapperUri); }); it("reserved-words", async () => { const client = await getClient(); - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/reserved-words` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/reserved-words`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); const ensUri = wrapperUri; @@ -159,11 +160,11 @@ describe("wasm-as test cases", () => { }); it("implementations - e2e", async () => { - const interfacePath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-interface` + const interfacePath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-interface`; const interfaceUri = `fs/${interfacePath}/build`; - const implementationPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-wrapper` - const implementationUri = `wrap://fs/${implementationPath}/build` + const implementationPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-wrapper`; + const implementationUri = `wrap://fs/${implementationPath}/build`; await buildWrapper(interfacePath); await buildWrapper(implementationPath); @@ -178,14 +179,16 @@ describe("wasm-as test cases", () => { }); await TestCases.runImplementationsTest( - client, interfaceUri, implementationUri + client, + interfaceUri, + implementationUri ); }); it("implementations - getImplementations", async () => { - const interfaceUri = "wrap://ens/interface.eth" + const interfaceUri = "wrap://ens/interface.eth"; - const implementationPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-use-getImpl` + const implementationPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-use-getImpl`; const implementationUri = `wrap://fs/${implementationPath}/build`; await buildWrapper(implementationPath); @@ -195,19 +198,21 @@ describe("wasm-as test cases", () => { { interface: interfaceUri, implementations: [implementationUri], - } + }, ], }); await TestCases.runGetImplementationsTest( - client, interfaceUri, implementationUri + client, + interfaceUri, + implementationUri ); }); it("e2e Interface invoke method", async () => { const interfaceUri = "wrap://ens/interface.eth"; - const implementationPath = `${GetPathToTestWrappers()}/wasm-as/interface-invoke/test-implementation` + const implementationPath = `${GetPathToTestWrappers()}/wasm-as/interface-invoke/test-implementation`; const implementationUri = `fs/${implementationPath}/build`; // Build interface polywrapper @@ -227,9 +232,9 @@ describe("wasm-as test cases", () => { ], }); - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/interface-invoke/test-wrapper` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/interface-invoke/test-wrapper`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); const query = await client.query<{ @@ -255,78 +260,64 @@ describe("wasm-as test cases", () => { }); it("invalid type errors", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/invalid-types` - const wrapperUri = `fs/${wrapperPath}/build` + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/invalid-types`; + const wrapperUri = `fs/${wrapperPath}/build`; await buildWrapper(wrapperPath); - await TestCases.runInvalidTypesTest( - await getClient(), wrapperUri - ); + await TestCases.runInvalidTypesTest(await getClient(), wrapperUri); }); it("JSON-type", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/json-type` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/json-type`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runJsonTypeTest( - await getClient(), wrapperUri - ); + await TestCases.runJsonTypeTest(await getClient(), wrapperUri); }); it("large-types", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/large-types` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/large-types`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runLargeTypesTest( - await getClient(), wrapperUri - ); + await TestCases.runLargeTypesTest(await getClient(), wrapperUri); }); it("number-types under and overflows", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/number-types` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/number-types`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runNumberTypesTest( - await getClient(), wrapperUri - ); + await TestCases.runNumberTypesTest(await getClient(), wrapperUri); }); it("object-types", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/object-types` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/object-types`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runObjectTypesTest( - await getClient(), wrapperUri - ); + await TestCases.runObjectTypesTest(await getClient(), wrapperUri); }); it("simple-storage", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple-storage` - const wrapperUri = `fs/${wrapperPath}/build` - + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple-storage`; + const wrapperUri = `fs/${wrapperPath}/build`; + await buildWrapper(wrapperPath); - await TestCases.runSimpleStorageTest( - await getClient(), wrapperUri - ); + await TestCases.runSimpleStorageTest(await getClient(), wrapperUri); }); it("simple env", async () => { - const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple-env-types` - const wrapperUri = `fs/${wrapperPath}/build` + const wrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple-env-types`; + const wrapperUri = `fs/${wrapperPath}/build`; - await buildWrapper( - wrapperPath - ); + await buildWrapper(wrapperPath); await TestCases.runSimpleEnvTest( await await getClient({ @@ -339,16 +330,17 @@ describe("wasm-as test cases", () => { }, }, ], - }), wrapperUri + }), + wrapperUri ); - }) + }); it("complex env", async () => { - const baseWrapperEnvPaths = `${GetPathToTestWrappers()}/wasm-as/env-types` - const wrapperPath = `${baseWrapperEnvPaths}/main` - const externalWrapperPath = `${baseWrapperEnvPaths}/external` - const wrapperUri = `fs/${wrapperPath}/build` - const externalWrapperUri = `fs/${externalWrapperPath}/build` + const baseWrapperEnvPaths = `${GetPathToTestWrappers()}/wasm-as/env-types`; + const wrapperPath = `${baseWrapperEnvPaths}/main`; + const externalWrapperPath = `${baseWrapperEnvPaths}/external`; + const wrapperUri = `fs/${wrapperPath}/build`; + const externalWrapperUri = `fs/${externalWrapperPath}/build`; await buildWrapper(externalWrapperPath); await buildWrapper(wrapperPath); @@ -374,17 +366,18 @@ describe("wasm-as test cases", () => { uri: externalWrapperUri, env: { externalArray: [1, 2, 3], - externalString: "iamexternal" + externalString: "iamexternal", }, }, ], redirects: [ { from: "ens/externalenv.polywrap.eth", - to: externalWrapperUri - } - ] - }), wrapperUri + to: externalWrapperUri, + }, + ], + }), + wrapperUri ); - }) + }); }); diff --git a/packages/js/client/src/__tests__/e2e/workflow.spec.ts b/packages/js/client/src/__tests__/e2e/workflow.spec.ts index a8f557b98d..15a8f56e3e 100644 --- a/packages/js/client/src/__tests__/e2e/workflow.spec.ts +++ b/packages/js/client/src/__tests__/e2e/workflow.spec.ts @@ -4,9 +4,14 @@ import { initTestEnvironment, stopTestEnvironment, ensAddresses, - providers + providers, } from "@polywrap/test-env-js"; -import { createPolywrapClient, JobResult, PolywrapClient, PolywrapClientConfig } from "../.."; +import { + createPolywrapClient, + JobResult, + PolywrapClient, + PolywrapClientConfig, +} from "../.."; import { outPropWorkflow, sanityWorkflow } from "./workflow-test-cases"; jest.setTimeout(200000); @@ -34,14 +39,25 @@ describe("workflow", () => { }, defaultNetwork: "testnet", }, - ipfs: { provider: ipfsProvider }, + ipfs: {}, ens: { addresses: { testnet: ensAddress, }, }, }, - config + { + ...config, + envs: [ + ...(config?.envs ?? []), + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: ipfsProvider, + }, + }, + ], + } ); }; @@ -75,9 +91,7 @@ describe("workflow", () => { "cases.case1.0": async (data: unknown, error: unknown) => { expect(error).toBeFalsy(); expect(data).toBeTruthy(); - expect(data).toContain( - "0x" - ); + expect(data).toContain("0x"); }, "cases.case1.1": async (data: unknown, error: unknown) => { expect(error).toBeFalsy(); diff --git a/packages/js/client/src/default-client-config.ts b/packages/js/client/src/default-client-config.ts index c562929667..c88dea0000 100644 --- a/packages/js/client/src/default-client-config.ts +++ b/packages/js/client/src/default-client-config.ts @@ -29,13 +29,15 @@ export const getDefaultClientConfig = Tracer.traceFunc( "client-js: getDefaultClientConfig", (): ClientConfig => { return { - envs: [{ - uri: new Uri("wrap://ens/ipfs.polywrap.eth"), - env: { - provider: defaultIpfsProviders[0], - fallbackProviders: defaultIpfsProviders.slice(1), - } - }], + envs: [ + { + uri: new Uri("wrap://ens/ipfs.polywrap.eth"), + env: { + provider: defaultIpfsProviders[0], + fallbackProviders: defaultIpfsProviders.slice(1), + }, + }, + ], redirects: [], plugins: [ // IPFS is required for downloading Polywrap packages diff --git a/packages/js/client/src/pluginConfigs/Ipfs.ts b/packages/js/client/src/pluginConfigs/Ipfs.ts index 94df81f0cb..acf0f28c06 100644 --- a/packages/js/client/src/pluginConfigs/Ipfs.ts +++ b/packages/js/client/src/pluginConfigs/Ipfs.ts @@ -5,7 +5,4 @@ /// Types generated from @polywrap/ipfs-plugin-js build files: /// build/index.d.ts -export interface IpfsPluginConfig { - provider: string; - fallbackProviders?: string[]; -} +export type NoConfig = Record; diff --git a/packages/js/client/src/pluginConfigs/index.ts b/packages/js/client/src/pluginConfigs/index.ts index 042e4ff673..d6ee66e563 100644 --- a/packages/js/client/src/pluginConfigs/index.ts +++ b/packages/js/client/src/pluginConfigs/index.ts @@ -2,20 +2,24 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable prettier/prettier */ +import { NoConfig } from "./Ipfs"; import { EthereumPluginConfig } from "./Ethereum"; import { EnsResolverPluginConfig } from "./Ens"; interface PluginConfigs { + ipfs?: NoConfig; ethereum?: EthereumPluginConfig; ens?: EnsResolverPluginConfig; } const modules: Record = { + ipfs: "@polywrap/ipfs-plugin-js", ethereum: "@polywrap/ethereum-plugin-js", ens: "@polywrap/ens-resolver-plugin-js", }; const uris: Record = { + ipfs: "wrap://ens/ipfs.polywrap.eth", ethereum: "wrap://ens/ethereum.polywrap.eth", ens: "wrap://ens/ens-resolver.polywrap.eth", }; diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index 17a3f1c192..a2bcdc2d50 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -40,7 +40,7 @@ const getOptions = ( return options; }; -type NoConfig = Record; +export type NoConfig = Record; export class IpfsPlugin extends Module { public async cat(args: Args_cat, _client: Client): Promise { diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts index 31118d5c1d..64c33bfe46 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts @@ -41,7 +41,7 @@ export class IpfsResolverPlugin extends Module { cid: `${args.path}/${manifestSearchPattern}`, options: { timeout: this.env.timeouts?.tryResolveUri, - disableParallelRequests: this.env.disableParallelRequests + disableParallelRequests: this.env.disableParallelRequests, }, }, _client @@ -67,20 +67,20 @@ export class IpfsResolverPlugin extends Module { try { let provider: string | undefined = undefined; - if(!this.env.skipCheckIfExistsBeforeGetFile){ + if (!this.env.skipCheckIfExistsBeforeGetFile) { const resolveResult = await Ipfs_Module.resolve( { cid: args.path, options: { timeout: this.env.timeouts?.checkIfExists, - disableParallelRequests: this.env.disableParallelRequests + disableParallelRequests: this.env.disableParallelRequests, }, }, client ); - + const result = resolveResult.data; - + if (!result) { return null; } From 1937a72f18dba9a5488d71dbdd7239f468386b85 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 11:26:54 +0200 Subject: [PATCH 06/18] add fallback providers to ipfs plugin method options --- packages/js/plugins/ipfs/src/index.ts | 22 +++++++++++++++------ packages/js/plugins/ipfs/src/schema.graphql | 4 ++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index a2bcdc2d50..d9dc762fd5 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -3,8 +3,8 @@ import { Args_resolve, Args_addFile, Args_cat, - Ipfs_Options, Ipfs_ResolveResult, + Options, manifest, Env, } from "./wrap"; @@ -19,10 +19,7 @@ const isNullOrUndefined = (arg: unknown) => { return arg === undefined || arg === null; }; -const getOptions = ( - args: Ipfs_Options | undefined | null, - env: Env -): Ipfs_Options => { +const getOptions = (args: Options | undefined | null, env: Env): Options => { const options = args || {}; if (isNullOrUndefined(options.disableParallelRequests)) { @@ -37,6 +34,10 @@ const getOptions = ( options.provider = env.provider; } + if (isNullOrUndefined(options.fallbackProviders)) { + options.fallbackProviders = env.fallbackProviders; + } + return options; }; @@ -101,7 +102,7 @@ export class IpfsPlugin extends Module { provider: string, options: unknown ) => Promise, - options?: Ipfs_Options + options?: Options ): Promise { const defaultIpfsClient = createIpfsClient(this.env.provider); @@ -129,6 +130,15 @@ export class IpfsPlugin extends Module { defaultProvider = options.provider; } + //insert fallback providers before the env providers and fallbacks + if (options.fallbackProviders) { + providers = [ + providers[0], + ...options.fallbackProviders, + ...providers.slice(1), + ]; + } + return await execFallbacks( operation, ipfs, diff --git a/packages/js/plugins/ipfs/src/schema.graphql b/packages/js/plugins/ipfs/src/schema.graphql index b2146cd4d5..ec5be0de60 100644 --- a/packages/js/plugins/ipfs/src/schema.graphql +++ b/packages/js/plugins/ipfs/src/schema.graphql @@ -19,5 +19,9 @@ type Env { fallbackProviders: [String!] } +type Options implements Ipfs_Options { + fallbackProviders: [String!] +} + type Module implements Ipfs_Module { } \ No newline at end of file From c56d690cd204a85dfe0f2e7a33b6fac6af165ab7 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 11:46:30 +0200 Subject: [PATCH 07/18] lint fix --- packages/js/plugins/ipfs/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index d9dc762fd5..42adf6fdea 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -10,6 +10,7 @@ import { } from "./wrap"; import { IpfsClient } from "./utils/IpfsClient"; import { execSimple, execFallbacks } from "./utils/exec"; + import { Client, PluginFactory } from "@polywrap/core-js"; // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/naming-convention @@ -130,7 +131,7 @@ export class IpfsPlugin extends Module { defaultProvider = options.provider; } - //insert fallback providers before the env providers and fallbacks + // insert fallback providers before the env providers and fallbacks if (options.fallbackProviders) { providers = [ providers[0], From 3c2a56203927c2a6b0f1155fae7ce26973c2290c Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 15:40:10 +0200 Subject: [PATCH 08/18] IPFS plugin timeout test --- .../js/plugins/ipfs/src/__tests__/e2e.spec.ts | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts index 2fb8b822d3..b17b9906e9 100644 --- a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts @@ -1,3 +1,4 @@ +import { InvokeResult } from "@polywrap/core-js"; import { PolywrapClient } from "@polywrap/client-js"; import { initTestEnvironment, @@ -93,4 +94,72 @@ describe("IPFS Plugin", () => { expect(contentsBuffer).toEqual(addedFileBuffer); }); + + it("Should timeout within a specified amount of time - env and options", async () => { + const createRacePromise = ( + timeout: number + ): Promise> => { + return new Promise>((resolve) => + setTimeout(() => { + resolve({ + data: Uint8Array.from([1, 2, 3, 4]), + error: undefined, + }); + }, timeout) + ); + }; + + const altClient = new PolywrapClient({ + plugins: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + plugin: ipfsPlugin({}), + }, + ], + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: providers.ipfs, + timeout: 1000, + }, + }, + ], + }); + + const nonExistentFileCid = "Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + + const catPromise = Ipfs_Module.cat({ cid: nonExistentFileCid }, altClient); + + let racePromise = createRacePromise(1100); + + const result = await Promise.race([catPromise, racePromise]); + + expect(result).toBeTruthy(); + expect(result.data).toBeFalsy(); + expect(result.error).toBeTruthy(); + expect(result.error?.stack).toMatch("Timeout has been reached"); + expect(result.error?.stack).toMatch("Timeout: 1000"); + + const catPromiseWithTimeoutOverride = Ipfs_Module.cat( + { + cid: nonExistentFileCid, + options: { timeout: 500 }, + }, + altClient + ); + + racePromise = createRacePromise(600); + + const resultForOverride = await Promise.race([ + catPromiseWithTimeoutOverride, + racePromise, + ]); + + expect(resultForOverride).toBeTruthy(); + expect(resultForOverride.data).toBeFalsy(); + expect(resultForOverride.error).toBeTruthy(); + expect(resultForOverride.error?.stack).toMatch("Timeout has been reached"); + expect(resultForOverride.error?.stack).toMatch("Timeout: 500"); + }); }); From 17baf6ddbbb86f39be2e2770097e9bb84698424a Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 15:55:51 +0200 Subject: [PATCH 09/18] fix ethereum e2e tests --- .../js/plugins/ethereum/src/__tests__/e2e.spec.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/js/plugins/ethereum/src/__tests__/e2e.spec.ts b/packages/js/plugins/ethereum/src/__tests__/e2e.spec.ts index b7f534c117..2c009beb29 100644 --- a/packages/js/plugins/ethereum/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/ethereum/src/__tests__/e2e.spec.ts @@ -48,6 +48,15 @@ describe("Ethereum Plugin", () => { registrarAddress = ensAddresses.registrarAddress; client = new PolywrapClient({ + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: providers.ipfs, + fallbackProviders: defaultIpfsProviders, + }, + }, + ], plugins: [ { uri: "wrap://ens/ethereum.polywrap.eth", @@ -65,10 +74,7 @@ describe("Ethereum Plugin", () => { }, { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ - provider: providers.ipfs, - fallbackProviders: defaultIpfsProviders, - }), + plugin: ipfsPlugin({}), }, { uri: "wrap://ens/ens-resolver.polywrap.eth", From 37180dbcf2db75ceeced961d56d6f8ae37aa91c3 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 19:12:10 +0200 Subject: [PATCH 10/18] ipfs resolver timeout tests --- .../ipfs-resolver/src/__tests__/e2e.spec.ts | 161 +++++++++++++++--- 1 file changed, 141 insertions(+), 20 deletions(-) diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts index 866975f5bc..a2d87d8edf 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts @@ -11,37 +11,25 @@ import { ipfsResolverPlugin } from ".."; import { ipfsPlugin } from "@polywrap/ipfs-plugin-js"; import { IpfsClient } from "./helpers/IpfsClient"; import { createIpfsClient } from "./helpers/createIpfsClient"; +import { InvokeResult } from "@polywrap/core-js"; jest.setTimeout(300000); describe("IPFS Resolver Plugin", () => { - let client: PolywrapClient; + let ipfsResolverUri = "wrap://ens/ipfs-resolver.polywrap.eth"; let ipfs: IpfsClient; let wrapperIpfsCid: string; - beforeAll(async () => { - await initTestEnvironment(); - - ipfs = createIpfsClient(providers.ipfs); - - let { ipfsCid } = await buildAndDeployWrapper({ - wrapperAbsPath: `${GetPathToTestWrappers()}/wasm-as/simple-storage`, - ipfsProvider: providers.ipfs, - ethereumProvider: providers.ethereum, - ensName: "cool.wrapper.eth", - }); - - wrapperIpfsCid = ipfsCid; - - client = new PolywrapClient({ + const getClientConfigWithIpfsResolverEnv = (env: Record) => { + return { plugins: [ { uri: "wrap://ens/ipfs.polywrap.eth", plugin: ipfsPlugin({}), }, { - uri: "wrap://ens/ipfs-resolver.polywrap.eth", + uri: ipfsResolverUri, plugin: ipfsResolverPlugin({}), }, ], @@ -54,12 +42,25 @@ describe("IPFS Resolver Plugin", () => { }, { uri: "wrap://ens/ipfs-resolver.polywrap.eth", - env: { - skipCheckIfExistsBeforeGetFile: false, - }, + env: env, }, ], + }; + }; + + beforeAll(async () => { + await initTestEnvironment(); + + ipfs = createIpfsClient(providers.ipfs); + + let { ipfsCid } = await buildAndDeployWrapper({ + wrapperAbsPath: `${GetPathToTestWrappers()}/wasm-as/simple-storage`, + ipfsProvider: providers.ipfs, + ethereumProvider: providers.ethereum, + ensName: "cool.wrapper.eth", }); + + wrapperIpfsCid = ipfsCid; }); afterAll(async () => { @@ -67,6 +68,8 @@ describe("IPFS Resolver Plugin", () => { }); it("Should successfully resolve a deployed wrapper - e2e", async () => { + const client = new PolywrapClient(getClientConfigWithIpfsResolverEnv({})); + const wrapperUri = `ipfs/${wrapperIpfsCid}`; const resolution = await client.resolveUri(wrapperUri); @@ -84,4 +87,122 @@ describe("IPFS Resolver Plugin", () => { const info = await resolution.wrapper?.getManifest({}, client); expect(info?.name).toBe("SimpleStorage"); }); + + const createRacePromise = ( + timeout: number + ): Promise> => { + return new Promise>((resolve) => + setTimeout(() => { + resolve({ + data: Uint8Array.from([1, 2, 3, 4]), + error: undefined, + }); + }, timeout) + ); + }; + + it("Should properly timeout - getFile", async () => { + const runGetFileTimeoutTestWithEnv = async ( + env: Record, + timeout: number + ) => { + const nonExistentFileCid = "Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const client = new PolywrapClient(getClientConfigWithIpfsResolverEnv(env)); + + const getFilePromise = client.invoke({ + uri: ipfsResolverUri, + method: "getFile", + args: { + path: nonExistentFileCid, + }, + }); + + const fasterRacePromise = createRacePromise(timeout - 100); + const slowerRacePromise = createRacePromise(timeout + 100); + + const fasterRaceResult = await Promise.race([ + fasterRacePromise, + getFilePromise, + ]); + const slowerRaceResult = await Promise.race([ + getFilePromise, + slowerRacePromise, + ]); + + expect(fasterRaceResult.data).toStrictEqual((await fasterRacePromise).data); + expect(slowerRaceResult.data).toStrictEqual((await getFilePromise).data); + }; + + const timeout = 1000; + + await runGetFileTimeoutTestWithEnv( + { + timeouts: { + getFile: timeout, + checkIfExists: timeout, + tryResolveUri: timeout, + }, + }, + timeout + ); + + await runGetFileTimeoutTestWithEnv( + { + timeouts: { + getFile: timeout, + checkIfExists: timeout, + tryResolveUri: timeout, + }, + skipCheckIfExistsBeforeGetFile: true + }, + timeout + ); + }); + + it("Should properly timeout - tryResolveUri", async () => { + const runTryResolveUriTimeoutTestWithEnv = async ( + env: Record, + timeout: number + ) => { + const nonExistentFileCid = "Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const client = new PolywrapClient(getClientConfigWithIpfsResolverEnv(env)); + + const getFilePromise = client.invoke({ + uri: ipfsResolverUri, + method: "tryResolveUri", + args: { + authority: "ipfs", + path: nonExistentFileCid, + }, + }); + + const fasterRacePromise = createRacePromise(timeout - 100); + const slowerRacePromise = createRacePromise(timeout + 100); + + const fasterRaceResult = await Promise.race([ + fasterRacePromise, + getFilePromise, + ]); + const slowerRaceResult = await Promise.race([ + getFilePromise, + slowerRacePromise, + ]); + + expect(fasterRaceResult.data).toStrictEqual((await fasterRacePromise).data); + expect(slowerRaceResult.data).toStrictEqual((await getFilePromise).data); + }; + + const timeout = 1000; + + await runTryResolveUriTimeoutTestWithEnv( + { + timeouts: { + getFile: timeout, + checkIfExists: timeout, + tryResolveUri: timeout, + }, + }, + timeout + ); + }); }); From ac3110c7522c4c70ed3cbaaad6323df3e577d411 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 20:04:09 +0200 Subject: [PATCH 11/18] ipfs plugin test method options provider --- .../js/plugins/ipfs/src/__tests__/e2e.spec.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts index b17b9906e9..beb05b5d5d 100644 --- a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts @@ -162,4 +162,48 @@ describe("IPFS Plugin", () => { expect(resultForOverride.error?.stack).toMatch("Timeout has been reached"); expect(resultForOverride.error?.stack).toMatch("Timeout: 500"); }); + + it("Should use provider from method options", async () => { + const clientWithBadProvider = new PolywrapClient({ + plugins: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + plugin: ipfsPlugin({}), + }, + ], + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: "this-provider-doesnt-exist", + }, + }, + ], + }); + + const catResult = await Ipfs_Module.cat( + { + cid: sampleFileIpfsInfo.hash.toString(), + options: { provider: providers.ipfs }, + }, + clientWithBadProvider + ); + + expect(catResult.error).toBeFalsy(); + expect(catResult.data).toEqual(sampleFileBuffer); + + const resolveResult = await Ipfs_Module.resolve( + { + cid: sampleFileIpfsInfo.hash.toString(), + options: { provider: providers.ipfs }, + }, + clientWithBadProvider + ); + + expect(resolveResult.error).toBeFalsy(); + expect(resolveResult.data).toEqual({ + cid: `/ipfs/${sampleFileIpfsInfo.hash.toString()}`, + provider: providers.ipfs, + }); + }); }); From 3b83f06c2045f65123d301619e8a2356fda3b62a Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 21 Jul 2022 20:26:36 +0200 Subject: [PATCH 12/18] fix failing tests --- .../ens-resolver/src/__tests__/e2e.spec.ts | 39 ++++++++++--------- .../src/__tests__/e2e.spec.ts | 16 +++++--- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/e2e.spec.ts b/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/e2e.spec.ts index 910ed05684..75e51a91dd 100644 --- a/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/uri-resolvers/ens-resolver/src/__tests__/e2e.spec.ts @@ -5,7 +5,7 @@ import { ensAddresses, initTestEnvironment, providers, - stopTestEnvironment + stopTestEnvironment, } from "@polywrap/test-env-js"; import { ensResolverPlugin } from ".."; @@ -30,40 +30,46 @@ describe("ENS Resolver Plugin", () => { wrapperAbsPath: wrapperAbsPath, ipfsProvider: providers.ipfs, ethereumProvider: providers.ethereum, - ensName: "cool.wrapper.eth" + ensName: "cool.wrapper.eth", }); wrapperEnsDomain = ensDomain; client = new PolywrapClient({ - plugins: [ + envs: [ { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ + env: { provider: providers.ipfs, fallbackProviders: defaultIpfsProviders - }) + } + } + ], + plugins: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + plugin: ipfsPlugin({}), }, { uri: "wrap://ens/ethereum.polywrap.eth", plugin: ethereumPlugin({ networks: { testnet: { - provider: providers.ethereum - } + provider: providers.ethereum, + }, }, - defaultNetwork: "testnet" - }) + defaultNetwork: "testnet", + }), }, { uri: "wrap://ens/ens-resolver.polywrap.eth", plugin: ensResolverPlugin({ addresses: { - testnet: ensAddresses.ensAddress - } - }) - } - ] + testnet: ensAddresses.ensAddress, + }, + }), + }, + ], }); }); @@ -87,10 +93,7 @@ describe("ENS Resolver Plugin", () => { expect(schema).toEqual(expectedSchema); - const manifest = await resolution.wrapper?.getManifest( - {}, - client - ); + const manifest = await resolution.wrapper?.getManifest({}, client); expect(manifest?.name).toBe("SimpleStorage"); }); diff --git a/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/e2e.spec.ts b/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/e2e.spec.ts index e6f7c7f636..8e38a8dbdc 100644 --- a/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/uri-resolvers/file-system-resolver/src/__tests__/e2e.spec.ts @@ -27,17 +27,23 @@ describe("Filesystem plugin", () => { await initTestEnvironment(); const config: Partial = { + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: providers.ipfs, + fallbackProviders: defaultIpfsProviders, + }, + }, + ], plugins: [ { uri: "wrap://ens/fs-resolver.polywrap.eth", - plugin: fileSystemResolverPlugin({ }), + plugin: fileSystemResolverPlugin({}), }, { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ - provider: providers.ipfs, - fallbackProviders: defaultIpfsProviders, - }), + plugin: ipfsPlugin({}), }, { uri: "wrap://ens/ens-resolver.polywrap.eth", From 076b92d5213e7cdd20037f819f4345a4d60a9abb Mon Sep 17 00:00:00 2001 From: Pileks Date: Fri, 22 Jul 2022 10:35:41 +0200 Subject: [PATCH 13/18] ipfs plugin fallback provider in method options, tests --- packages/interfaces/ipfs/src/schema.graphql | 5 ++ .../js/plugins/ipfs/src/__tests__/e2e.spec.ts | 50 +++++++++++++++++++ packages/js/plugins/ipfs/src/index.ts | 9 ++-- packages/js/plugins/ipfs/src/schema.graphql | 4 -- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/interfaces/ipfs/src/schema.graphql b/packages/interfaces/ipfs/src/schema.graphql index faa5fc9c9e..f54a6268f3 100644 --- a/packages/interfaces/ipfs/src/schema.graphql +++ b/packages/interfaces/ipfs/src/schema.graphql @@ -23,6 +23,11 @@ type Options { """ provider: String + """ + Fallback IPFS providers + """ + fallbackProviders: [String!] + """ Disable querying providers in parallel when resolving URIs """ diff --git a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts index beb05b5d5d..8cbb88e4f5 100644 --- a/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts @@ -206,4 +206,54 @@ describe("IPFS Plugin", () => { provider: providers.ipfs, }); }); + + it("Should use fallback provider from method options", async () => { + const clientWithBadProvider = new PolywrapClient({ + plugins: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + plugin: ipfsPlugin({}), + }, + ], + envs: [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: "this-provider-doesnt-exist", + }, + }, + ], + }); + + const catResult = await Ipfs_Module.cat( + { + cid: sampleFileIpfsInfo.hash.toString(), + options: { + provider: "this-provider-also-doesnt-exist", + fallbackProviders: [providers.ipfs], + }, + }, + clientWithBadProvider + ); + + expect(catResult.error).toBeFalsy(); + expect(catResult.data).toEqual(sampleFileBuffer); + + const resolveResult = await Ipfs_Module.resolve( + { + cid: sampleFileIpfsInfo.hash.toString(), + options: { + provider: "this-provider-also-doesnt-exist", + fallbackProviders: [providers.ipfs], + }, + }, + clientWithBadProvider + ); + + expect(resolveResult.error).toBeFalsy(); + expect(resolveResult.data).toEqual({ + cid: `/ipfs/${sampleFileIpfsInfo.hash.toString()}`, + provider: providers.ipfs, + }); + }); }); diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index 42adf6fdea..54896f6b70 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -4,7 +4,7 @@ import { Args_addFile, Args_cat, Ipfs_ResolveResult, - Options, + Ipfs_Options, manifest, Env, } from "./wrap"; @@ -20,7 +20,10 @@ const isNullOrUndefined = (arg: unknown) => { return arg === undefined || arg === null; }; -const getOptions = (args: Options | undefined | null, env: Env): Options => { +const getOptions = ( + args: Ipfs_Options | undefined | null, + env: Env +): Ipfs_Options => { const options = args || {}; if (isNullOrUndefined(options.disableParallelRequests)) { @@ -103,7 +106,7 @@ export class IpfsPlugin extends Module { provider: string, options: unknown ) => Promise, - options?: Options + options?: Ipfs_Options ): Promise { const defaultIpfsClient = createIpfsClient(this.env.provider); diff --git a/packages/js/plugins/ipfs/src/schema.graphql b/packages/js/plugins/ipfs/src/schema.graphql index ec5be0de60..b2146cd4d5 100644 --- a/packages/js/plugins/ipfs/src/schema.graphql +++ b/packages/js/plugins/ipfs/src/schema.graphql @@ -19,9 +19,5 @@ type Env { fallbackProviders: [String!] } -type Options implements Ipfs_Options { - fallbackProviders: [String!] -} - type Module implements Ipfs_Module { } \ No newline at end of file From fa06a4c1a52f76c5782a3282651962337d409c91 Mon Sep 17 00:00:00 2001 From: Pileks Date: Tue, 9 Aug 2022 14:15:11 +0200 Subject: [PATCH 14/18] remove providers from getClientwithEnsAndIpfs --- .../js/client/src/__tests__/utils/getClientWithEnsAndIpfs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/client/src/__tests__/utils/getClientWithEnsAndIpfs.ts b/packages/js/client/src/__tests__/utils/getClientWithEnsAndIpfs.ts index 63b530d775..2fc0d40f6e 100644 --- a/packages/js/client/src/__tests__/utils/getClientWithEnsAndIpfs.ts +++ b/packages/js/client/src/__tests__/utils/getClientWithEnsAndIpfs.ts @@ -14,7 +14,7 @@ export const getClientWithEnsAndIpfs = async ( }, defaultNetwork: "testnet", }, - ipfs: { provider: providers.ipfs }, + ipfs: {}, ens: { addresses: { testnet: ensAddresses.ensAddress, From bddda80e869ef64816707268feb336496b1df1c9 Mon Sep 17 00:00:00 2001 From: krisbitney Date: Wed, 7 Sep 2022 10:11:42 +0500 Subject: [PATCH 15/18] updated default client config after merge --- .../src/bundles/default-client-config.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/js/client-config-builder/src/bundles/default-client-config.ts b/packages/js/client-config-builder/src/bundles/default-client-config.ts index 0e5071ccb6..35674af07c 100644 --- a/packages/js/client-config-builder/src/bundles/default-client-config.ts +++ b/packages/js/client-config-builder/src/bundles/default-client-config.ts @@ -33,6 +33,13 @@ export const getDefaultClientConfig = (): ClientConfig => { provider: "https://api.thegraph.com", }, }, + { + uri: new Uri("wrap://ens/ipfs.polywrap.eth"), + env: { + provider: defaultIpfsProviders[0], + fallbackProviders: defaultIpfsProviders.slice(1), + }, + }, ], redirects: [ { @@ -52,10 +59,7 @@ export const getDefaultClientConfig = (): ClientConfig => { // IPFS is required for downloading Polywrap packages { uri: new Uri("wrap://ens/ipfs.polywrap.eth"), - plugin: ipfsPlugin({ - provider: defaultIpfsProviders[0], - fallbackProviders: defaultIpfsProviders.slice(1), - }), + plugin: ipfsPlugin({}), }, // ENS is required for resolving domain to IPFS hashes { From 82ca1a7e29315da4308741e64c9cf2e756743f35 Mon Sep 17 00:00:00 2001 From: krisbitney Date: Wed, 7 Sep 2022 12:11:59 +0500 Subject: [PATCH 16/18] updated @polywrap/react tests to use ipfs plugin env instead of plugin config --- .../react/src/__tests__/app/SimpleStorage.tsx | 5 +++- .../src/__tests__/{plugins.ts => config.ts} | 24 ++++++++++++------- .../react/src/__tests__/integration.spec.tsx | 11 +++++---- .../src/__tests__/usePolywrapClient.spec.tsx | 10 +++++--- .../src/__tests__/usePolywrapInvoke.spec.tsx | 9 ++++--- .../src/__tests__/usePolywrapQuery.spec.tsx | 9 ++++--- 6 files changed, 46 insertions(+), 22 deletions(-) rename packages/js/react/src/__tests__/{plugins.ts => config.ts} (78%) diff --git a/packages/js/react/src/__tests__/app/SimpleStorage.tsx b/packages/js/react/src/__tests__/app/SimpleStorage.tsx index 12d7eaa503..5698884977 100644 --- a/packages/js/react/src/__tests__/app/SimpleStorage.tsx +++ b/packages/js/react/src/__tests__/app/SimpleStorage.tsx @@ -2,6 +2,7 @@ import { usePolywrapQuery, PolywrapProvider, usePolywrapClient, createPolywrapPr import { PluginRegistration } from "@polywrap/client-js"; // eslint-disable-next-line import/no-extraneous-dependencies import React from "react"; +import { Env } from "@polywrap/core-js"; const SimpleStorage = ({ uri }: { uri: string }) => { const { execute: deployContract, data: deployData } = usePolywrapQuery<{ @@ -75,14 +76,16 @@ const SimpleStorage = ({ uri }: { uri: string }) => { const CustomProvider = createPolywrapProvider("custom"); export const SimpleStorageContainer = ({ + envs, plugins, ensUri, }: { + envs: Env[] plugins: PluginRegistration[]; ensUri: string; }) => ( - + diff --git a/packages/js/react/src/__tests__/plugins.ts b/packages/js/react/src/__tests__/config.ts similarity index 78% rename from packages/js/react/src/__tests__/plugins.ts rename to packages/js/react/src/__tests__/config.ts index 63b69c44f7..224ac9932f 100644 --- a/packages/js/react/src/__tests__/plugins.ts +++ b/packages/js/react/src/__tests__/config.ts @@ -1,13 +1,12 @@ -import { PluginRegistration } from "@polywrap/core-js"; -import { defaultIpfsProviders } from "@polywrap/client-config-builder-js"; +import { Env, PluginRegistration } from "@polywrap/core-js"; import { plugin as ensResolverPlugin } from "@polywrap/ens-resolver-plugin-js"; import { Connection, Connections, plugin as ethereumPlugin } from "@polywrap/ethereum-plugin-js"; import { plugin as ipfsPlugin } from "@polywrap/ipfs-plugin-js"; +import { defaultIpfsProviders } from "@polywrap/client-config-builder-js"; export function createPlugins( ensAddress: string, - ethereumProvider: string, - ipfsProvider: string + ethereumProvider: string ): PluginRegistration[] { return [ { @@ -22,10 +21,7 @@ export function createPlugins( }, { uri: "wrap://ens/ipfs.polywrap.eth", - plugin: ipfsPlugin({ - provider: ipfsProvider, - fallbackProviders: defaultIpfsProviders, - }), + plugin: ipfsPlugin({}), }, { uri: "wrap://ens/ens-resolver.polywrap.eth", @@ -36,4 +32,16 @@ export function createPlugins( }), }, ]; +} + +export function createEnvs(ipfsProvider: string): Env[] { + return [ + { + uri: "wrap://ens/ipfs.polywrap.eth", + env: { + provider: ipfsProvider, + fallbackProviders: defaultIpfsProviders, + } + } + ]; } \ No newline at end of file diff --git a/packages/js/react/src/__tests__/integration.spec.tsx b/packages/js/react/src/__tests__/integration.spec.tsx index 976175f753..bbf4349363 100644 --- a/packages/js/react/src/__tests__/integration.spec.tsx +++ b/packages/js/react/src/__tests__/integration.spec.tsx @@ -1,6 +1,6 @@ import { createPolywrapProvider } from ".."; import { SimpleStorageContainer } from "./app/SimpleStorage"; -import { createPlugins } from "./plugins"; +import { createEnvs, createPlugins } from "./config"; import { initTestEnvironment, @@ -10,7 +10,7 @@ import { providers } from "@polywrap/test-env-js"; import { GetPathToTestWrappers } from "@polywrap/test-cases"; -import { PluginRegistration } from "@polywrap/core-js"; +import { Env, PluginRegistration } from "@polywrap/core-js"; // eslint-disable-next-line import/no-extraneous-dependencies import React from "react"; @@ -18,6 +18,7 @@ import { render, fireEvent, screen, waitFor } from "@testing-library/react"; jest.setTimeout(360000); describe("Polywrap React Integration", () => { + let envs: Env[]; let plugins: PluginRegistration[]; let ensUri: string; let wrapper: { @@ -28,7 +29,9 @@ describe("Polywrap React Integration", () => { beforeAll(async () => { await initTestEnvironment(); - plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum, providers.ipfs); + envs = createEnvs(providers.ipfs); + + plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum); wrapper = await buildAndDeployWrapper({ wrapperAbsPath: `${GetPathToTestWrappers()}/wasm-as/simple-storage`, @@ -44,7 +47,7 @@ describe("Polywrap React Integration", () => { }); it("Deploys, read and write on Smart Contract ", async () => { - render(); + render(); fireEvent.click(screen.getByText("Deploy")); await waitFor(() => screen.getByText(/0x/), { timeout: 30000 }); diff --git a/packages/js/react/src/__tests__/usePolywrapClient.spec.tsx b/packages/js/react/src/__tests__/usePolywrapClient.spec.tsx index baa7359632..212f2a554a 100644 --- a/packages/js/react/src/__tests__/usePolywrapClient.spec.tsx +++ b/packages/js/react/src/__tests__/usePolywrapClient.spec.tsx @@ -4,9 +4,9 @@ import { createPolywrapProvider, usePolywrapClient } from ".."; -import { createPlugins } from "./plugins"; +import { createPlugins, createEnvs } from "./config"; -import { PluginRegistration } from "@polywrap/core-js"; +import { Env, PluginRegistration } from "@polywrap/core-js"; import { ensAddresses, providers, @@ -23,17 +23,21 @@ import { jest.setTimeout(360000); describe("usePolywrapClient hook", () => { + let envs: Env[]; let plugins: PluginRegistration[]; let WrapperProvider: RenderHookOptions; beforeAll(async () => { await initTestEnvironment(); - plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum, providers.ipfs); + envs = createEnvs(providers.ipfs); + + plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum); WrapperProvider = { wrapper: PolywrapProvider, initialProps: { + envs, plugins, }, }; diff --git a/packages/js/react/src/__tests__/usePolywrapInvoke.spec.tsx b/packages/js/react/src/__tests__/usePolywrapInvoke.spec.tsx index 2f2783fa7e..95529d4d2b 100644 --- a/packages/js/react/src/__tests__/usePolywrapInvoke.spec.tsx +++ b/packages/js/react/src/__tests__/usePolywrapInvoke.spec.tsx @@ -4,9 +4,9 @@ import { createPolywrapProvider, } from ".."; import { UsePolywrapInvokeProps } from "../invoke"; -import { createPlugins } from "./plugins"; +import { createPlugins, createEnvs } from "./config"; -import { PluginRegistration } from "@polywrap/core-js"; +import { Env, PluginRegistration } from "@polywrap/core-js"; import { initTestEnvironment, stopTestEnvironment, @@ -28,6 +28,7 @@ jest.setTimeout(360000); describe("usePolywrapInvoke hook", () => { let uri: string; let envUri: string; + let envs: Env[]; let plugins: PluginRegistration[]; let WrapperProvider: RenderHookOptions; @@ -48,10 +49,12 @@ describe("usePolywrapInvoke hook", () => { uri = `ens/testnet/${ensDomain}`; envUri = `ens/testnet/${envEnsDomain}`; - plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum, providers.ipfs); + envs = createEnvs(providers.ipfs); + plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum); WrapperProvider = { wrapper: PolywrapProvider, initialProps: { + envs, plugins, }, }; diff --git a/packages/js/react/src/__tests__/usePolywrapQuery.spec.tsx b/packages/js/react/src/__tests__/usePolywrapQuery.spec.tsx index a555bfc8d8..479388c973 100644 --- a/packages/js/react/src/__tests__/usePolywrapQuery.spec.tsx +++ b/packages/js/react/src/__tests__/usePolywrapQuery.spec.tsx @@ -6,9 +6,9 @@ import { import { UsePolywrapQueryProps } from "../query" -import { createPlugins } from "./plugins"; +import { createPlugins, createEnvs } from "./config"; -import { PluginRegistration } from "@polywrap/core-js"; +import { Env, PluginRegistration } from "@polywrap/core-js"; import { initTestEnvironment, stopTestEnvironment, @@ -30,6 +30,7 @@ jest.setTimeout(360000); describe("usePolywrapQuery hook", () => { let uri: string; let envUri: string; + let envs: Env[]; let plugins: PluginRegistration[]; let WrapperProvider: RenderHookOptions; @@ -50,10 +51,12 @@ describe("usePolywrapQuery hook", () => { uri = `ens/testnet/${ensDomain}`; envUri = `ens/testnet/${envEnsDomain}`; - plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum, providers.ipfs); + envs = createEnvs(providers.ipfs); + plugins = createPlugins(ensAddresses.ensAddress, providers.ethereum); WrapperProvider = { wrapper: PolywrapProvider, initialProps: { + envs, plugins, }, }; From 9fc044464fe28306425864fa9231d7f5e74adbd2 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 8 Sep 2022 00:01:24 +0200 Subject: [PATCH 17/18] rename skipCheckIfExistsBeforeGetFile to skipCheckIfExists --- .../uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts | 2 +- packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts | 2 +- .../js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts index 459d13041f..464b564c86 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/__tests__/e2e.spec.ts @@ -145,7 +145,7 @@ describe("IPFS Plugin", () => { checkIfExists: timeout, tryResolveUri: timeout, }, - skipCheckIfExistsBeforeGetFile: true + skipCheckIfExists: true }, timeout ); diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts index 64c33bfe46..5daa7bf78d 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts @@ -67,7 +67,7 @@ export class IpfsResolverPlugin extends Module { try { let provider: string | undefined = undefined; - if (!this.env.skipCheckIfExistsBeforeGetFile) { + if (!this.env.skipCheckIfExists) { const resolveResult = await Ipfs_Module.resolve( { cid: args.path, diff --git a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql index c9ce7dc1e5..10024abd4a 100644 --- a/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql +++ b/packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql @@ -11,7 +11,7 @@ type Env { """ Determines whether the plugin should try to resolve a file (check for its existence) or immediately get the file """ - skipCheckIfExistsBeforeGetFile: Boolean, + skipCheckIfExists: Boolean, """ Timeouts for methods """ From ce9809b9db0043090e503308c462d191b6c7d749 Mon Sep 17 00:00:00 2001 From: Pileks Date: Thu, 8 Sep 2022 00:17:16 +0200 Subject: [PATCH 18/18] set default timeout for ipfs plugin actions --- packages/js/plugins/ipfs/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/js/plugins/ipfs/src/index.ts b/packages/js/plugins/ipfs/src/index.ts index 54896f6b70..18aba44ddb 100644 --- a/packages/js/plugins/ipfs/src/index.ts +++ b/packages/js/plugins/ipfs/src/index.ts @@ -31,7 +31,8 @@ const getOptions = ( } if (isNullOrUndefined(options.timeout)) { - options.timeout = env.timeout; + // Default to a 5000ms timeout when none is provided + options.timeout = env.timeout ?? 5000; } if (isNullOrUndefined(options.provider)) {