Files: a90a1c43ce789756570fb5b3b4601ddd6614b091 / router / async / normalise.js
1888 bytesRaw
1 | const nest = require('depnest') |
2 | const { isBlobLink, isFeed, isMsg } = require('ssb-ref') |
3 | const ssbUri = require('ssb-uri') |
4 | |
5 | exports.gives = nest('router.async.normalise') |
6 | |
7 | exports.needs = nest({ |
8 | 'message.sync.unbox': 'first', |
9 | 'sbot.async.get': 'first' |
10 | }) |
11 | |
12 | exports.create = (api) => { |
13 | return nest('router.async.normalise', normalise) |
14 | |
15 | function normalise (location, cb) { |
16 | if (typeof location === 'object') { |
17 | cb(null, location) |
18 | return true |
19 | } |
20 | |
21 | // if someone has given you an annoying html encoded location |
22 | if (location.match(/^%25.*%3D.sha256$/)) { |
23 | location = decodeURIComponent(location) |
24 | } |
25 | |
26 | if (location.startsWith('ssb:')) { |
27 | try { |
28 | location = ssbUri.toSigilLink(location) |
29 | } catch (err) { |
30 | cb(err) |
31 | } |
32 | } |
33 | |
34 | if (isMsg(location)) { |
35 | api.sbot.async.get(location, (err, value) => { |
36 | if (err) cb(err) |
37 | else { |
38 | if (typeof value.content === 'string') value = api.message.sync.unbox(value) |
39 | cb(null, { key: location, value }) |
40 | } |
41 | }) |
42 | } else if (isBlobLink(location)) { |
43 | // handles public & private blobs |
44 | // TODO - parse into link and query? |
45 | cb(null, { blob: location }) |
46 | } else if (isChannelMulti(location)) cb(null, { channels: location.split('+') }) |
47 | else if (isChannel(location)) cb(null, { channel: location }) |
48 | else if (isFeed(location)) cb(null, { feed: location }) |
49 | else if (isPage(location)) cb(null, { page: location.substring(1) }) |
50 | |
51 | return true |
52 | } |
53 | } |
54 | |
55 | function isChannelMulti (str) { |
56 | if (typeof str !== 'string') return false |
57 | |
58 | const channels = str.split('+') |
59 | return channels.length > 1 && channels.every(isChannel) |
60 | } |
61 | |
62 | function isChannel (str) { |
63 | return typeof str === 'string' && str[0] === '#' && str.length > 1 |
64 | } |
65 | |
66 | function isPage (str) { |
67 | return typeof str === 'string' && str[0] === '/' && str.length > 1 |
68 | } |
69 |
Built with git-ssb-web