git ssb

16+

Dominic / patchbay



Tree: ca516ca29d3fd0b19d8f943f3480a27a4cbd5de1

Files: ca516ca29d3fd0b19d8f943f3480a27a4cbd5de1 / router / async / normalise.js

2298 bytesRaw
1const nest = require('depnest')
2const { isBlobLink, isFeed, isMsg, parseLink } = 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 var link = parseLink(location)
35
36 if (link && isMsg(link.link)) {
37 var params = { id: link.link }
38 if (link.query && link.query.unbox) {
39 params.private = true
40 params.unbox = link.query.unbox
41 }
42 api.sbot.async.get(params, function (err, value) {
43 if (err) cb(err)
44 else {
45 if (typeof value.content === 'string') value = api.message.sync.unbox(value)
46 cb(null, { key: link.link, value })
47 }
48 })
49 } else if (isBlobLink(location)) {
50 // handles public & private blobs
51 // TODO - parse into link and query?
52 cb(null, { blob: location })
53 } else if (isChannelMulti(location)) cb(null, { channels: location.split('+') })
54 else if (isChannel(location)) cb(null, { channel: location })
55 else if (isFeed(location)) cb(null, { feed: location })
56 else if (isPage(location)) cb(null, { page: location.substring(1) })
57 else if (isSearch(location)) cb(null, { page: 'search', query: location.substring(1) })
58
59 return true
60 }
61}
62
63function isChannelMulti (str) {
64 if (typeof str !== 'string') return false
65
66 const channels = str.split('+')
67 return channels.length > 1 && channels.every(isChannel)
68}
69
70function isChannel (str) {
71 return typeof str === 'string' && str[0] === '#' && str.length > 1
72}
73
74function isPage (str) {
75 return typeof str === 'string' && str[0] === '/' && str.length > 1
76}
77
78function isSearch (str) {
79 return typeof str === 'string' && str[0] === '?' && str.length > 1
80}
81

Built with git-ssb-web