git ssb

30+

cel / git-ssb-web



Tree: 55d2dbe265ce7a96e070134aa13efe6addfe7b2f

Files: 55d2dbe265ce7a96e070134aa13efe6addfe7b2f / lib / util.js

5414 bytesRaw
1var pull = require('pull-stream')
2var paramap = require('pull-paramap')
3var Highlight = require('highlight.js')
4var u = exports
5
6u.imgMimes = {
7 png: 'image/png',
8 jpeg: 'image/jpeg',
9 jpg: 'image/jpeg',
10 gif: 'image/gif',
11 tif: 'image/tiff',
12 svg: 'image/svg+xml',
13 bmp: 'image/bmp'
14}
15
16u.getExtension = function(filename) {
17 return (/\.([^.]+)$/.exec(filename) || [,filename])[1]
18}
19
20u.readNext = function (fn) {
21 var next
22 return function (end, cb) {
23 if (next) return next(end, cb)
24 fn(function (err, _next) {
25 if (err) return cb(err)
26 next = _next
27 next(null, cb)
28 })
29 }
30}
31
32u.readOnce = function (fn) {
33 var ended
34 return function (end, cb) {
35 fn(function (err, data) {
36 if (err || ended) return cb(err || ended)
37 ended = true
38 cb(null, data)
39 })
40 }
41}
42
43u.when = function (bool, el) {
44 return bool
45 ? el
46 : pull.once('')
47}
48
49u.sourceMap = function (source, fn) {
50 return pull(
51 source,
52 pull.filter(Boolean),
53 pull.map(fn)
54 )
55}
56
57u.paraSourceMap = function paraSourceMap (source, fn) {
58 return pull(
59 pull.values(source),
60 paramap(fn, 8)
61 )
62}
63
64u.escape = function (str) {
65 return String(str)
66 .replace(/&/g, '&')
67 .replace(/</g, '&lt;')
68 .replace(/>/g, '&gt;')
69 .replace(/"/g, '&quot;')
70}
71
72u.encodeLink = function (url) {
73 if (!Array.isArray(url)) url = [url]
74 return '/' + url.map(encodeURIComponent).join('/')
75}
76
77u.link = function (parts, text, raw, props) {
78 if (text == null) text = parts[parts.length-1]
79 if (!raw) text = u.escape(text)
80 return '<a href="' + u.encodeLink(parts) + '"' +
81 (props ? ' ' + props : '') +
82 '>' + text + '</a>'
83}
84
85u.timestamp = function (time, req) {
86 time = Number(time)
87 var d = new Date(time)
88 return '<span title="' + time + '">' +
89 d.toLocaleString(req._locale) + '</span>'
90}
91
92u.nav = function (links, page, after) {
93 return pull.once(['<nav>'].concat(
94 links.map(function (link) {
95 var href = typeof link[0] == 'string' ? link[0] : u.encodeLink(link[0])
96 var props = link[2] == page ? ' class="active"' : ''
97 return '<a href="' + href + '"' + props + '>' + link[1] + '</a>'
98 }), after || '', '</nav>').join('')
99 )
100}
101
102u.hiddenInputs = function (values) {
103 return Object.keys(values).map(function (key) {
104 return '<input type="hidden"' +
105 ' name="' + u.escape(key) + '"' +
106 ' value="' + u.escape(values[key]) + '"/>'
107 }).join('')
108}
109
110u.highlight = function(code, lang) {
111 if (code.length > 100000) return u.escape(code)
112 try {
113 return lang
114 ? Highlight.highlight(lang, code).value
115 : Highlight.highlightAuto(code).value
116 } catch(e) {
117 if (/^Unknown language/.test(e.message))
118 return u.escape(code)
119 throw e
120 }
121}
122
123u.pre = function (text) {
124 return '<pre>' + u.escape(text) + '</pre>'
125}
126
127u.json = function (obj) {
128 return u.linkify(u.pre(JSON.stringify(obj, null, 2)))
129}
130
131u.linkify = function (text) {
132 // regex is from ssb-ref
133 return text.replace(/(@|%|&|&amp;)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) {
134 return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>'
135 })
136}
137
138u.readObjectString = function (obj, cb) {
139 pull(obj.read, pull.collect(function (err, bufs) {
140 if (err) return cb(err)
141 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
142 }))
143}
144
145u.pullReverse = function () {
146 return function (read) {
147 return u.readNext(function (cb) {
148 pull(read, pull.collect(function (err, items) {
149 cb(err, items && pull.values(items.reverse()))
150 }))
151 })
152 }
153}
154
155function compareMsgs(a, b) {
156 return (a.value.timestamp - b.value.timestamp) || (a.key - b.key)
157}
158
159u.pullSort = function (comparator, descending) {
160 return function (read) {
161 return u.readNext(function (cb) {
162 pull(read, pull.collect(function (err, items) {
163 if (err) return cb(err)
164 items.sort(comparator)
165 if (descending) items.reverse()
166 cb(null, pull.values(items))
167 }))
168 })
169 }
170}
171
172u.sortMsgs = function (descending) {
173 return u.pullSort(compareMsgs, descending)
174}
175
176u.truncate = function (str, len) {
177 str = String(str)
178 return str.length < len ? str : str.substr(0, len) + '…'
179}
180
181u.messageTitle = function (msg) {
182 var c = msg.value.content
183 return u.truncate(c.title || c.text || msg.key, 40)
184}
185
186u.ifModifiedSince = function (req, lastMod) {
187 var ifModSince = req.headers['if-modified-since']
188 if (!ifModSince) return false
189 var d = new Date(ifModSince)
190 return d && Math.floor(d/1000) >= Math.floor(lastMod/1000)
191}
192
193u.decryptMessages = function (sbot) {
194 return paramap(function (msg, cb) {
195 var c = msg && msg.value && msg.value.content
196 if (c && typeof c === 'string' && c.slice(-4) === '.box') {
197 sbot.private.unbox(msg.value.content, function (err, content) {
198 if (err) return cb(null, msg) // leave message encrypted
199 var msg1 = {}
200 for (var k in msg) msg1[k] = msg[k]
201 msg1.value = {}
202 for (var j in msg.value) msg1.value[j] = msg.value[j]
203 msg1.value.private = true
204 msg1.value.content = content
205 if (!content.recps) {
206 sbot.whoami(function (err, feed) {
207 if (err) return cb(err)
208 content.recps = [msg1.value.author]
209 if (feed.id !== msg1.value.author) content.recps.push(feed.id)
210 cb(null, msg1)
211 })
212 } else cb(null, msg1)
213 })
214 } else cb(null, msg)
215 }, 4)
216}
217

Built with git-ssb-web