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 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 26x 13x 13x 13x 12x 12x 12x 12x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 12x 12x 13x 13x 12x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 52x 13x 13x 13x 13x 13x 13x 13x 13x 13x 12x | /** * Copyright 2017 Google Inc. * * 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 { assert } from '@firebase/util'; import { logWrapper, warn } from './util/util'; import { jsonEval } from '@firebase/util'; import { safeGet } from '@firebase/util'; import { querystring } from '@firebase/util'; import { ServerActions } from './ServerActions'; import { RepoInfo } from './RepoInfo'; import { AuthTokenProvider } from './AuthTokenProvider'; import { Query } from '../api/Query'; /** * An implementation of ServerActions that communicates with the server via REST requests. * This is mostly useful for compatibility with crawlers, where we don't want to spin up a full * persistent connection (using WebSockets or long-polling) */ export class ReadonlyRestClient extends ServerActions { reportStats(stats: { [k: string]: any }): void { throw new Error('Method not implemented.'); } /** @private {function(...[*])} */ private log_: (...args: any[]) => void = logWrapper('p:rest:'); /** * We don't actually need to track listens, except to prevent us calling an onComplete for a listen * that's been removed. :-/ * * @private {!Object.<string, !Object>} */ private listens_: { [k: string]: Object } = {}; /** * @param {!Query} query * @param {?number=} tag * @return {string} * @private */ static getListenId_(query: Query, tag?: number | null): string { if (tag !== undefined) { return 'tag$' + tag; } else { assert( query.getQueryParams().isDefault(), "should have a tag if it's not a default query." ); return query.path.toString(); } } /** * @param {!RepoInfo} repoInfo_ Data about the namespace we are connecting to * @param {function(string, *, boolean, ?number)} onDataUpdate_ A callback for new data from the server * @param {AuthTokenProvider} authTokenProvider_ * @implements {ServerActions} */ constructor( private repoInfo_: RepoInfo, private onDataUpdate_: ( a: string, b: any, c: boolean, d: number | null ) => void, private authTokenProvider_: AuthTokenProvider ) { super(); } /** @inheritDoc */ listen( query: Query, currentHashFn: () => string, tag: number | null, onComplete: (a: string, b: any) => void ) { const pathString = query.path.toString(); this.log_( 'Listen called for ' + pathString + ' ' + query.queryIdentifier() ); // Mark this listener so we can tell if it's removed. const listenId = ReadonlyRestClient.getListenId_(query, tag); const thisListen = {}; this.listens_[listenId] = thisListen; const queryStringParamaters = query .getQueryParams() .toRestQueryStringParameters(); this.restRequest_( pathString + '.json', queryStringParamaters, (error, result) => { let data = result; Iif (error === 404) { data = null; error = null; } Eif (error === null) { this.onDataUpdate_(pathString, data, /*isMerge=*/ false, tag); } if (safeGet(this.listens_, listenId) === thisListen) { let status; Eif (!error) { status = 'ok'; } else if (error == 401) { status = 'permission_denied'; } else { status = 'rest_error:' + error; } onComplete(status, null); } } ); } /** @inheritDoc */ unlisten(query: Query, tag: number | null) { const listenId = ReadonlyRestClient.getListenId_(query, tag); delete this.listens_[listenId]; } /** @inheritDoc */ refreshAuthToken(token: string) { // no-op since we just always call getToken. } /** * Performs a REST request to the given path, with the provided query string parameters, * and any auth credentials we have. * * @param {!string} pathString * @param {!Object.<string, *>} queryStringParameters * @param {?function(?number, *=)} callback * @private */ private restRequest_( pathString: string, IqueryStringParameters: { [k: string]: any } = {}, callback: ((a: number | null, b?: any) => void) | null ) { queryStringParameters['format'] = 'export'; this.authTokenProvider_ .getToken(/*forceRefresh=*/ false) .then(authTokenData => { const authToken = authTokenData && authTokenData.accessToken; Iif (authToken) { queryStringParameters['auth'] = authToken; } const url = (this.repoInfo_.secure ? 'https://' : 'http://') + this.repoInfo_.host + pathString + '?' + querystring(queryStringParameters); this.log_('Sending REST request for ' + url); const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (callback && xhr.readyState === 4) { this.log_( 'REST Response for ' + url + ' received. status:', xhr.status, 'response:', xhr.responseText ); let res = null; Eif (xhr.status >= 200 && xhr.status < 300) { try { res = jsonEval(xhr.responseText); } catch (e) { warn( 'Failed to parse JSON response for ' + url + ': ' + xhr.responseText ); } callback(null, res); } else { // 401 and 404 are expected. if (xhr.status !== 401 && xhr.status !== 404) { warn( 'Got unsuccessful REST response for ' + url + ' Status: ' + xhr.status ); } callback(xhr.status); } callback = null; } }; xhr.open('GET', url, /*asynchronous=*/ true); xhr.send(); }); } } |