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

Commit 1d4d3e9

Browse files
committed
feat: modularises object.patch* and object.new
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 9b345ec commit 1d4d3e9

File tree

9 files changed

+559
-1006
lines changed

9 files changed

+559
-1006
lines changed

js/src/object.js

Lines changed: 0 additions & 1003 deletions
This file was deleted.

js/src/object/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
new: require('./new'),
6+
put: require('./put'),
7+
get: require('./get'),
8+
data: require('./data'),
9+
links: require('./links'),
10+
stat: require('./stat'),
11+
patch: require('./patch')
12+
}
13+
14+
module.exports = createSuite(tests)

js/src/object/new.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const { getDescribe, getIt } = require('../utils/mocha')
9+
10+
const expect = chai.expect
11+
chai.use(dirtyChai)
12+
13+
module.exports = (createCommon, options) => {
14+
const describe = getDescribe(options)
15+
const it = getIt(options)
16+
const common = createCommon()
17+
18+
describe('.object.new', function () {
19+
this.timeout(80 * 1000)
20+
21+
let ipfs
22+
23+
before(function (done) {
24+
// CI takes longer to instantiate the daemon, so we need to increase the
25+
// timeout for the before step
26+
this.timeout(60 * 1000)
27+
28+
common.setup((err, factory) => {
29+
expect(err).to.not.exist()
30+
factory.spawnNode((err, node) => {
31+
expect(err).to.not.exist()
32+
ipfs = node
33+
done()
34+
})
35+
})
36+
})
37+
38+
after((done) => common.teardown(done))
39+
40+
it('should create a new object with no template', (done) => {
41+
ipfs.object.new((err, node) => {
42+
expect(err).to.not.exist()
43+
const nodeJSON = node.toJSON()
44+
expect(nodeJSON.multihash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
45+
done()
46+
})
47+
})
48+
49+
it('should create a new object with no template (promised)', () => {
50+
return ipfs.object.new()
51+
.then((node) => {
52+
const nodeJSON = node.toJSON()
53+
expect(nodeJSON.multihash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
54+
})
55+
})
56+
57+
it('should create a new object with unixfs-dir template', (done) => {
58+
ipfs.object.new('unixfs-dir', (err, node) => {
59+
expect(err).to.not.exist()
60+
const nodeJSON = node.toJSON()
61+
expect(nodeJSON.multihash)
62+
.to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
63+
done()
64+
})
65+
})
66+
})
67+
}

js/src/object/patch/add-link.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const dagPB = require('ipld-dag-pb')
9+
const DAGNode = dagPB.DAGNode
10+
const series = require('async/series')
11+
const { getDescribe, getIt } = require('../../utils/mocha')
12+
13+
const expect = chai.expect
14+
chai.use(dirtyChai)
15+
16+
module.exports = (createCommon, options) => {
17+
const describe = getDescribe(options)
18+
const it = getIt(options)
19+
const common = createCommon()
20+
21+
describe('.object.patch.addLink', function () {
22+
this.timeout(80 * 1000)
23+
24+
let ipfs
25+
26+
before(function (done) {
27+
// CI takes longer to instantiate the daemon, so we need to increase the
28+
// timeout for the before step
29+
this.timeout(60 * 1000)
30+
31+
common.setup((err, factory) => {
32+
expect(err).to.not.exist()
33+
factory.spawnNode((err, node) => {
34+
expect(err).to.not.exist()
35+
ipfs = node
36+
done()
37+
})
38+
})
39+
})
40+
41+
after((done) => common.teardown(done))
42+
43+
it('should add a link to an existing node', (done) => {
44+
let testNodeMultihash
45+
let node1a
46+
let node1b
47+
let node2
48+
49+
const obj = {
50+
Data: Buffer.from('patch test object'),
51+
Links: []
52+
}
53+
54+
series([
55+
(cb) => {
56+
ipfs.object.put(obj, (err, node) => {
57+
expect(err).to.not.exist()
58+
testNodeMultihash = node.multihash
59+
cb()
60+
})
61+
},
62+
(cb) => {
63+
DAGNode.create(obj.Data, obj.Links, (err, node) => {
64+
expect(err).to.not.exist()
65+
node1a = node
66+
cb()
67+
})
68+
},
69+
(cb) => {
70+
DAGNode.create(Buffer.from('some other node'), (err, node) => {
71+
expect(err).to.not.exist()
72+
node2 = node
73+
cb()
74+
})
75+
},
76+
(cb) => {
77+
// note: we need to put the linked obj, otherwise IPFS won't
78+
// timeout. Reason: it needs the node to get its size
79+
ipfs.object.put(node2, cb)
80+
},
81+
(cb) => {
82+
const link = node2.toJSON()
83+
link.name = 'link-to-node'
84+
DAGNode.addLink(node1a, link, (err, node) => {
85+
expect(err).to.not.exist()
86+
node1b = node
87+
cb()
88+
})
89+
},
90+
(cb) => {
91+
ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0], (err, node) => {
92+
expect(err).to.not.exist()
93+
expect(node1b.multihash).to.eql(node.multihash)
94+
cb()
95+
})
96+
}
97+
/* TODO: revisit this assertions.
98+
(cb) => {
99+
// note: make sure we can link js plain objects
100+
const content = Buffer.from(JSON.stringify({
101+
title: 'serialized object'
102+
}, null, 0))
103+
ipfs.add(content, (err, result) => {
104+
expect(err).to.not.exist()
105+
expect(result).to.exist()
106+
expect(result).to.have.lengthOf(1)
107+
const object = result.pop()
108+
node3 = {
109+
name: object.hash,
110+
multihash: object.hash,
111+
size: object.size
112+
}
113+
cb()
114+
})
115+
},
116+
(cb) => {
117+
ipfs.object.patch.addLink(testNodeWithLinkMultihash, node3, (err, node) => {
118+
expect(err).to.not.exist()
119+
expect(node).to.exist()
120+
testNodeWithLinkMultihash = node.multihash
121+
testLinkPlainObject = node3
122+
cb()
123+
})
124+
}
125+
*/
126+
], done)
127+
})
128+
129+
it('should add a link to an existing node (promised)', () => {
130+
let testNodeMultihash
131+
let node1a
132+
let node1b
133+
let node2
134+
135+
const obj = {
136+
Data: Buffer.from('patch test object (promised)'),
137+
Links: []
138+
}
139+
140+
return ipfs.object.put(obj)
141+
.then((node) => {
142+
testNodeMultihash = node.multihash
143+
})
144+
.then(() => new Promise((resolve, reject) => {
145+
DAGNode.create(obj.Data, obj.Links, function (err, node) {
146+
if (err) {
147+
return reject(err)
148+
}
149+
return resolve(node)
150+
})
151+
}))
152+
.then((node) => {
153+
node1a = node
154+
return new Promise((resolve, reject) => {
155+
DAGNode.create(Buffer.from('some other node'), function (err, node) {
156+
if (err) {
157+
return reject(err)
158+
}
159+
return resolve(node)
160+
})
161+
}).then((node1) => {
162+
node2 = node1
163+
return ipfs.object.put(node2)
164+
})
165+
})
166+
.then(() => {
167+
const link = node2.toJSON()
168+
link.name = 'link-to-node'
169+
return new Promise((resolve, reject) => {
170+
DAGNode.addLink(node1a, link, function (err, node) {
171+
if (err) {
172+
return reject(err)
173+
}
174+
return resolve(node)
175+
})
176+
}).then((node) => {
177+
node1b = node
178+
return ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0])
179+
})
180+
})
181+
.then((node) => {
182+
expect(node1b.multihash).to.eql(node.multihash)
183+
})
184+
})
185+
})
186+
}

