2 Commits
0.3.0 ... 0.3.2

Author SHA1 Message Date
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
4 changed files with 41 additions and 32 deletions

View File

@@ -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();
[ [

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/zim-utils", "name": "@ztimson/zim-utils",
"version": "0.3.0", "version": "0.3.2",
"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",

View File

@@ -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,7 +74,7 @@ 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) {

View File

@@ -34,30 +34,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 +89,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 +107,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 +123,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 +153,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,8 +175,8 @@ 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())};
} }