mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-05 14:36:27 +01:00
Require the React client instead of serving Vue
This commit is contained in:
1 parent
b734f38cda
commit
2dd5fa5478
3 files changed
+95
-69
No files matched your search
@@ -7,12 +7,13 @@ on:
|
|||||||
- 'dependabot/**' # Don't run dependabot branches, as they are already covered by pull requests
|
- 'dependabot/**' # Don't run dependabot branches, as they are already covered by pull requests
|
||||||
# Only build when files in these directories have been changed
|
# Only build when files in these directories have been changed
|
||||||
paths:
|
paths:
|
||||||
- client/**
|
|
||||||
- server/**
|
- server/**
|
||||||
- test/**
|
- test/**
|
||||||
- index.js
|
- index.js
|
||||||
- package.json
|
- package.json
|
||||||
- tsconfig.server.json
|
- tsconfig.server.json
|
||||||
|
- scripts/fetch-client-react-runtime.js
|
||||||
|
- .github/workflows/integration-test.yml
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
@@ -30,24 +31,19 @@ jobs:
|
|||||||
- name: install pkg (using yao-pkg fork for targeting node24)
|
- name: install pkg (using yao-pkg fork for targeting node24)
|
||||||
run: npm install -g @yao-pkg/pkg
|
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
|
- name: get server dependencies and compile server
|
||||||
run: npm ci && npm run build:server && npm prune --omit=dev
|
run: npm ci && npm run build:server && npm prune --omit=dev
|
||||||
|
|
||||||
- name: build binary
|
- 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
|
- name: run audiobookshelf
|
||||||
run: |
|
run: |
|
||||||
./audiobookshelf &
|
REACT_CLIENT_PATH="$PWD/dist/pkg/client-react" ./dist/pkg/audiobookshelf &
|
||||||
sleep 5
|
sleep 5
|
||||||
|
|
||||||
- name: test if server is available
|
- 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
|
||||||
@@ -49,6 +49,12 @@ const SOURCE = options.source || process.env.SOURCE || 'debian'
|
|||||||
|
|
||||||
const ROUTER_BASE_PATH = process.env.ROUTER_BASE_PATH ?? '/audiobookshelf'
|
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(`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}`)
|
console.log(`Options: CONFIG_PATH=${CONFIG_PATH}, METADATA_PATH=${METADATA_PATH}, PORT=${PORT}, HOST=${HOST}, SOURCE=${SOURCE}, ROUTER_BASE_PATH=${ROUTER_BASE_PATH}`)
|
||||||
|
|
||||||
|
|||||||
+81
-57
@@ -1,4 +1,5 @@
|
|||||||
const Path = require('path')
|
const Path = require('path')
|
||||||
|
const Module = require('module')
|
||||||
const Sequelize = require('sequelize')
|
const Sequelize = require('sequelize')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
@@ -68,6 +69,51 @@ function normalizeBasePath(basePath) {
|
|||||||
* one more `.server` down. Older react clients have no basePath and must keep
|
* one more `.server` down. Older react clients have no basePath and must keep
|
||||||
* Express-stripped URLs.
|
* 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) {
|
function getPreparedNextBasePath(nextApp) {
|
||||||
const nodeServer = nextApp.server?.server
|
const nodeServer = nextApp.server?.server
|
||||||
const basePath = nodeServer?.nextConfig?.basePath
|
const basePath = nodeServer?.nextConfig?.basePath
|
||||||
@@ -395,65 +441,43 @@ class Server {
|
|||||||
})
|
})
|
||||||
router.get('/healthcheck', (req, res) => res.sendStatus(200))
|
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
|
const ReactClientPath = process.env.REACT_CLIENT_PATH
|
||||||
if (!ReactClientPath) {
|
if (!String(ReactClientPath || '').trim()) {
|
||||||
// Static path to generated nuxt
|
throw new Error('REACT_CLIENT_PATH is required')
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
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/'
|
const unixSocketPrefix = 'unix/'
|
||||||
if (this.Host?.startsWith(unixSocketPrefix)) {
|
if (this.Host?.startsWith(unixSocketPrefix)) {
|
||||||
|
|||||||
Reference in new issue
Block a user