Files
shelfless/server/index.mjs
Scarriffle 83d8b7b99d Initial commit: Shelfless – alternative Audiobookshelf frontend
React + Vite + TypeScript SPA covering the full ABS feature set (library
browsing, item detail, metadata/cover editing, podcasts, player with session
sync, admin: users/libraries/scanner/server settings). Dev uses a dynamic
CORS proxy; production is served by server/index.mjs (static + reverse proxy
to ABS_URL). Includes systemd unit and installer under deploy/.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 20:23:04 +02:00

117 lines
3.7 KiB
JavaScript

// Production server for Shelfless.
// Serves the built SPA (dist/) and reverse-proxies ABS API paths to the Audiobookshelf
// server given by ABS_URL — so the browser talks to a single origin (no CORS) and the
// ABS address is fixed by the deployment, not entered per browser.
//
// ABS_URL=http://127.0.0.1:13378 PORT=8080 node server/index.mjs
//
// Only Node built-ins are used (no dependencies to install on the container).
import http from 'node:http'
import https from 'node:https'
import { createReadStream, existsSync, statSync } from 'node:fs'
import { join, extname, normalize } from 'node:path'
import { fileURLToPath } from 'node:url'
const PORT = Number(process.env.PORT || 8080)
const HOST = process.env.HOST || '0.0.0.0'
const ABS_URL = process.env.ABS_URL || 'http://127.0.0.1:13378'
const ROOT = fileURLToPath(new URL('..', import.meta.url))
const DIST = join(ROOT, 'dist')
// Paths that must be forwarded to Audiobookshelf.
const PROXY_PREFIXES = ['/api', '/login', '/logout', '/public', '/status', '/hls', '/feed', '/socket.io']
const MIME = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
'.woff2': 'font/woff2',
'.woff': 'font/woff',
'.webmanifest': 'application/manifest+json',
'.txt': 'text/plain; charset=utf-8',
}
const target = new URL(ABS_URL)
const proxyLib = target.protocol === 'https:' ? https : http
function isProxied(url) {
return PROXY_PREFIXES.some((p) => url === p || url.startsWith(p + '/') || url.startsWith(p + '?'))
}
function proxy(req, res) {
const headers = { ...req.headers, host: target.host }
delete headers.origin
delete headers.referer
const proxyReq = proxyLib.request(
{
protocol: target.protocol,
hostname: target.hostname,
port: target.port || (target.protocol === 'https:' ? 443 : 80),
method: req.method,
path: req.url,
headers,
},
(proxyRes) => {
res.writeHead(proxyRes.statusCode || 502, proxyRes.headers)
proxyRes.pipe(res)
},
)
proxyReq.on('error', (e) => {
res.statusCode = 502
res.end(`Upstream error: ${e.message}`)
})
req.pipe(proxyReq)
}
function sendFile(res, filePath, status = 200) {
res.writeHead(status, {
'Content-Type': MIME[extname(filePath)] || 'application/octet-stream',
// Hashed asset files are immutable; index.html should not be cached.
'Cache-Control': filePath.includes(`${join('dist', 'assets')}`)
? 'public, max-age=31536000, immutable'
: 'no-cache',
})
createReadStream(filePath).pipe(res)
}
function serveStatic(req, res) {
// Strip query, prevent path traversal, default to index.html (SPA fallback).
const urlPath = decodeURIComponent((req.url || '/').split('?')[0])
const safe = normalize(urlPath).replace(/^(\.\.[/\\])+/, '')
let filePath = join(DIST, safe)
if (!filePath.startsWith(DIST)) {
res.statusCode = 403
return res.end('Forbidden')
}
if (existsSync(filePath) && statSync(filePath).isFile()) {
return sendFile(res, filePath)
}
// SPA fallback
const indexFile = join(DIST, 'index.html')
if (existsSync(indexFile)) return sendFile(res, indexFile)
res.statusCode = 404
res.end('Not found')
}
const server = http.createServer((req, res) => {
const url = req.url || '/'
if (isProxied(url)) return proxy(req, res)
return serveStatic(req, res)
})
server.listen(PORT, HOST, () => {
console.log(`Shelfless running on http://${HOST}:${PORT} → ABS ${ABS_URL}`)
})