@@ -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
3541class 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