File size: 19,260 Bytes
7dac701
 
dac2ca2
 
 
 
 
 
7dac701
 
f392320
2315c7d
 
 
 
7dac701
15fc376
722c78c
9ceac12
 
 
dac2ca2
7dac701
dac2ca2
b93fb73
52361de
 
 
 
eb587c9
52361de
 
 
dac2ca2
 
 
 
 
52361de
dac2ca2
52361de
 
 
 
 
 
 
dac2ca2
52361de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dac701
dac2ca2
6853944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dac701
 
dac2ca2
722c78c
7dac701
f392320
6853944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f392320
 
 
52361de
 
 
 
 
 
 
 
6853944
 
 
 
 
 
 
 
f392320
7dac701
52361de
 
 
 
 
 
 
 
dac2ca2
f392320
7dac701
 
 
dac2ca2
52361de
722c78c
52361de
 
 
 
 
 
 
 
 
dac2ca2
52361de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac2ca2
7dac701
 
 
6853944
52361de
15fc376
52361de
6853944
 
 
 
 
52361de
7dac701
 
 
 
2315c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac2ca2
 
 
52361de
d61d6cf
7dac701
9ceac12
dac2ca2
6853944
2315c7d
 
 
 
7dac701
 
d61d6cf
 
 
 
7dac701
6853944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2315c7d
7dac701
 
 
 
 
 
2315c7d
7dac701
 
6853944
 
 
 
 
7dac701
 
52361de
2315c7d
52361de
 
7dac701
6853944
9ceac12
7dac701
 
dac2ca2
 
 
 
 
 
 
 
ffc49fd
 
 
 
 
 
7dac701
 
dac2ca2
7dac701
6853944
ffc49fd
 
7dac701
 
 
6853944
 
dac2ca2
52361de
 
6853944
52361de
7dac701
 
 
 
 
ffc49fd
 
 
 
 
 
6853944
 
 
 
 
 
 
 
ffc49fd
dac2ca2
52361de
6853944
 
2315c7d
6853944
 
 
 
 
 
2315c7d
6853944
 
 
 
 
52361de
 
6853944
 
2315c7d
6853944
 
2315c7d
 
 
 
6853944
 
 
 
 
 
 
 
 
 
52361de
dac2ca2
 
 
52361de
f392320
9ceac12
f392320
6853944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52361de
 
 
 
 
 
9ceac12
f392320
dac2ca2
f392320
ffc49fd
 
f392320
ffc49fd
f392320
ffc49fd
 
 
f392320
 
dac2ca2
f392320
 
9ceac12
f392320
6853944
 
 
 
 
 
 
52361de
 
 
 
 
6853944
9ceac12
f392320
dac2ca2
 
52361de
722c78c
52361de
 
