Files: e5ddc28fe060afb6aca444382f003022e1eac2f5 / index.js
2334 bytesRaw
1 | const ssbKeys = require('ssb-keys'); |
2 | const ssbConfigInject = require('ssb-config/inject'); |
3 | const path = require('path'); |
4 | const pify = require('pify'); |
5 | |
6 | function startScuttlebot() { |
7 | const config = ssbConfigInject(); |
8 | config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')); |
9 | config.port = 8198; |
10 | config.logging.level = ''; |
11 | return require('scuttlebot/index') |
12 | .use(require('scuttlebot/plugins/plugins')) |
13 | .use(require('scuttlebot/plugins/master')) |
14 | .use(require('scuttlebot/plugins/replicate')) |
15 | .use(require('ssb-friends')) |
16 | .use(require('ssb-names')) |
17 | .call(null, config); |
18 | } |
19 | |
20 | async function buildGraph(sbot) { |
21 | const hopsData = await pify(sbot.friends.hops)(); |
22 | const nodes = Object.keys(hopsData); |
23 | const nodeLabels = {}; |
24 | for (let key of nodes) { |
25 | try { |
26 | nodeLabels[key] = await pify(sbot.names.getSignifier)(key); |
27 | } catch (error) { |
28 | nodeLabels[key] = ''; |
29 | } |
30 | } |
31 | const follows = await pify(sbot.friends.get)(); |
32 | const edges = Object.keys(follows) |
33 | .map(orig => |
34 | Object.keys(follows[orig]) |
35 | .filter(dest => follows[orig][dest] === true) |
36 | .map(dest => [orig, dest]), |
37 | ) |
38 | .reduce((accumulated, current) => accumulated.concat(current), []); |
39 | return {nodes, edges, nodeLabels}; |
40 | } |
41 | |
42 | function* toGraphml({nodes, edges, nodeLabels}) { |
43 | yield '<?xml version="1.0" encoding="UTF-8"?>\n'; |
44 | yield `<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
45 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
46 | xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
47 | http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">\n`; |
48 | yield ' <graph id="G" edgedefault="directed">\n'; |
49 | yield ' <key id="d0" for="node" attr.name="label" attr.type="string"/>\n'; |
50 | for (let id of nodes) { |
51 | const name = nodeLabels[id]; |
52 | if (name) { |
53 | yield ` <node id="${id}">\n`; |
54 | yield ` <data key="d0">${encodeURIComponent(name)}</data>\n`; |
55 | yield ` </node>\n`; |
56 | } else { |
57 | yield ` <node id="${id}"/>\n`; |
58 | } |
59 | } |
60 | for (let [orig, dest] of edges) { |
61 | yield ` <edge source="${orig}" target="${dest}"/>\n`; |
62 | } |
63 | yield ' </graph>\n</graphml>'; |
64 | } |
65 | |
66 | module.exports = async function run() { |
67 | const sbot = startScuttlebot(); |
68 | const graph = await buildGraph(sbot); |
69 | sbot.close(); |
70 | return toGraphml(graph); |
71 | }; |
72 |
Built with git-ssb-web