Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 5dd2754

Browse files
dignifiedquiredaviddias
authored andcommitted
Update for new block interface (#538)
* feat: update for new block interface
1 parent 909f368 commit 5dd2754

21 files changed

+220
-158
lines changed

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
"flatmap": "0.0.3",
3131
"glob": "^7.1.1",
3232
"glob-escape": "0.0.2",
33-
"ipfs-block": "~0.5.5",
33+
"ipfs-block": "~0.6.0",
3434
"ipfs-unixfs": "~0.1.11",
35-
"ipld-dag-pb": "~0.10.1",
35+
"ipld-dag-pb": "~0.11.0",
3636
"is-ipfs": "~0.3.0",
3737
"isstream": "^0.1.2",
3838
"lru-cache": "^4.0.2",
@@ -60,12 +60,13 @@
6060
"url": "https://github.com/ipfs/js-ipfs-api"
6161
},
6262
"devDependencies": {
63-
"aegir": "^10.0.0",
63+
"aegir": "^11.0.1",
6464
"chai": "^3.5.0",
65-
"eslint-plugin-react": "^6.10.2",
65+
"dirty-chai": "^1.2.2",
66+
"eslint-plugin-react": "^6.10.3",
6667
"gulp": "^3.9.1",
6768
"hapi": "^16.1.0",
68-
"interface-ipfs-core": "~0.25.1",
69+
"interface-ipfs-core": "~0.26.0",
6970
"ipfsd-ctl": "~0.20.0",
7071
"pre-commit": "^1.2.2",
7172
"socket.io": "^1.7.3",

src/api/block.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,39 @@ const streamToValue = require('../stream-to-value')
99
module.exports = (send) => {
1010
return {
1111
get: promisify((args, opts, callback) => {
12-
// TODO this needs to be adjusted with the new go-ipfs http-api
13-
if (CID.isCID(args)) {
14-
args = multihash.toB58String(args.multihash)
15-
}
16-
if (Buffer.isBuffer(args)) {
17-
args = multihash.toB58String(args)
18-
}
1912
if (typeof opts === 'function') {
2013
callback = opts
2114
opts = {}
2215
}
2316

17+
// TODO this needs to be adjusted with the new go-ipfs http-api
18+
let cid
19+
try {
20+
if (CID.isCID(args)) {
21+
cid = args
22+
args = multihash.toB58String(args.multihash)
23+
} else if (Buffer.isBuffer(args)) {
24+
cid = new CID(args)
25+
args = multihash.toB58String(args)
26+
} else if (typeof args === 'string') {
27+
cid = new CID(args)
28+
} else {
29+
return callback(new Error('invalid argument'))
30+
}
31+
} catch (err) {
32+
return callback(err)
33+
}
34+
2435
// Transform the response from Buffer or a Stream to a Block
2536
const transform = (res, callback) => {
2637
if (Buffer.isBuffer(res)) {
27-
callback(null, new Block(res))
38+
callback(null, new Block(res, cid))
2839
} else {
2940
streamToValue(res, (err, data) => {
3041
if (err) {
3142
return callback(err)
3243
}
33-
callback(null, new Block(data))
44+
callback(null, new Block(data, cid))
3445
})
3546
}
3647
}
@@ -92,7 +103,9 @@ module.exports = (send) => {
92103
}
93104

94105
// Transform the response to a Block
95-
const transform = (blockInfo, callback) => callback(null, new Block(block))
106+
const transform = (info, callback) => {
107+
callback(null, new Block(block, new CID(info.Key)))
108+
}
96109

97110
send.andTransform(request, transform, callback)
98111
})

src/clean-multihash.js

-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ module.exports = function (multihash) {
1515
}
1616
return multihash
1717
}
18-

src/get-files-stream.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function loadPaths (opts, file) {
4646

4747
if (stats.isDirectory() && opts.recursive) {
4848
const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/')
49-
const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, {
49+
const mg = new glob.sync.GlobSync(`${globEscapedDir}` + '**/*', {
5050
follow: followSymlinks,
5151
dot: opts.hidden,
5252
ignore: (opts.ignore || []).map(function (ignoreGlob) {
@@ -84,9 +84,7 @@ function loadPaths (opts, file) {
8484
dir: true
8585
}
8686
}
87-
8887
// files inside symlinks and others
89-
return
9088
})
9189
// filter out null files
9290
.filter(Boolean)

src/request-api.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function parseError (res, cb) {
1818
if (err) {
1919
return cb(err)
2020
}
21+
2122
if (payload) {
2223
error.code = payload.Code
2324
error.message = payload.Message || payload.toString()

test/add.spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
/* eslint max-nested-callbacks: ["error", 8] */
33
'use strict'
44

5-
const expect = require('chai').expect
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
69
const isNode = require('detect-node')
710
const path = require('path')
811

@@ -18,7 +21,7 @@ describe('.add (extra tests)', () => {
1821
this.timeout(20 * 1000) // slow CI
1922
fc = new FactoryClient()
2023
fc.spawnNode((err, node) => {
21-
expect(err).to.not.exist
24+
expect(err).to.not.exist()
2225
ipfs = node
2326
done()
2427
})
@@ -30,7 +33,7 @@ describe('.add (extra tests)', () => {
3033
const validPath = path.join(process.cwd() + '/package.json')
3134

3235
ipfs.files.add(validPath, (err, res) => {
33-
expect(err).to.exist
36+
expect(err).to.exist()
3437
done()
3538
})
3639
})

test/bitswap.spec.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const expect = require('chai').expect
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
58
const FactoryClient = require('./ipfs-factory/client')
69

710
describe('.bitswap', () => {
@@ -12,7 +15,7 @@ describe('.bitswap', () => {
1215
this.timeout(20 * 1000) // slow CI
1316
fc = new FactoryClient()
1417
fc.spawnNode((err, node) => {
15-
expect(err).to.not.exist
18+
expect(err).to.not.exist()
1619
ipfs = node
1720
done()
1821
})
@@ -25,7 +28,7 @@ describe('.bitswap', () => {
2528
describe('Callback API', () => {
2629
it('.wantlist', (done) => {
2730
ipfs.bitswap.wantlist((err, res) => {
28-
expect(err).to.not.exist
31+
expect(err).to.not.exist()
2932
expect(res).to.have.to.be.eql({
3033
Keys: null
3134
})
@@ -35,7 +38,7 @@ describe('.bitswap', () => {
3538

3639
it('.stat', (done) => {
3740
ipfs.bitswap.stat((err, res) => {
38-
expect(err).to.not.exist
41+
expect(err).to.not.exist()
3942
expect(res).to.have.property('BlocksReceived')
4043
expect(res).to.have.property('DupBlksReceived')
4144
expect(res).to.have.property('DupDataReceived')
@@ -50,7 +53,7 @@ describe('.bitswap', () => {
5053
it('.unwant', (done) => {
5154
const key = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
5255
ipfs.bitswap.unwant(key, (err) => {
53-
expect(err).to.not.exist
56+
expect(err).to.not.exist()
5457
done()
5558
})
5659
})

test/bootstrap.spec.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
/* eslint max-nested-callbacks: ["error", 8] */
33
'use strict'
44

5-
const expect = require('chai').expect
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
69
const FactoryClient = require('./ipfs-factory/client')
710

811
const invalidArg = 'this/Is/So/Invalid/'
@@ -16,7 +19,7 @@ describe('.bootstrap', () => {
1619
this.timeout(20 * 1000) // slow CI
1720
fc = new FactoryClient()
1821
fc.spawnNode((err, node) => {
19-
expect(err).to.not.exist
22+
expect(err).to.not.exist()
2023
ipfs = node
2124
done()
2225
})
@@ -39,20 +42,20 @@ describe('.bootstrap', () => {
3942

4043
it('returns a list of containing the bootstrap peer when called with a valid arg (ip4)', (done) => {
4144
ipfs.bootstrap.add(validIp4, (err, res) => {
42-
expect(err).to.not.exist
45+
expect(err).to.not.exist()
4346
expect(res).to.be.eql({ Peers: [validIp4] })
4447
peers = res.Peers
45-
expect(peers).to.exist
48+
expect(peers).to.exist()
4649
expect(peers.length).to.eql(1)
4750
done()
4851
})
4952
})
5053

5154
it('returns a list of bootstrap peers when called with the default option', (done) => {
5255
ipfs.bootstrap.add({ default: true }, (err, res) => {
53-
expect(err).to.not.exist
56+
expect(err).to.not.exist()
5457
peers = res.Peers
55-
expect(peers).to.exist
58+
expect(peers).to.exist()
5659
expect(peers.length).to.above(1)
5760
done()
5861
})
@@ -62,9 +65,9 @@ describe('.bootstrap', () => {
6265
describe('.list', () => {
6366
it('returns a list of peers', (done) => {
6467
ipfs.bootstrap.list((err, res) => {
65-
expect(err).to.not.exist
68+
expect(err).to.not.exist()
6669
peers = res.Peers
67-
expect(peers).to.exist
70+
expect(peers).to.exist()
6871
done()
6972
})
7073
})
@@ -80,29 +83,29 @@ describe('.bootstrap', () => {
8083

8184
it('returns empty list because no peers removed when called without an arg or options', (done) => {
8285
ipfs.bootstrap.rm(null, (err, res) => {
83-
expect(err).to.not.exist
86+
expect(err).to.not.exist()
8487
peers = res.Peers
85-
expect(peers).to.exist
88+
expect(peers).to.exist()
8689
expect(peers.length).to.eql(0)
8790
done()
8891
})
8992
})
9093

9194
it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => {
9295
ipfs.bootstrap.rm(null, (err, res) => {
93-
expect(err).to.not.exist
96+
expect(err).to.not.exist()
9497
peers = res.Peers
95-
expect(peers).to.exist
98+
expect(peers).to.exist()
9699
expect(peers.length).to.eql(0)
97100
done()
98101
})
99102
})
100103

101104
it('returns list of all peers removed when all option is passed', (done) => {
102105
ipfs.bootstrap.rm(null, { all: true }, (err, res) => {
103-
expect(err).to.not.exist
106+
expect(err).to.not.exist()
104107
peers = res.Peers
105-
expect(peers).to.exist
108+
expect(peers).to.exist()
106109
done()
107110
})
108111
})
@@ -130,7 +133,7 @@ describe('.bootstrap', () => {
130133
.then((res) => {
131134
expect(res).to.be.eql({ Peers: [validIp4] })
132135
peers = res.Peers
133-
expect(peers).to.exist
136+
expect(peers).to.exist()
134137
expect(peers.length).to.eql(1)
135138
})
136139
})
@@ -139,7 +142,7 @@ describe('.bootstrap', () => {
139142
return ipfs.bootstrap.add(null, { default: true })
140143
.then((res) => {
141144
peers = res.Peers
142-
expect(peers).to.exist
145+
expect(peers).to.exist()
143146
expect(peers.length).to.above(1)
144147
})
145148
})
@@ -150,7 +153,7 @@ describe('.bootstrap', () => {
150153
return ipfs.bootstrap.list()
151154
.then((res) => {
152155
peers = res.Peers
153-
expect(peers).to.exist
156+
expect(peers).to.exist()
154157
})
155158
})
156159
})
@@ -167,7 +170,7 @@ describe('.bootstrap', () => {
167170
return ipfs.bootstrap.rm(null)
168171
.then((res) => {
169172
peers = res.Peers
170-
expect(peers).to.exist
173+
expect(peers).to.exist()
171174
expect(peers.length).to.eql(0)
172175
})
173176
})
@@ -176,7 +179,7 @@ describe('.bootstrap', () => {
176179
return ipfs.bootstrap.rm(null)
177180
.then((res) => {
178181
peers = res.Peers
179-
expect(peers).to.exist
182+
expect(peers).to.exist()
180183
expect(peers.length).to.eql(0)
181184
})
182185
})
@@ -185,7 +188,7 @@ describe('.bootstrap', () => {
185188
return ipfs.bootstrap.rm(null, { all: true })
186189
.then((res) => {
187190
peers = res.Peers
188-
expect(peers).to.exist
191+
expect(peers).to.exist()
189192
})
190193
})
191194
})

test/commands.spec.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const expect = require('chai').expect
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
59
const FactoryClient = require('./ipfs-factory/client')
610

711
describe('.commands', () => {
@@ -12,7 +16,7 @@ describe('.commands', () => {
1216
this.timeout(20 * 1000) // slow CI
1317
fc = new FactoryClient()
1418
fc.spawnNode((err, node) => {
15-
expect(err).to.not.exist
19+
expect(err).to.not.exist()
1620
ipfs = node
1721
done()
1822
})
@@ -24,8 +28,8 @@ describe('.commands', () => {
2428

2529
it('lists commands', (done) => {
2630
ipfs.commands((err, res) => {
27-
expect(err).to.not.exist
28-
expect(res).to.exist
31+
expect(err).to.not.exist()
32+
expect(res).to.exist()
2933
done()
3034
})
3135
})
@@ -34,7 +38,7 @@ describe('.commands', () => {
3438
it('lists commands', () => {
3539
return ipfs.commands()
3640
.then((res) => {
37-
expect(res).to.exist
41+
expect(res).to.exist()
3842
})
3943
})
4044
})

0 commit comments

Comments
 (0)