Files: 2fc72eabdc230a249add2e43b028b0a467a317c2 / router / async / normalise.js
1447 bytesRaw
1 | const nest = require('depnest') |
2 | const { isBlobLink, 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 (isBlobLink(location)) { |
34 | // handles public & private blobs |
35 | // TODO - parse into link and query? |
36 | cb(null, { blob: location }) |
37 | } else if (isChannel(location)) cb(null, { channel: location }) |
38 | else if (isFeed(location)) cb(null, { feed: location }) |
39 | else if (isPage(location)) cb(null, { page: location.substring(1) }) |
40 | |
41 | return true |
42 | } |
43 | } |
44 | |
45 | function isChannel (str) { |
46 | return typeof str === 'string' && str[0] === '#' && str.length > 1 |
47 | } |
48 | |
49 | function isPage (str) { |
50 | return typeof str === 'string' && str[0] === '/' && str.length > 1 |
51 | } |
52 |
Built with git-ssb-web