#!/usr/bin/env node "use strict"; /** * Laadt de plugin zoals LNReader dat doet: custom require-allowlist, * `new Function`, en daarna popularNovels / searchNovels / parseNovel / * parseChapter. De index komt van een lokale HTTP-server (testdata of * public/). parseChapter gebruikt een mock van Arctic Shift, plus * optioneel een live verzoek als HFY_LIVE=1. */ const fs = require("fs"); const http = require("http"); const path = require("path"); const ROOT = path.resolve(__dirname, ".."); const PLUGIN = fs.readFileSync(path.join(ROOT, "plugin", "hfy-novels.js"), "utf8"); const NovelStatus = { Completed: "Completed", Ongoing: "Ongoing", OnHiatus: "On Hiatus", Unknown: "Unknown", }; const FilterTypes = { Picker: "Picker", Switch: "Switch", CheckboxGroup: "CheckboxGroup" }; function mimeFor(file, asImg) { if (file.endsWith(".svg")) return asImg ? "image/svg+xml" : "text/plain; charset=utf-8"; if (file.endsWith(".json")) return "text/plain; charset=utf-8"; // BlueGit serveert raw ook als text/plain if (file.endsWith(".js")) return "text/plain; charset=utf-8"; if (file.endsWith(".png")) return "image/png"; return "application/octet-stream"; } function startServer(rootDir, chapterHandler) { return new Promise((resolve, reject) => { const server = http.createServer((req, res) => { const url = new URL(req.url, "http://127.0.0.1"); if (url.pathname.startsWith("/api/posts/ids")) { return chapterHandler(url, res); } // ?rev=master zoals BlueGit raw-URLs; voor het bestand negeren we de query. let rel = decodeURIComponent(url.pathname).replace(/^\/+/, ""); const file = path.join(rootDir, rel); if (!file.startsWith(path.resolve(rootDir))) { res.writeHead(403); res.end("forbidden"); return; } fs.readFile(file, (err, data) => { if (err) { res.writeHead(404, { "Content-Type": "text/plain" }); res.end("404 " + rel); return; } const asImg = url.searchParams.get("as") === "img"; res.writeHead(200, { "Content-Type": mimeFor(file, asImg) }); res.end(data); }); }); server.listen(0, "127.0.0.1", () => { const { port } = server.address(); resolve({ server, port, url: "http://127.0.0.1:" + port }); }); server.on("error", reject); }); } function defaultChapterHandler(url, res) { const ids = url.searchParams.get("ids") || ""; const id = ids.split(",")[0]; const fixture = { data: [ { id, title: "The Last Terran - Part 1", author: "WriterOne", selftext: "It begins.\n\n[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)", selftext_html: "
It begins.
", permalink: "/r/HFY/comments/" + id + "/", }, ], }; res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify(fixture)); } async function fetchApi(url, init) { const res = await fetch(url, init); return { ok: res.ok, status: res.status, headers: res.headers, async text() { return await res.text(); }, async json() { const text = await res.text(); return JSON.parse(text); }, url: res.url, }; } function loadPlugin(storageMap) { const allow = { "@libs/fetch": { fetchApi, fetchText: async (u, i) => (await fetchApi(u, i)).text() }, "@libs/filterInputs": { FilterTypes }, "@libs/defaultCover": { defaultCover: "https://example.invalid/cover.png" }, "@libs/novelStatus": { NovelStatus }, "@libs/storage": { storage: { get(key) { return storageMap[key]; }, set(key, value) { storageMap[key] = value; }, }, }, }; const pluginModule = { exports: {} }; const fn = new Function("exports", "require", "module", PLUGIN); fn(pluginModule.exports, (name) => { if (!allow[name]) { throw new Error("blocked require: " + name); } return allow[name]; }, pluginModule); const plugin = pluginModule.exports.default; if (!plugin) { throw new Error("geen default export"); } for (const method of ["popularNovels", "searchNovels", "parseNovel", "parseChapter"]) { if (typeof plugin[method] !== "function") { throw new Error("plugin mist " + method); } } return plugin; } function assert(cond, msg) { if (!cond) { throw new Error("FAIL: " + msg); } } async function main() { const fixtureRoot = path.join(ROOT, "testdata", "index-fixture"); const { server, url } = await startServer(fixtureRoot, defaultChapterHandler); const failures = []; try { const storageMap = { indexBaseUrl: url, indexRev: "", // geen ?rev= nodig op de testhost chapterSourceUrl: url, }; const plugin = loadPlugin(storageMap); assert(plugin.id === "hfyNovels", "id"); assert(plugin.name === "HFY Novels", "name"); assert(plugin.version === "0.1.0", "version"); assert(plugin.site === "https://www.reddit.com/r/HFY/", "site"); assert(plugin.pluginSettings && plugin.pluginSettings.indexBaseUrl, "pluginSettings"); const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} }); assert(Array.isArray(popular) && popular.length >= 1, "popularNovels geeft items"); assert(popular[0].name && popular[0].path, "NovelItem heeft name+path"); assert(popular[0].cover && popular[0].cover.indexOf("/covers/") !== -1, "cover-URL wijst naar covers/"); const coverIsSvg = /\.svg(\?|$)/i.test(popular[0].cover); if (coverIsSvg) { assert(popular[0].cover.indexOf("as=img") !== -1, "SVG-cover heeft as=img"); } const coverRes = await fetch(popular[0].cover); assert(coverRes.ok, "cover is ophaalbaar"); const coverBuf = Buffer.from(await coverRes.arrayBuffer()); if (coverIsSvg) { const coverBody = coverBuf.toString("utf8"); assert(coverBody.indexOf("