generated from ztimson/template
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f8637f913 | |||
| 57cb9c8bd1 | |||
| 7a2f17bfe9 | |||
| a2bde0b5cd | |||
| cab2571160 |
@@ -87,6 +87,7 @@ await server.stop(); // Kill server
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### List Local ZIMs
|
#### List Local ZIMs
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const local = await server.list();
|
const local = await server.list();
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/zim-utils",
|
"name": "@ztimson/zim-utils",
|
||||||
"version": "0.3.0",
|
"version": "0.3.5",
|
||||||
"description": "Native, dependency-light ZIM archive reader/searcher and Kiwix catalog downloader for Node.js",
|
"description": "Native, dependency-light ZIM archive reader/searcher and Kiwix catalog downloader for Node.js",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -10,24 +10,26 @@ import {zimCatalog, zimCatalogInfo, CATALOG_URL} from './catalog.js';
|
|||||||
export class ZimManager {
|
export class ZimManager {
|
||||||
#catalogUrl;
|
#catalogUrl;
|
||||||
#dir;
|
#dir;
|
||||||
server;
|
#server;
|
||||||
#ownsServer;
|
#ownsServer;
|
||||||
|
|
||||||
/** @param {{catalog?: string, server?: KiwixServer, port?: number, host?: string, binDir?: string}} [opts]
|
/** @param {{catalog?: string, server?: KiwixServer, port?: number, host?: string, binDir?: string, url?: string}} [opts]
|
||||||
* server: reuse an existing KiwixServer; if omitted, one is created (not yet started) and exposed via `.server` for reuse elsewhere. */
|
* server: reuse an existing KiwixServer instance directly.
|
||||||
constructor(dir, {catalog = CATALOG_URL, server, port, host, binDir} = {}) {
|
* url: attach to an already-running kiwix-serve by address instead (e.g. started elsewhere in your codebase
|
||||||
|
* with no shared reference) - mutually exclusive with `server`, ignored if `server` is given. */
|
||||||
|
constructor(dir, {catalog = CATALOG_URL, server, port, host, binDir, url} = {}) {
|
||||||
this.#catalogUrl = catalog;
|
this.#catalogUrl = catalog;
|
||||||
this.#dir = dir;
|
this.#dir = dir;
|
||||||
this.#ownsServer = !server;
|
this.#ownsServer = !server;
|
||||||
this.server = server ?? new KiwixServer(dir, {port, host, binDir});
|
this.#server = server ?? new KiwixServer(dir, {port, host, binDir, url});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The KiwixServer backing this manager - reuse it directly for content/search access, or pass into another ZimManager. */
|
/** The KiwixServer backing this manager - reuse it directly for content/search access, or pass into another ZimManager. */
|
||||||
get server() { return this.server; }
|
get server() { return this.#server; }
|
||||||
|
|
||||||
async #ensureServer() {
|
async #ensureServer() {
|
||||||
if (!this.server.running) await this.server.start();
|
if (!this.#server.running) await this.#server.start();
|
||||||
return this.server;
|
return this.#server;
|
||||||
}
|
}
|
||||||
|
|
||||||
async #download(url, destPath) {
|
async #download(url, destPath) {
|
||||||
@@ -72,14 +74,15 @@ export class ZimManager {
|
|||||||
|
|
||||||
/** Stops the internally-owned KiwixServer, if this manager created its own (no-op if one was passed in). */
|
/** Stops the internally-owned KiwixServer, if this manager created its own (no-op if one was passed in). */
|
||||||
async close() {
|
async close() {
|
||||||
if (this.#ownsServer) await this.server.stop();
|
if (this.#ownsServer) await this.#server.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(nameOrFile) {
|
async delete(nameOrFile) {
|
||||||
const local = await this.list();
|
const local = await this.list();
|
||||||
const match = local.find(l => l.name === nameOrFile || l.file === nameOrFile);
|
const match = local.find(l => l.name === nameOrFile || l.href === nameOrFile);
|
||||||
if (!match) throw new Error(`ZIM not found locally: ${nameOrFile}`);
|
if (!match) throw new Error(`ZIM not found locally: ${nameOrFile}`);
|
||||||
await fs.promises.rm(path.join(this.#dir, match.file), {force: true});
|
const file = match.href + (match.href.endsWith('.zim') ? '' : '.zim');
|
||||||
|
await fs.promises.rm(path.join(this.#dir, file), {force: true});
|
||||||
const server = await this.#ensureServer();
|
const server = await this.#ensureServer();
|
||||||
await server.reload();
|
await server.reload();
|
||||||
return {file: match.file, status: 'deleted'};
|
return {file: match.file, status: 'deleted'};
|
||||||
@@ -87,9 +90,9 @@ export class ZimManager {
|
|||||||
|
|
||||||
/** Downloads/updates a single ZIM by direct href, matching against any existing local copy by name. */
|
/** Downloads/updates a single ZIM by direct href, matching against any existing local copy by name. */
|
||||||
async download(href, {force = false} = {}) {
|
async download(href, {force = false} = {}) {
|
||||||
await fs.promises.mkdir(this.#dir, {recursive: true});
|
|
||||||
const {url: finalUrl} = await this.#resolveUrl(href);
|
const {url: finalUrl} = await this.#resolveUrl(href);
|
||||||
const filename = path.basename(new URL(finalUrl).pathname).replace(/\.meta4$/i, '');
|
const filename = path.basename(new URL(finalUrl).pathname).replace(/\.meta4$/i, '');
|
||||||
|
await fs.promises.mkdir(this.#dir, {recursive: true});
|
||||||
const name = filename.replace(/\.zim$/i, '').replace(/_\d{4}-\d{2}(?:_\d+)?$/, '');
|
const name = filename.replace(/\.zim$/i, '').replace(/_\d{4}-\d{2}(?:_\d+)?$/, '');
|
||||||
|
|
||||||
const local = await this.list();
|
const local = await this.list();
|
||||||
|
|||||||
@@ -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));
|
||||||
@@ -28,38 +27,64 @@ function findFreePort() {
|
|||||||
|
|
||||||
/** Owns a kiwix-serve process's full lifecycle: library.xml, start/stop/reload, content + search access. */
|
/** Owns a kiwix-serve process's full lifecycle: library.xml, start/stop/reload, content + search access. */
|
||||||
export class KiwixServer {
|
export class KiwixServer {
|
||||||
|
static #empty = '<?xml version="1.0" encoding="UTF-8" ?>\n<library version="20110515"></library>\n';
|
||||||
|
|
||||||
#dir;
|
#dir;
|
||||||
#host;
|
#host;
|
||||||
#port;
|
#port;
|
||||||
#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;
|
||||||
|
if (!this.#remote) this.#ensureLocalStore();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ensureLocalStore() {
|
||||||
|
fs.mkdirSync(this.#dir, {recursive: true});
|
||||||
|
if (!fs.existsSync(this.#libraryPath)) fs.writeFileSync(this.#libraryPath, KiwixServer.#empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
#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 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() {
|
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)]);
|
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() {
|
async #waitUntilReady() {
|
||||||
@@ -78,15 +103,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 +121,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 +137,18 @@ 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();
|
||||||
|
if (!xml) return [];
|
||||||
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 +168,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 +190,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 +224,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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,10 +40,3 @@ export function fuzzyMatch(target, ...terms) {
|
|||||||
similarities,
|
similarities,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function weightedScore(text, termList) {
|
|
||||||
if (!text) return 0;
|
|
||||||
const covered = termList.reduce((sum, t) => sum + t.length, 0);
|
|
||||||
const coverage = Math.min(1, covered / text.length);
|
|
||||||
return fuzzyMatch(text, ...termList).max * coverage;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user