@gasket/plugin-fastify 7.0.1-canary.12 → 7.0.1
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 +3 -70
- package/lib/create.js +61 -0
- package/lib/index.d.ts +29 -9
- package/lib/index.js +31 -15
- package/lib/utils.js +26 -0
- package/package.json +13 -17
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
|
+
});
|
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,12 @@ 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 { 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
|
-
});
|
|
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 = [];
|
|
47
|
-
|
|
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
|
-
}
|
|
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
|
-
});
|
|
78
|
-
|
|
12
|
+
const app = gasket.actions.getFastifyApp();
|
|
79
13
|
// allow consuming apps to directly append options to their server
|
|
80
14
|
await gasket.exec('fastify', app);
|
|
81
15
|
|
|
82
|
-
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(
|
|
83
|
-
|
|
84
|
-
);
|
|
16
|
+
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(Boolean);
|
|
17
|
+
// @ts-ignore
|
|
85
18
|
postRenderingStacks.forEach((stack) => app.use(stack));
|
|
86
19
|
|
|
87
20
|
return {
|
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,22 +1,34 @@
|
|
|
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 { Http2SecureServer, Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
13
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
14
|
+
|
|
15
|
+
export type AppRoutes = Array<MaybeAsync<(app: FastifyInstance) => void>>;
|
|
8
16
|
|
|
9
17
|
declare module '@gasket/core' {
|
|
18
|
+
export interface GasketActions {
|
|
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
|
-
/** Glob pattern for source files setting up fastify routes */
|
|
19
|
-
routes?: string;
|
|
20
32
|
};
|
|
21
33
|
/** Middleware configuration */
|
|
22
34
|
middleware?: {
|
|
@@ -43,21 +55,29 @@ declare module '@gasket/core' {
|
|
|
43
55
|
|
|
44
56
|
export interface HookExecTypes {
|
|
45
57
|
middleware(
|
|
46
|
-
|
|
58
|
+
app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>
|
|
47
59
|
): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
|
|
48
|
-
fastify(
|
|
60
|
+
fastify(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>): MaybeAsync<void>;
|
|
49
61
|
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
|
|
50
62
|
}
|
|
51
63
|
}
|
|
52
64
|
|
|
65
|
+
export function alignLogger(
|
|
66
|
+
logger: Logger
|
|
67
|
+
): FastifyBaseLogger
|
|
68
|
+
|
|
53
69
|
declare module 'create-gasket-app' {
|
|
54
70
|
export interface CreateContext {
|
|
55
71
|
/** Flag indicating if API app is enabled */
|
|
56
72
|
apiApp?: boolean;
|
|
73
|
+
addApiRoutes?: boolean;
|
|
74
|
+
typescript?: boolean;
|
|
57
75
|
}
|
|
58
76
|
}
|
|
59
77
|
|
|
60
|
-
|
|
78
|
+
const plugin: Plugin = {
|
|
61
79
|
name: '@gasket/plugin-fastify',
|
|
62
80
|
hooks: {}
|
|
63
81
|
};
|
|
82
|
+
|
|
83
|
+
export = plugin;
|
package/lib/index.js
CHANGED
|
@@ -1,32 +1,48 @@
|
|
|
1
1
|
/// <reference types="@gasket/core" />
|
|
2
2
|
/// <reference types="@gasket/plugin-metadata" />
|
|
3
3
|
|
|
4
|
-
const {
|
|
4
|
+
const {
|
|
5
|
+
name,
|
|
6
|
+
version,
|
|
7
|
+
description
|
|
8
|
+
} = require('../package.json');
|
|
9
|
+
const create = require('./create');
|
|
5
10
|
const createServers = require('./create-servers');
|
|
11
|
+
const fastify = require('fastify');
|
|
12
|
+
const { alignLogger } = require('./utils');
|
|
13
|
+
|
|
14
|
+
let app;
|
|
6
15
|
|
|
7
16
|
/** @type {import('@gasket/core').Plugin} */
|
|
8
17
|
const plugin = {
|
|
9
18
|
name,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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);
|
|
13
26
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
app ??= fastify({ logger: fastifyLogger, trustProxy, https, http2 });
|
|
17
29
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
},
|
|
30
|
+
return app;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
hooks: {
|
|
34
|
+
create,
|
|
26
35
|
createServers,
|
|
27
36
|
metadata(gasket, meta) {
|
|
28
37
|
return {
|
|
29
38
|
...meta,
|
|
39
|
+
actions: [
|
|
40
|
+
{
|
|
41
|
+
name: 'getFastifyApp',
|
|
42
|
+
description: 'Get the Fastify app instance',
|
|
43
|
+
link: 'README.md#getFastifyApp'
|
|
44
|
+
}
|
|
45
|
+
],
|
|
30
46
|
lifecycles: [
|
|
31
47
|
{
|
|
32
48
|
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.0.1
|
|
3
|
+
"version": "7.0.1",
|
|
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,26 @@
|
|
|
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
|
-
"dependencies": {
|
|
42
|
-
"compression": "^1.7.4",
|
|
43
|
-
"cookie-parser": "^1.4.6",
|
|
44
|
-
"diagnostics": "^2.0.2",
|
|
45
|
-
"middie": "^5.4.0"
|
|
46
|
-
},
|
|
47
39
|
"devDependencies": {
|
|
48
|
-
"@gasket/core": "^7.0.1
|
|
40
|
+
"@gasket/core": "^7.0.1",
|
|
49
41
|
"cross-env": "^7.0.3",
|
|
50
42
|
"eslint": "^8.56.0",
|
|
51
|
-
"eslint-config-godaddy": "^7.1.
|
|
52
|
-
"eslint-plugin-jest": "^
|
|
43
|
+
"eslint-config-godaddy": "^7.1.1",
|
|
44
|
+
"eslint-plugin-jest": "^28.6.0",
|
|
53
45
|
"eslint-plugin-json": "^3.1.0",
|
|
54
|
-
"eslint-plugin-unicorn": "^
|
|
55
|
-
"fastify": "^
|
|
46
|
+
"eslint-plugin-unicorn": "^55.0.0",
|
|
47
|
+
"fastify": "^4.28.1",
|
|
48
|
+
"http2": "^3.3.7",
|
|
56
49
|
"jest": "^29.7.0",
|
|
57
50
|
"typescript": "^5.4.5"
|
|
58
51
|
},
|
|
59
52
|
"peerDependencies": {
|
|
60
|
-
"fastify": "^3
|
|
53
|
+
"fastify": "^3 || ^4"
|
|
61
54
|
},
|
|
62
55
|
"eslintConfig": {
|
|
63
56
|
"extends": [
|
|
@@ -76,5 +69,8 @@
|
|
|
76
69
|
"eslintIgnore": [
|
|
77
70
|
"generator"
|
|
78
71
|
],
|
|
79
|
-
"
|
|
72
|
+
"jest": {
|
|
73
|
+
"rootDir": "test"
|
|
74
|
+
},
|
|
75
|
+
"gitHead": "0426b7f57bc2618606955912c0f3cd55174f5367"
|
|
80
76
|
}
|