git ssb

16+

Dominic / patchbay



Tree: 570c255b6470d315d7cb0f8174e494ebaffc6892

Files: 570c255b6470d315d7cb0f8174e494ebaffc6892 / router / async / normalise.js

1888 bytesRaw
1const nest = require('depnest')
2const { isBlobLink, isFeed, isMsg } = require('ssb-ref')
3const ssbUri = require('ssb-uri')
4
5exports.gives = nest('router.async.normalise')
6
7exports.needs = nest({
8 'message.sync.unbox': 'first',
9 'sbot.async.get': 'first'
10})
11
12exports.create = (api) => {
13 return nest('router.async.normalise', normalise)
14
15 function normalise (location, cb) {
16 if (typeof location === 'object') {
17 cb(null, location)
18 return true
19 }
20
21 // if someone has given you an annoying html encoded location
22 if (location.match(/^%25.*%3D.sha256$/)) {
23 location = decodeURIComponent(location)
24 }
25
26 if (location.startsWith('ssb:')) {
27 try {
28 location = ssbUri.toSigilLink(location)
29 } catch (err) {
30 cb(err)
31 }
32 }
33
34 if (isMsg(location)) {
35 api.sbot.async.get(location, (err, value) => {
36 if (err) cb(err)
37 else {
38 if (typeof value.content === 'string') value = api.message.sync.unbox(value)
39 cb(null, { key: location, value })
40 }
41 })
42 } else if (isBlobLink(location)) {
43 // handles public & private blobs
44 // TODO - parse into link and query?
45 cb(null, { blob: location })
46 } else if (isChannelMulti(location)) cb(null, { channels: location.split('+') })
47 else if (isChannel(location)) cb(null, { channel: location })
48 else if (isFeed(location)) cb(null, { feed: location })
49 else if (isPage(location)) cb(null, { page: location.substring(1) })
50
51 return true
52 }
53}
54
55function isChannelMulti (str) {
56 if (typeof str !== 'string') return false
57
58 const channels = str.split('+')
59 return channels.length > 1 && channels.every(isChannel)
60}
61
62function isChannel (str) {
63 return typeof str === 'string' && str[0] === '#' && str.length > 1
64}
65
66function isPage (str) {
67 return typeof str === 'string' && str[0] === '/' && str.length > 1
68}
69

Built with git-ssb-web