generated from ztimson/template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36f954bb6b | ||
|
|
a1aa158264 |
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/zim-utils",
|
||||
"version": "0.4.9",
|
||||
"version": "0.4.11",
|
||||
"description": "Native, dependency-light ZIM archive reader/searcher and Kiwix catalog downloader for Node.js",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
+9
-2
@@ -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;
|
||||
@@ -136,6 +142,7 @@ export class ZimManager {
|
||||
const match = local.find(l => l.name === nameOrFile || l.href === nameOrFile);
|
||||
if (!match) throw new Error(`ZIM not found locally: ${nameOrFile}`);
|
||||
const file = match.href + (match.href.endsWith('.zim') ? '' : '.zim');
|
||||
await this.#server.stop();
|
||||
await fs.promises.rm(path.join(this.#dir, file), {force: true});
|
||||
await this.#ensureServer();
|
||||
await this.#waitUntilVisible(file, {expect: false});
|
||||
|
||||
+9
-13
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user