|
|
|
|
@@ -6,8 +6,7 @@ import net from 'node:net';
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import {fileURLToPath} from 'node:url';
|
|
|
|
|
import {decodeHtml, fromXml} from '@ztimson/utils';
|
|
|
|
|
import {fuzzyMatch, weightedScore} from './utils.js';
|
|
|
|
|
import {fromXml} from '@ztimson/utils';
|
|
|
|
|
|
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
@@ -28,6 +27,8 @@ function findFreePort() {
|
|
|
|
|
|
|
|
|
|
/** Owns a kiwix-serve process's full lifecycle: library.xml, start/stop/reload, content + search access. */
|
|
|
|
|
export class KiwixServer {
|
|
|
|
|
static #empty = '<?xml version="1.0" encoding="UTF-8" ?>\n<library version="20110515"></library>\n';
|
|
|
|
|
|
|
|
|
|
#dir;
|
|
|
|
|
#host;
|
|
|
|
|
#port;
|
|
|
|
|
@@ -50,6 +51,12 @@ export class KiwixServer {
|
|
|
|
|
this.#binDir = binDir;
|
|
|
|
|
this.#libraryPath = path.join(dir, 'library.xml');
|
|
|
|
|
this.#remote = url ? url.replace(/\/$/, '') : null;
|
|
|
|
|
if (!this.#remote) this.#ensureLocalStore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ensureLocalStore() {
|
|
|
|
|
fs.mkdirSync(this.#dir, {recursive: true});
|
|
|
|
|
if (!fs.existsSync(this.#libraryPath)) fs.writeFileSync(this.#libraryPath, KiwixServer.#empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#assertRunning() {
|
|
|
|
|
@@ -62,15 +69,22 @@ export class KiwixServer {
|
|
|
|
|
|
|
|
|
|
/** Reads library.xml from disk if we own the server, or over HTTP if attached to a remote one. */
|
|
|
|
|
async #fetchLibraryXml() {
|
|
|
|
|
if (this.#remote) return (await fetch(`${this.#remote}/library.xml`).catch(() => null))?.text?.() ?? '';
|
|
|
|
|
return fs.promises.readFile(this.#libraryPath, 'utf8').catch(() => '');
|
|
|
|
|
if (!this.#remote) return fs.promises.readFile(this.#libraryPath, 'utf8').catch(() => '');
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(`${this.#remote}/library.xml`);
|
|
|
|
|
return res.ok ? await res.text() : '';
|
|
|
|
|
} catch {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Rebuilds library.xml from scratch by scanning `dir` for .zim files - no-op if attached to a remote server. */
|
|
|
|
|
async #rebuildLibrary() {
|
|
|
|
|
if (this.#remote) return;
|
|
|
|
|
await fs.promises.rm(this.#libraryPath, {force: true});
|
|
|
|
|
for (const f of await this.#zimFiles()) await execFileAsync(this.#bin('kiwix-manage'), [this.#libraryPath, 'add', path.join(this.#dir, f)]);
|
|
|
|
|
const files = await this.#zimFiles();
|
|
|
|
|
if (!files.length) return fs.promises.writeFile(this.#libraryPath, KiwixServer.#empty);
|
|
|
|
|
for (const f of files) await execFileAsync(this.#bin('kiwix-manage'), [this.#libraryPath, 'add', path.join(this.#dir, f)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async #waitUntilReady() {
|
|
|
|
|
@@ -134,6 +148,7 @@ export class KiwixServer {
|
|
|
|
|
async list() {
|
|
|
|
|
this.#assertRunning();
|
|
|
|
|
const xml = await this.#fetchLibraryXml();
|
|
|
|
|
if (!xml) return [];
|
|
|
|
|
const entries = fromXml(xml);
|
|
|
|
|
return (entries?.library?.book || []).map(e => {
|
|
|
|
|
const tags = e.tags.split(';');
|
|
|
|
|
@@ -180,30 +195,23 @@ export class KiwixServer {
|
|
|
|
|
return {mimetype: res.headers.get('content-type'), data: Buffer.from(await res.arrayBuffer())};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Two-pass fulltext search across every local ZIM: xapian prefilter (kiwix-serve's
|
|
|
|
|
* own index), then fuzzy-reranked by title so the strongest matches surface first.
|
|
|
|
|
* Returns a flat array matching the catalog/list shape: {id, title, name, category, ..., href, score}
|
|
|
|
|
*/
|
|
|
|
|
/** Fulltext search across every local ZIM via kiwix-serve's own xapian index */
|
|
|
|
|
async search(terms, limit = 20) {
|
|
|
|
|
this.#assertRunning();
|
|
|
|
|
const termList = String(terms).split(',').map(t => t.trim()).filter(Boolean);
|
|
|
|
|
const termList = String(terms).split(/[,\s]+/).map(t => t.trim()).filter(Boolean);
|
|
|
|
|
if (!termList.length) return [];
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams({pattern: termList.join(' '), format: 'xml', pageLength: String(limit)});
|
|
|
|
|
const res = await fetch(`${this.baseUrl}/search?${params}`);
|
|
|
|
|
if (!res.ok) return [];
|
|
|
|
|
|
|
|
|
|
const xml = await res.text();
|
|
|
|
|
let found = fromXml(xml);
|
|
|
|
|
found = found?.rss?.channel?.item || [];
|
|
|
|
|
|
|
|
|
|
const found = fromXml(await res.text())?.rss?.channel?.item || [];
|
|
|
|
|
const books = await this.list();
|
|
|
|
|
const bookMap = new Map(books.map(b => [b.title, b]));
|
|
|
|
|
|
|
|
|
|
const enriched = found.map(hit => {
|
|
|
|
|
return found.map(hit => {
|
|
|
|
|
const book = bookMap.get(hit.book.title);
|
|
|
|
|
if(!book) return null;
|
|
|
|
|
if (!book) return null;
|
|
|
|
|
const prefix = `/content/${book.href}/`;
|
|
|
|
|
const page = hit.link.startsWith(prefix) ? hit.link.slice(prefix.length) : hit.link.replace(/^\/+/, '');
|
|
|
|
|
return {
|
|
|
|
|
@@ -216,10 +224,8 @@ export class KiwixServer {
|
|
|
|
|
icon: book.icon,
|
|
|
|
|
viewer: this.baseUrl + hit.link,
|
|
|
|
|
summary: hit.description,
|
|
|
|
|
score: weightedScore(hit.title, termList) + weightedScore(hit.description, termList),
|
|
|
|
|
score: +hit.score || 0,
|
|
|
|
|
};
|
|
|
|
|
}).filter(hit => !!hit && hit.score > 0);
|
|
|
|
|
|
|
|
|
|
return enriched.toSorted((a, b) => b.score - a.score).slice(0, limit);
|
|
|
|
|
}).filter(Boolean);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|