5 Commits
0.3.0 ... 0.3.5

Author SHA1 Message Date
0f8637f913 Ensure library.xml is setup
All checks were successful
Publish Library / Build NPM Project (push) Successful in 45s
Publish Library / Tag Version (push) Successful in 8s
2026-08-26 18:41:15 -04:00
57cb9c8bd1 Fixed delete by href
All checks were successful
Publish Library / Build NPM Project (push) Successful in 44s
Publish Library / Tag Version (push) Successful in 26s
2026-08-25 01:00:28 -04:00
7a2f17bfe9 Use kiwix search completely, no fuzzy ranking
All checks were successful
Publish Library / Build NPM Project (push) Successful in 16s
Publish Library / Tag Version (push) Successful in 10s
2026-08-25 00:42:59 -04:00
a2bde0b5cd Fixed server getter
All checks were successful
Publish Library / Build NPM Project (push) Successful in 14s
Publish Library / Tag Version (push) Successful in 11s
2026-08-24 21:18:24 -04:00
cab2571160 Accenpt kiwix server URL and patched local zim icon to be dataURL
All checks were successful
Publish Library / Build NPM Project (push) Successful in 15s
Publish Library / Tag Version (push) Successful in 10s
2026-08-24 21:06:23 -04:00
5 changed files with 70 additions and 61 deletions

View File

@@ -87,6 +87,7 @@ await server.stop(); // Kill server
```
#### List Local ZIMs
```js
const local = await server.list();
[

View File

@@ -1,6 +1,6 @@
{
"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",
"author": "Zak Timson",
"license": "MIT",

View File

@@ -10,24 +10,26 @@ import {zimCatalog, zimCatalogInfo, CATALOG_URL} from './catalog.js';
export class ZimManager {
#catalogUrl;
#dir;
server;
#server;
#ownsServer;
/** @param {{catalog?: string, server?: KiwixServer, port?: number, host?: string, binDir?: string}} [opts]
* server: reuse an existing KiwixServer; if omitted, one is created (not yet started) and exposed via `.server` for reuse elsewhere. */
constructor(dir, {catalog = CATALOG_URL, server, port, host, binDir} = {}) {
/** @param {{catalog?: string, server?: KiwixServer, port?: number, host?: string, binDir?: string, url?: string}} [opts]
* server: reuse an existing KiwixServer instance directly.
* 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.#dir = dir;
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. */
get server() { return this.server; }
get server() { return this.#server; }
async #ensureServer() {
if (!this.server.running) await this.server.start();
return this.server;
if (!this.#server.running) await this.#server.start();
return this.#server;
}
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). */
async close() {
if (this.#ownsServer) await this.server.stop();
if (this.#ownsServer) await this.#server.stop();
}
async delete(nameOrFile) {
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}`);
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();
await server.reload();
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. */
async download(href, {force = false} = {}) {
await fs.promises.mkdir(this.#dir, {recursive: true});
const {url: finalUrl} = await this.#resolveUrl(href);
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 local = await this.list();

View File

@@ -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,38 +27,64 @@ 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;
#binDir;
#libraryPath;
#child = null;
#remote; // baseUrl string if attached to an externally-managed kiwix-serve, else null
get port() { return this.#port; }
get running() { return !!this.#child; }
get baseUrl() { return this.#child ? `http://${this.#host}:${this.#port}` : null; }
get running() { return !!this.#remote || !!this.#child; }
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. */
constructor(dir, {port, host = '127.0.0.1', binDir = DEFAULT_BIN_DIR} = {}) {
/** @param {{port?: number, host?: string, binDir?: string, url?: string}} [opts]
* 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.#host = host;
this.#port = port;
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() {
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) {
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() {
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() {
@@ -78,15 +103,9 @@ export class KiwixServer {
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. */
async start() {
if (this.#child) return;
if (this.#remote || this.#child) return;
await fs.promises.mkdir(this.#dir, {recursive: true});
await this.#rebuildLibrary();
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() {
if (!this.#child) return;
if (this.#remote || !this.#child) return;
const child = this.#child;
await new Promise(resolve => {
child.once('exit', resolve);
@@ -118,16 +137,18 @@ export class KiwixServer {
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() {
if(!this.#child) return;
if (this.#remote || !this.#child) return;
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() {
this.#assertRunning();
const xml = await fs.promises.readFile(this.#libraryPath, 'utf8').catch(() => '');
const xml = await this.#fetchLibraryXml();
if (!xml) return [];
const entries = fromXml(xml);
return (entries?.library?.book || []).map(e => {
const tags = e.tags.split(';');
@@ -147,7 +168,7 @@ export class KiwixServer {
articleCount: +e.articleCount || 0,
sizeMb: +(Number(e.size) / 1024).toFixed(1) || 0,
href: name,
icon: e.favicon,
icon: `data:${e.faviconMimetype || 'image/png'};base64,${e.favicon}`,
viewer: `${this.baseUrl}/content/${name}`,
};
});
@@ -169,35 +190,28 @@ export class KiwixServer {
/** Fetches a single asset's raw bytes straight from kiwix-serve. */
async raw(href) {
const res = await fetch(this.fetch(href));
if (!res.ok) return null;
const res = await fetch(this.link(href));
if(!res.ok) return null;
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 {
@@ -210,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);
}
}

View File

@@ -40,10 +40,3 @@ export function fuzzyMatch(target, ...terms) {
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;
}