@gasket/plugin-fastify 7.0.0-next.34 → 7.0.0-next.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -34,7 +34,6 @@ All the configurations for the plugin are added under `fastify` in the config:
34
34
 
35
35
  - `compression`: true by default. Can be set to false if applying compression
36
36
  differently.
37
- - `excludedRoutesRegex`: Routes to be excluded based on a regex
38
37
  - `trustProxy`: Enable trust proxy option, [see Fastify documentation for possible values](https://fastify.dev/docs/latest/Reference/Server/#trustproxy)
39
38
 
40
39
  #### Example configuration
@@ -46,61 +45,15 @@ module.exports = {
46
45
  },
47
46
  fastify: {
48
47
  compression: false,
48
+ routes: 'api/*.js',
49
49
  excludedRoutesRegex: /^(?!\/_next\/)/,
50
50
  trustProxy: true
51
51
  }
52
52
  }
53
53
  ```
54
54
 
55
- ### Middleware paths
56
-
57
- The `gasket.config.js` can contain a `middleware` property, which is an array of
58
- objects that map plugins to route or path patterns, allowing apps to tune which
59
- middleware are triggered for which requests.
60
-
61
- ```js
62
- middleware: [
63
- {
64
- plugin:'gasket-plugin-example', // Name of the Gasket plugin
65
- paths: ['/api']
66
- },
67
- {
68
- plugin:'@some/gasket-plugin-example',
69
- paths: [/\/default/]
70
- },
71
- {
72
- plugin: '@another/gasket-plugin-example',
73
- paths: ['/proxy', /\/home/]
74
- }
75
- ]
76
- ```
77
-
78
55
  ## Lifecycles
79
56
 
80
- ### middleware
81
-
82
- Executed when the `fastify` server has been created, it will apply all returned
83
- functions as middleware.
84
-
85
- ```js
86
- module.exports = {
87
- hooks: {
88
- /**
89
- * Add Fastify middleware
90
- *
91
- * @param {Gasket} gasket The Gasket API
92
- * @param {Fastify} app - Fastify app instance
93
- * @returns {function|function[]} middleware(s)
94
- */
95
- middleware: function (gasket, app) {
96
- return require('x-xss-protection')();
97
- }
98
- }
99
- }
100
- ```
101
-
102
- You may also return an `Array` to inject more than one middleware.
103
-
104
57
  ### fastify
105
58
 
106
59
  Executed **after** the `middleware` event for when you need full control over
@@ -1,12 +1,6 @@
1
1
  /// <reference types="@gasket/plugin-https" />
2
2
  /// <reference types="@gasket/plugin-logger" />
3
3
 
4
- const fastify = require('fastify');
5
- const middie = require('middie');
6
- const cookieParser = require('cookie-parser');
7
- const compression = require('compression');
8
- const debug = require('diagnostics')('gasket:fastify');
9
-
10
4
  /**
11
5
  * Create the Fastify instance and setup the lifecycle hooks.
12
6
  * Fastify is compatible with express middleware out of the box, so we can
@@ -15,73 +9,32 @@ const debug = require('diagnostics')('gasket:fastify');
15
9
  */
16
10
  // eslint-disable-next-line max-statements
17
11
  module.exports = async function createServers(gasket, serverOpts) {
18
- const { logger, config } = gasket;
19
- const { middleware: middlewareConfig } = config;
20
- const trustProxy = config.fastify?.trustProxy ?? false;
21
- const excludedRoutesRegex =
22
- config.fastify && config.fastify.excludedRoutesRegex;
23
- // @ts-ignore
24
- const app = fastify({ logger, trustProxy });
25
-
26
- // Enable middleware for fastify@3
27
- await app.register(middie);
28
-
29
- // Add express-like `res.locals` object attaching data
30
- app.use(function attachLocals(req, res, next) {
31
- res.locals = {};
32
- next();
33
- });
12
+ const fastify = require('fastify');
13
+ const { config, logger } = gasket;
14
+ const {
15
+ fastify: {
16
+ routes,
17
+ trustProxy = false
18
+ } = {},
19
+ http2
20
+ } = config;
34
21
 
35
- if (excludedRoutesRegex) {
36
- app.use(excludedRoutesRegex, cookieParser());
37
- } else {
38
- app.use(cookieParser());
39
- }
40
-
41
- const { compression: compressionConfig = true } = config.fastify || {};
42
- if (compressionConfig) {
43
- app.use(compression());
44
- }
22
+ // @ts-ignore
23
+ const app = fastify({ logger, trustProxy, http2 });
45
24
 
46
- const middlewares = [];
25
+ // allow consuming apps to directly append options to their server
26
+ await gasket.exec('fastify', app);
47
27
 
48
- await gasket.execApply('middleware', async (plugin, handler) => {
49
- const middleware = await handler(app);
50
- if (middleware) {
51
- if (middlewareConfig) {
52
- const pluginName = plugin && plugin.name ? plugin.name : '';
53
- const mwConfig = middlewareConfig.find(
54
- (mw) => mw.plugin === pluginName
55
- );
56
- if (mwConfig) {
57
- middleware.paths = mwConfig.paths;
58
- if (excludedRoutesRegex) {
59
- middleware.paths.push(excludedRoutesRegex);
60
- }
61
- }
28
+ if (routes) {
29
+ for (const route of routes) {
30
+ if (typeof route !== 'function') {
31
+ throw new Error('Route must be a function');
62
32
  }
63
- middlewares.push(middleware);
64
- }
65
- });
66
-
67
- debug('applied %s middleware layers to fastify', middlewares.length);
68
- middlewares.forEach((layer) => {
69
- const { paths } = layer;
70
- if (paths) {
71
- app.use(paths, layer);
72
- } else if (excludedRoutesRegex) {
73
- app.use(excludedRoutesRegex, layer);
74
- } else {
75
- app.use(layer);
33
+ await route(app);
76
34
  }
77
- });
78
-
79
- // allow consuming apps to directly append options to their server
80
- await gasket.exec('fastify', app);
35
+ }
81
36
 
82
- const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(
83
- Boolean
84
- );
37
+ const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(Boolean);
85
38
  postRenderingStacks.forEach((stack) => app.use(stack));
86
39
 
87
40
  return {
package/lib/create.js ADDED
@@ -0,0 +1,34 @@
1
+ /// <reference types="@gasket/core" />
2
+
3
+ const { name, version, devDependencies } = require('../package.json');
4
+
5
+ /**
6
+ * Add files & extend package.json for new apps.
7
+ * @type {import('@gasket/core').HookHandler<'create'>}
8
+ */
9
+ module.exports = async function create(gasket, context) {
10
+ const generatorDir = `${__dirname}/../generator`;
11
+
12
+ context.gasketConfig.addPlugin('pluginFastify', name);
13
+
14
+ context.pkg.add('dependencies', {
15
+ [name]: `^${version}`,
16
+ fastify: devDependencies.fastify
17
+ });
18
+
19
+ if ('apiApp' in context && context.apiApp) {
20
+ context.gasketConfig.add('fastify', {
21
+ routes: []
22
+ });
23
+
24
+ if ('typescript' in context && context.typescript) {
25
+ context.files.add(`${generatorDir}/**/!(*.js)`);
26
+ } else {
27
+ context.files.add(`${generatorDir}/**/!(*.ts)`);
28
+ }
29
+
30
+ context.gasketConfig
31
+ .addImport('{ routes }', './routes/index.js')
32
+ .injectValue('fastify.routes', 'routes');
33
+ }
34
+ };
package/lib/index.d.ts CHANGED
@@ -1,22 +1,26 @@
1
1
  import type { MaybeAsync, MaybeMultiple } from '@gasket/core';
2
2
  import type {
3
- Fastify,
3
+ FastifyInstance,
4
4
  FastifyRequest,
5
5
  FastifyReply,
6
6
  FastifyServerOptions
7
7
  } from 'fastify';
8
8
 
9
+ export type AppRoutes = Array<MaybeAsync<(app: FastifyInstance) => void>>;
10
+
9
11
  declare module '@gasket/core' {
10
12
  export interface GasketConfig {
11
13
  fastify?: {
12
14
  /** Enable compression */
13
15
  compression?: boolean;
14
- /** Regular expression for excluded routes */
16
+ /** Filter for which request URLs invoke Gasket middleware */
17
+ middlewareInclusionRegex?: RegExp;
18
+ /** @deprecated */
15
19
  excludedRoutesRegex?: RegExp;
16
20
  /** Trust proxy configuration */
17
21
  trustProxy?: FastifyServerOptions['trustProxy'];
18
22
  /** Glob pattern for source files setting up fastify routes */
19
- routes?: string;
23
+ routes?: Array<MaybeAsync<(app: FastifyInstance) => void>>;
20
24
  };
21
25
  /** Middleware configuration */
22
26
  middleware?: {
@@ -45,7 +49,7 @@ declare module '@gasket/core' {
45
49
  middleware(
46
50
  fastify: Fastify
47
51
  ): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
48
- fastify(fastify: Fastify): MaybeAsync<void>;
52
+ fastify(fastify: FastifyInstance): MaybeAsync<void>;
49
53
  errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
50
54
  }
51
55
  }
@@ -59,5 +63,7 @@ declare module 'create-gasket-app' {
59
63
 
60
64
  export default {
61
65
  name: '@gasket/plugin-fastify',
66
+ version: '',
67
+ description: '',
62
68
  hooks: {}
63
69
  };
package/lib/index.js CHANGED
@@ -1,28 +1,21 @@
1
1
  /// <reference types="@gasket/core" />
2
2
  /// <reference types="@gasket/plugin-metadata" />
3
3
 
4
- const { peerDependencies, name } = require('../package.json');
4
+ const {
5
+ name,
6
+ version,
7
+ description
8
+ } = require('../package.json');
9
+ const create = require('./create');
5
10
  const createServers = require('./create-servers');
6
11
 
7
12
  /** @type {import('@gasket/core').Plugin} */
8
13
  const plugin = {
9
14
  name,
15
+ version,
16
+ description,
10
17
  hooks: {
11
- create: async function create(gasket, context) {
12
- const generatorDir = `${__dirname}/../generator`;
13
-
14
- context.pkg.add('dependencies', {
15
- fastify: peerDependencies.fastify
16
- });
17
-
18
- if ('apiApp' in context && context.apiApp) {
19
- context.files.add(`${generatorDir}/**/*`);
20
-
21
- context.gasketConfig.add('fastify', {
22
- routes: './routes/*'
23
- });
24
- }
25
- },
18
+ create,
26
19
  createServers,
27
20
  metadata(gasket, meta) {
28
21
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gasket/plugin-fastify",
3
- "version": "7.0.0-next.34",
3
+ "version": "7.0.0-next.38",
4
4
  "description": "Adds fastify support to your application",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -38,14 +38,8 @@
38
38
  "url": "https://github.com/godaddy/gasket/issues"
39
39
  },
40
40
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-fastify",
41
- "dependencies": {
42
- "compression": "^1.7.4",
43
- "cookie-parser": "^1.4.6",
44
- "diagnostics": "^2.0.2",
45
- "middie": "^5.4.0"
46
- },
47
41
  "devDependencies": {
48
- "@gasket/core": "^7.0.0-next.34",
42
+ "@gasket/core": "^7.0.0-next.38",
49
43
  "cross-env": "^7.0.3",
50
44
  "eslint": "^8.56.0",
51
45
  "eslint-config-godaddy": "^7.1.0",
@@ -76,5 +70,5 @@
76
70
  "eslintIgnore": [
77
71
  "generator"
78
72
  ],
79
- "gitHead": "219ddcd68d2d6efd13a8324da31a7dc51cf2b78a"
73
+ "gitHead": "ba4f49da6e6b7cbbdf4949194ece663caeef5ef5"
80
74
  }