@firebase/util 1.4.3-canary.ff2f7d4c8 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1891,6 +1891,150 @@ function getModularInstance(service) {
1891
1891
  }
1892
1892
  }
1893
1893
 
1894
+ /**
1895
+ * @license
1896
+ * Copyright 2022 Google LLC
1897
+ *
1898
+ * Licensed under the Apache License, Version 2.0 (the "License");
1899
+ * you may not use this file except in compliance with the License.
1900
+ * You may obtain a copy of the License at
1901
+ *
1902
+ * http://www.apache.org/licenses/LICENSE-2.0
1903
+ *
1904
+ * Unless required by applicable law or agreed to in writing, software
1905
+ * distributed under the License is distributed on an "AS IS" BASIS,
1906
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1907
+ * See the License for the specific language governing permissions and
1908
+ * limitations under the License.
1909
+ */
1910
+ function promisifyRequest(request, errorMessage) {
1911
+ return new Promise(function (resolve, reject) {
1912
+ request.onsuccess = function (event) {
1913
+ resolve(event.target.result);
1914
+ };
1915
+ request.onerror = function (event) {
1916
+ var _a;
1917
+ reject(errorMessage + ": " + ((_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message));
1918
+ };
1919
+ });
1920
+ }
1921
+ var DBWrapper = /** @class */ (function () {
1922
+ function DBWrapper(_db) {
1923
+ this._db = _db;
1924
+ this.objectStoreNames = this._db.objectStoreNames;
1925
+ }
1926
+ DBWrapper.prototype.transaction = function (storeNames, mode) {
1927
+ return new TransactionWrapper(this._db.transaction.call(this._db, storeNames, mode));
1928
+ };
1929
+ DBWrapper.prototype.createObjectStore = function (storeName, options) {
1930
+ return new ObjectStoreWrapper(this._db.createObjectStore(storeName, options));
1931
+ };
1932
+ DBWrapper.prototype.close = function () {
1933
+ this._db.close();
1934
+ };
1935
+ return DBWrapper;
1936
+ }());
1937
+ var TransactionWrapper = /** @class */ (function () {
1938
+ function TransactionWrapper(_transaction) {
1939
+ var _this = this;
1940
+ this._transaction = _transaction;
1941
+ this.complete = new Promise(function (resolve, reject) {
1942
+ _this._transaction.oncomplete = function () {
1943
+ resolve();
1944
+ };
1945
+ _this._transaction.onerror = function () {
1946
+ reject(_this._transaction.error);
1947
+ };
1948
+ _this._transaction.onabort = function () {
1949
+ reject(_this._transaction.error);
1950
+ };
1951
+ });
1952
+ }
1953
+ TransactionWrapper.prototype.objectStore = function (storeName) {
1954
+ return new ObjectStoreWrapper(this._transaction.objectStore(storeName));
1955
+ };
1956
+ return TransactionWrapper;
1957
+ }());
1958
+ var ObjectStoreWrapper = /** @class */ (function () {
1959
+ function ObjectStoreWrapper(_store) {
1960
+ this._store = _store;
1961
+ }
1962
+ ObjectStoreWrapper.prototype.index = function (name) {
1963
+ return new IndexWrapper(this._store.index(name));
1964
+ };
1965
+ ObjectStoreWrapper.prototype.createIndex = function (name, keypath, options) {
1966
+ return new IndexWrapper(this._store.createIndex(name, keypath, options));
1967
+ };
1968
+ ObjectStoreWrapper.prototype.get = function (key) {
1969
+ var request = this._store.get(key);
1970
+ return promisifyRequest(request, 'Error reading from IndexedDB');
1971
+ };
1972
+ ObjectStoreWrapper.prototype.put = function (value, key) {
1973
+ var request = this._store.put(value, key);
1974
+ return promisifyRequest(request, 'Error writing to IndexedDB');
1975
+ };
1976
+ ObjectStoreWrapper.prototype.delete = function (key) {
1977
+ var request = this._store.delete(key);
1978
+ return promisifyRequest(request, 'Error deleting from IndexedDB');
1979
+ };
1980
+ ObjectStoreWrapper.prototype.clear = function () {
1981
+ var request = this._store.clear();
1982
+ return promisifyRequest(request, 'Error clearing IndexedDB object store');
1983
+ };
1984
+ return ObjectStoreWrapper;
1985
+ }());
1986
+ var IndexWrapper = /** @class */ (function () {
1987
+ function IndexWrapper(_index) {
1988
+ this._index = _index;
1989
+ }
1990
+ IndexWrapper.prototype.get = function (key) {
1991
+ var request = this._index.get(key);
1992
+ return promisifyRequest(request, 'Error reading from IndexedDB');
1993
+ };
1994
+ return IndexWrapper;
1995
+ }());
1996
+ function openDB(dbName, dbVersion, upgradeCallback) {
1997
+ return new Promise(function (resolve, reject) {
1998
+ try {
1999
+ var request_1 = indexedDB.open(dbName, dbVersion);
2000
+ request_1.onsuccess = function (event) {
2001
+ resolve(new DBWrapper(event.target.result));
2002
+ };
2003
+ request_1.onerror = function (event) {
2004
+ var _a;
2005
+ reject("Error opening indexedDB: " + ((_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message));
2006
+ };
2007
+ request_1.onupgradeneeded = function (event) {
2008
+ upgradeCallback(new DBWrapper(request_1.result), event.oldVersion, event.newVersion, new TransactionWrapper(request_1.transaction));
2009
+ };
2010
+ }
2011
+ catch (e) {
2012
+ reject("Error opening indexedDB: " + e.message);
2013
+ }
2014
+ });
2015
+ }
2016
+ function deleteDB(dbName) {
2017
+ return tslib.__awaiter(this, void 0, void 0, function () {
2018
+ return tslib.__generator(this, function (_a) {
2019
+ return [2 /*return*/, new Promise(function (resolve, reject) {
2020
+ try {
2021
+ var request = indexedDB.deleteDatabase(dbName);
2022
+ request.onsuccess = function () {
2023
+ resolve();
2024
+ };
2025
+ request.onerror = function (event) {
2026
+ var _a;
2027
+ reject("Error deleting indexedDB database \"" + dbName + "\": " + ((_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message));
2028
+ };
2029
+ }
2030
+ catch (e) {
2031
+ reject("Error deleting indexedDB database \"" + dbName + "\": " + e.message);
2032
+ }
2033
+ })];
2034
+ });
2035
+ });
2036
+ }
2037
+
1894
2038
  /**
1895
2039
  * @license
1896
2040
  * Copyright 2017 Google LLC
@@ -1911,6 +2055,7 @@ function getModularInstance(service) {
1911
2055
  CONSTANTS.NODE_CLIENT = true;
1912
2056
 
1913
2057
  exports.CONSTANTS = CONSTANTS;
2058
+ exports.DBWrapper = DBWrapper;
1914
2059
  exports.Deferred = Deferred;
1915
2060
  exports.ErrorFactory = ErrorFactory;
1916
2061
  exports.FirebaseError = FirebaseError;
@@ -1933,6 +2078,7 @@ exports.decode = decode;
1933
2078
  exports.deepCopy = deepCopy;
1934
2079
  exports.deepEqual = deepEqual;
1935
2080
  exports.deepExtend = deepExtend;
2081
+ exports.deleteDB = deleteDB;
1936
2082
  exports.errorPrefix = errorPrefix;
1937
2083
  exports.extractQuerystring = extractQuerystring;
1938
2084
  exports.getGlobal = getGlobal;
@@ -1956,6 +2102,7 @@ exports.isValidTimestamp = isValidTimestamp;
1956
2102
  exports.issuedAtTime = issuedAtTime;
1957
2103
  exports.jsonEval = jsonEval;
1958
2104
  exports.map = map;
2105
+ exports.openDB = openDB;
1959
2106
  exports.ordinal = ordinal;
1960
2107
  exports.querystring = querystring;
1961
2108
  exports.querystringDecode = querystringDecode;