|
|
|
@@ -6,8 +6,7 @@ import net from 'node:net';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
import path from 'node:path';
|
|
|
|
import {fileURLToPath} from 'node:url';
|
|
|
|
import {fileURLToPath} from 'node:url';
|
|
|
|
import {decodeHtml, fromXml} from '@ztimson/utils';
|
|
|
|
import {fromXml} from '@ztimson/utils';
|
|
|
|
import {fuzzyMatch, weightedScore} from './utils.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
@@ -34,30 +33,41 @@ export class KiwixServer {
|
|
|
|
#binDir;
|
|
|
|
#binDir;
|
|
|
|
#libraryPath;
|
|
|
|
#libraryPath;
|
|
|
|
#child = null;
|
|
|
|
#child = null;
|
|
|
|
|
|
|
|
#remote; // baseUrl string if attached to an externally-managed kiwix-serve, else null
|
|
|
|
|
|
|
|
|
|
|
|
get port() { return this.#port; }
|
|
|
|
get port() { return this.#port; }
|
|
|
|
get running() { return !!this.#child; }
|
|
|
|
get running() { return !!this.#remote || !!this.#child; }
|
|
|
|
get baseUrl() { return this.#child ? `http://${this.#host}:${this.#port}` : null; }
|
|
|
|
get baseUrl() { return this.#remote || (this.#child ? `http://${this.#host}:${this.#port}` : null); }
|
|
|
|
|
|
|
|
|
|
|
|
/** @param {{port?: number, host?: string, binDir?: string}} [opts] port defaults to an auto-picked free port; host defaults to localhost-only; binDir defaults to the bundled ./bin next to this package. */
|
|
|
|
/** @param {{port?: number, host?: string, binDir?: string, url?: string}} [opts]
|
|
|
|
constructor(dir, {port, host = '127.0.0.1', binDir = DEFAULT_BIN_DIR} = {}) {
|
|
|
|
* url: attach to an already-running kiwix-serve (e.g. one started elsewhere in your codebase) instead of
|
|
|
|
|
|
|
|
* spawning/owning one - start/stop/reload become no-ops, and library.xml is read over HTTP instead of disk. */
|
|
|
|
|
|
|
|
constructor(dir, {port, host = '127.0.0.1', binDir = DEFAULT_BIN_DIR, url} = {}) {
|
|
|
|
this.#dir = dir;
|
|
|
|
this.#dir = dir;
|
|
|
|
this.#host = host;
|
|
|
|
this.#host = host;
|
|
|
|
this.#port = port;
|
|
|
|
this.#port = port;
|
|
|
|
this.#binDir = binDir;
|
|
|
|
this.#binDir = binDir;
|
|
|
|
this.#libraryPath = path.join(dir, 'library.xml');
|
|
|
|
this.#libraryPath = path.join(dir, 'library.xml');
|
|
|
|
|
|
|
|
this.#remote = url ? url.replace(/\/$/, '') : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#assertRunning() {
|
|
|
|
#assertRunning() {
|
|
|
|
if (!this.#child) throw new Error('KiwixServer is not running - call start() first');
|
|
|
|
if (!this.running) throw new Error('KiwixServer is not running - call start() first');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#bin(name) {
|
|
|
|
#bin(name) {
|
|
|
|
return path.join(this.#binDir, process.platform === 'win32' ? `${name}.exe` : name);
|
|
|
|
return path.join(this.#binDir, process.platform === 'win32' ? `${name}.exe` : name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Rebuilds library.xml from scratch by scanning `dir` for .zim files - a full rebuild is simpler and less bug-prone than tracking incremental add/remove. */
|
|
|
|
/** 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(() => '');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Rebuilds library.xml from scratch by scanning `dir` for .zim files - no-op if attached to a remote server. */
|
|
|
|
async #rebuildLibrary() {
|
|
|
|
async #rebuildLibrary() {
|
|
|
|
|
|
|
|
if (this.#remote) return;
|
|
|
|
await fs.promises.rm(this.#libraryPath, {force: true});
|
|
|
|
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)]);
|
|
|
|
for (const f of await this.#zimFiles()) await execFileAsync(this.#bin('kiwix-manage'), [this.#libraryPath, 'add', path.join(this.#dir, f)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -78,15 +88,9 @@ export class KiwixServer {
|
|
|
|
return (await fs.promises.readdir(this.#dir).catch(() => [])).filter(f => f.endsWith('.zim'));
|
|
|
|
return (await fs.promises.readdir(this.#dir).catch(() => [])).filter(f => f.endsWith('.zim'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** URL for a single asset, e.g. for pulling one html/image file when you already know the path (from search). */
|
|
|
|
|
|
|
|
contentUrl(book, pathInZim = '') {
|
|
|
|
|
|
|
|
this.#assertRunning();
|
|
|
|
|
|
|
|
return `${this.baseUrl}/content/${book}/${pathInZim}`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Rebuilds library.xml and starts kiwix-serve. Resolves once the server is responding. */
|
|
|
|
/** Rebuilds library.xml and starts kiwix-serve. Resolves once the server is responding. */
|
|
|
|
async start() {
|
|
|
|
async start() {
|
|
|
|
if (this.#child) return;
|
|
|
|
if (this.#remote || this.#child) return;
|
|
|
|
await fs.promises.mkdir(this.#dir, {recursive: true});
|
|
|
|
await fs.promises.mkdir(this.#dir, {recursive: true});
|
|
|
|
await this.#rebuildLibrary();
|
|
|
|
await this.#rebuildLibrary();
|
|
|
|
this.#port ??= await findFreePort();
|
|
|
|
this.#port ??= await findFreePort();
|
|
|
|
@@ -102,9 +106,9 @@ export class KiwixServer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Gracefully stops kiwix-serve, if running. */
|
|
|
|
/** Gracefully stops kiwix-serve, if we own it. No-op if attached to a remote instance. */
|
|
|
|
async stop() {
|
|
|
|
async stop() {
|
|
|
|
if (!this.#child) return;
|
|
|
|
if (this.#remote || !this.#child) return;
|
|
|
|
const child = this.#child;
|
|
|
|
const child = this.#child;
|
|
|
|
await new Promise(resolve => {
|
|
|
|
await new Promise(resolve => {
|
|
|
|
child.once('exit', resolve);
|
|
|
|
child.once('exit', resolve);
|
|
|
|
@@ -118,16 +122,17 @@ export class KiwixServer {
|
|
|
|
await this.start();
|
|
|
|
await this.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Rebuilds library.xml from disk and hot-reloads kiwix-serve via SIGHUP - no downtime/restart needed. */
|
|
|
|
/** Rebuilds library.xml from disk and restarts kiwix-serve. No-op if attached to a remote instance -
|
|
|
|
|
|
|
|
* whoever owns that process is responsible for reloading it. */
|
|
|
|
async reload() {
|
|
|
|
async reload() {
|
|
|
|
if(!this.#child) return;
|
|
|
|
if (this.#remote || !this.#child) return;
|
|
|
|
await this.restart();
|
|
|
|
await this.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Local catalog listing - same flat shape as the online catalog (catalog.js), plus a `file` field since these already live on disk. */
|
|
|
|
/** Local catalog listing - same flat shape as the online catalog (catalog.js), plus a `file` field. */
|
|
|
|
async list() {
|
|
|
|
async list() {
|
|
|
|
this.#assertRunning();
|
|
|
|
this.#assertRunning();
|
|
|
|
const xml = await fs.promises.readFile(this.#libraryPath, 'utf8').catch(() => '');
|
|
|
|
const xml = await this.#fetchLibraryXml();
|
|
|
|
const entries = fromXml(xml);
|
|
|
|
const entries = fromXml(xml);
|
|
|
|
return (entries?.library?.book || []).map(e => {
|
|
|
|
return (entries?.library?.book || []).map(e => {
|
|
|
|
const tags = e.tags.split(';');
|
|
|
|
const tags = e.tags.split(';');
|
|
|
|
@@ -147,7 +152,7 @@ export class KiwixServer {
|
|
|
|
articleCount: +e.articleCount || 0,
|
|
|
|
articleCount: +e.articleCount || 0,
|
|
|
|
sizeMb: +(Number(e.size) / 1024).toFixed(1) || 0,
|
|
|
|
sizeMb: +(Number(e.size) / 1024).toFixed(1) || 0,
|
|
|
|
href: name,
|
|
|
|
href: name,
|
|
|
|
icon: e.favicon,
|
|
|
|
icon: `data:${e.faviconMimetype || 'image/png'};base64,${e.favicon}`,
|
|
|
|
viewer: `${this.baseUrl}/content/${name}`,
|
|
|
|
viewer: `${this.baseUrl}/content/${name}`,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
@@ -169,35 +174,28 @@ export class KiwixServer {
|
|
|
|
|
|
|
|
|
|
|
|
/** Fetches a single asset's raw bytes straight from kiwix-serve. */
|
|
|
|
/** Fetches a single asset's raw bytes straight from kiwix-serve. */
|
|
|
|
async raw(href) {
|
|
|
|
async raw(href) {
|
|
|
|
const res = await fetch(this.fetch(href));
|
|
|
|
const res = await fetch(this.link(href));
|
|
|
|
if (!res.ok) return null;
|
|
|
|
if(!res.ok) return null;
|
|
|
|
return {mimetype: res.headers.get('content-type'), data: Buffer.from(await res.arrayBuffer())};
|
|
|
|
return {mimetype: res.headers.get('content-type'), data: Buffer.from(await res.arrayBuffer())};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/** Fulltext search across every local ZIM via kiwix-serve's own xapian index */
|
|
|
|
* 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}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async search(terms, limit = 20) {
|
|
|
|
async search(terms, limit = 20) {
|
|
|
|
this.#assertRunning();
|
|
|
|
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 [];
|
|
|
|
if (!termList.length) return [];
|
|
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams({pattern: termList.join(' '), format: 'xml', pageLength: String(limit)});
|
|
|
|
const params = new URLSearchParams({pattern: termList.join(' '), format: 'xml', pageLength: String(limit)});
|
|
|
|
const res = await fetch(`${this.baseUrl}/search?${params}`);
|
|
|
|
const res = await fetch(`${this.baseUrl}/search?${params}`);
|
|
|
|
if (!res.ok) return [];
|
|
|
|
if (!res.ok) return [];
|
|
|
|
|
|
|
|
|
|
|
|
const xml = await res.text();
|
|
|
|
const found = fromXml(await res.text())?.rss?.channel?.item || [];
|
|
|
|
let found = fromXml(xml);
|
|
|
|
|
|
|
|
found = found?.rss?.channel?.item || [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const books = await this.list();
|
|
|
|
const books = await this.list();
|
|
|
|
const bookMap = new Map(books.map(b => [b.title, b]));
|
|
|
|
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);
|
|
|
|
const book = bookMap.get(hit.book.title);
|
|
|
|
if(!book) return null;
|
|
|
|
if (!book) return null;
|
|
|
|
const prefix = `/content/${book.href}/`;
|
|
|
|
const prefix = `/content/${book.href}/`;
|
|
|
|
const page = hit.link.startsWith(prefix) ? hit.link.slice(prefix.length) : hit.link.replace(/^\/+/, '');
|
|
|
|
const page = hit.link.startsWith(prefix) ? hit.link.slice(prefix.length) : hit.link.replace(/^\/+/, '');
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
@@ -210,10 +208,8 @@ export class KiwixServer {
|
|
|
|
icon: book.icon,
|
|
|
|
icon: book.icon,
|
|
|
|
viewer: this.baseUrl + hit.link,
|
|
|
|
viewer: this.baseUrl + hit.link,
|
|
|
|
summary: hit.description,
|
|
|
|
summary: hit.description,
|
|
|
|
score: weightedScore(hit.title, termList) + weightedScore(hit.description, termList),
|
|
|
|
score: +hit.score || 0,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}).filter(hit => !!hit && hit.score > 0);
|
|
|
|
}).filter(Boolean);
|
|
|
|
|
|
|
|
|
|
|
|
return enriched.toSorted((a, b) => b.score - a.score).slice(0, limit);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|