From d95d19cd3828ea3791a67f0ab8c1e7411e1008d9 Mon Sep 17 00:00:00 2001 From: Marco D'Aleo Date: Fri, 13 Nov 2026 07:44:48 +0000 Subject: [PATCH] Give the plugin its own CalVer The plugin ships inside the server image but shared the app's CalVer, so an app-only release marked every paired reader as needing a plugin update. It also let the version drift: the 2026.10.09 release still reported 2026.09.25. - read the plugin version from const.lua at startup so LATEST_PLUGIN_VERSION cannot diverge from the packaged plugin - keep MIN_COVER_PLUGIN_VERSION and MIN_INCREMENTAL_PLUGIN_VERSION as independent compatibility floors instead of copies of the app version - add scripts/version.mjs (check by default, --write to sync) with a dependency-free zip reader; npm test now fails on a drifted version string or a stale public/kovi-plugin.zip - bump the app version to 2026.11.13 --- lib/covers.mjs | 2 +- package.json | 6 ++- scripts/version.mjs | 116 +++++++++++++++++++++++++++++++++++++++++ scripts/zip.mjs | 39 ++++++++++++++ server.mjs | 9 +++- tests/plugin.test.mjs | 7 ++- tests/version.test.mjs | 13 +++++ 7 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 scripts/version.mjs create mode 100644 scripts/zip.mjs create mode 100644 tests/version.test.mjs diff --git a/lib/covers.mjs b/lib/covers.mjs index d51673d..5e0e701 100644 --- a/lib/covers.mjs +++ b/lib/covers.mjs @@ -5,7 +5,7 @@ import { cleanText, nowIso, randomId } from './security.mjs'; import { info, warn } from './logger.mjs'; import { fetchCoverWithCalibre } from './calibre.mjs'; -const VERSION = '2026.09.25'; +const VERSION = '2026.11.13'; const MATCH_THRESHOLD = 0.72; const NETWORK_RETRIES = 2; const COVER_WORKER_CONCURRENCY = Math.max(1, Math.min(4, Number.parseInt(process.env.COVER_WORKER_CONCURRENCY || '2', 10) || 2)); diff --git a/package.json b/package.json index a38b858..383aa65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kovi", - "version": "2026.09.25", + "version": "2026.11.13", "private": true, "license": "GPL-3.0-only", "type": "module", @@ -12,6 +12,8 @@ "start": "node server.mjs", "dev": "node --watch server.mjs", "test": "node --test tests/*.test.mjs", - "fixture": "node scripts/make-fixture.mjs" + "fixture": "node scripts/make-fixture.mjs", + "version:check": "node scripts/version.mjs", + "version:write": "node scripts/version.mjs --write" } } diff --git a/scripts/version.mjs b/scripts/version.mjs new file mode 100644 index 0000000..d891cea --- /dev/null +++ b/scripts/version.mjs @@ -0,0 +1,116 @@ +#!/usr/bin/env node +import fs from 'node:fs'; +import path from 'node:path'; +import { spawnSync } from 'node:child_process'; +import { fileURLToPath, pathToFileURL } from 'node:url'; +import { readZipEntries } from './zip.mjs'; + +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); +const PLUGIN_DIR = path.join(ROOT, 'plugins', 'kovi.koplugin'); +const PLUGIN_ZIP = path.join(ROOT, 'public', 'kovi-plugin.zip'); +const CONST_FILE = path.join(PLUGIN_DIR, 'const.lua'); +const META_FILE = path.join(PLUGIN_DIR, '_meta.lua'); +const SERVER_FILE = path.join(ROOT, 'server.mjs'); +const COVERS_FILE = path.join(ROOT, 'lib', 'covers.mjs'); +const PACKAGE_FILE = path.join(ROOT, 'package.json'); +const CALVER = /^\d{4}\.\d{2}\.\d{2}$/; +const PLUGIN_VERSION_PATTERN = /VERSION\s*=\s*"([^"]+)"/; +const META_VERSION_PATTERN = /\(v[\d.]+\)/; + +const read = (file) => fs.readFileSync(file, 'utf8'); +const firstGroup = (source, pattern) => (pattern.exec(source) || [])[1]; +const zipName = (name) => name.slice(name.indexOf('/') + 1); + +export function versions() { + return { + app: JSON.parse(read(PACKAGE_FILE)).version, + plugin: firstGroup(read(CONST_FILE), PLUGIN_VERSION_PATTERN), + }; +} + +function versionParts(value) { + return String(value || '0').split('.').map((part) => Number.parseInt(part, 10) || 0).slice(0, 3); +} + +function versionAtLeast(value, minimum) { + const a = versionParts(value); + const b = versionParts(minimum); + for (let i = 0; i < 3; i += 1) { + if ((a[i] || 0) > (b[i] || 0)) return true; + if ((a[i] || 0) < (b[i] || 0)) return false; + } + return true; +} + +function checkZip(problems) { + const entries = new Map([...readZipEntries(PLUGIN_ZIP)] + .filter(([name]) => !name.endsWith('/')) + .map(([name, data]) => [zipName(name), data])); + const source = new Map(fs.readdirSync(PLUGIN_DIR) + .filter((name) => fs.statSync(path.join(PLUGIN_DIR, name)).isFile()) + .map((name) => [name, fs.readFileSync(path.join(PLUGIN_DIR, name))])); + for (const [name, data] of source) { + const packaged = entries.get(name); + if (!packaged) problems.push(`public/kovi-plugin.zip is missing ${name}`); + else if (!packaged.equals(data)) problems.push(`public/kovi-plugin.zip has a stale ${name}`); + } + for (const name of entries.keys()) { + if (!source.has(name)) problems.push(`public/kovi-plugin.zip contains stale ${name}`); + } +} + +export function check() { + const problems = []; + const { app, plugin } = versions(); + const server = read(SERVER_FILE); + if (!CALVER.test(app || '')) problems.push(`package.json version "${app}" is not CalVer`); + if (!CALVER.test(plugin || '')) problems.push(`plugins/kovi.koplugin/const.lua version "${plugin}" is not CalVer`); + + const serverVersion = firstGroup(server, /const VERSION = '([^']+)'/); + if (serverVersion !== app) problems.push(`server.mjs VERSION is ${serverVersion}, expected ${app} from package.json`); + const coversVersion = firstGroup(read(COVERS_FILE), /const VERSION = '([^']+)'/); + if (coversVersion !== app) problems.push(`lib/covers.mjs VERSION is ${coversVersion}, expected ${app} from package.json`); + + if (/const LATEST_PLUGIN_VERSION = ['"]/.test(server)) problems.push('server.mjs must derive LATEST_PLUGIN_VERSION from plugins/kovi.koplugin/const.lua'); + for (const name of ['MIN_COVER_PLUGIN_VERSION', 'MIN_INCREMENTAL_PLUGIN_VERSION']) { + const floor = firstGroup(server, new RegExp(`const ${name} = '([^']+)'`)); + if (!floor) problems.push(`server.mjs is missing ${name}`); + else if (!versionAtLeast(plugin, floor)) problems.push(`${name} (${floor}) is newer than the plugin version (${plugin})`); + } + + if (!read(META_FILE).includes(`(v${plugin})`)) problems.push(`plugins/kovi.koplugin/_meta.lua must describe version v${plugin}`); + checkZip(problems); + return problems; +} + +export function write() { + const { app, plugin } = versions(); + const sync = (file, pattern, value) => { + const source = read(file); + const next = source.replace(pattern, value); + if (next !== source) fs.writeFileSync(file, next); + }; + sync(SERVER_FILE, /const VERSION = '[^']+'/, `const VERSION = '${app}'`); + sync(COVERS_FILE, /const VERSION = '[^']+'/, `const VERSION = '${app}'`); + sync(META_FILE, META_VERSION_PATTERN, `(v${plugin})`); + const result = spawnSync('zip', ['-FSr', path.relative(path.join(ROOT, 'plugins'), PLUGIN_ZIP), 'kovi.koplugin'], { + cwd: path.join(ROOT, 'plugins'), + stdio: 'inherit', + }); + if (result.error) throw new Error(`Cannot rebuild public/kovi-plugin.zip: ${result.error.message}`); + if (result.status !== 0) throw new Error('zip failed while rebuilding public/kovi-plugin.zip'); +} + +const cli = process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url; +if (cli) { + if (process.argv.includes('--write')) write(); + const problems = check(); + if (problems.length) { + console.error('kovi version check failed:'); + for (const problem of problems) console.error(` - ${problem}`); + process.exitCode = 1; + } else { + const { app, plugin } = versions(); + console.log(`kovi versions OK · app ${app} · plugin ${plugin}`); + } +} diff --git a/scripts/zip.mjs b/scripts/zip.mjs new file mode 100644 index 0000000..900e1ad --- /dev/null +++ b/scripts/zip.mjs @@ -0,0 +1,39 @@ +import fs from 'node:fs'; +import { inflateRawSync } from 'node:zlib'; + +const EOCD_SIGNATURE = 0x06054b50; +const CENTRAL_SIGNATURE = 0x02014b50; +const LOCAL_SIGNATURE = 0x04034b50; +const EOCD_SIZE = 22; +const MAX_COMMENT_SIZE = 65535; + +// Minimal ZIP reader for the tracked plugin archive: enough to compare entry +// names and bytes without adding a dependency or requiring the zip binary. +export function readZipEntries(file) { + const buf = fs.readFileSync(file); + let eocd = -1; + for (let i = buf.length - EOCD_SIZE; i >= Math.max(0, buf.length - EOCD_SIZE - MAX_COMMENT_SIZE); i -= 1) { + if (buf.readUInt32LE(i) === EOCD_SIGNATURE) { eocd = i; break; } + } + if (eocd < 0) throw new Error(`Not a ZIP archive: ${file}`); + const count = buf.readUInt16LE(eocd + 10); + const entries = new Map(); + let offset = buf.readUInt32LE(eocd + 16); + for (let i = 0; i < count; i += 1) { + if (buf.readUInt32LE(offset) !== CENTRAL_SIGNATURE) throw new Error(`Corrupt ZIP central directory: ${file}`); + const method = buf.readUInt16LE(offset + 10); + const compressedSize = buf.readUInt32LE(offset + 20); + const nameLength = buf.readUInt16LE(offset + 28); + const extraLength = buf.readUInt16LE(offset + 30); + const commentLength = buf.readUInt16LE(offset + 32); + const localOffset = buf.readUInt32LE(offset + 42); + const name = buf.toString('utf8', offset + 46, offset + 46 + nameLength); + if (buf.readUInt32LE(localOffset) !== LOCAL_SIGNATURE) throw new Error(`Corrupt ZIP local header for ${name}: ${file}`); + const dataStart = localOffset + 30 + buf.readUInt16LE(localOffset + 26) + buf.readUInt16LE(localOffset + 28); + const stored = buf.subarray(dataStart, dataStart + compressedSize); + if (method !== 0 && method !== 8) throw new Error(`Unsupported ZIP compression for ${name}: ${file}`); + entries.set(name, method === 0 ? Buffer.from(stored) : inflateRawSync(stored)); + offset += 46 + nameLength + extraLength + commentLength; + } + return entries; +} diff --git a/server.mjs b/server.mjs index cfa4b77..a685b15 100644 --- a/server.mjs +++ b/server.mjs @@ -13,11 +13,11 @@ import { json, randomId, cleanText } from './lib/security.mjs'; import { info, warn, error as logError } from './lib/logger.mjs'; import { createBackupArchive, sendBooksCsv, sendHighlightsMarkdown } from './lib/exports.mjs'; -const VERSION = '2026.09.25'; -const LATEST_PLUGIN_VERSION = '2026.09.25'; +const VERSION = '2026.11.13'; const MIN_COVER_PLUGIN_VERSION = '2026.09.25'; const MIN_INCREMENTAL_PLUGIN_VERSION = '2026.09.25'; const ROOT = path.dirname(fileURLToPath(import.meta.url)); +const LATEST_PLUGIN_VERSION = readPluginVersion(path.join(ROOT, 'plugins', 'kovi.koplugin', 'const.lua')); const DATA_DIR = path.resolve(process.env.DATA_PATH || path.join(ROOT, 'data')); const PUBLIC_DIR = path.join(ROOT, 'public'); const UPLOAD_DIR = path.join(DATA_DIR, 'uploads'); @@ -41,6 +41,11 @@ const pairAttempts = new Map(); let lastPingLogAt = 0; let backupInFlight = false; +function readPluginVersion(file){ + const match = /VERSION\s*=\s*"([^"]+)"/.exec(fs.readFileSync(file, 'utf8')); + if (!match) throw new Error(`Cannot read the plugin version from ${file}.`); + return match[1]; +} function versionParts(value){return String(value||'0').split('.').map(x=>Number.parseInt(x,10)||0).slice(0,3)} function versionAtLeast(value,minimum){const a=versionParts(value),b=versionParts(minimum);for(let i=0;i<3;i++){if((a[i]||0)>(b[i]||0))return true;if((a[i]||0)<(b[i]||0))return false}return true} function deviceView(d){return {...d,needs_update:!versionAtLeast(d.plugin_version,LATEST_PLUGIN_VERSION),supports_cover_sync:versionAtLeast(d.plugin_version,MIN_COVER_PLUGIN_VERSION),supports_incremental_sync:versionAtLeast(d.plugin_version,MIN_INCREMENTAL_PLUGIN_VERSION),latest_plugin_version:LATEST_PLUGIN_VERSION,min_cover_plugin_version:MIN_COVER_PLUGIN_VERSION,min_incremental_plugin_version:MIN_INCREMENTAL_PLUGIN_VERSION}} diff --git a/tests/plugin.test.mjs b/tests/plugin.test.mjs index 29c034d..00f9f20 100644 --- a/tests/plugin.test.mjs +++ b/tests/plugin.test.mjs @@ -45,9 +45,12 @@ test('server detects plugins too old for embedded-cover requests',()=>{ assert.match(server,/update_recommended/); }); -test('packaged plugin version is aligned with kovi 2026.09.25',()=>{ +test('plugin version is declared once and surfaced in its metadata',()=>{ const constants=fs.readFileSync(new URL('../plugins/kovi.koplugin/const.lua', import.meta.url),'utf8'); - assert.match(constants,/2026\.09\.25/); + const meta=fs.readFileSync(new URL('../plugins/kovi.koplugin/_meta.lua', import.meta.url),'utf8'); + const version=/VERSION\s*=\s*"([^"]+)"/.exec(constants)?.[1]; + assert.match(version||'',/^\d{4}\.\d{2}\.\d{2}$/); + assert.ok(meta.includes(`v${version}`)); }); diff --git a/tests/version.test.mjs b/tests/version.test.mjs new file mode 100644 index 0000000..3da974a --- /dev/null +++ b/tests/version.test.mjs @@ -0,0 +1,13 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { check, versions } from '../scripts/version.mjs'; + +test('app and plugin versions stay consistent across sources and the packaged zip', () => { + assert.deepEqual(check(), []); +}); + +test('app and plugin versions are independent CalVer values', () => { + const { app, plugin } = versions(); + assert.match(app, /^\d{4}\.\d{2}\.\d{2}$/); + assert.match(plugin, /^\d{4}\.\d{2}\.\d{2}$/); +});