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