File size: 8,569 Bytes
4fbb2dd
 
3c5518a
2887bf0
ee2f2ba
4fbb2dd
17df17b
4fbb2dd
17df17b
4fbb2dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54bcbbc
17df17b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fbb2dd
 
 
 
 
 
17df17b
4fbb2dd
17df17b
4fbb2dd
 
 
 
17df17b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fbb2dd
17df17b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
086a293
17df17b
 
 
 
 
 
4fbb2dd
17df17b
 
 
 
 
 
 
 
 
4fbb2dd
 
17df17b
4fbb2dd
 
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
const PACKED_LIMIT = 64 * 1024 * 1024;
const TEXT_LIMIT = 256 * 1024 * 1024;
import {countMatches, resultPage, aborted, PAGE_SIZE} from "../reader-chapter-search-core.mjs";
import {isSearchIndexProxyURL} from "../reader-chapter-search-proxy-url.mjs";
import {validateIndex} from "../reader-chapter-search-index.mjs";

/** @param {ReadableStream<Uint8Array<ArrayBuffer>> | null} stream @param {number} limit @param {AbortSignal} signal */
async function readBounded(stream, limit, signal) {
  if (!stream) throw new Error("全文搜索索引无效");
  const reader = stream.getReader(), chunks = [];
  let size = 0;
  const cancel = () => reader.cancel().catch(() => {});
  signal.addEventListener("abort", cancel, { once: true });
  try {
    while (true) {
      if (signal.aborted) throw aborted();
      const { value, done } = await reader.read();
      if (signal.aborted) throw aborted();
      if (done) break;
      size += value.byteLength;
      if (size > limit) throw new Error("全文搜索数据超过大小限制");
      chunks.push(value);
    }
    const result = new Uint8Array(size);
    let offset = 0;
    for (const chunk of chunks) { result.set(chunk, offset); offset += chunk.length; }
    return result;
  } catch (error) {
    await reader.cancel().catch(() => {});
    throw error;
  } finally {
    signal.removeEventListener("abort", cancel);
    reader.releaseLock();
  }
}


/** @typedef {import('../reader-chapter-search-index.mjs').ExpectedChapter} ExpectedChapter */
/** @typedef {{url: string, bytes: number, sha256: string, text_profile?: string | undefined}} IndexReference */
/** @typedef {{index: IndexReference, chapters: ExpectedChapter[]}} Configuration */
/** @typedef {Awaited<ReturnType<typeof countMatches>> & {query: string, id: number}} SearchSession */
/** @typedef {{type: 'init', configuration: Configuration} | {type: 'cancel'} |
 * {type: 'search', id: number, query: unknown} | {type: 'page', id: number, session: number, offset: number}} ChapterMessage */
