diff --git a/package.json b/package.json index a3e2f6c..e3399ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/zim-utils", - "version": "0.4.9", + "version": "0.4.10", "description": "Native, dependency-light ZIM archive reader/searcher and Kiwix catalog downloader for Node.js", "author": "Zak Timson", "license": "MIT", diff --git a/src/manager.js b/src/manager.js index 2cda52f..aa85d50 100644 --- a/src/manager.js +++ b/src/manager.js @@ -80,10 +80,16 @@ export class ZimManager { * 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; + const server = await this.#ensureServer(); while (Date.now() < deadline) { const local = await this.#rawList(); - const present = local.some(l => l.file === filename); - if (present === expect) return true; + const match = local.find(l => l.file === filename); + if (!expect) { + if (!match) return true; + } else if (match?.name) { + const probe = await fetch(`${server.baseUrl}/content/${encodeURIComponent(match.name)}/`).catch(() => null); + if (probe?.ok) return true; + } await new Promise(r => setTimeout(r, VISIBLE_POLL_INTERVAL)); } return false; diff --git a/src/server.js b/src/server.js index e317c37..42433ce 100644 --- a/src/server.js +++ b/src/server.js @@ -95,20 +95,16 @@ export class KiwixServer { async #rebuildLibrary() { if(this.#remote) return; - - await fs.promises.rm(this.#libraryPath, {force: true}); - const files = await this.#zimFiles(); - - if(!files.length) - return fs.promises.writeFile(this.#libraryPath, KiwixServer.#empty); - - for(const file of files) - await execFileAsync(this.#bin('kiwix-manage'), [ - this.#libraryPath, - 'add', - path.join(this.#dir, file), - ]); + const tmpPath = `${this.#libraryPath}.tmp-${process.pid}`; + await fs.promises.rm(tmpPath, {force: true}); + if(!files.length) { + await fs.promises.writeFile(tmpPath, KiwixServer.#empty); + } else { + for(const file of files) + await execFileAsync(this.#bin('kiwix-manage'), [tmpPath, 'add', path.join(this.#dir, file)]); + } + await fs.promises.rename(tmpPath, this.#libraryPath); } async #waitUntilReady() {