Skip to content

Commit 57b6314

Browse files
authored
Merge pull request #1559 from polywrap/0.9-retries-in-ipfs-resolver
0.9 feat: add retries to ipfs resolver plugin
2 parents d000aad + bf2e909 commit 57b6314

3 files changed

Lines changed: 23 additions & 39 deletions

File tree

packages/js/client-config-builder/src/bundles/default-client-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ export const getDefaultClientConfig = (
4040
fallbackProviders: defaultIpfsProviders.slice(1),
4141
},
4242
},
43+
{
44+
uri: new Uri("wrap://ens/ipfs-resolver.polywrap.eth"),
45+
env: {
46+
retries: { tryResolveUri: 1, getFile: 1 },
47+
},
48+
},
4349
],
4450
redirects: [
4551
{

packages/js/plugins/uri-resolvers/ipfs-resolver/src/index.ts

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export class IpfsResolverPlugin extends Module<NoConfig> {
3333

3434
let manifest: Bytes | undefined;
3535

36-
try {
36+
let attempts = (this.env.retries?.tryResolveUri ?? 0) + 1;
37+
while (attempts-- > 0) {
3738
const manifestResult = await Ipfs_Module.cat(
3839
{
3940
cid: `${args.path}/${manifestSearchPattern}`,
@@ -45,63 +46,35 @@ export class IpfsResolverPlugin extends Module<NoConfig> {
4546
_client
4647
);
4748

48-
if (manifestResult.ok) {
49+
if (manifestResult.ok && manifestResult.value?.length) {
4950
manifest = Buffer.from(manifestResult.value);
51+
return { uri: null, manifest };
5052
}
51-
} catch (e) {
52-
// TODO: logging
53-
// https://github.com/polywrap/monorepo/issues/33
5453
}
5554

56-
return { uri: null, manifest: manifest ?? null };
55+
return { uri: null, manifest: null };
5756
}
5857

5958
public async getFile(
6059
args: Args_getFile,
6160
client: Client
6261
): Promise<Bytes | null> {
63-
try {
64-
let provider: string | undefined = undefined;
65-
66-
if (!this.env.skipCheckIfExists) {
67-
const resolveResult = await Ipfs_Module.resolve(
68-
{
69-
cid: args.path,
70-
options: {
71-
timeout: this.env.timeouts?.checkIfExists,
72-
disableParallelRequests: this.env.disableParallelRequests,
73-
},
74-
},
75-
client
76-
);
77-
78-
if (!resolveResult.ok || !resolveResult.value) {
79-
return null;
80-
}
81-
82-
provider = resolveResult.value.provider;
83-
}
84-
62+
let attempts = (this.env.retries?.getFile ?? 0) + 1;
63+
while (attempts-- > 0) {
8564
const catResult = await Ipfs_Module.cat(
8665
{
8766
cid: args.path,
8867
options: {
89-
provider: provider,
9068
timeout: this.env.timeouts?.getFile,
9169
disableParallelRequests: this.env.disableParallelRequests,
9270
},
9371
},
9472
client
9573
);
96-
97-
if (!catResult.ok) {
98-
return null;
99-
}
100-
101-
return catResult.value;
102-
} catch (e) {
103-
return null;
74+
if (catResult.ok) return catResult.value;
10475
}
76+
77+
return null;
10578
}
10679

10780
private static isCID(cid: string): boolean {

packages/js/plugins/uri-resolvers/ipfs-resolver/src/schema.graphql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ type Timeouts {
77
tryResolveUri: UInt32,
88
}
99

10+
type Retries {
11+
getFile: UInt32
12+
tryResolveUri: UInt32
13+
}
14+
1015
type Env {
1116
"""
12-
Determines whether the plugin should try to resolve a file (check for its existence) or immediately get the file
17+
Re-attempt resolution on failure
1318
"""
14-
skipCheckIfExists: Boolean,
19+
retries: Retries
1520
"""
1621
Timeouts for methods
1722
"""

0 commit comments

Comments
 (0)