Skip to content

Commit e82af32

Browse files
committed
feat: support /ipns/ at HTTP Gateway
It requires below to PRs to land first: ipfs#2002 ipfs/js-ipfs-http-response#19 ipfs-inactive/js-ipfs-mfs#48 License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent ccff48b commit e82af32

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
"ipfs-block": "~0.8.0",
114114
"ipfs-block-service": "~0.15.1",
115115
"ipfs-http-client": "^30.1.1",
116-
"ipfs-http-response": "~0.2.1",
117-
"ipfs-mfs": "~0.10.2",
116+
"ipfs-http-response": "https://github.com/ipfs/js-ipfs-http-response/tarball/7c2a49f43d1903689b50f87e14d1e5f046e3fe78/js-ipfs-http-response.tar.gz",
117+
"ipfs-mfs": "https://github.com/ipfs/js-ipfs-mfs/tarball/c644291f31a57f7bc65318ba8bf83e002b83e030/js-ipfs-mfs.tar.gz",
118118
"ipfs-multipart": "~0.1.0",
119119
"ipfs-repo": "~0.26.1",
120120
"ipfs-unixfs": "~0.1.16",

src/http/gateway/resources/gateway.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function detectContentType (ref, chunk) {
3030
return mime.contentType(mimeType)
3131
}
3232

33+
async function resolveIpns (ref, ipfs) {
34+
const [ root ] = PathUtils.splitPath(ref)
35+
const immutableRoot = await ipfs.name.resolve(root, { recursive: true })
36+
return ref.replace(`/ipns/${root}`, PathUtils.removeTrailingSlash(immutableRoot))
37+
}
38+
3339
// Enable streaming of compressed payload
3440
// https://github.com/hapijs/hapi/issues/3599
3541
class ResponseStream extends PassThrough {
@@ -62,17 +68,23 @@ module.exports = {
6268
const { ref } = request.pre.args
6369
const { ipfs } = request.server.app
6470

71+
// The resolver from ipfs-http-response supports only immutable /ipfs/ for now,
72+
// so we convert /ipns/ to /ipfs/ before passing it to the resolver ¯\_(ツ)_/¯
73+
// This can be removed if a solution proposed in
74+
// https://github.com/ipfs/js-ipfs-http-response/issues/22 lands upstream
75+
const immutableRef = ref.startsWith('/ipns/') ? await resolveIpns(ref, ipfs) : ref
76+
6577
let data
6678
try {
67-
data = await resolver.cid(ipfs, ref)
79+
data = await resolver.cid(ipfs, immutableRef)
6880
} catch (err) {
6981
const errorToString = err.toString()
7082
log.error('err: ', errorToString, ' fileName: ', err.fileName)
7183

7284
// switch case with true feels so wrong.
7385
switch (true) {
7486
case (errorToString === 'Error: This dag node is a directory'):
75-
data = await resolver.directory(ipfs, ref, err.cid)
87+
data = await resolver.directory(ipfs, immutableRef, err.cid)
7688

7789
if (typeof data === 'string') {
7890
// no index file found

0 commit comments

Comments
 (0)