a59751e
52361de
 
 
6853944
52361de
 
 
 
 
 
 
 
 
dac2ca2
7dac701
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
(function (root) {
  "use strict";
  /** @typedef {import('../../../typings/reader-store-strict').StoreHistoryInput} StoreHistoryInput */
  /** @typedef {import('../../../typings/reader-store-strict').HistoryEntry} HistoryEntry */
  /** @typedef {import('../../../typings/reader-store-strict').BookmarkInput} BookmarkInput */
  /** @typedef {import('../../../typings/reader-store-strict').BookmarkEntry} BookmarkEntry */
  /** @typedef {import('../../../typings/reader-store-strict').StoreChange} StoreChange */
  /** @typedef {import('../../../typings/reader-store-strict').StoreMessage} StoreMessage */
  const DB_NAME = "voiceofml-reader";
  const STORE_NAME = "entries";
  const BOOKMARK_STORE_NAME = "bookmarks";
  // Unindexed array keys in the bookmarks store cannot collide with string
  // bookmark IDs. This keeps v3 readable and leaves history capacity unchanged.
  const HISTORY_ORDER_KEY = "reader-history-order-v1";
  const HISTORY_WRITE_STORES = [STORE_NAME, BOOKMARK_STORE_NAME];
  const MAX_ENTRIES = 200;
  const SCHEMA_VERSION = 1;
  const DB_VERSION = 3;
  const script = /** @type {HTMLScriptElement | null | undefined} */ (root.document?.currentScript);
  const STORE_VERSION = script?.dataset.readerStoreVersion ||
    /reader-store\.([0-9a-f]{12})\.js(?:\?|$)/.exec(script?.src || "")?.[1] || "";
  /** @type {Promise<IDBDatabase> | null} */
  let databasePromise = null;
  /** @type {Set<(change: StoreMessage) => void>} */
  const subscribers = new Set();
  const storeChannel =
    typeof root.BroadcastChannel === "function"
      ? new root.BroadcastChannel("voiceofml-reader-store-v1")
      : null;
  let disposed = false;
  if (storeChannel)
    storeChannel.onmessage = (event) => {
      if (disposed) return;
      /** @type {unknown} */
      const data = event.data;
      // The channel historically forwards any payload; this assertion only permits
      // the existing object spread, it does not validate or filter the payload.
      for (const listener of subscribers) listener({ .../** @type {object} */ (data), remote: true });
    };
  /** @param {StoreChange} change */
  function notify(change) {
    if (disposed) return;
    try {
      storeChannel?.postMessage(change);
    } catch (_) {}
    for (const listener of subscribers) listener({ ...change, remote: false });
  }
  /** @param {(change: StoreMessage) => void} listener */
  function subscribe(listener) {
    if (typeof listener !== "function")
      throw new TypeError("Reader store subscriber must be a function");
    if (disposed) return () => {};
    subscribers.add(listener);
    return () => subscribers.delete(listener);
  }
  function dispose() {
    if (disposed) return;
    disposed = true;
    subscribers.clear();
    try {
      storeChannel?.close?.();
    } catch (_) {}
  }

  /** @param {IDBDatabase} database */
  function validateLayout(database) {
    if (!database.objectStoreNames.contains(STORE_NAME) || !database.objectStoreNames.contains(BOOKMARK_STORE_NAME))
      throw new Error("Unsupported Reader database layout");
    const tx = database.transaction([STORE_NAME, BOOKMARK_STORE_NAME], "readonly");
    const store = tx.objectStore(STORE_NAME), bookmarks = tx.objectStore(BOOKMARK_STORE_NAME);
    if (store.keyPath !== "url" || bookmarks.keyPath !== "id")
      throw new Error("Unsupported Reader database key layout");
    const requiredIndexes = /** @type {[IDBObjectStore, string][]} */ (
      [[store, "lastReadAt"], [bookmarks, "url"], [bookmarks, "createdAt"]]
    );
    for (const [objectStore, name] of requiredIndexes) {
      if (!objectStore.indexNames.contains(name)) throw new Error("Unsupported Reader database indexes");
      const index = objectStore.index(name);
      if (index.keyPath !== name || index.unique || index.multiEntry)
        throw new Error("Unsupported Reader database indexes");
    }
  }

  function openDatabase() {
    if (databasePromise) return databasePromise;
    const opening = /** @type {Promise<IDBDatabase>} */ (new Promise((resolve, reject) => {
      const request = indexedDB.open(DB_NAME, DB_VERSION);
      request.onupgradeneeded = () => {
        const database = request.result;
        const upgrade = request.transaction;
        if (!upgrade) throw new Error("Reader database upgrade has no transaction");
        const store = database.objectStoreNames.contains(STORE_NAME)
          ? upgrade.objectStore(STORE_NAME)
          : database.createObjectStore(STORE_NAME, { keyPath: "url" });
        const bookmarks = database.objectStoreNames.contains(BOOKMARK_STORE_NAME)
          ? upgrade.objectStore(BOOKMARK_STORE_NAME)
          : database.createObjectStore(BOOKMARK_STORE_NAME, { keyPath: "id" });
        // Unknown historical layouts are not safely migratable here. Abort the
        // upgrade instead of deleting a store or silently creating new keys.
        if (store.keyPath !== "url" || bookmarks.keyPath !== "id") {
          upgrade.abort();
          return;
        }
        const requiredIndexes = /** @type {[IDBObjectStore, string][]} */ (
          [[store, "lastReadAt"], [bookmarks, "url"], [bookmarks, "createdAt"]]
        );
        for (const [objectStore, name] of requiredIndexes) {
          if (objectStore.indexNames.contains(name)) {
            const index = objectStore.index(name);
            if (index.keyPath !== name || index.unique || index.multiEntry) {
              upgrade.abort();
              return;
            }
          } else objectStore.createIndex(name, name);
        }
      };
      request.onsuccess = () => {
        const database = request.result;
        database.onversionchange = () => {
          database.close();
          if (databasePromise === opening) databasePromise = null;
        };
        if (databasePromise !== opening) {
          database.close();
          return;
        }
        try {
          validateLayout(database);
        } catch (error) {
          database.close();
          databasePromise = null;
          reject(error);
          return;
        }
        resolve(database);
      };
      request.onblocked = () => {
        if (databasePromise === opening) databasePromise = null;
        reject(new Error("Reader database upgrade was blocked by another tab"));
      };
      request.onerror = () => {
        if (databasePromise === opening) databasePromise = null;
        reject(request.error);
      };
    }));
    databasePromise = opening;
    return databasePromise;
  }

  /** @param {StoreHistoryInput | null} entry @returns {HistoryEntry | null} */
  function normalizeHistoryEntry(entry) {
    if (!entry || typeof entry.url !== "string" || !entry.url)
      return null;
    return {
      ...entry,
      schemaVersion: SCHEMA_VERSION,
      title: String(entry.title || ""),
      extension: String(entry.extension || ""),
      lastReadAt: Number.isFinite(Number(entry.lastReadAt)) ? Number(entry.lastReadAt) : Date.now()
    };
  }
  /** @param {BookmarkInput | null} entry @returns {BookmarkEntry | null} */
  function normalizeBookmarkEntry(entry) {
    if (
      !entry ||
      typeof entry.id !== "string" ||
      !entry.id ||
      typeof entry.url !== "string" ||
      !entry.url
    )
      return null;
    return {
      ...entry,
      schemaVersion: SCHEMA_VERSION,
      label: String(entry.label || "未命名书签"),
      createdAt: Number.isFinite(Number(entry.createdAt)) ? Number(entry.createdAt) : Date.now()
    };
  }
  /** @param {string} url @returns {Promise<HistoryEntry | null>} */
  async function get(url) {
    const database = await openDatabase();
    return new Promise((resolve, reject) => {
      const tx = database.transaction(STORE_NAME, "readonly"),
        store = tx.objectStore(STORE_NAME);
      const request = store.get(url);
      request.onsuccess = () => {
        const raw = request.result || null;
        // Pre-schema records are readable without rewriting the original bytes.
        // Unknown future versions stay untouched and must not be interpreted.
        resolve(raw && (raw.schemaVersion == null || raw.schemaVersion === SCHEMA_VERSION)
          ? normalizeHistoryEntry(raw) : null);
      };
      request.onerror = () => reject(request.error);
    });
  }

  /** @param {string} [url] */
  function historyOrderKey(url) {
    return url === undefined ? [HISTORY_ORDER_KEY] : [HISTORY_ORDER_KEY, url];
  }

  /** @param {IDBObjectStore} order @param {string} [url] */
  function recordHistoryDeletion(order, url) {
    const id = historyOrderKey(url), previous = order.get(id);
    const deletedAt = Date.now();
    previous.onsuccess = () => {
      const cutoff = Math.max(deletedAt, previous.result?.deletedAt ?? 0);
      order.put({ id, deletedAt: cutoff });
      if (url !== undefined) return;
      // Clear supersedes older per-book markers. Keep any newer ones if the
      // wall clock moved backwards, and leave all bookmark keys untouched.
      const books = order.openCursor(IDBKeyRange.bound(id, [HISTORY_ORDER_KEY, []], true, true));
      books.onsuccess = () => {
        const cursor = books.result;
        if (!cursor) return;
        const key = cursor.primaryKey;
        if (Array.isArray(key) && key.length === 2 && typeof key[1] === "string" &&
            cursor.value?.deletedAt <= cutoff) cursor.delete();
        cursor.continue();
      };
    };
  }

  /** @param {StoreHistoryInput} supplied */
  async function put(supplied) {
    const entry = normalizeHistoryEntry(supplied);
    if (!entry) throw new TypeError("Invalid reader history entry");
    const hasCaptureTime = supplied.lastReadAt != null && Number.isFinite(Number(supplied.lastReadAt));
    const database = await openDatabase();
    await /** @type {Promise<void>} */ (new Promise((resolve, reject) => {
      /** @type {Error | null} */
      let abortReason = null;
      let written = false;
      const tx = database.transaction(HISTORY_WRITE_STORES, "readwrite");
      const store = tx.objectStore(STORE_NAME), order = tx.objectStore(BOOKMARK_STORE_NAME);
      const cleared = order.get(historyOrderKey()), removed = order.get(historyOrderKey(entry.url));
      const existing = store.get(entry.url);
      existing.onsuccess = () => {
        // Delayed snapshots carry an explicit capture time and lose ties to
        // deletion. An undated put is fresh and follows transaction order.
        if (hasCaptureTime && (entry.lastReadAt <= (cleared.result?.deletedAt ?? -Infinity) ||
            entry.lastReadAt <= (removed.result?.deletedAt ?? -Infinity))) return;
        if (existing.result) {
          if (existing.result.schemaVersion != null && existing.result.schemaVersion !== SCHEMA_VERSION) {
            tx.abort();
            return;
          }
          const previous = normalizeHistoryEntry(existing.result);
          if (!previous) {
            tx.abort();
            return;
          }
          if (Number(previous.lastReadAt) > Number(entry.lastReadAt)) return;
          const merged = { ...previous, ...entry };
          // A partial update must not replace retained metadata with empty
          // defaults introduced only by normalization.
          if (!Object.prototype.hasOwnProperty.call(supplied, "title")) merged.title = previous.title;
          if (!Object.prototype.hasOwnProperty.call(supplied, "extension")) merged.extension = previous.extension;
          store.put(merged);
          written = true;
          return;
        }
        const count = store.count();
        count.onsuccess = () => {
          if (count.result < MAX_ENTRIES) {
            store.put(entry);
            written = true;
            return;
          }
          // Refuse a new entry at the existing limit rather than evicting
          // someone else's reading progress to make room.
          abortReason = new Error("Reader history is full");
          abortReason.name = "ReaderHistoryFullError";
          tx.abort();
        };
      };
      tx.oncomplete = () => {
        if (written) notify({ type: "history", url: entry.url, lastReadAt: entry.lastReadAt });
        resolve();
      };
      tx.onerror = () => reject(tx.error);
      tx.onabort = () => reject(abortReason || tx.error || new Error("Reader history transaction aborted"));
    }));
  }

  /**
   * @template T, U
   * @param {string} storeName
   * @param {string} indexName
   * @param {(value: U) => T | null} normalize
   * @param {{query?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection, limit?: number}} [options]
   * @returns {Promise<T[]>}
   */
  async function listEntries(
    storeName,
    indexName,
    normalize,
    { query = null, direction = "next", limit = Infinity } = {}
  ) {
    const database = await openDatabase();
    return new Promise((resolve, reject) => {
      /** @type {T[]} */
      const entries = [];
      const tx = database.transaction(storeName, "readonly"),
        store = tx.objectStore(storeName);
      const request = store.index(indexName).openCursor(query, direction);
      request.onsuccess = () => {
        const cursor = request.result;
        if (!cursor || entries.length >= limit) return resolve(entries);
        const value = cursor.value;
        const entry = value && (value.schemaVersion == null || value.schemaVersion === SCHEMA_VERSION)
          ? normalize(/** @type {U} */ (value)) : null;
        if (entry) {
          entries.push(entry);
        }
        cursor.continue();
      };
      request.onerror = () => reject(request.error);
    });
  }

  function list(limit = MAX_ENTRIES) {
    return listEntries(STORE_NAME, "lastReadAt", normalizeHistoryEntry, {
      direction: "prev",
      limit
    });
  }
  async function isHistoryFull() {
    const database = await openDatabase();
    return /** @type {Promise<boolean>} */ (new Promise((resolve, reject) => {
      const tx = database.transaction(STORE_NAME, "readonly"), request = tx.objectStore(STORE_NAME).count();
      request.onsuccess = () => resolve(request.result >= MAX_ENTRIES);
      request.onerror = () => reject(request.error);
    }));
  }

  /** @param {string} url */
  async function remove(url) {
    const database = await openDatabase();
    await /** @type {Promise<void>} */ (new Promise((resolve, reject) => {
      const tx = database.transaction(HISTORY_WRITE_STORES, "readwrite"), store = tx.objectStore(STORE_NAME);
      const request = store.get(url);
      request.onsuccess = () => {
        const value = request.result;
        if (value && ((value.schemaVersion != null && value.schemaVersion !== SCHEMA_VERSION)
            || !normalizeHistoryEntry(value))) return tx.abort();
        store.delete(url);
        recordHistoryDeletion(tx.objectStore(BOOKMARK_STORE_NAME), url);
      };
      tx.oncomplete = () => { notify({ type: "history-remove", url }); resolve(); };
      tx.onerror = () => reject(tx.error);
      tx.onabort = () => reject(tx.error || new Error("Reader history cannot be removed safely"));
    }));
  }
  async function clearHistory() {
    const database = await openDatabase();
    await /** @type {Promise<void>} */ (new Promise((resolve, reject) => {
      const tx = database.transaction(HISTORY_WRITE_STORES, "readwrite"), request = tx.objectStore(STORE_NAME).openCursor();
      request.onsuccess = () => {
        const cursor = request.result;
        if (!cursor) {
          recordHistoryDeletion(tx.objectStore(BOOKMARK_STORE_NAME));
          return;
        }
        const value = cursor.value;
        if (value && (value.schemaVersion == null || value.schemaVersion === SCHEMA_VERSION)
            && normalizeHistoryEntry(value) && typeof value.lastReadAt === "number"
            && Number.isFinite(value.lastReadAt)) cursor.delete();
        cursor.continue();
      };
      tx.oncomplete = () => { notify({ type: "history-clear" }); resolve(); };
      tx.onerror = () => reject(tx.error);
      tx.onabort = () => reject(tx.error || new Error("Reader history clear aborted"));
    }));
  }
  /** @param {BookmarkInput} supplied */
  async function putBookmark(supplied) {
    const entry = normalizeBookmarkEntry(supplied);
    if (!entry) throw new TypeError("Invalid reader bookmark entry");
    const database = await openDatabase();
    await /** @type {Promise<void>} */ (new Promise((resolve, reject) => {
      const tx = database.transaction(BOOKMARK_STORE_NAME, "readwrite");
      const store = tx.objectStore(BOOKMARK_STORE_NAME);
      const existing = store.get(entry.id);
      existing.onsuccess = () => {
        if (existing.result?.schemaVersion != null && existing.result.schemaVersion !== SCHEMA_VERSION) {
          tx.abort();
          return;
        }
        const previous = existing.result ? normalizeBookmarkEntry(existing.result) : null;
        if (existing.result && (!previous || previous.url !== entry.url)) {
          tx.abort();
          return;
        }
        const merged = { ...previous, ...entry };
        if (previous && !Object.prototype.hasOwnProperty.call(supplied, "label")) merged.label = previous.label;
        if (previous && !Object.prototype.hasOwnProperty.call(supplied, "createdAt")) merged.createdAt = previous.createdAt;
        store.put(merged);
      };
      tx.oncomplete = () => {
        notify({ type: "bookmark", url: entry.url, id: entry.id });
        resolve();
      };
      tx.onerror = () => reject(tx.error);
      tx.onabort = () => reject(tx.error);
    }));
  }
  /** @param {string} url */
  async function listBookmarks(url) {
    const entries = await listEntries(BOOKMARK_STORE_NAME, "url", normalizeBookmarkEntry, {
      query: IDBKeyRange.only(url)
    });
    return entries.sort((a, b) => b.createdAt - a.createdAt);
  }
  function listAllBookmarks() {
    return listEntries(BOOKMARK_STORE_NAME, "createdAt", normalizeBookmarkEntry, {
      direction: "prev"
    });
  }
  /** @param {string} id */
  async function removeBookmark(id) {
    const database = await openDatabase();
    await /** @type {Promise<void>} */ (new Promise((resolve, reject) => {
      const tx = database.transaction(BOOKMARK_STORE_NAME, "readwrite");
      const store = tx.objectStore(BOOKMARK_STORE_NAME), request = store.get(id);
      request.onsuccess = () => {
        const value = request.result;
        if (value && ((value.schemaVersion != null && value.schemaVersion !== SCHEMA_VERSION)
            || !normalizeBookmarkEntry(value))) return tx.abort();
        store.delete(id);
      };
      tx.oncomplete = () => {
        notify({ type: "bookmark-remove", id });
        resolve();
      };
      tx.onerror = () => reject(tx.error);
      tx.onabort = () => reject(tx.error || new Error("Reader bookmark cannot be removed safely"));
    }));
  }
  /** @satisfies {import('../../../typings/reader-store-strict').ReaderStoreStrictApi} */
  const api = Object.freeze({
    DB_NAME,
    DB_VERSION,
    MAX_ENTRIES,
    SCHEMA_VERSION,
    STORE_VERSION,
    get,
    put,
    list,
    isHistoryFull,
    remove,
    clearHistory,
    putBookmark,
    listBookmarks,
    listAllBookmarks,
    removeBookmark,
    subscribe,
    dispose
  });
  root.VoiceOfMLReaderStore = api;
})(typeof self !== "undefined" ? self : window);