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

Commit 7d45b09

Browse files
hackergrrldaviddias
authored andcommitted
Make ipfs.files.add return DAGNodes.
1 parent 689f262 commit 7d45b09

File tree

4 files changed

+93
-21
lines changed

4 files changed

+93
-21
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"main": "lib/index.js",
66
"jsnext:main": "src/index.js",
77
"dependencies": {
8-
"bl": "^1.1.2",
8+
"async": "^2.0.0-rc.5",
99
"babel-runtime": "^6.6.1",
10+
"bl": "^1.1.2",
1011
"bs58": "^3.0.0",
1112
"detect-node": "^2.0.3",
1213
"flatmap": "0.0.3",

src/api/add.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
const Wreck = require('wreck')
4+
const async = require('async')
5+
const DAGNode = require('ipfs-merkle-dag').DAGNode
46

57
module.exports = (send) => {
68
return function add (files, opts, cb) {
@@ -9,6 +11,8 @@ module.exports = (send) => {
911
opts = {}
1012
}
1113

14+
send = send.withTransform(transform)
15+
1216
if (typeof files === 'string' && files.startsWith('http')) {
1317
return Wreck.request('GET', files, null, (err, res) => {
1418
if (err) return cb(err)
@@ -18,5 +22,22 @@ module.exports = (send) => {
1822
}
1923

2024
return send('add', null, opts, files, cb)
25+
26+
// transform returned objects into DAGNodes
27+
function transform (err, res, done) {
28+
if (err) return done(err)
29+
30+
async.map(res,
31+
function map (entry, fin) {
32+
send('object/get', entry.Hash, null, null, function (err, result) {
33+
if (err) return done(err)
34+
const node = new DAGNode(result.Data, result.Links)
35+
fin(err, node)
36+
})
37+
},
38+
function complete (err, results) {
39+
if (done) return done(err, results)
40+
})
41+
}
2142
}
2243
}

src/request-api.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
110110
// -- Interface
111111

112112
exports = module.exports = function getRequestAPI (config) {
113-
return function (path, args, qs, files, buffer, cb) {
113+
var send = function (path, args, qs, files, buffer, cb) {
114114
if (typeof buffer === 'function') {
115115
cb = buffer
116116
buffer = false
@@ -127,4 +127,41 @@ exports = module.exports = function getRequestAPI (config) {
127127

128128
return requestAPI(config, path, args, qs, files, buffer, cb)
129129
}
130+
131+
// Wraps the 'send' function such that an asynchronous transform may be
132+
// applied to its result before passing it on to either its callback or
133+
// promise.
134+
send.withTransform = function (transform) {
135+
return function (path, args, qs, files, buffer, cb) {
136+
if (typeof buffer === 'function') {
137+
cb = buffer
138+
buffer = false
139+
}
140+
141+
var p = send(path, args, qs, files, buffer, wrap(cb))
142+
143+
if (p instanceof Promise) {
144+
return p.then((res) => {
145+
return new Promise(function (resolve, reject) {
146+
transform(null, res, function (err, res) {
147+
if (err) reject(err)
148+
else resolve(res)
149+
})
150+
})
151+
})
152+
} else {
153+
return p
154+
}
155+
156+
function wrap (done) {
157+
if (done) {
158+
return function (err, res) {
159+
transform(err, res, done)
160+
}
161+
}
162+
}
163+
}
164+
}
165+
166+
return send
130167
}

test/api/add.spec.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Readable = require('stream').Readable
77
const path = require('path')
88
const isNode = require('detect-node')
99
const fs = require('fs')
10+
const bs58 = require('bs58')
1011

1112
const testfileBig = fs.readFileSync(path.join(__dirname, '/../15mb.random'))
1213
const testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt'))
@@ -26,8 +27,9 @@ describe('.add', () => {
2627
expect(err).to.not.exist
2728

2829
const added = res[0] != null ? res[0] : res
29-
expect(added).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
30-
expect(added).to.have.property('Name', 'testfile.txt')
30+
const mh = bs58.encode(added.multihash()).toString()
31+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
32+
expect(added.links).to.have.length(0)
3133
done()
3234
})
3335
})
@@ -38,7 +40,9 @@ describe('.add', () => {
3840
expect(err).to.not.exist
3941

4042
expect(res).to.have.length(1)
41-
expect(res[0]).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
43+
const mh = bs58.encode(res[0].multihash()).toString()
44+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
45+
expect(res[0].links).to.have.length(0)
4246
done()
4347
})
4448
})
@@ -52,7 +56,9 @@ describe('.add', () => {
5256
expect(err).to.not.exist
5357

5458
expect(res).to.have.length(1)
55-
expect(res[0]).to.have.a.property('Hash', 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
59+
const mh = bs58.encode(res[0].multihash()).toString()
60+
expect(mh).to.equal('Qmcx5werSWQPdrGVap7LARHB4QUSPRPJwxhFuHvdoXqQXT')
61+
expect(res[0].links).to.have.length(58)
5662
done()
5763
})
5864
})
@@ -66,7 +72,9 @@ describe('.add', () => {
6672
expect(err).to.not.exist
6773

6874
const added = res[0] != null ? res[0] : res
69-
expect(added).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
75+
const mh = bs58.encode(added.multihash()).toString()
76+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
77+
expect(added.links).to.have.length(0)
7078
done()
7179
})
7280
})
@@ -77,14 +85,9 @@ describe('.add', () => {
7785
expect(err).to.not.exist
7886

7987
const added = res[res.length - 1]
80-
expect(added).to.have.property('Hash', 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6')
81-
82-
// check that the symlink was replaced by the target file
83-
const linkPath = 'test-folder/hello-link'
84-
const filePath = 'test-folder/files/hello.txt'
85-
const linkHash = res.filter((e) => e.Name === linkPath)[0].Hash
86-
const fileHash = res.filter((e) => e.Name === filePath)[0].Hash
87-
expect(linkHash).to.equal(fileHash)
88+
const mh = bs58.encode(added.multihash()).toString()
89+
expect(mh).to.equal('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6')
90+
expect(added.links).to.have.length(7)
8891

8992
done()
9093
} else {
@@ -101,7 +104,9 @@ describe('.add', () => {
101104

102105
const added = res[res.length - 1]
103106
// same hash as the result from the cli (ipfs add test/test-folder -r)
104-
expect(added).to.have.property('Hash', 'QmRArDYd8Rk7Zb7K2699KqmQM1uUoejn1chtEAcqkvjzGg')
107+
const mh = bs58.encode(added.multihash()).toString()
108+
expect(mh).to.equal('QmRArDYd8Rk7Zb7K2699KqmQM1uUoejn1chtEAcqkvjzGg')
109+
expect(added.links).to.have.length(7)
105110
done()
106111
} else {
107112
expect(err.message).to.be.equal('Recursive uploads are not supported in the browser')
@@ -137,7 +142,9 @@ describe('.add', () => {
137142
expect(err).to.not.exist
138143

139144
const added = res[res.length - 1]
140-
expect(added).to.have.property('Hash', 'QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj')
145+
const mh = bs58.encode(added.multihash()).toString()
146+
expect(mh).to.equal('QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj')
147+
expect(added.links).to.have.length(6)
141148
done()
142149
})
143150
})
@@ -151,7 +158,9 @@ describe('.add', () => {
151158
expect(err).to.not.exist
152159

153160
const added = res[0] != null ? res[0] : res
154-
expect(added).to.have.a.property('Hash', 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
161+
const mh = bs58.encode(added.multihash()).toString()
162+
expect(mh).to.equal('QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
163+
expect(added.links).to.have.length(0)
155164
done()
156165
})
157166
})
@@ -162,7 +171,9 @@ describe('.add', () => {
162171
expect(err).to.not.exist
163172

164173
const added = res[0] != null ? res[0] : res
165-
expect(added).to.have.a.property('Hash', 'QmZmHgEX9baxUn3qMjsEXQzG6DyNcrVnwieQQTrpDdrFvt')
174+
const mh = bs58.encode(added.multihash()).toString()
175+
expect(mh).to.equal('QmRzvSX35JpzQ2Lyn55r3YwWqdVP6PPxYHFpiWpwQTff8A')
176+
expect(added.links).to.have.length(0)
166177
done()
167178
})
168179
})
@@ -172,8 +183,10 @@ describe('.add', () => {
172183
let buf = new Buffer(testfile)
173184
return apiClients.a.add(buf)
174185
.then((res) => {
175-
expect(res).to.have.length(1)
176-
expect(res[0]).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
186+
const added = res[0] != null ? res[0] : res
187+
const mh = bs58.encode(added.multihash()).toString()
188+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
189+
expect(added.links).to.have.length(0)
177190
})
178191
})
179192
})

0 commit comments

Comments
 (0)