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.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
439 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict' | ||
|
||
const { print } = require('../utils') | ||
|
||
const Format = { | ||
default: '<dst>', | ||
edges: '<src> -> <dst>' | ||
} | ||
|
||
module.exports = { | ||
command: 'refs <key>', | ||
|
||
describe: 'List links (references) from an object', | ||
|
||
builder: { | ||
r: { | ||
alias: 'recursive', | ||
desc: 'Recursively list links of child nodes.', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
format: { | ||
desc: 'Output edges with given format. Available tokens: <src> <dst> <linkname>.', | ||
type: 'string', | ||
default: Format.default | ||
}, | ||
e: { | ||
alias: 'edges', | ||
desc: 'Output edge format: `<from> -> <to>`', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
u: { | ||
alias: 'unique', | ||
desc: 'Omit duplicate refs from output.', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
'max-depth': { | ||
desc: 'Only for recursive refs, limits fetch and listing to the given depth.', | ||
type: 'number' | ||
} | ||
}, | ||
|
||
handler ({ getIpfs, key, recursive, format, e, u, resolve, maxDepth }) { | ||
resolve((async () => { | ||
if (format !== Format.default && e) { | ||
throw new Error('Cannot set edges to true and also specify format') | ||
} | ||
|
||
if (maxDepth === 0) { | ||
return | ||
} | ||
|
||
const ipfs = await getIpfs() | ||
let links = await ipfs.refs(key, { recursive, maxDepth }) | ||
if (!links.length) { | ||
return | ||
} | ||
|
||
const linkDAG = getLinkDAG(links) | ||
format = e ? Format.edges : format || Format.default | ||
printLinks(linkDAG, links[0], format, u && new Set()) | ||
})()) | ||
} | ||
} | ||
|
||
function getLinkDAG (links) { | ||
const linkNames = {} | ||
for (const link of links) { | ||
linkNames[link.name] = link | ||
} | ||
|
||
const linkDAG = {} | ||
for (const link of links) { | ||
const parentName = link.path.substring(0, link.path.lastIndexOf('/')) | ||
linkDAG[parentName] = linkDAG[parentName] || [] | ||
linkDAG[parentName].push(link) | ||
} | ||
return linkDAG | ||
} | ||
|
||
function printLinks (linkDAG, link, format, uniques) { | ||
const children = linkDAG[link.path] || [] | ||
for (const child of children) { | ||
if (!uniques || !uniques.has(child.hash)) { | ||
uniques && uniques.add(child.hash) | ||
printLink(link, child, format) | ||
printLinks(linkDAG, child, format, uniques) | ||
} | ||
} | ||
} | ||
|
||
function printLink (src, dst, format) { | ||
let out = format.replace(/<src>/g, src.hash) | ||
out = out.replace(/<dst>/g, dst.hash) | ||
out = out.replace(/<linkname>/g, dst.name) | ||
print(out) | ||
} | ||
|
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,32 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-exporter') | ||
const pull = require('pull-stream') | ||
const { normalizePath } = require('./utils') | ||
|
||
module.exports = function (self) { | ||
return function (ipfsPath, options = {}) { | ||
const path = normalizePath(ipfsPath) | ||
const pathComponents = path.split('/') | ||
const pathDepth = pathComponents.length | ||
if (options.maxDepth === undefined) { | ||
options.maxDepth = options.recursive ? global.Infinity : pathDepth | ||
} else { | ||
options.maxDepth = options.maxDepth + pathDepth - 1 | ||
} | ||
|
||
if (options.preload !== false) { | ||
self._preload(pathComponents[0]) | ||
} | ||
|
||
return pull( | ||
exporter(ipfsPath, self._ipld, options), | ||
pull.map(node => { | ||
node.hash = node.cid.toString() | ||
delete node.cid | ||
delete node.content | ||
return node | ||
}) | ||
) | ||
} | ||
} |
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,25 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream') | ||
|
||
module.exports = function (self) { | ||
return promisify((ipfsPath, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = options || {} | ||
|
||
pull( | ||
self.refsPullStream(ipfsPath, options), | ||
pull.collect((err, values) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, values) | ||
}) | ||
) | ||
}) | ||
} |
Oops, something went wrong.