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