Files: 0bed468ddc8682a39b1a3c0ad3d77d6cbe455b7a / index.js
2693 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 nodeSeqs = {}; |
32 | for (let key of nodes) { |
33 | try { |
34 | nodeSeqs[key] = await pify(sbot.latestSequence)(key); |
35 | } catch (error) { |
36 | nodeSeqs[key] = 0; |
37 | } |
38 | } |
39 | const follows = await pify(sbot.friends.get)(); |
40 | const edges = Object.keys(follows) |
41 | .map(orig => |
42 | Object.keys(follows[orig]) |
43 | .filter(dest => follows[orig][dest] === true) |
44 | .map(dest => [orig, dest]), |
45 | ) |
46 | .reduce((accumulated, current) => accumulated.concat(current), []); |
47 | return {nodes, edges, nodeLabels, nodeSeqs}; |
48 | } |
49 | |
50 | function* toGraphml({nodes, edges, nodeLabels, nodeSeqs}) { |
51 | yield '<?xml version="1.0" encoding="UTF-8"?>\n'; |
52 | yield `<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
53 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
54 | xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
55 | http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">\n`; |
56 | yield ' <graph id="G" edgedefault="directed">\n'; |
57 | yield ' <key id="d0" for="node" attr.name="label" attr.type="string"/>\n'; |
58 | yield ' <key id="d1" for="node" attr.name="seq" attr.type="int"/>\n'; |
59 | for (let id of nodes) { |
60 | const name = nodeLabels[id]; |
61 | const seq = nodeSeqs[id]; |
62 | if (name && seq) { |
63 | yield ` <node id="${id}">\n`; |
64 | yield ` <data key="d0">${encodeURIComponent(name)}</data>\n`; |
65 | yield ` <data key="d1">${seq}</data>\n`; |
66 | yield ` </node>\n`; |
67 | } else { |
68 | yield ` <node id="${id}"/>\n`; |
69 | } |
70 | } |
71 | for (let [orig, dest] of edges) { |
72 | yield ` <edge source="${orig}" target="${dest}"/>\n`; |
73 | } |
74 | yield ' </graph>\n</graphml>'; |
75 | } |
76 | |
77 | module.exports = async function run() { |
78 | const sbot = startScuttlebot(); |
79 | const graph = await buildGraph(sbot); |
80 | sbot.close(); |
81 | return toGraphml(graph); |
82 | }; |
83 |
Built with git-ssb-web