git ssb

0+

Dominic / ssb-ooo



Tree: a50da3928500f3ac0fbead0a1b335a3dd5bbc096

Files: a50da3928500f3ac0fbead0a1b335a3dd5bbc096 / index.js

1984 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 cb(null, data)
60 })
61 })
62 }
63
64 sbot.get.hook(function (fn, args) {
65 var id = args[0]
66 var cb = args[1]
67 if(id.raw) fn(id.id, cb)
68 else
69 fn(id, function (err, value) {
70 console.log("GOT", id, err, value)
71 if(!err) cb(null, value)
72 else get(id, cb)
73 })
74 })
75
76 sbot.on('rpc:connect', function (rpc, isClient) {
77 console.log('CONNECT...', id.substring(0, 5), rpc.id.substring(0, 5))
78 if(isClient) {
79 var stream = gq.createStream(rpc.id)
80 pull(stream, rpc.ooo.stream(function () {}), stream)
81 }
82 })
83
84 return {
85 stream: function () {
86 //called by muxrpc, so remote id is set as this.id
87 return gq.createStream(this.id)
88 },
89 get: get
90 }
91}
92
93

Built with git-ssb-web