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

Commit 826eaf8

Browse files
committed
Merge pull request #85 from ipfs/feature/object
Add object endpoints and cli commands
2 parents d9b4a38 + a0785af commit 826eaf8

File tree

15 files changed

+1192
-15
lines changed

15 files changed

+1192
-15
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@
6868
"webpack": "^2.0.7-beta"
6969
},
7070
"dependencies": {
71-
"bl": "^1.0.0",
71+
"bl": "^1.1.2",
7272
"boom": "^3.1.1",
7373
"bs58": "^3.0.0",
7474
"debug": "^2.2.0",
7575
"hapi": "^12.0.0",
7676
"ipfs-api": "^2.13.1",
7777
"ipfs-blocks": "^0.1.0",
7878
"ipfs-merkle-dag": "^0.2.1",
79-
"ipfs-multipart": "0.0.1",
79+
"ipfs-multipart": "^0.1.0",
8080
"ipfs-repo": "^0.5.0",
8181
"joi": "^8.0.2",
8282
"lodash.get": "^4.0.0",

src/cli/commands/object/data.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const utils = require('../../utils')
5+
const bs58 = require('bs58')
6+
const debug = require('debug')
7+
const log = debug('cli:object')
8+
log.error = debug('cli:object:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Outputs the raw bytes in an IPFS object',
12+
13+
options: {},
14+
15+
run: (key) => {
16+
if (!key) {
17+
throw new Error("Argument 'key' is required")
18+
}
19+
20+
var ipfs = utils.getIPFS()
21+
22+
const mh = utils.isDaemonOn()
23+
? key
24+
: new Buffer(bs58.decode(key))
25+
26+
ipfs.object.data(mh, (err, data) => {
27+
if (err) {
28+
log.error(err)
29+
throw err
30+
}
31+
32+
if (data instanceof Buffer) {
33+
console.log(data.toString())
34+
return
35+
}
36+
37+
// js-ipfs-api output (http stream)
38+
data.pipe(process.stdout)
39+
})
40+
}
41+
})

src/cli/commands/object/get.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const utils = require('../../utils')
5+
const bs58 = require('bs58')
6+
const debug = require('debug')
7+
const log = debug('cli:object')
8+
log.error = debug('cli:object:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Get and serialize the DAG node named by <key>',
12+
13+
options: {},
14+
15+
run: (key) => {
16+
if (!key) {
17+
throw new Error("Argument 'key' is required")
18+
}
19+
20+
var ipfs = utils.getIPFS()
21+
22+
if (utils.isDaemonOn()) {
23+
return ipfs.object.get(key, (err, obj) => {
24+
if (err) {
25+
log.error(err)
26+
throw err
27+
}
28+
29+
console.log(JSON.stringify(obj))
30+
})
31+
}
32+
33+
const mh = new Buffer(bs58.decode(key))
34+
ipfs.object.get(mh, (err, obj) => {
35+
if (err) {
36+
log.error(err)
37+
throw err
38+
}
39+
40+
console.log(JSON.stringify({
41+
Links: obj.links.map((link) => ({
42+
Name: link.name,
43+
Hash: bs58.encode(link.hash).toString(),
44+
Size: link.size
45+
})),
46+
Data: obj.data.toString()
47+
}))
48+
})
49+
}
50+
})

src/cli/commands/object/links.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const utils = require('../../utils')
5+
const bs58 = require('bs58')
6+
const debug = require('debug')
7+
const log = debug('cli:object')
8+
log.error = debug('cli:object:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Outputs the links pointed to by the specified object',
12+
13+
options: {},
14+
15+
run: (key) => {
16+
if (!key) {
17+
throw new Error("Argument 'key' is required")
18+
}
19+
20+
var ipfs = utils.getIPFS()
21+
22+
const mh = utils.isDaemonOn()
23+
? key
24+
: new Buffer(bs58.decode(key))
25+
26+
ipfs.object.links(mh, (err, links) => {
27+
if (err) {
28+
log.error(err)
29+
throw err
30+
}
31+
32+
if (links.Links) { // js-ipfs-api output
33+
links.Links.forEach((link) => {
34+
console.log(link.Hash, link.Size, link.Name)
35+
})
36+
return
37+
}
38+
39+
links.forEach((link) => {
40+
console.log(bs58.encode(link.hash).toString(), link.size, link.name)
41+
})
42+
})
43+
}
44+
})

