generated from ztimson/template
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a2f17bfe9 | |||
| a2bde0b5cd |
@@ -87,6 +87,7 @@ await server.stop(); // Kill server
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### List Local ZIMs
|
#### List Local ZIMs
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const local = await server.list();
|
const local = await server.list();
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/zim-utils",
|
"name": "@ztimson/zim-utils",
|
||||||
"version": "0.3.1",
|
"version": "0.3.3",
|
||||||
"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",
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {zimCatalog, zimCatalogInfo, CATALOG_URL} from './catalog.js';
|
|||||||
export class ZimManager {
|
export class ZimManager {
|
||||||
#catalogUrl;
|
#catalogUrl;
|
||||||
#dir;
|
#dir;
|
||||||
server;
|
#server;
|
||||||
#ownsServer;
|
#ownsServer;
|
||||||
|
|
||||||
/** @param {{catalog?: string, server?: KiwixServer, port?: number, host?: string, binDir?: string, url?: string}} [opts]
|
/** @param {{catalog?: string, server?: KiwixServer, port?: number, host?: string, binDir?: string, url?: string}} [opts]
|
||||||
@@ -21,15 +21,15 @@ export class ZimManager {
|
|||||||
this.#catalogUrl = catalog;
|
this.#catalogUrl = catalog;
|
||||||
this.#dir = dir;
|
this.#dir = dir;
|
||||||
this.#ownsServer = !server;
|
this.#ownsServer = !server;
|
||||||
this.server = server ?? new KiwixServer(dir, {port, host, binDir, url});
|
this.#server = server ?? new KiwixServer(dir, {port, host, binDir, url});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The KiwixServer backing this manager - reuse it directly for content/search access, or pass into another ZimManager. */
|
/** The KiwixServer backing this manager - reuse it directly for content/search access, or pass into another ZimManager. */
|
||||||
get server() { return this.server; }
|
get server() { return this.#server; }
|
||||||
|
|
||||||
async #ensureServer() {
|
async #ensureServer() {
|
||||||
if (!this.server.running) await this.server.start();
|
if (!this.#server.running) await this.#server.start();
|
||||||
return this.server;
|
return this.#server;
|
||||||
}
|
}
|
||||||
|
|
||||||
async #download(url, destPath) {
|
async #download(url, destPath) {
|
||||||
@@ -74,7 +74,7 @@ export class ZimManager {
|
|||||||
|
|
||||||
/** Stops the internally-owned KiwixServer, if this manager created its own (no-op if one was passed in). */
|
/** Stops the internally-owned KiwixServer, if this manager created its own (no-op if one was passed in). */
|
||||||
async close() {
|
async close() {
|
||||||
if (this.#ownsServer) await this.server.stop();
|
if (this.#ownsServer) await this.#server.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(nameOrFile) {
|
async delete(nameOrFile) {
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import net from 'node:net';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import {fileURLToPath} from 'node:url';
|
import {fileURLToPath} from 'node:url';
|
||||||
import {decodeHtml, fromXml} from '@ztimson/utils';
|
import {fromXml} from '@ztimson/utils';
|
||||||
import {fuzzyMatch, weightedScore} from './utils.js';
|
|
||||||
|
|
||||||
const execFileAsync = promisify(execFile);
|
const execFileAsync = promisify(execFile);
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
@@ -180,28 +179,21 @@ export class KiwixServer {
|
|||||||
return {mimetype: res.headers.get('content-type'), data: Buffer.from(await res.arrayBuffer())};
|
return {mimetype: res.headers.get('content-type'), data: Buffer.from(await res.arrayBuffer())};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Fulltext search across every local ZIM via kiwix-serve's own xapian index */
|
||||||
* Two-pass fulltext search across every local ZIM: xapian prefilter (kiwix-serve's
|
|
||||||
* own index), then fuzzy-reranked by title so the strongest matches surface first.
|
|
||||||
* Returns a flat array matching the catalog/list shape: {id, title, name, category, ..., href, score}
|
|
||||||
*/
|
|
||||||
async search(terms, limit = 20) {
|
async search(terms, limit = 20) {
|
||||||
this.#assertRunning();
|
this.#assertRunning();
|
||||||
const termList = String(terms).split(',').map(t => t.trim()).filter(Boolean);
|
const termList = String(terms).split(/[,\s]+/).map(t => t.trim()).filter(Boolean);
|
||||||
if (!termList.length) return [];
|
if (!termList.length) return [];
|
||||||
|
|
||||||
const params = new URLSearchParams({pattern: termList.join(' '), format: 'xml', pageLength: String(limit)});
|
const params = new URLSearchParams({pattern: termList.join(' '), format: 'xml', pageLength: String(limit)});
|
||||||
const res = await fetch(`${this.baseUrl}/search?${params}`);
|
const res = await fetch(`${this.baseUrl}/search?${params}`);
|
||||||
if (!res.ok) return [];
|
if (!res.ok) return [];
|
||||||
|
|
||||||
const xml = await res.text();
|
const found = fromXml(await res.text())?.rss?.channel?.item || [];
|
||||||
let found = fromXml(xml);
|
|
||||||
found = found?.rss?.channel?.item || [];
|
|
||||||
|
|
||||||
const books = await this.list();
|
const books = await this.list();
|
||||||
const bookMap = new Map(books.map(b => [b.title, b]));
|
const bookMap = new Map(books.map(b => [b.title, b]));
|
||||||
|
|
||||||
const enriched = found.map(hit => {
|
return found.map(hit => {
|
||||||
const book = bookMap.get(hit.book.title);
|
const book = bookMap.get(hit.book.title);
|
||||||
if (!book) return null;
|
if (!book) return null;
|
||||||
const prefix = `/content/${book.href}/`;
|
const prefix = `/content/${book.href}/`;
|
||||||
@@ -216,10 +208,8 @@ export class KiwixServer {
|
|||||||
icon: book.icon,
|
icon: book.icon,
|
||||||
viewer: this.baseUrl + hit.link,
|
viewer: this.baseUrl + hit.link,
|
||||||
summary: hit.description,
|
summary: hit.description,
|
||||||
score: weightedScore(hit.title, termList) + weightedScore(hit.description, termList),
|
score: +hit.score || 0,
|
||||||
};
|
};
|
||||||
}).filter(hit => !!hit && hit.score > 0);
|
}).filter(Boolean);
|
||||||
|
|
||||||
return enriched.toSorted((a, b) => b.score - a.score).slice(0, limit);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,10 +40,3 @@ export function fuzzyMatch(target, ...terms) {
|
|||||||
similarities,
|
similarities,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function weightedScore(text, termList) {
|
|
||||||
if (!text) return 0;
|
|
||||||
const covered = termList.reduce((sum, t) => sum + t.length, 0);
|
|
||||||
const coverage = Math.min(1, covered / text.length);
|
|
||||||
return fuzzyMatch(text, ...termList).max * coverage;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user