3
3
const promisify = require ( 'promisify-es6' )
4
4
const bs58 = require ( 'bs58' )
5
5
var Base64 = require ( 'js-base64' ) . Base64
6
+ var Stream = require ( 'stream' )
6
7
// const Wreck = require('wreck')
7
8
var http = require ( 'http' )
8
- var Writable = require ( 'stream' ) . Writable ;
9
9
10
10
module . exports = ( send ) => {
11
11
const api = {
@@ -19,48 +19,28 @@ module.exports = (send) => {
19
19
}
20
20
21
21
22
- // var ws = Writable();
23
- var Stream = require ( 'stream' ) ;
24
- var rs = new Stream ;
25
- rs . readable = true ;
26
-
27
- // ws._write = function (chunk, enc, next) {
28
- // // console.log('>', d.toString())
29
- // var parsed = JSON.parse(chunk)
30
- // parsed.from = bs58.encode(parsed.from)
31
- // parsed.data = Base64.decode(parsed.data)
32
- // parsed.seqno = Base64.decode(parsed.seqno)
33
- // console.log(parsed)
34
- // next();
35
- // };
22
+ var rs = new Stream ( )
23
+ rs . readable = true
36
24
37
25
console . log ( 'Sub' , topic )
38
26
http . get ( {
39
27
host : 'localhost' ,
40
28
port : 5001 ,
41
29
path : '/api/v0/pubsub/sub/' + topic
42
30
} , function ( response ) {
43
- // Continuously update stream with data
44
- // var body = ''
45
- // response.pipe
46
31
response . on ( 'data' , function ( d ) {
47
- // console.log("chunk", d.toString())
48
32
var parsed = JSON . parse ( d )
49
- parsed . from = bs58 . encode ( parsed . from )
50
- parsed . data = Base64 . decode ( parsed . data )
51
- parsed . seqno = Base64 . decode ( parsed . seqno )
52
- // console.log (parsed)
53
- rs . emit ( ' data' , parsed )
54
- // rs.push(JSON.stringify (parsed) )
55
- // ws.write(d )
56
- // body += d
33
+
34
+ // skip "double subscription" error
35
+ if ( ! parsed . Message ) {
36
+ parsed . from = bs58 . encode ( parsed . from )
37
+ parsed . data = Base64 . decode ( parsed . data )
38
+ parsed . seqno = Base64 . decode ( parsed . seqno )
39
+ rs . emit ( 'data' , parsed )
40
+ }
57
41
} )
58
42
response . on ( 'end' , function ( ) {
59
43
rs . emit ( 'end' )
60
- // Data reception is done, do whatever with it!
61
- // var parsed = JSON.parse(body)
62
- // console.log(parsed)
63
- // callback(null, parsed)
64
44
} )
65
45
66
46
callback ( null , rs )
@@ -74,7 +54,6 @@ module.exports = (send) => {
74
54
// return callback(err)
75
55
// }
76
56
77
- // console.log("THIS IS THE CALLBACK")
78
57
// callback(null, response.pipe(ndjson.parse()))
79
58
// // callback(null, result) // result is a Stream
80
59
// })
0 commit comments