Files
zim-utils/src/reader.js
ztimson bc90557de7
Some checks failed
Publish Library / Build NPM Project (push) Failing after 21s
Publish Library / Tag Version (push) Has been skipped
use kiwix-serach as prefilter before fuzzy ranking instead of building index
2026-08-24 11:02:47 -04:00

290 lines
10 KiB
JavaScript

'use strict';
import fs from 'node:fs';
import {decompressPool} from './decompress.js';
import {fuzzyMatch, titleFromUrl, kiwixSearch} from './utils.js';
const HEADER_SIZE = 80;
const NS_CONTENT = 'C';
const NS_METADATA = 'M';
const TITLE_SENTINEL = 0xffffffffffffffffn; // Indicator -> ZIM v6+ archives with no title
const DEFAULT_CLUSTER_CACHE_MAX = 32;
const DEFAULT_CLUSTER_TTL = 60_000;
/** Native, dependency-light reader for .zim archives. Supports zstd & LZMA cluster compression. */
export class ZimReader {
#fd = null;
#header = null;
#mimeTypes = [];
#hasTitleListing = false;
#clusterCache = new Map(); // clusterNumber -> {data, extended, timer}
#pending = new Map(); // clusterNumber -> Promise, dedupes concurrent misses
#clusterCacheMax;
#clusterTTL;
get articleCount() { return this.#header?.articleCount ?? 0; }
get mediaCount() { return this.#header?.clusterCount ?? 0; }
/** @param {{clusterCacheMax?: number, clusterTTL?: number}} [opts] clusterTTL in ms; 0/null disables idle eviction. */
constructor(path, {clusterCacheMax = DEFAULT_CLUSTER_CACHE_MAX, clusterTTL = DEFAULT_CLUSTER_TTL} = {}) {
this.path = path;
this.#clusterCacheMax = clusterCacheMax;
this.#clusterTTL = clusterTTL;
}
/** Binary search the URL pointer list for namespace+url. */
async #findByUrl(url, namespace) {
const key = namespace + url;
let lo = 0, hi = this.#header.articleCount - 1;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
const dirent = await this.#readDirent(await this.#ptr64(this.#header.urlPtrPos, mid));
const dirKey = dirent.namespace + dirent.url;
const cmp = key < dirKey ? -1 : key > dirKey ? 1 : 0;
if (cmp === 0) return dirent;
if (cmp < 0) hi = mid - 1; else lo = mid + 1;
}
return null;
}
/** Binary search the title index for an exact title match. */
async #findByTitle(title) {
if (!this.#hasTitleListing) return null;
const q = title.toLowerCase();
let lo = 0, hi = this.#header.articleCount - 1;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
const urlIdx = (await this.#read(this.#header.titlePtrPos + mid * 4, 4)).readUInt32LE(0);
const dirent = await this.#readDirent(await this.#ptr64(this.#header.urlPtrPos, urlIdx));
const t = dirent.title.toLowerCase();
if (t === q) return dirent;
if (t < q) lo = mid + 1; else hi = mid - 1;
}
return null;
}
/** Resets a cluster's idle-eviction timer. No-op when TTL disabled. */
#touch(clusterNumber, entry) {
if (!this.#clusterTTL) return;
clearTimeout(entry.timer);
entry.timer = setTimeout(() => this.#clusterCache.delete(clusterNumber), this.#clusterTTL).unref();
}
/** Fetches + decompresses a cluster exactly once, offloading decompression to the worker pool. */
async #loadCluster(clusterNumber) {
const start = await this.#ptr64(this.#header.clusterPtrPos, clusterNumber);
const isLast = clusterNumber === this.#header.clusterCount - 1;
const end = isLast
? (await fs.promises.stat(this.path)).size
: await this.#ptr64(this.#header.clusterPtrPos, clusterNumber + 1);
const raw = await this.#read(start, end - start);
const compType = raw[0] & 0x0f;
const extended = (raw[0] & 0x10) !== 0;
const body = raw.subarray(1);
let data;
if (compType <= 1) data = Buffer.from(body);
else if (compType === 4 || compType === 5) data = await decompressPool.run({compType, body});
else throw new Error(`Unsupported cluster compression type: ${compType}`);
if (!data) throw new Error(`Cluster ${clusterNumber} failed to decompress (compType ${compType})`);
return {data, extended};
}
async #getBlob(clusterNumber, blobNumber) {
let entry = this.#clusterCache.get(clusterNumber);
if (!entry) {
let pending = this.#pending.get(clusterNumber);
if (!pending) {
pending = this.#loadCluster(clusterNumber);
this.#pending.set(clusterNumber, pending);
}
entry = await pending;
this.#pending.delete(clusterNumber);
this.#clusterCache.set(clusterNumber, entry);
if (this.#clusterCache.size > this.#clusterCacheMax) {
const oldestKey = this.#clusterCache.keys().next().value;
clearTimeout(this.#clusterCache.get(oldestKey)?.timer);
this.#clusterCache.delete(oldestKey);
}
}
this.#touch(clusterNumber, entry);
const readPtr = i => entry.extended ? Number(entry.data.readBigUInt64LE(i * 8)) : entry.data.readUInt32LE(i * 4);
return entry.data.subarray(readPtr(blobNumber), readPtr(blobNumber + 1));
}
async #icon(size = 48) {
const page = await this.readPage(`Illustration_${size}x${size}@1`, NS_METADATA)
|| await this.readPage('Favicon', NS_METADATA);
if (!page) return null;
return `data:${page.mimetype};base64,${page.data.toString('base64')}`;
}
async #ptr64(base, index) {
return Number((await this.#read(base + index * 8, 8)).readBigUInt64LE(0));
}
async #read(pos, length) {
const buf = Buffer.alloc(length);
await this.#fd.read(buf, 0, length, pos);
return buf;
}
async #readHeader() {
const b = await this.#read(0, HEADER_SIZE);
const titlePtrRaw = b.readBigUInt64LE(40);
this.#hasTitleListing = titlePtrRaw !== TITLE_SENTINEL;
this.#header = {
articleCount: b.readUInt32LE(24),
clusterCount: b.readUInt32LE(28),
urlPtrPos: Number(b.readBigUInt64LE(32)),
titlePtrPos: this.#hasTitleListing ? Number(titlePtrRaw) : null,
clusterPtrPos: Number(b.readBigUInt64LE(48)),
mimeListPos: Number(b.readBigUInt64LE(56)),
mainPage: b.readUInt32LE(64),
};
}
async #readMimeTypes() {
let pos = this.#header.mimeListPos, str = '';
for (;;) {
str += (await this.#read(pos, 1024)).toString('binary');
const end = str.indexOf('\0\0');
if (end !== -1) { str = str.slice(0, end + 1); break; }
pos += 1024;
}
this.#mimeTypes = str.split('\0').filter(Boolean);
}
/** Directory entry (article record) at byte `offset`, growing the read window as needed. */
async #readDirent(offset) {
for (let size = 512; ; size *= 2) {
const buf = await this.#read(offset, size);
let o = 0;
const mimetype = buf.readUInt16LE(o); o += 2;
o += 1; // extraLen, unused
const namespace = String.fromCharCode(buf.readUInt8(o)); o += 1;
o += 4; // revision, unused
let redirectIndex = null, cluster = null, blob = null;
if (mimetype === 0xffff) { redirectIndex = buf.readUInt32LE(o); o += 4; }
else { cluster = buf.readUInt32LE(o); o += 4; blob = buf.readUInt32LE(o); o += 4; }
const urlEnd = buf.indexOf(0, o);
if (urlEnd === -1) continue;
const titleEnd = buf.indexOf(0, urlEnd + 1);
if (titleEnd === -1) continue;
const url = buf.toString('utf8', o, urlEnd);
const title = buf.toString('utf8', urlEnd + 1, titleEnd) || url;
return {mimetype, namespace, redirectIndex, cluster, blob, url, title};
}
}
async #resolveRedirect(dirent) {
if (dirent.mimetype !== 0xffff) return dirent;
return this.#readDirent(await this.#ptr64(this.#header.urlPtrPos, dirent.redirectIndex));
}
async close() {
if (this.#fd) await this.#fd.close();
this.#fd = null;
for (const entry of this.#clusterCache.values()) clearTimeout(entry.timer);
this.#clusterCache.clear();
this.#pending.clear();
}
/** Deletes the zim archive. Safe to call on unopened readers. */
async delete() {
await this.close();
await fs.promises.rm(this.path, {force: true});
}
/** Reads an 'M' namespace metadata value (e.g. Name, Date, Title). Returns null if missing. */
async metadata(key) {
const get = async key => {
const page = await this.readPage(key, NS_METADATA);
return page ? page.data.toString('utf8') : null;
};
if(key) return get(key);
const [title, creator, publisher, date, description, language, name, tags] = await Promise.all(
['Title', 'Creator', 'Publisher', 'Date', 'Description', 'Language', 'Name', 'Tags'].map(get)
);
return {
title,
updated: date ? new Date(date) : null,
summary: description,
language,
name,
category: tags ? tags.split(';')[0] || '' : '',
tags: tags ? tags.split(';') : [],
author: creator,
publisher,
articleCount: this.articleCount,
mediaCount: this.mediaCount,
sizeMb: +((await fs.promises.stat(this.path)).size / 1024 / 1024).toFixed(1),
icon: await this.#icon(),
};
}
/** Opens the archive and parses its header + mimetype list. */
async open() {
this.#fd = await fs.promises.open(this.path, 'r');
try {
await this.#readHeader();
await this.#readMimeTypes();
} catch (e) {
await this.#fd.close();
this.#fd = null;
throw e;
}
return this;
}
/** Read a page's content by URL. Returns `{mimetype, data}` or `null` if not found. */
async readPage(url, namespace = NS_CONTENT) {
let dirent = await this.#findByUrl(url, namespace);
if (!dirent) return null;
dirent = await this.#resolveRedirect(dirent);
const data = await this.#getBlob(dirent.cluster, dirent.blob);
return {mimetype: this.#mimeTypes[dirent.mimetype] || 'application/octet-stream', data};
}
/** Reads the archive's designated main/landing page, if one is set. */
async mainPage() {
if (this.#header.mainPage === 0xffffffff) return null;
let dirent = await this.#readDirent(await this.#ptr64(this.#header.urlPtrPos, this.#header.mainPage));
dirent = await this.#resolveRedirect(dirent);
const data = await this.#getBlob(dirent.cluster, dirent.blob);
return {mimetype: this.#mimeTypes[dirent.mimetype] || 'application/octet-stream', data, url: dirent.url, namespace: dirent.namespace};
}
/**
* Fuzzy-ranked title search. Accepts comma-separated `terms` the same way the
* catalog search does. Uses kiwix-search's embedded fulltext index as a prefilter
* to narrow candidates before fuzzy scoring.
*/
async search(terms, {limit = 20, htmlOnly = true} = {}) {
const termList = String(terms).split(',').map(t => t.trim()).filter(Boolean);
if (!termList.length) return [];
const titles = await kiwixSearch(this.path, termList.join(' '));
const candidates = (await Promise.all(titles.map(t => this.#findByTitle(t)))).filter(Boolean);
const scored = [];
for (const dirent of candidates) {
if (htmlOnly && !(this.#mimeTypes[dirent.mimetype] || '').startsWith('text/html')) continue;
const urlTitle = titleFromUrl(dirent.url);
const titleScore = fuzzyMatch(dirent.title, ...termList).max;
const urlScore = fuzzyMatch(urlTitle, ...termList).max;
scored.push({url: dirent.url, title: dirent.title.length > urlTitle.length ? dirent.title : urlTitle, namespace: NS_CONTENT, score: Math.max(titleScore, urlScore)});
}
const {summary, mediaCount, articleCount, sizeMb, ...meta} = await this.metadata();
return scored.filter(a => a.score > 0).toSorted((a, b) => b.score - a.score).slice(0, limit).map(a => ({...meta, ...a}));
}
}