Skip to content

Commit b77d855

Browse files
await normalizeIpfsProvider in IPFS functions and tests for improved async handling
1 parent a26daad commit b77d855

5 files changed

Lines changed: 54 additions & 8 deletions

File tree

src/releaseUploader/ipfsNode/addDirFromUrls.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ export async function ipfsAddDirFromUrls(
2828
}
2929

3030
// Parse the ipfsProvider the a full base apiUrl
31+
const apiUrl = await normalizeIpfsProvider(ipfsProvider);
3132
const res = await got({
32-
prefixUrl: normalizeIpfsProvider(ipfsProvider),
33+
prefixUrl: apiUrl,
3334
url: "api/v0/add",
3435
method: "POST",
3536
headers: form.getHeaders(),

src/releaseUploader/ipfsNode/addFromFs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function ipfsAddFromFs(
1919

2020
// Parse the ipfsProvider the a full base apiUrl
2121
let lastPercent = -1;
22-
const apiUrl = normalizeIpfsProvider(ipfsProvider);
22+
const apiUrl = await normalizeIpfsProvider(ipfsProvider);
2323
const res = await got({
2424
prefixUrl: apiUrl,
2525
url: "api/v0/add",

src/releaseUploader/ipfsNode/ipfsProvider.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,53 @@
11
import { URL } from "url";
2+
import dns from "dns";
3+
import { promisify } from "util";
4+
import { shell } from "../../utils/shell.js";
25

3-
function getIpfsProviderUrl(provider = "dappnode"): string {
6+
const dnsLookup = promisify(dns.lookup);
7+
8+
const DAPPNODE_IPFS_HOST = "ipfs.dappnode";
9+
const DAPPNODE_IPFS_CONTAINER = "DAppNodeCore-ipfs.dnp.dappnode.eth";
10+
11+
/**
12+
* Check if a hostname can be resolved via DNS
13+
*/
14+
async function canResolveHost(hostname: string): Promise<boolean> {
15+
try {
16+
await dnsLookup(hostname);
17+
return true;
18+
} catch {
19+
return false;
20+
}
21+
}
22+
23+
/**
24+
* Get the IP address of the IPFS container from Docker
25+
* @throws If the container does not exist or has no IP
26+
*/
27+
async function getIpfsContainerIp(): Promise<string> {
28+
const ip = await shell(
29+
`docker inspect ${DAPPNODE_IPFS_CONTAINER} --format '{{.NetworkSettings.Networks.dncore_network.IPAddress}}'`
30+
);
31+
32+
if (!ip || ip.trim() === "") {
33+
throw new Error(
34+
`Could not get IP address for container ${DAPPNODE_IPFS_CONTAINER}`
35+
);
36+
}
37+
38+
return ip.trim();
39+
}
40+
41+
async function getIpfsProviderUrl(provider = "dappnode"): Promise<string> {
442
if (provider === "dappnode") {
5-
return "http://ipfs.dappnode";
43+
// Try to resolve ipfs.dappnode first
44+
if (await canResolveHost(DAPPNODE_IPFS_HOST)) {
45+
return `http://${DAPPNODE_IPFS_HOST}`;
46+
}
47+
48+
// Fallback to Docker container IP
49+
const containerIp = await getIpfsContainerIp();
50+
return `http://${containerIp}`;
651
} else if (provider === "remote") {
752
return "https://api.ipfs.dappnode.io";
853
} else if (provider === "infura") {
@@ -28,8 +73,8 @@ function parseIpfsProviderUrl(provider: string) {
2873
}
2974
}
3075

31-
export function normalizeIpfsProvider(provider: string): string {
32-
const providerUrl = getIpfsProviderUrl(provider);
76+
export async function normalizeIpfsProvider(provider: string): Promise<string> {
77+
const providerUrl = await getIpfsProviderUrl(provider);
3378
const { host, port, protocol } = parseIpfsProviderUrl(providerUrl);
3479
const fullUrl = `${protocol}://${host}:${port}`;
3580
// #### TEMP: Make sure the URL is correct

src/releaseUploader/ipfsNode/ipfsVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function ipfsVersion(
1717
ipfsProvider: string
1818
): Promise<IpfsApiVersionResponse> {
1919
// Parse the ipfsProvider the a full base apiUrl
20-
const apiUrl = normalizeIpfsProvider(ipfsProvider);
20+
const apiUrl = await normalizeIpfsProvider(ipfsProvider);
2121
const res = await got<IpfsApiVersionResponse>({
2222
prefixUrl: apiUrl,
2323
url: "api/v0/version",

test/commands/build.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "../../src/params.js";
1111
import { normalizeIpfsProvider } from "../../src/releaseUploader/ipfsNode/ipfsProvider.js";
1212

13-
const contentProvider = normalizeIpfsProvider("remote");
13+
const contentProvider = await normalizeIpfsProvider("remote");
1414

1515
// This test will create the following fake files
1616
// ./dappnode_package.json => fake manifest

0 commit comments

Comments
 (0)