git ssb

9+

cel / ssb-viewer



Tree: a1c6e510fbdd17ac6da0e72f53c75bdd6687a7d8

Files: a1c6e510fbdd17ac6da0e72f53c75bdd6687a7d8 / index.js

13075 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 renderShowAll,
21 renderRssItem,
22 wrapRss,
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 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) {
99 console.log("serving feed: " + feedId)
100
101 var showAll = req.url.endsWith("?showAll");
102
103 getAbout(feedId, function (err, about) {
104 if (err) return respond(res, 500, err.stack || err)
105
106 function render() {
107 switch (ext) {
108 case 'rss':
109 return pull(
110 // formatMsgs(feedId, ext, defaultOpts)
111 renderRssItem(defaultOpts), wrapRss(about.name, defaultOpts)
112 );
113 default:
114 return pull(
115 renderAbout(defaultOpts, about,
116 renderShowAll(showAll, req.url)), wrapPage(about.name)
117 );
118 }
119 }
120
121 pull(
122 sbot.createUserStream({ id: feedId, reverse: true, limit: showAll ? -1 : (ext == 'rss' ? 25 :10) }),
123 pull.collect(function (err, logs) {
124 if (err) return respond(res, 500, err.stack || err)
125 res.writeHead(200, {
126 'Content-Type': ctype(ext)
127 })
128 pull(
129 pull.values(logs),
130 paramap(addAuthorAbout, 8),
131 paramap(addFollowAbout, 8),
132 paramap(addVoteMessage, 8),
133 paramap(addGitLinks, 8),
134 render(),
135 toPull(res, function (err) {
136 if (err) console.error('[viewer]', err)
137 })
138 )
139 })
140 )
141 })
142 }
143
144 function serveUserFeed(req, res, url) {
145 var feedId = url.substring(url.lastIndexOf('user-feed/')+10, 100)
146 console.log("serving user feed: " + feedId)
147
148 var following = []
149 var channelSubscriptions = []
150
151 getAbout(feedId, function (err, about) {
152 pull(
153 sbot.createUserStream({ id: feedId }),
154 pull.filter((msg) => {
155 return !msg.value ||
156 msg.value.content.type == 'contact' ||
157 (msg.value.content.type == 'channel' &&
158 typeof msg.value.content.subscribed != 'undefined')
159 }),
160 pull.collect(function (err, msgs) {
161 msgs.forEach((msg) => {
162 if (msg.value.content.type == 'contact')
163 {
164 if (msg.value.content.following)
165 following[msg.value.content.contact] = 1
166 else
167 delete following[msg.value.content.contact]
168 }
169 else // channel subscription
170 {
171 if (msg.value.content.subscribed)
172 channelSubscriptions[msg.value.content.channel] = 1
173 else
174 delete channelSubscriptions[msg.value.content.channel]
175 }
176 })
177
178 serveFeeds(req, res, following, channelSubscriptions, feedId, 'user feed ' + (about ? about.name : ""))
179 })
180 )
181 })
182 }
183
184 function serveFeeds(req, res, following, channelSubscriptions, feedId, name) {
185 pull(
186 sbot.createLogStream({ reverse: true, limit: 5000 }),
187 pull.filter((msg) => {
188 return !msg.value ||
189 (msg.value.author in following ||
190 msg.value.content.channel in channelSubscriptions)
191 }),
192 pull.take(150),
193 pull.collect(function (err, logs) {
194 if (err) return respond(res, 500, err.stack || err)
195 res.writeHead(200, {
196 'Content-Type': ctype("html")
197 })
198 pull(
199 pull.values(logs),
200 paramap(addAuthorAbout, 8),
201 paramap(addFollowAbout, 8),
202 paramap(addVoteMessage, 8),
203 paramap(addGitLinks, 8),
204 pull(renderThread(defaultOpts), wrapPage(name)),
205 toPull(res, function (err) {
206 if (err) console.error('[viewer]', err)
207 })
208 )
209 })
210 )
211 }
212
213 function serveChannel(req, res, url) {
214 var channelId = url.substring(url.lastIndexOf('channel/')+8, 100)
215 console.log("serving channel: " + channelId)
216
217 var showAll = req.url.endsWith("?showAll")
218
219 pull(
220 sbot.query.read({ limit: showAll ? 300 : 10, reverse: true, query: [{$filter: { value: { content: { channel: channelId }}}}]}),
221 pull.collect(function (err, logs) {
222 if (err) return respond(res, 500, err.stack || err)
223 res.writeHead(200, {
224 'Content-Type': ctype("html")
225 })
226 pull(
227 pull.values(logs),
228 paramap(addAuthorAbout, 8),
229 paramap(addVoteMessage, 8),
230 pull(renderThread(defaultOpts, '',
231 renderShowAll(showAll, req.url)),
232 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