js/src/object/patch/append-data.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const { getDescribe, getIt } = require('../../utils/mocha')
9+
10+
const expect = chai.expect
11+
chai.use(dirtyChai)
12+
13+
module.exports = (createCommon, options) => {
14+
const describe = getDescribe(options)
15+
const it = getIt(options)
16+
const common = createCommon()
17+
18+
describe('.object.patch.appendData', function () {
19+
this.timeout(80 * 1000)
20+
21+
let ipfs
22+
23+
before(function (done) {
24+
// CI takes longer to instantiate the daemon, so we need to increase the
25+
// timeout for the before step
26+
this.timeout(60 * 1000)
27+
28+
common.setup((err, factory) => {
29+
expect(err).to.not.exist()
30+
factory.spawnNode((err, node) => {
31+
expect(err).to.not.exist()
32+
ipfs = node
33+
done()
34+
})
35+
})
36+
})
37+
38+
after((done) => common.teardown(done))
39+
40+
it('should append data to an existing node', (done) => {
41+
const obj = {
42+
Data: Buffer.from('patch test object'),
43+
Links: []
44+
}
45+
46+
ipfs.object.put(obj, (err, node) => {
47+
expect(err).to.not.exist()
48+
49+
ipfs.object.patch.appendData(node.multihash, Buffer.from('append'), (err, patchedNode) => {
50+
expect(err).to.not.exist()
51+
expect(patchedNode.multihash).to.not.deep.equal(node.multihash)
52+
done()
53+
})
54+
})
55+
})
56+
57+
it('should append data to an existing node (promised)', () => {
58+
const obj = {
59+
Data: Buffer.from('patch test object (promised)'),
60+
Links: []
61+
}
62+
63+
return ipfs.object.put(obj)
64+
.then((node) => {
65+
return ipfs.object.patch.appendData(node.multihash, Buffer.from('append'))
66+
.then((patchedNode) => ({ patchedNode, node }))
67+
})
68+
.then(({ patchedNode, node }) => {
69+
expect(patchedNode.multihash).to.not.deep.equal(node.multihash)
70+
})
71+
})
72+
})
73+
}

js/src/object/patch/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
const { createSuite } = require('../../utils/suite')
3+
4+
const tests = {
5+
addLink: require('./add-link'),
6+
rmLink: require('./rm-link'),
7+
appendData: require('./append-data'),
8+
setData: require('./set-data')
9+
}
10+
11+
module.exports = createSuite(tests, 'patch')

0 commit comments

Comments
 (0)