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.js 39KB

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