aboutsummaryrefslogtreecommitdiff
path: root/lib/util.js
blob: 267df9f5602f7839f724780d72073730e0c3dfb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var pull = require('pull-stream')
var cat = require('pull-cat')
var h = require('hyperscript')
var u = exports

u.ssbRefRegex = /((?:@|%|&|ssb:\/\/%)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g

u.isRef = function (str) {
  if (!str) return false
  u.ssbRefRegex.lastIndex = 0
  return u.ssbRefRegex.test(str)
}

u.readNext = function (fn) {
  var next
  return function (end, cb) {
    if (next) return next(end, cb)
    fn(function (err, _next) {
      if (err) return cb(err)
      next = _next
      next(null, cb)
    })
  }
}

u.pullReverse = function () {
  return function (read) {
    return u.readNext(function (cb) {
      pull(read, pull.collect(function (err, items) {
        cb(err, items && pull.values(items.reverse()))
      }))
    })
  }
}

u.toHTML = function (el) {
  if (!el) return ''
  if (typeof el === 'string' || Array.isArray(el)) {
    return h('div', el).innerHTML
  }
  var html = el.outerHTML || String(el)
  if (el.nodeName === 'html') html = '<!doctype html>' + html + '\n'
  return html
}

u.hyperwrap = function (fn) {
  var token = '__HYPERWRAP_' + Math.random() + '__'
  return function (read) {
    return u.readNext(function (cb) {
      fn(token, function (err, el) {
        if (err) return cb(err)
        var parts = u.toHTML(el).split(token)
        switch (parts.length) {
          case 0: return cb(null, pull.empty())
          case 1: return cb(null, pull.once(parts[0]))
          case 2: return cb(null,
            cat([pull.once(parts[0]), read, pull.once(parts[1])]))
          default: return cb(new Error('duplicate wrap'))
        }
      })
    })
  }
}

u.toLink = function (link) {
  return typeof link === 'string' ? {link: link} : link
}

u.linkDest = function (link) {
  return typeof link === 'string' ? link : link && link.link || link
}

u.toArray = function (x) {
  return !x ? [] : Array.isArray(x) ? x : [x]
}

u.fromArray = function (arr) {
  return Array.isArray(arr) && arr.length === 1 ? arr[0] : arr
}

u.renderError = function(err) {
  return h('div.error',
    h('h3', err.name),
    h('pre', err.stack))
}

u.pullLength = function (cb) {
  var len = 0
  return pull.through(function (data) {
    len += data.length
  }, function (err) {
    cb(err, len)
  })
}

u.tryDecodeJSON = function (json) {
  try {
    return JSON.parse(json)
  } catch(e) {
    return null
  }
}

u.extractFeedIds = function (str) {
  var ids = []
  String(str).replace(u.ssbRefRegex, function (id) {
    ids.push(id)
  })
  return ids
}

u.isMsgReadable = function (msg) {
  var c = msg && msg.value && msg.value.content
  return typeof c === 'object' && c !== null
}

u.isMsgEncrypted = function (msg) {
  var c = msg && msg.value.content
  return typeof c === 'string'
}

u.pullConcat = function (cb) {
  return pull.collect(function (err, bufs) {
    if (err) return cb(err)
    cb(null, Buffer.concat(bufs))
  })
}

u.customError = function (name) {
  return function (message) {
    var error = new Error(message)
    error.name = name
    error.stack = error.stack.replace(/^    at .*\n/m, '')
    return error
  }
}

u.escapeHTML = function (html) {
  return html.toString('utf8')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
}