/** @param {unknown} value @returns {value is Record<string, unknown>} */
function isRecord(value) { return value !== null && typeof value === 'object'; }
/** @param {unknown} value @returns {value is Configuration} */
function isConfiguration(value) {
  return isRecord(value) && isRecord(value.index) && typeof value.index.url === 'string' &&
    typeof value.index.bytes === 'number' && typeof value.index.sha256 === 'string' &&
    (value.index.text_profile === undefined || typeof value.index.text_profile === 'string') &&
    Array.isArray(value.chapters) && value.chapters.every(chapter => isRecord(chapter) &&
      typeof chapter.index === 'number' && typeof chapter.path === 'string');
}
/** @param {unknown} value @returns {value is ChapterMessage} */
function isMessage(value) {
  if (!isRecord(value)) return false;
  if (value.type === 'init') return isConfiguration(value.configuration);
  if (value.type === 'cancel') return true;
  if (value.type === 'search') return typeof value.id === 'number';
  return value.type === 'page' && typeof value.id === 'number' && typeof value.session === 'number' && typeof value.offset === 'number';
}
/** @type {Configuration | undefined} */
let configuration;
/** @type {Promise<import('../reader-chapter-search-core.mjs').SearchChapter[]> | undefined} */
let indexPromise;
/** @type {import('../reader-chapter-search-core.mjs').SearchChapter[] | undefined} */
let chapters;
/** @type {SearchSession | null} */
let session = null;
/** @type {AbortController | null} */
let loadController = null;
let active = 0;
/** @param {IndexReference} index @param {URL} url */
function validateIndexReference(index,url) {
  if (!isSearchIndexProxyURL(url, self.location.origin) ||
      !Number.isSafeInteger(index.bytes) || index.bytes <= 0 || index.bytes > PACKED_LIMIT ||
      !/^[0-9a-f]{64}$/.test(index.sha256)) throw new Error("全文搜索索引无效");
}
/** @param {AbortController} controller @returns {Promise<import('../reader-chapter-search-core.mjs').SearchChapter[]>} */
async function fetchIndex(controller) {
  if (!configuration) throw new Error("全文搜索索引无效");
  const { index, chapters: expected } = configuration;
  const url = new URL(index.url, self.location.origin);
  validateIndexReference(index,url);
  const response = await fetch(url, { signal: controller.signal, credentials: "same-origin" });
  if (!response.ok) throw new Error(`全文搜索加载失败(HTTP ${response.status}),请重试`);
  const bytes = await readBounded(response.body, PACKED_LIMIT, controller.signal);
  const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
  const sha = [...digest].map(value => value.toString(16).padStart(2, "0")).join("");
  if (bytes.length !== index.bytes || sha !== index.sha256) throw new Error("全文搜索索引校验失败,请重试");
  const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"));
  const text = await readBounded(stream, TEXT_LIMIT, controller.signal);
  /** @type {unknown} */
  const raw = JSON.parse(new TextDecoder().decode(text));
  const loaded = validateIndex(raw, expected, index.text_profile);
  if (controller.signal.aborted) throw aborted();
  chapters = loaded;
  return loaded;
}
async function loadIndex() {
  if (chapters) return chapters;
  if (indexPromise) return indexPromise;
  const controller = new AbortController();
  loadController = controller;
  const timer = setTimeout(() => controller.abort(), 60000);
  indexPromise = fetchIndex(controller).finally(() => {
    clearTimeout(timer);
    indexPromise = undefined;
    if (loadController === controller) loadController = null;
  });
  return indexPromise;
}
/** @param {string} query @param {number} id @param {() => boolean} current @param {{postMessage(message: unknown): void}} scope */
async function beginSearch(query,id,current,scope) {
  session = null;
  query = String(query || "").trim().replace(/\s+/gu, " ");
  if (!query || query.length > 2048) throw new Error("请输入 1–2048 个字符");
  if (loadController?.signal.aborted) await indexPromise?.catch(() => {});
  const book=await loadIndex();
  if (!current()) return;
  const counts=await countMatches(book,query,current,async partial=>{
    if (current()) scope.postMessage({id,progress:true,total:partial.total,scanned:partial.scanned,
      chapters:book.length,offset:0,pageSize:PAGE_SIZE,results:partial.firstPage.slice()});
  });
  session={...counts,query,id};
}
/** @param {number} requested @param {number} expected */
function pageOffset(requested,expected) {
  if (!session || expected !== session.id) throw aborted();
  return Math.max(0,Math.min(Math.floor(requested/PAGE_SIZE)*PAGE_SIZE,
    Math.max(0,Math.ceil(session.total/PAGE_SIZE)-1)*PAGE_SIZE));
}

/** @param {unknown} error */
function requestError(error) {
  const detail=error instanceof Error ? error : new Error(String(error));
  return detail.name === "AbortError" ? "全文搜索已取消或加载超时,请重试" : detail.message;
}
/** @param {Extract<ChapterMessage,{type:'search'|'page'}>} data @param {{postMessage(message: unknown): void}} scope */
async function handleRequest(data,scope) {
  const token=++active,current=()=>token===active;
  try {
    let offset=0;
    if (data.type === "search") {
      await beginSearch(String(data.query || ""),data.id,current,scope);
      if (!current()) return;
    } else offset=pageOffset(data.offset,data.session);
    if (!session || !chapters) throw aborted();
    const result=offset ? await resultPage(chapters,session.query,session.counts,offset,current,session.checkpoints) : session.firstPage.slice();
    if (current()) scope.postMessage({id:data.id,session:session.id,total:session.total,
      offset,pageSize:PAGE_SIZE,results:result});
  } catch (error) {
    if (current()) scope.postMessage({id:data.id,error:requestError(error)});
  }
}
if (typeof self !== "undefined" && typeof self.postMessage === "function") {
  const scope = self;
  scope.onmessage = event => {
    /** @type {unknown} */
    const data=event.data;
    if (!isMessage(data)) {
      if (isRecord(data) && data.type === 'init') configuration=undefined;
      if (isRecord(data) && typeof data.id === 'number') scope.postMessage({id:data.id,error:'全文搜索索引无效'});
      return;
    }
    if (data.type === "init") { configuration = data.configuration; return; }
    if (data.type === "cancel") { active++; session = null; loadController?.abort(); return; }
    return handleRequest(data,scope);
  };
}