generated from ztimson/template
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f8637f913 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/zim-utils",
|
"name": "@ztimson/zim-utils",
|
||||||
"version": "0.3.4",
|
"version": "0.3.5",
|
||||||
"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",
|
||||||
|
|||||||
@@ -90,9 +90,9 @@ export class ZimManager {
|
|||||||
|
|
||||||
/** Downloads/updates a single ZIM by direct href, matching against any existing local copy by name. */
|
/** Downloads/updates a single ZIM by direct href, matching against any existing local copy by name. */
|
||||||
async download(href, {force = false} = {}) {
|
async download(href, {force = false} = {}) {
|
||||||
await fs.promises.mkdir(this.#dir, {recursive: true});
|
|
||||||
const {url: finalUrl} = await this.#resolveUrl(href);
|
const {url: finalUrl} = await this.#resolveUrl(href);
|
||||||
const filename = path.basename(new URL(finalUrl).pathname).replace(/\.meta4$/i, '');
|
const filename = path.basename(new URL(finalUrl).pathname).replace(/\.meta4$/i, '');
|
||||||
|
await fs.promises.mkdir(this.#dir, {recursive: true});
|
||||||
const name = filename.replace(/\.zim$/i, '').replace(/_\d{4}-\d{2}(?:_\d+)?$/, '');
|
const name = filename.replace(/\.zim$/i, '').replace(/_\d{4}-\d{2}(?:_\d+)?$/, '');
|
||||||
|
|
||||||
const local = await this.list();
|
const local = await this.list();
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ function findFreePort() {
|
|||||||
|
|
||||||
/** Owns a kiwix-serve process's full lifecycle: library.xml, start/stop/reload, content + search access. */
|
/** Owns a kiwix-serve process's full lifecycle: library.xml, start/stop/reload, content + search access. */
|
||||||
export class KiwixServer {
|
export class KiwixServer {
|
||||||
|
static #empty = '<?xml version="1.0" encoding="UTF-8" ?>\n<library version="20110515"></library>\n';
|
||||||
|
|
||||||
#dir;
|
#dir;
|
||||||
#host;
|
#host;
|
||||||
#port;
|
#port;
|
||||||
@@ -49,6 +51,12 @@ export class KiwixServer {
|
|||||||
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;
|
this.#remote = url ? url.replace(/\/$/, '') : null;
|
||||||
|
if (!this.#remote) this.#ensureLocalStore();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ensureLocalStore() {
|
||||||
|
fs.mkdirSync(this.#dir, {recursive: true});
|
||||||
|
if (!fs.existsSync(this.#libraryPath)) fs.writeFileSync(this.#libraryPath, KiwixServer.#empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
#assertRunning() {
|
#assertRunning() {
|
||||||
@@ -61,15 +69,22 @@ export class KiwixServer {
|
|||||||
|
|
||||||
/** Reads library.xml from disk if we own the server, or over HTTP if attached to a remote one. */
|
/** Reads library.xml from disk if we own the server, or over HTTP if attached to a remote one. */
|
||||||
async #fetchLibraryXml() {
|
async #fetchLibraryXml() {
|
||||||
if (this.#remote) return (await fetch(`${this.#remote}/library.xml`).catch(() => null))?.text?.() ?? '';
|
if (!this.#remote) return fs.promises.readFile(this.#libraryPath, 'utf8').catch(() => '');
|
||||||
return fs.promises.readFile(this.#libraryPath, 'utf8').catch(() => '');
|
try {
|
||||||
|
const res = await fetch(`${this.#remote}/library.xml`);
|
||||||
|
return res.ok ? await res.text() : '';
|
||||||
|
} catch {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Rebuilds library.xml from scratch by scanning `dir` for .zim files - no-op if attached to a remote server. */
|
/** 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;
|
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)]);
|
const files = await this.#zimFiles();
|
||||||
|
if (!files.length) return fs.promises.writeFile(this.#libraryPath, KiwixServer.#empty);
|
||||||
|
for (const f of files) await execFileAsync(this.#bin('kiwix-manage'), [this.#libraryPath, 'add', path.join(this.#dir, f)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async #waitUntilReady() {
|
async #waitUntilReady() {
|
||||||
@@ -133,6 +148,7 @@ export class KiwixServer {
|
|||||||
async list() {
|
async list() {
|
||||||
this.#assertRunning();
|
this.#assertRunning();
|
||||||
const xml = await this.#fetchLibraryXml();
|
const xml = await this.#fetchLibraryXml();
|
||||||
|
if (!xml) return [];
|
||||||
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(';');
|
||||||
|
|||||||
Reference in New Issue
Block a user