git ssb

0+

Dominic / ssb-ooo



Tree: ada2e9cac25f910004c85ab51e8d6b3ece0805b4

Files: ada2e9cac25f910004c85ab51e8d6b3ece0805b4 / index.js

2154 bytesRaw
1var pull = require('pull-stream')
2var GQ = require('gossip-query')
3var hash = require('ssb-keys/util').hash
4
5function getId(msg) {
6 return '%'+hash(JSON.stringify(msg, null, 2))
7}
8
9var Store = require('./store')
10var log = console.error
11
12console.error = function (m) {
13 log(new Error('---------------').stack)
14 log(m)
15
16}
17
18exports.name = 'ooo'
19exports.version = '1.0.0'
20exports.manifest = {
21 stream: 'duplex',
22 get: 'async'
23}
24exports.permissions = {
25 anonymous: {allow: ['stream']}
26}
27
28var Flume = require('flumedb')
29var OffsetLog = require('flumelog-offset')
30var mkdirp = require('mkdirp')
31var ViewHashtable = require('flumeview-hashtable')
32
33exports.init = function (sbot, config) {
34 var id = sbot.id
35
36 store = Store(config)
37
38 var gq = GQ({
39 check: function (key, cb) {
40 store.keys.get(key, function (err, data) {
41 if(data) cb(null, data.value)
42 else
43 sbot.get({id:key, raw: true}, function (err, msg) {
44 cb(null, msg)
45 })
46 })
47 },
48 process: function (id, msg, cb) {
49 if(id !== getId(msg))
50 cb()
51 else cb(null, msg)
52 }
53 })
54
55 function get (id, cb) {
56 gq.query(id, function (err, msg) {
57 if(err) return cb(err)
58 store.add(msg, function (err, data) {
59 data.ooo = true
60 cb(null, data)
61 })
62 })
63 }
64
65 sbot.get.hook(function (fn, args) {
66 var id = args[0]
67 var cb = args[1]
68 if(id.raw) fn(id.id, cb)
69 else
70 fn(id, function (err, value) {
71 if(!err) cb(null, value)
72 else get(id, function (err, data) {
73 if(err) cb(err)
74 else cb(null, data.value)
75 })
76 })
77 })
78
79 sbot.status.hook(function (fn, args) {
80 var status = fn()
81 status.ooo = {}
82 for(var id in gq.state)
83 status.ooo[id] = gq.state[id]
84 return status
85 })
86
87 sbot.on('rpc:connect', function (rpc, isClient) {
88 if(isClient) {
89 var stream = gq.createStream(rpc.id)
90 pull(stream, rpc.ooo.stream(function () {}), stream)
91 }
92 })
93
94 return {
95 stream: function () {
96 //called by muxrpc, so remote id is set as this.id
97 return gq.createStream(this.id)
98 },
99 get: get
100 }
101}
102
103

Built with git-ssb-web