@fluidframework/driver-web-cache 0.59.3000-66610 → 0.59.3000-67119

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.
@@ -17,10 +17,11 @@ class FluidCache {
17
17
  this.partitionKey = config.partitionKey;
18
18
  this.maxCacheItemAge = config.maxCacheItemAge;
19
19
  (0, scheduleIdleTask_1.scheduleIdleTask)(async () => {
20
+ var _a;
20
21
  // Log how much storage space is currently being used by indexed db.
21
22
  // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.
22
23
  // Exception added when eslint rule was added, this should be revisited when modifying this code
23
- if (navigator.storage && navigator.storage.estimate) {
24
+ if ((_a = navigator.storage) === null || _a === void 0 ? void 0 : _a.estimate) {
24
25
  const estimate = await navigator.storage.estimate();
25
26
  // Some browsers have a usageDetails property that will tell you
26
27
  // more detailed information on how the storage is being used
@@ -1 +1 @@
1
- {"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAWH,qEAA8D;AAC9D,yDAAsD;AACtD,+DAI+B;AAoC/B;;GAEG;AACH,MAAa,UAAU;IAOnB,YAAY,MAAwB;QAChC,IAAI,CAAC,MAAM,GAAG,6BAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,IAAA,mCAAgB,EAAC,KAAK,IAAI,EAAE;YACxB,oEAAoE;YACpE,wGAAwG;YACxG,gGAAgG;YAChG,IAAI,SAAS,CAAC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE;gBACjD,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAEpD,gEAAgE;gBAChE,6DAA6D;gBAC7D,IAAI,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Licensed under the MIT License.\n */\n\nimport {\n IPersistedCache,\n ICacheEntry,\n IFileEntry,\n} from \"@fluidframework/odsp-driver-definitions\";\nimport {\n ITelemetryBaseLogger,\n ITelemetryLogger,\n} from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n getFluidCacheIndexedDbInstance,\n FluidDriverObjectStoreName,\n getKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n FluidCacheErrorEvent,\n FluidCacheEventSubCategories,\n FluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n indexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n /**\n * A string to specify what partition of the cache you wish to use (e.g. a user id).\n * Null can be used to explicity indicate no partitioning, and has been chosen\n * vs undefined so that it is clear this is an intentional choice by the caller.\n * A null value should only be used when the host can ensure that the cache is not able\n * to be shared with multiple users.\n */\n // eslint-disable-next-line @rushstack/no-new-null\n partitionKey: string | null;\n\n /**\n * A logger that can be used to get insight into cache performance and errors\n */\n logger?: ITelemetryBaseLogger;\n\n /**\n * A value in milliseconds that determines the maximum age of a cache entry to return.\n * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n */\n maxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n private readonly logger: ITelemetryLogger;\n\n private readonly partitionKey: string | null;\n\n private readonly maxCacheItemAge: number;\n\n constructor(config: FluidCacheConfig) {\n this.logger = ChildLogger.create(config.logger);\n this.partitionKey = config.partitionKey;\n this.maxCacheItemAge = config.maxCacheItemAge;\n\n scheduleIdleTask(async () => {\n // Log how much storage space is currently being used by indexed db.\n // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n // Exception added when eslint rule was added, this should be revisited when modifying this code\n if (navigator.storage && navigator.storage.estimate) {\n const estimate = await navigator.storage.estimate();\n\n // Some browsers have a usageDetails property that will tell you\n // more detailed information on how the storage is being used\n let indexedDBSize: number | undefined;\n if (\"usageDetails\" in estimate) {\n indexedDBSize = (\n (estimate as any)\n .usageDetails as StorageQuotaUsageDetails\n ).indexedDB;\n }\n\n this.logger.sendTelemetryEvent({\n eventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n quota: estimate.quota,\n usage: estimate.usage,\n indexedDBSize,\n });\n }\n });\n\n scheduleIdleTask(async () => {\n // Delete entries that have not been accessed recently to clean up space\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"lastAccessTimeMs\");\n // Get items that have not been accessed in 4 weeks\n const keysToDelete = await index.getAllKeys(\n IDBKeyRange.upperBound(\n new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000,\n ),\n );\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n });\n }\n\n public async removeEntries(file: IFileEntry): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"fileId\");\n\n const keysToDelete = await index.getAllKeys(file.docId);\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n }\n\n public async get(cacheEntry: ICacheEntry): Promise<any> {\n const startTime = performance.now();\n\n const cachedItem = await this.getItemFromCache(cacheEntry);\n\n this.logger.sendPerformanceEvent({\n eventName: \"FluidCacheAccess\",\n cacheHit: cachedItem !== undefined,\n type: cacheEntry.type,\n duration: performance.now() - startTime,\n });\n\n // Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return cachedItem?.cachedObject;\n }\n\n private async getItemFromCache(cacheEntry: ICacheEntry) {\n try {\n const key = getKeyForCacheEntry(cacheEntry);\n\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const value = await db.get(FluidDriverObjectStoreName, key);\n\n if (!value) {\n return undefined;\n }\n\n // If the data does not come from the same partition, don't return it\n if (value.partitionKey !== this.partitionKey) {\n this.logger.sendTelemetryEvent({\n eventName:\n FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n });\n\n return undefined;\n }\n\n const currentTime = new Date().getTime();\n\n // If too much time has passed since this cache entry was used, we will also return undefined\n if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n return undefined;\n }\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n // We don't want to block the get return of this function on updating the last accessed time\n // We catch this promise because there is no user bad if this is rejected.\n transaction.store\n .get(key)\n .then(async (valueToUpdate) => {\n // This value in the database could have been updated concurrently by other tabs/iframes\n // since we first read it. Only update the last accessed time if the current value in the\n // DB was the same one we returned.\n if (\n valueToUpdate !== undefined &&\n valueToUpdate.createdTimeMs === value.createdTimeMs &&\n (valueToUpdate.lastAccessTimeMs === undefined ||\n valueToUpdate.lastAccessTimeMs < currentTime)\n ) {\n await transaction.store.put(\n { ...valueToUpdate, lastAccessTimeMs: currentTime },\n key,\n );\n }\n await transaction.done;\n\n db.close();\n })\n .catch(() => { });\n return value;\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us. Return undefined in this case\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCacheGetError },\n error,\n );\n return undefined;\n }\n }\n\n public async put(entry: ICacheEntry, value: any): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const currentTime = new Date().getTime();\n\n await db.put(\n FluidDriverObjectStoreName,\n {\n cachedObject: value,\n fileId: entry.file.docId,\n type: entry.type,\n cacheItemId: entry.key,\n partitionKey: this.partitionKey,\n createdTimeMs: currentTime,\n lastAccessTimeMs: currentTime,\n },\n getKeyForCacheEntry(entry),\n );\n\n db.close();\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCachePutError },\n error,\n );\n }\n }\n}\n"]}
1
+ {"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAWH,qEAA8D;AAC9D,yDAAsD;AACtD,+DAI+B;AAoC/B;;GAEG;AACH,MAAa,UAAU;IAOnB,YAAY,MAAwB;QAChC,IAAI,CAAC,MAAM,GAAG,6BAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,IAAA,mCAAgB,EAAC,KAAK,IAAI,EAAE;;YACxB,oEAAoE;YACpE,wGAAwG;YACxG,gGAAgG;YAChG,IAAI,MAAA,SAAS,CAAC,OAAO,0CAAE,QAAQ,EAAE;gBAC7B,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAEpD,gEAAgE;gBAChE,6DAA6D;gBAC7D,IAAI,aAAiC,CAAC;gBACtC,IAAI,cAAc,IAAI,QAAQ,EAAE;oBAC5B,aAAa,GACR,QAAgB;yBACZ,YACR,CAAC,SAAS,CAAC;iBACf;gBAED,IAAI,CAAC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D,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;YACtB,OAAO,KAAK,CAAC;SAChB;QAAC,OAAO,KAAU,EAAE;YACjB,uDAAuD;YACvD,4FAA4F;YAC5F,IAAI,CAAC,MAAM,CAAC,cAAc,CACtB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACR,CAAC;YACF,OAAO,SAAS,CAAC;SACpB;IACL,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,KAAkB,EAAE,KAAU;QAC3C,IAAI;YACA,MAAM,EAAE,GAAG,MAAM,IAAA,oDAA8B,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAE7D,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;YAEzC,MAAM,EAAE,CAAC,GAAG,CACR,gDAA0B,EAC1B;gBACI,YAAY,EAAE,KAAK;gBACnB,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK;gBACxB,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,WAAW,EAAE,KAAK,CAAC,GAAG;gBACtB,YAAY,EAAE,IAAI,CAAC,YAAY;gBAC/B,aAAa,EAAE,WAAW;gBAC1B,gBAAgB,EAAE,WAAW;aAChC,EACD,IAAA,yCAAmB,EAAC,KAAK,CAAC,CAC7B,CAAC;YAEF,EAAE,CAAC,KAAK,EAAE,CAAC;SACd;QAAC,OAAO,KAAU,EAAE;YACjB,uDAAuD;YACvD,6DAA6D;YAC7D,IAAI,CAAC,MAAM,CAAC,cAAc,CACtB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACR,CAAC;SACL;IACL,CAAC;CACJ;AAxND,gCAwNC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n IPersistedCache,\n ICacheEntry,\n IFileEntry,\n} from \"@fluidframework/odsp-driver-definitions\";\nimport {\n ITelemetryBaseLogger,\n ITelemetryLogger,\n} from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n getFluidCacheIndexedDbInstance,\n FluidDriverObjectStoreName,\n getKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n FluidCacheErrorEvent,\n FluidCacheEventSubCategories,\n FluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n indexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n /**\n * A string to specify what partition of the cache you wish to use (e.g. a user id).\n * Null can be used to explicity indicate no partitioning, and has been chosen\n * vs undefined so that it is clear this is an intentional choice by the caller.\n * A null value should only be used when the host can ensure that the cache is not able\n * to be shared with multiple users.\n */\n // eslint-disable-next-line @rushstack/no-new-null\n partitionKey: string | null;\n\n /**\n * A logger that can be used to get insight into cache performance and errors\n */\n logger?: ITelemetryBaseLogger;\n\n /**\n * A value in milliseconds that determines the maximum age of a cache entry to return.\n * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n */\n maxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n private readonly logger: ITelemetryLogger;\n\n private readonly partitionKey: string | null;\n\n private readonly maxCacheItemAge: number;\n\n constructor(config: FluidCacheConfig) {\n this.logger = ChildLogger.create(config.logger);\n this.partitionKey = config.partitionKey;\n this.maxCacheItemAge = config.maxCacheItemAge;\n\n scheduleIdleTask(async () => {\n // Log how much storage space is currently being used by indexed db.\n // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n // Exception added when eslint rule was added, this should be revisited when modifying this code\n if (navigator.storage?.estimate) {\n const estimate = await navigator.storage.estimate();\n\n // Some browsers have a usageDetails property that will tell you\n // more detailed information on how the storage is being used\n let indexedDBSize: number | undefined;\n if (\"usageDetails\" in estimate) {\n indexedDBSize = (\n (estimate as any)\n .usageDetails as StorageQuotaUsageDetails\n ).indexedDB;\n }\n\n this.logger.sendTelemetryEvent({\n eventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n quota: estimate.quota,\n usage: estimate.usage,\n indexedDBSize,\n });\n }\n });\n\n scheduleIdleTask(async () => {\n // Delete entries that have not been accessed recently to clean up space\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"lastAccessTimeMs\");\n // Get items that have not been accessed in 4 weeks\n const keysToDelete = await index.getAllKeys(\n IDBKeyRange.upperBound(\n new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000,\n ),\n );\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n });\n }\n\n public async removeEntries(file: IFileEntry): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"fileId\");\n\n const keysToDelete = await index.getAllKeys(file.docId);\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n }\n\n public async get(cacheEntry: ICacheEntry): Promise<any> {\n const startTime = performance.now();\n\n const cachedItem = await this.getItemFromCache(cacheEntry);\n\n this.logger.sendPerformanceEvent({\n eventName: \"FluidCacheAccess\",\n cacheHit: cachedItem !== undefined,\n type: cacheEntry.type,\n duration: performance.now() - startTime,\n });\n\n // Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return cachedItem?.cachedObject;\n }\n\n private async getItemFromCache(cacheEntry: ICacheEntry) {\n try {\n const key = getKeyForCacheEntry(cacheEntry);\n\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const value = await db.get(FluidDriverObjectStoreName, key);\n\n if (!value) {\n return undefined;\n }\n\n // If the data does not come from the same partition, don't return it\n if (value.partitionKey !== this.partitionKey) {\n this.logger.sendTelemetryEvent({\n eventName:\n FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n });\n\n return undefined;\n }\n\n const currentTime = new Date().getTime();\n\n // If too much time has passed since this cache entry was used, we will also return undefined\n if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n return undefined;\n }\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n // We don't want to block the get return of this function on updating the last accessed time\n // We catch this promise because there is no user bad if this is rejected.\n transaction.store\n .get(key)\n .then(async (valueToUpdate) => {\n // This value in the database could have been updated concurrently by other tabs/iframes\n // since we first read it. Only update the last accessed time if the current value in the\n // DB was the same one we returned.\n if (\n valueToUpdate !== undefined &&\n valueToUpdate.createdTimeMs === value.createdTimeMs &&\n (valueToUpdate.lastAccessTimeMs === undefined ||\n valueToUpdate.lastAccessTimeMs < currentTime)\n ) {\n await transaction.store.put(\n { ...valueToUpdate, lastAccessTimeMs: currentTime },\n key,\n );\n }\n await transaction.done;\n\n db.close();\n })\n .catch(() => { });\n return value;\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us. Return undefined in this case\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCacheGetError },\n error,\n );\n return undefined;\n }\n }\n\n public async put(entry: ICacheEntry, value: any): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const currentTime = new Date().getTime();\n\n await db.put(\n FluidDriverObjectStoreName,\n {\n cachedObject: value,\n fileId: entry.file.docId,\n type: entry.type,\n cacheItemId: entry.key,\n partitionKey: this.partitionKey,\n createdTimeMs: currentTime,\n lastAccessTimeMs: currentTime,\n },\n getKeyForCacheEntry(entry),\n );\n\n db.close();\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCachePutError },\n error,\n );\n }\n }\n}\n"]}
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export declare const pkgName = "@fluidframework/driver-web-cache";
8
- export declare const pkgVersion = "0.59.3000-66610";
8
+ export declare const pkgVersion = "0.59.3000-67119";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -8,5 +8,5 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.pkgVersion = exports.pkgName = void 0;
10
10
  exports.pkgName = "@fluidframework/driver-web-cache";
11
- exports.pkgVersion = "0.59.3000-66610";
11
+ exports.pkgVersion = "0.59.3000-67119";
12
12
  //# sourceMappingURL=packageVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,kCAAkC,CAAC;AAC7C,QAAA,UAAU,GAAG,iBAAiB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"0.59.3000-66610\";\n"]}
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,kCAAkC,CAAC;AAC7C,QAAA,UAAU,GAAG,iBAAiB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"0.59.3000-67119\";\n"]}
package/lib/FluidCache.js CHANGED
@@ -14,10 +14,11 @@ export class FluidCache {
14
14
  this.partitionKey = config.partitionKey;
15
15
  this.maxCacheItemAge = config.maxCacheItemAge;
16
16
  scheduleIdleTask(async () => {
17
+ var _a;
17
18
  // Log how much storage space is currently being used by indexed db.
18
19
  // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.
19
20
  // Exception added when eslint rule was added, this should be revisited when modifying this code
20
- if (navigator.storage && navigator.storage.estimate) {
21
+ if ((_a = navigator.storage) === null || _a === void 0 ? void 0 : _a.estimate) {
21
22
  const estimate = await navigator.storage.estimate();
22
23
  // Some browsers have a usageDetails property that will tell you
23
24
  // more detailed information on how the storage is being used
@@ -1 +1 @@
1
- {"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACH,8BAA8B,EAC9B,0BAA0B,EAC1B,mBAAmB,GACtB,MAAM,uBAAuB,CAAC;AAoC/B;;GAEG;AACH,MAAM,OAAO,UAAU;IAOnB,YAAY,MAAwB;QAChC,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,e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,CAAC;SACd;QAAC,OAAO,KAAU,EAAE;YACjB,uDAAuD;YACvD,6DAA6D;YAC7D,IAAI,CAAC,MAAM,CAAC,cAAc,CACtB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACR,CAAC;SACL;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n IPersistedCache,\n ICacheEntry,\n IFileEntry,\n} from \"@fluidframework/odsp-driver-definitions\";\nimport {\n ITelemetryBaseLogger,\n ITelemetryLogger,\n} from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n getFluidCacheIndexedDbInstance,\n FluidDriverObjectStoreName,\n getKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n FluidCacheErrorEvent,\n FluidCacheEventSubCategories,\n FluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n indexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n /**\n * A string to specify what partition of the cache you wish to use (e.g. a user id).\n * Null can be used to explicity indicate no partitioning, and has been chosen\n * vs undefined so that it is clear this is an intentional choice by the caller.\n * A null value should only be used when the host can ensure that the cache is not able\n * to be shared with multiple users.\n */\n // eslint-disable-next-line @rushstack/no-new-null\n partitionKey: string | null;\n\n /**\n * A logger that can be used to get insight into cache performance and errors\n */\n logger?: ITelemetryBaseLogger;\n\n /**\n * A value in milliseconds that determines the maximum age of a cache entry to return.\n * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n */\n maxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n private readonly logger: ITelemetryLogger;\n\n private readonly partitionKey: string | null;\n\n private readonly maxCacheItemAge: number;\n\n constructor(config: FluidCacheConfig) {\n this.logger = ChildLogger.create(config.logger);\n this.partitionKey = config.partitionKey;\n this.maxCacheItemAge = config.maxCacheItemAge;\n\n scheduleIdleTask(async () => {\n // Log how much storage space is currently being used by indexed db.\n // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n // Exception added when eslint rule was added, this should be revisited when modifying this code\n if (navigator.storage && navigator.storage.estimate) {\n const estimate = await navigator.storage.estimate();\n\n // Some browsers have a usageDetails property that will tell you\n // more detailed information on how the storage is being used\n let indexedDBSize: number | undefined;\n if (\"usageDetails\" in estimate) {\n indexedDBSize = (\n (estimate as any)\n .usageDetails as StorageQuotaUsageDetails\n ).indexedDB;\n }\n\n this.logger.sendTelemetryEvent({\n eventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n quota: estimate.quota,\n usage: estimate.usage,\n indexedDBSize,\n });\n }\n });\n\n scheduleIdleTask(async () => {\n // Delete entries that have not been accessed recently to clean up space\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"lastAccessTimeMs\");\n // Get items that have not been accessed in 4 weeks\n const keysToDelete = await index.getAllKeys(\n IDBKeyRange.upperBound(\n new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000,\n ),\n );\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n });\n }\n\n public async removeEntries(file: IFileEntry): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"fileId\");\n\n const keysToDelete = await index.getAllKeys(file.docId);\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n }\n\n public async get(cacheEntry: ICacheEntry): Promise<any> {\n const startTime = performance.now();\n\n const cachedItem = await this.getItemFromCache(cacheEntry);\n\n this.logger.sendPerformanceEvent({\n eventName: \"FluidCacheAccess\",\n cacheHit: cachedItem !== undefined,\n type: cacheEntry.type,\n duration: performance.now() - startTime,\n });\n\n // Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return cachedItem?.cachedObject;\n }\n\n private async getItemFromCache(cacheEntry: ICacheEntry) {\n try {\n const key = getKeyForCacheEntry(cacheEntry);\n\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const value = await db.get(FluidDriverObjectStoreName, key);\n\n if (!value) {\n return undefined;\n }\n\n // If the data does not come from the same partition, don't return it\n if (value.partitionKey !== this.partitionKey) {\n this.logger.sendTelemetryEvent({\n eventName:\n FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n });\n\n return undefined;\n }\n\n const currentTime = new Date().getTime();\n\n // If too much time has passed since this cache entry was used, we will also return undefined\n if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n return undefined;\n }\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n // We don't want to block the get return of this function on updating the last accessed time\n // We catch this promise because there is no user bad if this is rejected.\n transaction.store\n .get(key)\n .then(async (valueToUpdate) => {\n // This value in the database could have been updated concurrently by other tabs/iframes\n // since we first read it. Only update the last accessed time if the current value in the\n // DB was the same one we returned.\n if (\n valueToUpdate !== undefined &&\n valueToUpdate.createdTimeMs === value.createdTimeMs &&\n (valueToUpdate.lastAccessTimeMs === undefined ||\n valueToUpdate.lastAccessTimeMs < currentTime)\n ) {\n await transaction.store.put(\n { ...valueToUpdate, lastAccessTimeMs: currentTime },\n key,\n );\n }\n await transaction.done;\n\n db.close();\n })\n .catch(() => { });\n return value;\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us. Return undefined in this case\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCacheGetError },\n error,\n );\n return undefined;\n }\n }\n\n public async put(entry: ICacheEntry, value: any): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const currentTime = new Date().getTime();\n\n await db.put(\n FluidDriverObjectStoreName,\n {\n cachedObject: value,\n fileId: entry.file.docId,\n type: entry.type,\n cacheItemId: entry.key,\n partitionKey: this.partitionKey,\n createdTimeMs: currentTime,\n lastAccessTimeMs: currentTime,\n },\n getKeyForCacheEntry(entry),\n );\n\n db.close();\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCachePutError },\n error,\n );\n }\n }\n}\n"]}
1
+ {"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACH,8BAA8B,EAC9B,0BAA0B,EAC1B,mBAAmB,GACtB,MAAM,uBAAuB,CAAC;AAoC/B;;GAEG;AACH,MAAM,OAAO,UAAU;IAOnB,YAAY,MAAwB;QAChC,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,gBAAgB,CAAC,K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D;YACvD,6DAA6D;YAC7D,IAAI,CAAC,MAAM,CAAC,cAAc,CACtB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACR,CAAC;SACL;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n IPersistedCache,\n ICacheEntry,\n IFileEntry,\n} from \"@fluidframework/odsp-driver-definitions\";\nimport {\n ITelemetryBaseLogger,\n ITelemetryLogger,\n} from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n getFluidCacheIndexedDbInstance,\n FluidDriverObjectStoreName,\n getKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n FluidCacheErrorEvent,\n FluidCacheEventSubCategories,\n FluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n indexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n /**\n * A string to specify what partition of the cache you wish to use (e.g. a user id).\n * Null can be used to explicity indicate no partitioning, and has been chosen\n * vs undefined so that it is clear this is an intentional choice by the caller.\n * A null value should only be used when the host can ensure that the cache is not able\n * to be shared with multiple users.\n */\n // eslint-disable-next-line @rushstack/no-new-null\n partitionKey: string | null;\n\n /**\n * A logger that can be used to get insight into cache performance and errors\n */\n logger?: ITelemetryBaseLogger;\n\n /**\n * A value in milliseconds that determines the maximum age of a cache entry to return.\n * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n */\n maxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n private readonly logger: ITelemetryLogger;\n\n private readonly partitionKey: string | null;\n\n private readonly maxCacheItemAge: number;\n\n constructor(config: FluidCacheConfig) {\n this.logger = ChildLogger.create(config.logger);\n this.partitionKey = config.partitionKey;\n this.maxCacheItemAge = config.maxCacheItemAge;\n\n scheduleIdleTask(async () => {\n // Log how much storage space is currently being used by indexed db.\n // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n // Exception added when eslint rule was added, this should be revisited when modifying this code\n if (navigator.storage?.estimate) {\n const estimate = await navigator.storage.estimate();\n\n // Some browsers have a usageDetails property that will tell you\n // more detailed information on how the storage is being used\n let indexedDBSize: number | undefined;\n if (\"usageDetails\" in estimate) {\n indexedDBSize = (\n (estimate as any)\n .usageDetails as StorageQuotaUsageDetails\n ).indexedDB;\n }\n\n this.logger.sendTelemetryEvent({\n eventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n quota: estimate.quota,\n usage: estimate.usage,\n indexedDBSize,\n });\n }\n });\n\n scheduleIdleTask(async () => {\n // Delete entries that have not been accessed recently to clean up space\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"lastAccessTimeMs\");\n // Get items that have not been accessed in 4 weeks\n const keysToDelete = await index.getAllKeys(\n IDBKeyRange.upperBound(\n new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000,\n ),\n );\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n });\n }\n\n public async removeEntries(file: IFileEntry): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n const index = transaction.store.index(\"fileId\");\n\n const keysToDelete = await index.getAllKeys(file.docId);\n\n await Promise.all(\n keysToDelete.map((key) => transaction.store.delete(key)),\n );\n await transaction.done;\n } catch (error: any) {\n this.logger.sendErrorEvent(\n {\n eventName:\n FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n },\n error,\n );\n }\n }\n\n public async get(cacheEntry: ICacheEntry): Promise<any> {\n const startTime = performance.now();\n\n const cachedItem = await this.getItemFromCache(cacheEntry);\n\n this.logger.sendPerformanceEvent({\n eventName: \"FluidCacheAccess\",\n cacheHit: cachedItem !== undefined,\n type: cacheEntry.type,\n duration: performance.now() - startTime,\n });\n\n // Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return cachedItem?.cachedObject;\n }\n\n private async getItemFromCache(cacheEntry: ICacheEntry) {\n try {\n const key = getKeyForCacheEntry(cacheEntry);\n\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const value = await db.get(FluidDriverObjectStoreName, key);\n\n if (!value) {\n return undefined;\n }\n\n // If the data does not come from the same partition, don't return it\n if (value.partitionKey !== this.partitionKey) {\n this.logger.sendTelemetryEvent({\n eventName:\n FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n subCategory: FluidCacheEventSubCategories.FluidCache,\n });\n\n return undefined;\n }\n\n const currentTime = new Date().getTime();\n\n // If too much time has passed since this cache entry was used, we will also return undefined\n if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n return undefined;\n }\n\n const transaction = db.transaction(\n FluidDriverObjectStoreName,\n \"readwrite\",\n );\n // We don't want to block the get return of this function on updating the last accessed time\n // We catch this promise because there is no user bad if this is rejected.\n transaction.store\n .get(key)\n .then(async (valueToUpdate) => {\n // This value in the database could have been updated concurrently by other tabs/iframes\n // since we first read it. Only update the last accessed time if the current value in the\n // DB was the same one we returned.\n if (\n valueToUpdate !== undefined &&\n valueToUpdate.createdTimeMs === value.createdTimeMs &&\n (valueToUpdate.lastAccessTimeMs === undefined ||\n valueToUpdate.lastAccessTimeMs < currentTime)\n ) {\n await transaction.store.put(\n { ...valueToUpdate, lastAccessTimeMs: currentTime },\n key,\n );\n }\n await transaction.done;\n\n db.close();\n })\n .catch(() => { });\n return value;\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us. Return undefined in this case\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCacheGetError },\n error,\n );\n return undefined;\n }\n }\n\n public async put(entry: ICacheEntry, value: any): Promise<void> {\n try {\n const db = await getFluidCacheIndexedDbInstance(this.logger);\n\n const currentTime = new Date().getTime();\n\n await db.put(\n FluidDriverObjectStoreName,\n {\n cachedObject: value,\n fileId: entry.file.docId,\n type: entry.type,\n cacheItemId: entry.key,\n partitionKey: this.partitionKey,\n createdTimeMs: currentTime,\n lastAccessTimeMs: currentTime,\n },\n getKeyForCacheEntry(entry),\n );\n\n db.close();\n } catch (error: any) {\n // We can fail to open the db for a variety of reasons,\n // such as the database version having upgraded underneath us\n this.logger.sendErrorEvent(\n { eventName: FluidCacheErrorEvent.FluidCachePutError },\n error,\n );\n }\n }\n}\n"]}
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export declare const pkgName = "@fluidframework/driver-web-cache";
8
- export declare const pkgVersion = "0.59.3000-66610";
8
+ export declare const pkgVersion = "0.59.3000-67119";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -5,5 +5,5 @@
5
5
  * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
6
  */
7
7
  export const pkgName = "@fluidframework/driver-web-cache";
8
- export const pkgVersion = "0.59.3000-66610";
8
+ export const pkgVersion = "0.59.3000-67119";
9
9
  //# sourceMappingURL=packageVersion.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,kCAAkC,CAAC;AAC1D,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"0.59.3000-66610\";\n"]}
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,kCAAkC,CAAC;AAC1D,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"0.59.3000-67119\";\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/driver-web-cache",
3
- "version": "0.59.3000-66610",
3
+ "version": "0.59.3000-67119",
4
4
  "description": "Implementation of the driver caching API for a web browser",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -38,13 +38,13 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@fluidframework/common-definitions": "^0.20.1",
41
- "@fluidframework/odsp-driver-definitions": "0.59.3000-66610",
42
- "@fluidframework/telemetry-utils": "0.59.3000-66610",
41
+ "@fluidframework/odsp-driver-definitions": "0.59.3000-67119",
42
+ "@fluidframework/telemetry-utils": "0.59.3000-67119",
43
43
  "idb": "^6.1.2"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@fluidframework/build-common": "^0.23.0",
47
- "@fluidframework/build-tools": "^0.2.66048",
47
+ "@fluidframework/build-tools": "^0.2.66793",
48
48
  "@fluidframework/driver-web-cache-previous": "npm:@fluidframework/driver-web-cache@0.59.2000",
49
49
  "@fluidframework/eslint-config-fluid": "^0.28.2000-0",
50
50
  "@microsoft/api-extractor": "^7.22.2",
package/src/FluidCache.ts CHANGED
@@ -73,7 +73,7 @@ export class FluidCache implements IPersistedCache {
73
73
  // Log how much storage space is currently being used by indexed db.
74
74
  // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.
75
75
  // Exception added when eslint rule was added, this should be revisited when modifying this code
76
- if (navigator.storage && navigator.storage.estimate) {
76
+ if (navigator.storage?.estimate) {
77
77
  const estimate = await navigator.storage.estimate();
78
78
 
79
79
  // Some browsers have a usageDetails property that will tell you
@@ -6,4 +6,4 @@
6
6
  */
7
7
 
8
8
  export const pkgName = "@fluidframework/driver-web-cache";
9
- export const pkgVersion = "0.59.3000-66610";
9
+ export const pkgVersion = "0.59.3000-67119";