git ssb

16+

Dominic / patchbay



Tree: c41c84e2804a1472dcf9e911208022e6a64e37e6

Files: c41c84e2804a1472dcf9e911208022e6a64e37e6 / router / async / normalise.js

1344 bytesRaw
1const nest = require('depnest')
2const { isBlob, isFeed, isMsg } = require('ssb-ref')
3
4exports.gives = nest('router.async.normalise')
5
6exports.needs = nest({
7 'message.sync.unbox': 'first',
8 'sbot.async.get': 'first'
9})
10
11exports.create = (api) => {
12 return nest('router.async.normalise', normalise)
13
14 function normalise (location, cb) {
15 if (typeof location === 'object') {
16 cb(null, location)
17 return true
18 }
19
20 // if someone has given you an annoying html encoded location
21 if (location.match(/^%25.*%3D.sha256$/)) {
22 location = decodeURIComponent(location)
23 }
24
25 if (isMsg(location)) {
26 api.sbot.async.get(location, (err, value) => {
27 if (err) cb(err)
28 else {
29 if (typeof value.content === 'string') value = api.message.sync.unbox(value)
30 cb(null, {key: location, value})
31 }
32 })
33 } else if (isBlob(location)) cb(null, { blob: location })
34 else if (isChannel(location)) cb(null, { channel: location })
35 else if (isFeed(location)) cb(null, { feed: location })
36 else if (isPage(location)) cb(null, { page: location.substring(1) })
37
38 return true
39 }
40}
41
42function isChannel (str) {
43 return typeof str === 'string' && str[0] === '#' && str.length > 1
44}
45
46function isPage (str) {
47 return typeof str === 'string' && str[0] === '/' && str.length > 1
48}
49

Built with git-ssb-web