import { getFile } from '../files.js' import { isNotFoundError, isVersionMismatchError } from '../errors.js' import createError from 'http-errors' export default async function serveFilePlugin (fastify, options) { fastify.get('*', async (req, reply) => { try { const version = reply.version const url = req.urlData('path').substr(process.env.APP_ROOT.length - 1) const path = url === '/' ? '/index.html' : url const { body, headers } = await getFile({ version, path }) reply.headers(headers) reply.send(body) } catch (err) { if (isNotFoundError(err) || isVersionMismatchError(err)) throw createError(404, `File "${req.urlData('path')}" does not exist.`) throw createError(err.statusCode || 500) } }) }