@fluidframework/driver-web-cache 0.58.2000-58133

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.
Files changed (62) hide show
  1. package/.eslintrc.js +19 -0
  2. package/LICENSE +21 -0
  3. package/README.md +56 -0
  4. package/api-extractor.json +4 -0
  5. package/dist/FluidCache.d.ts +39 -0
  6. package/dist/FluidCache.d.ts.map +1 -0
  7. package/dist/FluidCache.js +159 -0
  8. package/dist/FluidCache.js.map +1 -0
  9. package/dist/FluidCacheIndexedDb.d.ts +65 -0
  10. package/dist/FluidCacheIndexedDb.d.ts.map +1 -0
  11. package/dist/FluidCacheIndexedDb.js +65 -0
  12. package/dist/FluidCacheIndexedDb.js.map +1 -0
  13. package/dist/fluidCacheTelemetry.d.ts +19 -0
  14. package/dist/fluidCacheTelemetry.d.ts.map +1 -0
  15. package/dist/fluidCacheTelemetry.js +7 -0
  16. package/dist/fluidCacheTelemetry.js.map +1 -0
  17. package/dist/index.d.ts +7 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +21 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/packageVersion.d.ts +9 -0
  22. package/dist/packageVersion.d.ts.map +1 -0
  23. package/dist/packageVersion.js +12 -0
  24. package/dist/packageVersion.js.map +1 -0
  25. package/dist/scheduleIdleTask.d.ts +22 -0
  26. package/dist/scheduleIdleTask.d.ts.map +1 -0
  27. package/dist/scheduleIdleTask.js +80 -0
  28. package/dist/scheduleIdleTask.js.map +1 -0
  29. package/jest.config.js +12 -0
  30. package/lib/FluidCache.d.ts +39 -0
  31. package/lib/FluidCache.d.ts.map +1 -0
  32. package/lib/FluidCache.js +155 -0
  33. package/lib/FluidCache.js.map +1 -0
  34. package/lib/FluidCacheIndexedDb.d.ts +65 -0
  35. package/lib/FluidCacheIndexedDb.d.ts.map +1 -0
  36. package/lib/FluidCacheIndexedDb.js +59 -0
  37. package/lib/FluidCacheIndexedDb.js.map +1 -0
  38. package/lib/fluidCacheTelemetry.d.ts +19 -0
  39. package/lib/fluidCacheTelemetry.d.ts.map +1 -0
  40. package/lib/fluidCacheTelemetry.js +6 -0
  41. package/lib/fluidCacheTelemetry.js.map +1 -0
  42. package/lib/index.d.ts +7 -0
  43. package/lib/index.d.ts.map +1 -0
  44. package/lib/index.js +7 -0
  45. package/lib/index.js.map +1 -0
  46. package/lib/packageVersion.d.ts +9 -0
  47. package/lib/packageVersion.d.ts.map +1 -0
  48. package/lib/packageVersion.js +9 -0
  49. package/lib/packageVersion.js.map +1 -0
  50. package/lib/scheduleIdleTask.d.ts +22 -0
  51. package/lib/scheduleIdleTask.d.ts.map +1 -0
  52. package/lib/scheduleIdleTask.js +76 -0
  53. package/lib/scheduleIdleTask.js.map +1 -0
  54. package/package.json +67 -0
  55. package/src/FluidCache.ts +276 -0
  56. package/src/FluidCacheIndexedDb.ts +155 -0
  57. package/src/fluidCacheTelemetry.ts +21 -0
  58. package/src/index.ts +7 -0
  59. package/src/packageVersion.ts +9 -0
  60. package/src/scheduleIdleTask.ts +119 -0
  61. package/tsconfig.esnext.json +7 -0
  62. package/tsconfig.json +15 -0
