Files: 55d2dbe265ce7a96e070134aa13efe6addfe7b2f / lib / util.js
5414 bytesRaw
1 | var pull = require('pull-stream') |
2 | var paramap = require('pull-paramap') |
3 | var Highlight = require('highlight.js') |
4 | var u = exports |
5 | |
6 | u.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 | |
16 | u.getExtension = function(filename) { |
17 | return (/\.([^.]+)$/.exec(filename) || [,filename])[1] |
18 | } |
19 | |
20 | u.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 | |
32 | u.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 | |
43 | u.when = function (bool, el) { |
44 | return bool |
45 | ? el |
46 | : pull.once('') |
47 | } |
48 | |
49 | u.sourceMap = function (source, fn) { |
50 | return pull( |
51 | source, |
52 | pull.filter(Boolean), |
53 | pull.map(fn) |
54 | ) |
55 | } |
56 | |
57 | u.paraSourceMap = function paraSourceMap (source, fn) { |
58 | return pull( |
59 | pull.values(source), |
60 | paramap(fn, 8) |
61 | ) |
62 | } |
63 | |
64 | u.escape = function (str) { |
65 | return String(str) |
66 | .replace(/&/g, '&') |
67 | .replace(/</g, '<') |
68 | .replace(/>/g, '>') |
69 | .replace(/"/g, '"') |
70 | } |
71 | |
72 | u.encodeLink = function (url) { |
73 | if (!Array.isArray(url)) url = [url] |
74 | return '/' + url.map(encodeURIComponent).join('/') |
75 | } |
76 | |
77 | u.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 | |
85 | u.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 | |
92 | u.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 | |
102 | u.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 | |
110 | u.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 | |
123 | u.pre = function (text) { |
124 | return '<pre>' + u.escape(text) + '</pre>' |
125 | } |
126 | |
127 | u.json = function (obj) { |
128 | return u.linkify(u.pre(JSON.stringify(obj, null, 2))) |
129 | } |
130 | |
131 | u.linkify = function (text) { |
132 | // regex is from ssb-ref |
133 | return text.replace(/(@|%|&|&)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) { |
134 | return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>' |
135 | }) |
136 | } |
137 | |
138 | u.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 | |
145 | u.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 | |
155 | function compareMsgs(a, b) { |
156 | return (a.value.timestamp - b.value.timestamp) || (a.key - b.key) |
157 | } |
158 | |
159 | u.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 | |
172 | u.sortMsgs = function (descending) { |
173 | return u.pullSort(compareMsgs, descending) |
174 | } |
175 | |
176 | u.truncate = function (str, len) { |
177 | str = String(str) |
178 | return str.length < len ? str : str.substr(0, len) + '…' |
179 | } |
180 | |
181 | u.messageTitle = function (msg) { |
182 | var c = msg.value.content |
183 | return u.truncate(c.title || c.text || msg.key, 40) |
184 | } |
185 | |
186 | u.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 | |
193 | u.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