From 752e904801d22e984879b70252e08c02d18e3cec Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 6 May 2019 12:11:19 +0200 Subject: [PATCH] feat: support /ipns/ at HTTP Gateway It requires below to land first: https://github.com/ipfs/js-ipfs/issues/1918 License: MIT Signed-off-by: Marcin Rataj --- src/http/api/routes/webui.js | 4 +-- src/http/gateway/resources/gateway.js | 13 +++++--- src/http/gateway/routes/gateway.js | 46 +++++++++++++++++++-------- src/http/gateway/routes/index.js | 2 +- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/http/api/routes/webui.js b/src/http/api/routes/webui.js index fb67397e90..5a3afcf6c0 100644 --- a/src/http/api/routes/webui.js +++ b/src/http/api/routes/webui.js @@ -5,10 +5,10 @@ const resources = require('../../gateway/resources') module.exports = [ { method: '*', - path: '/ipfs/{cid*}', + path: '/ipfs/{immutableId*}', options: { pre: [ - { method: resources.gateway.checkCID, assign: 'args' } + { method: resources.gateway.checkImmutableId, assign: 'args' } ] }, handler: resources.gateway.handler diff --git a/src/http/gateway/resources/gateway.js b/src/http/gateway/resources/gateway.js index 9b903625ac..c58982b306 100644 --- a/src/http/gateway/resources/gateway.js +++ b/src/http/gateway/resources/gateway.js @@ -45,12 +45,17 @@ class ResponseStream extends PassThrough { } module.exports = { - checkCID (request, h) { - if (!request.params.cid) { + checkImmutableId (request, h) { + if (!request.params.immutableId) { throw Boom.badRequest('Path Resolve error: path must contain at least one component') } - - return { ref: `/ipfs/${request.params.cid}` } + return { ref: `/ipfs/${request.params.immutableId}` } + }, + checkMutableId (request, h) { + if (!request.params.mutableId) { + throw Boom.badRequest('Path Resolve error: path must contain at least one component') + } + return { ref: `/ipns/${request.params.mutableId}` } }, async handler (request, h) { diff --git a/src/http/gateway/routes/gateway.js b/src/http/gateway/routes/gateway.js index 4fe5d640b0..94989bd473 100644 --- a/src/http/gateway/routes/gateway.js +++ b/src/http/gateway/routes/gateway.js @@ -2,19 +2,37 @@ const resources = require('../resources') -module.exports = { - method: '*', - path: '/ipfs/{cid*}', - options: { - handler: resources.gateway.handler, - pre: [ - { method: resources.gateway.checkCID, assign: 'args' } - ], - response: { - ranges: false // disable built-in support, we do it manually - }, - ext: { - onPostHandler: { method: resources.gateway.afterHandler } +module.exports = [ + { + method: '*', + path: '/ipfs/{immutableId*}', + options: { + handler: resources.gateway.handler, + pre: [ + { method: resources.gateway.checkImmutableId, assign: 'args' } + ], + response: { + ranges: false // disable built-in support, we do it manually + }, + ext: { + onPostHandler: { method: resources.gateway.afterHandler } + } + } + }, + { + method: '*', + path: '/ipns/{mutableId*}', + options: { + handler: resources.gateway.handler, + pre: [ + { method: resources.gateway.checkMutableId, assign: 'args' } + ], + response: { + ranges: false // disable built-in support, we do it manually + }, + ext: { + onPostHandler: { method: resources.gateway.afterHandler } + } } } -} +] diff --git a/src/http/gateway/routes/index.js b/src/http/gateway/routes/index.js index 2cbf163b04..8f796ab609 100644 --- a/src/http/gateway/routes/index.js +++ b/src/http/gateway/routes/index.js @@ -1,3 +1,3 @@ 'use strict' -module.exports = [require('./gateway')] +module.exports = [...require('./gateway')]