@@ -0,0 +1,76 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ // A set of tasks that still have to be run
6
+ let taskQueue = [];
7
+ // Set to true when we have a pending idle task scheduled
8
+ let idleTaskScheduled = false;
9
+ /**
10
+ * A function that schedules a non critical task to be run when the browser has cycles available
11
+ * @param task - The task to be executed
12
+ * @param options - Optional configuration for the task execution
13
+ */
14
+ export function scheduleIdleTask(task) {
15
+ taskQueue.push({
16
+ task,
17
+ });
18
+ ensureIdleCallback();
19
+ }
20
+ /**
21
+ * Ensures an idle callback has been scheduled for the remaining tasks
22
+ */
23
+ function ensureIdleCallback() {
24
+ if (!idleTaskScheduled) {
25
+ // Exception added when eslint rule was added, this should be revisited when modifying this code
26
+ if (window.requestIdleCallback) {
27
+ window.requestIdleCallback(idleTaskCallback);
28
+ }
29
+ else {
30
+ const deadline = Date.now() + 50;
31
+ window.setTimeout(() => idleTaskCallback({
32
+ timeRemaining: () => Math.max(deadline - Date.now(), 0),
33
+ didTimeout: false,
34
+ }), 0);
35
+ }
36
+ idleTaskScheduled = true;
37
+ }
38
+ }
39
+ /**
40
+ * Runs tasks from the task queue
41
+ * @param filter - An optional function that will be called for each task to see if it should run.
42
+ * Returns false for tasks that should not run. If omitted all tasks run.
43
+ * @param shouldContinueRunning - An optional function that will be called to determine if
44
+ * we have enough time to continue running tasks. If omitted, we don't stop running tasks.
45
+ */
46
+ function runTasks(filter, shouldContinueRunning) {
47
+ // The next value for the task queue
48
+ const newTaskQueue = [];
49
+ for (let index = 0; index < taskQueue.length; index += 1) {
50
+ if (shouldContinueRunning && !shouldContinueRunning()) {
51
+ // Add the tasks we didn't get to to the end of the new task queue
52
+ newTaskQueue.push(...taskQueue.slice(index));
53
+ break;
54
+ }
55
+ const taskQueueItem = taskQueue[index];
56
+ if (filter && !filter(taskQueueItem)) {
57
+ newTaskQueue.push(taskQueueItem);
58
+ }
59
+ else {
60
+ taskQueueItem.task();
61
+ }
62
+ }
63
+ taskQueue = newTaskQueue;
64
+ }
65
+ // Runs all the tasks in the task queue
66
+ function idleTaskCallback(deadline) {
67
+ // Minimum time that must be available on deadline to run any more tasks
68
+ const minTaskTime = 10;
69
+ runTasks(undefined, () => deadline.timeRemaining() > minTaskTime);
70
+ idleTaskScheduled = false;
71
+ // If we didn't run through the entire queue, schedule another idle callback
72
+ if (taskQueue.length > 0) {
73
+ ensureIdleCallback();
74
+ }
75
+ }
76
+ //# sourceMappingURL=scheduleIdleTask.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduleIdleTask.js","sourceRoot":"","sources":["../src/scheduleIdleTask.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA0BH,2CAA2C;AAC3C,IAAI,SAAS,GAAoB,EAAE,CAAC;AAEpC,yDAAyD;AACzD,IAAI,iBAAiB,GAAG,KAAK,CAAC;AAE9B;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAgB;IAC7C,SAAS,CAAC,IAAI,CAAC;QACX,IAAI;KACP,CAAC,CAAC;IAEH,kBAAkB,EAAE,CAAC;AACzB,CAAC;AAED;;GAEG;AACH,SAAS,kBAAkB;IACvB,IAAI,CAAC,iBAAiB,EAAE;QACpB,gGAAgG;QAChG,IAAI,MAAM,CAAC,mBAAmB,EAAE;YAC5B,MAAM,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CAAC;SAChD;aAAM;YACH,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;YACjC,MAAM,CAAC,UAAU,CACb,GAAG,EAAE,CACD,gBAAgB,CAAC;gBACb,aAAa,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;gBACvD,UAAU,EAAE,KAAK;aACpB,CAAC,EACN,CAAC,CACJ,CAAC;SACL;QACD,iBAAiB,GAAG,IAAI,CAAC;KAC5B;AACL,CAAC;AAED;;;;;;GAMG;AACH,SAAS,QAAQ,CACb,MAAkD,EAClD,qBAAqC;IAErC,oCAAoC;IACpC,MAAM,YAAY,GAAoB,EAAE,CAAC;IAEzC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE;QACtD,IAAI,qBAAqB,IAAI,CAAC,qBAAqB,EAAE,EAAE;YACnD,kEAAkE;YAClE,YAAY,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;YAC7C,MAAM;SACT;QAED,MAAM,aAAa,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAEvC,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE;YAClC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;SACpC;aAAM;YACH,aAAa,CAAC,IAAI,EAAE,CAAC;SACxB;KACJ;IAED,SAAS,GAAG,YAAY,CAAC;AAC7B,CAAC;AAED,uCAAuC;AACvC,SAAS,gBAAgB,CAAC,QAGzB;IACG,wEAAwE;IACxE,MAAM,WAAW,GAAG,EAAE,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,aAAa,EAAE,GAAG,WAAW,CAAC,CAAC;IAClE,iBAAiB,GAAG,KAAK,CAAC;IAE1B,4EAA4E;IAC5E,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE;QACtB,kBAAkB,EAAE,CAAC;KACxB;AACL,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n// Older versions of Typescript considered this API experimental and not in Typescript included types.\n// This can be removed when FF updates to Typescript 4.4 or later\ndeclare global {\n interface Window {\n requestIdleCallback:\n | ((\n callback: (deadline: {\n readonly didTimeout: boolean;\n timeRemaining: () => number;\n }) => void,\n opts?: {\n timeout: number;\n }\n ) => number)\n | undefined;\n cancelIdleCallback: ((handle: number) => void) | undefined;\n }\n}\n\ninterface TaskQueueItem {\n /** The task to run */\n task: () => void;\n}\n\n// A set of tasks that still have to be run\nlet taskQueue: TaskQueueItem[] = [];\n\n// Set to true when we have a pending idle task scheduled\nlet idleTaskScheduled = false;\n\n/**\n * A function that schedules a non critical task to be run when the browser has cycles available\n * @param task - The task to be executed\n * @param options - Optional configuration for the task execution\n */\nexport function scheduleIdleTask(task: () => void) {\n taskQueue.push({\n task,\n });\n\n ensureIdleCallback();\n}\n\n/**\n * Ensures an idle callback has been scheduled for the remaining tasks\n */\nfunction ensureIdleCallback() {\n if (!idleTaskScheduled) {\n // Exception added when eslint rule was added, this should be revisited when modifying this code\n if (window.requestIdleCallback) {\n window.requestIdleCallback(idleTaskCallback);\n } else {\n const deadline = Date.now() + 50;\n window.setTimeout(\n () =>\n idleTaskCallback({\n timeRemaining: () => Math.max(deadline - Date.now(), 0),\n didTimeout: false,\n }),\n 0,\n );\n }\n idleTaskScheduled = true;\n }\n}\n\n/**\n * Runs tasks from the task queue\n * @param filter - An optional function that will be called for each task to see if it should run.\n * Returns false for tasks that should not run. If omitted all tasks run.\n * @param shouldContinueRunning - An optional function that will be called to determine if\n * we have enough time to continue running tasks. If omitted, we don't stop running tasks.\n */\nfunction runTasks(\n filter?: (taskQueueItem: TaskQueueItem) => boolean,\n shouldContinueRunning?: () => boolean,\n) {\n // The next value for the task queue\n const newTaskQueue: TaskQueueItem[] = [];\n\n for (let index = 0; index < taskQueue.length; index += 1) {\n if (shouldContinueRunning && !shouldContinueRunning()) {\n // Add the tasks we didn't get to to the end of the new task queue\n newTaskQueue.push(...taskQueue.slice(index));\n break;\n }\n\n const taskQueueItem = taskQueue[index];\n\n if (filter && !filter(taskQueueItem)) {\n newTaskQueue.push(taskQueueItem);\n } else {\n taskQueueItem.task();\n }\n }\n\n taskQueue = newTaskQueue;\n}\n\n// Runs all the tasks in the task queue\nfunction idleTaskCallback(deadline: {\n timeRemaining: () => number;\n readonly didTimeout: boolean;\n}) {\n // Minimum time that must be available on deadline to run any more tasks\n const minTaskTime = 10;\n runTasks(undefined, () => deadline.timeRemaining() > minTaskTime);\n idleTaskScheduled = false;\n\n // If we didn't run through the entire queue, schedule another idle callback\n if (taskQueue.length > 0) {\n ensureIdleCallback();\n }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@fluidframework/driver-web-cache",
3
+ "version": "0.58.2000-58133",
4
+ "description": "Implementation of the driver caching API for a web browser",
5
+ "homepage": "https://fluidframework.com",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/microsoft/FluidFramework.git",
9
+ "directory": "packages/drivers/driver-web-cache"
10
+ },
11
+ "license": "MIT",
12
+ "author": "Microsoft and contributors",
13
+ "sideEffects": false,
14
+ "main": "dist/index.js",
15
+ "module": "lib/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "scripts": {
18
+ "build": "npm run build:genver && concurrently npm:build:compile npm:lint && npm run build:docs",
19
+ "build:commonjs": "npm run tsc && npm run build:test",
20
+ "build:compile": "concurrently npm:build:commonjs npm:build:esnext",
21
+ "build:docs": "api-extractor run --local --typescript-compiler-folder ../../../node_modules/typescript && copyfiles -u 1 ./_api-extractor-temp/doc-models/* ../../../_api-extractor-temp/",
22
+ "build:esnext": "tsc --project ./tsconfig.esnext.json",
23
+ "build:full": "npm run build",
24
+ "build:full:compile": "npm run build:compile",
25
+ "build:genver": "gen-version",
26
+ "build:test": "tsc --project ./src/test/tsconfig.json",
27
+ "ci:build:docs": "api-extractor run --typescript-compiler-folder ../../../node_modules/typescript && copyfiles -u 1 ./_api-extractor-temp/* ../../../_api-extractor-temp/",
28
+ "clean": "rimraf dist lib *.tsbuildinfo *.build.log",
29
+ "eslint": "eslint --format stylish src",
30
+ "eslint:fix": "eslint --format stylish src --fix --fix-type problem,suggestion,layout",
31
+ "lint": "npm run eslint",
32
+ "lint:fix": "npm run eslint:fix",
33
+ "test": "jest",
34
+ "tsc": "tsc",
35
+ "tsfmt": "tsfmt --verify",
36
+ "tsfmt:fix": "tsfmt --replace"
37
+ },
38
+ "dependencies": {
39
+ "@fluidframework/common-definitions": "^0.20.1",
40
+ "@fluidframework/odsp-driver-definitions": "0.58.2000-58133",
41
+ "@fluidframework/telemetry-utils": "0.58.2000-58133",
42
+ "idb": "^6.1.2"
43
+ },
44
+ "devDependencies": {
45
+ "@fluidframework/build-common": "^0.23.0",
46
+ "@fluidframework/eslint-config-fluid": "^0.27.0",
47
+ "@microsoft/api-extractor": "^7.16.1",
48
+ "@rushstack/eslint-config": "^2.5.1",
49
+ "@types/jest": "22.2.3",
50
+ "@types/node": "^14.18.0",
51
+ "@typescript-eslint/eslint-plugin": "~5.9.0",
52
+ "@typescript-eslint/parser": "~5.9.0",
53
+ "concurrently": "^6.2.0",
54
+ "copyfiles": "^2.1.0",
55
+ "eslint": "~8.6.0",
56
+ "eslint-plugin-editorconfig": "~3.2.0",
57
+ "eslint-plugin-eslint-comments": "~3.2.0",
58
+ "eslint-plugin-import": "~2.25.4",
59
+ "eslint-plugin-no-null": "~1.0.2",
60
+ "eslint-plugin-react": "~7.28.0",
61
+ "eslint-plugin-unicorn": "~40.0.0",
62
+ "fake-indexeddb": "3.1.4",
63
+ "jest": "^26.6.3",
64
+ "typescript": "~4.1.3",
65
+ "typescript-formatter": "7.1.0"
66
+ }
67
+ }
@@ -0,0 +1,276 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ IPersistedCache,
8
+ ICacheEntry,
9
+ IFileEntry,
10
+ } from "@fluidframework/odsp-driver-definitions";
11
+ import {
12
+ ITelemetryBaseLogger,
13
+ ITelemetryLogger,
14
+ } from "@fluidframework/common-definitions";
15
+ import { ChildLogger } from "@fluidframework/telemetry-utils";
16
+ import { scheduleIdleTask } from "./scheduleIdleTask";
17
+ import {
18
+ getFluidCacheIndexedDbInstance,
19
+ FluidDriverObjectStoreName,
20
+ getKeyForCacheEntry,
21
+ } from "./FluidCacheIndexedDb";
22
+ import {
23
+ FluidCacheErrorEvent,
24
+ FluidCacheEventSubCategories,
25
+ FluidCacheGenericEvent,
26
+ } from "./fluidCacheTelemetry";
27
+
28
+ // Some browsers have a usageDetails property that will tell you more detailed information
29
+ // on how the storage is being used
30
+ interface StorageQuotaUsageDetails {
31
+ indexedDB: number | undefined;
32
+ }
33
+
34
+ export interface FluidCacheConfig {
35
+ /**
36
+ * A string to specify what partition of the cache you wish to use (e.g. a user id).
37
+ * Null can be used to explicity indicate no partitioning, and has been chosen
38
+ * vs undefined so that it is clear this is an intentional choice by the caller.
39
+ * A null value should only be used when the host can ensure that the cache is not able
40
+ * to be shared with multiple users.
41
+ */
42
+ // eslint-disable-next-line @rushstack/no-new-null
43
+ partitionKey: string | null;
44
+
45
+ /**
46
+ * A logger that can be used to get insight into cache performance and errors
47
+ */
48
+ logger?: ITelemetryBaseLogger;
49
+
50
+ /**
51
+ * A value in milliseconds that determines the maximum age of a cache entry to return.
52
+ * If an entry exists in the cache, but is older than this value, the cached value will not be returned.
53
+ */
54
+ maxCacheItemAge: number;
55
+ }
56
+
57
+ /**
58
+ * A cache that can be used by the Fluid ODSP driver to cache data for faster performance
59
+ */
60
+ export class FluidCache implements IPersistedCache {
61
+ private readonly logger: ITelemetryLogger;
62
+
63
+ private readonly partitionKey: string | null;
64
+
65
+ private readonly maxCacheItemAge: number;
66
+
67
+ constructor(config: FluidCacheConfig) {
68
+ this.logger = ChildLogger.create(config.logger);
69
+ this.partitionKey = config.partitionKey;
70
+ this.maxCacheItemAge = config.maxCacheItemAge;
71
+
72
+ scheduleIdleTask(async () => {
73
+ // Log how much storage space is currently being used by indexed db.
74
+ // NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.
75
+ // Exception added when eslint rule was added, this should be revisited when modifying this code
76
+ if (navigator.storage && navigator.storage.estimate) {
77
+ const estimate = await navigator.storage.estimate();
78
+
79
+ // Some browsers have a usageDetails property that will tell you
80
+ // more detailed information on how the storage is being used
81
+ let indexedDBSize: number | undefined;
82
+ if ("usageDetails" in estimate) {
83
+ indexedDBSize = (
84
+ (estimate as any)
85
+ .usageDetails as StorageQuotaUsageDetails
86
+ ).indexedDB;
87
+ }
88
+
89
+ this.logger.sendTelemetryEvent({
90
+ eventName: FluidCacheGenericEvent.FluidCacheStorageInfo,
91
+ subCategory: FluidCacheEventSubCategories.FluidCache,
92
+ quota: estimate.quota,
93
+ usage: estimate.usage,
94
+ indexedDBSize,
95
+ });
96
+ }
97
+ });
98
+
99
+ scheduleIdleTask(async () => {
100
+ // Delete entries that have not been accessed recently to clean up space
101
+ try {
102
+ const db = await getFluidCacheIndexedDbInstance(this.logger);
103
+
104
+ const transaction = db.transaction(
105
+ FluidDriverObjectStoreName,
106
+ "readwrite",
107
+ );
108
+ const index = transaction.store.index("lastAccessTimeMs");
109
+ // Get items that have not been accessed in 4 weeks
110
+ const keysToDelete = await index.getAllKeys(
111
+ IDBKeyRange.upperBound(
112
+ new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000,
113
+ ),
114
+ );
115
+
116
+ await Promise.all(
117
+ keysToDelete.map((key) => transaction.store.delete(key)),
118
+ );
119
+ await transaction.done;
120
+ } catch (error: any) {
121
+ this.logger.sendErrorEvent(
122
+ {
123
+ eventName:
124
+ FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,
125
+ },
126
+ error,
127
+ );
128
+ }
129
+ });
130
+ }
131
+
132
+ public async removeEntries(file: IFileEntry): Promise<void> {
133
+ try {
134
+ const db = await getFluidCacheIndexedDbInstance(this.logger);
135
+
136
+ const transaction = db.transaction(
137
+ FluidDriverObjectStoreName,
138
+ "readwrite",
139
+ );
140
+ const index = transaction.store.index("fileId");
141
+
142
+ const keysToDelete = await index.getAllKeys(file.docId);
143
+
144
+ await Promise.all(
145
+ keysToDelete.map((key) => transaction.store.delete(key)),
146
+ );
147
+ await transaction.done;
148
+ } catch (error: any) {
149
+ this.logger.sendErrorEvent(
150
+ {
151
+ eventName:
152
+ FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,
153
+ },
154
+ error,
155
+ );
156
+ }
157
+ }
158
+
159
+ public async get(cacheEntry: ICacheEntry): Promise<any> {
160
+ const startTime = performance.now();
161
+
162
+ const cachedItem = await this.getItemFromCache(cacheEntry);
163
+
164
+ this.logger.sendPerformanceEvent({
165
+ eventName: "FluidCacheAccess",
166
+ cacheHit: cachedItem !== undefined,
167
+ type: cacheEntry.type,
168
+ duration: performance.now() - startTime,
169
+ });
170
+
171
+ // Value will contain metadata like the expiry time, we just want to return the object we were asked to cache
172
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
173
+ return cachedItem?.cachedObject;
174
+ }
175
+
176
+ private async getItemFromCache(cacheEntry: ICacheEntry) {
177
+ try {
178
+ const key = getKeyForCacheEntry(cacheEntry);
179
+
180
+ const db = await getFluidCacheIndexedDbInstance(this.logger);
181
+
182
+ const value = await db.get(FluidDriverObjectStoreName, key);
183
+
184
+ if (!value) {
185
+ return undefined;
186
+ }
187
+
188
+ // If the data does not come from the same partition, don't return it
189
+ if (value.partitionKey !== this.partitionKey) {
190
+ this.logger.sendTelemetryEvent({
191
+ eventName:
192
+ FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,
193
+ subCategory: FluidCacheEventSubCategories.FluidCache,
194
+ });
195
+
196
+ return undefined;
197
+ }
198
+
199
+ const currentTime = new Date().getTime();
200
+
201
+ // If too much time has passed since this cache entry was used, we will also return undefined
202
+ if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {
203
+ return undefined;
204
+ }
205
+
206
+ const transaction = db.transaction(
207
+ FluidDriverObjectStoreName,
208
+ "readwrite",
209
+ );
210
+ // We don't want to block the get return of this function on updating the last accessed time
211
+ // We catch this promise because there is no user bad if this is rejected.
212
+ transaction.store
213
+ .get(key)
214
+ .then(async (valueToUpdate) => {
215
+ // This value in the database could have been updated concurrently by other tabs/iframes
216
+ // since we first read it. Only update the last accessed time if the current value in the
217
+ // DB was the same one we returned.
218
+ if (
219
+ valueToUpdate !== undefined &&
220
+ valueToUpdate.createdTimeMs === value.createdTimeMs &&
221
+ (valueToUpdate.lastAccessTimeMs === undefined ||
222
+ valueToUpdate.lastAccessTimeMs < currentTime)
223
+ ) {
224
+ await transaction.store.put(
225
+ { ...valueToUpdate, lastAccessTimeMs: currentTime },
226
+ key,
227
+ );
228
+ }
229
+ await transaction.done;
230
+
231
+ db.close();
232
+ })
233
+ .catch(() => { });
234
+ return value;
235
+ } catch (error: any) {
236
+ // We can fail to open the db for a variety of reasons,
237
+ // such as the database version having upgraded underneath us. Return undefined in this case
238
+ this.logger.sendErrorEvent(
239
+ { eventName: FluidCacheErrorEvent.FluidCacheGetError },
240
+ error,
241
+ );
242
+ return undefined;
243
+ }
244
+ }
245
+
246
+ public async put(entry: ICacheEntry, value: any): Promise<void> {
247
+ try {
248
+ const db = await getFluidCacheIndexedDbInstance(this.logger);
249
+
250
+ const currentTime = new Date().getTime();
251
+
252
+ await db.put(
253
+ FluidDriverObjectStoreName,
254
+ {
255
+ cachedObject: value,
256
+ fileId: entry.file.docId,
257
+ type: entry.type,
258
+ cacheItemId: entry.key,
259
+ partitionKey: this.partitionKey,
260
+ createdTimeMs: currentTime,
261
+ lastAccessTimeMs: currentTime,
262
+ },
263
+ getKeyForCacheEntry(entry),
264
+ );
265
+
266
+ db.close();
267
+ } catch (error: any) {
268
+ // We can fail to open the db for a variety of reasons,
269
+ // such as the database version having upgraded underneath us
270
+ this.logger.sendErrorEvent(
271
+ { eventName: FluidCacheErrorEvent.FluidCachePutError },
272
+ error,
273
+ );
274
+ }
275
+ }
276
+ }
@@ -0,0 +1,155 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { openDB, DBSchema, IDBPDatabase, deleteDB } from "idb";
7
+ import { ICacheEntry } from "@fluidframework/odsp-driver-definitions";
8
+ import { ITelemetryBaseLogger } from "@fluidframework/common-definitions";
9
+ import { ChildLogger } from "@fluidframework/telemetry-utils";
10
+ import { FluidCacheErrorEvent } from "./fluidCacheTelemetry";
11
+
12
+ // The name of the database that we use for caching Fluid info.
13
+ export const FluidDriverCacheDBName = "fluidDriverCache";
14
+
15
+ // The name of the object store within the indexed db instance that the driver will use to cache Fluid content.
16
+ export const FluidDriverObjectStoreName = "driverStorage.V3";
17
+
18
+ export const CurrentCacheVersion = 3;
19
+
20
+ // Note that V1 and V2 were misspelled as "diver", and we need to keep using the misspelling here.
21
+ export const oldVersionNameMapping: Partial<{ [key: number]: string }> = {
22
+ 1: "diverStorage",
23
+ 2: "diverStorage.V2",
24
+ };
25
+
26
+ export function getKeyForCacheEntry(entry: ICacheEntry) {
27
+ return `${entry.file.docId}_${entry.type}_${entry.key}`;
28
+ }
29
+
30
+ export function getFluidCacheIndexedDbInstance(
31
+ logger?: ITelemetryBaseLogger,
32
+ ): Promise<IDBPDatabase<FluidCacheDBSchema>> {
33
+ return new Promise((resolve, reject) => {
34
+ openDB<FluidCacheDBSchema>(
35
+ FluidDriverCacheDBName,
36
+ CurrentCacheVersion,
37
+ {
38
+ upgrade: (db, oldVersion) => {
39
+ try {
40
+ // We changed the format of the object store, so we must
41
+ // delete the old stores to create a new one in V3
42
+ const cacheToDelete = oldVersionNameMapping[oldVersion];
43
+ if (cacheToDelete) {
44
+ // We don't include the old object stores in the schema, so we need to
45
+ // use a typecast here to prevent IDB from complaining
46
+ db.deleteObjectStore(cacheToDelete as any);
47
+ }
48
+ } catch (error: any) {
49
+ // Catch any error done when attempting to delete the older version.
50
+ // If the object does not exist db will throw.
51
+ // We can now assume that the old version is no longer there regardless.
52
+ ChildLogger.create(logger).sendErrorEvent(
53
+ {
54
+ eventName:
55
+ FluidCacheErrorEvent.FluidCacheDeleteOldDbError,
56
+ },
57
+ error,
58
+ );
59
+ }
60
+
61
+ const cacheObjectStore = db.createObjectStore(
62
+ FluidDriverObjectStoreName,
63
+ );
64
+ cacheObjectStore.createIndex(
65
+ "createdTimeMs",
66
+ "createdTimeMs",
67
+ );
68
+ cacheObjectStore.createIndex(
69
+ "lastAccessTimeMs",
70
+ "lastAccessTimeMs",
71
+ );
72
+ cacheObjectStore.createIndex(
73
+ "partitionKey",
74
+ "partitionKey",
75
+ );
76
+ cacheObjectStore.createIndex("fileId", "fileId");
77
+ },
78
+ blocked: () => {
79
+ reject(
80
+ new Error(
81
+ "Could not open DB since it is blocked by an older client that has the DB open",
82
+ ),
83
+ );
84
+ },
85
+ },
86
+ ).then(resolve, reject);
87
+ });
88
+ }
89
+
90
+ // Deletes the indexed DB instance.
91
+ // Warning this can throw an error in Firefox incognito, where accessing storage is prohibited.
92
+ export function deleteFluidCacheIndexDbInstance(): Promise<void> {
93
+ return deleteDB(FluidDriverCacheDBName);
94
+ }
95
+
96
+ /**
97
+ * Schema for the object store used to cache driver information
98
+ */
99
+ export interface FluidCacheDBSchema extends DBSchema {
100
+ [FluidDriverObjectStoreName]: {
101
+ /**
102
+ * A unique identifier for an item in the cache. It is a combination of file, type, and cacheItemId
103
+ */
104
+ key: string;
105
+
106
+ value: {
107
+ /**
108
+ * The identifier of the file associated with the cache entry
109
+ */
110
+ fileId: string;
111
+
112
+ /**
113
+ * Describes the type of content being cached, such as snapshot
114
+ */
115
+ type: string;
116
+
117
+ /**
118
+ * Files may have multiple cached items associated with them,
119
+ * this property uniquely identifies a specific cache entry for a file.
120
+ * This is not globally unique, but rather a unique id for this file
121
+ */
122
+ cacheItemId: string;
123
+
124
+ /*
125
+ * Opaque object that the driver asks us to store in a cache for performance reasons
126
+ */
127
+ cachedObject: any;
128
+
129
+ /**
130
+ * A string to specify what partition of the cache you wish to use (e.g. a user id).
131
+ * Null can be used to explicity indicate no partitioning.
132
+ */
133
+ // eslint-disable-next-line @rushstack/no-new-null
134
+ partitionKey: string | null;
135
+
136
+ /**
137
+ * The time when the cache entry was put into the cache
138
+ */
139
+ createdTimeMs: number;
140
+
141
+ /**
142
+ * The last time the cache entry was used.
143
+ * This is initially set to the time the cache entry was created Measured as ms since unix epoch.
144
+ */
145
+ lastAccessTimeMs: number;
146
+ };
147
+
148
+ indexes: {
149
+ createdTimeMs: number;
150
+ partitionKey: string;
151
+ lastAccessTimeMs: number;
152
+ fileId: string;
153
+ };
154
+ };
155
+ }
@@ -0,0 +1,21 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export const enum FluidCacheGenericEvent {
7
+ "FluidCacheStorageInfo" = "FluidCacheStorageInfo",
8
+ "FluidCachePartitionKeyMismatch" = "FluidCachePartitionKeyMismatch",
9
+ }
10
+
11
+ export const enum FluidCacheErrorEvent {
12
+ "FluidCacheDeleteOldEntriesError" = "FluidCacheDeleteOldEntriesError",
13
+ "FluidCacheGetError" = "FluidCacheGetError",
14
+ "FluidCachePutError" = "FluidCachePutError",
15
+ "FluidCacheUpdateUsageError" = "FluidCacheUpdateUsageError",
16
+ "FluidCacheDeleteOldDbError" = "FluidCacheDeleteOldDbError",
17
+ }
18
+
19
+ export const enum FluidCacheEventSubCategories {
20
+ "FluidCache" = "FluidCache",
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export * from "./FluidCache";
7
+ export { deleteFluidCacheIndexDbInstance } from "./FluidCacheIndexedDb";
@@ -0,0 +1,9 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ *
5
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
6
+ */
7
+
8
+ export const pkgName = "@fluidframework/driver-web-cache";
9
+ export const pkgVersion = "0.58.2000-58133";