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

Commit

Permalink
Add object endpoints and cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaiodias committed Mar 14, 2016
1 parent d9b4a38 commit b6c07a3
Show file tree
Hide file tree
Showing 15 changed files with 1,187 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@
"webpack": "^2.0.7-beta"
},
"dependencies": {
"bl": "^1.0.0",
"bl": "^1.1.2",
"boom": "^3.1.1",
"bs58": "^3.0.0",
"debug": "^2.2.0",
"hapi": "^12.0.0",
"ipfs-api": "^2.13.1",
"ipfs-blocks": "^0.1.0",
"ipfs-merkle-dag": "^0.2.1",
"ipfs-multipart": "0.0.1",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.5.0",
"joi": "^8.0.2",
"lodash.get": "^4.0.0",
Expand Down
41 changes: 41 additions & 0 deletions src/cli/commands/object/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: 'Outputs the raw bytes in an IPFS object',

options: {},

run: (key) => {
var ipfs = utils.getIPFS()

if (!key) {
throw new Error("Argument 'key' is required")
}

const mh = utils.isDaemonOn()
? key
: new Buffer(bs58.decode(key))

ipfs.object.data(mh, (err, data) => {
if (err) {
log.error(err)
throw err
}

if (data instanceof Buffer) {
console.log(data.toString())
return
}

// js-ipfs-api output (http stream)
data.pipe(process.stdout)
})
}
})
50 changes: 50 additions & 0 deletions src/cli/commands/object/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: 'Get and serialize the DAG node named by <key>',

options: {},

run: (key) => {
var ipfs = utils.getIPFS()

if (!key) {
throw new Error("Argument 'key' is required")
}

if (utils.isDaemonOn()) {
return ipfs.object.get(key, (err, obj) => {
if (err) {
log.error(err)
throw err
}

console.log(JSON.stringify(obj))
})
}

const mh = new Buffer(bs58.decode(key))
ipfs.object.get(mh, (err, obj) => {
if (err) {
log.error(err)
throw err
}

console.log(JSON.stringify({
Links: obj.links.map((link) => ({
Name: link.name,
Hash: bs58.encode(link.hash).toString(),
Size: link.size
})),
Data: obj.data.toString()
}))
})
}
})
44 changes: 44 additions & 0 deletions src/cli/commands/object/links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: 'Outputs the links pointed to by the specified object',

options: {},

run: (key) => {
var ipfs = utils.getIPFS()

if (!key) {
throw new Error("Argument 'key' is required")
}

const mh = utils.isDaemonOn()
? key
: new Buffer(bs58.decode(key))

ipfs.object.links(mh, (err, links) => {
if (err) {
log.error(err)
throw err
}

if (links.Links) { // js-ipfs-api output
links.Links.forEach((link) => {
console.log(link.Hash, link.Size, link.Name)
})
return
}

links.forEach((link) => {
console.log(bs58.encode(link.hash).toString(), link.size, link.name)
})
})
}
})
32 changes: 32 additions & 0 deletions src/cli/commands/object/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: 'Create new ipfs objects',

options: {},

run: (template) => {
var ipfs = utils.getIPFS()

ipfs.object.new(template, (err, obj) => {
if (err) {
log.error(err)
throw err
}

if (typeof obj.Hash === 'string') { // js-ipfs-api output
console.log(obj.Hash)
return
}

console.log(bs58.encode(obj.Hash).toString())
})
}
})
75 changes: 75 additions & 0 deletions src/cli/commands/object/put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const bl = require('bl')
const fs = require('fs')
const mDAG = require('ipfs-merkle-dag')
const DAGNode = mDAG.DAGNode
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: 'Stores input as a DAG object, outputs its key',

options: {},

run: (filePath) => {
var ipfs = utils.getIPFS()

function parseAndAddNode (buf) {
if (utils.isDaemonOn()) {
return ipfs.object.put(buf, 'json', (err, obj) => {
if (err) {
log.error(err)
throw err
}

console.log('added', obj.Hash)
})
}

let parsed
try {
parsed = JSON.parse(buf.toString())
parsed.Links = parsed.Links || []
} catch (err) {
log.error(err)
throw new Error('failed to parse JSON: ' + err)
}

const data = new Buffer(parsed.Data)
const links = parsed.Links.map((link) => ({
name: link.Name,
hash: new Buffer(bs58.decode(link.Hash)),
size: link.Size
}))

const dagNode = new DAGNode(data, links)

ipfs.object.put(dagNode, (err, obj) => {
if (err) {
log.error(err)
throw err
}

console.log('added', bs58.encode(dagNode.multihash()).toString())
})
}

if (filePath) {
return parseAndAddNode(fs.readFileSync(filePath))
}

process.stdin.pipe(bl((err, input) => {
if (err) {
log.error(err)
throw err
}

parseAndAddNode(input)
}))
}
})
39 changes: 39 additions & 0 deletions src/cli/commands/object/stat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: 'Get stats for the DAG node named by <key>',

options: {},

run: (key) => {
var ipfs = utils.getIPFS()

if (!key) {
throw new Error("Argument 'key' is required")
}

const mh = utils.isDaemonOn()
? key
: new Buffer(bs58.decode(key))

ipfs.object.stat(mh, (err, stats) => {
if (err) {
log.error(err)
throw err
}

delete stats.Hash // only for js-ipfs-api output

Object.keys(stats).forEach((key) => {
console.log(`${key}: ${stats[key]}`)
})
})
}
})
Loading

0 comments on commit b6c07a3

Please sign in to comment.