git ssb

16+

Dominic / patchbay



Tree: e37288c1eddd72e5d911ccbc966d5b58020827c8

Files: e37288c1eddd72e5d911ccbc966d5b58020827c8 / router / async / normalise.js

1633 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 (isChannel(location)) cb(null, { channel: location })
47 else if (isFeed(location)) cb(null, { feed: location })
48 else if (isPage(location)) cb(null, { page: location.substring(1) })
49
50 return true
51 }
52}
53
54function isChannel (str) {
55 return typeof str === 'string' && str[0] === '#' && str.length > 1
56}
57
58function isPage (str) {
59 return typeof str === 'string' && str[0] === '/' && str.length > 1
60}
61

Built with git-ssb-web