Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 97d03fc

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

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
"ipfs-block": "~0.8.1",
9595
"ipfs-block-service": "~0.15.1",
9696
"ipfs-http-client": "^32.0.1",
97-
"ipfs-http-response": "~0.3.0",
98-
"ipfs-mfs": "~0.11.4",
97+
"ipfs-http-response": "~0.3.1",
98+
"ipfs-mfs": "~0.11.5",
9999
"ipfs-multipart": "~0.1.0",
100100
"ipfs-repo": "~0.26.6",
101101
"ipfs-unixfs": "~0.1.16",

src/http/api/routes/webui.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const resources = require('../../gateway/resources')
55
module.exports = [
66
{
77
method: '*',
8-
path: '/ipfs/{cid*}',
8+
path: '/ipfs/{immutableId*}',
99
options: {
1010
pre: [
11-
{ method: resources.gateway.checkCID, assign: 'args' }
11+
{ method: resources.gateway.checkImmutableId, assign: 'args' }
1212
]
1313
},
1414
handler: resources.gateway.handler

src/http/gateway/resources/gateway.js

+23-6
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 {
@@ -45,29 +51,40 @@ class ResponseStream extends PassThrough {
4551
}
4652

4753
module.exports = {
48-
checkCID (request, h) {
49-
if (!request.params.cid) {
54+
checkImmutableId (request, h) {
55+
if (!request.params.immutableId) {
5056
throw Boom.badRequest('Path Resolve error: path must contain at least one component')
5157
}
52-
53-
return { ref: `/ipfs/${request.params.cid}` }
58+
return { ref: `/ipfs/${request.params.immutableId}` }
59+
},
60+
checkMutableId (request, h) {
61+
if (!request.params.mutableId) {
62+
throw Boom.badRequest('Path Resolve error: path must contain at least one component')
63+
}
64+
return { ref: `/ipns/${request.params.mutableId}` }
5465
},
5566

5667
async handler (request, h) {
5768
const { ref } = request.pre.args
5869
const { ipfs } = request.server.app
5970

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+
6077
let data
6178
try {
62-
data = await resolver.cid(ipfs, ref)
79+
data = await resolver.cid(ipfs, immutableRef)
6380
} catch (err) {
6481
const errorToString = err.toString()
6582
log.error('err: ', errorToString, ' fileName: ', err.fileName)
6683

6784
// switch case with true feels so wrong.
6885
switch (true) {
6986
case (errorToString === 'Error: This dag node is a directory'):
70-
data = await resolver.directory(ipfs, ref, err.cid)
87+
data = await resolver.directory(ipfs, immutableRef, err.cid)
7188

7289
if (typeof data === 'string') {
7390
// no index file found

src/http/gateway/routes/gateway.js

+32-14
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,37 @@
22

33
const resources = require('../resources')
44

5-
module.exports = {
6-
method: '*',
7-
path: '/ipfs/{cid*}',
8-
options: {
9-
handler: resources.gateway.handler,
10-
pre: [
11-
{ method: resources.gateway.checkCID, assign: 'args' }
12-
],
13-
response: {
14-
ranges: false // disable built-in support, we do it manually
15-
},
16-
ext: {
17-
onPostHandler: { method: resources.gateway.afterHandler }
5+
module.exports = [
6+
{
7+
method: '*',
8+
path: '/ipfs/{immutableId*}',
9+
options: {
10+
handler: resources.gateway.handler,
11+
pre: [
12+
{ method: resources.gateway.checkImmutableId, assign: 'args' }
13+
],
14+
response: {
15+
ranges: false // disable built-in support, we do it manually
16+
},
17+
ext: {
18+
onPostHandler: { method: resources.gateway.afterHandler }
19+
}
20+
}
21+
},
22+
{
23+
method: '*',
24+
path: '/ipns/{mutableId*}',
25+
options: {
26+
handler: resources.gateway.handler,
27+
pre: [
28+
{ method: resources.gateway.checkMutableId, assign: 'args' }
29+
],
30+
response: {
31+
ranges: false // disable built-in support, we do it manually
32+
},
33+
ext: {
34+
onPostHandler: { method: resources.gateway.afterHandler }
35+
}
1836
}
1937
}
20-
}
38+
]

src/http/gateway/routes/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict'
22

3-
module.exports = [require('./gateway')]
3+
module.exports = [...require('./gateway')]

0 commit comments

Comments
 (0)