Files: b66bcecec258b0a2631ec338501afa9409882fe8 / lib / util.js
4202 bytesRaw
1 | var pull = require('pull-stream') |
2 | var cat = require('pull-cat') |
3 | var h = require('hyperscript') |
4 | var u = exports |
5 | |
6 | u.ssbRefRegex = /((?:@|%|&|ssb:\/\/%)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g |
7 | u.ssbRefEncRegex = /((?:ssb:\/\/)?(?:[@%&]|%26|%40|%25)(?:[A-Za-z0-9\/+]|%2[fF]|%2[bB]){43}(?:=|%3[dD])\.[\w\d]+)/g |
8 | |
9 | u.isRef = function (str) { |
10 | if (!str) return false |
11 | u.ssbRefRegex.lastIndex = 0 |
12 | return u.ssbRefRegex.test(str) |
13 | } |
14 | |
15 | u.readNext = function (fn) { |
16 | var next |
17 | return function (end, cb) { |
18 | if (next) return next(end, cb) |
19 | fn(function (err, _next) { |
20 | if (err) return cb(err) |
21 | next = _next |
22 | next(null, cb) |
23 | }) |
24 | } |
25 | } |
26 | |
27 | u.pullReverse = function () { |
28 | return function (read) { |
29 | return u.readNext(function (cb) { |
30 | pull(read, pull.collect(function (err, items) { |
31 | cb(err, items && pull.values(items.reverse())) |
32 | })) |
33 | }) |
34 | } |
35 | } |
36 | |
37 | u.toHTML = function (el) { |
38 | if (!el) return '' |
39 | if (typeof el === 'string' || Array.isArray(el)) { |
40 | return h('div', el).innerHTML |
41 | } |
42 | var html = el.outerHTML || String(el) |
43 | if (el.nodeName === 'html') html = '<!doctype html>' + html + '\n' |
44 | return html |
45 | } |
46 | |
47 | u.hyperwrap = function (fn) { |
48 | var token = '__HYPERWRAP_' + Math.random() + '__' |
49 | return function (read) { |
50 | return u.readNext(function (cb) { |
51 | fn(token, function (err, el) { |
52 | if (err) return cb(err) |
53 | var parts = u.toHTML(el).split(token) |
54 | switch (parts.length) { |
55 | case 0: return cb(null, pull.empty()) |
56 | case 1: return cb(null, pull.once(parts[0])) |
57 | case 2: return cb(null, |
58 | cat([pull.once(parts[0]), read, pull.once(parts[1])])) |
59 | default: return cb(new Error('duplicate wrap')) |
60 | } |
61 | }) |
62 | }) |
63 | } |
64 | } |
65 | |
66 | u.toLink = function (link) { |
67 | return typeof link === 'string' ? {link: link} : link |
68 | } |
69 | |
70 | u.linkDest = function (link) { |
71 | return typeof link === 'string' ? link : link && link.link || link |
72 | } |
73 | |
74 | u.toArray = function (x) { |
75 | return !x ? [] : Array.isArray(x) ? x : [x] |
76 | } |
77 | |
78 | u.fromArray = function (arr) { |
79 | return Array.isArray(arr) && arr.length === 1 ? arr[0] : arr |
80 | } |
81 | |
82 | u.renderError = function(err) { |
83 | return h('div.error', |
84 | h('h3', err.name), |
85 | h('pre', err.stack)) |
86 | } |
87 | |
88 | u.pullLength = function (cb) { |
89 | var len = 0 |
90 | return pull.through(function (data) { |
91 | len += data.length |
92 | }, function (err) { |
93 | cb(err, len) |
94 | }) |
95 | } |
96 | |
97 | u.tryDecodeJSON = function (json) { |
98 | try { |
99 | return JSON.parse(json) |
100 | } catch(e) { |
101 | return null |
102 | } |
103 | } |
104 | |
105 | u.extractFeedIds = function (str) { |
106 | var ids = [] |
107 | String(str).replace(u.ssbRefRegex, function (id) { |
108 | ids.push(id) |
109 | }) |
110 | return ids |
111 | } |
112 | |
113 | u.isMsgReadable = function (msg) { |
114 | var c = msg && msg.value && msg.value.content |
115 | return typeof c === 'object' && c !== null |
116 | } |
117 | |
118 | u.isMsgEncrypted = function (msg) { |
119 | var c = msg && msg.value.content |
120 | return typeof c === 'string' |
121 | } |
122 | |
123 | u.pullConcat = function (cb) { |
124 | return pull.collect(function (err, bufs) { |
125 | if (err) return cb(err) |
126 | cb(null, Buffer.concat(bufs)) |
127 | }) |
128 | } |
129 | |
130 | u.customError = function (name) { |
131 | return function (message) { |
132 | var error = new Error(message) |
133 | error.name = name |
134 | error.stack = error.stack.replace(/^ at .*\n/m, '') |
135 | return error |
136 | } |
137 | } |
138 | |
139 | u.escapeHTML = function (html) { |
140 | return html.toString('utf8') |
141 | .replace(/</g, '<') |
142 | .replace(/>/g, '>') |
143 | } |
144 | |
145 | u.pullSlice = function (start, end) { |
146 | if (end == null) end = Infinity |
147 | var offset = 0 |
148 | return function (read) { |
149 | return function (abort, cb) { |
150 | if (abort) read(abort, cb) |
151 | else if (offset >= end) read(true, function (err) { |
152 | cb(err || true) |
153 | }) |
154 | else if (offset < start) read(null, function next(err, data) { |
155 | if (err) return cb(err) |
156 | offset += data.length |
157 | if (offset <= start) read(null, next) |
158 | else if (offset < end) cb(null, |
159 | data.slice(data.length - (offset - start))) |
160 | else cb(null, data.slice(data.length - (offset - start), |
161 | data.length - (offset - end))) |
162 | }) |
163 | else read(null, function (err, data) { |
164 | if (err) return cb(err) |
165 | offset += data.length |
166 | if (offset <= end) cb(null, data) |
167 | else cb(null, data.slice(0, data.length - (offset - end))) |
168 | }) |
169 | } |
170 | } |
171 | } |
172 |
Built with git-ssb-web