@@ -30,6 +30,12 @@ function detectContentType (ref, chunk) {
30
30
return mime . contentType ( mimeType )
31
31
}
32
32
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
+
33
39
// Enable streaming of compressed payload
34
40
// https://github.com/hapijs/hapi/issues/3599
35
41
class ResponseStream extends PassThrough {
@@ -62,17 +68,23 @@ module.exports = {
62
68
const { ref } = request . pre . args
63
69
const { ipfs } = request . server . app
64
70
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
+
65
77
let data
66
78
try {
67
- data = await resolver . cid ( ipfs , ref )
79
+ data = await resolver . cid ( ipfs , immutableRef )
68
80
} catch ( err ) {
69
81
const errorToString = err . toString ( )
70
82
log . error ( 'err: ' , errorToString , ' fileName: ' , err . fileName )
71
83
72
84
// switch case with true feels so wrong.
73
85
switch ( true ) {
74
86
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 )
76
88
77
89
if ( typeof data === 'string' ) {
78
90
// no index file found
0 commit comments