This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
forked from zabawaba99/firego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
264 lines (229 loc) · 5.52 KB
/
watch.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package firego
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"time"
)
const (
// EventTypePut is the event type sent when new data is inserted to the
// Firebase instance.
EventTypePut = "put"
// EventTypePatch is the event type sent when data at the Firebase instance is
// updated.
EventTypePatch = "patch"
// EventTypeError is the event type sent when an unknown error is encountered.
EventTypeError = "event_error"
// EventTypeAuthRevoked is the event type sent when the supplied auth parameter
// is no longer valid.
EventTypeAuthRevoked = "auth_revoked"
eventTypeKeepAlive = "keep-alive"
eventTypeCancel = "cancel"
eventTypeRulesDebug = "rules_debug"
)
// Event represents a notification received when watching a
// firebase reference.
type Event struct {
// Type of event that was received
Type string
// Path to the data that changed
Path string
// Data that changed
Data interface{}
rawData []byte
}
// Value converts the raw payload of the event into the given interface.
func (e Event) Value(v interface{}) error {
var tmp struct {
Data interface{} `json:"data"`
}
tmp.Data = &v
return json.Unmarshal(e.rawData, &tmp)
}
// StopWatching stops tears down all connections that are watching.
func (fb *firebase) StopWatching() {
fb.watchMtx.Lock()
defer fb.watchMtx.Unlock()
if fb.watching {
// flip the bit back to not watching
fb.watching = false
// signal connection to terminal
fb.stopWatching <- struct{}{}
}
}
func (fb *firebase) setWatching(v bool) {
fb.watchMtx.Lock()
fb.watching = v
fb.watchMtx.Unlock()
}
// Watch listens for changes on a firebase instance and
// passes over to the given chan.
//
// Only one connection can be established at a time. The
// second call to this function without a call to fb.StopWatching
// will close the channel given and return nil immediately.
func (fb *firebase) Watch(notifications chan Event) error {
fb.watchMtx.Lock()
if fb.watching {
fb.watchMtx.Unlock()
close(notifications)
return nil
}
fb.watching = true
fb.watchMtx.Unlock()
stop := make(chan struct{})
events, err := fb.watch(stop)
if err != nil {
return err
}
var closedManually bool
go func() {
<-fb.stopWatching
closedManually = true
stop <- struct{}{}
}()
go func() {
defer close(notifications)
for event := range events {
if closedManually {
return
}
notifications <- event
}
}()
return nil
}
func readLine(rdr *bufio.Reader, prefix string) ([]byte, error) {
// read event: line
line, err := rdr.ReadBytes('\n')
if err != nil {
return nil, err
}
// empty line check for empty prefix
if len(prefix) == 0 {
line = bytes.TrimSpace(line)
if len(line) != 0 {
return nil, errors.New("expected empty line")
}
return line, nil
}
// check line has event prefix
if !bytes.HasPrefix(line, []byte(prefix)) {
return nil, errors.New("missing prefix")
}
// trim space
line = line[len(prefix):]
return bytes.TrimSpace(line), nil
}
func (fb *firebase) watch(stop chan struct{}) (chan Event, error) {
// build SSE request
req, err := http.NewRequest("GET", fb.String(), nil)
if err != nil {
fb.setWatching(false)
return nil, err
}
req.Header.Add("Accept", "text/event-stream")
// do request
resp, err := fb.client.Do(req)
if err != nil {
fb.setWatching(false)
return nil, err
}
notifications := make(chan Event)
go func() {
<-stop
resp.Body.Close()
}()
heartbeat := make(chan struct{})
go func() {
for {
select {
case <-heartbeat:
// do nothing
case <-time.After(fb.watchHeartbeat):
resp.Body.Close()
return
}
}
}()
// start parsing response body
go func() {
defer func() {
resp.Body.Close()
close(notifications)
}()
// build scanner for response body
scanner := bufio.NewReader(resp.Body)
sendError := func(err error) {
notifications <- Event{
Type: EventTypeError,
Data: err,
}
}
for {
select {
case heartbeat <- struct{}{}:
default:
}
// scan for 'event:'
evt, err := readLine(scanner, "event: ")
if err != nil {
sendError(err)
return
}
// scan for 'data:'
dat, err := readLine(scanner, "data: ")
if err != nil {
sendError(err)
return
}
// read the empty line
_, err = readLine(scanner, "")
if err != nil {
sendError(err)
return
}
// create a base event
event := Event{
Type: string(evt),
Data: string(dat),
rawData: dat,
}
// should be reacting differently based off the type of event
switch event.Type {
case EventTypePut, EventTypePatch:
// we've got extra data we've got to parse
var data map[string]interface{}
if err := json.Unmarshal(event.rawData, &data); err != nil {
sendError(err)
return
}
// set the extra fields
event.Path = data["path"].(string)
event.Data = data["data"]
// ship it
notifications <- event
case eventTypeKeepAlive:
// received ping - nothing to do here
case eventTypeCancel:
// The data for this event is null
// This event will be sent if the Security and Firebase Rules
// cause a read at the requested location to no longer be allowed
// send the cancel event
notifications <- event
return
case EventTypeAuthRevoked:
// The data for this event is a string indicating that a the credential has expired
// This event will be sent when the supplied auth parameter is no longer valid
notifications <- event
return
case eventTypeRulesDebug:
log.Printf("Rules-Debug: %s\n%s\n", evt, dat)
}
}
}()
return notifications, nil
}