git ssb

16+

cel / patchfoo



Commit 487a99ede50bdbbd5308b1b446b9fc354d0a8c27

Add peers page

cel committed on 4/5/2017, 12:40:09 AM
Parent: ba3a576d5ae23bed296e9d2329b42e975482fc1a

Files changed

lib/app.jschanged
lib/serve.jschanged
lib/app.jsView
@@ -156,4 +156,32 @@
156156 return opts.sortByTimestamp
157157 ? this.sbot.createFeedStream(opts)
158158 : this.sbot.createLogStream(opts)
159159 }
160 +
161 +var stateVals = {
162 + connected: 3,
163 + connecting: 2,
164 + disconnecting: 1,
165 +}
166 +
167 +function comparePeers(a, b) {
168 + var aState = stateVals[a.state] || 0
169 + var bState = stateVals[b.state] || 0
170 + return bState > aState ? 1 : bState < aState ? -1 :
171 + b.stateChange - a.stateChange
172 +}
173 +
174 +App.prototype.streamPeers = function (opts) {
175 + var gossip = this.sbot.gossip
176 + return u.readNext(function (cb) {
177 + gossip.peers(function (err, peers) {
178 + if (err) return cb(err)
179 + if (opts) peers = peers.filter(function (peer) {
180 + for (var k in opts) if (opts[k] !== peer[k]) return false
181 + return true
182 + })
183 + peers.sort(comparePeers)
184 + cb(null, pull.values(peers))
185 + })
186 + })
187 +}
lib/serve.jsView
@@ -16,8 +16,9 @@
1616 var pkg = require('../package')
1717 var Busboy = require('busboy')
1818 var mime = require('mime-types')
1919 var ident = require('pull-identify-filetype')
20 +var htime = require('human-time')
2021
2122 module.exports = Serve
2223
2324 var emojiDir = path.join(require.resolve('emoji-named-characters'), '../pngs')
@@ -36,8 +37,9 @@
3637
3738 function ctype(name) {
3839 switch (name && /[^.\/]*$/.exec(name)[0] || 'html') {
3940 case 'html': return 'text/html'
41 + case 'txt': return 'text/plain'
4042 case 'js': return 'text/javascript'
4143 case 'css': return 'text/css'
4244 case 'png': return 'image/png'
4345 case 'json': return 'application/json'
@@ -208,8 +210,9 @@
208210 case '/public': return this.public(m[2])
209211 case '/private': return this.private(m[2])
210212 case '/search': return this.search(m[2])
211213 case '/vote': return this.vote(m[2])
214 + case '/peers': return this.peers(m[2])
212215 }
213216 m = /^(\/?[^\/]*)(\/.*)?$/.exec(url)
214217 switch (m[1]) {
215218 case '/type': return this.type(m[2])
@@ -304,8 +307,54 @@
304307 })
305308 )
306309 }
307310
311 +Serve.prototype.peers = function (ext) {
312 + var self = this
313 + if (self.data.action === 'connect') {
314 + return self.app.sbot.gossip.connect(self.data.address, function (err) {
315 + if (err) return pull(
316 + pull.once(u.renderError(err, ext).outerHTML),
317 + self.wrapPage('peers'),
318 + self.respondSink(400, {'Content-Type': ctype(ext)})
319 + )
320 + self.data = {}
321 + return self.peers(ext)
322 + })
323 + }
324 +
325 + pull(
326 + self.app.streamPeers(),
327 + paramap(function (peer, cb) {
328 + var done = multicb({pluck: 1, spread: true})
329 + var connectedTime = Date.now() - peer.stateChange
330 + var addr = peer.host + ':' + peer.port + ':' + peer.key
331 + done()(null, h('section',
332 + h('form', {method: 'post', action: ''},
333 + peer.client ? '→' : '←', ' ',
334 + h('code', peer.host, ':', peer.port, ':'),
335 + self.app.render.idLink(peer.key, done()), ' ',
336 + htime(new Date(peer.stateChange)), ' ',
337 + peer.state === 'connected' ? 'connected' : [
338 + h('input', {name: 'action', type: 'submit', value: 'connect'}),
339 + h('input', {name: 'address', type: 'hidden', value: addr})
340 + ]
341 + )
342 + // h('div', 'source: ', peer.source)
343 + // JSON.stringify(peer, 0, 2)).outerHTML
344 + ))
345 + done(cb)
346 + }, 8),
347 + pull.map(u.toHTML),
348 + self.wrapPeers(),
349 + self.wrapPage('peers'),
350 + self.respondSink(200, {
351 + 'Content-Type': ctype(ext)
352 + })
353 + )
354 +}
355 +
356 +
308357 Serve.prototype.type = function (path) {
309358 var q = this.query
310359 var type = path.substr(1)
311360 var opts = {
@@ -687,8 +736,9 @@
687736 h('body',
688737 h('nav.nav-bar', h('form', {action: render.toUrl('/search'), method: 'get'},
689738 h('a', {href: render.toUrl('/public')}, 'public'), ' ',
690739 h('a', {href: render.toUrl('/private')}, 'private') , ' ',
740 + h('a', {href: render.toUrl('/peers')}, 'peers') , ' ',
691741 render.idLink(self.app.sbot.id, done()), ' ',
692742 h('input.search-input', {name: 'q', value: searchQ,
693743 placeholder: 'search'})
694744 // h('a', {href: '/convos'}, 'convos'), ' ',
@@ -833,8 +883,20 @@
833883 ])
834884 })
835885 }
836886
887 +Serve.prototype.wrapPeers = function (opts) {
888 + var self = this
889 + return u.hyperwrap(function (peers, cb) {
890 + cb(null, [
891 + h('section',
892 + h('h3', 'Peers')
893 + ),
894 + peers
895 + ])
896 + })
897 +}
898 +
837899 function rows(str) {
838900 return String(str).split(/[^\n]{150}|\n/).length
839901 }
840902

Built with git-ssb-web