git ssb

9+

cel / ssb-viewer



Tree: 9d1adf94d2c4611eca74cab2ef5baf9a8fbe28dd

Files: 9d1adf94d2c4611eca74cab2ef5baf9a8fbe28dd / index.js

13167 bytesRaw
1var fs = require('fs')
2var http = require('http')
3var qs = require('querystring')
4var path = require('path')
5var crypto = require('crypto')
6var pull = require('pull-stream')
7var paramap = require('pull-paramap')
8var sort = require('ssb-sort')
9var toPull = require('stream-to-pull-stream')
10var memo = require('asyncmemo')
11var lru = require('lrucache')
12var serveEmoji = require('emoji-server')()
13var {
14 MdRenderer,
15 renderEmoji,
16 formatMsgs,
17 wrapPage,
18 renderThread,
19 renderAbout,
20 renderRssItem,
21 wrapRss,
22 renderRssContent,
23} = require('./render');
24
25var appHash = hash([fs.readFileSync(__filename)])
26
27var urlIdRegex = /^(?:\/(([%&@]|%25|%26|%40)(?:[A-Za-z0-9\/+]|%2[Ff]|%2[Bb]){43}(?:=|%3[Dd])\.(?:sha256|ed25519))(?:\.([^?]*))?|(\/.*?))(?:\?(.*))?$/
28
29function hash(arr) {
30 return arr.reduce(function (hash, item) {
31 return hash.update(String(item))
32 }, crypto.createHash('sha256')).digest('base64')
33}
34
35exports.name = 'viewer'
36exports.manifest = {}
37exports.version = require('./package').version
38
39exports.init = function (sbot, config) {
40 var conf = config.viewer || {}
41 var port = conf.port || 8807
42 var host = conf.host || config.host || '::'
43
44 var base = conf.base || '/'
45 var defaultOpts = {
46 base: base,
47 msg_base: conf.msg_base || base,
48 feed_base: conf.feed_base || base,
49 blob_base: conf.blob_base || base,
50 img_base: conf.img_base || base,
51 emoji_base: conf.emoji_base || (base + 'emoji/'),
52 }
53
54 defaultOpts.marked = {
55 gfm: true,
56 mentions: true,
57 tables: true,
58 breaks: true,
59 pedantic: false,
60 sanitize: true,
61 smartLists: true,
62 smartypants: false,
63 emoji: renderEmoji,
64 renderer: new MdRenderer(defaultOpts)
65 }
66
67 var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot)
68 var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot)
69
70 http.createServer(serve).listen(port, host, function () {
71 console.log('[viewer] Listening on http://' + host + ':' + port)
72 })
73
74 function serve(req, res) {
75 if (req.method !== 'GET' && req.method !== 'HEAD') {
76 return respond(res, 405, 'Method must be GET or HEAD')
77 }
78
79 var m = urlIdRegex.exec(req.url)
80
81 if (req.url.startsWith('/user-feed/')) return serveUserFeed(req, res, m[4])
82 else if (req.url.startsWith('/channel/')) return serveChannel(req, res, m[4])
83
84 if (m[2] && m[2].length === 3) {
85 m[1] = decodeURIComponent(m[1])
86 m[2] = m[1][0]
87 }
88 switch (m[2]) {
89 case '%': return serveId(req, res, m[1], m[3], m[5])
90 case '@': return serveFeed(req, res, m[1], m[3], m[5])
91 case '&': return serveBlob(req, res, sbot, m[1])
92 default: return servePath(req, res, m[4])
93 }
94 }
95
96 function serveFeed(req, res, feedId, ext) {
97 console.log("serving feed: " + feedId)
98
99 var showAll = req.url.endsWith("?showAll")
100 var showAllHTML = showAll ? '' : '<br/><a href="' + req.url + '?showAll">Show whole feed</a>'
101
102 getAbout(feedId, function (err, about) {
103 if (err) return respond(res, 500, err.stack || err)
104
105 function render() {
106 switch (ext) {
107 case 'rss':
108 return pull(
109 // formatMsgs(feedId, ext, defaultOpts)
110 pull.filter( (msg) => {
111 return !!renderRssContent(defaultOpts, msg.value.content || {});
112 }),
113 renderRssItem(defaultOpts), wrapRss(about.name, defaultOpts)
114 );
115 default:
116 return pull(
117 renderAbout(defaultOpts, about, showAllHTML), wrapPage(about.name)
118 );
119 }
120 }
121
122 pull(
123 sbot.createUserStream({ id: feedId, reverse: true, limit: showAll ? -1 : (ext == 'rss' ? 25 :10) }),
124 pull.collect(function (err, logs) {
125 if (err) return respond(res, 500, err.stack || err)
126 res.writeHead(200, {
127 'Content-Type': ctype(ext)
128 })
129 pull(
130 pull.values(logs),
131 paramap(addAuthorAbout, 8),
132 paramap(addFollowAbout, 8),
133 paramap(addVoteMessage, 8),
134 paramap(addGitLinks, 8),
135 render(),
136 toPull(res, function (err) {
137 if (err) console.error('[viewer]', err)
138 })
139 )
140 })
141 )
142 })
143 }
144
145 function serveUserFeed(req, res, url) {
146 var feedId = url.substring(url.lastIndexOf('user-feed/')+10, 100)
147 console.log("serving user feed: " + feedId)
148
149 var following = []
150 var channelSubscriptions = []
151
152 getAbout(feedId, function (err, about) {
153 pull(
154 sbot.createUserStream({ id: feedId }),
155 pull.filter((msg) => {
156 return !msg.value ||
157 msg.value.content.type == 'contact' ||
158 (msg.value.content.type == 'channel' &&
159 typeof msg.value.content.subscribed != 'undefined')
160 }),
161 pull.collect(function (err, msgs) {
162 msgs.forEach((msg) => {
163 if (msg.value.content.type == 'contact')
164 {
165 if (msg.value.content.following)
166 following[msg.value.content.contact] = 1
167 else
168 delete following[msg.value.content.contact]
169 }
170 else // channel subscription
171 {
172 if (msg.value.content.subscribed)
173 channelSubscriptions[msg.value.content.channel] = 1
174 else
175 delete channelSubscriptions[msg.value.content.channel]
176 }
177 })
178
179 serveFeeds(req, res, following, channelSubscriptions, feedId, 'user feed ' + about.name)
180 })
181 )
182 })
183 }
184
185 function serveFeeds(req, res, following, channelSubscriptions, feedId, name) {
186 pull(
187 sbot.createLogStream({ reverse: true, limit: 5000 }),
188 pull.filter((msg) => {
189 return !msg.value ||
190 (msg.value.author in following ||
191 msg.value.content.channel in channelSubscriptions)
192 }),
193 pull.take(150),
194 pull.collect(function (err, logs) {
195 if (err) return respond(res, 500, err.stack || err)
196 res.writeHead(200, {
197 'Content-Type': ctype("html")
198 })
199 pull(
200 pull.values(logs),
201 paramap(addAuthorAbout, 8),
202 paramap(addFollowAbout, 8),
203 paramap(addVoteMessage, 8),
204 paramap(addGitLinks, 8),
205 pull(renderThread(defaultOpts), wrapPage(name)),
206 toPull(res, function (err) {
207 if (err) console.error('[viewer]', err)
208 })
209 )
210 })
211 )
212 }
213
214 function serveChannel(req, res, url) {
215 var channelId = url.substring(url.lastIndexOf('channel/')+8, 100)
216 console.log("serving channel: " + channelId)
217
218 var showAll = req.url.endsWith("?showAll")
219 var showAllHTML = showAll ? '' : '<br/><a href="' + req.url + '?showAll">Show whole feed</a>'
220
221 pull(
222 sbot.query.read({ limit: showAll ? 300 : 10, reverse: true, query: [{$filter: { value: { content: { channel: channelId }}}}]}),
223 pull.collect(function (err, logs) {
224 if (err) return respond(res, 500, err.stack || err)
225 res.writeHead(200, {
226 'Content-Type': ctype("html")
227 })
228 pull(
229 pull.values(logs),
230 paramap(addAuthorAbout, 8),
231 paramap(addVoteMessage, 8),
232 pull(renderThread(defaultOpts, showAllHTML), wrapPage('#' + channelId)),
233 toPull(res, function (err) {
234 if (err) console.error('[viewer]', err)
235 })
236 )
237 })
238 )
239 }
240
241 function serveId(req, res, id, ext, query) {
242 var q = query ? qs.parse(query) : {}
243 var includeRoot = !('noroot' in q)
244 var base = q.base || conf.base
245 var baseToken
246 if (!base) {
247 if (ext === 'js') base = baseToken = '__BASE_' + Math.random() + '_'
248 else base = '/'
249 }
250 var opts = {
251 base: base,
252 base_token: baseToken,
253 msg_base: q.msg_base || conf.msg_base || base,
254 feed_base: q.feed_base || conf.feed_base || base,
255 blob_base: q.blob_base || conf.blob_base || base,
256 img_base: q.img_base || conf.img_base || base,
257 emoji_base: q.emoji_base || conf.emoji_base || (base + 'emoji/'),
258 }
259 opts.marked = {
260 gfm: true,
261 mentions: true,
262 tables: true,
263 breaks: true,
264 pedantic: false,
265 sanitize: true,
266 smartLists: true,
267 smartypants: false,
268 emoji: renderEmoji,
269 renderer: new MdRenderer(opts)
270 }
271
272 var format = formatMsgs(id, ext, opts)
273 if (format === null) return respond(res, 415, 'Invalid format')
274
275 pull(
276 sbot.links({dest: id, values: true }),
277 includeRoot && prepend(getMsg, id),
278 pull.unique('key'),
279 pull.collect(function (err, links) {
280 if (err) return respond(res, 500, err.stack || err)
281 var etag = hash(sort.heads(links).concat(appHash, ext, qs))
282 if (req.headers['if-none-match'] === etag) return respond(res, 304)
283 res.writeHead(200, {
284 'Content-Type': ctype(ext),
285 'etag': etag
286 })
287 pull(
288 pull.values(sort(links)),
289 paramap(addAuthorAbout, 8),
290 format,
291 toPull(res, function (err) {
292 if (err) console.error('[viewer]', err)
293 })
294 )
295 })
296 )
297 }
298
299 function addFollowAbout(msg, cb) {
300 if (msg.value.content.contact)
301 getAbout(msg.value.content.contact, function (err, about) {
302 if (err) return cb(err)
303 msg.value.content.contactAbout = about
304 cb(null, msg)
305 })
306 else
307 cb(null, msg)
308 }
309
310 function addVoteMessage(msg, cb) {
311 if (msg.value.content.type == 'vote' && msg.value.content.vote.link[0] == '%')
312 getMsg(msg.value.content.vote.link, function (err, linkedMsg) {
313 if (linkedMsg)
314 msg.value.content.vote.linkedText = linkedMsg.value.content.text
315 cb(null, msg)
316 })
317 else
318 cb(null, msg)
319 }
320
321 function addAuthorAbout(msg, cb) {
322 getAbout(msg.value.author, function (err, about) {
323 if (err) return cb(err)
324 msg.author = about
325 cb(null, msg)
326 })
327 }
328
329 function addGitLinks(msg, cb) {
330 if (msg.value.content.type == 'git-update')
331 getMsg(msg.value.content.repo, function (err, gitRepo) {
332 if (gitRepo)
333 msg.value.content.repoName = gitRepo.value.content.name
334 cb(null, msg)
335 })
336 else if (msg.value.content.type == 'issue')
337 getMsg(msg.value.content.project, function (err, gitRepo) {
338 if (gitRepo)
339 msg.value.content.repoName = gitRepo.value.content.name
340 cb(null, msg)
341 })
342 else
343 cb(null, msg)
344 }
345}
346
347function serveBlob(req, res, sbot, id) {
348 if (req.headers['if-none-match'] === id) return respond(res, 304)
349 sbot.blobs.has(id, function (err, has) {
350 if (err) {
351 if (/^invalid/.test(err.message)) return respond(res, 400, err.message)
352 else return respond(res, 500, err.message || err)
353 }
354 if (!has) return respond(res, 404, 'Not found')
355 res.writeHead(200, {
356 'Cache-Control': 'public, max-age=315360000',
357 'etag': id
358 })
359 pull(
360 sbot.blobs.get(id),
361 toPull(res, function (err) {
362 if (err) console.error('[viewer]', err)
363 })
364 )
365 })
366}
367
368function getMsgWithValue(sbot, id, cb) {
369 sbot.get(id, function (err, value) {
370 if (err) return cb(err)
371 cb(null, {key: id, value: value})
372 })
373}
374
375function respond(res, status, message) {
376 res.writeHead(status)
377 res.end(message)
378}
379
380function ctype(name) {
381 switch (name && /[^.\/]*$/.exec(name)[0] || 'html') {
382 case 'html': return 'text/html'
383 case 'js': return 'text/javascript'
384 case 'css': return 'text/css'
385 case 'json': return 'application/json'
386 case 'rss': return 'text/xml'
387 }
388}
389
390function servePath(req, res, url) {
391 switch (url) {
392 case '/robots.txt': return res.end('User-agent: *')
393 }
394 var m = /^(\/?[^\/]*)(\/.*)?$/.exec(url)
395 switch (m[1]) {
396 case '/static': return serveStatic(req, res, m[2])
397 case '/emoji': return serveEmoji(req, res, m[2])
398 }
399 return respond(res, 404, 'Not found')
400}
401
402function ifModified(req, lastMod) {
403 var ifModSince = req.headers['if-modified-since']
404 if (!ifModSince) return false
405 var d = new Date(ifModSince)
406 return d && Math.floor(d/1000) >= Math.floor(lastMod/1000)
407}
408
409function serveStatic(req, res, file) {
410 serveFile(req, res, path.join(__dirname, 'static', file))
411}
412
413function serveFile(req, res, file) {
414 fs.stat(file, function (err, stat) {
415 if (err && err.code === 'ENOENT') return respond(res, 404, 'Not found')
416 if (err) return respond(res, 500, err.stack || err)
417 if (!stat.isFile()) return respond(res, 403, 'May only load files')
418 if (ifModified(req, stat.mtime)) return respond(res, 304, 'Not modified')
419 res.writeHead(200, {
420 'Content-Type': ctype(file),
421 'Content-Length': stat.size,
422 'Last-Modified': stat.mtime.toGMTString()
423 })
424 fs.createReadStream(file).pipe(res)
425 })
426}
427
428function prepend(fn, arg) {
429 return function (read) {
430 return function (abort, cb) {
431 if (fn && !abort) {
432 var _fn = fn
433 fn = null
434 return _fn(arg, function (err, value) {
435 if (err) return read(err, function (err) {
436 cb(err || true)
437 })
438 cb(null, value)
439 })
440 }
441 read(abort, cb)
442 }
443 }
444}
445

Built with git-ssb-web