git ssb

16+

cel / patchfoo



Tree: a36543131c127121355391b4c743f49b7280b2aa

Files: a36543131c127121355391b4c743f49b7280b2aa / lib / util.js

7078 bytesRaw
1var pull = require('pull-stream')
2var cat = require('pull-cat')
3var h = require('hyperscript')
4var b64url = require('base64-url')
5var u = exports
6
7u.ssbRefRegex = /((?:@|%|&|ssb:\/\/%)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g
8u.ssbRefRegexOnly = /^(?:@|%|&|ssb:\/\/%)[A-Za-z0-9\/+]{43}=\.[\w\d]+$/
9u.ssbRefEncRegex = /((?:ssb:\/\/)?(?:[@%&]|%26|%40|%25)(?:[A-Za-z0-9\/+]|%2[fF]|%2[bB]){43}(?:=|%3[dD])\.[\w\d]+)/g
10
11u.isRef = function (str) {
12 if (!str) return false
13 u.ssbRefRegexOnly.lastIndex = 0
14 return u.ssbRefRegexOnly.test(str)
15}
16
17u.readNext = function (fn) {
18 var next
19 return function (end, cb) {
20 if (next) return next(end, cb)
21 fn(function (err, _next) {
22 if (err) return cb(err)
23 next = _next
24 next(null, cb)
25 })
26 }
27}
28
29u.pullReverse = function () {
30 return function (read) {
31 return u.readNext(function (cb) {
32 pull(read, pull.collect(function (err, items) {
33 cb(err, items && pull.values(items.reverse()))
34 }))
35 })
36 }
37}
38
39u.toHTML = function (el) {
40 if (!el) return ''
41 if (typeof el === 'string' || Array.isArray(el)) {
42 return h('div', el).innerHTML
43 }
44 var html = el.outerHTML || String(el)
45 if (el.nodeName === 'html') html = '<!doctype html>' + html + '\n'
46 return html
47}
48
49u.hyperwrap = function (fn) {
50 var token = '__HYPERWRAP_' + Math.random() + '__'
51 return function (read) {
52 return u.readNext(function (cb) {
53 fn(token, function (err, el) {
54 if (err) return cb(err)
55 var parts = u.toHTML(el).split(token)
56 switch (parts.length) {
57 case 0: return cb(null, pull.empty())
58 case 1: return cb(null, pull.once(parts[0]))
59 case 2: return cb(null,
60 cat([pull.once(parts[0]), read, pull.once(parts[1])]))
61 default: return cb(new Error('duplicate wrap'))
62 }
63 })
64 })
65 }
66}
67
68u.ifString = function (str) {
69 if (typeof str === 'string') return str
70}
71
72u.ifNumber = function (num) {
73 if (!isNaN(num)) return num
74}
75
76u.toLink = function (link) {
77 return typeof link === 'string' ? {link: link} : link || null
78}
79
80u.linkDest = function (link) {
81 return link && (u.ifString(link) || u.ifString(link.link))
82}
83
84u.toArray = function (x) {
85 return x == null ? [] : Array.isArray(x) ? x : [x]
86}
87
88u.fromArray = function (arr) {
89 return Array.isArray(arr) && arr.length === 1 ? arr[0] : arr
90}
91
92u.toLinkArray = function (x) {
93 return u.toArray(x).map(u.toLink).filter(u.linkDest)
94}
95
96u.toString = function (str) {
97 switch (typeof str) {
98 case 'string': return str
99 case 'object':
100 if (str !== null) return JSON.stringify(str)
101 default:
102 return String(str)
103 }
104}
105
106u.renderError = function(err) {
107 return h('div.error',
108 h('h3', err.name),
109 h('pre', err.stack))
110}
111
112u.pullLength = function (cb) {
113 var len = 0
114 return pull.through(function (data) {
115 len += data.length
116 }, function (err) {
117 cb(err, len)
118 })
119}
120
121u.tryDecodeJSON = function (json) {
122 try {
123 return JSON.parse(json)
124 } catch(e) {
125 return null
126 }
127}
128
129u.extractRefs = function (str) {
130 var ids = []
131 String(str).replace(u.ssbRefRegex, function (id) {
132 ids.push(id)
133 })
134 return ids
135}
136
137u.extractFeedIds = function (str) {
138 return u.extractRefs(str).filter(function (ref) {
139 return ref[0] === '@'
140 })
141}
142
143u.extractBlobIds = function (str) {
144 return u.extractRefs(str).filter(function (ref) {
145 return ref[0] === '&'
146 })
147}
148
149u.isMsgReadable = function (msg) {
150 var c = msg && msg.value && msg.value.content
151 return typeof c === 'object' && c !== null
152}
153
154u.isMsgEncrypted = function (msg) {
155 var value = msg && msg.value
156 return value && (value.private || typeof value.content === 'string')
157}
158
159u.pullConcat = function (cb) {
160 return pull.collect(function (err, bufs) {
161 if (err) return cb(err)
162 cb(null, Buffer.concat(bufs))
163 })
164}
165
166u.customError = function (name) {
167 return function (message) {
168 var error = new Error(message)
169 error.name = name
170 error.stack = error.stack.replace(/^ at .*\n/m, '')
171 return error
172 }
173}
174
175u.escapeHTML = function (html) {
176 if (!html) return ''
177 return html.toString('utf8')
178 .replace(/&/g, '&amp;')
179 .replace(/"/g, '&quot;')
180 .replace(/</g, '&lt;')
181 .replace(/>/g, '&gt;')
182 .replace(/\n/g, '&#x0a;')
183}
184
185u.unescapeHTML = function (text) {
186 if (typeof text !== 'string') return text
187 return text.replace(/&amp;/g, '&')
188 .replace(/&quot;/g, '"')
189 .replace(/&#39;/g, '\'')
190 .replace(/&gt;/g, '>')
191 .replace(/&lt;/g, '<')
192}
193
194u.pullSlice = function (start, end) {
195 if (end == null) end = Infinity
196 var offset = 0
197 return function (read) {
198 return function (abort, cb) {
199 if (abort) read(abort, cb)
200 else if (offset >= end) read(true, function (err) {
201 cb(err || true)
202 })
203 else if (offset < start) read(null, function next(err, data) {
204 if (err) return cb(err)
205 offset += data.length
206 if (offset <= start) read(null, next)
207 else if (offset < end) cb(null,
208 data.slice(data.length - (offset - start)))
209 else cb(null, data.slice(data.length - (offset - start),
210 data.length - (offset - end)))
211 })
212 else read(null, function (err, data) {
213 if (err) return cb(err)
214 offset += data.length
215 if (offset <= end) cb(null, data)
216 else cb(null, data.slice(0, data.length - (offset - end)))
217 })
218 }
219 }
220}
221
222u.mergeOpts = function (a, b) {
223 var obj = {}, k
224 for (k in a) {
225 obj[k] = a[k]
226 }
227 for (k in b) {
228 if (b[k] != null) obj[k] = b[k]
229 else delete obj[k]
230 }
231 return obj
232}
233
234u.escapeId = function (id) {
235 return b64url.escape(id)
236}
237
238u.unescapeId = function (str) {
239 var m = /^(.)(.*)(\..*)$/.exec(str)
240 if (!m) return b64url.unescape(str)
241 return m[1] + b64url.unescape(m[2]) + m[3]
242}
243
244u.rows = function (str) {
245 return String(str).split(/[^\n]{150}|\n/).length
246}
247
248u.token = function () {
249 return '__' + Math.random().toString(36).substr(2) + '__'
250}
251
252u.unwrapP = function(html) {
253 return String(html).replace(/^<p>(.*)<\/p>\s*$/, function ($0, $1) {
254 return $1
255 })
256}
257
258u.estimateMessageSize = function (content) {
259 var draftMsg = {
260 key: '%0000000000000000000000000000000000000000000=.sha256',
261 value: {
262 previous: '%0000000000000000000000000000000000000000000=.sha256',
263 author: '@0000000000000000000000000000000000000000000=.ed25519',
264 sequence: 10000,
265 timestamp: 1000000000000,
266 hash: 'sha256',
267 content: content,
268 signature: '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000==.sig.ed25519'
269 }
270 }
271
272 return JSON.stringify(draftMsg, null, 2).length
273}
274
275u.truncate = function (str, len) {
276 str = String(str)
277 return str.length > len ? str.substr(0, len) + '...' : str
278}
279
280u.extractHostPort = function (id, addr) {
281 var i = addr.lastIndexOf(':' + id)
282 if (i > -1 && addr.length === i + id.length + 1) {
283 return addr.substr(0, i)
284 }
285 var m = /^(?:net|onion):([^~;]*(?::[0-9]*)?)~shs:([0-9a-zA-Z/=+]{44})$/.exec(addr)
286 if (m && id === '@' + m[2] + '.ed25519') {
287 return m[1]
288 }
289 return addr
290}
291

Built with git-ssb-web