@gasket/plugin-fastify 7.2.0-canary.20 → 7.2.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 +34 -64
- package/generator/app/plugins/README.md +11 -0
- package/generator/app/plugins/routes-plugin.js +30 -0
- package/generator/app/plugins/routes-plugin.ts +29 -0
- package/generator/jest/jest.config.js +21 -0
- package/generator/jest/test/index.test.js +20 -0
- package/generator/jest/test/index.test.ts +26 -0
- package/generator/mocha/test/index.test.js +23 -0
- package/generator/mocha/test/index.test.ts +23 -0
- package/lib/create-servers.js +10 -68
- package/lib/create.js +62 -0
- package/lib/index.d.ts +31 -11
- package/lib/index.js +27 -16
- package/lib/utils.js +26 -0
- package/package.json +13 -15
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,72 +30,44 @@ 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)
|
|
34
|
+
- `disableRequestLogging`: Turn off request logging, true by default
|
|
39
35
|
|
|
40
36
|
#### Example configuration
|
|
41
37
|
|
|
42
38
|
```js
|
|
43
|
-
|
|
44
|
-
plugins:
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
export default makeGasket({
|
|
40
|
+
plugins: [
|
|
41
|
+
pluginFastify
|
|
42
|
+
],
|
|
47
43
|
fastify: {
|
|
48
44
|
compression: false,
|
|
45
|
+
routes: 'api/*.js',
|
|
49
46
|
excludedRoutesRegex: /^(?!\/_next\/)/,
|
|
50
47
|
trustProxy: true
|
|
51
48
|
}
|
|
52
|
-
}
|
|
49
|
+
});
|
|
53
50
|
```
|
|
54
51
|
|
|
55
|
-
###
|
|
52
|
+
### Route Definition
|
|
56
53
|
|
|
57
|
-
|
|
58
|
-
objects that map plugins to route or path patterns, allowing apps to tune which
|
|
59
|
-
middleware are triggered for which requests.
|
|
54
|
+
Routes can be defined in a in-app plugin in the `plugins` directory. The plugin will hook the `fastify` lifecycle to add the routes to the fastify app.
|
|
60
55
|
|
|
61
56
|
```js
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
## Lifecycles
|
|
79
|
-
|
|
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 = {
|
|
57
|
+
// plugins/routes-plugin.js
|
|
58
|
+
export default {
|
|
59
|
+
name: 'routes-plugin',
|
|
87
60
|
hooks: {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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')();
|
|
61
|
+
fastify: async function (gasket, app) {
|
|
62
|
+
app.get('/hello', (req, res) => {
|
|
63
|
+
res.send('Hello World!');
|
|
64
|
+
});
|
|
97
65
|
}
|
|
98
66
|
}
|
|
99
|
-
}
|
|
67
|
+
};
|
|
100
68
|
```
|
|
101
69
|
|
|
102
|
-
|
|
70
|
+
## Lifecycles
|
|
103
71
|
|
|
104
72
|
### fastify
|
|
105
73
|
|
|
@@ -107,7 +75,8 @@ Executed **after** the `middleware` event for when you need full control over
|
|
|
107
75
|
the `fastify` instance.
|
|
108
76
|
|
|
109
77
|
```js
|
|
110
|
-
|
|
78
|
+
export default {
|
|
79
|
+
name: 'sample-plugin',
|
|
111
80
|
hooks: {
|
|
112
81
|
/**
|
|
113
82
|
* Update Fastify app instance
|
|
@@ -119,7 +88,7 @@ module.exports = {
|
|
|
119
88
|
fastify: async function (gasket, fastify) {
|
|
120
89
|
}
|
|
121
90
|
}
|
|
122
|
-
}
|
|
91
|
+
};
|
|
123
92
|
```
|
|
124
93
|
|
|
125
94
|
### errorMiddleware
|
|
@@ -128,7 +97,8 @@ Executed after the `fastify` event. All middleware functions returned from this
|
|
|
128
97
|
hook will be applied to Fastify.
|
|
129
98
|
|
|
130
99
|
```js
|
|
131
|
-
|
|
100
|
+
export default {
|
|
101
|
+
name: 'sample-plugin',
|
|
132
102
|
hooks: {
|
|
133
103
|
/**
|
|
134
104
|
* Add Fastify error middlewares
|
|
@@ -139,7 +109,7 @@ module.exports = {
|
|
|
139
109
|
errorMiddleware: function (gasket) {
|
|
140
110
|
}
|
|
141
111
|
}
|
|
142
|
-
}
|
|
112
|
+
};
|
|
143
113
|
```
|
|
144
114
|
|
|
145
115
|
## How it works
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const defaultHandler = async (req, res) => {
|
|
2
|
+
if (res.statusCode === 200) {
|
|
3
|
+
res.send({ message: 'Welcome to your default route...' });
|
|
4
|
+
}
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
name: 'routes-plugin',
|
|
9
|
+
hooks: {
|
|
10
|
+
fastify(gasket, app) {
|
|
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
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const defaultHandler = async (req, res) => {
|
|
2
|
+
if (res.statusCode === 200) {
|
|
3
|
+
res.send({ message: 'Welcome to your default route...' });
|
|
4
|
+
}
|
|
5
|
+
};
|
|
6
|
+
export default {
|
|
7
|
+
name: 'routes-plugin',
|
|
8
|
+
hooks: {
|
|
9
|
+
fastify(gasket, app) {
|
|
10
|
+
{{#if useSwagger }}
|
|
11
|
+
/**
|
|
12
|
+
* @swagger
|
|
13
|
+
*
|
|
14
|
+
* /default:
|
|
15
|
+
* get:
|
|
16
|
+
* summary: "Get default route"
|
|
17
|
+
* produces:
|
|
18
|
+
* - "application/json"
|
|
19
|
+
* responses:
|
|
20
|
+
* "200":
|
|
21
|
+
* description: "Returns welcome message."
|
|
22
|
+
* content:
|
|
23
|
+
* application/json
|
|
24
|
+
*/
|
|
25
|
+
{{/if }}
|
|
26
|
+
app.get('/default', defaultHandler);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{{#if typescript}}
|
|
2
|
+
const config = {
|
|
3
|
+
preset: 'ts-jest/presets/default-esm',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
injectGlobals: true,
|
|
6
|
+
moduleDirectories: ['node_modules', '<rootDir>'],
|
|
7
|
+
moduleNameMapper: {
|
|
8
|
+
'^(\\.{1,2}/.*)\\.js$': '$1'
|
|
9
|
+
},
|
|
10
|
+
transform: {
|
|
11
|
+
'^.+.ts$': ['ts-jest', { useESM: true }]
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default config;
|
|
16
|
+
{{else}}
|
|
17
|
+
export default {
|
|
18
|
+
testEnvironment: 'node',
|
|
19
|
+
injectGlobals: true
|
|
20
|
+
};
|
|
21
|
+
{{/if}}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import { defaultHandler } from '../plugins/routes-plugin.js';
|
|
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 '../plugins/routes-plugin.js';
|
|
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 '../plugins/routes-plugin.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 '../plugins/routes-plugin.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
|
+
});
|
package/lib/create-servers.js
CHANGED
|
@@ -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,21 @@ 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
|
|
19
|
-
const {
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
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
|
-
});
|
|
34
|
-
|
|
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
|
-
}
|
|
45
|
-
|
|
46
|
-
const middlewares = [];
|
|
12
|
+
const fastify = require('fastify');
|
|
13
|
+
const { alignLogger } = require('./utils');
|
|
14
|
+
const { fastify: fastifyConfig = {}, http2, https } = gasket.config;
|
|
15
|
+
const { trustProxy = false, disableRequestLogging = true } = fastifyConfig;
|
|
16
|
+
const fastifyLogger = alignLogger(gasket.logger);
|
|
47
17
|
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
}
|
|
62
|
-
}
|
|
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);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
const app = fastify({ logger: fastifyLogger, trustProxy, https, http2, disableRequestLogging });
|
|
78
20
|
|
|
79
21
|
// allow consuming apps to directly append options to their server
|
|
22
|
+
// @ts-ignore
|
|
80
23
|
await gasket.exec('fastify', app);
|
|
81
24
|
|
|
82
|
-
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(
|
|
83
|
-
|
|
84
|
-
);
|
|
25
|
+
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(Boolean);
|
|
26
|
+
// @ts-ignore
|
|
85
27
|
postRenderingStacks.forEach((stack) => app.use(stack));
|
|
86
28
|
|
|
87
29
|
return {
|
package/lib/create.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
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}/*`, `${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)';
|
|
57
|
+
files.add(`${generatorDir}/app/**/${globIgnore}`);
|
|
58
|
+
gasketConfig.addPlugin('pluginRoutes', './plugins/routes-plugin.js');
|
|
59
|
+
|
|
60
|
+
createTestFiles({ files, generatorDir, testPlugins, globIgnore });
|
|
61
|
+
}
|
|
62
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
|
-
import type { MaybeAsync, MaybeMultiple } from '@gasket/core';
|
|
1
|
+
import type { MaybeAsync, MaybeMultiple, Plugin } from '@gasket/core';
|
|
2
|
+
import { Logger } from '@gasket/plugin-logger';
|
|
2
3
|
import type {
|
|
3
|
-
|
|
4
|
+
FastifyInstance,
|
|
4
5
|
FastifyRequest,
|
|
5
6
|
FastifyReply,
|
|
6
|
-
FastifyServerOptions
|
|
7
|
+
FastifyServerOptions,
|
|
8
|
+
FastifyBaseLogger,
|
|
9
|
+
FastifyTypeProviderDefault,
|
|
10
|
+
RawServerDefault
|
|
7
11
|
} from 'fastify';
|
|
12
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
13
|
+
|
|
14
|
+
export type AppRoutes = Array<MaybeAsync<(app: FastifyInstance) => void>>;
|
|
8
15
|
|
|
9
16
|
declare module '@gasket/core' {
|
|
17
|
+
export interface GasketActions {
|
|
18
|
+
/** @deprecated */
|
|
19
|
+
getFastifyApp(): FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>;
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
export interface GasketConfig {
|
|
11
23
|
fastify?: {
|
|
12
24
|
/** Enable compression */
|
|
13
25
|
compression?: boolean;
|
|
14
|
-
/**
|
|
26
|
+
/** Filter for which request URLs invoke Gasket middleware */
|
|
27
|
+
middlewareInclusionRegex?: RegExp;
|
|
28
|
+
/** @deprecated */
|
|
15
29
|
excludedRoutesRegex?: RegExp;
|
|
16
30
|
/** Trust proxy configuration */
|
|
17
31
|
trustProxy?: FastifyServerOptions['trustProxy'];
|
|
18
|
-
/**
|
|
19
|
-
|
|
32
|
+
/** Fastify request logging per route */
|
|
33
|
+
disableRequestLogging?: boolean;
|
|
20
34
|
};
|
|
21
35
|
/** Middleware configuration */
|
|
22
36
|
middleware?: {
|
|
@@ -43,23 +57,29 @@ declare module '@gasket/core' {
|
|
|
43
57
|
|
|
44
58
|
export interface HookExecTypes {
|
|
45
59
|
middleware(
|
|
46
|
-
|
|
60
|
+
app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>
|
|
47
61
|
): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
|
|
48
|
-
fastify(
|
|
62
|
+
fastify(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>): MaybeAsync<void>;
|
|
49
63
|
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
|
|
50
64
|
}
|
|
51
65
|
}
|
|
52
66
|
|
|
67
|
+
export function alignLogger(
|
|
68
|
+
logger: Logger
|
|
69
|
+
): FastifyBaseLogger
|
|
70
|
+
|
|
53
71
|
declare module 'create-gasket-app' {
|
|
54
72
|
export interface CreateContext {
|
|
55
73
|
/** Flag indicating if API app is enabled */
|
|
56
74
|
apiApp?: boolean;
|
|
75
|
+
addApiRoutes?: boolean;
|
|
76
|
+
typescript?: boolean;
|
|
57
77
|
}
|
|
58
78
|
}
|
|
59
79
|
|
|
60
|
-
|
|
80
|
+
const plugin: Plugin = {
|
|
61
81
|
name: '@gasket/plugin-fastify',
|
|
62
|
-
version: '',
|
|
63
|
-
description: '',
|
|
64
82
|
hooks: {}
|
|
65
83
|
};
|
|
84
|
+
|
|
85
|
+
export = plugin;
|
package/lib/index.js
CHANGED
|
@@ -4,36 +4,47 @@
|
|
|
4
4
|
const {
|
|
5
5
|
name,
|
|
6
6
|
version,
|
|
7
|
-
description
|
|
8
|
-
peerDependencies
|
|
7
|
+
description
|
|
9
8
|
} = require('../package.json');
|
|
9
|
+
const create = require('./create');
|
|
10
10
|
const createServers = require('./create-servers');
|
|
11
|
+
const fastify = require('fastify');
|
|
12
|
+
const { alignLogger } = require('./utils');
|
|
13
|
+
|
|
14
|
+
let app;
|
|
11
15
|
|
|
12
16
|
/** @type {import('@gasket/core').Plugin} */
|
|
13
17
|
const plugin = {
|
|
14
18
|
name,
|
|
15
19
|
version,
|
|
16
20
|
description,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
actions: {
|
|
22
|
+
/** @deprecated */
|
|
23
|
+
getFastifyApp(gasket) {
|
|
24
|
+
const { fastify: fastifyConfig = {}, http2, https } = gasket.config;
|
|
25
|
+
const { trustProxy = false, disableRequestLogging = true } = fastifyConfig;
|
|
26
|
+
const fastifyLogger = alignLogger(gasket.logger);
|
|
20
27
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
});
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
app ??= fastify({ logger: fastifyLogger, trustProxy, https, http2, disableRequestLogging });
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
},
|
|
31
|
+
return app;
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
hooks: {
|
|
35
|
+
create,
|
|
33
36
|
createServers,
|
|
34
37
|
metadata(gasket, meta) {
|
|
35
38
|
return {
|
|
36
39
|
...meta,
|
|
40
|
+
actions: [
|
|
41
|
+
{
|
|
42
|
+
name: 'getFastifyApp',
|
|
43
|
+
description: 'Get the Fastify app instance',
|
|
44
|
+
link: 'README.md#getFastifyApp',
|
|
45
|
+
deprecated: true
|
|
46
|
+
}
|
|
47
|
+
],
|
|
37
48
|
lifecycles: [
|
|
38
49
|
{
|
|
39
50
|
name: 'middleware',
|
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.2.0
|
|
3
|
+
"version": "7.2.0",
|
|
4
4
|
"description": "Adds fastify support to your application",
|
|
5
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": {
|
|
@@ -30,34 +31,28 @@
|
|
|
30
31
|
"plugin"
|
|
31
32
|
],
|
|
32
33
|
"author": "GoDaddy Operating Company, LLC",
|
|
33
|
-
"maintainers": [
|
|
34
|
-
"Sivan Mehta <smehta@godaddy.com>"
|
|
35
|
-
],
|
|
36
34
|
"license": "MIT",
|
|
37
35
|
"bugs": {
|
|
38
36
|
"url": "https://github.com/godaddy/gasket/issues"
|
|
39
37
|
},
|
|
40
38
|
"homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-fastify",
|
|
41
39
|
"dependencies": {
|
|
42
|
-
"
|
|
43
|
-
"cookie-parser": "^1.4.6",
|
|
44
|
-
"diagnostics": "^2.0.2",
|
|
45
|
-
"middie": "^5.4.0"
|
|
40
|
+
"fastify": "^4.28.1"
|
|
46
41
|
},
|
|
47
42
|
"devDependencies": {
|
|
48
|
-
"@gasket/core": "^7.2.0
|
|
43
|
+
"@gasket/core": "^7.2.0",
|
|
49
44
|
"cross-env": "^7.0.3",
|
|
50
45
|
"eslint": "^8.56.0",
|
|
51
|
-
"eslint-config-godaddy": "^7.1.
|
|
52
|
-
"eslint-plugin-jest": "^
|
|
46
|
+
"eslint-config-godaddy": "^7.1.1",
|
|
47
|
+
"eslint-plugin-jest": "^28.6.0",
|
|
53
48
|
"eslint-plugin-json": "^3.1.0",
|
|
54
|
-
"eslint-plugin-unicorn": "^
|
|
55
|
-
"fastify": "^
|
|
49
|
+
"eslint-plugin-unicorn": "^55.0.0",
|
|
50
|
+
"fastify": "^4.28.1",
|
|
56
51
|
"jest": "^29.7.0",
|
|
57
52
|
"typescript": "^5.4.5"
|
|
58
53
|
},
|
|
59
54
|
"peerDependencies": {
|
|
60
|
-
"fastify": "^
|
|
55
|
+
"fastify": "^4"
|
|
61
56
|
},
|
|
62
57
|
"eslintConfig": {
|
|
63
58
|
"extends": [
|
|
@@ -76,5 +71,8 @@
|
|
|
76
71
|
"eslintIgnore": [
|
|
77
72
|
"generator"
|
|
78
73
|
],
|
|
79
|
-
"
|
|
74
|
+
"jest": {
|
|
75
|
+
"rootDir": "test"
|
|
76
|
+
},
|
|
77
|
+
"gitHead": "8790fd065f4bcb853fc9a2deecf0833999f41443"
|
|
80
78
|
}
|