@gasket/plugin-fastify 7.0.0-next.8 → 7.0.0
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 +22 -71
- package/generator/app/routes/index.js +28 -0
- package/generator/app/routes/index.ts +29 -0
- package/generator/jest/jest.config.js +4 -0
- package/generator/jest/jest.config.ts +17 -0
- package/generator/jest/test/index.test.js +20 -0
- package/generator/jest/test/index.test.ts +26 -0
- package/generator/jest/tsconfig.test.json +7 -0
- package/generator/mocha/test/index.test.js +23 -0
- package/generator/mocha/test/index.test.ts +23 -0
- package/lib/create-servers.js +27 -0
- package/lib/create.js +61 -0
- package/lib/index.d.ts +69 -14
- package/lib/index.js +85 -148
- package/lib/utils.js +26 -0
- package/package.json +23 -22
package/README.md
CHANGED
|
@@ -6,26 +6,22 @@ Adds Fastify to your application.
|
|
|
6
6
|
|
|
7
7
|
#### New apps
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
gasket create <app-name> --plugins @gasket/plugin-fastify
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
#### Existing apps
|
|
14
|
-
|
|
15
9
|
```
|
|
16
10
|
npm i @gasket/plugin-fastify
|
|
17
11
|
```
|
|
18
12
|
|
|
19
|
-
|
|
13
|
+
Update your `gasket` file plugin configuration:
|
|
20
14
|
|
|
21
15
|
```diff
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
// gasket.js
|
|
17
|
+
|
|
18
|
+
+ import pluginFastify from '@gasket/plugin-fastify';
|
|
19
|
+
|
|
20
|
+
export default makeGasket({
|
|
21
|
+
plugins: [
|
|
22
|
+
+ pluginFastify
|
|
23
|
+
]
|
|
24
|
+
});
|
|
29
25
|
```
|
|
30
26
|
|
|
31
27
|
## Configuration
|
|
@@ -34,80 +30,34 @@ All the configurations for the plugin are added under `fastify` in the config:
|
|
|
34
30
|
|
|
35
31
|
- `compression`: true by default. Can be set to false if applying compression
|
|
36
32
|
differently.
|
|
37
|
-
- `excludedRoutesRegex`: Routes to be excluded based on a regex
|
|
38
33
|
- `trustProxy`: Enable trust proxy option, [see Fastify documentation for possible values](https://fastify.dev/docs/latest/Reference/Server/#trustproxy)
|
|
39
34
|
|
|
40
35
|
#### Example configuration
|
|
41
36
|
|
|
42
37
|
```js
|
|
43
|
-
|
|
44
|
-
plugins:
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
export default makeGasket({
|
|
39
|
+
plugins: [
|
|
40
|
+
pluginFastify
|
|
41
|
+
],
|
|
47
42
|
fastify: {
|
|
48
43
|
compression: false,
|
|
44
|
+
routes: 'api/*.js',
|
|
49
45
|
excludedRoutesRegex: /^(?!\/_next\/)/,
|
|
50
46
|
trustProxy: true
|
|
51
47
|
}
|
|
52
|
-
}
|
|
53
|
-
```
|
|
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
|
-
]
|
|
48
|
+
});
|
|
76
49
|
```
|
|
77
50
|
|
|
78
51
|
## Lifecycles
|
|
79
52
|
|
|
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
53
|
### fastify
|
|
105
54
|
|
|
106
55
|
Executed **after** the `middleware` event for when you need full control over
|
|
107
56
|
the `fastify` instance.
|
|
108
57
|
|
|
109
58
|
```js
|
|
110
|
-
|
|
59
|
+
export default {
|
|
60
|
+
name: 'sample-plugin',
|
|
111
61
|
hooks: {
|
|
112
62
|
/**
|
|
113
63
|
* Update Fastify app instance
|
|
@@ -119,7 +69,7 @@ module.exports = {
|
|
|
119
69
|
fastify: async function (gasket, fastify) {
|
|
120
70
|
}
|
|
121
71
|
}
|
|
122
|
-
}
|
|
72
|
+
};
|
|
123
73
|
```
|
|
124
74
|
|
|
125
75
|
### errorMiddleware
|
|
@@ -128,7 +78,8 @@ Executed after the `fastify` event. All middleware functions returned from this
|
|
|
128
78
|
hook will be applied to Fastify.
|
|
129
79
|
|
|
130
80
|
```js
|
|
131
|
-
|
|
81
|
+
export default {
|
|
82
|
+
name: 'sample-plugin',
|
|
132
83
|
hooks: {
|
|
133
84
|
/**
|
|
134
85
|
* Add Fastify error middlewares
|
|
@@ -139,7 +90,7 @@ module.exports = {
|
|
|
139
90
|
errorMiddleware: function (gasket) {
|
|
140
91
|
}
|
|
141
92
|
}
|
|
142
|
-
}
|
|
93
|
+
};
|
|
143
94
|
```
|
|
144
95
|
|
|
145
96
|
## How it works
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import gasket from '../gasket.js';
|
|
2
|
+
|
|
3
|
+
const app = gasket.actions.getFastifyApp();
|
|
4
|
+
|
|
5
|
+
export const defaultHandler = async (req, res) => {
|
|
6
|
+
if (res.statusCode === 200) {
|
|
7
|
+
res.send({ message: 'Welcome to your default route...' });
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
{{#if useSwagger}}
|
|
12
|
+
/**
|
|
13
|
+
* @swagger
|
|
14
|
+
*
|
|
15
|
+
* /default:
|
|
16
|
+
* get:
|
|
17
|
+
* summary: "Get default route"
|
|
18
|
+
* produces:
|
|
19
|
+
* - "application/json"
|
|
20
|
+
* responses:
|
|
21
|
+
* "200":
|
|
22
|
+
* description: "Returns welcome message."
|
|
23
|
+
* content:
|
|
24
|
+
* application/json
|
|
25
|
+
*/
|
|
26
|
+
{{/if}}
|
|
27
|
+
app.get('/default', defaultHandler);
|
|
28
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { FastifyRequest, FastifyReply } from 'fastify';
|
|
2
|
+
import gasket from '../gasket.js';
|
|
3
|
+
|
|
4
|
+
const app = gasket.actions.getFastifyApp();
|
|
5
|
+
|
|
6
|
+
export const defaultHandler = async (req: FastifyRequest, res: FastifyReply) => {
|
|
7
|
+
if (res.statusCode === 200) {
|
|
8
|
+
res.send({ message: 'Welcome to your default route...' });
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
{{#if useSwagger}}
|
|
13
|
+
/**
|
|
14
|
+
* @swagger
|
|
15
|
+
*
|
|
16
|
+
* /default:
|
|
17
|
+
* get:
|
|
18
|
+
* summary: "Get default route"
|
|
19
|
+
* produces:
|
|
20
|
+
* - "application/json"
|
|
21
|
+
* responses:
|
|
22
|
+
* "200":
|
|
23
|
+
* description: "Returns welcome message."
|
|
24
|
+
* content:
|
|
25
|
+
* application/json
|
|
26
|
+
*/
|
|
27
|
+
{{/if}}
|
|
28
|
+
app.get('/default', defaultHandler);
|
|
29
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JestConfigWithTsJest } from 'ts-jest';
|
|
2
|
+
|
|
3
|
+
const config: JestConfigWithTsJest = {
|
|
4
|
+
preset: 'ts-jest/presets/default-esm',
|
|
5
|
+
testEnvironment: 'node',
|
|
6
|
+
injectGlobals: true,
|
|
7
|
+
moduleDirectories: ['node_modules', '<rootDir>'],
|
|
8
|
+
moduleNameMapper: {
|
|
9
|
+
'^(\\.{1,2}/.*)\\.js$': '$1'
|
|
10
|
+
},
|
|
11
|
+
transform: {
|
|
12
|
+
'^.+.ts$': ['ts-jest', { useESM: true }]
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default config;
|
|
17
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import { defaultHandler } from '../routes';
|
|
3
|
+
|
|
4
|
+
describe('Routes', () => {
|
|
5
|
+
let mockRequest, mockResponse;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
mockRequest = {};
|
|
8
|
+
mockResponse = {
|
|
9
|
+
statusCode: 200,
|
|
10
|
+
send: jest.fn()
|
|
11
|
+
};
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('defaultHandler should use expeced message', async () => {
|
|
15
|
+
await defaultHandler(mockRequest, mockResponse);
|
|
16
|
+
expect(mockResponse.send).toHaveBeenCalledWith({
|
|
17
|
+
message: 'Welcome to your default route...'
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
jest,
|
|
3
|
+
describe,
|
|
4
|
+
beforeEach,
|
|
5
|
+
it,
|
|
6
|
+
expect
|
|
7
|
+
} from '@jest/globals';
|
|
8
|
+
import { defaultHandler } from '../routes';
|
|
9
|
+
|
|
10
|
+
describe('Routes', () => {
|
|
11
|
+
let mockRequest, mockResponse;
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
mockRequest = {};
|
|
14
|
+
mockResponse = {
|
|
15
|
+
statusCode: 200,
|
|
16
|
+
send: jest.fn()
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('defaultHandler should use expeced message', async () => {
|
|
21
|
+
await defaultHandler(mockRequest, mockResponse);
|
|
22
|
+
expect(mockResponse.send).toHaveBeenCalledWith({
|
|
23
|
+
message: 'Welcome to your default route...'
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import sinon from 'sinon';
|
|
3
|
+
import { defaultHandler } from '../routes/index.js';
|
|
4
|
+
|
|
5
|
+
describe('Routes', () => {
|
|
6
|
+
let mockRequest, mockResponse, sendSpy;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
mockRequest = {};
|
|
10
|
+
sendSpy = sinon.spy();
|
|
11
|
+
mockResponse = {
|
|
12
|
+
statusCode: 200,
|
|
13
|
+
send: sendSpy
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('defaultHandler should use expected message', async () => {
|
|
18
|
+
await defaultHandler(mockRequest, mockResponse);
|
|
19
|
+
expect(sendSpy.calledWith({
|
|
20
|
+
message: 'Welcome to your default route...'
|
|
21
|
+
})).to.be.true;
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import sinon from 'sinon';
|
|
3
|
+
import { defaultHandler } from '../routes/index.js';
|
|
4
|
+
|
|
5
|
+
describe('Routes', () => {
|
|
6
|
+
let mockRequest, mockResponse, sendSpy;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
mockRequest = {};
|
|
10
|
+
sendSpy = sinon.spy();
|
|
11
|
+
mockResponse = {
|
|
12
|
+
statusCode: 200,
|
|
13
|
+
send: sendSpy
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('defaultHandler should use expected message', async () => {
|
|
18
|
+
await defaultHandler(mockRequest, mockResponse);
|
|
19
|
+
expect(sendSpy.calledWith({
|
|
20
|
+
message: 'Welcome to your default route...'
|
|
21
|
+
})).to.be.true;
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="@gasket/plugin-https" />
|
|
2
|
+
/// <reference types="@gasket/plugin-logger" />
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Create the Fastify instance and setup the lifecycle hooks.
|
|
6
|
+
* Fastify is compatible with express middleware out of the box, so we can
|
|
7
|
+
* use the same middleware lifecycles.
|
|
8
|
+
* @type {import('@gasket/core').HookHandler<'createServers'>}
|
|
9
|
+
*/
|
|
10
|
+
// eslint-disable-next-line max-statements
|
|
11
|
+
module.exports = async function createServers(gasket, serverOpts) {
|
|
12
|
+
const app = gasket.actions.getFastifyApp();
|
|
13
|
+
// allow consuming apps to directly append options to their server
|
|
14
|
+
await gasket.exec('fastify', app);
|
|
15
|
+
|
|
16
|
+
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(Boolean);
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
postRenderingStacks.forEach((stack) => app.use(stack));
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
...serverOpts,
|
|
22
|
+
handler: async function handler(...args) {
|
|
23
|
+
await app.ready();
|
|
24
|
+
app.server.emit('request', ...args);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
package/lib/create.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/// <reference types="@gasket/core" />
|
|
2
|
+
|
|
3
|
+
const { name, version, devDependencies } = require('../package.json');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* createTestFiles
|
|
7
|
+
* @property {Files} files - The Gasket Files API.
|
|
8
|
+
* @property {generatorDir} - The directory of the generator.
|
|
9
|
+
* @property {testPlugins} - Array of selected test plugins
|
|
10
|
+
* @property {globIgnore} - Extension pattern to ignore
|
|
11
|
+
*/
|
|
12
|
+
function createTestFiles({ files, generatorDir, testPlugins, globIgnore }) {
|
|
13
|
+
if (!testPlugins || testPlugins.length === 0) return;
|
|
14
|
+
const unit = ['jest', 'mocha'];
|
|
15
|
+
const integration = ['cypress'];
|
|
16
|
+
const frameworks = [...unit, ...integration];
|
|
17
|
+
const frameworksRegex = new RegExp(frameworks.join('|'));
|
|
18
|
+
|
|
19
|
+
testPlugins.forEach((testPlugin) => {
|
|
20
|
+
const match = frameworksRegex.exec(testPlugin);
|
|
21
|
+
if (match) {
|
|
22
|
+
const matchedFramework = match[0];
|
|
23
|
+
if (unit.includes(matchedFramework)) {
|
|
24
|
+
files.add(`${generatorDir}/${matchedFramework}/*/${globIgnore}`, `${generatorDir}/${matchedFramework}/**/${globIgnore}`);
|
|
25
|
+
} else {
|
|
26
|
+
files.add(`${generatorDir}/${matchedFramework}/*`, `${generatorDir}/${matchedFramework}/**/*`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Add files & extend package.json for new apps.
|
|
34
|
+
* @type {import('@gasket/core').HookHandler<'create'>}
|
|
35
|
+
*/
|
|
36
|
+
module.exports = async function create(gasket, context) {
|
|
37
|
+
const {
|
|
38
|
+
files,
|
|
39
|
+
typescript,
|
|
40
|
+
apiApp,
|
|
41
|
+
addApiRoutes = true,
|
|
42
|
+
testPlugins,
|
|
43
|
+
pkg,
|
|
44
|
+
gasketConfig
|
|
45
|
+
} = context;
|
|
46
|
+
const generatorDir = `${__dirname}/../generator`;
|
|
47
|
+
|
|
48
|
+
gasketConfig.addPlugin('pluginFastify', name);
|
|
49
|
+
|
|
50
|
+
pkg.add('dependencies', {
|
|
51
|
+
[name]: `^${version}`,
|
|
52
|
+
fastify: devDependencies.fastify
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (apiApp && addApiRoutes) {
|
|
56
|
+
const globIgnore = typescript ? '!(*.js)' : '!(*.ts|*.json)';
|
|
57
|
+
files.add(`${generatorDir}/app/**/${globIgnore}`);
|
|
58
|
+
|
|
59
|
+
createTestFiles({ files, generatorDir, testPlugins, globIgnore });
|
|
60
|
+
}
|
|
61
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,51 @@
|
|
|
1
|
-
import type { MaybeAsync, MaybeMultiple } from '@gasket/
|
|
2
|
-
import
|
|
1
|
+
import type { MaybeAsync, MaybeMultiple, Plugin } from '@gasket/core';
|
|
2
|
+
import { Logger } from '@gasket/plugin-logger';
|
|
3
|
+
import type {
|
|
4
|
+
FastifyInstance,
|
|
5
|
+
FastifyRequest,
|
|
6
|
+
FastifyReply,
|
|
7
|
+
FastifyServerOptions,
|
|
8
|
+
FastifyBaseLogger,
|
|
9
|
+
FastifyTypeProviderDefault,
|
|
10
|
+
RawServerDefault
|
|
11
|
+
} from 'fastify';
|
|
12
|
+
import { Http2SecureServer, Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
13
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
14
|
+
|
|
15
|
+
export type AppRoutes = Array<MaybeAsync<(app: FastifyInstance) => void>>;
|
|
16
|
+
|
|
17
|
+
declare module '@gasket/core' {
|
|
18
|
+
export interface GasketActions {
|
|
19
|
+
getFastifyApp(): FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>;
|
|
20
|
+
}
|
|
3
21
|
|
|
4
|
-
declare module '@gasket/engine' {
|
|
5
22
|
export interface GasketConfig {
|
|
6
|
-
|
|
7
|
-
|
|
23
|
+
fastify?: {
|
|
24
|
+
/** Enable compression */
|
|
25
|
+
compression?: boolean;
|
|
26
|
+
/** Filter for which request URLs invoke Gasket middleware */
|
|
27
|
+
middlewareInclusionRegex?: RegExp;
|
|
28
|
+
/** @deprecated */
|
|
29
|
+
excludedRoutesRegex?: RegExp;
|
|
30
|
+
/** Trust proxy configuration */
|
|
31
|
+
trustProxy?: FastifyServerOptions['trustProxy'];
|
|
32
|
+
};
|
|
33
|
+
/** Middleware configuration */
|
|
34
|
+
middleware?: {
|
|
35
|
+
/** Plugin name */
|
|
36
|
+
plugin: string;
|
|
37
|
+
/** Paths for middleware */
|
|
38
|
+
paths?: (string | RegExp)[];
|
|
39
|
+
}[];
|
|
8
40
|
}
|
|
9
41
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Handler function type - The middie middleware is added for express
|
|
44
|
+
* compatibility
|
|
45
|
+
*/
|
|
46
|
+
type Handler = (req: any, res: any, next: (error?: Error) => void) => void;
|
|
47
|
+
|
|
48
|
+
/** Error handler function type */
|
|
16
49
|
type ErrorHandler = (
|
|
17
50
|
error: Error,
|
|
18
51
|
req: FastifyRequest,
|
|
@@ -21,8 +54,30 @@ declare module '@gasket/engine' {
|
|
|
21
54
|
) => void;
|
|
22
55
|
|
|
23
56
|
export interface HookExecTypes {
|
|
24
|
-
middleware(
|
|
25
|
-
|
|
26
|
-
|
|
57
|
+
middleware(
|
|
58
|
+
app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>
|
|
59
|
+
): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
|
|
60
|
+
fastify(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>): MaybeAsync<void>;
|
|
61
|
+
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
|
|
27
62
|
}
|
|
28
63
|
}
|
|
64
|
+
|
|
65
|
+
export function alignLogger(
|
|
66
|
+
logger: Logger
|
|
67
|
+
): FastifyBaseLogger
|
|
68
|
+
|
|
69
|
+
declare module 'create-gasket-app' {
|
|
70
|
+
export interface CreateContext {
|
|
71
|
+
/** Flag indicating if API app is enabled */
|
|
72
|
+
apiApp?: boolean;
|
|
73
|
+
addApiRoutes?: boolean;
|
|
74
|
+
typescript?: boolean;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const plugin: Plugin = {
|
|
79
|
+
name: '@gasket/plugin-fastify',
|
|
80
|
+
hooks: {}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export = plugin;
|
package/lib/index.js
CHANGED
|
@@ -1,161 +1,98 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/// <reference types="@gasket/core" />
|
|
2
|
+
/// <reference types="@gasket/plugin-metadata" />
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const {
|
|
5
5
|
name,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* @public
|
|
14
|
-
*/
|
|
15
|
-
create: async function create(gasket, context) {
|
|
16
|
-
const generatorDir = `${ __dirname }/../generator`;
|
|
17
|
-
|
|
18
|
-
context.pkg.add('dependencies', {
|
|
19
|
-
fastify: peerDependencies.fastify
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
if (context.apiApp) {
|
|
23
|
-
context.files.add(`${ generatorDir }/**/*`);
|
|
24
|
-
|
|
25
|
-
context.gasketConfig.add('fastify', {
|
|
26
|
-
routes: './routes/*'
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
/**
|
|
31
|
-
* Create the Fastify instance and setup the lifecycle hooks.
|
|
32
|
-
* Fastify is compatible with express middleware out of the box, so we can
|
|
33
|
-
* use the same middleware lifecycles.
|
|
34
|
-
*
|
|
35
|
-
* @param {Gasket} gasket Gasket API.
|
|
36
|
-
* @param {Object} serverOpts Server options.
|
|
37
|
-
* @returns {Express} The web server.
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
// eslint-disable-next-line max-statements
|
|
41
|
-
createServers: async function createServers(gasket, serverOpts) {
|
|
42
|
-
const fastify = require('fastify');
|
|
43
|
-
const middie = require('middie');
|
|
44
|
-
const cookieParser = require('cookie-parser');
|
|
45
|
-
const compression = require('compression');
|
|
46
|
-
|
|
47
|
-
const { logger, config } = gasket;
|
|
48
|
-
const { middleware: middlewareConfig } = config;
|
|
49
|
-
const trustProxy = config.fastify?.trustProxy ?? false;
|
|
50
|
-
const excludedRoutesRegex = config.fastify && config.fastify.excludedRoutesRegex;
|
|
51
|
-
const app = fastify({ logger, trustProxy });
|
|
52
|
-
|
|
53
|
-
// Enable middleware for fastify@3
|
|
54
|
-
await app.register(middie);
|
|
55
|
-
|
|
56
|
-
// Add express-like `res.locals` object attaching data
|
|
57
|
-
app.use(function attachLocals(req, res, next) {
|
|
58
|
-
res.locals = {};
|
|
59
|
-
next();
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (excludedRoutesRegex) {
|
|
63
|
-
app.use(excludedRoutesRegex, cookieParser());
|
|
64
|
-
} else {
|
|
65
|
-
app.use(cookieParser());
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const { compression: compressionConfig = true } = config.fastify || {};
|
|
69
|
-
if (compressionConfig) {
|
|
70
|
-
app.use(compression());
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const middlewares = [];
|
|
74
|
-
await gasket.execApply('middleware', async (plugin, handler) => {
|
|
75
|
-
const middleware = await handler(app);
|
|
76
|
-
if (middleware) {
|
|
77
|
-
if (middlewareConfig) {
|
|
78
|
-
const pluginName = plugin && plugin.name ? plugin.name : '';
|
|
79
|
-
const mwConfig = middlewareConfig.find(mw => mw.plugin === pluginName);
|
|
80
|
-
if (mwConfig) {
|
|
81
|
-
middleware.paths = mwConfig.paths;
|
|
82
|
-
if (excludedRoutesRegex) {
|
|
83
|
-
middleware.paths.push(excludedRoutesRegex);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
middlewares.push(middleware);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
6
|
+
version,
|
|
7
|
+
description
|
|
8
|
+
} = require('../package.json');
|
|
9
|
+
const create = require('./create');
|
|
10
|
+
const createServers = require('./create-servers');
|
|
11
|
+
const fastify = require('fastify');
|
|
12
|
+
const { alignLogger } = require('./utils');
|
|
90
13
|
|
|
91
|
-
|
|
92
|
-
middlewares.forEach(layer => {
|
|
93
|
-
const { paths } = layer;
|
|
94
|
-
if (paths) {
|
|
95
|
-
app.use(paths, layer);
|
|
96
|
-
} else if (excludedRoutesRegex) {
|
|
97
|
-
app.use(excludedRoutesRegex, layer);
|
|
98
|
-
} else {
|
|
99
|
-
app.use(layer);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
14
|
+
let app;
|
|
102
15
|
|
|
103
|
-
|
|
104
|
-
|
|
16
|
+
/** @type {import('@gasket/core').Plugin} */
|
|
17
|
+
const plugin = {
|
|
18
|
+
name,
|
|
19
|
+
version,
|
|
20
|
+
description,
|
|
21
|
+
actions: {
|
|
22
|
+
getFastifyApp(gasket) {
|
|
23
|
+
const { fastify: fastifyConfig = {}, http2, https } = gasket.config;
|
|
24
|
+
const { trustProxy = false } = fastifyConfig;
|
|
25
|
+
const fastifyLogger = alignLogger(gasket.logger);
|
|
105
26
|
|
|
106
|
-
|
|
107
|
-
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
app ??= fastify({ logger: fastifyLogger, trustProxy, https, http2 });
|
|
108
29
|
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
};
|
|
116
|
-
},
|
|
30
|
+
return app;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
hooks: {
|
|
34
|
+
create,
|
|
35
|
+
createServers,
|
|
117
36
|
metadata(gasket, meta) {
|
|
118
37
|
return {
|
|
119
38
|
...meta,
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
39
|
+
actions: [
|
|
40
|
+
{
|
|
41
|
+
name: 'getFastifyApp',
|
|
42
|
+
description: 'Get the Fastify app instance',
|
|
43
|
+
link: 'README.md#getFastifyApp'
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
lifecycles: [
|
|
47
|
+
{
|
|
48
|
+
name: 'middleware',
|
|
49
|
+
method: 'exec',
|
|
50
|
+
description: 'Add Express style middleware for Fastify',
|
|
51
|
+
link: 'README.md#middleware',
|
|
52
|
+
parent: 'createServers'
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'fastify',
|
|
56
|
+
method: 'exec',
|
|
57
|
+
description: 'Modify the Fastify instance to for adding endpoints',
|
|
58
|
+
link: 'README.md#express',
|
|
59
|
+
parent: 'createServers',
|
|
60
|
+
after: 'middleware'
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'errorMiddleware',
|
|
64
|
+
method: 'exec',
|
|
65
|
+
description:
|
|
66
|
+
'Add Express style middleware for handling errors with Fastify',
|
|
67
|
+
link: 'README.md#errorMiddleware',
|
|
68
|
+
parent: 'createServers',
|
|
69
|
+
after: 'express'
|
|
70
|
+
}
|
|
71
|
+
],
|
|
72
|
+
configurations: [
|
|
73
|
+
{
|
|
74
|
+
name: 'fastify',
|
|
75
|
+
link: 'README.md#configuration',
|
|
76
|
+
description: 'Fastify configuration object',
|
|
77
|
+
type: 'object'
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: 'fastify.compression',
|
|
81
|
+
link: 'README.md#configuration',
|
|
82
|
+
description: 'Automatic compression',
|
|
83
|
+
type: 'boolean',
|
|
84
|
+
default: true
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: 'fastify.excludedRoutesRegex',
|
|
88
|
+
link: 'README.md#configuration',
|
|
89
|
+
description: 'Routes to be excluded based on a regex',
|
|
90
|
+
type: 'RegExp'
|
|
91
|
+
}
|
|
92
|
+
]
|
|
158
93
|
};
|
|
159
94
|
}
|
|
160
95
|
}
|
|
161
96
|
};
|
|
97
|
+
|
|
98
|
+
module.exports = plugin;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
const fallbackMap = {
|
|
4
|
+
fatal: 'error',
|
|
5
|
+
trace: 'debug'
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A helper function to ensure that all the required log levels
|
|
10
|
+
* for a Fastify server are present.
|
|
11
|
+
* @type {import('./index').alignLogger}
|
|
12
|
+
*/
|
|
13
|
+
function alignLogger(logger) {
|
|
14
|
+
const fastifyLogger = logger;
|
|
15
|
+
['fatal', 'trace'].map(level => {
|
|
16
|
+
if (!logger[level]) {
|
|
17
|
+
fastifyLogger[level] = logger[fallbackMap[level]];
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return fastifyLogger;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = {
|
|
25
|
+
alignLogger
|
|
26
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gasket/plugin-fastify",
|
|
3
|
-
"version": "7.0.0
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "Adds fastify support to your application",
|
|
5
|
-
"main": "lib",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
+
"generator",
|
|
8
9
|
"lib"
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
@@ -13,7 +14,9 @@
|
|
|
13
14
|
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
|
|
14
15
|
"test:watch": "jest --watch",
|
|
15
16
|
"test:coverage": "jest --coverage",
|
|
16
|
-
"posttest": "npm run lint"
|
|
17
|
+
"posttest": "npm run lint && npm run typecheck",
|
|
18
|
+
"typecheck": "tsc",
|
|
19
|
+
"typecheck:watch": "tsc --watch"
|
|
17
20
|
},
|
|
18
21
|
"repository": {
|
|
19
22
|
"type": "git",
|
|
@@ -28,41 +31,36 @@
|
|
|
28
31
|
"plugin"
|
|
29
32
|
],
|
|
30
33
|
"author": "GoDaddy Operating Company, LLC",
|
|
31
|
-
"maintainers": [
|
|
32
|
-
"Sivan Mehta <smehta@godaddy.com>"
|
|
33
|
-
],
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"bugs": {
|
|
36
36
|
"url": "https://github.com/godaddy/gasket/issues"
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-fastify",
|
|
39
|
-
"dependencies": {
|
|
40
|
-
"compression": "^1.7.4",
|
|
41
|
-
"cookie-parser": "^1.4.6",
|
|
42
|
-
"diagnostics": "^2.0.2",
|
|
43
|
-
"middie": "^5.4.0"
|
|
44
|
-
},
|
|
45
39
|
"devDependencies": {
|
|
46
|
-
"@gasket/
|
|
40
|
+
"@gasket/core": "7.0.0",
|
|
47
41
|
"cross-env": "^7.0.3",
|
|
48
42
|
"eslint": "^8.56.0",
|
|
49
|
-
"eslint-config-godaddy": "^7.
|
|
50
|
-
"eslint-plugin-jest": "^
|
|
43
|
+
"eslint-config-godaddy": "^7.1.1",
|
|
44
|
+
"eslint-plugin-jest": "^28.6.0",
|
|
51
45
|
"eslint-plugin-json": "^3.1.0",
|
|
52
|
-
"eslint-plugin-unicorn": "^
|
|
53
|
-
"fastify": "^
|
|
54
|
-
"
|
|
46
|
+
"eslint-plugin-unicorn": "^55.0.0",
|
|
47
|
+
"fastify": "^4.28.1",
|
|
48
|
+
"http2": "^3.3.7",
|
|
49
|
+
"jest": "^29.7.0",
|
|
50
|
+
"typescript": "^5.4.5"
|
|
55
51
|
},
|
|
56
52
|
"peerDependencies": {
|
|
57
|
-
"fastify": "^3
|
|
53
|
+
"fastify": "^3 || ^4"
|
|
58
54
|
},
|
|
59
55
|
"eslintConfig": {
|
|
60
56
|
"extends": [
|
|
61
57
|
"godaddy",
|
|
62
|
-
"plugin:jest/recommended"
|
|
58
|
+
"plugin:jest/recommended",
|
|
59
|
+
"plugin:jsdoc/recommended-typescript-flavor"
|
|
63
60
|
],
|
|
64
61
|
"plugins": [
|
|
65
|
-
"unicorn"
|
|
62
|
+
"unicorn",
|
|
63
|
+
"jsdoc"
|
|
66
64
|
],
|
|
67
65
|
"rules": {
|
|
68
66
|
"unicorn/filename-case": "error"
|
|
@@ -71,5 +69,8 @@
|
|
|
71
69
|
"eslintIgnore": [
|
|
72
70
|
"generator"
|
|
73
71
|
],
|
|
74
|
-
"
|
|
72
|
+
"jest": {
|
|
73
|
+
"rootDir": "test"
|
|
74
|
+
},
|
|
75
|
+
"gitHead": "0d353a2d81f967662947ae4ed2cd949505417dab"
|
|
75
76
|
}
|