index.jsView |
---|
37 | 37 … | exports.version = require('./package').version |
38 | 38 … | |
39 | 39 … | exports.init = function (sbot, config) { |
40 | 40 … | var conf = config.viewer || {} |
41 | | - var port = conf.port || 8807 |
42 | | - var host = conf.host || config.host || '::' |
43 | 41 … | |
| 42 … | + var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot) |
| 43 … | + var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot) |
| 44 … | + |
44 | 45 … | var base = conf.base || '/' |
45 | 46 … | var defaultOpts = { |
46 | 47 … | base: base, |
47 | 48 … | msg_base: conf.msg_base || base, |
63 | 64 … | emoji: renderEmoji, |
64 | 65 … | renderer: new MdRenderer(defaultOpts) |
65 | 66 … | } |
66 | 67 … | |
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) { |
99 | 69 … | console.log("serving feed: " + feedId) |
100 | 70 … | |
101 | 71 … | var showAll = req.url.endsWith("?showAll"); |
102 | 72 … | |
112 | 82 … | ); |
113 | 83 … | default: |
114 | 84 … | return pull( |
115 | 85 … | renderAbout(defaultOpts, about, |
116 | | - renderShowAll(showAll, req.url)), wrapPage(about.name) |
| 86 … | + renderShowAll(req.url)), wrapPage(about.name) |
117 | 87 … | ); |
118 | 88 … | } |
119 | 89 … | } |
120 | 90 … | |
121 | 91 … | 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) }), |
123 | 94 … | pull.collect(function (err, logs) { |
124 | 95 … | if (err) return respond(res, 500, err.stack || err) |
125 | 96 … | res.writeHead(200, { |
126 | 97 … | 'Content-Type': ctype(ext) |
140 | 111 … | ) |
141 | 112 … | }) |
142 | 113 … | } |
143 | 114 … | |
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) |
146 | 117 … | console.log("serving user feed: " + feedId) |
147 | 118 … | |
148 | 119 … | var following = [] |
149 | 120 … | var channelSubscriptions = [] |
216 | 187 … | }) |
217 | 188 … | ) |
218 | 189 … | } |
219 | 190 … | |
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) |
222 | 193 … | console.log("serving channel: " + channelId) |
223 | 194 … | |
224 | 195 … | var showAll = req.url.endsWith("?showAll") |
225 | 196 … | |
226 | 197 … | 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 }}}}]}), |
228 | 200 … | pull.collect(function (err, logs) { |
229 | 201 … | if (err) return respond(res, 500, err.stack || err) |
230 | 202 … | res.writeHead(200, { |
231 | 203 … | 'Content-Type': ctype("html") |
233 | 205 … | pull( |
234 | 206 … | pull.values(logs), |
235 | 207 … | paramap(addAuthorAbout, 8), |
236 | 208 … | paramap(addVoteMessage, 8), |
237 | | - pull(renderThread(defaultOpts, '', |
238 | | - renderShowAll(showAll, req.url)), |
| 209 … | + pull(renderThread(defaultOpts, '', renderShowAll(req.url)), |
239 | 210 … | wrapPage('#' + channelId)), |
240 | 211 … | toPull(res, function (err) { |
241 | 212 … | if (err) console.error('[viewer]', err) |
242 | 213 … | }) |
244 | 215 … | }) |
245 | 216 … | ) |
246 | 217 … | } |
247 | 218 … | |
248 | | - function serveId(req, res, id, ext, query) { |
| 219 … | + exports.serveId = function(req, res, id, ext, query) { |
249 | 220 … | var q = query ? qs.parse(query) : {} |
250 | 221 … | var includeRoot = !('noroot' in q) |
251 | 222 … | var base = q.base || conf.base |
252 | 223 … | var baseToken |
350 | 321 … | cb(null, msg) |
351 | 322 … | } |
352 | 323 … | } |
353 | 324 … | |
| 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 … | + |
354 | 360 … | function serveBlob(req, res, sbot, id) { |
355 | 361 … | if (req.headers['if-none-match'] === id) return respond(res, 304) |
356 | 362 … | sbot.blobs.has(id, function (err, has) { |
357 | 363 … | if (err) { |