Skip to content

Commit

Permalink
return function from configureServer
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsmith committed Jan 26, 2025
1 parent f7f3a66 commit f36abec
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const serverPath = isWindows ? serverFile.replaceAll("\\", "//") : serverFile;
* @typedef {import('astro').AstroUserConfig} AstroUserConfig
@typedef {import('astro').AstroConfig} AstroConfig
* @typedef {import('vite').Plugin} VitePlugin
*
*
* @typedef {import('./types').IntegrationOptions} IntegrationOptions
*
*
*/

/**
Expand All @@ -38,42 +38,42 @@ function vitePlugin(options) {
return {
name: '@matthewp/astro-fastify:vite',
async configureServer(server) {
const nextSymbol = Symbol('next');

/** @type {import('fastify').FastifyServerFactory} */
const serverFactory = (handler, opts) => {
server.middlewares.use((req, res, next) => {
req[nextSymbol] = next;
handler(req, res);
return async () => {
const nextSymbol = Symbol('next');
/** @type {import('fastify').FastifyServerFactory} */
const serverFactory = (handler, opts) => {
server.middlewares.use((req, res, next) => {
req[nextSymbol] = next;
handler(req, res);
});
return /** @type {import('http').Server} */(server.httpServer ?? new http.Server());
}

const fastify = Fastify({
logger: options?.logger ?? true,
serverFactory
});
return /** @type {import('http').Server} */(server.httpServer ?? new http.Server());
}

const fastify = Fastify({
logger: options?.logger ?? true,
serverFactory
});

if(options?.entry) {
const entry = entryToPath(options.entry);
const entryModule = await server.ssrLoadModule(entry);
const setupRoutes = entryModule.default;
if(typeof setupRoutes !== 'function') {
throw new Error(`@matthewp/astro-fastify: ${entry} should export a default function.`);

if(options?.entry) {
const entry = entryToPath(options.entry);
const entryModule = await server.ssrLoadModule(entry);
const setupRoutes = entryModule.default;
if(typeof setupRoutes !== 'function') {
throw new Error(`@matthewp/astro-fastify: ${entry} should export a default function.`);
}
setupRoutes(fastify);
}
setupRoutes(fastify);

// Final catch-all route forwards back to the Vite server
fastify.all('/*', function (request) {
/** @type {import('connect').NextFunction} */
const next = request.raw[nextSymbol];
if (typeof next === "function") {
next();
}
});
await fastify.ready();
}

// Final catch-all route forwards back to the Vite server
fastify.all('/*', function (request) {
/** @type {import('connect').NextFunction} */
const next = request.raw[nextSymbol];
if (typeof next === "function") {
next();
}
});

await fastify.ready();
},
transform(code, id) {
if(options?.entry && id.includes('@matthewp/astro-fastify/lib/server.js')) {
Expand Down

0 comments on commit f36abec

Please sign in to comment.