-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathfetch.ts
More file actions
243 lines (222 loc) · 6.1 KB
/
fetch.ts
File metadata and controls
243 lines (222 loc) · 6.1 KB
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
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
DataSnapshot,
DatabaseReference,
Query,
endAt,
equalTo,
get,
limitToFirst,
off,
onValue,
orderByChild,
query,
startAt,
} from 'firebase/database';
import { Observable, ReplaySubject, defer, from, of } from 'rxjs';
import {
catchError,
concatMap,
map,
mapTo,
shareReplay,
switchMap,
} from 'rxjs/operators';
import { fetchMaybeWithCredentials } from '../../../common/rest_api';
import {
DEFAULT_PAGE_SIZE,
QueryParams,
ViewModel,
getDbRootUrl,
} from './view_model';
/**
* Timeout for silent call, which should determine if the data is
* safely loadable at the current node
*/
const REST_TIMEOUT = `1s`;
const NO_CHILDREN: string[] = [];
// Accepted as admin by Realtime Database Emulator.
const ADMIN_AUTH_HEADERS = { Authorization: 'Bearer owner' };
/**
* Creates an observable view model and query subject. The view model will
* update when the query emits/changes.
*/
export function createViewModel(
ref: DatabaseReference,
canDoRealtime$: Observable<boolean>
) {
const query = new ReplaySubject<QueryParams>(1);
query.next({ limit: DEFAULT_PAGE_SIZE });
const viewModel$ = query.pipe(
switchMap((queryParams) => {
return canDoRealtime$.pipe(
concatMap((realtime) =>
realtime
? realtimeToViewModel(ref, queryParams)
: nonRealtimeToViewModel(ref, queryParams)
)
);
})
);
return { query, viewModel$ };
}
/**
* Checks if realtime is possible at the node. Only checks once for the current
* node.
*/
export function canDoRealtime(
realtimeRef: DatabaseReference
): Observable<boolean> {
const silent = restUrl(realtimeRef, {
print: 'silent',
timeout: REST_TIMEOUT,
});
return defer(() =>
fetchMaybeWithCredentials(silent, { headers: ADMIN_AUTH_HEADERS })
).pipe(
mapTo(true),
catchError(() => of(false)),
shareReplay({ bufferSize: 1, refCount: true })
);
}
function realtimeToViewModel(ref: DatabaseReference, queryParams: QueryParams) {
let query: Query;
return once(ref).pipe(
switchMap((snap) => {
// The "default" query contains limitToFirst(50), so we can not blindly
// apply the query to leaf nodes. leafNodeRef.limitToFirst(n) will yield
// null data.
if (snap.hasChildren()) {
query = applyQuery(ref, queryParams);
return toObservable(query);
}
return toObservable(ref);
}),
map(
(snap): ViewModel => ({
value: snap.hasChildren() ? undefined : snap.val(),
children: snap.hasChildren() ? Object.keys(snap.val()) : NO_CHILDREN,
isRealtime: true,
isLoading: false,
query,
})
)
);
}
function nonRealtimeToViewModel(
ref: DatabaseReference,
queryParams: QueryParams
): Observable<ViewModel> {
return fetchNonRealtime(ref, queryParams).pipe(
map(
(children): ViewModel => ({
children,
isLoading: false,
isRealtime: false,
})
)
);
}
function once(query: Query) {
return from(get(query));
}
function toObservable(query: Query) {
return new Observable<DataSnapshot>((o) => {
onValue(query, (snapshot) => o.next(snapshot));
return () => off(query);
});
}
export function applyQuery(ref: DatabaseReference, params: QueryParams): Query {
const { key, operator, value, limit } = params;
// Check the existence value instead of it being falsy. This prevents bugs
// where the "value" is actually false.
// ex: { key: "completed", operator: "==", value: "false" }
if (key != null && operator != null && value != null) {
let queryOp = query(
ref,
orderByChild(key),
limitToFirst(limit || DEFAULT_PAGE_SIZE)
);
switch (operator) {
case '==':
return query(queryOp, equalTo(value));
case '<=':
return query(queryOp, endAt(value));
case '>=':
return query(queryOp, startAt(value));
}
}
return query(ref, limitToFirst(limit || DEFAULT_PAGE_SIZE));
}
/** Use REST to fetch the children for a node */
function fetchNonRealtime(
realtimeRef: DatabaseReference,
query: QueryParams
): Observable<string[]> {
const params = getRestQueryParams(query);
const shallow = restUrl(realtimeRef, { ...params, shallow: 'true' });
return defer(() =>
fetchMaybeWithCredentials(shallow, { headers: ADMIN_AUTH_HEADERS })
).pipe(
map((r) => r.json()),
map((data) => Object.keys(data))
);
}
interface RestQueryParams {
orderBy?: string;
limitToFirst: string;
equalTo?: string;
endAt?: string;
startAt?: string;
}
function getRestQueryParams(query: QueryParams): RestQueryParams {
const { key, operator, value, limit } = query;
const params: RestQueryParams = {
orderBy: key,
limitToFirst: `${limit || DEFAULT_PAGE_SIZE}`,
};
if (key && operator && value) {
switch (operator) {
case '==':
params.equalTo = value;
break;
case '<=':
params.endAt = value;
break;
case '>=':
params.startAt = value;
break;
}
}
return params;
}
function restUrl(
ref: DatabaseReference,
params: Record<string, string | undefined>
): string {
const rootUrl = getDbRootUrl(ref);
// TODO(yuchenshi): Find some cross-browser and lightweight way to parse query.
if (rootUrl.indexOf('?ns=') >= 0) {
const ns = rootUrl.split('?ns=')[1];
params = { ...params, ns };
}
const query = Object.entries(params)
.filter(([key, value]) => !!value)
.map(([key, value]) => `${key}=${encodeURIComponent(value!)}`)
.join('&');
return `${ref.toString()}.json?${query}`;
}