Skip to content

Commit

Permalink
Make all Fastify plugins async
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Oct 2, 2024
1 parent 807bf20 commit 088507d
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 48 deletions.
4 changes: 1 addition & 3 deletions server/src/plugins/access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -21,8 +21,6 @@ export const accessTokenPlugin = fastifyPlugin(
req.accessToken = accessToken;
}
});

pluginDone();
},
{ fastify: '5', name: ACCESS_TOKEN_PLUGIN_ID },
);
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/api-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ApiProxyPluginOptions {
}

export const apiProxyPlugin = fastifyPlugin<ApiProxyPluginOptions>(
(app, { appNames }, pluginDone) => {
async (app, { appNames }) => {
app.decorateRequest('proxyStartTime', 0);

app.addHook('onSend', async (req, reply) => {
Expand Down Expand Up @@ -116,8 +116,6 @@ export const apiProxyPlugin = fastifyPlugin<ApiProxyPluginOptions>(
},
});
}

pluginDone();
},
{ fastify: '5', name: 'api-proxy', dependencies: [OBO_ACCESS_TOKEN_PLUGIN_ID, SERVER_TIMING_PLUGIN_ID] },
);
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/client-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | undefined> }>) => {
Expand All @@ -22,8 +22,6 @@ export const clientVersionPlugin = fastifyPlugin(
req.client_version = client_version;
}
});

pluginDone();
},
{ fastify: '5', name: CLIENT_VERSION_PLUGIN_ID },
);
4 changes: 1 addition & 3 deletions server/src/plugins/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const QUERYSTRING = Type.Object({
type QuerystringType = Static<typeof QUERYSTRING>;

export const documentPlugin = fastifyPlugin(
(app, _, pluginDone) => {
async (app) => {
app
.withTypeProvider<TypeBoxTypeProvider>()

Expand Down Expand Up @@ -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] },
);
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -34,8 +34,6 @@ export const healthPlugin = fastifyPlugin(

return reply.status(200).type('text/plain').send('Ready');
});

pluginDone();
},
{ fastify: '5', name: HEALTH_PLUGIN_ID },
);
4 changes: 1 addition & 3 deletions server/src/plugins/http-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,8 +36,6 @@ export const httpLoggerPlugin = fastifyPlugin(
response_content_type: res.getHeader('content-type'),
});
});

pluginDone();
},
{
fastify: '5',
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/nav-ident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -57,8 +57,6 @@ export const navIdentPlugin = fastifyPlugin(
});
}
});

pluginDone();
},
{
fastify: '5',
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/obo-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
Expand Down Expand Up @@ -64,8 +64,6 @@ export const oboAccessTokenPlugin = fastifyPlugin(
app.decorateRequest('getOboAccessToken', ASYNC_NOOP);
app.decorateRequest('getCachedOboAccessToken', SYNC_NOOP);
}

pluginDone();
},
{
fastify: '5',
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/proxy-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
10 changes: 4 additions & 6 deletions server/src/plugins/serve-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface FileEntry {
mimeType: string;
}

const files: Map<string, FileEntry> = new Map();
const FILE_ENTRY_MAP: Map<string, FileEntry> = new Map();

readdirSync(ASSETS_FOLDER).forEach(async (fileName) => {
const filePath = `${ASSETS_FOLDER}/${fileName}`;
Expand All @@ -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 } });
Expand All @@ -47,8 +47,6 @@ export const serveAssetsPlugin = fastifyPlugin(

return res.send(data);
});

pluginDone();
},
{ fastify: '5', name: SERVE_ASSETS_PLUGIN_ID },
);
4 changes: 1 addition & 3 deletions server/src/plugins/serve-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
4 changes: 1 addition & 3 deletions server/src/plugins/server-timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const SERVER_TIMING_PLUGIN_ID = 'server-timing';
* - `request.getResponseTime()`: Returns the duration of the request.
*/
export const serverTimingPlugin = fastifyPlugin<ServerTimingPluginOptions>(
(app, { enableAutoTotal = true }, pluginDone) => {
async (app, { enableAutoTotal = true }) => {
app.decorateRequest('startTime', 0);
app.decorateReply<ServerTiming[] | null>(serverTimingsKey, null);
app.decorateReply<Map<string, ServerTimingStart> | null>(serverTimingStartsKey, null);
Expand Down Expand Up @@ -168,8 +168,6 @@ export const serverTimingPlugin = fastifyPlugin<ServerTimingPluginOptions>(

reply.header(SERVER_TIMING_HEADER, serverTimingHeader.join(', '));
});

pluginDone();
},
{ fastify: '5', name: SERVER_TIMING_PLUGIN_ID },
);
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/tab-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | undefined> }>) => {
Expand All @@ -22,8 +22,6 @@ export const tabIdPlugin = fastifyPlugin(
req.tab_id = tab_id;
}
});

pluginDone();
},
{ fastify: '5', name: TAB_ID_PLUGIN_ID },
);
4 changes: 1 addition & 3 deletions server/src/plugins/traceparent/traceparent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', '');
Expand All @@ -29,8 +29,6 @@ export const traceparentPlugin = fastifyPlugin(
req.span_id = span_id;
req.traceparent = traceparent;
});

pluginDone();
},
{ fastify: '5', name: TRACEPARENT_PLUGIN_ID },
);
Expand Down
4 changes: 1 addition & 3 deletions server/src/plugins/version/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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' },
);
Expand Down

0 comments on commit 088507d

Please sign in to comment.