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

Commit

Permalink
Changes required for #968 feat/Gateway (#989)
Browse files Browse the repository at this point in the history
* feat: add gateway route to daemon

* feat: adding Gateway endeavour - improve codebase

* clean up, remove commented out lines. (#971)

* clean up, remove commented out lines.

* cleaning code, removing commented out blocks.

* gateway initial tests.

* clean up , working tests on node v8.4.0

License: MIT
Signed-off-by: Yahya <ya7yaz@gmail.com>

* fix using js-ipfs-repo in gateway tests.

License: MIT
Signed-off-by: Yahya <ya7yaz@gmail.com>

* Using unix-fs to detect dirs, replacing object.get with DAG.get, CID checks

License: MIT
Signed-off-by: Yahya <ya7yaz@gmail.com>

* rename checkHash -> checkCID

License: MIT
Signed-off-by: Yahya <ya7yaz@gmail.com>

* gateway tests: init a fresh repo
  • Loading branch information
ya7ya authored and daviddias committed Sep 5, 2017
1 parent c31e013 commit 2349f1e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
30 changes: 15 additions & 15 deletions src/http/gateway/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const mh = require('multihashes')
const promisify = require('promisify-es6')
const eachOfSeries = require('async/eachOfSeries')
const CID = require('cids')
const Unixfs = require('ipfs-unixfs')
const debug = require('debug')
const log = debug('jsipfs:http-gateway:resolver')
log.error = debug('jsipfs:http-gateway:resolver:error')

const html = require('./utils/html')
const PathUtil = require('./utils/path')

Expand Down Expand Up @@ -41,32 +42,31 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
const partsLength = parts.length

let currentMultihash = parts[0]

let currentCid
eachOfSeries(parts, (multihash, currentIndex, next) => {
// throws error when invalid multihash is passed
mh.validate(mh.fromB58String(currentMultihash))
// throws error when invalid CID is passed
try {
currentCid = new CID(mh.fromB58String(currentMultihash))
} catch (e) {
if (e) throw e
}

log('currentMultihash: ', currentMultihash)
log('currentIndex: ', currentIndex, '/', partsLength)

ipfs.object.get(currentMultihash, { enc: 'base58' }, (err, dagNode) => {
ipfs.dag.get(currentCid, (err, result) => {
if (err) { return next(err) }
let dagNode = result.value

if (currentIndex === partsLength - 1) {
// leaf node
log('leaf node: ', currentMultihash)

// TODO: Check if it is a directory by using Unixfs Type, right now
// it won't detect empty dirs
if (dagNode.links &&
dagNode.links.length > 0 &&
dagNode.links[0].name.length > 0) {
// this is a directory.

let dagDataObj = Unixfs.unmarshal(dagNode.data)
if (dagDataObj.type === 'directory') {
let isDirErr = new Error('This dag node is a directory')
// add currentMultihash as a fileName so it can be used by resolveDirectory
isDirErr.fileName = currentMultihash
return next(isDirErr)
}

return next()
}

Expand Down
6 changes: 3 additions & 3 deletions src/http/gateway/resources/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ const PathUtils = require('../utils/path')
const Stream = require('stream')

module.exports = {
checkHash: (request, reply) => {
if (!request.params.hash) {
checkCID: (request, reply) => {
if (!request.params.cid) {
return reply({
Message: 'Path Resolve error: path must contain at least one component',
Code: 0
}).code(400).takeover()
}

return reply({
ref: `/ipfs/${request.params.hash}`
ref: `/ipfs/${request.params.cid}`
})
},
handler: (request, reply) => {
Expand Down
4 changes: 2 additions & 2 deletions src/http/gateway/routes/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = (server) => {

gateway.route({
method: '*',
path: '/ipfs/{hash*}',
path: '/ipfs/{cid*}',
config: {
pre: [
{ method: resources.gateway.checkHash, assign: 'args' }
{ method: resources.gateway.checkCID, assign: 'args' }
],
handler: resources.gateway.handler
}
Expand Down
23 changes: 6 additions & 17 deletions test/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,23 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const API = require('../../src/http')
const ncp = require('ncp').ncp
const path = require('path')
const clean = require('../utils/clean')

describe('HTTP GATEWAY', () => {
const repoExample = path.join(__dirname, '../go-ipfs-repo')
const repoTests = path.join(__dirname, '../repo-tests-run')

describe('HTTP Gateway', () => {
let http = {}
let gateway

before((done) => {
http.api = new API(repoTests)

ncp(repoExample, repoTests, (err) => {
expect(err).to.not.exist()
http.api = new API()

http.api.start(false, () => {
gateway = http.api.server.select('Gateway')
done()
})
http.api.start(true, () => {
gateway = http.api.server.select('Gateway')
done()
})
})

after((done) => {
http.api.stop((err) => {
expect(err).to.not.exist()
clean(repoTests)
done()
})
})
Expand Down Expand Up @@ -67,7 +56,7 @@ describe('HTTP GATEWAY', () => {
url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n'))
expect(res.rawPayload).to.deep.equal(Buffer.from('hello world' + '\n'))
expect(res.payload).to.equal('hello world' + '\n')
done()
})
Expand Down

0 comments on commit 2349f1e

Please sign in to comment.