diff --git a/package.json b/package.json index 0750fad5b4..c59216d7e7 100644 --- a/package.json +++ b/package.json @@ -113,8 +113,8 @@ "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", "ipfs-http-client": "^30.1.1", - "ipfs-http-response": "~0.2.1", - "ipfs-mfs": "~0.10.2", + "ipfs-http-response": "https://github.com/ipfs/js-ipfs-http-response/tarball/7c2a49f43d1903689b50f87e14d1e5f046e3fe78/js-ipfs-http-response.tar.gz", + "ipfs-mfs": "https://github.com/ipfs/js-ipfs-mfs/tarball/c644291f31a57f7bc65318ba8bf83e002b83e030/js-ipfs-mfs.tar.gz", "ipfs-multipart": "~0.1.0", "ipfs-repo": "~0.26.1", "ipfs-unixfs": "~0.1.16", diff --git a/src/http/gateway/resources/gateway.js b/src/http/gateway/resources/gateway.js index c58982b306..ca97f926fe 100644 --- a/src/http/gateway/resources/gateway.js +++ b/src/http/gateway/resources/gateway.js @@ -30,6 +30,12 @@ function detectContentType (ref, chunk) { return mime.contentType(mimeType) } +async function resolveIpns (ref, ipfs) { + const [ root ] = PathUtils.splitPath(ref) + const immutableRoot = await ipfs.name.resolve(root, { recursive: true }) + return ref.replace(`/ipns/${root}`, PathUtils.removeTrailingSlash(immutableRoot)) +} + // Enable streaming of compressed payload // https://github.com/hapijs/hapi/issues/3599 class ResponseStream extends PassThrough { @@ -62,9 +68,15 @@ module.exports = { const { ref } = request.pre.args const { ipfs } = request.server.app + // The resolver from ipfs-http-response supports only immutable /ipfs/ for now, + // so we convert /ipns/ to /ipfs/ before passing it to the resolver ¯\_(ツ)_/¯ + // This can be removed if a solution proposed in + // https://github.com/ipfs/js-ipfs-http-response/issues/22 lands upstream + const immutableRef = ref.startsWith('/ipns/') ? await resolveIpns(ref, ipfs) : ref + let data try { - data = await resolver.cid(ipfs, ref) + data = await resolver.cid(ipfs, immutableRef) } catch (err) { const errorToString = err.toString() log.error('err: ', errorToString, ' fileName: ', err.fileName) @@ -72,7 +84,7 @@ module.exports = { // switch case with true feels so wrong. switch (true) { case (errorToString === 'Error: This dag node is a directory'): - data = await resolver.directory(ipfs, ref, err.cid) + data = await resolver.directory(ipfs, immutableRef, err.cid) if (typeof data === 'string') { // no index file found