Files: dc2e96d4d0a86072d9393a2d15d786a80cb341ff / router / async / normalise.js
1150 bytesRaw
1 | const nest = require('depnest') |
2 | const { isBlob, isFeed, isMsg } = require('ssb-ref') |
3 | |
4 | exports.gives = nest('router.async.normalise') |
5 | |
6 | exports.needs = nest({ |
7 | 'message.sync.unbox': 'first', |
8 | 'sbot.async.get': 'first' |
9 | }) |
10 | |
11 | exports.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 | |
33 | function isChannel (str) { |
34 | return typeof str === 'string' && str[0] === '#' && str.length > 1 |
35 | } |
36 | |
37 | function isPage (str) { |
38 | return typeof str === 'string' && str[0] === '/' && str.length > 1 |
39 | } |
40 |
Built with git-ssb-web