git ssb

0+

Dominic / ssb-ooo



Tree: cb4321af23bded36cc76a4088cd515b5045aeee8

Files: cb4321af23bded36cc76a4088cd515b5045aeee8 / index.js

1869 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 if(!err) cb(null, value)
71 else get(id, cb)
72 })
73 })
74
75 sbot.on('rpc:connect', function (rpc, isClient) {
76 if(isClient) {
77 var stream = gq.createStream(rpc.id)
78 pull(stream, rpc.ooo.stream(function () {}), stream)
79 }
80 })
81
82 return {
83 stream: function () {
84 //called by muxrpc, so remote id is set as this.id
85 return gq.createStream(this.id)
86 },
87 get: get
88 }
89}
90
91
92
93

Built with git-ssb-web