Skip to content

Commit

Permalink
Merge pull request #1060 from polywrap/pileks-ipfs-config-env
Browse files Browse the repository at this point in the history
IPFS Plugin - move config into envs + method options
  • Loading branch information
dOrgJelli authored Sep 15, 2022
2 parents 796cf8a + ce9809b commit c377c85
Show file tree
Hide file tree
Showing 23 changed files with 572 additions and 136 deletions.
13 changes: 10 additions & 3 deletions packages/cli/src/lib/helpers/client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,6 +18,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",
Expand Down Expand Up @@ -45,10 +47,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 });
Expand Down
17 changes: 13 additions & 4 deletions packages/cli/src/lib/test-env/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} 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<PolywrapClientConfig>
Expand Down Expand Up @@ -41,10 +42,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",
Expand All @@ -56,7 +54,18 @@ export async function getTestEnvClientConfig(): Promise<
},
];

const envs: Env[] = [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: ipfsProvider,
fallbackProviders: defaultIpfsProviders,
},
},
];

return {
plugins,
envs,
};
}
5 changes: 5 additions & 0 deletions packages/interfaces/ipfs/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Options {
"""
provider: String

"""
Fallback IPFS providers
"""
fallbackProviders: [String!]

"""
Disable querying providers in parallel when resolving URIs
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export const getDefaultClientConfig = (
provider: "https://api.thegraph.com",
},
},
{
uri: new Uri("wrap://ens/ipfs.polywrap.eth"),
env: {
provider: defaultIpfsProviders[0],
fallbackProviders: defaultIpfsProviders.slice(1),
},
},
],
redirects: [
{
Expand All @@ -51,10 +58,7 @@ export const getDefaultClientConfig = (
// 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
{
Expand Down
4 changes: 2 additions & 2 deletions packages/js/client/scripts/extractPluginConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const plugins: PluginConfigSource[] = [
name: "Ipfs",
module: "@polywrap/ipfs-plugin-js",
uri: "wrap://ens/ipfs.polywrap.eth",
config: "IpfsPluginConfig",
config: "NoConfig",
files: [
{
name: "build/index.d.ts",
interfaces: ["IpfsPluginConfig"],
types: ["NoConfig"]
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getClientWithEnsAndIpfs = async (
return createPolywrapClient(
{
ethereum: { connections },
ipfs: { provider: providers.ipfs },
ipfs: {},
ens: {
addresses: {
testnet: ensAddresses.ensAddress,
Expand Down
5 changes: 1 addition & 4 deletions packages/js/client/src/pluginConfigs/Ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, never>;
4 changes: 2 additions & 2 deletions packages/js/client/src/pluginConfigs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable prettier/prettier */

import { IpfsPluginConfig } from "./Ipfs";
import { NoConfig } from "./Ipfs";
import { EthereumPluginConfig } from "./Ethereum";
import { EnsResolverPluginConfig } from "./Ens";

interface PluginConfigs {
ipfs?: IpfsPluginConfig;
ipfs?: NoConfig;
ethereum?: EthereumPluginConfig;
ens?: EnsResolverPluginConfig;
}
Expand Down
14 changes: 10 additions & 4 deletions packages/js/plugins/ethereum/src/__tests__/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,23 @@ describe("Ethereum Plugin", () => {
});

client = new PolywrapClient({
envs: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: providers.ipfs,
fallbackProviders: defaultIpfsProviders,
},
},
],
plugins: [
{
uri: "wrap://ens/ethereum.polywrap.eth",
plugin: ethereumPlugin({ connections }),
},
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({
provider: providers.ipfs,
fallbackProviders: defaultIpfsProviders,
}),
plugin: ipfsPlugin({}),
},
{
uri: "wrap://ens/ens-resolver.polywrap.eth",
Expand Down
173 changes: 171 additions & 2 deletions packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InvokeResult } from "@polywrap/core-js";
import { PolywrapClient } from "@polywrap/client-js";
import {
initTestEnvironment,
Expand Down Expand Up @@ -29,9 +30,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,
}),
},
},
],
});
Expand Down Expand Up @@ -87,4 +94,166 @@ 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<InvokeResult<Uint8Array>> => {
return new Promise<InvokeResult<Uint8Array>>((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");
});

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,
});
});

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,
});
});
});
Loading

0 comments on commit c377c85

Please sign in to comment.