git ssb

16+

cel / patchfoo



Tree: cab781470c10d358f770475c7b25d87e29f614bb

Files: cab781470c10d358f770475c7b25d87e29f614bb / lib / util.js

5505 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.renderError = function(err) {
97 return h('div.error',
98 h('h3', err.name),
99 h('pre', err.stack))
100}
101
102u.pullLength = function (cb) {
103 var len = 0
104 return pull.through(function (data) {
105 len += data.length
106 }, function (err) {
107 cb(err, len)
108 })
109}
110
111u.tryDecodeJSON = function (json) {
112 try {
113 return JSON.parse(json)
114 } catch(e) {
115 return null
116 }
117}
118
119u.extractRefs = function (str) {
120 var ids = []
121 String(str).replace(u.ssbRefRegex, function (id) {
122 ids.push(id)
123 })
124 return ids
125}
126
127u.extractFeedIds = function (str) {
128 return u.extractRefs(str).filter(function (ref) {
129 return ref[0] === '@'
130 })
131}
132
133u.extractBlobIds = function (str) {
134 return u.extractRefs(str).filter(function (ref) {
135 return ref[0] === '&'
136 })
137}
138
139u.isMsgReadable = function (msg) {
140 var c = msg && msg.value && msg.value.content
141 return typeof c === 'object' && c !== null
142}
143
144u.isMsgEncrypted = function (msg) {
145 var c = msg && msg.value.content
146 return typeof c === 'string'
147}
148
149u.pullConcat = function (cb) {
150 return pull.collect(function (err, bufs) {
151 if (err) return cb(err)
152 cb(null, Buffer.concat(bufs))
153 })
154}
155
156u.customError = function (name) {
157 return function (message) {
158 var error = new Error(message)
159 error.name = name
160 error.stack = error.stack.replace(/^ at .*\n/m, '')
161 return error
162 }
163}
164
165u.escapeHTML = function (html) {
166 if (!html) return ''
167 return html.toString('utf8')
168 .replace(/</g, '&lt;')
169 .replace(/>/g, '&gt;')
170}
171
172u.pullSlice = function (start, end) {
173 if (end == null) end = Infinity
174 var offset = 0
175 return function (read) {
176 return function (abort, cb) {
177 if (abort) read(abort, cb)
178 else if (offset >= end) read(true, function (err) {
179 cb(err || true)
180 })
181 else if (offset < start) read(null, function next(err, data) {
182 if (err) return cb(err)
183 offset += data.length
184 if (offset <= start) read(null, next)
185 else if (offset < end) cb(null,
186 data.slice(data.length - (offset - start)))
187 else cb(null, data.slice(data.length - (offset - start),
188 data.length - (offset - end)))
189 })
190 else read(null, function (err, data) {
191 if (err) return cb(err)
192 offset += data.length
193 if (offset <= end) cb(null, data)
194 else cb(null, data.slice(0, data.length - (offset - end)))
195 })
196 }
197 }
198}
199
200u.mergeOpts = function (a, b) {
201 var obj = {}, k
202 for (k in a) {
203 obj[k] = a[k]
204 }
205 for (k in b) {
206 if (b[k] != null) obj[k] = b[k]
207 else delete obj[k]
208 }
209 return obj
210}
211
212u.escapeId = function (id) {
213 return b64url.escape(id)
214}
215
216u.unescapeId = function (str) {
217 var m = /^(.)(.*)(\..*)$/.exec(str)
218 if (!m) return b64url.unescape(str)
219 return m[1] + b64url.unescape(m[2]) + m[3]
220}
221
222u.rows = function (str) {
223 return String(str).split(/[^\n]{150}|\n/).length
224}
225
226u.token = function () {
227 return '__' + Math.random().toString(36).substr(2) + '__'
228}
229
230u.unwrapP = function(html) {
231 return String(html).replace(/^<p>(.*)<\/p>\s*$/, function ($0, $1) {
232 return $1
233 })
234}
235
236

Built with git-ssb-web