git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 67568ff7c705f4f8538755e6f3682678d0859822

Files: 67568ff7c705f4f8538755e6f3682678d0859822 / router / sync / router.js

1114 bytesRaw
1const nest = require('depnest')
2const { isBlob, isFeed, isMsg } = require('ssb-ref')
3
4exports.gives = nest('router.sync.router')
5
6exports.needs = nest('router.sync.routes', 'reduce')
7
8exports.create = (api) => {
9 var _router = null
10
11 return nest('router.sync.router', (location) => {
12 if (!_router) {
13 const routes = api.router.sync.routes()
14 _router = buildRouter(routes)
15 }
16
17 const locationObject = normalise(location)
18 return _router(locationObject)
19 })
20}
21
22function normalise(location) {
23 if (typeof location === 'object') return location
24
25 if (isBlob(location)) return { blob: location }
26 if (isChannel(location)) return { channel: location }
27 if (isFeed(location)) return { feed: location }
28 if (isMsg(location)) return { msg: location }
29}
30
31function isChannel (str) {
32 return typeof str === 'string' && str[0] === '#' && str.length > 1
33}
34
35function buildRouter (routes) {
36 return (location) => {
37 const route = routes.find(([validator]) => validator(location))
38 // signature of a route is [ routeValidator, routeFunction ]
39
40 if (route) return route[1](location)
41 }
42}
43
44

Built with git-ssb-web