From 088507d10fbcd8f1201fffe14c760ef1c9c0d48f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Skr=C3=B8vseth?= Date: Wed, 2 Oct 2024 16:18:49 +0200 Subject: [PATCH] Make all Fastify plugins async --- server/src/plugins/access-token.ts | 4 +--- server/src/plugins/api-proxy.ts | 4 +--- server/src/plugins/client-version.ts | 4 +--- server/src/plugins/document.ts | 4 +--- server/src/plugins/health.ts | 4 +--- server/src/plugins/http-logger.ts | 4 +--- server/src/plugins/nav-ident.ts | 4 +--- server/src/plugins/obo-token.ts | 4 +--- server/src/plugins/proxy-version.ts | 4 +--- server/src/plugins/serve-assets.ts | 10 ++++------ server/src/plugins/serve-index.ts | 4 +--- server/src/plugins/server-timing.ts | 4 +--- server/src/plugins/tab-id.ts | 4 +--- server/src/plugins/traceparent/traceparent.ts | 4 +--- server/src/plugins/version/version.ts | 4 +--- 15 files changed, 18 insertions(+), 48 deletions(-) diff --git a/server/src/plugins/access-token.ts b/server/src/plugins/access-token.ts index 53214219e..917a4a063 100644 --- a/server/src/plugins/access-token.ts +++ b/server/src/plugins/access-token.ts @@ -11,7 +11,7 @@ declare module 'fastify' { export const ACCESS_TOKEN_PLUGIN_ID = 'access-token'; export const accessTokenPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.decorateRequest('accessToken', ''); app.addHook('preHandler', async (req) => { @@ -21,8 +21,6 @@ export const accessTokenPlugin = fastifyPlugin( req.accessToken = accessToken; } }); - - pluginDone(); }, { fastify: '5', name: ACCESS_TOKEN_PLUGIN_ID }, ); diff --git a/server/src/plugins/api-proxy.ts b/server/src/plugins/api-proxy.ts index 6481a73c0..74fb1aa6a 100644 --- a/server/src/plugins/api-proxy.ts +++ b/server/src/plugins/api-proxy.ts @@ -20,7 +20,7 @@ export interface ApiProxyPluginOptions { } export const apiProxyPlugin = fastifyPlugin( - (app, { appNames }, pluginDone) => { + async (app, { appNames }) => { app.decorateRequest('proxyStartTime', 0); app.addHook('onSend', async (req, reply) => { @@ -116,8 +116,6 @@ export const apiProxyPlugin = fastifyPlugin( }, }); } - - pluginDone(); }, { fastify: '5', name: 'api-proxy', dependencies: [OBO_ACCESS_TOKEN_PLUGIN_ID, SERVER_TIMING_PLUGIN_ID] }, ); diff --git a/server/src/plugins/client-version.ts b/server/src/plugins/client-version.ts index 0ca3db420..b142cf668 100644 --- a/server/src/plugins/client-version.ts +++ b/server/src/plugins/client-version.ts @@ -12,7 +12,7 @@ declare module 'fastify' { export const CLIENT_VERSION_PLUGIN_ID = 'client-version'; export const clientVersionPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.decorateRequest('client_version', ''); app.addHook('preHandler', async (req: FastifyRequest<{ Querystring: Record }>) => { @@ -22,8 +22,6 @@ export const clientVersionPlugin = fastifyPlugin( req.client_version = client_version; } }); - - pluginDone(); }, { fastify: '5', name: CLIENT_VERSION_PLUGIN_ID }, ); diff --git a/server/src/plugins/document.ts b/server/src/plugins/document.ts index 507367450..29d0a0ff0 100644 --- a/server/src/plugins/document.ts +++ b/server/src/plugins/document.ts @@ -52,7 +52,7 @@ const QUERYSTRING = Type.Object({ type QuerystringType = Static; export const documentPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app .withTypeProvider() @@ -193,8 +193,6 @@ export const documentPlugin = fastifyPlugin( return send(reply, url, documentIdList, metadata.title); }, ); - - pluginDone(); }, { fastify: '5', name: 'document-routes', dependencies: [OBO_ACCESS_TOKEN_PLUGIN_ID, SERVER_TIMING_PLUGIN_ID] }, ); diff --git a/server/src/plugins/health.ts b/server/src/plugins/health.ts index bcc48b4a4..e5fcbfd3e 100644 --- a/server/src/plugins/health.ts +++ b/server/src/plugins/health.ts @@ -8,7 +8,7 @@ export const HEALTH_PLUGIN_ID = 'health'; const log = getLogger('liveness'); export const healthPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.get('/isAlive', (__, reply) => reply.status(200).type('text/plain').send('Alive')); app.get('/isReady', async (__, reply) => { @@ -34,8 +34,6 @@ export const healthPlugin = fastifyPlugin( return reply.status(200).type('text/plain').send('Ready'); }); - - pluginDone(); }, { fastify: '5', name: HEALTH_PLUGIN_ID }, ); diff --git a/server/src/plugins/http-logger.ts b/server/src/plugins/http-logger.ts index ba8198655..692493338 100644 --- a/server/src/plugins/http-logger.ts +++ b/server/src/plugins/http-logger.ts @@ -9,7 +9,7 @@ import fastifyPlugin from 'fastify-plugin'; export const HTTP_LOGGER_PLUGIN_ID = 'http-logger'; export const httpLoggerPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.addHook('onResponse', async (req, res) => { const { url } = req; @@ -36,8 +36,6 @@ export const httpLoggerPlugin = fastifyPlugin( response_content_type: res.getHeader('content-type'), }); }); - - pluginDone(); }, { fastify: '5', diff --git a/server/src/plugins/nav-ident.ts b/server/src/plugins/nav-ident.ts index ba2728c21..50e417f64 100644 --- a/server/src/plugins/nav-ident.ts +++ b/server/src/plugins/nav-ident.ts @@ -18,7 +18,7 @@ const log = getLogger('nav-ident-plugin'); export const NAV_IDENT_PLUGIN_ID = 'nav-ident'; export const navIdentPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.decorateRequest('navIdent', ''); app.addHook('preHandler', async (req) => { @@ -57,8 +57,6 @@ export const navIdentPlugin = fastifyPlugin( }); } }); - - pluginDone(); }, { fastify: '5', diff --git a/server/src/plugins/obo-token.ts b/server/src/plugins/obo-token.ts index 94b5af6b3..3f3e95764 100644 --- a/server/src/plugins/obo-token.ts +++ b/server/src/plugins/obo-token.ts @@ -29,7 +29,7 @@ const SYNC_NOOP = () => undefined; export const OBO_ACCESS_TOKEN_PLUGIN_ID = 'obo-access-token'; export const oboAccessTokenPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.decorateRequest('oboAccessTokenMap'); app.addHook('onRequest', async (req): Promise => { @@ -64,8 +64,6 @@ export const oboAccessTokenPlugin = fastifyPlugin( app.decorateRequest('getOboAccessToken', ASYNC_NOOP); app.decorateRequest('getCachedOboAccessToken', SYNC_NOOP); } - - pluginDone(); }, { fastify: '5', diff --git a/server/src/plugins/proxy-version.ts b/server/src/plugins/proxy-version.ts index 2f0c010a7..8d699cd63 100644 --- a/server/src/plugins/proxy-version.ts +++ b/server/src/plugins/proxy-version.ts @@ -5,13 +5,11 @@ import fastifyPlugin from 'fastify-plugin'; export const PROXY_VERSION_PLUGIN_ID = 'proxy-version'; export const proxyVersionPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { // Add proxy version header to all responses. app.addHook('onSend', async (__, reply) => { reply.header(PROXY_VERSION_HEADER, PROXY_VERSION); }); - - pluginDone(); }, { fastify: '5', name: PROXY_VERSION_PLUGIN_ID }, ); diff --git a/server/src/plugins/serve-assets.ts b/server/src/plugins/serve-assets.ts index 1647a4855..ee47c264f 100644 --- a/server/src/plugins/serve-assets.ts +++ b/server/src/plugins/serve-assets.ts @@ -12,7 +12,7 @@ interface FileEntry { mimeType: string; } -const files: Map = new Map(); +const FILE_ENTRY_MAP: Map = new Map(); readdirSync(ASSETS_FOLDER).forEach(async (fileName) => { const filePath = `${ASSETS_FOLDER}/${fileName}`; @@ -21,16 +21,16 @@ readdirSync(ASSETS_FOLDER).forEach(async (fileName) => { const fileKey = `/assets/${fileName}`; const data = readFileSync(filePath); - files.set(fileKey, { data, mimeType: getMimeType(fileName) }); + FILE_ENTRY_MAP.set(fileKey, { data, mimeType: getMimeType(fileName) }); } }); export const SERVE_ASSETS_PLUGIN_ID = 'serve-assets'; export const serveAssetsPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.get('/assets/*', async (req, res) => { - const fileEntry = files.get(req.url); + const fileEntry = FILE_ENTRY_MAP.get(req.url); if (fileEntry === undefined) { log.warn({ msg: 'File not found', data: { path: req.url } }); @@ -47,8 +47,6 @@ export const serveAssetsPlugin = fastifyPlugin( return res.send(data); }); - - pluginDone(); }, { fastify: '5', name: SERVE_ASSETS_PLUGIN_ID }, ); diff --git a/server/src/plugins/serve-index.ts b/server/src/plugins/serve-index.ts index 5d0700740..bfa5dd676 100644 --- a/server/src/plugins/serve-index.ts +++ b/server/src/plugins/serve-index.ts @@ -27,11 +27,9 @@ const serveIndexHandler: RouteHandler = async (_, reply) => { export const SERVE_INDEX_PLUGIN_ID = 'serve-index'; export const serveIndexPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.get('/', serveIndexHandler); app.get('*', serveIndexHandler); - - pluginDone(); }, { fastify: '5', name: SERVE_INDEX_PLUGIN_ID }, ); diff --git a/server/src/plugins/server-timing.ts b/server/src/plugins/server-timing.ts index ebd1ddd07..36ec424a7 100644 --- a/server/src/plugins/server-timing.ts +++ b/server/src/plugins/server-timing.ts @@ -101,7 +101,7 @@ export const SERVER_TIMING_PLUGIN_ID = 'server-timing'; * - `request.getResponseTime()`: Returns the duration of the request. */ export const serverTimingPlugin = fastifyPlugin( - (app, { enableAutoTotal = true }, pluginDone) => { + async (app, { enableAutoTotal = true }) => { app.decorateRequest('startTime', 0); app.decorateReply(serverTimingsKey, null); app.decorateReply | null>(serverTimingStartsKey, null); @@ -168,8 +168,6 @@ export const serverTimingPlugin = fastifyPlugin( reply.header(SERVER_TIMING_HEADER, serverTimingHeader.join(', ')); }); - - pluginDone(); }, { fastify: '5', name: SERVER_TIMING_PLUGIN_ID }, ); diff --git a/server/src/plugins/tab-id.ts b/server/src/plugins/tab-id.ts index 6b3d3d391..081dd27a0 100644 --- a/server/src/plugins/tab-id.ts +++ b/server/src/plugins/tab-id.ts @@ -12,7 +12,7 @@ declare module 'fastify' { export const TAB_ID_PLUGIN_ID = 'tab-id'; export const tabIdPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.decorateRequest('tab_id', ''); app.addHook('preHandler', async (req: FastifyRequest<{ Querystring: Record }>) => { @@ -22,8 +22,6 @@ export const tabIdPlugin = fastifyPlugin( req.tab_id = tab_id; } }); - - pluginDone(); }, { fastify: '5', name: TAB_ID_PLUGIN_ID }, ); diff --git a/server/src/plugins/traceparent/traceparent.ts b/server/src/plugins/traceparent/traceparent.ts index ddaa02c62..a35e71501 100644 --- a/server/src/plugins/traceparent/traceparent.ts +++ b/server/src/plugins/traceparent/traceparent.ts @@ -18,7 +18,7 @@ declare module 'fastify' { export const TRACEPARENT_PLUGIN_ID = 'traceparent'; export const traceparentPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { app.decorateRequest('traceparent', ''); app.decorateRequest('trace_id', ''); app.decorateRequest('span_id', ''); @@ -29,8 +29,6 @@ export const traceparentPlugin = fastifyPlugin( req.span_id = span_id; req.traceparent = traceparent; }); - - pluginDone(); }, { fastify: '5', name: TRACEPARENT_PLUGIN_ID }, ); diff --git a/server/src/plugins/version/version.ts b/server/src/plugins/version/version.ts index 7bce450df..af610655a 100644 --- a/server/src/plugins/version/version.ts +++ b/server/src/plugins/version/version.ts @@ -11,7 +11,7 @@ const log = getLogger('version'); const RETRY_DELAY = 5_000; export const versionPlugin = fastifyPlugin( - (app, _, pluginDone) => { + async (app) => { const RETRY_SSE = `retry: ${RETRY_DELAY}\n\n`; const VERSION_SSE = formatSseEvent(EventNames.SERVER_VERSION, PROXY_VERSION); @@ -78,8 +78,6 @@ export const versionPlugin = fastifyPlugin( reply.raw.write(VERSION_SSE); reply.raw.write(formatSseEvent(EventNames.UPDATE_REQUEST, getUpdateRequest(req))); }); - - pluginDone(); }, { fastify: '5', name: 'version' }, );