git ssb

30+

cel / git-ssb-web



Tree: 47ebea298eda55724bb6e637ea900aada10c5eab

Files: 47ebea298eda55724bb6e637ea900aada10c5eab / lib / util.js

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

Built with git-ssb-web