|
| 1 | +interface CallbackOneParam<T1, T2 = void> { |
| 2 | + (param1: T1): T2; |
| 3 | +} |
| 4 | + |
| 5 | +export class BrowserKVStorage{ |
| 6 | + |
| 7 | + private _databaseName:string |
| 8 | + private _storageName:string |
| 9 | + private _dbVersion:number |
| 10 | + private _iDB:IDBDatabase |
| 11 | + |
| 12 | + private constructor({ |
| 13 | + databaseName, |
| 14 | + storageName, |
| 15 | + dbVersion, |
| 16 | + iDB |
| 17 | + }:{ |
| 18 | + databaseName:string, |
| 19 | + storageName:string, |
| 20 | + dbVersion:number, |
| 21 | + iDB:IDBDatabase |
| 22 | + }){ |
| 23 | + |
| 24 | + this._databaseName = databaseName |
| 25 | + this._storageName = storageName |
| 26 | + this._dbVersion = 1 |
| 27 | + this._iDB = iDB |
| 28 | + } |
| 29 | + |
| 30 | + private isAlphanumeric(str:string) { |
| 31 | + return /^[a-zA-Z0-9]+$/.test(str); |
| 32 | + } |
| 33 | + |
| 34 | + private showError(msg:string = 'Error'){ |
| 35 | + throw new Error(msg) |
| 36 | + } |
| 37 | + |
| 38 | + public static async init({ |
| 39 | + databaseName = "data", |
| 40 | + storageName, |
| 41 | + }:{ |
| 42 | + databaseName?:string, |
| 43 | + storageName:string |
| 44 | + }): Promise<BrowserKVStorage>{ |
| 45 | + |
| 46 | + function isAlphanumeric(str:string) { |
| 47 | + return /^[a-zA-Z0-9]+$/.test(str); |
| 48 | + } |
| 49 | + |
| 50 | + function showError(msg:string = 'Error'){ |
| 51 | + throw new Error(msg) |
| 52 | + } |
| 53 | + |
| 54 | + function indexedDBStuff () { |
| 55 | + // Check for IndexedDB support |
| 56 | + if (!('indexedDB' in window)) { |
| 57 | + // Can't use IndexedDB |
| 58 | + showError("This browser doesn't support IndexedDB") |
| 59 | + return |
| 60 | + } else { |
| 61 | + // Do IndexedDB stuff here |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Run IndexedDB code |
| 66 | + indexedDBStuff() |
| 67 | + |
| 68 | + const dbVersion = 1 |
| 69 | + |
| 70 | + if(!isAlphanumeric(databaseName))showError('dataDirName must be Alphanumeric') |
| 71 | + if(!isAlphanumeric(storageName))showError('storageName must be Alphanumeric') |
| 72 | + const createdb:Promise<IDBDatabase> = new Promise((resolve) => { |
| 73 | + let request = window.indexedDB.open(databaseName,dbVersion) |
| 74 | + request.onerror = function(){ |
| 75 | + console.error("Error", request.error) |
| 76 | + } |
| 77 | + request.onsuccess = function(){ |
| 78 | + let db = request.result |
| 79 | + resolve(db) |
| 80 | + } |
| 81 | + request.onupgradeneeded = function(event){ |
| 82 | + let db = request.result |
| 83 | + if (!db.objectStoreNames.contains(storageName)) { |
| 84 | + db.createObjectStore(storageName, {keyPath: 'key'}); |
| 85 | + } |
| 86 | + } |
| 87 | + request.onblocked = function(){ |
| 88 | + console.error("Error, conflict") |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + const iDB = await createdb |
| 93 | + |
| 94 | + return new BrowserKVStorage({databaseName,storageName,dbVersion,iDB}) |
| 95 | + } |
| 96 | + |
| 97 | + public async put(key:string,value:string){ |
| 98 | + return new Promise((resolve) => { |
| 99 | + if(!this.isAlphanumeric(key))this.showError('Key must be Alphanumeric') |
| 100 | + let transaction = this._iDB.transaction(this._storageName, "readwrite") |
| 101 | + let request = transaction.objectStore(this._storageName).put({key: key, value: value}) |
| 102 | + request.onsuccess = function() { |
| 103 | + resolve(true) |
| 104 | + } |
| 105 | + request.onerror = function() { |
| 106 | + resolve(false) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + public async get(key:string){ |
| 112 | + return new Promise((resolve) => { |
| 113 | + if(!this.isAlphanumeric(key))this.showError('Key must be Alphanumeric') |
| 114 | + let transaction = this._iDB.transaction(this._storageName, "readonly") |
| 115 | + let request = transaction.objectStore(this._storageName).get(key) |
| 116 | + request.onsuccess = function(event) { |
| 117 | + if(request.result != undefined){resolve(request.result.value)}else{resolve(false)} |
| 118 | + } |
| 119 | + request.onerror = function() { |
| 120 | + resolve(false) |
| 121 | + } |
| 122 | + |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + public async delete(key:string){ |
| 127 | + return new Promise((resolve) => { |
| 128 | + if(!this.isAlphanumeric(key))this.showError('Key must be Alphanumeric') |
| 129 | + let transaction = this._iDB.transaction(this._storageName, "readwrite") |
| 130 | + let request = transaction.objectStore(this._storageName).delete(key) |
| 131 | + request.onsuccess = function(event) { |
| 132 | + resolve(true) |
| 133 | + } |
| 134 | + request.onerror = function() { |
| 135 | + resolve(false) |
| 136 | + } |
| 137 | + |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + public async has(key:string){ |
| 142 | + return new Promise((resolve) => { |
| 143 | + if(!this.isAlphanumeric(key))this.showError('Key must be Alphanumeric') |
| 144 | + let transaction = this._iDB.transaction(this._storageName, "readonly") |
| 145 | + let request = transaction.objectStore(this._storageName).get(key) |
| 146 | + request.onsuccess = function(event) { |
| 147 | + if(request.result != undefined){resolve(true)}else{resolve(false)} |
| 148 | + } |
| 149 | + request.onerror = function() { |
| 150 | + resolve(false) |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + public async list(){ |
| 156 | + return new Promise((resolve) => { |
| 157 | + let transaction = this._iDB.transaction(this._storageName, "readonly") |
| 158 | + let request = transaction.objectStore(this._storageName).getAllKeys() |
| 159 | + request.onsuccess = function(event) { |
| 160 | + let result = { |
| 161 | + keys:request.result, |
| 162 | + complete:true |
| 163 | + } |
| 164 | + resolve(result) |
| 165 | + } |
| 166 | + request.onerror = function() { |
| 167 | + resolve(false) |
| 168 | + } |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
0 commit comments