This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathpubsub.js
162 lines (136 loc) · 3.83 KB
/
pubsub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
const promisify = require('promisify-es6')
const EventEmitter = require('events')
const eos = require('end-of-stream')
const isNode = require('detect-node')
const PubsubMessageStream = require('../pubsub-message-stream')
const stringlistToArray = require('../stringlist-to-array')
const NotSupportedError = () => new Error('pubsub is currently not supported when run in the browser')
/* Public API */
module.exports = (send) => {
/* Internal subscriptions state and functions */
const ps = new EventEmitter()
const subscriptions = {}
ps.id = Math.random()
return {
subscribe: (topic, options, handler, callback) => {
const defaultOptions = {
discover: false
}
if (typeof options === 'function') {
callback = handler
handler = options
options = defaultOptions
}
if (!options) {
options = defaultOptions
}
// Throw an error if ran in the browsers
if (!isNode) {
if (!callback) {
return Promise.reject(NotSupportedError())
}
return callback(NotSupportedError())
}
// promisify doesn't work as we always pass a
// function as last argument (`handler`)
if (!callback) {
return new Promise((resolve, reject) => {
subscribe(topic, options, handler, (err) => {
if (err) {
return reject(err)
}
resolve()
})
})
}
subscribe(topic, options, handler, callback)
},
unsubscribe: (topic, handler) => {
if (!isNode) {
throw NotSupportedError()
}
if (ps.listenerCount(topic) === 0 || !subscriptions[topic]) {
throw new Error(`Not subscribed to '${topic}'`)
}
ps.removeListener(topic, handler)
// Drop the request once we are actualy done
if (ps.listenerCount(topic) === 0) {
subscriptions[topic].abort()
subscriptions[topic] = null
}
},
publish: promisify((topic, data, callback) => {
if (!isNode) {
return callback(NotSupportedError())
}
if (!Buffer.isBuffer(data)) {
return callback(new Error('data must be a Buffer'))
}
const request = {
path: 'pubsub/pub',
args: [topic, data]
}
send(request, callback)
}),
ls: promisify((callback) => {
if (!isNode) {
return callback(NotSupportedError())
}
const request = {
path: 'pubsub/ls'
}
send.andTransform(request, stringlistToArray, callback)
}),
peers: promisify((topic, callback) => {
if (!isNode) {
return callback(NotSupportedError())
}
const request = {
path: 'pubsub/peers',
args: [topic]
}
send.andTransform(request, stringlistToArray, callback)
}),
setMaxListeners (n) {
return ps.setMaxListeners(n)
}
}
function subscribe (topic, options, handler, callback) {
ps.on(topic, handler)
if (subscriptions[topic]) {
return callback()
}
// Request params
const request = {
path: 'pubsub/sub',
args: [topic],
qs: {
discover: options.discover
}
}
// Start the request and transform the response
// stream to Pubsub messages stream
subscriptions[topic] = send.andTransform(request, PubsubMessageStream.from, (err, stream) => {
if (err) {
subscriptions[topic] = null
ps.removeListener(topic, handler)
return callback(err)
}
stream.on('data', (msg) => {
ps.emit(topic, msg)
})
stream.on('error', (err) => {
ps.emit('error', err)
})
eos(stream, (err) => {
if (err) {
ps.emit('error', err)
}
subscriptions[topic] = null
ps.removeListener(topic, handler)
})
callback()
})
}
}