Files: 13a679793e88e66d13fa7d8512201c70c15154a8 / lib / util.js
5135 bytesRaw
1 | var pull = require('pull-stream') |
2 | var cat = require('pull-cat') |
3 | var h = require('hyperscript') |
4 | var b64url = require('base64-url') |
5 | var u = exports |
6 | |
7 | u.ssbRefRegex = /((?:@|%|&|ssb:\/\/%)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g |
8 | u.ssbRefEncRegex = /((?:ssb:\/\/)?(?:[@%&]|%26|%40|%25)(?:[A-Za-z0-9\/+]|%2[fF]|%2[bB]){43}(?:=|%3[dD])\.[\w\d]+)/g |
9 | |
10 | u.isRef = function (str) { |
11 | if (!str) return false |
12 | u.ssbRefRegex.lastIndex = 0 |
13 | return u.ssbRefRegex.test(str) |
14 | } |
15 | |
16 | u.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 | |
28 | u.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 | |
38 | u.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 | |
48 | u.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 | |
67 | u.ifString = function (str) { |
68 | if (typeof str === 'string') return str |
69 | } |
70 | |
71 | u.ifNumber = function (num) { |
72 | if (!isNaN(num)) return num |
73 | } |
74 | |
75 | u.toLink = function (link) { |
76 | return typeof link === 'string' ? {link: link} : link || null |
77 | } |
78 | |
79 | u.linkDest = function (link) { |
80 | return link && (u.ifString(link) || u.ifString(link.link)) |
81 | } |
82 | |
83 | u.toArray = function (x) { |
84 | return x == null ? [] : Array.isArray(x) ? x : [x] |
85 | } |
86 | |
87 | u.fromArray = function (arr) { |
88 | return Array.isArray(arr) && arr.length === 1 ? arr[0] : arr |
89 | } |
90 | |
91 | u.toLinkArray = function (x) { |
92 | return u.toArray(x).map(u.toLink).filter(u.linkDest) |
93 | } |
94 | |
95 | u.renderError = function(err) { |
96 | return h('div.error', |
97 | h('h3', err.name), |
98 | h('pre', err.stack)) |
99 | } |
100 | |
101 | u.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 | |
110 | u.tryDecodeJSON = function (json) { |
111 | try { |
112 | return JSON.parse(json) |
113 | } catch(e) { |
114 | return null |
115 | } |
116 | } |
117 | |
118 | u.extractRefs = function (str) { |
119 | var ids = [] |
120 | String(str).replace(u.ssbRefRegex, function (id) { |
121 | ids.push(id) |
122 | }) |
123 | return ids |
124 | } |
125 | |
126 | u.extractFeedIds = function (str) { |
127 | return u.extractRefs(str).filter(function (ref) { |
128 | return ref[0] === '@' |
129 | }) |
130 | } |
131 | |
132 | u.extractBlobIds = function (str) { |
133 | return u.extractRefs(str).filter(function (ref) { |
134 | return ref[0] === '&' |
135 | }) |
136 | } |
137 | |
138 | u.isMsgReadable = function (msg) { |
139 | var c = msg && msg.value && msg.value.content |
140 | return typeof c === 'object' && c !== null |
141 | } |
142 | |
143 | u.isMsgEncrypted = function (msg) { |
144 | var c = msg && msg.value.content |
145 | return typeof c === 'string' |
146 | } |
147 | |
148 | u.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 | |
155 | u.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 | |
164 | u.escapeHTML = function (html) { |
165 | if (!html) return '' |
166 | return html.toString('utf8') |
167 | .replace(/</g, '<') |
168 | .replace(/>/g, '>') |
169 | } |
170 | |
171 | u.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 | |
199 | u.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 | |
211 | u.escapeId = function (id) { |
212 | return b64url.escape(id) |
213 | } |
214 | |
215 | u.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