@@ -17,6 +17,7 @@ package db
17
17
import (
18
18
"bufio"
19
19
"context"
20
+ "encoding/json"
20
21
"errors"
21
22
"fmt"
22
23
"net/http"
@@ -75,26 +76,19 @@ func getInitialNodeSnapshot(resp *http.Response) (string, error) {
75
76
b = scanner .Bytes ()
76
77
s := string (b )
77
78
78
- // https://firebase.google.com/docs/reference/rest/database/#section-streaming
79
- // JSON encoded data payload
80
- // sample
81
- // s = data: {"path":"/","data":{"test3":{"test4":4}}}
82
-
83
- // sse data = path + snapshot
84
- // we only want snapshot
85
- // path is always root for initial json payload, so 1st 25 char/byte is conistent
86
- // data: {"path":"/","data":
87
- // 1234567890123456789012345
88
- if s [:25 ] != "data: {\" path\" :\" /\" ,\" data\" :" {
89
- return "" , errors .New ("sse data json error, 25 char/byte sequence" )
90
- }
79
+ var snapshotMap map [string ]interface {}
91
80
92
- if s [:5 ] == "data:" {
93
- ss := s [6 :] // trim first 6 chars:'data: '
94
- ss = ss [19 :] // trim first 19 chars:'{path":"/","data":'
95
- ss = ss [:len (ss )- 1 ] // trim last char }
96
- return ss , nil // {"test3":{"test4":4}} = snapshot
81
+ err := json .Unmarshal ([]byte (s [6 :]), & snapshotMap )
82
+ if err != nil {
83
+ return "" , err
84
+ }
85
+ snapshotByte , err := json .Marshal (snapshotMap ["data" ])
86
+ if err != nil {
87
+ return "" , err
97
88
}
89
+
90
+ return string (snapshotByte ), nil
91
+
98
92
}
99
93
}
100
94
@@ -161,39 +155,29 @@ func (r *Ref) startListeningWithReconnect(ctx context.Context, opts []internal.H
161
155
}
162
156
163
157
// returns path and snapshot
164
- func splitSSEData (json string ) (string , string , error ) {
165
-
166
- // sse data = path + snapshot
167
- // expected json payload string is similar to this:
168
- // {"path":"/test2","data":{"test3":{"test4":4}}}
169
-
170
- // IMPORTANT: quote and comma are valid in path, but are escaped in json payload
171
- // so the 3 char/byte sequence used in count "," should only occur once
158
+ func splitSSEData (sseData string ) (path string , snapshot string , err error ) {
172
159
173
- count := strings .Count (json , "\" ,\" " ) // unique 3 char/byte sequence in json payload
174
- index := strings .Index (json , "\" ,\" " )
160
+ var sseDataMap map [string ]interface {}
175
161
176
- if count != 1 {
177
- // count must equal to 1 or json payload is incorrect
178
- return "" , "" , errors . New ( "sse data json count error" )
162
+ err = json . Unmarshal ([] byte ( sseData ), & sseDataMap )
163
+ if err != nil {
164
+ return "" , "" , err
179
165
}
180
166
181
- if index < 11 { // 11 comes from root path which is minimum '{"path":"/"'
182
- return "" , "" , errors .New ("sse data json index error" )
167
+ pathByte , err := json .Marshal (sseDataMap ["path" ])
168
+ if err != nil {
169
+ return "" , "" , err
183
170
}
184
171
185
- if json [:8 ] == "{\" path\" :" {
186
- path := json
187
- path = path [:index ]
188
- path = path [9 :] // 9 = {"path":"
189
-
190
- ss := json
191
- ss = ss [index + 9 :] // trim '..."/","data":'
192
- ss = ss [:len (ss )- 1 ] // trim last char }
193
- return path , ss , nil // {"test3":{"test4":4}} = snapshot
172
+ snapshotByte , err := json .Marshal (sseDataMap ["data" ])
173
+ if err != nil {
174
+ return "" , "" , err
194
175
}
195
176
196
- return "" , "" , errors .New ("sse data json {\" path\" : not found error" )
177
+ path = string (pathByte )
178
+ snapshot = string (snapshotByte )
179
+
180
+ return
197
181
}
198
182
199
183
func (c * Client ) sendListen (
0 commit comments