Files: 47ebea298eda55724bb6e637ea900aada10c5eab / lib / util.js
5370 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.sourceMap = function sourceMap (source, fn) { |
44 | return pull( |
45 | source, |
46 | pull.filter(Boolean), |
47 | pull.map(fn), |
48 | pull.flatten() |
49 | ) |
50 | } |
51 | |
52 | u.paraSourceMap = function paraSourceMap (source, fn) { |
53 | return pull( |
54 | pull.values(source), |
55 | paramap(fn), |
56 | pull.flatten() |
57 | ) |
58 | } |
59 | |
60 | u.escape = function (str) { |
61 | return String(str) |
62 | .replace(/&/g, '&') |
63 | .replace(/</g, '<') |
64 | .replace(/>/g, '>') |
65 | .replace(/"/g, '"') |
66 | } |
67 | |
68 | u.encodeLink = function (url) { |
69 | if (!Array.isArray(url)) url = [url] |
70 | return '/' + url.map(encodeURIComponent).join('/') |
71 | } |
72 | |
73 | u.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 | |
81 | u.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 | |
88 | u.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 | |
97 | u.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 | |
105 | u.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 | |
118 | u.pre = function (text) { |
119 | return '<pre>' + u.escape(text) + '</pre>' |
120 | } |
121 | |
122 | u.json = function (obj) { |
123 | return u.linkify(u.pre(JSON.stringify(obj, null, 2))) |
124 | } |
125 | |
126 | u.linkify = function (text) { |
127 | // regex is from ssb-ref |
128 | return text.replace(/(@|%|&|&)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) { |
129 | return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>' |
130 | }) |
131 | } |
132 | |
133 | u.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 | |
140 | u.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 | |
150 | function compareMsgs(a, b) { |
151 | return (a.value.timestamp - b.value.timestamp) || (a.key - b.key) |
152 | } |
153 | |
154 | u.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 | |
167 | u.sortMsgs = function (descending) { |
168 | return u.pullSort(compareMsgs, descending) |
169 | } |
170 | |
171 | u.truncate = function (str, len) { |
172 | str = String(str) |
173 | return str.length < len ? str : str.substr(0, len) + '…' |
174 | } |
175 | |
176 | u.messageTitle = function (msg) { |
177 | var c = msg.value.content |
178 | return u.truncate(c.title || c.text || msg.key, 40) |
179 | } |
180 | |
181 | u.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 | |
188 | u.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