git ssb

16+

Dominic / patchbay



Tree: f9247af42625faa86a666f3a03671aa524a870a4

Files: f9247af42625faa86a666f3a03671aa524a870a4 / router / async / normalise.js

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

Built with git-ssb-web