@pluv/addon-indexeddb 0.0.0-canary-20261027022227
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/LICENSE +21 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +86 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -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.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ClientDefs, PluvRoom, PluvRoomAddon } from "@pluv/client";
|
|
2
|
+
interface AddonIndexedDBConfig<TDefs extends ClientDefs = any> {
|
|
3
|
+
enabled?: boolean | ((room: PluvRoom<TDefs>) => boolean);
|
|
4
|
+
}
|
|
5
|
+
export declare const addonIndexedDB: <TDefs extends ClientDefs = any>(config?: AddonIndexedDBConfig<TDefs>) => PluvRoomAddon<any>;
|
|
6
|
+
export {};
|
|
7
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/addonIndexedDB.ts"],"mappings":";UAGiB,qBAAqB,cAAc;EAChD,sBAAsB,MAAM,SAAS;;qBAG5B,iBAAkB,cAAc,kBAAgB,SAChD,qBAAqB,WAC/B"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { AbstractStorageStore } from "@pluv/client";
|
|
2
|
+
import { openDB } from "idb";
|
|
3
|
+
// src/IndexedDBStorage.ts
|
|
4
|
+
const STORAGE_STORE_FLATTEN_THRESHOLD = 500;
|
|
5
|
+
const UPGRADES_KEY = "__PLUV_UPDATES";
|
|
6
|
+
var IndexedDBStorage = class extends AbstractStorageStore {
|
|
7
|
+
constructor(room) {
|
|
8
|
+
super(room);
|
|
9
|
+
this._db = null;
|
|
10
|
+
this._dbRef = 0;
|
|
11
|
+
this._dbSize = 0;
|
|
12
|
+
this.room = room;
|
|
13
|
+
}
|
|
14
|
+
async addUpdate(update) {
|
|
15
|
+
const db = this._db;
|
|
16
|
+
if (!db) return;
|
|
17
|
+
await db.add(UPGRADES_KEY, update);
|
|
18
|
+
this._dbSize += 1;
|
|
19
|
+
}
|
|
20
|
+
destroy() {
|
|
21
|
+
if (!this._db) return Promise.resolve();
|
|
22
|
+
this._db.close();
|
|
23
|
+
this._db = null;
|
|
24
|
+
this._dbRef = 0;
|
|
25
|
+
this._dbSize = 0;
|
|
26
|
+
return Promise.resolve();
|
|
27
|
+
}
|
|
28
|
+
async flatten(encodedState) {
|
|
29
|
+
const db = this._db;
|
|
30
|
+
if (!db) return;
|
|
31
|
+
await db.add(UPGRADES_KEY, encodedState);
|
|
32
|
+
await db.delete(UPGRADES_KEY, this._getUpperBound(this._dbRef, true));
|
|
33
|
+
this._dbSize = await this._getSize();
|
|
34
|
+
}
|
|
35
|
+
async getShouldFlatten() {
|
|
36
|
+
return await this._getSize() >= STORAGE_STORE_FLATTEN_THRESHOLD;
|
|
37
|
+
}
|
|
38
|
+
async getUpdates(start = 0) {
|
|
39
|
+
const db = this._db;
|
|
40
|
+
if (!db) return [];
|
|
41
|
+
const updates = await db.getAll(UPGRADES_KEY, this._getLowerBound(start));
|
|
42
|
+
const lastKey = await this._getLastKey();
|
|
43
|
+
this._dbRef = lastKey + 1;
|
|
44
|
+
return updates;
|
|
45
|
+
}
|
|
46
|
+
async initialize() {
|
|
47
|
+
const newDB = await openDB(this.room, void 0, { upgrade: (db) => {
|
|
48
|
+
db.createObjectStore(UPGRADES_KEY, { autoIncrement: true });
|
|
49
|
+
} });
|
|
50
|
+
newDB.addEventListener("versionchange", () => {
|
|
51
|
+
newDB.close();
|
|
52
|
+
});
|
|
53
|
+
if (typeof window !== "undefined") window.addEventListener("unload", () => {
|
|
54
|
+
newDB.close();
|
|
55
|
+
});
|
|
56
|
+
this._db = newDB;
|
|
57
|
+
}
|
|
58
|
+
async _getLastKey() {
|
|
59
|
+
const db = this._db;
|
|
60
|
+
if (!db) throw new Error("IndexedDB not initialized");
|
|
61
|
+
return (await db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY).openCursor(null, "prev"))?.key ?? -1;
|
|
62
|
+
}
|
|
63
|
+
_getLowerBound(lower, open = false) {
|
|
64
|
+
return IDBKeyRange.lowerBound(lower, open);
|
|
65
|
+
}
|
|
66
|
+
async _getSize() {
|
|
67
|
+
if (this._dbSize) return this._dbSize;
|
|
68
|
+
const db = this._db;
|
|
69
|
+
if (!db) throw new Error("IndexedDB not initialized");
|
|
70
|
+
return db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY).count();
|
|
71
|
+
}
|
|
72
|
+
_getUpperBound(upper, open = false) {
|
|
73
|
+
return IDBKeyRange.upperBound(upper, open);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
// src/addonIndexedDB.ts
|
|
77
|
+
const addonIndexedDB = (config) => {
|
|
78
|
+
const { enabled = true } = config ?? {};
|
|
79
|
+
return ({ room }) => {
|
|
80
|
+
if (!(typeof enabled === "boolean" ? enabled : enabled(room))) return {};
|
|
81
|
+
return { storage: new IndexedDBStorage(room.id) };
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export { addonIndexedDB };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/IndexedDBStorage.ts","../src/addonIndexedDB.ts"],"sourcesContent":["import { AbstractStorageStore } from \"@pluv/client\";\nimport type { IDBPDatabase } from \"idb\";\nimport { openDB } from \"idb\";\n\nconst STORAGE_STORE_FLATTEN_THRESHOLD = 500;\nconst UPGRADES_KEY = \"__PLUV_UPDATES\";\n\ntype IndexDBSchema = {\n [UPGRADES_KEY]: {\n key: number;\n value: string;\n };\n};\n\ntype IndexedDB = IDBPDatabase<IndexDBSchema>;\n\nexport class IndexedDBStorage extends AbstractStorageStore {\n public room: string;\n\n private _db: IndexedDB | null = null;\n private _dbRef: number = 0;\n private _dbSize: number = 0;\n\n constructor(room: string) {\n super(room);\n\n this.room = room;\n }\n\n public async addUpdate(update: string): Promise<void> {\n const db = this._db;\n\n if (!db) return;\n\n await db.add(UPGRADES_KEY, update);\n\n this._dbSize += 1;\n }\n\n public destroy(): Promise<void> {\n if (!this._db) return Promise.resolve();\n\n this._db.close();\n this._db = null;\n this._dbRef = 0;\n this._dbSize = 0;\n\n return Promise.resolve();\n }\n\n public async flatten(encodedState: string): Promise<void> {\n const db = this._db;\n\n if (!db) return;\n\n await db.add(UPGRADES_KEY, encodedState);\n await db.delete(UPGRADES_KEY, this._getUpperBound(this._dbRef, true));\n\n this._dbSize = await this._getSize();\n }\n\n public async getShouldFlatten(): Promise<boolean> {\n const dbSize = await this._getSize();\n\n return dbSize >= STORAGE_STORE_FLATTEN_THRESHOLD;\n }\n\n public async getUpdates(start: number = 0): Promise<readonly string[]> {\n const db = this._db;\n\n if (!db) return [];\n\n const updates = await db.getAll(UPGRADES_KEY, this._getLowerBound(start));\n\n const lastKey = await this._getLastKey();\n\n this._dbRef = lastKey + 1;\n\n return updates;\n }\n\n public async initialize(): Promise<void> {\n const newDB = await openDB<IndexDBSchema>(this.room, undefined, {\n upgrade: (db) => {\n db.createObjectStore(UPGRADES_KEY, { autoIncrement: true });\n },\n });\n\n newDB.addEventListener(\"versionchange\", () => {\n newDB.close();\n });\n\n if (typeof window !== \"undefined\") {\n window.addEventListener(\"unload\", () => {\n newDB.close();\n });\n }\n\n this._db = newDB;\n }\n\n private async _getLastKey(): Promise<number> {\n const db = this._db;\n\n if (!db) throw new Error(\"IndexedDB not initialized\");\n\n const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);\n const cursor = await store.openCursor(null, \"prev\");\n\n return cursor?.key ?? -1;\n }\n\n private _getLowerBound(lower: number, open: boolean = false): IDBKeyRange {\n return IDBKeyRange.lowerBound(lower, open);\n }\n\n private async _getSize(): Promise<number> {\n if (this._dbSize) return this._dbSize;\n\n const db = this._db;\n\n if (!db) throw new Error(\"IndexedDB not initialized\");\n\n const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);\n\n return store.count();\n }\n\n private _getUpperBound(upper: number, open: boolean = false): IDBKeyRange {\n return IDBKeyRange.upperBound(upper, open);\n }\n}\n","import type { ClientDefs, PluvRoom, PluvRoomAddon } from \"@pluv/client\";\nimport { IndexedDBStorage } from \"./IndexedDBStorage\";\n\nexport interface AddonIndexedDBConfig<TDefs extends ClientDefs = any> {\n enabled?: boolean | ((room: PluvRoom<TDefs>) => boolean);\n}\n\nexport const addonIndexedDB = <TDefs extends ClientDefs = any>(\n config?: AddonIndexedDBConfig<TDefs>,\n): PluvRoomAddon<any> => {\n const { enabled = true } = config ?? {};\n\n return ({ room }) => {\n const _enabled = typeof enabled === \"boolean\" ? enabled : enabled(room);\n\n if (!_enabled) return {};\n\n return {\n storage: new IndexedDBStorage(room.id),\n };\n };\n};\n"],"mappings":";;;AAIA,MAAM,kCAAkC;AACxC,MAAM,eAAe;AAWrB,IAAa,mBAAb,cAAsC,qBAAqB;CAOvD,YAAY,MAAc;EACtB,MAAM,IAAI;EALkB,KAAA,MAAA;EACP,KAAA,SAAA;EACC,KAAA,UAAA;EAKtB,KAAK,OAAO;CAChB;CAEA,MAAa,UAAU,QAA+B;EAClD,MAAM,KAAK,KAAK;EAEhB,IAAI,CAAC,IAAI;EAET,MAAM,GAAG,IAAI,cAAc,MAAM;EAEjC,KAAK,WAAW;CACpB;CAEA,UAAgC;EAC5B,IAAI,CAAC,KAAK,KAAK,OAAO,QAAQ,QAAQ;EAEtC,KAAK,IAAI,MAAM;EACf,KAAK,MAAM;EACX,KAAK,SAAS;EACd,KAAK,UAAU;EAEf,OAAO,QAAQ,QAAQ;CAC3B;CAEA,MAAa,QAAQ,cAAqC;EACtD,MAAM,KAAK,KAAK;EAEhB,IAAI,CAAC,IAAI;EAET,MAAM,GAAG,IAAI,cAAc,YAAY;EACvC,MAAM,GAAG,OAAO,cAAc,KAAK,eAAe,KAAK,QAAQ,IAAI,CAAC;EAEpE,KAAK,UAAU,MAAM,KAAK,SAAS;CACvC;CAEA,MAAa,mBAAqC;EAG9C,OAAO,MAFc,KAAK,SAAS,KAElB;CACrB;CAEA,MAAa,WAAW,QAAgB,GAA+B;EACnE,MAAM,KAAK,KAAK;EAEhB,IAAI,CAAC,IAAI,OAAO,CAAC;EAEjB,MAAM,UAAU,MAAM,GAAG,OAAO,cAAc,KAAK,eAAe,KAAK,CAAC;EAExE,MAAM,UAAU,MAAM,K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0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/addon-indexeddb",
|
|
3
|
+
"version": "0.0.0-canary-20261027022227",
|
|
4
|
+
"description": "@pluv/io indexeddb for client storage persistence",
|
|
5
|
+
"author": {
|
|
6
|
+
"email": "david@pluv.io",
|
|
7
|
+
"name": "i3dly",
|
|
8
|
+
"url": "https://github.com/i3dly"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"homepage": "https://github.com/pluv-io/pluv",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/pluv-io/pluv.git",
|
|
15
|
+
"directory": "packages/addon-indexeddb"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@pluv/client": "^0.0.0-canary-20261027022227",
|
|
27
|
+
"@pluv/crdt": "^0.0.0-canary-20261027022227",
|
|
28
|
+
"idb": "^8.0.4"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@pluv/tsconfig": "^0.0.0-canary-20261027022227",
|
|
32
|
+
"@pluv/types": "^0.0.0-canary-20261027022227",
|
|
33
|
+
"tsdown": "^0.23.2",
|
|
34
|
+
"typescript": "7.1.0-beta"
|
|
35
|
+
},
|
|
36
|
+
"exports": {
|
|
37
|
+
".": "./dist/index.mjs",
|
|
38
|
+
"./*": "./*"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"dev": "tsdown --watch",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
45
|
+
}
|
|
46
|
+
}
|