From f6290e96fa7fcfbfbf9723514484aea4664c3935 Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Wed, 18 Oct 2023 15:53:15 +0200 Subject: [PATCH] trifid: register plugins --- .changeset/early-ads-rescue.md | 5 ++++ .changeset/sixty-garlics-return.md | 5 ++++ package-lock.json | 36 ++++++++++++++++++++++- packages/trifid/index.js | 12 ++++++-- packages/trifid/lib/registerPlugin.js | 20 +++++++++++++ packages/trifid/middlewares/health.js | 16 ---------- packages/trifid/package.json | 6 +++- packages/trifid/plugins/health.js | 28 ++++++++++++++++++ packages/trifid/plugins/templateEngine.js | 36 +++++++++++++++++++++++ packages/trifid/types/plugin.d.ts | 6 ++++ packages/trifid/views/layouts/main.hbs | 1 + 11 files changed, 150 insertions(+), 21 deletions(-) create mode 100644 .changeset/early-ads-rescue.md create mode 100644 .changeset/sixty-garlics-return.md create mode 100644 packages/trifid/lib/registerPlugin.js delete mode 100644 packages/trifid/middlewares/health.js create mode 100644 packages/trifid/plugins/health.js create mode 100644 packages/trifid/plugins/templateEngine.js create mode 100644 packages/trifid/types/plugin.d.ts create mode 100644 packages/trifid/views/layouts/main.hbs diff --git a/.changeset/early-ads-rescue.md b/.changeset/early-ads-rescue.md new file mode 100644 index 00000000..c67fbc3c --- /dev/null +++ b/.changeset/early-ads-rescue.md @@ -0,0 +1,5 @@ +--- +"trifid": patch +--- + +Install Helmet diff --git a/.changeset/sixty-garlics-return.md b/.changeset/sixty-garlics-return.md new file mode 100644 index 00000000..f1abb394 --- /dev/null +++ b/.changeset/sixty-garlics-return.md @@ -0,0 +1,5 @@ +--- +"trifid": minor +--- + +Allow to register plugins diff --git a/package-lock.json b/package-lock.json index 485912a3..23d00397 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2321,6 +2321,24 @@ "fast-json-stringify": "^5.7.0" } }, + "node_modules/@fastify/helmet": { + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/@fastify/helmet/-/helmet-11.1.1.tgz", + "integrity": "sha512-pjJxjk6SLEimITWadtYIXt6wBMfFC1I6OQyH/jYVCqSAn36sgAIFjeNiibHtifjCd+e25442pObis3Rjtame6A==", + "dependencies": { + "fastify-plugin": "^4.2.1", + "helmet": "^7.0.0" + } + }, + "node_modules/@fastify/view": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@fastify/view/-/view-8.2.0.tgz", + "integrity": "sha512-hBSiBofCnJNlPHEMZWpO1SL84eqOaqujJ1hR3jntFyZZCkweH5jMs12DKYyGesjVll7SJFRRxPUBB8kmUmneRQ==", + "dependencies": { + "fastify-plugin": "^4.0.0", + "hashlru": "^2.3.0" + } + }, "node_modules/@fortawesome/fontawesome-common-types": { "version": "0.2.36", "resolved": "https://registry.npmjs.org/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.2.36.tgz", @@ -11297,6 +11315,11 @@ "node": ">=8" } }, + "node_modules/hashlru": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/hashlru/-/hashlru-2.3.0.tgz", + "integrity": "sha512-0cMsjjIC8I+D3M44pOQdsy0OHXGLVz6Z0beRuufhKa0KfaD2wGwAev6jILzXsd3/vpnNQJmWyZtIILqM1N+n5A==" + }, "node_modules/he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", @@ -11306,6 +11329,14 @@ "he": "bin/he" } }, + "node_modules/helmet": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/helmet/-/helmet-7.0.0.tgz", + "integrity": "sha512-MsIgYmdBh460ZZ8cJC81q4XJknjG567wzEmv46WOBblDb6TUd3z8/GhgmsM9pn8g2B80tAJ4m5/d3Bi1KrSUBQ==", + "engines": { + "node": ">=16.0.0" + } + }, "node_modules/help-me": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/help-me/-/help-me-4.2.0.tgz", @@ -21255,7 +21286,10 @@ "license": "Apache-2.0", "dependencies": { "@fastify/cors": "^8.4.0", - "fastify": "^4.23.2" + "@fastify/helmet": "^11.1.1", + "@fastify/view": "^8.2.0", + "fastify": "^4.23.2", + "handlebars": "^4.7.8" }, "bin": { "trifid": "server.js" diff --git a/packages/trifid/index.js b/packages/trifid/index.js index fe2635f9..437203b2 100644 --- a/packages/trifid/index.js +++ b/packages/trifid/index.js @@ -1,8 +1,11 @@ // @ts-check import Fastify from 'fastify' import cors from '@fastify/cors' +import helmet from '@fastify/helmet' -import healthMiddleware from './middlewares/health.js' +import { registerPlugin } from './lib/registerPlugin.js' +import healthPlugin from './plugins/health.js' +import templateEngine from './plugins/templateEngine.js' const server = Fastify({ logger: { @@ -14,9 +17,12 @@ await server.register(cors, { origin: '*', credentials: true, }) +await server.register(helmet, { + global: false, +}) -// Health check -server.get('/healthz', healthMiddleware) +await registerPlugin(server, templateEngine) +await registerPlugin(server, healthPlugin) const start = async () => { try { diff --git a/packages/trifid/lib/registerPlugin.js b/packages/trifid/lib/registerPlugin.js new file mode 100644 index 00000000..6d9a8318 --- /dev/null +++ b/packages/trifid/lib/registerPlugin.js @@ -0,0 +1,20 @@ +// @ts-check + +/** + * Register a Trifid plugin. + * + * @param {import('fastify').FastifyInstance} fastifyInstance Fastify instance + * @param {import('../types/plugin.d.ts').TrifidPlugin} trifidPlugin Trifid plugin + * @returns {Promise} + */ +export const registerPlugin = async (fastifyInstance, trifidPlugin) => { + const { plugin, pluginOptions, routeOptions } = await trifidPlugin() + + if (plugin) { + fastifyInstance.register(plugin, pluginOptions) + } + + if (routeOptions) { + fastifyInstance.route(routeOptions) + } +} diff --git a/packages/trifid/middlewares/health.js b/packages/trifid/middlewares/health.js deleted file mode 100644 index 0a35f968..00000000 --- a/packages/trifid/middlewares/health.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Health middleware. - * - * @param {import('fastify').FastifyRequest} request - * @param {import('fastify').FastifyReply} reply - * @returns {Promise} - */ -const healthMiddleware = async (request, reply) => { - request.log.debug('Some info about the current request') - reply - .code(200) - .header('Content-Type', 'text/plain; charset=utf-8') - .send('OK') -} - -export default healthMiddleware diff --git a/packages/trifid/package.json b/packages/trifid/package.json index 92fca08d..5e947c49 100644 --- a/packages/trifid/package.json +++ b/packages/trifid/package.json @@ -15,6 +15,7 @@ "url": "https://github.com/zazuko/trifid/issues" }, "scripts": { + "dev": "node --watch index.js", "test": "true", "help": "node server.js --help", "start:tbbt": "node server.js -c instances/tbbt/config.yaml", @@ -25,6 +26,9 @@ }, "dependencies": { "@fastify/cors": "^8.4.0", - "fastify": "^4.23.2" + "@fastify/helmet": "^11.1.1", + "@fastify/view": "^8.2.0", + "fastify": "^4.23.2", + "handlebars": "^4.7.8" } } diff --git a/packages/trifid/plugins/health.js b/packages/trifid/plugins/health.js new file mode 100644 index 00000000..2095e2c9 --- /dev/null +++ b/packages/trifid/plugins/health.js @@ -0,0 +1,28 @@ +// @ts-check + +/** + * Health plugin. + * + * @param {import('fastify').FastifyRequest} request Fastify request + * @param {import('fastify').FastifyReply} reply Fastify reply + * @returns {Promise} + */ +const healthPlugin = async (request, reply) => { + request.log.debug('Some info about the current request') + reply + .code(200) + .header('Content-Type', 'text/plain; charset=utf-8') + .send('OK') +} + +/** @type {import('../types/plugin.d.ts').TrifidPlugin}'} */ +const trifidPlugin = async () => { + return { + routeOptions: { + method: 'GET', + url: '/healthz', + handler: healthPlugin, + }, + } +} +export default trifidPlugin diff --git a/packages/trifid/plugins/templateEngine.js b/packages/trifid/plugins/templateEngine.js new file mode 100644 index 00000000..2ce43df9 --- /dev/null +++ b/packages/trifid/plugins/templateEngine.js @@ -0,0 +1,36 @@ +// @ts-check +import { dirname, join as pathJoin } from 'node:path' +import { fileURLToPath } from 'node:url' + +import fastifyView from '@fastify/view' +import Handlebars from 'handlebars' + +const trifidRootPath = dirname(dirname(fileURLToPath(import.meta.url))) + +/** @type {import('@fastify/view').default} */ +const plugin = fastifyView + +/** @type {import('@fastify/view').FastifyViewOptions} */ +const pluginOptions = { + engine: { + handlebars: Handlebars, + }, + root: pathJoin(trifidRootPath, 'views'), + layout: './layouts/main', + includeViewExtension: true, + viewExt: 'hbs', + propertyName: 'view', + defaultContext: { + dev: process.env.NODE_ENV === 'development', + }, + options: {}, +} + +/** @type {import('../types/plugin.d.ts').TrifidPlugin} */ +const trifidPlugin = async () => { + return { + plugin, + pluginOptions, + } +} +export default trifidPlugin diff --git a/packages/trifid/types/plugin.d.ts b/packages/trifid/types/plugin.d.ts new file mode 100644 index 00000000..6a4b5056 --- /dev/null +++ b/packages/trifid/types/plugin.d.ts @@ -0,0 +1,6 @@ +export type TrifidPluginResult = { + plugin?: any; + pluginOptions?: any; + routeOptions?: import("fastify").RouteOptions; +}; +export type TrifidPlugin = () => Promise; diff --git a/packages/trifid/views/layouts/main.hbs b/packages/trifid/views/layouts/main.hbs new file mode 100644 index 00000000..cd087558 --- /dev/null +++ b/packages/trifid/views/layouts/main.hbs @@ -0,0 +1 @@ +Hello world!