Skip to content

Commit

Permalink
Use urlJoin and joinIpfsLocation utilities consistently (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Apr 3, 2020
1 parent c4d631d commit 1c763b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/utils/apm/resolveContent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch from 'node-fetch'
import { urlJoin } from '~/src/utils/url'
import { AragonManifest, AragonArtifact } from '~/src/types'
import { contentUriToFetchUrl, toUtf8IfHex } from './utils'

Expand All @@ -15,8 +16,8 @@ export async function resolveRepoContentUri(
const url = contentUriToFetchUrl(toUtf8IfHex(contentUri), options)

const [manifest, artifact] = await Promise.all([
_fetchJson<AragonManifest>(`${url}/manifest.json`),
_fetchJson<AragonArtifact>(`${url}/artifact.json`)
_fetchJson<AragonManifest>(urlJoin(url, 'manifest.json')),
_fetchJson<AragonArtifact>(urlJoin(url, 'artifact.json'))
])

return { manifest, artifact }
Expand All @@ -35,7 +36,7 @@ export async function resolveRepoContentUriFile(
options?: { ipfsGateway?: string }
): Promise<string> {
const url = contentUriToFetchUrl(toUtf8IfHex(contentUri), options)
return await _fetchText(`${url}/${filepath}`)
return await _fetchText(urlJoin(url, filepath))
}

/**
Expand Down
22 changes: 17 additions & 5 deletions src/utils/apm/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ethers } from 'ethers'
import semver from 'semver'
import { urlJoin } from '~/src/utils/url'
import { ApmVersion, ApmVersionReturn } from './types'

/**
Expand Down Expand Up @@ -52,6 +53,21 @@ export function stipIpfsPrefix(ipfsDirtyHash: string): string {
)
}

/**
* Returns a joined IPFS location given an IPFS gateway and an IPFS path
* This util makes sure the url is properly joined, and that it contains
* the "ipfs" route only once, stripping it from the gateway and the location
* @param ipfsGateway "https://ipfs.io"
* @param location "Qmzz"
* @return "https://ipfs.io/ipfs/Qmzz/artifact.json"
*/
export function joinIpfsLocation(
ipfsGateway: string,
location: string
): string {
return urlJoin(stipIpfsPrefix(ipfsGateway), 'ipfs', stipIpfsPrefix(location))
}

/**
* Return a fetchable URL to get the resources of a contentURI
* @param contentUri "ipfs:QmaT4Eef..."
Expand All @@ -71,11 +87,7 @@ export function contentUriToFetchUrl(
case 'ipfs':
if (!options || !options.ipfsGateway)
throw Error(`Must provide an ipfsGateway for protocol 'ipfs'`)
return [
stipIpfsPrefix(options.ipfsGateway),
'ipfs',
stipIpfsPrefix(location)
].join('/')
return joinIpfsLocation(options.ipfsGateway, location)
default:
throw Error(`Protocol '${protocol}' not supported`)
}
Expand Down
8 changes: 6 additions & 2 deletions src/utils/appInstaller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
NetworkType
} from '~/src/types'
import { ANY_ADDRESS } from '~/src/params'
import { toApmVersionArray, getRepoVersion } from '~/src/utils/apm'
import {
toApmVersionArray,
getRepoVersion,
joinIpfsLocation
} from '~/src/utils/apm'
import { getFullAppName } from '~/src/utils/appName'
import { getLog } from '~/src/utils/getLog'
import { namehash } from '~/src/utils/namehash'
Expand Down Expand Up @@ -167,7 +171,7 @@ async function _installExternalApp(
const shortName = name.split('.')[0]
const initialVersionArray = toApmVersionArray('1.0.0')
const contentUriHttpFromPublicGateway = utf8ToHex(
`http:${ipfsGateway}${getContentHash(contentURI)}`
'http:' + joinIpfsLocation(ipfsGateway, getContentHash(contentURI))
)
const APMRegistry = bre.artifacts.require('APMRegistry')
const apmRegistry: APMRegistryInstance = await APMRegistry.at(apmAddress)
Expand Down

0 comments on commit 1c763b8

Please sign in to comment.