diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index 31eada873..7a34c55b9 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -7,12 +7,13 @@ on: - 'dependabot/**' # Don't run dependabot branches, as they are already covered by pull requests # Only build when files in these directories have been changed paths: - - client/** - server/** - test/** - index.js - package.json - tsconfig.server.json + - scripts/fetch-client-react-runtime.js + - .github/workflows/integration-test.yml jobs: build: @@ -30,24 +31,19 @@ jobs: - name: install pkg (using yao-pkg fork for targeting node24) run: npm install -g @yao-pkg/pkg - - name: get client dependencies - working-directory: client - run: npm ci - - - name: build client - working-directory: client - run: npm run generate - - name: get server dependencies and compile server run: npm ci && npm run build:server && npm prune --omit=dev - name: build binary - run: pkg --config package.json -t node24-linux-x64 -o audiobookshelf dist-server/index.js + run: pkg --config package.json -t node24-linux-x64 -o dist/pkg/audiobookshelf dist-server/index.js + + - name: fetch the React client runtime + run: node scripts/fetch-client-react-runtime.js --out dist/pkg/client-react - name: run audiobookshelf run: | - ./audiobookshelf & + REACT_CLIENT_PATH="$PWD/dist/pkg/client-react" ./dist/pkg/audiobookshelf & sleep 5 - name: test if server is available - run: curl -sf http://127.0.0.1:3333 | grep Audiobookshelf + run: curl -sf --retry 30 --retry-delay 2 --retry-connrefused http://127.0.0.1:3333/healthcheck diff --git a/index.js b/index.js index 7c42e30ae..599f1a19c 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,12 @@ const SOURCE = options.source || process.env.SOURCE || 'debian' const ROUTER_BASE_PATH = process.env.ROUTER_BASE_PATH ?? '/audiobookshelf' +// v3 serves only the React client. +if (!String(process.env.REACT_CLIENT_PATH || '').trim()) { + console.error('REACT_CLIENT_PATH is required') + process.exit(1) +} + console.log(`Running in ${process.env.NODE_ENV} mode.`) console.log(`Options: CONFIG_PATH=${CONFIG_PATH}, METADATA_PATH=${METADATA_PATH}, PORT=${PORT}, HOST=${HOST}, SOURCE=${SOURCE}, ROUTER_BASE_PATH=${ROUTER_BASE_PATH}`) diff --git a/server/Server.js b/server/Server.js index 327cc94bf..f0f263441 100644 --- a/server/Server.js +++ b/server/Server.js @@ -1,4 +1,5 @@ const Path = require('path') +const Module = require('module') const Sequelize = require('sequelize') const express = require('express') const http = require('http') @@ -68,6 +69,51 @@ function normalizeBasePath(basePath) { * one more `.server` down. Older react clients have no basePath and must keep * Express-stripped URLs. */ +/** + * Next 16's production custom server loads `start-server.js` during prepare(). + * That file loads `app-info-log.js`, which requires `inspector` at module scope + * to print a debugger URL. pkg's Node binary has no inspector, and calling the + * real builtin throws ERR_INSPECTOR_NOT_AVAILABLE. Return a stub instead of + * calling it. Outside pkg, Next gets the real module. + */ +function inspectorStub() { + const stub = { + url: () => undefined, + open() {}, + close() {}, + waitForDebugger() {}, + console + } + stub.Session = class Session { + connect() {} + disconnect() {} + post(_method, callback) { + if (typeof callback === 'function') callback(null, {}) + } + } + return stub +} + +function isInspectorRequest(id) { + return id === 'inspector' || id === 'node:inspector' +} + +function allowPkgWithoutInspector() { + if (!process.pkg || Module.prototype.require.__absInspectorPatched) return + const originalRequire = Module.prototype.require + const originalLoad = Module._load + function requireWithInspectorFallback(id) { + if (isInspectorRequest(id)) return inspectorStub() + return originalRequire.apply(this, arguments) + } + requireWithInspectorFallback.__absInspectorPatched = true + Module.prototype.require = requireWithInspectorFallback + Module._load = function (request, parent, isMain) { + if (isInspectorRequest(request)) return inspectorStub() + return originalLoad.call(this, request, parent, isMain) + } +} + function getPreparedNextBasePath(nextApp) { const nodeServer = nextApp.server?.server const basePath = nodeServer?.nextConfig?.basePath @@ -395,65 +441,43 @@ class Server { }) router.get('/healthcheck', (req, res) => res.sendStatus(200)) + // v3 serves only the React client. REACT_CLIENT_PATH is required. + // Next joins process.cwd() with dir. On Windows, path.join does not reset + // when dir is an absolute drive path, so the manifest lookup becomes + // cwd\D:\client\D:\client\.next\.... Run Next from the client directory + // and pass a relative dir. const ReactClientPath = process.env.REACT_CLIENT_PATH - if (!ReactClientPath) { - // Static path to generated nuxt - const distPath = Path.join(global.appRoot, '/client/dist') - router.use(express.static(distPath)) - - // Client dynamic routes - const dynamicRoutes = [ - '/item/:id', - '/author/:id', - '/audiobook/:id/chapters', - '/audiobook/:id/edit', - '/audiobook/:id/manage', - '/library/:library', - '/library/:library/search', - '/library/:library/bookshelf/:id?', - '/library/:library/authors', - '/library/:library/narrators', - '/library/:library/stats', - '/library/:library/series/:id?', - '/library/:library/podcast/search', - '/library/:library/podcast/latest', - '/library/:library/podcast/download-queue', - '/config/users/:id', - '/config/users/:id/sessions', - '/config/item-metadata-utils/:id', - '/collection/:id', - '/playlist/:id', - '/share/:slug' - ] - dynamicRoutes.forEach((route) => router.get(route, (req, res) => res.sendFile(Path.join(distPath, 'index.html')))) - } else { - // This is for using the experimental Next.js client - Logger.info(`Using React client at ${ReactClientPath}`) - const nextPath = Path.join(ReactClientPath, 'node_modules/next') - const next = require(nextPath) - const nextApp = next({ dev: Logger.isDev, dir: ReactClientPath }) - const handle = nextApp.getRequestHandler() - await nextApp.prepare() - const nextBasePath = getPreparedNextBasePath(nextApp) - if (nextBasePath) { - Logger.info(`[Server] Next client basePath is "${nextBasePath}"`) - } else { - Logger.info(`[Server] Next client has no basePath; leaving Express-stripped URLs as-is`) - } - router.all('*', (req, res) => { - // Next is configured with the same base path as this router, but Express strips the mount - // prefix from req.url, so put it back. The bare mount path is passed through without a - // trailing slash, which Next would otherwise redirect away from on every request. - // Skip when Next has no basePath (older client on master). - if (nextBasePath && req.baseUrl) { - const queryIndex = req.url.indexOf('?') - const pathname = queryIndex === -1 ? req.url : req.url.slice(0, queryIndex) - const search = queryIndex === -1 ? '' : req.url.slice(queryIndex) - req.url = `${req.baseUrl}${pathname === '/' ? '' : pathname}${search}` - } - handle(req, res) - }) + if (!String(ReactClientPath || '').trim()) { + throw new Error('REACT_CLIENT_PATH is required') } + const reactClientDir = Path.resolve(ReactClientPath) + process.chdir(reactClientDir) + Logger.info(`Using React client at ${reactClientDir}`) + allowPkgWithoutInspector() + const nextPath = Path.join(reactClientDir, 'node_modules', 'next') + const next = require(nextPath) + const nextApp = next({ dev: Logger.isDev, dir: '.' }) + const handle = nextApp.getRequestHandler() + await nextApp.prepare() + const nextBasePath = getPreparedNextBasePath(nextApp) + if (nextBasePath) { + Logger.info(`[Server] Next client basePath is "${nextBasePath}"`) + } else { + Logger.info(`[Server] Next client has no basePath; leaving Express-stripped URLs as-is`) + } + router.all('*', (req, res) => { + // Next is configured with the same base path as this router, but Express strips the mount + // prefix from req.url, so put it back. The bare mount path is passed through without a + // trailing slash, which Next would otherwise redirect away from on every request. + // Skip when Next has no basePath (older client on master). + if (nextBasePath && req.baseUrl) { + const queryIndex = req.url.indexOf('?') + const pathname = queryIndex === -1 ? req.url : req.url.slice(0, queryIndex) + const search = queryIndex === -1 ? '' : req.url.slice(queryIndex) + req.url = `${req.baseUrl}${pathname === '/' ? '' : pathname}${search}` + } + handle(req, res) + }) const unixSocketPrefix = 'unix/' if (this.Host?.startsWith(unixSocketPrefix)) {