git ssb

16+

Dominic / patchbay



Tree: dc2e96d4d0a86072d9393a2d15d786a80cb341ff

Files: dc2e96d4d0a86072d9393a2d15d786a80cb341ff / router / async / normalise.js

1150 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') cb(null, location)
16 else if (isMsg(location)) {
17 api.sbot.async.get(location, (err, value) => {
18 if (err) cb(err)
19 else {
20 if (typeof value.content === 'string') value = api.message.sync.unbox(value)
21 cb(null, {key: location, value})
22 }
23 })
24 } else if (isBlob(location)) cb(null, { blob: location })
25 else if (isChannel(location)) cb(null, { channel: location })
26 else if (isFeed(location)) cb(null, { feed: location })
27 else if (isPage(location)) cb(null, { page: location.substring(1) })
28
29 return true
30 }
31}
32
33function isChannel (str) {
34 return typeof str === 'string' && str[0] === '#' && str.length > 1
35}
36
37function isPage (str) {
38 return typeof str === 'string' && str[0] === '/' && str.length > 1
39}
40

Built with git-ssb-web