screentinker/server/db/sqlite-driver.js
ScreenTinker 1bb24e7604 Choose the SQLite driver at runtime, and ship the FFmpeg licence with the binaries
TWO CHANGES, together because they touch the same packager hunks.

1. THE DRIVER.

The BrightSign package used to be MANUFACTURED. scripts/build-server-zip.sh dropped
better-sqlite3 from package.json and then installed db/sqlite-compat.js into
node_modules under that name, so every require resolved to the façade. It worked —
and it shipped a database layer that no test had ever executed. That is the same
shape as the TELEMETRY_COLLECTOR TDZ crash that took production down while 1676
tests and four CI jobs were green: a build-time rewrite cannot be tested by the
build that performs it.

db/sqlite-driver.js now decides at runtime: the native driver when it loads, the
node:sqlite façade otherwise. One artifact, one code path, and — the point — both
branches reachable from a test. ST_SQLITE_DRIVER=node runs the entire suite the way
a player runs it, and a new CI job does exactly that on Node 24 with --omit=optional
so the fallback is reached the same way it is on hardware, not by an env var alone.

better-sqlite3 becomes an optionalDependency, so a host with no compiler installs
cleanly and falls back rather than failing. preflight-deps stops trying to rebuild a
native module on a host that has no toolchain and a working built-in driver — on a
player that was a five-minute node-gyp failure ending in a server that never started.
Asking for the native driver BY NAME (ST_SQLITE_DRIVER=better-sqlite3) still fails
loudly, because a production box that has lost its native module is broken and should
say so rather than quietly running something else.

⚠️ NODE 24 IN PRACTICE. node:sqlite is unflagged only from 23.4; on the 22.x line it
needs --experimental-sqlite and on 20.x it does not exist. So the code probes rather
than comparing versions, the player package pins engines >=24, and the built-in cases
skip on the Node 20 CI job rather than failing there.

Verified on Node 24, both drivers, full suite:
  better-sqlite3   1762 pass / 0 fail
  node:sqlite      1762 pass / 0 fail
and the built payload resolves node:sqlite with no better-sqlite3 present at all.

2. THE LICENCE.

The ffprobe/ffmpeg binaries added in the previous commit are LGPL 2.1 and statically
linked, so the licence text has to travel WITH them — a link on a website is not the
copy the licence asks to accompany the work. The packager now copies
COPYING.LGPLv2.1 and a build README into bin/, and refuses to build if the licence is
missing. legal/third-party.html gains an LGPL section with the written offer required
by section 6 for static linking, and the exact configure line.

It also drops Sharp, which that page still listed although #263 removed it, and names
what actually does the image work now (jimp, @jsquash/webp, @jsquash/avif).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014kfhrUPit5MCqxeTQyqr56
2026-08-18 20:57:05 -05:00

80 lines
3.6 KiB
JavaScript

'use strict';
/*
* WHICH SQLite driver this process uses, decided at RUNTIME.
*
* There are two: the native `better-sqlite3`, and `./sqlite-compat.js` — a better-sqlite3-shaped
* façade over Node's built-in `node:sqlite`. Both work; they differ in what they need from the host.
*
* WHY THIS EXISTS. The BrightSign player package used to be MANUFACTURED: the packager dropped
* better-sqlite3 from package.json and then installed the shim into node_modules under the name
* `better-sqlite3`, so every require resolved to it by construction. That worked, and it shipped a
* code path CI had never executed — the same shape as the TELEMETRY_COLLECTOR TDZ crash that took
* production down while 1676 tests were green. A build-time rewrite cannot be tested by the build it
* rewrites.
*
* Deciding at runtime instead means ONE artifact, and both paths are reachable from a test:
* ST_SQLITE_DRIVER=node runs the whole suite on the built-in driver, on an ordinary developer
* machine, with no player involved.
*
* ⚠️ CONSTRUCT A DATABASE, do not merely require it. better-sqlite3's entry point is plain
* JavaScript that loads its compiled .node binding LAZILY, so `require()` succeeds under a Node
* whose ABI the binary was never built for. Opening an in-memory database is what actually pulls the
* binding in — this is the same reasoning as lib/preflight-deps.js, learned from a real Node 18/20
* mismatch that reported a broken install as healthy.
*
* ⚠️ FALLING BACK IS NOT ALWAYS RIGHT. On a server that is SUPPOSED to have the native module, a
* silent downgrade would hide a broken install behind slightly different behaviour — so
* ST_SQLITE_DRIVER=better-sqlite3 makes the failure loud instead. The default is to fall back,
* because the alternative on a player is a server that will not boot at all.
*/
const FORCED = String(process.env.ST_SQLITE_DRIVER || '').trim();
let Database = null;
let driverName = null;
let fallbackReason = null;
function loadNative() {
const D = require('better-sqlite3');
// The ABI check. Touches no file.
new D(':memory:').close();
return D;
}
if (FORCED === 'node' || FORCED === 'node:sqlite') {
Database = require('./sqlite-compat');
driverName = 'node:sqlite';
fallbackReason = 'forced by ST_SQLITE_DRIVER';
} else {
try {
Database = loadNative();
driverName = 'better-sqlite3';
} catch (e) {
const msg = String((e && e.message) || e);
if (FORCED === 'better-sqlite3') {
// Asked for the native driver by name: refuse to quietly become something else.
throw new Error('ST_SQLITE_DRIVER=better-sqlite3 was requested but it is not usable: ' + msg);
}
/*
* ⚠️ NODE 24 IN PRACTICE. node:sqlite is unflagged only from Node 23.4; on the 22.x line it
* exists solely behind --experimental-sqlite. So on a 22.x host with a broken native module
* BOTH drivers are gone, and the bare failure would be "Cannot find module 'node:sqlite'" —
* which names the fallback rather than the actual problem, on a server that is already down.
*/
try {
Database = require('./sqlite-compat');
} catch (inner) {
throw new Error(
'No usable SQLite driver. better-sqlite3 failed with: ' + msg +
' — and the built-in node:sqlite is unavailable on ' + process.version +
' (it is unflagged only from Node 23.4; 22.x needs --experimental-sqlite). ' +
'Rebuild better-sqlite3 for this Node, or run Node 24.');
}
driverName = 'node:sqlite';
fallbackReason = msg;
}
}
module.exports = { Database, driverName, fallbackReason };