@pluv/addon-indexeddb 0.1.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.
- package/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +17 -0
- package/CHANGELOG.md +50 -0
- package/LICENSE +21 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +171 -0
- package/dist/index.mjs +145 -0
- package/package.json +37 -0
- package/src/IndexedDBStorage.ts +134 -0
- package/src/addonIndexedDB.ts +31 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +5 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/addon-indexeddb@0.1.0 build /home/runner/work/pluv/pluv/packages/addon-indexeddb
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v7.0.0
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[34mCJS[39m Build start
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m3.84 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 147ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m4.87 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 148ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 8280ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m728.00 B[39m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @pluv/addon-indexeddb
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 829d31b: Added support for defining persistant frontend storage for rooms via a new `addons` option on rooms.
|
|
8
|
+
|
|
9
|
+
This also introduces the first new addon `@pluv/addon-indexeddb`, which is more-or-less the equivalent to `y-indexeddb` which you can install like so:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install @pluv/addon-indexeddb
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
To use this new addon, simply pass it to options when creating a room:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { addonIndexedDB } from "@pluv/addon-indexeddb";
|
|
19
|
+
import { createClient } from "@pluv/client";
|
|
20
|
+
|
|
21
|
+
const client = createClient({
|
|
22
|
+
// ...
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const room = client.createRoom("my-new-room", {
|
|
26
|
+
addons: [
|
|
27
|
+
// Define your addons in an array like so
|
|
28
|
+
addonIndexedDB(),
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or when using `@pluv/react`:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const PluvRoom = createRoomBundle({
|
|
37
|
+
// ...
|
|
38
|
+
addons: [
|
|
39
|
+
// Define your addons in an array like so
|
|
40
|
+
addonIndexedDB(),
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Patch Changes
|
|
46
|
+
|
|
47
|
+
- 8d11672: bumped dependencies to latest
|
|
48
|
+
- Updated dependencies [8d11672]
|
|
49
|
+
- Updated dependencies [829d31b]
|
|
50
|
+
- @pluv/client@0.9.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Shoubhit Dash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as _pluv_types from '@pluv/types';
|
|
2
|
+
import { IOLike, JsonObject } from '@pluv/types';
|
|
3
|
+
import { AbstractType, PluvRoomAddon, PluvRoom } from '@pluv/client';
|
|
4
|
+
|
|
5
|
+
interface AddonIndexedDBConfig<TIO extends IOLike, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}> {
|
|
6
|
+
enabled?: boolean | ((room: PluvRoom<TIO, TPresence, TStorage>) => boolean);
|
|
7
|
+
}
|
|
8
|
+
declare const addonIndexedDB: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any>, {}, {}, {}, {}>, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}>(config?: AddonIndexedDBConfig<TIO, TPresence, TStorage> | undefined) => PluvRoomAddon<TIO, TPresence, TStorage>;
|
|
9
|
+
|
|
10
|
+
export { addonIndexedDB };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/index.ts
|
|
41
|
+
var src_exports = {};
|
|
42
|
+
__export(src_exports, {
|
|
43
|
+
addonIndexedDB: () => addonIndexedDB
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
|
|
47
|
+
// src/IndexedDBStorage.ts
|
|
48
|
+
var import_client = require("@pluv/client");
|
|
49
|
+
var import_idb = require("idb");
|
|
50
|
+
var STORAGE_STORE_FLATTEN_THRESHOLD = 500;
|
|
51
|
+
var UPGRADES_KEY = "__PLUV_UPDATES";
|
|
52
|
+
var IndexedDBStorage = class extends import_client.AbstractStorageStore {
|
|
53
|
+
constructor(room) {
|
|
54
|
+
super(room);
|
|
55
|
+
this._db = null;
|
|
56
|
+
this._dbRef = 0;
|
|
57
|
+
this._dbSize = 0;
|
|
58
|
+
this.room = room;
|
|
59
|
+
}
|
|
60
|
+
addUpdate(update) {
|
|
61
|
+
return __async(this, null, function* () {
|
|
62
|
+
const db = this._db;
|
|
63
|
+
if (!db)
|
|
64
|
+
return;
|
|
65
|
+
yield db.add(UPGRADES_KEY, update);
|
|
66
|
+
this._dbSize += 1;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
destroy() {
|
|
70
|
+
if (!this._db)
|
|
71
|
+
return Promise.resolve();
|
|
72
|
+
this._db.close();
|
|
73
|
+
this._db = null;
|
|
74
|
+
this._dbRef = 0;
|
|
75
|
+
this._dbSize = 0;
|
|
76
|
+
return Promise.resolve();
|
|
77
|
+
}
|
|
78
|
+
flatten(encodedState) {
|
|
79
|
+
return __async(this, null, function* () {
|
|
80
|
+
const db = this._db;
|
|
81
|
+
if (!db)
|
|
82
|
+
return;
|
|
83
|
+
yield db.add(UPGRADES_KEY, encodedState);
|
|
84
|
+
yield db.delete(UPGRADES_KEY, this._getUpperBound(this._dbRef, true));
|
|
85
|
+
this._dbSize = yield this._getSize();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
getShouldFlatten() {
|
|
89
|
+
return __async(this, null, function* () {
|
|
90
|
+
const dbSize = yield this._getSize();
|
|
91
|
+
return dbSize >= STORAGE_STORE_FLATTEN_THRESHOLD;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
getUpdates(start = 0) {
|
|
95
|
+
return __async(this, null, function* () {
|
|
96
|
+
const db = this._db;
|
|
97
|
+
if (!db)
|
|
98
|
+
return [];
|
|
99
|
+
const updates = yield db.getAll(
|
|
100
|
+
UPGRADES_KEY,
|
|
101
|
+
this._getLowerBound(start)
|
|
102
|
+
);
|
|
103
|
+
const lastKey = yield this._getLastKey();
|
|
104
|
+
this._dbRef = lastKey + 1;
|
|
105
|
+
return updates;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
initialize() {
|
|
109
|
+
return __async(this, null, function* () {
|
|
110
|
+
const newDB = yield (0, import_idb.openDB)(this.room, void 0, {
|
|
111
|
+
upgrade: (db) => {
|
|
112
|
+
db.createObjectStore(UPGRADES_KEY, { autoIncrement: true });
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
newDB.addEventListener("versionchange", () => {
|
|
116
|
+
newDB.close();
|
|
117
|
+
});
|
|
118
|
+
if (typeof window !== "undefined") {
|
|
119
|
+
window.addEventListener("unload", () => {
|
|
120
|
+
newDB.close();
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
this._db = newDB;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
_getLastKey() {
|
|
127
|
+
return __async(this, null, function* () {
|
|
128
|
+
var _a;
|
|
129
|
+
const db = this._db;
|
|
130
|
+
if (!db)
|
|
131
|
+
throw new Error("IndexedDB not initialized");
|
|
132
|
+
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
133
|
+
const cursor = yield store.openCursor(null, "prev");
|
|
134
|
+
return (_a = cursor == null ? void 0 : cursor.key) != null ? _a : -1;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
_getLowerBound(lower, open = false) {
|
|
138
|
+
return IDBKeyRange.lowerBound(lower, open);
|
|
139
|
+
}
|
|
140
|
+
_getSize() {
|
|
141
|
+
return __async(this, null, function* () {
|
|
142
|
+
if (this._dbSize)
|
|
143
|
+
return this._dbSize;
|
|
144
|
+
const db = this._db;
|
|
145
|
+
if (!db)
|
|
146
|
+
throw new Error("IndexedDB not initialized");
|
|
147
|
+
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
148
|
+
return store.count();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
_getUpperBound(upper, open = false) {
|
|
152
|
+
return IDBKeyRange.upperBound(upper, open);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/addonIndexedDB.ts
|
|
157
|
+
var addonIndexedDB = (config) => {
|
|
158
|
+
const { enabled = true } = config != null ? config : {};
|
|
159
|
+
return ({ room }) => {
|
|
160
|
+
const _enabled = typeof enabled === "boolean" ? enabled : enabled(room);
|
|
161
|
+
if (!_enabled)
|
|
162
|
+
return {};
|
|
163
|
+
return {
|
|
164
|
+
storage: new IndexedDBStorage(room.id)
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
169
|
+
0 && (module.exports = {
|
|
170
|
+
addonIndexedDB
|
|
171
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/IndexedDBStorage.ts
|
|
23
|
+
import { AbstractStorageStore } from "@pluv/client";
|
|
24
|
+
import { openDB } from "idb";
|
|
25
|
+
var STORAGE_STORE_FLATTEN_THRESHOLD = 500;
|
|
26
|
+
var UPGRADES_KEY = "__PLUV_UPDATES";
|
|
27
|
+
var IndexedDBStorage = class extends AbstractStorageStore {
|
|
28
|
+
constructor(room) {
|
|
29
|
+
super(room);
|
|
30
|
+
this._db = null;
|
|
31
|
+
this._dbRef = 0;
|
|
32
|
+
this._dbSize = 0;
|
|
33
|
+
this.room = room;
|
|
34
|
+
}
|
|
35
|
+
addUpdate(update) {
|
|
36
|
+
return __async(this, null, function* () {
|
|
37
|
+
const db = this._db;
|
|
38
|
+
if (!db)
|
|
39
|
+
return;
|
|
40
|
+
yield db.add(UPGRADES_KEY, update);
|
|
41
|
+
this._dbSize += 1;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
destroy() {
|
|
45
|
+
if (!this._db)
|
|
46
|
+
return Promise.resolve();
|
|
47
|
+
this._db.close();
|
|
48
|
+
this._db = null;
|
|
49
|
+
this._dbRef = 0;
|
|
50
|
+
this._dbSize = 0;
|
|
51
|
+
return Promise.resolve();
|
|
52
|
+
}
|
|
53
|
+
flatten(encodedState) {
|
|
54
|
+
return __async(this, null, function* () {
|
|
55
|
+
const db = this._db;
|
|
56
|
+
if (!db)
|
|
57
|
+
return;
|
|
58
|
+
yield db.add(UPGRADES_KEY, encodedState);
|
|
59
|
+
yield db.delete(UPGRADES_KEY, this._getUpperBound(this._dbRef, true));
|
|
60
|
+
this._dbSize = yield this._getSize();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
getShouldFlatten() {
|
|
64
|
+
return __async(this, null, function* () {
|
|
65
|
+
const dbSize = yield this._getSize();
|
|
66
|
+
return dbSize >= STORAGE_STORE_FLATTEN_THRESHOLD;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
getUpdates(start = 0) {
|
|
70
|
+
return __async(this, null, function* () {
|
|
71
|
+
const db = this._db;
|
|
72
|
+
if (!db)
|
|
73
|
+
return [];
|
|
74
|
+
const updates = yield db.getAll(
|
|
75
|
+
UPGRADES_KEY,
|
|
76
|
+
this._getLowerBound(start)
|
|
77
|
+
);
|
|
78
|
+
const lastKey = yield this._getLastKey();
|
|
79
|
+
this._dbRef = lastKey + 1;
|
|
80
|
+
return updates;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
initialize() {
|
|
84
|
+
return __async(this, null, function* () {
|
|
85
|
+
const newDB = yield openDB(this.room, void 0, {
|
|
86
|
+
upgrade: (db) => {
|
|
87
|
+
db.createObjectStore(UPGRADES_KEY, { autoIncrement: true });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
newDB.addEventListener("versionchange", () => {
|
|
91
|
+
newDB.close();
|
|
92
|
+
});
|
|
93
|
+
if (typeof window !== "undefined") {
|
|
94
|
+
window.addEventListener("unload", () => {
|
|
95
|
+
newDB.close();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
this._db = newDB;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
_getLastKey() {
|
|
102
|
+
return __async(this, null, function* () {
|
|
103
|
+
var _a;
|
|
104
|
+
const db = this._db;
|
|
105
|
+
if (!db)
|
|
106
|
+
throw new Error("IndexedDB not initialized");
|
|
107
|
+
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
108
|
+
const cursor = yield store.openCursor(null, "prev");
|
|
109
|
+
return (_a = cursor == null ? void 0 : cursor.key) != null ? _a : -1;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
_getLowerBound(lower, open = false) {
|
|
113
|
+
return IDBKeyRange.lowerBound(lower, open);
|
|
114
|
+
}
|
|
115
|
+
_getSize() {
|
|
116
|
+
return __async(this, null, function* () {
|
|
117
|
+
if (this._dbSize)
|
|
118
|
+
return this._dbSize;
|
|
119
|
+
const db = this._db;
|
|
120
|
+
if (!db)
|
|
121
|
+
throw new Error("IndexedDB not initialized");
|
|
122
|
+
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
123
|
+
return store.count();
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
_getUpperBound(upper, open = false) {
|
|
127
|
+
return IDBKeyRange.upperBound(upper, open);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// src/addonIndexedDB.ts
|
|
132
|
+
var addonIndexedDB = (config) => {
|
|
133
|
+
const { enabled = true } = config != null ? config : {};
|
|
134
|
+
return ({ room }) => {
|
|
135
|
+
const _enabled = typeof enabled === "boolean" ? enabled : enabled(room);
|
|
136
|
+
if (!_enabled)
|
|
137
|
+
return {};
|
|
138
|
+
return {
|
|
139
|
+
storage: new IndexedDBStorage(room.id)
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
export {
|
|
144
|
+
addonIndexedDB
|
|
145
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/addon-indexeddb",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@pluv/io indexeddb for client storage persistance",
|
|
5
|
+
"author": "leedavidcs",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/pluv-io/pluv",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/pluv-io/pluv.git",
|
|
11
|
+
"directory": "packages/addon-indexeddb"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"idb": "^7.1.1",
|
|
21
|
+
"@pluv/client": "^0.9.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"eslint": "^8.43.0",
|
|
25
|
+
"tsup": "^7.0.0",
|
|
26
|
+
"typescript": "^5.1.3",
|
|
27
|
+
"@pluv/tsconfig": "^0.1.7",
|
|
28
|
+
"@pluv/types": "^0.2.1",
|
|
29
|
+
"eslint-config-pluv": "^0.1.13"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
33
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
34
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
35
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { AbstractStorageStore } from "@pluv/client";
|
|
2
|
+
import { IDBPDatabase, openDB } from "idb";
|
|
3
|
+
|
|
4
|
+
const STORAGE_STORE_FLATTEN_THRESHOLD = 500;
|
|
5
|
+
const UPGRADES_KEY = "__PLUV_UPDATES";
|
|
6
|
+
|
|
7
|
+
type IndexDBSchema = {
|
|
8
|
+
[UPGRADES_KEY]: {
|
|
9
|
+
key: number;
|
|
10
|
+
value: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type IndexedDB = IDBPDatabase<IndexDBSchema>;
|
|
15
|
+
|
|
16
|
+
export class IndexedDBStorage extends AbstractStorageStore {
|
|
17
|
+
public room: string;
|
|
18
|
+
|
|
19
|
+
private _db: IndexedDB | null = null;
|
|
20
|
+
private _dbRef: number = 0;
|
|
21
|
+
private _dbSize: number = 0;
|
|
22
|
+
|
|
23
|
+
constructor(room: string) {
|
|
24
|
+
super(room);
|
|
25
|
+
|
|
26
|
+
this.room = room;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public async addUpdate(update: string): Promise<void> {
|
|
30
|
+
const db = this._db;
|
|
31
|
+
|
|
32
|
+
if (!db) return;
|
|
33
|
+
|
|
34
|
+
await db.add(UPGRADES_KEY, update);
|
|
35
|
+
|
|
36
|
+
this._dbSize += 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public destroy(): Promise<void> {
|
|
40
|
+
if (!this._db) return Promise.resolve();
|
|
41
|
+
|
|
42
|
+
this._db.close();
|
|
43
|
+
this._db = null;
|
|
44
|
+
this._dbRef = 0;
|
|
45
|
+
this._dbSize = 0;
|
|
46
|
+
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async flatten(encodedState: string): Promise<void> {
|
|
51
|
+
const db = this._db;
|
|
52
|
+
|
|
53
|
+
if (!db) return;
|
|
54
|
+
|
|
55
|
+
await db.add(UPGRADES_KEY, encodedState);
|
|
56
|
+
await db.delete(UPGRADES_KEY, this._getUpperBound(this._dbRef, true));
|
|
57
|
+
|
|
58
|
+
this._dbSize = await this._getSize();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async getShouldFlatten(): Promise<boolean> {
|
|
62
|
+
const dbSize = await this._getSize();
|
|
63
|
+
|
|
64
|
+
return dbSize >= STORAGE_STORE_FLATTEN_THRESHOLD;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public async getUpdates(start: number = 0): Promise<readonly string[]> {
|
|
68
|
+
const db = this._db;
|
|
69
|
+
|
|
70
|
+
if (!db) return [];
|
|
71
|
+
|
|
72
|
+
const updates = await db.getAll(
|
|
73
|
+
UPGRADES_KEY,
|
|
74
|
+
this._getLowerBound(start)
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const lastKey = await this._getLastKey();
|
|
78
|
+
|
|
79
|
+
this._dbRef = lastKey + 1;
|
|
80
|
+
|
|
81
|
+
return updates;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public async initialize(): Promise<void> {
|
|
85
|
+
const newDB = await openDB<IndexDBSchema>(this.room, undefined, {
|
|
86
|
+
upgrade: (db) => {
|
|
87
|
+
db.createObjectStore(UPGRADES_KEY, { autoIncrement: true });
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
newDB.addEventListener("versionchange", () => {
|
|
92
|
+
newDB.close();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (typeof window !== "undefined") {
|
|
96
|
+
window.addEventListener("unload", () => {
|
|
97
|
+
newDB.close();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this._db = newDB;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private async _getLastKey(): Promise<number> {
|
|
105
|
+
const db = this._db;
|
|
106
|
+
|
|
107
|
+
if (!db) throw new Error("IndexedDB not initialized");
|
|
108
|
+
|
|
109
|
+
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
110
|
+
const cursor = await store.openCursor(null, "prev");
|
|
111
|
+
|
|
112
|
+
return cursor?.key ?? -1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private _getLowerBound(lower: number, open: boolean = false): IDBKeyRange {
|
|
116
|
+
return IDBKeyRange.lowerBound(lower, open);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private async _getSize(): Promise<number> {
|
|
120
|
+
if (this._dbSize) return this._dbSize;
|
|
121
|
+
|
|
122
|
+
const db = this._db;
|
|
123
|
+
|
|
124
|
+
if (!db) throw new Error("IndexedDB not initialized");
|
|
125
|
+
|
|
126
|
+
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
127
|
+
|
|
128
|
+
return store.count();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private _getUpperBound(upper: number, open: boolean = false): IDBKeyRange {
|
|
132
|
+
return IDBKeyRange.upperBound(upper, open);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AbstractType, PluvRoom, PluvRoomAddon } from "@pluv/client";
|
|
2
|
+
import type { IOLike, JsonObject } from "@pluv/types";
|
|
3
|
+
import { IndexedDBStorage } from "./IndexedDBStorage";
|
|
4
|
+
|
|
5
|
+
export interface AddonIndexedDBConfig<
|
|
6
|
+
TIO extends IOLike,
|
|
7
|
+
TPresence extends JsonObject = {},
|
|
8
|
+
TStorage extends Record<string, AbstractType<any>> = {}
|
|
9
|
+
> {
|
|
10
|
+
enabled?: boolean | ((room: PluvRoom<TIO, TPresence, TStorage>) => boolean);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const addonIndexedDB = <
|
|
14
|
+
TIO extends IOLike,
|
|
15
|
+
TPresence extends JsonObject = {},
|
|
16
|
+
TStorage extends Record<string, AbstractType<any>> = {}
|
|
17
|
+
>(
|
|
18
|
+
config?: AddonIndexedDBConfig<TIO, TPresence, TStorage>
|
|
19
|
+
): PluvRoomAddon<TIO, TPresence, TStorage> => {
|
|
20
|
+
const { enabled = true } = config ?? {};
|
|
21
|
+
|
|
22
|
+
return ({ room }) => {
|
|
23
|
+
const _enabled = typeof enabled === "boolean" ? enabled : enabled(room);
|
|
24
|
+
|
|
25
|
+
if (!_enabled) return {};
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
storage: new IndexedDBStorage(room.id),
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { addonIndexedDB } from "./addonIndexedDB";
|