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