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

Commit 8c5eeb5

Browse files
committed
Return object wrapping path+node on ipfs.files.add
1 parent e96dc85 commit 8c5eeb5

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

README.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,25 @@ Where `data` may be
7979

8080
`callback` must follow `function (err, res) {}` signature, where `err` is an
8181
error if the operation was not successful. `res` will be an array of
82-
[DAGNode][]s.
82+
83+
```js
84+
{
85+
path: '/tmp/myfile.txt',
86+
node: DAGNode
87+
}
88+
```
8389

8490
If no `callback` is passed, a promise is returned.
8591

8692
```js
8793
var files = [
8894
{
8995
path: '/tmp/myfile.txt',
90-
content: fs.createReadStream('/tmp/myfile.txt')
96+
content: (Buffer or Readable stream)
9197
}
9298
]
93-
ipfs.files.add(files, function (err, dagNodes) {
94-
// 'res' will be an array of DAGNodes
99+
ipfs.files.add(files, function (err, files) {
100+
// 'files' will be an array of objects
95101
})
96102
```
97103

@@ -122,8 +128,12 @@ If no `callback` is passed, a promise is returned.
122128

123129
```js
124130
ipfs.files.createAddStream(function (err, stream) {
125-
stream.on('data', function (dagNode) {
126-
// ...
131+
stream.on('data', function (file) {
132+
// 'file' will be of the form
133+
// {
134+
// path: '/tmp/myfile.txt',
135+
// node: DAGNode
136+
// }
127137
})
128138

129139
stream.write({path: <path to file>, content: <buffer or readable stream>})

test/files.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ module.exports = (common) => {
5050
ipfs.files.add(arr, (err, res) => {
5151
expect(err).to.not.exist
5252
expect(res).to.be.length(1)
53-
expect(res[0].size()).to.equal(17)
53+
expect(res[0].path).to.equal('data.txt')
54+
expect(res[0].node.size()).to.equal(17)
5455
const mh = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
55-
expect(bs58.encode(res[0].multihash()).toString()).to.equal(mh)
56+
expect(bs58.encode(res[0].node.multihash()).toString()).to.equal(mh)
5657
done()
5758
})
5859
})
@@ -69,9 +70,10 @@ module.exports = (common) => {
6970
expect(err).to.not.exist
7071

7172
const added = res[0] != null ? res[0] : res
72-
const mh = bs58.encode(added.multihash()).toString()
73+
const mh = bs58.encode(added.node.multihash()).toString()
7374
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
74-
expect(added.links).to.have.length(0)
75+
expect(added.path).to.equal('testfile.txt')
76+
expect(added.node.links).to.have.length(0)
7577
done()
7678
})
7779
})
@@ -82,9 +84,10 @@ module.exports = (common) => {
8284
expect(err).to.not.exist
8385

8486
expect(res).to.have.length(1)
85-
const mh = bs58.encode(res[0].multihash()).toString()
87+
const mh = bs58.encode(res[0].node.multihash()).toString()
8688
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
87-
expect(res[0].links).to.have.length(0)
89+
expect(res[0].path).to.equal(mh)
90+
expect(res[0].node.links).to.have.length(0)
8891
done()
8992
})
9093
})
@@ -98,9 +101,9 @@ module.exports = (common) => {
98101
expect(err).to.not.exist
99102

100103
expect(res).to.have.length(1)
101-
const mh = bs58.encode(res[0].multihash()).toString()
104+
const mh = bs58.encode(res[0].node.multihash()).toString()
102105
expect(mh).to.equal('Qmcx5werSWQPdrGVap7LARHB4QUSPRPJwxhFuHvdoXqQXT')
103-
expect(res[0].links).to.have.length(58)
106+
expect(res[0].node.links).to.have.length(58)
104107
done()
105108
})
106109
})
@@ -130,9 +133,10 @@ module.exports = (common) => {
130133
expect(err).to.not.exist
131134

132135
const added = res[res.length - 1]
133-
const mh = bs58.encode(added.multihash()).toString()
136+
const mh = bs58.encode(added.node.multihash()).toString()
134137
expect(mh).to.equal('QmW2d9LPX4KxpeLDyH3tqYnHrkRMHx8xMQp9cpXEEj2DyR')
135-
expect(added.links).to.have.length(5)
138+
expect(added.path).to.equal('test-folder')
139+
expect(added.node.links).to.have.length(5)
136140
done()
137141
})
138142
})
@@ -143,9 +147,10 @@ module.exports = (common) => {
143147
return ipfs.files.add(buf)
144148
.then((res) => {
145149
const added = res[0] != null ? res[0] : res
146-
const mh = bs58.encode(added.multihash()).toString()
150+
const mh = bs58.encode(added.node.multihash()).toString()
147151
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
148-
expect(added.links).to.have.length(0)
152+
expect(added.path).to.equal(mh)
153+
expect(added.node.links).to.have.length(0)
149154
})
150155
.catch((err) => {
151156
expect(err).to.not.exist

0 commit comments

Comments
 (0)