Files: 707b786ad8b53851c240262677d2e164cf75bf59 / router / async / normalise.js
1344 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') { |
16 | cb(null, location) |
17 | return true |
18 | } |
19 | |
20 | // if someone has given you an annoying html encoded location |
21 | if (location.match(/^%25.*%3D.sha256$/)) { |
22 | location = decodeURIComponent(location) |
23 | } |
24 | |
25 | if (isMsg(location)) { |
26 | api.sbot.async.get(location, (err, value) => { |
27 | if (err) cb(err) |
28 | else { |
29 | if (typeof value.content === 'string') value = api.message.sync.unbox(value) |
30 | cb(null, {key: location, value}) |
31 | } |
32 | }) |
33 | } else if (isBlob(location)) cb(null, { blob: location }) |
34 | else if (isChannel(location)) cb(null, { channel: location }) |
35 | else if (isFeed(location)) cb(null, { feed: location }) |
36 | else if (isPage(location)) cb(null, { page: location.substring(1) }) |
37 | |
38 | return true |
39 | } |
40 | } |
41 | |
42 | function isChannel (str) { |
43 | return typeof str === 'string' && str[0] === '#' && str.length > 1 |
44 | } |
45 | |
46 | function isPage (str) { |
47 | return typeof str === 'string' && str[0] === '/' && str.length > 1 |
48 | } |
49 |
Built with git-ssb-web