Auto library reloading
Publish Library / Build NPM Project (push) Successful in 5m45s
Publish Library / Tag Version (push) Successful in 26s

This commit is contained in:
2026-09-20 12:54:56 -04:00
parent 4b86fe8051
commit 997bd6064d
3 changed files with 76 additions and 19 deletions
+25 -6
View File
@@ -5,8 +5,13 @@ import {Readable} from 'node:stream';
import {KiwixServer} from './server.js';
import {zimCatalog, zimCatalogInfo, CATALOG_URL} from './catalog.js';
const VISIBLE_TIMEOUT = 5_000;
const VISIBLE_POLL_INTERVAL = 200;
/** Manages a local directory of ZIM archives: catalog search, downloads, update checks, deletion.
* Reuses (or owns & lazily starts) a KiwixServer for local listing/search, so `list()` and `catalog()` return the same shape. */
* Reuses (or owns & lazily starts) a KiwixServer for local listing/search, so `list()` and `catalog()` return the same shape.
* Doesn't drive library.xml/kiwix-serve reloads itself - the owning KiwixServer watches its directory and
* reloads automatically whenever .zim files change, no matter which ZimManager (local or remote-attached) wrote them. */
export class ZimManager {
#catalogUrl;
#dir;
@@ -56,6 +61,20 @@ export class ZimManager {
return {res, url: m[1]};
}
/** Polls list() until `filename` shows up (or disappears, if `expect: false`), so callers get an accurate
* status back rather than one that's ahead of what the server has actually picked up yet. The owning
* KiwixServer's directory watcher does the real reload work in the background; this just waits for it. */
async #waitUntilVisible(filename, {expect = true, timeout = VISIBLE_TIMEOUT} = {}) {
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
const local = await this.list();
const present = local.some(l => l.file === filename);
if (present === expect) return true;
await new Promise(r => setTimeout(r, VISIBLE_POLL_INTERVAL));
}
return false;
}
async #update(name, catalogEntry, localMatch, force) {
const remoteDate = catalogEntry.updated ? new Date(catalogEntry.updated) : null;
const localDate = localMatch?.updated ?? null;
@@ -67,8 +86,8 @@ export class ZimManager {
const destPath = path.join(this.#dir, filename);
await this.#download(catalogEntry.href, destPath);
if (localMatch && localMatch.file !== filename) await fs.promises.rm(path.join(this.#dir, localMatch.file), {force: true});
const server = await this.#ensureServer();
await server.reload();
await this.#ensureServer();
await this.#waitUntilVisible(filename, {expect: true});
return {name, status: 'updated', file: filename};
}
@@ -87,8 +106,8 @@ export class ZimManager {
if (!match) throw new Error(`ZIM not found locally: ${nameOrFile}`);
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();
await this.#ensureServer();
await this.#waitUntilVisible(file, {expect: false});
return {file: match.file, status: 'deleted'};
}
@@ -101,7 +120,7 @@ export class ZimManager {
const exact = local.find(l => l.file === filename);
if (exact && !force) return {name: exact.name ?? this.#stripDate(filename), status: 'skipped', reason: 'already downloaded', file: filename};
const name = this.#stripDate(filename);
const localMatch = local.find(l => this.#stripDate(l.file) === name) ?? null;
const localMatch = local.find(l => l.name === name) ?? null;
const catalogEntry = await zimCatalogInfo(name, this.#catalogUrl) || {name, updated: null, href};
return this.#update(name, catalogEntry, localMatch, force);
}