git ssb

16+

cel / patchfoo



Tree: 13a679793e88e66d13fa7d8512201c70c15154a8

Files: 13a679793e88e66d13fa7d8512201c70c15154a8 / lib / util.js

5135 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.ssbRefEncRegex = /((?:ssb:\/\/)?(?:[@%&]|%26|%40|%25)(?:[A-Za-z0-9\/+]|%2[fF]|%2[bB]){43}(?:=|%3[dD])\.[\w\d]+)/g
9
10u.isRef = function (str) {
11 if (!str) return false
12 u.ssbRefRegex.lastIndex = 0
13 return u.ssbRefRegex.test(str)
14}
15
16u.readNext = function (fn) {
17 var next
18 return function (end, cb) {
19 if (next) return next(end, cb)
20 fn(function (err, _next) {
21 if (err) return cb(err)
22 next = _next
23 next(null, cb)
24 })
25 }
26}
27
28u.pullReverse = function () {
29 return function (read) {
30 return u.readNext(function (cb) {
31 pull(read, pull.collect(function (err, items) {
32 cb(err, items && pull.values(items.reverse()))
33 }))
34 })
35 }
36}
37
38u.toHTML = function (el) {
39 if (!el) return ''
40 if (typeof el === 'string' || Array.isArray(el)) {
41 return h('div', el).innerHTML
42 }
43 var html = el.outerHTML || String(el)
44 if (el.nodeName === 'html') html = '<!doctype html>' + html + '\n'
45 return html
46}
47
48u.hyperwrap = function (fn) {
49 var token = '__HYPERWRAP_' + Math.random() + '__'
50 return function (read) {
51 return u.readNext(function (cb) {
52 fn(token, function (err, el) {
53 if (err) return cb(err)
54 var parts = u.toHTML(el).split(token)
55 switch (parts.length) {
56 case 0: return cb(null, pull.empty())
57 case 1: return cb(null, pull.once(parts[0]))
58 case 2: return cb(null,
59 cat([pull.once(parts[0]), read, pull.once(parts[1])]))
60 default: return cb(new Error('duplicate wrap'))
61 }
62 })
63 })
64 }
65}
66
67u.ifString = function (str) {
68 if (typeof str === 'string') return str
69}
70
71u.ifNumber = function (num) {
72 if (!isNaN(num)) return num
73}
74
75u.toLink = function (link) {
76 return typeof link === 'string' ? {link: link} : link || null
77}
78
79u.linkDest = function (link) {
80 return link && (u.ifString(link) || u.ifString(link.link))
81}
82
83u.toArray = function (x) {
84 return x == null ? [] : Array.isArray(x) ? x : [x]
85}
86
87u.fromArray = function (arr) {
88 return Array.isArray(arr) && arr.length === 1 ? arr[0] : arr
89}
90
91u.toLinkArray = function (x) {
92 return u.toArray(x).map(u.toLink).filter(u.linkDest)
93}
94
95u.renderError = function(err) {
96 return h('div.error',
97 h('h3', err.name),
98 h('pre', err.stack))
99}
100
101u.pullLength = function (cb) {
102 var len = 0
103 return pull.through(function (data) {
104 len += data.length
105 }, function (err) {
106 cb(err, len)
107 })
108}
109
110u.tryDecodeJSON = function (json) {
111 try {
112 return JSON.parse(json)
113 } catch(e) {
114 return null
115 }
116}
117
118u.extractRefs = function (str) {
119 var ids = []
120 String(str).replace(u.ssbRefRegex, function (id) {
121 ids.push(id)
122 })
123 return ids
124}
125
126u.extractFeedIds = function (str) {
127 return u.extractRefs(str).filter(function (ref) {
128 return ref[0] === '@'
129 })
130}
131
132u.extractBlobIds = function (str) {
133 return u.extractRefs(str).filter(function (ref) {
134 return ref[0] === '&'
135 })
136}
137
138u.isMsgReadable = function (msg) {
139 var c = msg && msg.value && msg.value.content
140 return typeof c === 'object' && c !== null
141}
142
143u.isMsgEncrypted = function (msg) {
144 var c = msg && msg.value.content
145 return typeof c === 'string'
146}
147
148u.pullConcat = function (cb) {
149 return pull.collect(function (err, bufs) {
150 if (err) return cb(err)
151 cb(null, Buffer.concat(bufs))
152 })
153}
154
155u.customError = function (name) {
156 return function (message) {
157 var error = new Error(message)
158 error.name = name
159 error.stack = error.stack.replace(/^ at .*\n/m, '')
160 return error
161 }
162}
163
164u.escapeHTML = function (html) {
165 if (!html) return ''
166 return html.toString('utf8')
167 .replace(/</g, '&lt;')
168 .replace(/>/g, '&gt;')
169}
170
171u.pullSlice = function (start, end) {
172 if (end == null) end = Infinity
173 var offset = 0
174 return function (read) {
175 return function (abort, cb) {
176 if (abort) read(abort, cb)
177 else if (offset >= end) read(true, function (err) {
178 cb(err || true)
179 })
180 else if (offset < start) read(null, function next(err, data) {
181 if (err) return cb(err)
182 offset += data.length
183 if (offset <= start) read(null, next)
184 else if (offset < end) cb(null,
185 data.slice(data.length - (offset - start)))
186 else cb(null, data.slice(data.length - (offset - start),
187 data.length - (offset - end)))
188 })
189 else read(null, function (err, data) {
190 if (err) return cb(err)
191 offset += data.length
192 if (offset <= end) cb(null, data)
193 else cb(null, data.slice(0, data.length - (offset - end)))
194 })
195 }
196 }
197}
198
199u.mergeOpts = function (a, b) {
200 var obj = {}, k
201 for (k in a) {
202 obj[k] = a[k]
203 }
204 for (k in b) {
205 if (b[k] != null) obj[k] = b[k]
206 else delete obj[k]
207 }
208 return obj
209}
210
211u.escapeId = function (id) {
212 return b64url.escape(id)
213}
214
215u.unescapeId = function (str) {
216 var m = /^(.)(.*)(\..*)$/.exec(str)
217 if (!m) return b64url.unescape(str)
218 return m[1] + b64url.unescape(m[2]) + m[3]
219}
220

Built with git-ssb-web