No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.esm5.js 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. import firebase from '@firebase/app-compat';
  2. import { Provider, ComponentContainer, Component } from '@firebase/component';
  3. import { _validatePathString, onChildMoved, onChildChanged, onChildRemoved, onChildAdded, onValue, off, get, query, limitToFirst, limitToLast, orderByChild, orderByKey, orderByPriority, orderByValue, startAt, startAfter, endAt, endBefore, equalTo, _ReferenceImpl, _QueryImpl, _QueryParams, child, set, _validateWritablePath, update, setWithPriority, remove, runTransaction, setPriority, push, OnDisconnect as OnDisconnect$1, connectDatabaseEmulator, refFromURL, ref, goOffline, goOnline, serverTimestamp, increment, forceWebSockets, forceLongPolling, _setSDKVersion, _repoManagerDatabaseFromApp, enableLogging } from '@firebase/database';
  4. import { errorPrefix, validateArgCount, validateCallback, validateContextObject, Deferred } from '@firebase/util';
  5. import { __extends } from 'tslib';
  6. import { Logger } from '@firebase/logger';
  7. var name = "@firebase/database-compat";
  8. var version = "0.3.1";
  9. /**
  10. * @license
  11. * Copyright 2021 Google LLC
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. var logClient = new Logger('@firebase/database-compat');
  26. var warn = function (msg) {
  27. var message = 'FIREBASE WARNING: ' + msg;
  28. logClient.warn(message);
  29. };
  30. /**
  31. * @license
  32. * Copyright 2021 Google LLC
  33. *
  34. * Licensed under the Apache License, Version 2.0 (the "License");
  35. * you may not use this file except in compliance with the License.
  36. * You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing, software
  41. * distributed under the License is distributed on an "AS IS" BASIS,
  42. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  43. * See the License for the specific language governing permissions and
  44. * limitations under the License.
  45. */
  46. var validateBoolean = function (fnName, argumentName, bool, optional) {
  47. if (optional && bool === undefined) {
  48. return;
  49. }
  50. if (typeof bool !== 'boolean') {
  51. throw new Error(errorPrefix(fnName, argumentName) + 'must be a boolean.');
  52. }
  53. };
  54. var validateEventType = function (fnName, eventType, optional) {
  55. if (optional && eventType === undefined) {
  56. return;
  57. }
  58. switch (eventType) {
  59. case 'value':
  60. case 'child_added':
  61. case 'child_removed':
  62. case 'child_changed':
  63. case 'child_moved':
  64. break;
  65. default:
  66. throw new Error(errorPrefix(fnName, 'eventType') +
  67. 'must be a valid event type = "value", "child_added", "child_removed", ' +
  68. '"child_changed", or "child_moved".');
  69. }
  70. };
  71. /**
  72. * @license
  73. * Copyright 2017 Google LLC
  74. *
  75. * Licensed under the Apache License, Version 2.0 (the "License");
  76. * you may not use this file except in compliance with the License.
  77. * You may obtain a copy of the License at
  78. *
  79. * http://www.apache.org/licenses/LICENSE-2.0
  80. *
  81. * Unless required by applicable law or agreed to in writing, software
  82. * distributed under the License is distributed on an "AS IS" BASIS,
  83. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84. * See the License for the specific language governing permissions and
  85. * limitations under the License.
  86. */
  87. var OnDisconnect = /** @class */ (function () {
  88. function OnDisconnect(_delegate) {
  89. this._delegate = _delegate;
  90. }
  91. OnDisconnect.prototype.cancel = function (onComplete) {
  92. validateArgCount('OnDisconnect.cancel', 0, 1, arguments.length);
  93. validateCallback('OnDisconnect.cancel', 'onComplete', onComplete, true);
  94. var result = this._delegate.cancel();
  95. if (onComplete) {
  96. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  97. }
  98. return result;
  99. };
  100. OnDisconnect.prototype.remove = function (onComplete) {
  101. validateArgCount('OnDisconnect.remove', 0, 1, arguments.length);
  102. validateCallback('OnDisconnect.remove', 'onComplete', onComplete, true);
  103. var result = this._delegate.remove();
  104. if (onComplete) {
  105. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  106. }
  107. return result;
  108. };
  109. OnDisconnect.prototype.set = function (value, onComplete) {
  110. validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
  111. validateCallback('OnDisconnect.set', 'onComplete', onComplete, true);
  112. var result = this._delegate.set(value);
  113. if (onComplete) {
  114. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  115. }
  116. return result;
  117. };
  118. OnDisconnect.prototype.setWithPriority = function (value, priority, onComplete) {
  119. validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
  120. validateCallback('OnDisconnect.setWithPriority', 'onComplete', onComplete, true);
  121. var result = this._delegate.setWithPriority(value, priority);
  122. if (onComplete) {
  123. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  124. }
  125. return result;
  126. };
  127. OnDisconnect.prototype.update = function (objectToMerge, onComplete) {
  128. validateArgCount('OnDisconnect.update', 1, 2, arguments.length);
  129. if (Array.isArray(objectToMerge)) {
  130. var newObjectToMerge = {};
  131. for (var i = 0; i < objectToMerge.length; ++i) {
  132. newObjectToMerge['' + i] = objectToMerge[i];
  133. }
  134. objectToMerge = newObjectToMerge;
  135. warn('Passing an Array to firebase.database.onDisconnect().update() is deprecated. Use set() if you want to overwrite the ' +
  136. 'existing data, or an Object with integer keys if you really do want to only update some of the children.');
  137. }
  138. validateCallback('OnDisconnect.update', 'onComplete', onComplete, true);
  139. var result = this._delegate.update(objectToMerge);
  140. if (onComplete) {
  141. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  142. }
  143. return result;
  144. };
  145. return OnDisconnect;
  146. }());
  147. /**
  148. * @license
  149. * Copyright 2017 Google LLC
  150. *
  151. * Licensed under the Apache License, Version 2.0 (the "License");
  152. * you may not use this file except in compliance with the License.
  153. * You may obtain a copy of the License at
  154. *
  155. * http://www.apache.org/licenses/LICENSE-2.0
  156. *
  157. * Unless required by applicable law or agreed to in writing, software
  158. * distributed under the License is distributed on an "AS IS" BASIS,
  159. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  160. * See the License for the specific language governing permissions and
  161. * limitations under the License.
  162. */
  163. var TransactionResult = /** @class */ (function () {
  164. /**
  165. * A type for the resolve value of Firebase.transaction.
  166. */
  167. function TransactionResult(committed, snapshot) {
  168. this.committed = committed;
  169. this.snapshot = snapshot;
  170. }
  171. // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  172. // for end-users
  173. TransactionResult.prototype.toJSON = function () {
  174. validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
  175. return { committed: this.committed, snapshot: this.snapshot.toJSON() };
  176. };
  177. return TransactionResult;
  178. }());
  179. /**
  180. * @license
  181. * Copyright 2017 Google LLC
  182. *
  183. * Licensed under the Apache License, Version 2.0 (the "License");
  184. * you may not use this file except in compliance with the License.
  185. * You may obtain a copy of the License at
  186. *
  187. * http://www.apache.org/licenses/LICENSE-2.0
  188. *
  189. * Unless required by applicable law or agreed to in writing, software
  190. * distributed under the License is distributed on an "AS IS" BASIS,
  191. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  192. * See the License for the specific language governing permissions and
  193. * limitations under the License.
  194. */
  195. /**
  196. * Class representing a firebase data snapshot. It wraps a SnapshotNode and
  197. * surfaces the public methods (val, forEach, etc.) we want to expose.
  198. */
  199. var DataSnapshot = /** @class */ (function () {
  200. function DataSnapshot(_database, _delegate) {
  201. this._database = _database;
  202. this._delegate = _delegate;
  203. }
  204. /**
  205. * Retrieves the snapshot contents as JSON. Returns null if the snapshot is
  206. * empty.
  207. *
  208. * @returns JSON representation of the DataSnapshot contents, or null if empty.
  209. */
  210. DataSnapshot.prototype.val = function () {
  211. validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
  212. return this._delegate.val();
  213. };
  214. /**
  215. * Returns the snapshot contents as JSON, including priorities of node. Suitable for exporting
  216. * the entire node contents.
  217. * @returns JSON representation of the DataSnapshot contents, or null if empty.
  218. */
  219. DataSnapshot.prototype.exportVal = function () {
  220. validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
  221. return this._delegate.exportVal();
  222. };
  223. // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  224. // for end-users
  225. DataSnapshot.prototype.toJSON = function () {
  226. // Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
  227. validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
  228. return this._delegate.toJSON();
  229. };
  230. /**
  231. * Returns whether the snapshot contains a non-null value.
  232. *
  233. * @returns Whether the snapshot contains a non-null value, or is empty.
  234. */
  235. DataSnapshot.prototype.exists = function () {
  236. validateArgCount('DataSnapshot.exists', 0, 0, arguments.length);
  237. return this._delegate.exists();
  238. };
  239. /**
  240. * Returns a DataSnapshot of the specified child node's contents.
  241. *
  242. * @param path - Path to a child.
  243. * @returns DataSnapshot for child node.
  244. */
  245. DataSnapshot.prototype.child = function (path) {
  246. validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
  247. // Ensure the childPath is a string (can be a number)
  248. path = String(path);
  249. _validatePathString('DataSnapshot.child', 'path', path, false);
  250. return new DataSnapshot(this._database, this._delegate.child(path));
  251. };
  252. /**
  253. * Returns whether the snapshot contains a child at the specified path.
  254. *
  255. * @param path - Path to a child.
  256. * @returns Whether the child exists.
  257. */
  258. DataSnapshot.prototype.hasChild = function (path) {
  259. validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
  260. _validatePathString('DataSnapshot.hasChild', 'path', path, false);
  261. return this._delegate.hasChild(path);
  262. };
  263. /**
  264. * Returns the priority of the object, or null if no priority was set.
  265. *
  266. * @returns The priority.
  267. */
  268. DataSnapshot.prototype.getPriority = function () {
  269. validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
  270. return this._delegate.priority;
  271. };
  272. /**
  273. * Iterates through child nodes and calls the specified action for each one.
  274. *
  275. * @param action - Callback function to be called
  276. * for each child.
  277. * @returns True if forEach was canceled by action returning true for
  278. * one of the child nodes.
  279. */
  280. DataSnapshot.prototype.forEach = function (action) {
  281. var _this = this;
  282. validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
  283. validateCallback('DataSnapshot.forEach', 'action', action, false);
  284. return this._delegate.forEach(function (expDataSnapshot) {
  285. return action(new DataSnapshot(_this._database, expDataSnapshot));
  286. });
  287. };
  288. /**
  289. * Returns whether this DataSnapshot has children.
  290. * @returns True if the DataSnapshot contains 1 or more child nodes.
  291. */
  292. DataSnapshot.prototype.hasChildren = function () {
  293. validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
  294. return this._delegate.hasChildren();
  295. };
  296. Object.defineProperty(DataSnapshot.prototype, "key", {
  297. get: function () {
  298. return this._delegate.key;
  299. },
  300. enumerable: false,
  301. configurable: true
  302. });
  303. /**
  304. * Returns the number of children for this DataSnapshot.
  305. * @returns The number of children that this DataSnapshot contains.
  306. */
  307. DataSnapshot.prototype.numChildren = function () {
  308. validateArgCount('DataSnapshot.numChildren', 0, 0, arguments.length);
  309. return this._delegate.size;
  310. };
  311. /**
  312. * @returns The Firebase reference for the location this snapshot's data came
  313. * from.
  314. */
  315. DataSnapshot.prototype.getRef = function () {
  316. validateArgCount('DataSnapshot.ref', 0, 0, arguments.length);
  317. return new Reference(this._database, this._delegate.ref);
  318. };
  319. Object.defineProperty(DataSnapshot.prototype, "ref", {
  320. get: function () {
  321. return this.getRef();
  322. },
  323. enumerable: false,
  324. configurable: true
  325. });
  326. return DataSnapshot;
  327. }());
  328. /**
  329. * A Query represents a filter to be applied to a firebase location. This object purely represents the
  330. * query expression (and exposes our public API to build the query). The actual query logic is in ViewBase.js.
  331. *
  332. * Since every Firebase reference is a query, Firebase inherits from this object.
  333. */
  334. var Query = /** @class */ (function () {
  335. function Query(database, _delegate) {
  336. this.database = database;
  337. this._delegate = _delegate;
  338. }
  339. Query.prototype.on = function (eventType, callback, cancelCallbackOrContext, context) {
  340. var _this = this;
  341. var _a;
  342. validateArgCount('Query.on', 2, 4, arguments.length);
  343. validateCallback('Query.on', 'callback', callback, false);
  344. var ret = Query.getCancelAndContextArgs_('Query.on', cancelCallbackOrContext, context);
  345. var valueCallback = function (expSnapshot, previousChildName) {
  346. callback.call(ret.context, new DataSnapshot(_this.database, expSnapshot), previousChildName);
  347. };
  348. valueCallback.userCallback = callback;
  349. valueCallback.context = ret.context;
  350. var cancelCallback = (_a = ret.cancel) === null || _a === void 0 ? void 0 : _a.bind(ret.context);
  351. switch (eventType) {
  352. case 'value':
  353. onValue(this._delegate, valueCallback, cancelCallback);
  354. return callback;
  355. case 'child_added':
  356. onChildAdded(this._delegate, valueCallback, cancelCallback);
  357. return callback;
  358. case 'child_removed':
  359. onChildRemoved(this._delegate, valueCallback, cancelCallback);
  360. return callback;
  361. case 'child_changed':
  362. onChildChanged(this._delegate, valueCallback, cancelCallback);
  363. return callback;
  364. case 'child_moved':
  365. onChildMoved(this._delegate, valueCallback, cancelCallback);
  366. return callback;
  367. default:
  368. throw new Error(errorPrefix('Query.on', 'eventType') +
  369. 'must be a valid event type = "value", "child_added", "child_removed", ' +
  370. '"child_changed", or "child_moved".');
  371. }
  372. };
  373. Query.prototype.off = function (eventType, callback, context) {
  374. validateArgCount('Query.off', 0, 3, arguments.length);
  375. validateEventType('Query.off', eventType, true);
  376. validateCallback('Query.off', 'callback', callback, true);
  377. validateContextObject('Query.off', 'context', context, true);
  378. if (callback) {
  379. var valueCallback = function () { };
  380. valueCallback.userCallback = callback;
  381. valueCallback.context = context;
  382. off(this._delegate, eventType, valueCallback);
  383. }
  384. else {
  385. off(this._delegate, eventType);
  386. }
  387. };
  388. /**
  389. * Get the server-value for this query, or return a cached value if not connected.
  390. */
  391. Query.prototype.get = function () {
  392. var _this = this;
  393. return get(this._delegate).then(function (expSnapshot) {
  394. return new DataSnapshot(_this.database, expSnapshot);
  395. });
  396. };
  397. /**
  398. * Attaches a listener, waits for the first event, and then removes the listener
  399. */
  400. Query.prototype.once = function (eventType, callback, failureCallbackOrContext, context) {
  401. var _this = this;
  402. validateArgCount('Query.once', 1, 4, arguments.length);
  403. validateCallback('Query.once', 'callback', callback, true);
  404. var ret = Query.getCancelAndContextArgs_('Query.once', failureCallbackOrContext, context);
  405. var deferred = new Deferred();
  406. var valueCallback = function (expSnapshot, previousChildName) {
  407. var result = new DataSnapshot(_this.database, expSnapshot);
  408. if (callback) {
  409. callback.call(ret.context, result, previousChildName);
  410. }
  411. deferred.resolve(result);
  412. };
  413. valueCallback.userCallback = callback;
  414. valueCallback.context = ret.context;
  415. var cancelCallback = function (error) {
  416. if (ret.cancel) {
  417. ret.cancel.call(ret.context, error);
  418. }
  419. deferred.reject(error);
  420. };
  421. switch (eventType) {
  422. case 'value':
  423. onValue(this._delegate, valueCallback, cancelCallback, {
  424. onlyOnce: true
  425. });
  426. break;
  427. case 'child_added':
  428. onChildAdded(this._delegate, valueCallback, cancelCallback, {
  429. onlyOnce: true
  430. });
  431. break;
  432. case 'child_removed':
  433. onChildRemoved(this._delegate, valueCallback, cancelCallback, {
  434. onlyOnce: true
  435. });
  436. break;
  437. case 'child_changed':
  438. onChildChanged(this._delegate, valueCallback, cancelCallback, {
  439. onlyOnce: true
  440. });
  441. break;
  442. case 'child_moved':
  443. onChildMoved(this._delegate, valueCallback, cancelCallback, {
  444. onlyOnce: true
  445. });
  446. break;
  447. default:
  448. throw new Error(errorPrefix('Query.once', 'eventType') +
  449. 'must be a valid event type = "value", "child_added", "child_removed", ' +
  450. '"child_changed", or "child_moved".');
  451. }
  452. return deferred.promise;
  453. };
  454. /**
  455. * Set a limit and anchor it to the start of the window.
  456. */
  457. Query.prototype.limitToFirst = function (limit) {
  458. validateArgCount('Query.limitToFirst', 1, 1, arguments.length);
  459. return new Query(this.database, query(this._delegate, limitToFirst(limit)));
  460. };
  461. /**
  462. * Set a limit and anchor it to the end of the window.
  463. */
  464. Query.prototype.limitToLast = function (limit) {
  465. validateArgCount('Query.limitToLast', 1, 1, arguments.length);
  466. return new Query(this.database, query(this._delegate, limitToLast(limit)));
  467. };
  468. /**
  469. * Given a child path, return a new query ordered by the specified grandchild path.
  470. */
  471. Query.prototype.orderByChild = function (path) {
  472. validateArgCount('Query.orderByChild', 1, 1, arguments.length);
  473. return new Query(this.database, query(this._delegate, orderByChild(path)));
  474. };
  475. /**
  476. * Return a new query ordered by the KeyIndex
  477. */
  478. Query.prototype.orderByKey = function () {
  479. validateArgCount('Query.orderByKey', 0, 0, arguments.length);
  480. return new Query(this.database, query(this._delegate, orderByKey()));
  481. };
  482. /**
  483. * Return a new query ordered by the PriorityIndex
  484. */
  485. Query.prototype.orderByPriority = function () {
  486. validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
  487. return new Query(this.database, query(this._delegate, orderByPriority()));
  488. };
  489. /**
  490. * Return a new query ordered by the ValueIndex
  491. */
  492. Query.prototype.orderByValue = function () {
  493. validateArgCount('Query.orderByValue', 0, 0, arguments.length);
  494. return new Query(this.database, query(this._delegate, orderByValue()));
  495. };
  496. Query.prototype.startAt = function (value, name) {
  497. if (value === void 0) { value = null; }
  498. validateArgCount('Query.startAt', 0, 2, arguments.length);
  499. return new Query(this.database, query(this._delegate, startAt(value, name)));
  500. };
  501. Query.prototype.startAfter = function (value, name) {
  502. if (value === void 0) { value = null; }
  503. validateArgCount('Query.startAfter', 0, 2, arguments.length);
  504. return new Query(this.database, query(this._delegate, startAfter(value, name)));
  505. };
  506. Query.prototype.endAt = function (value, name) {
  507. if (value === void 0) { value = null; }
  508. validateArgCount('Query.endAt', 0, 2, arguments.length);
  509. return new Query(this.database, query(this._delegate, endAt(value, name)));
  510. };
  511. Query.prototype.endBefore = function (value, name) {
  512. if (value === void 0) { value = null; }
  513. validateArgCount('Query.endBefore', 0, 2, arguments.length);
  514. return new Query(this.database, query(this._delegate, endBefore(value, name)));
  515. };
  516. /**
  517. * Load the selection of children with exactly the specified value, and, optionally,
  518. * the specified name.
  519. */
  520. Query.prototype.equalTo = function (value, name) {
  521. validateArgCount('Query.equalTo', 1, 2, arguments.length);
  522. return new Query(this.database, query(this._delegate, equalTo(value, name)));
  523. };
  524. /**
  525. * @returns URL for this location.
  526. */
  527. Query.prototype.toString = function () {
  528. validateArgCount('Query.toString', 0, 0, arguments.length);
  529. return this._delegate.toString();
  530. };
  531. // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  532. // for end-users.
  533. Query.prototype.toJSON = function () {
  534. // An optional spacer argument is unnecessary for a string.
  535. validateArgCount('Query.toJSON', 0, 1, arguments.length);
  536. return this._delegate.toJSON();
  537. };
  538. /**
  539. * Return true if this query and the provided query are equivalent; otherwise, return false.
  540. */
  541. Query.prototype.isEqual = function (other) {
  542. validateArgCount('Query.isEqual', 1, 1, arguments.length);
  543. if (!(other instanceof Query)) {
  544. var error = 'Query.isEqual failed: First argument must be an instance of firebase.database.Query.';
  545. throw new Error(error);
  546. }
  547. return this._delegate.isEqual(other._delegate);
  548. };
  549. /**
  550. * Helper used by .on and .once to extract the context and or cancel arguments.
  551. * @param fnName - The function name (on or once)
  552. *
  553. */
  554. Query.getCancelAndContextArgs_ = function (fnName, cancelOrContext, context) {
  555. var ret = { cancel: undefined, context: undefined };
  556. if (cancelOrContext && context) {
  557. ret.cancel = cancelOrContext;
  558. validateCallback(fnName, 'cancel', ret.cancel, true);
  559. ret.context = context;
  560. validateContextObject(fnName, 'context', ret.context, true);
  561. }
  562. else if (cancelOrContext) {
  563. // we have either a cancel callback or a context.
  564. if (typeof cancelOrContext === 'object' && cancelOrContext !== null) {
  565. // it's a context!
  566. ret.context = cancelOrContext;
  567. }
  568. else if (typeof cancelOrContext === 'function') {
  569. ret.cancel = cancelOrContext;
  570. }
  571. else {
  572. throw new Error(errorPrefix(fnName, 'cancelOrContext') +
  573. ' must either be a cancel callback or a context object.');
  574. }
  575. }
  576. return ret;
  577. };
  578. Object.defineProperty(Query.prototype, "ref", {
  579. get: function () {
  580. return new Reference(this.database, new _ReferenceImpl(this._delegate._repo, this._delegate._path));
  581. },
  582. enumerable: false,
  583. configurable: true
  584. });
  585. return Query;
  586. }());
  587. var Reference = /** @class */ (function (_super) {
  588. __extends(Reference, _super);
  589. /**
  590. * Call options:
  591. * new Reference(Repo, Path) or
  592. * new Reference(url: string, string|RepoManager)
  593. *
  594. * Externally - this is the firebase.database.Reference type.
  595. */
  596. function Reference(database, _delegate) {
  597. var _this = _super.call(this, database, new _QueryImpl(_delegate._repo, _delegate._path, new _QueryParams(), false)) || this;
  598. _this.database = database;
  599. _this._delegate = _delegate;
  600. return _this;
  601. }
  602. /** @returns {?string} */
  603. Reference.prototype.getKey = function () {
  604. validateArgCount('Reference.key', 0, 0, arguments.length);
  605. return this._delegate.key;
  606. };
  607. Reference.prototype.child = function (pathString) {
  608. validateArgCount('Reference.child', 1, 1, arguments.length);
  609. if (typeof pathString === 'number') {
  610. pathString = String(pathString);
  611. }
  612. return new Reference(this.database, child(this._delegate, pathString));
  613. };
  614. /** @returns {?Reference} */
  615. Reference.prototype.getParent = function () {
  616. validateArgCount('Reference.parent', 0, 0, arguments.length);
  617. var parent = this._delegate.parent;
  618. return parent ? new Reference(this.database, parent) : null;
  619. };
  620. /** @returns {!Reference} */
  621. Reference.prototype.getRoot = function () {
  622. validateArgCount('Reference.root', 0, 0, arguments.length);
  623. return new Reference(this.database, this._delegate.root);
  624. };
  625. Reference.prototype.set = function (newVal, onComplete) {
  626. validateArgCount('Reference.set', 1, 2, arguments.length);
  627. validateCallback('Reference.set', 'onComplete', onComplete, true);
  628. var result = set(this._delegate, newVal);
  629. if (onComplete) {
  630. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  631. }
  632. return result;
  633. };
  634. Reference.prototype.update = function (values, onComplete) {
  635. validateArgCount('Reference.update', 1, 2, arguments.length);
  636. if (Array.isArray(values)) {
  637. var newObjectToMerge = {};
  638. for (var i = 0; i < values.length; ++i) {
  639. newObjectToMerge['' + i] = values[i];
  640. }
  641. values = newObjectToMerge;
  642. warn('Passing an Array to Firebase.update() is deprecated. ' +
  643. 'Use set() if you want to overwrite the existing data, or ' +
  644. 'an Object with integer keys if you really do want to ' +
  645. 'only update some of the children.');
  646. }
  647. _validateWritablePath('Reference.update', this._delegate._path);
  648. validateCallback('Reference.update', 'onComplete', onComplete, true);
  649. var result = update(this._delegate, values);
  650. if (onComplete) {
  651. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  652. }
  653. return result;
  654. };
  655. Reference.prototype.setWithPriority = function (newVal, newPriority, onComplete) {
  656. validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
  657. validateCallback('Reference.setWithPriority', 'onComplete', onComplete, true);
  658. var result = setWithPriority(this._delegate, newVal, newPriority);
  659. if (onComplete) {
  660. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  661. }
  662. return result;
  663. };
  664. Reference.prototype.remove = function (onComplete) {
  665. validateArgCount('Reference.remove', 0, 1, arguments.length);
  666. validateCallback('Reference.remove', 'onComplete', onComplete, true);
  667. var result = remove(this._delegate);
  668. if (onComplete) {
  669. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  670. }
  671. return result;
  672. };
  673. Reference.prototype.transaction = function (transactionUpdate, onComplete, applyLocally) {
  674. var _this = this;
  675. validateArgCount('Reference.transaction', 1, 3, arguments.length);
  676. validateCallback('Reference.transaction', 'transactionUpdate', transactionUpdate, false);
  677. validateCallback('Reference.transaction', 'onComplete', onComplete, true);
  678. validateBoolean('Reference.transaction', 'applyLocally', applyLocally, true);
  679. var result = runTransaction(this._delegate, transactionUpdate, {
  680. applyLocally: applyLocally
  681. }).then(function (transactionResult) {
  682. return new TransactionResult(transactionResult.committed, new DataSnapshot(_this.database, transactionResult.snapshot));
  683. });
  684. if (onComplete) {
  685. result.then(function (transactionResult) {
  686. return onComplete(null, transactionResult.committed, transactionResult.snapshot);
  687. }, function (error) { return onComplete(error, false, null); });
  688. }
  689. return result;
  690. };
  691. Reference.prototype.setPriority = function (priority, onComplete) {
  692. validateArgCount('Reference.setPriority', 1, 2, arguments.length);
  693. validateCallback('Reference.setPriority', 'onComplete', onComplete, true);
  694. var result = setPriority(this._delegate, priority);
  695. if (onComplete) {
  696. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  697. }
  698. return result;
  699. };
  700. Reference.prototype.push = function (value, onComplete) {
  701. var _this = this;
  702. validateArgCount('Reference.push', 0, 2, arguments.length);
  703. validateCallback('Reference.push', 'onComplete', onComplete, true);
  704. var expPromise = push(this._delegate, value);
  705. var promise = expPromise.then(function (expRef) { return new Reference(_this.database, expRef); });
  706. if (onComplete) {
  707. promise.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  708. }
  709. var result = new Reference(this.database, expPromise);
  710. result.then = promise.then.bind(promise);
  711. result.catch = promise.catch.bind(promise, undefined);
  712. return result;
  713. };
  714. Reference.prototype.onDisconnect = function () {
  715. _validateWritablePath('Reference.onDisconnect', this._delegate._path);
  716. return new OnDisconnect(new OnDisconnect$1(this._delegate._repo, this._delegate._path));
  717. };
  718. Object.defineProperty(Reference.prototype, "key", {
  719. get: function () {
  720. return this.getKey();
  721. },
  722. enumerable: false,
  723. configurable: true
  724. });
  725. Object.defineProperty(Reference.prototype, "parent", {
  726. get: function () {
  727. return this.getParent();
  728. },
  729. enumerable: false,
  730. configurable: true
  731. });
  732. Object.defineProperty(Reference.prototype, "root", {
  733. get: function () {
  734. return this.getRoot();
  735. },
  736. enumerable: false,
  737. configurable: true
  738. });
  739. return Reference;
  740. }(Query));
  741. /**
  742. * @license
  743. * Copyright 2017 Google LLC
  744. *
  745. * Licensed under the Apache License, Version 2.0 (the "License");
  746. * you may not use this file except in compliance with the License.
  747. * You may obtain a copy of the License at
  748. *
  749. * http://www.apache.org/licenses/LICENSE-2.0
  750. *
  751. * Unless required by applicable law or agreed to in writing, software
  752. * distributed under the License is distributed on an "AS IS" BASIS,
  753. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  754. * See the License for the specific language governing permissions and
  755. * limitations under the License.
  756. */
  757. /**
  758. * Class representing a firebase database.
  759. */
  760. var Database = /** @class */ (function () {
  761. /**
  762. * The constructor should not be called by users of our public API.
  763. */
  764. function Database(_delegate, app) {
  765. var _this = this;
  766. this._delegate = _delegate;
  767. this.app = app;
  768. this.INTERNAL = {
  769. delete: function () { return _this._delegate._delete(); },
  770. forceWebSockets: forceWebSockets,
  771. forceLongPolling: forceLongPolling
  772. };
  773. }
  774. /**
  775. * Modify this instance to communicate with the Realtime Database emulator.
  776. *
  777. * <p>Note: This method must be called before performing any other operation.
  778. *
  779. * @param host - the emulator host (ex: localhost)
  780. * @param port - the emulator port (ex: 8080)
  781. * @param options.mockUserToken - the mock auth token to use for unit testing Security Rules
  782. */
  783. Database.prototype.useEmulator = function (host, port, options) {
  784. if (options === void 0) { options = {}; }
  785. connectDatabaseEmulator(this._delegate, host, port, options);
  786. };
  787. Database.prototype.ref = function (path) {
  788. validateArgCount('database.ref', 0, 1, arguments.length);
  789. if (path instanceof Reference) {
  790. var childRef = refFromURL(this._delegate, path.toString());
  791. return new Reference(this, childRef);
  792. }
  793. else {
  794. var childRef = ref(this._delegate, path);
  795. return new Reference(this, childRef);
  796. }
  797. };
  798. /**
  799. * Returns a reference to the root or the path specified in url.
  800. * We throw a exception if the url is not in the same domain as the
  801. * current repo.
  802. * @returns Firebase reference.
  803. */
  804. Database.prototype.refFromURL = function (url) {
  805. var apiName = 'database.refFromURL';
  806. validateArgCount(apiName, 1, 1, arguments.length);
  807. var childRef = refFromURL(this._delegate, url);
  808. return new Reference(this, childRef);
  809. };
  810. // Make individual repo go offline.
  811. Database.prototype.goOffline = function () {
  812. validateArgCount('database.goOffline', 0, 0, arguments.length);
  813. return goOffline(this._delegate);
  814. };
  815. Database.prototype.goOnline = function () {
  816. validateArgCount('database.goOnline', 0, 0, arguments.length);
  817. return goOnline(this._delegate);
  818. };
  819. Database.ServerValue = {
  820. TIMESTAMP: serverTimestamp(),
  821. increment: function (delta) { return increment(delta); }
  822. };
  823. return Database;
  824. }());
  825. /**
  826. * @license
  827. * Copyright 2017 Google LLC
  828. *
  829. * Licensed under the Apache License, Version 2.0 (the "License");
  830. * you may not use this file except in compliance with the License.
  831. * You may obtain a copy of the License at
  832. *
  833. * http://www.apache.org/licenses/LICENSE-2.0
  834. *
  835. * Unless required by applicable law or agreed to in writing, software
  836. * distributed under the License is distributed on an "AS IS" BASIS,
  837. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  838. * See the License for the specific language governing permissions and
  839. * limitations under the License.
  840. */
  841. /**
  842. * Used by console to create a database based on the app,
  843. * passed database URL and a custom auth implementation.
  844. *
  845. * @param app - A valid FirebaseApp-like object
  846. * @param url - A valid Firebase databaseURL
  847. * @param version - custom version e.g. firebase-admin version
  848. * @param customAuthImpl - custom auth implementation
  849. */
  850. function initStandalone(_a) {
  851. var app = _a.app, url = _a.url, version = _a.version, customAuthImpl = _a.customAuthImpl, namespace = _a.namespace, _b = _a.nodeAdmin, nodeAdmin = _b === void 0 ? false : _b;
  852. _setSDKVersion(version);
  853. /**
  854. * ComponentContainer('database-standalone') is just a placeholder that doesn't perform
  855. * any actual function.
  856. */
  857. var authProvider = new Provider('auth-internal', new ComponentContainer('database-standalone'));
  858. authProvider.setComponent(new Component('auth-internal', function () { return customAuthImpl; }, "PRIVATE" /* ComponentType.PRIVATE */));
  859. return {
  860. instance: new Database(_repoManagerDatabaseFromApp(app, authProvider,
  861. /* appCheckProvider= */ undefined, url, nodeAdmin), app),
  862. namespace: namespace
  863. };
  864. }
  865. var INTERNAL = /*#__PURE__*/Object.freeze({
  866. __proto__: null,
  867. initStandalone: initStandalone
  868. });
  869. /**
  870. * @license
  871. * Copyright 2021 Google LLC
  872. *
  873. * Licensed under the Apache License, Version 2.0 (the "License");
  874. * you may not use this file except in compliance with the License.
  875. * You may obtain a copy of the License at
  876. *
  877. * http://www.apache.org/licenses/LICENSE-2.0
  878. *
  879. * Unless required by applicable law or agreed to in writing, software
  880. * distributed under the License is distributed on an "AS IS" BASIS,
  881. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  882. * See the License for the specific language governing permissions and
  883. * limitations under the License.
  884. */
  885. var ServerValue = Database.ServerValue;
  886. function registerDatabase(instance) {
  887. // Register the Database Service with the 'firebase' namespace.
  888. instance.INTERNAL.registerComponent(new Component('database-compat', function (container, _a) {
  889. var url = _a.instanceIdentifier;
  890. /* Dependencies */
  891. // getImmediate for FirebaseApp will always succeed
  892. var app = container.getProvider('app-compat').getImmediate();
  893. var databaseExp = container
  894. .getProvider('database')
  895. .getImmediate({ identifier: url });
  896. return new Database(databaseExp, app);
  897. }, "PUBLIC" /* ComponentType.PUBLIC */)
  898. .setServiceProps(
  899. // firebase.database namespace properties
  900. {
  901. Reference: Reference,
  902. Query: Query,
  903. Database: Database,
  904. DataSnapshot: DataSnapshot,
  905. enableLogging: enableLogging,
  906. INTERNAL: INTERNAL,
  907. ServerValue: ServerValue
  908. })
  909. .setMultipleInstances(true));
  910. instance.registerVersion(name, version);
  911. }
  912. registerDatabase(firebase);
  913. export { registerDatabase };
  914. //# sourceMappingURL=index.esm5.js.map