This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add HTTP Gateway support for /ipns/ paths #2020
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
97d03fc
feat: support /ipns/ at HTTP Gateway
lidel 31233e0
Merge master into feat/ipns-on-gateway
lidel 99d92b7
style: apply name suggestions from review
lidel bb67cc2
refactor(gateway): simplify path validation
lidel eebde0b
style: unify route param names
lidel c21709d
test(gateway): load from URI-encoded path
lidel 7e3e7b2
style: unify names related to content path
lidel f1da001
style: immutablePath → ipfsPath
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,43 @@ | ||
'use strict' | ||
|
||
const Joi = require('@hapi/joi') | ||
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/{path*}', | ||
options: { | ||
handler: resources.gateway.handler, | ||
validate: { | ||
params: { | ||
path: Joi.string().required() | ||
} | ||
}, | ||
response: { | ||
ranges: false // disable built-in support, handler does it manually | ||
}, | ||
ext: { | ||
onPostHandler: { method: resources.gateway.afterHandler } | ||
} | ||
} | ||
}, | ||
{ | ||
method: '*', | ||
path: '/ipns/{path*}', | ||
options: { | ||
handler: resources.gateway.handler, | ||
validate: { | ||
params: { | ||
path: Joi.string().required() | ||
} | ||
}, | ||
response: { | ||
ranges: false // disable built-in support, handler does it manually | ||
}, | ||
ext: { | ||
onPostHandler: { method: resources.gateway.afterHandler } | ||
} | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
'use strict' | ||
|
||
module.exports = [require('./gateway')] | ||
module.exports = require('./gateway') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ const os = require('os') | |
const path = require('path') | ||
const hat = require('hat') | ||
const fileType = require('file-type') | ||
const CID = require('cids') | ||
|
||
const bigFile = loadFixture('test/fixtures/15mb.random', 'interface-ipfs-core') | ||
const directoryContent = { | ||
|
@@ -20,6 +21,7 @@ const directoryContent = { | |
'nested-folder/ipfs.txt': loadFixture('test/gateway/test-folder/nested-folder/ipfs.txt'), | ||
'nested-folder/nested.html': loadFixture('test/gateway/test-folder/nested-folder/nested.html'), | ||
'cat-folder/cat.jpg': loadFixture('test/gateway/test-folder/cat-folder/cat.jpg'), | ||
'utf8/cat-with-óąśśł-and-أعظم._.jpg': loadFixture('test/gateway/test-folder/cat-folder/cat.jpg'), | ||
'unsniffable-folder/hexagons-xml.svg': loadFixture('test/gateway/test-folder/unsniffable-folder/hexagons-xml.svg'), | ||
'unsniffable-folder/hexagons.svg': loadFixture('test/gateway/test-folder/unsniffable-folder/hexagons.svg') | ||
} | ||
|
@@ -84,6 +86,10 @@ describe('HTTP Gateway', function () { | |
content('unsniffable-folder/hexagons-xml.svg'), | ||
content('unsniffable-folder/hexagons.svg') | ||
]) | ||
// QmaRdtkDark8TgXPdDczwBneadyF44JvFGbrKLTkmTUhHk | ||
await http.api._ipfs.add([content('utf8/cat-with-óąśśł-and-أعظم._.jpg')]) | ||
// Publish QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ to IPNS using self key | ||
await http.api._ipfs.name.publish('QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ', { resolve: false }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for the future: unless we're trying to validate particular content creates a particular CID we should add the content and store the returned CID for use in future tests. Hard coding CIDs like this is a maintenance nightmare when we want to change defaults. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am aware, but did not want to rewrite existing tests, so as a compromise I reused existing CID of |
||
}) | ||
|
||
after(() => http.api.stop()) | ||
|
@@ -526,4 +532,73 @@ describe('HTTP Gateway', function () { | |
expect(res.headers.location).to.equal('/ipfs/QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/index.html') | ||
expect(res.headers['x-ipfs-path']).to.equal(undefined) | ||
}) | ||
|
||
it('test(gateway): load from URI-encoded path', async () => { | ||
// non-ascii characters will be URI-encoded by the browser | ||
const utf8path = '/ipfs/QmaRdtkDark8TgXPdDczwBneadyF44JvFGbrKLTkmTUhHk/cat-with-óąśśł-and-أعظم._.jpg' | ||
const escapedPath = encodeURI(utf8path) // this is what will be actually requested | ||
const res = await gateway.inject({ | ||
method: 'GET', | ||
url: escapedPath | ||
}) | ||
|
||
expect(res.statusCode).to.equal(200) | ||
expect(res.headers['content-type']).to.equal('image/jpeg') | ||
expect(res.headers['x-ipfs-path']).to.equal(escapedPath) | ||
expect(res.headers['cache-control']).to.equal('public, max-age=29030400, immutable') | ||
expect(res.headers['last-modified']).to.equal('Thu, 01 Jan 1970 00:00:01 GMT') | ||
expect(res.headers['content-length']).to.equal(res.rawPayload.length) | ||
expect(res.headers.etag).to.equal('"Qmd286K6pohQcTKYqnS1YhWrCiS4gz7Xi34sdwMe9USZ7u"') | ||
expect(res.headers.suborigin).to.equal('ipfs000bafybeiftsm4u7cn24bn2suwg3x7sldx2uplvfylsk3e4bgylyxwjdevhqm') | ||
}) | ||
|
||
it('load a file from IPNS', async () => { | ||
const { id } = await http.api._ipfs.id() | ||
const ipnsPath = `/ipns/${id}/cat.jpg` | ||
|
||
const res = await gateway.inject({ | ||
method: 'GET', | ||
url: ipnsPath | ||
}) | ||
|
||
const kittyDirectCid = 'Qmd286K6pohQcTKYqnS1YhWrCiS4gz7Xi34sdwMe9USZ7u' | ||
|
||
expect(res.statusCode).to.equal(200) | ||
expect(res.headers['content-type']).to.equal('image/jpeg') | ||
expect(res.headers['content-length']).to.equal(res.rawPayload.length).to.equal(443230) | ||
expect(res.headers['x-ipfs-path']).to.equal(ipnsPath) | ||
expect(res.headers['etag']).to.equal(`"${kittyDirectCid}"`) | ||
expect(res.headers['cache-control']).to.equal('no-cache') // TODO: should be record TTL | ||
expect(res.headers['last-modified']).to.equal(undefined) | ||
expect(res.headers.etag).to.equal('"Qmd286K6pohQcTKYqnS1YhWrCiS4gz7Xi34sdwMe9USZ7u"') | ||
expect(res.headers.suborigin).to.equal(`ipns000${new CID(id).toV1().toBaseEncodedString('base32')}`) | ||
|
||
let fileSignature = fileType(res.rawPayload) | ||
expect(fileSignature.mime).to.equal('image/jpeg') | ||
expect(fileSignature.ext).to.equal('jpg') | ||
}) | ||
|
||
it('load a directory from IPNS', async () => { | ||
const { id } = await http.api._ipfs.id() | ||
const ipnsPath = `/ipns/${id}/` | ||
|
||
const res = await gateway.inject({ | ||
method: 'GET', | ||
url: ipnsPath | ||
}) | ||
|
||
expect(res.statusCode).to.equal(200) | ||
expect(res.headers['content-type']).to.equal('text/html; charset=utf-8') | ||
expect(res.headers['x-ipfs-path']).to.equal(ipnsPath) | ||
expect(res.headers['cache-control']).to.equal('no-cache') | ||
expect(res.headers['last-modified']).to.equal(undefined) | ||
expect(res.headers['content-length']).to.equal(res.rawPayload.length) | ||
expect(res.headers.etag).to.equal(undefined) | ||
expect(res.headers.suborigin).to.equal(`ipns000${new CID(id).toV1().toBaseEncodedString('base32')}`) | ||
|
||
// check if the cat picture is in the payload as a way to check | ||
// if this is an index of this directory | ||
let listedFile = res.payload.match(/\/cat\.jpg/g) | ||
expect(listedFile).to.have.lengthOf(1) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need an
/ipns
handler also?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is only for
/webui
and hardcoded/ipfs/{cid_of_webui}
on API port.(
/ipns/
is only available on the Gateway port)