git ssb

9+

cel / ssb-viewer



Commit 07dd6934bb87b5461859bed91c1e7761f254db75

Split index so its easier to reuse

Anders Rune Jensen committed on 1/11/2018, 11:23:21 PM
Parent: f09e7259ef382ed0b8862e0631cc1f157bbc101a

Files changed

bin.jschanged
index.jschanged
render.jschanged
bin.jsView
@@ -1,6 +1,8 @@
11 #!/usr/bin/env node
22
33 require('ssb-client')(function (err, sbot, config) {
44 if (err) throw err
5- require('.').init(sbot, config)
5 + var viewer = require('.')
6 + viewer.init(sbot, config)
7 + viewer.serveHttp(sbot, config)
68 })
index.jsView
@@ -37,11 +37,12 @@
3737 exports.version = require('./package').version
3838
3939 exports.init = function (sbot, config) {
4040 var conf = config.viewer || {}
41- var port = conf.port || 8807
42- var host = conf.host || config.host || '::'
4341
42 + var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot)
43 + var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot)
44 +
4445 var base = conf.base || '/'
4546 var defaultOpts = {
4647 base: base,
4748 msg_base: conf.msg_base || base,
@@ -63,40 +64,9 @@
6364 emoji: renderEmoji,
6465 renderer: new MdRenderer(defaultOpts)
6566 }
6667
67- var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot)
68- var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot)
69- var serveAcmeChallenge = require('ssb-acme-validator')(sbot)
70-
71- http.createServer(serve).listen(port, host, function () {
72- console.log('[viewer] Listening on http://' + host + ':' + port)
73- })
74-
75- function serve(req, res) {
76- if (req.method !== 'GET' && req.method !== 'HEAD') {
77- return respond(res, 405, 'Method must be GET or HEAD')
78- }
79-
80- var m = urlIdRegex.exec(req.url)
81-
82- if (req.url.startsWith('/user-feed/')) return serveUserFeed(req, res, m[4])
83- else if (req.url.startsWith('/channel/')) return serveChannel(req, res, m[4])
84- else if (req.url.startsWith('/.well-known/acme-challenge')) return serveAcmeChallenge(req, res)
85-
86- if (m[2] && m[2].length === 3) {
87- m[1] = decodeURIComponent(m[1])
88- m[2] = m[1][0]
89- }
90- switch (m[2]) {
91- case '%': return serveId(req, res, m[1], m[3], m[5])
92- case '@': return serveFeed(req, res, m[1], m[3], m[5])
93- case '&': return serveBlob(req, res, sbot, m[1])
94- default: return servePath(req, res, m[4])
95- }
96- }
97-
98- function serveFeed(req, res, feedId, ext) {
68 + exports.serveFeed = function(req, res, feedId, ext) {
9969 console.log("serving feed: " + feedId)
10070
10171 var showAll = req.url.endsWith("?showAll");
10272
@@ -112,15 +82,16 @@
11282 );
11383 default:
11484 return pull(
11585 renderAbout(defaultOpts, about,
116- renderShowAll(showAll, req.url)), wrapPage(about.name)
86 + renderShowAll(req.url)), wrapPage(about.name)
11787 );
11888 }
11989 }
12090
12191 pull(
122- sbot.createUserStream({ id: feedId, reverse: true, limit: showAll ? -1 : (ext == 'rss' ? 25 : 10) }),
92 + sbot.createUserStream({ id: feedId, reverse: true,
93 + limit: showAll ? -1 : (ext == 'rss' ? 25 : 10) }),
12394 pull.collect(function (err, logs) {
12495 if (err) return respond(res, 500, err.stack || err)
12596 res.writeHead(200, {
12697 'Content-Type': ctype(ext)
@@ -140,10 +111,10 @@
140111 )
141112 })
142113 }
143114
144- function serveUserFeed(req, res, url) {
145- var feedId = url.substring(url.lastIndexOf('user-feed/')+10, 100)
115 + exports.serveUserFeed = function(req, res) {
116 + var feedId = req.url.substring(req.url.lastIndexOf('user-feed/')+10, 100)
146117 console.log("serving user feed: " + feedId)
147118
148119 var following = []
149120 var channelSubscriptions = []
@@ -216,16 +187,17 @@
216187 })
217188 )
218189 }
219190
220- function serveChannel(req, res, url) {
221- var channelId = url.substring(url.lastIndexOf('channel/')+8, 100)
191 + exports.serveChannel = function(req, res) {
192 + var channelId = req.url.substring(req.url.lastIndexOf('channel/')+8, 100)
222193 console.log("serving channel: " + channelId)
223194
224195 var showAll = req.url.endsWith("?showAll")
225196
226197 pull(
227- sbot.query.read({ limit: showAll ? 300 : 10, reverse: true, query: [{$filter: { value: { content: { channel: channelId }}}}]}),
198 + sbot.query.read({ limit: showAll ? 300 : 10, reverse: true,
199 + query: [{$filter: { value: { content: { channel: channelId }}}}]}),
228200 pull.collect(function (err, logs) {
229201 if (err) return respond(res, 500, err.stack || err)
230202 res.writeHead(200, {
231203 'Content-Type': ctype("html")
@@ -233,10 +205,9 @@
233205 pull(
234206 pull.values(logs),
235207 paramap(addAuthorAbout, 8),
236208 paramap(addVoteMessage, 8),
237- pull(renderThread(defaultOpts, '',
238- renderShowAll(showAll, req.url)),
209 + pull(renderThread(defaultOpts, '', renderShowAll(req.url)),
239210 wrapPage('#' + channelId)),
240211 toPull(res, function (err) {
241212 if (err) console.error('[viewer]', err)
242213 })
@@ -244,9 +215,9 @@
244215 })
245216 )
246217 }
247218
248- function serveId(req, res, id, ext, query) {
219 + exports.serveId = function(req, res, id, ext, query) {
249220 var q = query ? qs.parse(query) : {}
250221 var includeRoot = !('noroot' in q)
251222 var base = q.base || conf.base
252223 var baseToken
@@ -350,8 +321,43 @@
350321 cb(null, msg)
351322 }
352323 }
353324
325 +exports.serveHttp = function (sbot, config) {
326 + var conf = config.viewer || {}
327 + var port = conf.port || 8807
328 + var host = conf.host || config.host || '::'
329 +
330 + var serveAcmeChallenge = require('ssb-acme-validator')(sbot)
331 +
332 + http.createServer(serve).listen(port, host, function () {
333 + console.log('[viewer] Listening on http://' + host + ':' + port)
334 + })
335 +
336 + function serve(req, res) {
337 + if (req.method !== 'GET' && req.method !== 'HEAD') {
338 + return respond(res, 405, 'Method must be GET or HEAD')
339 + }
340 +
341 + var m = urlIdRegex.exec(req.url)
342 +
343 + if (req.url.startsWith('/user-feed/')) return exports.serveUserFeed(req, res)
344 + else if (req.url.startsWith('/channel/')) return exports.serveChannel(req, res)
345 + else if (req.url.startsWith('/.well-known/acme-challenge')) return serveAcmeChallenge(req, res)
346 +
347 + if (m[2] && m[2].length === 3) {
348 + m[1] = decodeURIComponent(m[1])
349 + m[2] = m[1][0]
350 + }
351 + switch (m[2]) {
352 + case '%': return exports.serveId(req, res, m[1], m[3], m[5])
353 + case '@': return exports.serveFeed(req, res, m[1], m[3], m[5])
354 + case '&': return serveBlob(req, res, sbot, m[1])
355 + default: return servePath(req, res, m[4])
356 + }
357 + }
358 +}
359 +
354360 function serveBlob(req, res, sbot, id) {
355361 if (req.headers['if-none-match'] === id) return respond(res, 304)
356362 sbot.blobs.has(id, function (err, has) {
357363 if (err) {
render.jsView
@@ -528,8 +528,8 @@
528528 function renderDefault(c) {
529529 return h('pre', JSON.stringify(c, 0, 2));
530530 }
531531
532-function renderShowAll(showAll, url) {
533- if (!showAll)
532 +function renderShowAll(url) {
533 + if (!url.endsWith("?showAll"))
534534 return '<br>' + h('a', { href : url + '?showAll' }, 'Show whole feed').outerHTML;
535535 }

Built with git-ssb-web