This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add object endpoints and cli commands
- Loading branch information
Showing
15 changed files
with
1,192 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
|
||
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() | ||
})) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
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) | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
})) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}`) | ||
}) | ||
}) | ||
} | ||
}) |
Oops, something went wrong.