generated from ztimson/template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33e4ee35c7 | ||
|
|
36f954bb6b | ||
|
|
a1aa158264 |
Generated
+12
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@ztimson/zim-utils",
|
||||
"version": "0.4.8",
|
||||
"version": "0.4.11",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@ztimson/zim-utils",
|
||||
"version": "0.4.8",
|
||||
"version": "0.4.11",
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@@ -14,6 +14,7 @@
|
||||
"adm-zip": "^0.5.18",
|
||||
"lzma1": "^0.3.0",
|
||||
"tar": "^7.4.3",
|
||||
"undici": "^8.11.2",
|
||||
"zstd-codec": "^0.1.5"
|
||||
}
|
||||
},
|
||||
@@ -106,6 +107,15 @@
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/undici": {
|
||||
"version": "8.11.2",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-8.11.2.tgz",
|
||||
"integrity": "sha512-u4UB2/IrKdU6lFxumHmmo1a3fCQO5tzQllRorfoRS63txhrB7xTpSn1PftwC4qEHkOaqP95fCWW4lJzwErwzhQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=22.19.0"
|
||||
}
|
||||
},
|
||||
"node_modules/var-persist": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/var-persist/-/var-persist-1.0.1.tgz",
|
||||
|
||||
+4
-3
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/zim-utils",
|
||||
"version": "0.4.9",
|
||||
"version": "0.4.12",
|
||||
"description": "Native, dependency-light ZIM archive reader/searcher and Kiwix catalog downloader for Node.js",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
@@ -16,9 +16,10 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@ztimson/utils": "^0.30.9",
|
||||
"adm-zip": "^0.5.18",
|
||||
"lzma1": "^0.3.0",
|
||||
"tar": "^7.4.3",
|
||||
"zstd-codec": "^0.1.5",
|
||||
"adm-zip": "^0.5.18"
|
||||
"undici": "^8.11.2",
|
||||
"zstd-codec": "^0.1.5"
|
||||
}
|
||||
}
|
||||
|
||||
+14
-4
@@ -5,6 +5,9 @@ import {Readable} from 'node:stream';
|
||||
import {PromiseProgress} from '@ztimson/utils';
|
||||
import {KiwixServer} from './server.js';
|
||||
import {zimCatalog, zimCatalogInfo, CATALOG_URL} from './catalog.js';
|
||||
import {Agent} from 'undici';
|
||||
|
||||
const agent = new Agent({headersTimeout: 0, bodyTimeout: 0});
|
||||
|
||||
const VISIBLE_TIMEOUT = 5_000;
|
||||
const VISIBLE_POLL_INTERVAL = 200;
|
||||
@@ -65,13 +68,13 @@ export class ZimManager {
|
||||
|
||||
/** Resolves a `.meta4` metalink URL down to the real mirror `.zim` download URL. */
|
||||
async #resolveUrl(url) {
|
||||
const head = await fetch(url);
|
||||
const head = await fetch(url, {dispatcher: agent});
|
||||
const ct = head.headers.get('content-type') || '';
|
||||
if (!ct.includes('metalink') && !url.endsWith('.meta4')) return {res: head, url};
|
||||
const meta = await head.text();
|
||||
const m = meta.match(/<url[^>]*>([^<]+\.zim)<\/url>/);
|
||||
if (!m) throw new Error('Could not resolve metalink mirror');
|
||||
const res = await fetch(m[1]);
|
||||
const res = await fetch(m[1], {dispatcher: agent});
|
||||
return {res, url: m[1]};
|
||||
}
|
||||
|
||||
@@ -80,10 +83,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 +145,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