Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 347b3d3

Browse files
committed
Merge pull request #63 from ipfs/feature/object
feature object
2 parents 957fa7d + 8e4b058 commit 347b3d3

17 files changed

+403
-24
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"rimraf": "^2.4.4",
5959
"standard": "^5.4.1",
6060
"transform-loader": "^0.2.3",
61-
"webpack": "diasdavid/webpack#81f5994"
61+
"webpack": "^2.0.5-beta"
6262
},
6363
"dependencies": {
6464
"bl": "^1.0.0",
@@ -67,7 +67,7 @@
6767
"debug": "^2.2.0",
6868
"hapi": "^12.0.0",
6969
"ipfs-blocks": "^0.1.0",
70-
"ipfs-merkle-dag": "^0.1.1",
70+
"ipfs-merkle-dag": "^0.2.1",
7171
"ipfs-repo": "^0.5.0",
7272
"lodash.get": "^4.0.0",
7373
"lodash.set": "^4.0.0",

src/cli/commands/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = Command.extend({
2424

2525
run: (bool, json, key, value) => {
2626
if (!key) {
27-
throw new Error('argument \'key\' is required')
27+
throw new Error("argument 'key' is required")
2828
}
2929

3030
var node = new IPFS()

src/ipfs-core/index.js

Lines changed: 137 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
const defaultRepo = require('./default-repo')
44
// const bl = require('bl')
5-
// const MerkleDAG = require('ipfs-merkle-dag')
65
const blocks = require('ipfs-blocks')
76
const BlockService = blocks.BlockService
8-
// const Block = MerkleDAG.Block
7+
const Block = blocks.Block
8+
const mDAG = require('ipfs-merkle-dag')
9+
const DAGNode = mDAG.DAGNode
10+
const DAGService = mDAG.DAGService
911

1012
exports = module.exports = IPFS
1113

@@ -17,7 +19,8 @@ function IPFS (repo) {
1719
if (!repo) {
1820
repo = defaultRepo()
1921
}
20-
const bs = new BlockService(repo)
22+
const blockS = new BlockService(repo)
23+
const dagS = new DAGService(blockS)
2124

2225
this.daemon = callback => {
2326
// 1. read repo to get peer data
@@ -133,16 +136,16 @@ function IPFS (repo) {
133136

134137
this.block = {
135138
get: (multihash, callback) => {
136-
bs.getBlock(multihash, callback)
139+
blockS.getBlock(multihash, callback)
137140
},
138141
put: (block, callback) => {
139-
bs.addBlock(block, callback)
142+
blockS.addBlock(block, callback)
140143
},
141144
del: (multihash, callback) => {
142-
bs.deleteBlock(multihash, callback)
145+
blockS.deleteBlock(multihash, callback)
143146
},
144147
stat: (multihash, callback) => {
145-
bs.getBlock(multihash, (err, block) => {
148+
blockS.getBlock(multihash, (err, block) => {
146149
if (err) {
147150
return callback(err)
148151
}
@@ -153,4 +156,131 @@ function IPFS (repo) {
153156
})
154157
}
155158
}
159+
160+
this.object = {
161+
new: (template, callback) => {
162+
if (!callback) {
163+
callback = template
164+
}
165+
var node = new DAGNode()
166+
var block = new Block(node.marshal())
167+
blockS.addBlock(block, function (err) {
168+
if (err) {
169+
return callback(err)
170+
}
171+
callback(null, {
172+
Hash: block.key,
173+
Size: node.size(),
174+
Name: ''
175+
})
176+
})
177+
},
178+
patch: {
179+
appendData: (multihash, data, callback) => {
180+
this.object.get(multihash, (err, obj) => {
181+
if (err) { return callback(err) }
182+
obj.data = Buffer.concat([obj.data, data])
183+
dagS.add(obj, (err) => {
184+
if (err) {
185+
return callback(err)
186+
}
187+
callback(null, obj.multihash())
188+
})
189+
})
190+
},
191+
addLink: (multihash, link, callback) => {
192+
this.object.get(multihash, (err, obj) => {
193+
if (err) { return callback(err) }
194+
obj.addRawLink(link)
195+
dagS.add(obj, (err) => {
196+
if (err) {
197+
return callback(err)
198+
}
199+
callback(null, obj.multihash())
200+
})
201+
})
202+
},
203+
rmLink: (multihash, multihashLink, callback) => {
204+
this.object.get(multihash, (err, obj) => {
205+
if (err) { return callback(err) }
206+
obj.links = obj.links.filter((link) => {
207+
if (link.hash.equals(multihashLink)) {
208+
return false
209+
}
210+
return true
211+
})
212+
dagS.add(obj, (err) => {
213+
if (err) {
214+
return callback(err)
215+
}
216+
callback(null, obj.multihash())
217+
})
218+
})
219+
},
220+
setData: (multihash, data, callback) => {
221+
this.object.get(multihash, (err, obj) => {
222+
if (err) { return callback(err) }
223+
obj.data = data
224+
dagS.add(obj, (err) => {
225+
if (err) {
226+
return callback(err)
227+
}
228+
callback(null, obj.multihash())
229+
})
230+
})
231+
}
232+
},
233+
data: (multihash, callback) => {
234+
this.object.get(multihash, (err, obj) => {
235+
if (err) {
236+
return callback(err)
237+
}
238+
callback(null, obj.data)
239+
})
240+
},
241+
links: (multihash, callback) => {
242+
this.object.get(multihash, (err, obj) => {
243+
if (err) {
244+
return callback(err)
245+
}
246+
callback(null, obj.links)
247+
})
248+
},
249+
get: (multihash, options, callback) => {
250+
if (typeof options === 'function') {
251+
callback = options
252+
options = {}
253+
}
254+
dagS.get(multihash, callback)
255+
},
256+
put: (dagNode, options, callback) => {
257+
if (typeof options === 'function') {
258+
callback = options
259+
options = {}
260+
}
261+
dagS.add(dagNode, callback)
262+
},
263+
stat: (multihash, options, callback) => {
264+
if (typeof options === 'function') {
265+
callback = options
266+
options = {}
267+
}
268+
269+
this.object.get(multihash, (err, obj) => {
270+
if (err) {
271+
return callback(err)
272+
}
273+
var res = {
274+
NumLinks: obj.links.length,
275+
BlockSize: obj.marshal().length,
276+
LinksSize: obj.links.reduce((prev, link) => {
277+
return prev + link.size
278+
}, 0),
279+
DataSize: obj.data.length,
280+
CumulativeSize: ''
281+
}
282+
callback(null, res)
283+
})
284+
}
285+
}
156286
}

tests/repo-example/blocks/12203aa9/12203aa913048b2ef582d4ee2bf6eccad869e2ae1d9e41a2df4e086fe2e1403e8a5b.data

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/
2+
" wҢ���x�G�ܙ��}X���������yZdirect�T2
3+
" �˧7Fw�}�|ɣ�*�#��'V>�|� recursive�T
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/
2+
" ��!6���1�ص��F��2$`7#u@1�direct�T2
3+
" ����Hz�8�#3u2�ED� ��ƥ��*Q�KMQ� recursive�T

tests/repo-example/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
��# 0.1 - Quick Start
3+
4+
This is a set of short examples with minimal explanation. It is meant as
5+
a "quick start". Soon, we'll write a longer tour :-)
6+
7+
8+
Add a file to ipfs:
9+
10+
echo "hello world" >hello
11+
ipfs add hello
12+
13+
14+
View it:
15+
16+
ipfs cat <the-hash-you-got-here>
17+
18+
19+
Try a directory:
20+
21+
mkdir foo
22+
mkdir foo/bar
23+
echo "baz" > foo/baz
24+
echo "baz" > foo/bar/baz
25+
ipfs add -r foo
26+
27+
28+
View things:
29+
30+
ipfs ls <the-hash-here>
31+
ipfs ls <the-hash-here>/bar
32+
ipfs cat <the-hash-here>/baz
33+
ipfs cat <the-hash-here>/bar/baz
34+
ipfs cat <the-hash-here>/bar
35+
ipfs ls <the-hash-here>/baz
36+
37+
38+
References:
39+
40+
ipfs refs <the-hash-here>
41+
ipfs refs -r <the-hash-here>
42+
ipfs refs --help
43+
44+
45+
Get:
46+
47+
ipfs get <the-hash-here> -o foo2
48+
diff foo foo2
49+
50+
51+
Objects:
52+
53+
ipfs object get <the-hash-here>
54+
ipfs object get <the-hash-here>/foo2
55+
ipfs object --help
56+
57+
58+
Pin + GC:
59+
60+
ipfs pin add <the-hash-here>
61+
ipfs repo gc
62+
ipfs ls <the-hash-here>
63+
ipfs pin rm <the-hash-here>
64+
ipfs repo gc
65+
66+
67+
Daemon:
68+
69+
ipfs daemon (in another terminal)
70+
ipfs id
71+
72+
73+
Network:
74+
75+
(must be online)
76+
ipfs swarm peers
77+
ipfs id
78+
ipfs cat <hash-of-remote-object>
79+
80+
81+
Mount:
82+
83+
(warning: fuse is finicky!)
84+
ipfs mount
85+
cd /ipfs/<the-hash-here>
86+
ls
87+
88+
89+
Tool:
90+
91+
ipfs version
92+
ipfs update
93+
ipfs commands
94+
ipfs config --help
95+
open http://localhost:5001/webui
96+
97+
98+
Browse:
99+
100+
webui:
101+
102+
http://localhost:5001/webui
103+
104+
video:
105+
106+
http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXse
107+
108+
images:
109+
110+
http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs
111+
112+
markdown renderer app:
113+
114+
http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown
115+
�
141 Bytes
Binary file not shown.

tests/test-cli/test-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('config', () => {
6666
it('set a config key with invalid json', done => {
6767
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'])
6868
.run((err, stdout, exitcode) => {
69-
const expected = 'error invalid JSON provided'
69+
const expected = 'error\tinvalid JSON provided'
7070
expect(stdout[0]).to.equal(expected)
7171
expect(err).to.not.exist
7272
expect(exitcode).to.equal(1)
@@ -77,7 +77,7 @@ describe('config', () => {
7777
it('call config with no arguments', done => {
7878
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'])
7979
.run((err, stdout, exitcode) => {
80-
const expected = 'error argument \'key\' is required'
80+
const expected = "error\targument 'key' is required"
8181
expect(stdout[0]).to.equal(expected)
8282
expect(err).to.not.exist
8383
expect(exitcode).to.equal(1)

tests/test-core/browser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const _ = require('lodash')
77
const repoContext = require.context('buffer!./../repo-example', true)
88

99
const idb = window.indexedDB ||
10-
window.mozIndexedDB ||
11-
window.webkitIndexedDB ||
12-
window.msIndexedDB
10+
window.mozIndexedDB ||
11+
window.webkitIndexedDB ||
12+
window.msIndexedDB
1313

1414
idb.deleteDatabase('ipfs')
1515
idb.deleteDatabase('ipfs/blocks')

tests/test-core/test-block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('block', function () {
6161

6262
it('stat', function (done) {
6363
const mh = new Buffer(base58
64-
.decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'))
64+
.decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'))
6565
ipfs.block.stat(mh, (err, stats) => {
6666
expect(err).to.not.exist
6767
expect(stats.Key.equals(mh)).to.equal(true)

tests/test-core/test-config.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ describe('config', () => {
9090
})
9191
})
9292

93-
// cli only feature built with show and replace
94-
// it.skip('edit', done => {
95-
// const ipfs = new IPFS()
96-
// ipfs.config((err, config) => {
97-
// expect(err).to.not.exist
98-
// done()
99-
// })
100-
// })
93+
// cli only feature built with show and replace
94+
// it.skip('edit', done => {
95+
// const ipfs = new IPFS()
96+
// ipfs.config((err, config) => {
97+
// expect(err).to.not.exist
98+
// done()
99+
// })
100+
// })
101101
})

0 commit comments

Comments
 (0)