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

Commit 9dc8c99

Browse files
ya7yadaviddias
authored andcommitted
Changes required for #968 feat/Gateway (#989)
* 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 <[email protected]> * fix using js-ipfs-repo in gateway tests. License: MIT Signed-off-by: Yahya <[email protected]> * Using unix-fs to detect dirs, replacing object.get with DAG.get, CID checks License: MIT Signed-off-by: Yahya <[email protected]> * rename checkHash -> checkCID License: MIT Signed-off-by: Yahya <[email protected]> * gateway tests: init a fresh repo
1 parent 3c2b1ad commit 9dc8c99

File tree

4 files changed

+26
-37
lines changed

4 files changed

+26
-37
lines changed

src/http/gateway/resolver.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
const mh = require('multihashes')
44
const promisify = require('promisify-es6')
55
const eachOfSeries = require('async/eachOfSeries')
6+
const CID = require('cids')
7+
const Unixfs = require('ipfs-unixfs')
68
const debug = require('debug')
79
const log = debug('jsipfs:http-gateway:resolver')
810
log.error = debug('jsipfs:http-gateway:resolver:error')
9-
1011
const html = require('./utils/html')
1112
const PathUtil = require('./utils/path')
1213

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

4344
let currentMultihash = parts[0]
44-
45+
let currentCid
4546
eachOfSeries(parts, (multihash, currentIndex, next) => {
46-
// throws error when invalid multihash is passed
47-
mh.validate(mh.fromB58String(currentMultihash))
47+
// throws error when invalid CID is passed
48+
try {
49+
currentCid = new CID(mh.fromB58String(currentMultihash))
50+
} catch (e) {
51+
if (e) throw e
52+
}
53+
4854
log('currentMultihash: ', currentMultihash)
4955
log('currentIndex: ', currentIndex, '/', partsLength)
5056

51-
ipfs.object.get(currentMultihash, { enc: 'base58' }, (err, dagNode) => {
57+
ipfs.dag.get(currentCid, (err, result) => {
5258
if (err) { return next(err) }
59+
let dagNode = result.value
5360

5461
if (currentIndex === partsLength - 1) {
55-
// leaf node
56-
log('leaf node: ', currentMultihash)
57-
58-
// TODO: Check if it is a directory by using Unixfs Type, right now
59-
// it won't detect empty dirs
60-
if (dagNode.links &&
61-
dagNode.links.length > 0 &&
62-
dagNode.links[0].name.length > 0) {
63-
// this is a directory.
64-
62+
let dagDataObj = Unixfs.unmarshal(dagNode.data)
63+
if (dagDataObj.type === 'directory') {
6564
let isDirErr = new Error('This dag node is a directory')
6665
// add currentMultihash as a fileName so it can be used by resolveDirectory
6766
isDirErr.fileName = currentMultihash
6867
return next(isDirErr)
6968
}
69+
7070
return next()
7171
}
7272

src/http/gateway/resources/gateway.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ const PathUtils = require('../utils/path')
1212
const Stream = require('stream')
1313

1414
module.exports = {
15-
checkHash: (request, reply) => {
16-
if (!request.params.hash) {
15+
checkCID: (request, reply) => {
16+
if (!request.params.cid) {
1717
return reply({
1818
Message: 'Path Resolve error: path must contain at least one component',
1919
Code: 0
2020
}).code(400).takeover()
2121
}
2222

2323
return reply({
24-
ref: `/ipfs/${request.params.hash}`
24+
ref: `/ipfs/${request.params.cid}`
2525
})
2626
},
2727
handler: (request, reply) => {

src/http/gateway/routes/gateway.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ module.exports = (server) => {
77

88
gateway.route({
99
method: '*',
10-
path: '/ipfs/{hash*}',
10+
path: '/ipfs/{cid*}',
1111
config: {
1212
pre: [
13-
{ method: resources.gateway.checkHash, assign: 'args' }
13+
{ method: resources.gateway.checkCID, assign: 'args' }
1414
],
1515
handler: resources.gateway.handler
1616
}

test/gateway/index.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,23 @@ const dirtyChai = require('dirty-chai')
66
const expect = chai.expect
77
chai.use(dirtyChai)
88
const API = require('../../src/http')
9-
const ncp = require('ncp').ncp
10-
const path = require('path')
11-
const clean = require('../utils/clean')
12-
13-
describe('HTTP GATEWAY', () => {
14-
const repoExample = path.join(__dirname, '../go-ipfs-repo')
15-
const repoTests = path.join(__dirname, '../repo-tests-run')
169

10+
describe('HTTP Gateway', () => {
1711
let http = {}
1812
let gateway
1913

2014
before((done) => {
21-
http.api = new API(repoTests)
22-
23-
ncp(repoExample, repoTests, (err) => {
24-
expect(err).to.not.exist()
15+
http.api = new API()
2516

26-
http.api.start(false, () => {
27-
gateway = http.api.server.select('Gateway')
28-
done()
29-
})
17+
http.api.start(true, () => {
18+
gateway = http.api.server.select('Gateway')
19+
done()
3020
})
3121
})
3222

3323
after((done) => {
3424
http.api.stop((err) => {
3525
expect(err).to.not.exist()
36-
clean(repoTests)
3726
done()
3827
})
3928
})
@@ -67,7 +56,7 @@ describe('HTTP GATEWAY', () => {
6756
url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
6857
}, (res) => {
6958
expect(res.statusCode).to.equal(200)
70-
expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n'))
59+
expect(res.rawPayload).to.deep.equal(Buffer.from('hello world' + '\n'))
7160
expect(res.payload).to.equal('hello world' + '\n')
7261
done()
7362
})

0 commit comments

Comments
 (0)