|
| 1 | +/* eslint @typescript-eslint/no-unused-vars: 0 */ |
| 2 | + |
| 3 | +export interface Cache { |
| 4 | + put: (key: string, data: string | ArrayBuffer) => Promise<void>; |
| 5 | + get: (key: string, defaultValue?: string | ArrayBuffer) => Promise<string | ArrayBuffer>; |
| 6 | + forEach: (each: (key: string, value: any) => void, onend: () => void) => void; |
| 7 | + close: () => void; |
| 8 | +} |
| 9 | + |
| 10 | +class CacheNoop implements Cache { |
| 11 | + // eslint-disable-next-line |
| 12 | + public close() { |
| 13 | + } |
| 14 | + |
| 15 | + public put(key: string, data: string | ArrayBuffer): Promise<void> { |
| 16 | + return Promise.resolve(); |
| 17 | + } |
| 18 | + |
| 19 | + public get(key: string, defaultValue?: string | ArrayBuffer): Promise<string | ArrayBuffer> { |
| 20 | + if (defaultValue !== undefined) { |
| 21 | + return Promise.resolve(defaultValue); |
| 22 | + } |
| 23 | + return Promise.reject(new Error("Cache is not supported on this host")); |
| 24 | + } |
| 25 | + |
| 26 | + public forEach(each: (key: string, value: any) => void, onend: () => void) { |
| 27 | + onend(); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export function makeCache(version: string, logger: { onErr(...args: any[]): any }): Promise<Cache> { |
| 32 | + return new Promise((resolve) => { |
| 33 | + new CacheDbImpl(version, resolve, (msg: string) => { |
| 34 | + logger.onErr(msg); |
| 35 | + resolve(new CacheNoop()); |
| 36 | + }); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +class CacheDbImpl implements Cache { |
| 41 | + public version: string; |
| 42 | + private storeName = "files"; |
| 43 | + private indexedDB: IDBFactory; |
| 44 | + private db: IDBDatabase | null = null; |
| 45 | + |
| 46 | + constructor(version: string, onready: (cache: Cache) => void, onerror: (msg: string) => void) { |
| 47 | + this.version = version; |
| 48 | + this.indexedDB = (typeof window === "undefined" ? undefined : window.indexedDB || |
| 49 | + (window as any).mozIndexedDB || |
| 50 | + (window as any).webkitIndexedDB || (window as any).msIndexedDB) as any; |
| 51 | + |
| 52 | + if (!this.indexedDB) { |
| 53 | + onerror("Indexed db is not supported on this host"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + const openRequest = this.indexedDB.open("js-dos-cache (" + version + ")", 1); |
| 59 | + openRequest.onerror = (event) => { |
| 60 | + onerror("Can't open cache database: " + openRequest.error?.message); |
| 61 | + }; |
| 62 | + openRequest.onsuccess = (event) => { |
| 63 | + this.db = openRequest.result; |
| 64 | + onready(this); |
| 65 | + }; |
| 66 | + openRequest.onupgradeneeded = (event) => { |
| 67 | + try { |
| 68 | + this.db = openRequest.result; |
| 69 | + this.db.onerror = (event) => { |
| 70 | + onerror("Can't upgrade cache database"); |
| 71 | + }; |
| 72 | + |
| 73 | + this.db.createObjectStore(this.storeName); |
| 74 | + } catch (e) { |
| 75 | + onerror("Can't upgrade cache database"); |
| 76 | + } |
| 77 | + }; |
| 78 | + } catch (e: any) { |
| 79 | + onerror("Can't open cache database: " + e.message); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public close() { |
| 84 | + if (this.db !== null) { |
| 85 | + this.db.close(); |
| 86 | + this.db = null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public put(key: string, data: string | ArrayBuffer): Promise<void> { |
| 91 | + return new Promise((resolve) => { |
| 92 | + if (this.db === null) { |
| 93 | + resolve(); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const transaction = this.db.transaction(this.storeName, "readwrite"); |
| 98 | + transaction.oncomplete = () => resolve(); |
| 99 | + transaction.objectStore(this.storeName).put(data, key); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + public get(key: string, defaultValue?: string | ArrayBuffer): Promise<string | ArrayBuffer> { |
| 104 | + return new Promise((resolve, reject) => { |
| 105 | + function rejectOrResolve(message: string) { |
| 106 | + if (defaultValue === undefined) { |
| 107 | + reject(new Error(message)); |
| 108 | + } else { |
| 109 | + resolve(defaultValue); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + if (this.db === null) { |
| 115 | + rejectOrResolve("db is not initalized"); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const transaction = this.db.transaction(this.storeName, "readonly"); |
| 120 | + const request = transaction.objectStore(this.storeName).get(key); |
| 121 | + request.onerror = () => reject(new Error("Can't read value for key '" + key + "'")); |
| 122 | + request.onsuccess = () => { |
| 123 | + if (request.result) { |
| 124 | + resolve(request.result); |
| 125 | + } else { |
| 126 | + rejectOrResolve("Result is empty for key '" + key + "', result: " + request.result); |
| 127 | + } |
| 128 | + }; |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + public forEach(each: (key: string, value: any) => void, onend: () => void) { |
| 133 | + if (this.db === null) { |
| 134 | + onend(); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const transaction = this.db.transaction(this.storeName, "readonly"); |
| 139 | + const request = transaction.objectStore(this.storeName).openCursor(); |
| 140 | + request.onerror = () => onend(); |
| 141 | + request.onsuccess = (event) => { |
| 142 | + const cursor = (event.target as any).result as IDBCursorWithValue; |
| 143 | + if (cursor) { |
| 144 | + each(cursor.key.toString(), cursor.value); |
| 145 | + cursor.continue(); |
| 146 | + } else { |
| 147 | + onend(); |
| 148 | + } |
| 149 | + }; |
| 150 | + } |
| 151 | +} |
0 commit comments