-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add object endpoints and cli commands #85
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
if (!key) { | ||
throw new Error("Argument 'key' is required") | ||
} | ||
|
||
var ipfs = utils.getIPFS() | ||
|
||
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) | ||
}) | ||
} | ||
}) |
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) => { | ||
if (!key) { | ||
throw new Error("Argument 'key' is required") | ||
} | ||
|
||
var ipfs = utils.getIPFS() | ||
|
||
if (utils.isDaemonOn()) { | ||
return ipfs.object.get(key, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log(JSON.stringify(obj)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this print in the same way that go-ipfs does? I believe go-ipfs does some formatting rather then printing the obj directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool :) |
||
}) | ||
} | ||
|
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which seems you do it here :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's because core returns an object with lowercase keys and the multihashes as a buffer, on the other hand the |
||
})) | ||
}) | ||
} | ||
}) |
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) => { | ||
if (!key) { | ||
throw new Error("Argument 'key' is required") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you do this before utils.getIPFS() ? |
||
} | ||
|
||
var ipfs = utils.getIPFS() | ||
|
||
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) | ||
}) | ||
}) | ||
} | ||
}) |
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()) | ||
}) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'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') | ||
|
||
function parseJSONBuffer (buf) { | ||
try { | ||
const parsed = JSON.parse(buf.toString()) | ||
return { | ||
data: new Buffer(parsed.Data), | ||
links: parsed.Links ? parsed.Links.map((link) => ({ | ||
name: link.Name, | ||
hash: new Buffer(bs58.decode(link.Hash)), | ||
size: link.Size | ||
})) : [] | ||
} | ||
} catch (err) { | ||
log.error(err) | ||
throw new Error('failed to parse JSON: ' + err) | ||
} | ||
} | ||
|
||
function parseAndAddNode (buf) { | ||
var ipfs = utils.getIPFS() | ||
|
||
if (utils.isDaemonOn()) { | ||
return ipfs.object.put(buf, 'json', (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log('added', obj.Hash) | ||
}) | ||
} | ||
|
||
const parsed = parseJSONBuffer(buf) | ||
const dagNode = new DAGNode(parsed.data, parsed.links) | ||
ipfs.object.put(dagNode, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log('added', bs58.encode(dagNode.multihash()).toString()) | ||
}) | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Stores input as a DAG object, outputs its key', | ||
|
||
options: {}, | ||
|
||
run: (filePath) => { | ||
if (filePath) { | ||
return parseAndAddNode(fs.readFileSync(filePath)) | ||
} | ||
|
||
process.stdin.pipe(bl((err, input) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
parseAndAddNode(input) | ||
})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this whole command could be more simple. Also, seems like I can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to
Will try to simplify it though :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. I meant that it doesn't seem the code to prevent something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
}) |
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) => { | ||
if (!key) { | ||
throw new Error("Argument 'key' is required") | ||
} | ||
|
||
var ipfs = utils.getIPFS() | ||
|
||
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]}`) | ||
}) | ||
}) | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js-ipfs-api
'sobject.data
accepts multihashes as a string, core needs theBuffer
instead.js-ipfs-api
returns astream
, core returns aBuffer