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

Commit c8cdb63

Browse files
committed
Make ipfs.files.add return DAGNodes.
1 parent 4a7991f commit c8cdb63

File tree

4 files changed

+93
-21
lines changed

4 files changed

+93
-21
lines changed

package.json

+2-1
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

+21
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

+38-1
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

+32-19
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
let testfile
1213
let testfileBig
@@ -37,8 +38,9 @@ describe('.add', () => {
3738
expect(err).to.not.exist
3839

3940
const added = res[0] != null ? res[0] : res
40-
expect(added).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
41-
expect(added).to.have.property('Name', 'testfile.txt')
41+
const mh = bs58.encode(added.multihash()).toString()
42+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
43+
expect(added.links).to.have.length(0)
4244
done()
4345
})
4446
})
@@ -49,7 +51,9 @@ describe('.add', () => {
4951
expect(err).to.not.exist
5052

5153
expect(res).to.have.length(1)
52-
expect(res[0]).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
54+
const mh = bs58.encode(res[0].multihash()).toString()
55+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
56+
expect(res[0].links).to.have.length(0)
5357
done()
5458
})
5559
})
@@ -63,7 +67,9 @@ describe('.add', () => {
6367
expect(err).to.not.exist
6468

6569
expect(res).to.have.length(1)
66-
expect(res[0]).to.have.a.property('Hash', 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
70+
const mh = bs58.encode(res[0].multihash()).toString()
71+
expect(mh).to.equal('Qmcx5werSWQPdrGVap7LARHB4QUSPRPJwxhFuHvdoXqQXT')
72+
expect(res[0].links).to.have.length(58)
6773
done()
6874
})
6975
})
@@ -77,7 +83,9 @@ describe('.add', () => {
7783
expect(err).to.not.exist
7884

7985
const added = res[0] != null ? res[0] : res
80-
expect(added).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
86+
const mh = bs58.encode(added.multihash()).toString()
87+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
88+
expect(added.links).to.have.length(0)
8189
done()
8290
})
8391
})
@@ -88,14 +96,9 @@ describe('.add', () => {
8896
expect(err).to.not.exist
8997

9098
const added = res[res.length - 1]
91-
expect(added).to.have.property('Hash', 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6')
92-
93-
// check that the symlink was replaced by the target file
94-
const linkPath = 'test-folder/hello-link'
95-
const filePath = 'test-folder/files/hello.txt'
96-
const linkHash = res.filter((e) => e.Name === linkPath)[0].Hash
97-
const fileHash = res.filter((e) => e.Name === filePath)[0].Hash
98-
expect(linkHash).to.equal(fileHash)
99+
const mh = bs58.encode(added.multihash()).toString()
100+
expect(mh).to.equal('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6')
101+
expect(added.links).to.have.length(7)
99102

100103
done()
101104
} else {
@@ -112,7 +115,9 @@ describe('.add', () => {
112115

113116
const added = res[res.length - 1]
114117
// same hash as the result from the cli (ipfs add test/test-folder -r)
115-
expect(added).to.have.property('Hash', 'QmRArDYd8Rk7Zb7K2699KqmQM1uUoejn1chtEAcqkvjzGg')
118+
const mh = bs58.encode(added.multihash()).toString()
119+
expect(mh).to.equal('QmRArDYd8Rk7Zb7K2699KqmQM1uUoejn1chtEAcqkvjzGg')
120+
expect(added.links).to.have.length(7)
116121
done()
117122
} else {
118123
expect(err.message).to.be.equal('Recursive uploads are not supported in the browser')
@@ -147,7 +152,9 @@ describe('.add', () => {
147152
expect(err).to.not.exist
148153

149154
const added = res[res.length - 1]
150-
expect(added).to.have.property('Hash', 'QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj')
155+
const mh = bs58.encode(added.multihash()).toString()
156+
expect(mh).to.equal('QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj')
157+
expect(added.links).to.have.length(6)
151158
done()
152159
})
153160
})
@@ -161,7 +168,9 @@ describe('.add', () => {
161168
expect(err).to.not.exist
162169

163170
const added = res[0] != null ? res[0] : res
164-
expect(added).to.have.a.property('Hash', 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
171+
const mh = bs58.encode(added.multihash()).toString()
172+
expect(mh).to.equal('QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
173+
expect(added.links).to.have.length(0)
165174
done()
166175
})
167176
})
@@ -172,7 +181,9 @@ describe('.add', () => {
172181
expect(err).to.not.exist
173182

174183
const added = res[0] != null ? res[0] : res
175-
expect(added).to.have.a.property('Hash', 'QmZmHgEX9baxUn3qMjsEXQzG6DyNcrVnwieQQTrpDdrFvt')
184+
const mh = bs58.encode(added.multihash()).toString()
185+
expect(mh).to.equal('QmRzvSX35JpzQ2Lyn55r3YwWqdVP6PPxYHFpiWpwQTff8A')
186+
expect(added.links).to.have.length(0)
176187
done()
177188
})
178189
})
@@ -182,8 +193,10 @@ describe('.add', () => {
182193
let buf = new Buffer(testfile)
183194
return apiClients.a.add(buf)
184195
.then((res) => {
185-
expect(res).to.have.length(1)
186-
expect(res[0]).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
196+
const added = res[0] != null ? res[0] : res
197+
const mh = bs58.encode(added.multihash()).toString()
198+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
199+
expect(added.links).to.have.length(0)
187200
})
188201
})
189202
})

0 commit comments

Comments
 (0)