src/cli/commands/object/new.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const utils = require('../../utils')
5+
const bs58 = require('bs58')
6+
const debug = require('debug')
7+
const log = debug('cli:object')
8+
log.error = debug('cli:object:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Create new ipfs objects',
12+
13+
options: {},
14+
15+
run: (template) => {
16+
var ipfs = utils.getIPFS()
17+
18+
ipfs.object.new(template, (err, obj) => {
19+
if (err) {
20+
log.error(err)
21+
throw err
22+
}
23+
24+
if (typeof obj.Hash === 'string') { // js-ipfs-api output
25+
console.log(obj.Hash)
26+
return
27+
}
28+
29+
console.log(bs58.encode(obj.Hash).toString())
30+
})
31+
}
32+
})

src/cli/commands/object/put.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const utils = require('../../utils')
5+
const bs58 = require('bs58')
6+
const bl = require('bl')
7+
const fs = require('fs')
8+
const mDAG = require('ipfs-merkle-dag')
9+
const DAGNode = mDAG.DAGNode
10+
const debug = require('debug')
11+
const log = debug('cli:object')
12+
log.error = debug('cli:object:error')
13+
14+
function parseJSONBuffer (buf) {
15+
try {
16+
const parsed = JSON.parse(buf.toString())
17+
return {
18+
data: new Buffer(parsed.Data),
19+
links: parsed.Links ? parsed.Links.map((link) => ({
20+
name: link.Name,
21+
hash: new Buffer(bs58.decode(link.Hash)),
22+
size: link.Size
23+
})) : []
24+
}
25+
} catch (err) {
26+
log.error(err)
27+
throw new Error('failed to parse JSON: ' + err)
28+
}
29+
}
30+
31+
function parseAndAddNode (buf) {
32+
var ipfs = utils.getIPFS()
33+
34+
if (utils.isDaemonOn()) {
35+
return ipfs.object.put(buf, 'json', (err, obj) => {
36+
if (err) {
37+
log.error(err)
38+
throw err
39+
}
40+
41+
console.log('added', obj.Hash)
42+
})
43+
}
44+
45+
const parsed = parseJSONBuffer(buf)
46+
const dagNode = new DAGNode(parsed.data, parsed.links)
47+
ipfs.object.put(dagNode, (err, obj) => {
48+
if (err) {
49+
log.error(err)
50+
throw err
51+
}
52+
53+
console.log('added', bs58.encode(dagNode.multihash()).toString())
54+
})
55+
}
56+
57+
module.exports = Command.extend({
58+
desc: 'Stores input as a DAG object, outputs its key',
59+
60+
options: {},
61+
62+
run: (filePath) => {
63+
if (filePath) {
64+
return parseAndAddNode(fs.readFileSync(filePath))
65+
}
66+
67+
process.stdin.pipe(bl((err, input) => {
68+
if (err) {
69+
log.error(err)
70+
throw err
71+
}
72+
73+
parseAndAddNode(input)
74+
}))
75+
}
76+
})

src/cli/commands/object/stat.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const utils = require('../../utils')
5+
const bs58 = require('bs58')
6+
const debug = require('debug')
7+
const log = debug('cli:object')
8+
log.error = debug('cli:object:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Get stats for the DAG node named by <key>',
12+
13+
options: {},
14+
15+
run: (key) => {
16+
if (!key) {
17+
throw new Error("Argument 'key' is required")
18+
}
19+
20+
var ipfs = utils.getIPFS()
21+
22+
const mh = utils.isDaemonOn()
23+
? key
24+
: new Buffer(bs58.decode(key))
25+
26+
ipfs.object.stat(mh, (err, stats) => {
27+
if (err) {
28+
log.error(err)
29+
throw err
30+
}
31+
32+
delete stats.Hash // only for js-ipfs-api output
33+
34+
Object.keys(stats).forEach((key) => {
35+
console.log(`${key}: ${stats[key]}`)
36+
})
37+
})
38+
}
39+
})

0 commit comments

Comments
 (0)