@gasket/plugin-fastify 8.0.0-next.2 → 8.0.0-next.3
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 +9 -11
- package/lib/create-servers.js +19 -9
- package/lib/index.d.ts +3 -7
- package/lib/index.js +1 -15
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -40,8 +40,6 @@ The plugin automatically detects your Fastify version and applies the correct co
|
|
|
40
40
|
|
|
41
41
|
All the configurations for the plugin are added under `fastify` in the config:
|
|
42
42
|
|
|
43
|
-
- `compression`: true by default. Can be set to false if applying compression
|
|
44
|
-
differently.
|
|
45
43
|
- `trustProxy`: Enable trust proxy option, [see Fastify documentation for possible values](https://fastify.dev/docs/latest/Reference/Server/#trustproxy)
|
|
46
44
|
- `disableRequestLogging`: Turn off request logging, true by default
|
|
47
45
|
|
|
@@ -53,8 +51,6 @@ export default makeGasket({
|
|
|
53
51
|
pluginFastify
|
|
54
52
|
],
|
|
55
53
|
fastify: {
|
|
56
|
-
compression: false,
|
|
57
|
-
excludedRoutesRegex: /^(?!\/_next\/)/,
|
|
58
54
|
trustProxy: true
|
|
59
55
|
}
|
|
60
56
|
});
|
|
@@ -82,8 +78,7 @@ export default {
|
|
|
82
78
|
|
|
83
79
|
### fastify
|
|
84
80
|
|
|
85
|
-
|
|
86
|
-
the `fastify` instance.
|
|
81
|
+
Used to add routes and middleware directly to the `fastify` instance.
|
|
87
82
|
|
|
88
83
|
```js
|
|
89
84
|
export default {
|
|
@@ -94,7 +89,6 @@ export default {
|
|
|
94
89
|
*
|
|
95
90
|
* @param {Gasket} gasket The Gasket API
|
|
96
91
|
* @param {Fastify} fastify Fastify app instance
|
|
97
|
-
* @returns {function|function[]} middleware(s)
|
|
98
92
|
*/
|
|
99
93
|
fastify: async function (gasket, fastify) {
|
|
100
94
|
}
|
|
@@ -104,20 +98,24 @@ export default {
|
|
|
104
98
|
|
|
105
99
|
### errorMiddleware
|
|
106
100
|
|
|
107
|
-
Executed after the `fastify` event. All
|
|
108
|
-
hook will be applied to Fastify.
|
|
101
|
+
Executed after the `fastify` event. All error handler functions returned from this
|
|
102
|
+
hook will be applied to Fastify in order.
|
|
109
103
|
|
|
110
104
|
```js
|
|
111
105
|
export default {
|
|
112
106
|
name: 'sample-plugin',
|
|
113
107
|
hooks: {
|
|
114
108
|
/**
|
|
115
|
-
* Add
|
|
109
|
+
* Add error handling for Fastify
|
|
116
110
|
*
|
|
117
111
|
* @param {Gasket} gasket The Gasket API
|
|
118
|
-
* @returns {function|function[]} error
|
|
112
|
+
* @returns {function|function[]} error handler(s)
|
|
119
113
|
*/
|
|
120
114
|
errorMiddleware: function (gasket) {
|
|
115
|
+
return function (err, req, res, next) {
|
|
116
|
+
// handle error or pass to next handler
|
|
117
|
+
next(err);
|
|
118
|
+
};
|
|
121
119
|
}
|
|
122
120
|
}
|
|
123
121
|
};
|
package/lib/create-servers.js
CHANGED
|
@@ -5,23 +5,33 @@ import { getAppInstance } from './utils.js';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Create the Fastify instance and setup the lifecycle hooks.
|
|
8
|
-
* Fastify is compatible with express middleware out of the box, so we can
|
|
9
|
-
* use the same middleware lifecycles.
|
|
10
8
|
* @type {import('@gasket/core').HookHandler<'createServers'>}
|
|
11
9
|
*/
|
|
12
10
|
export default async function createServers(gasket, serverOpts) {
|
|
13
|
-
|
|
14
|
-
const app = /** @type {import('fastify').FastifyInstance & { use: Function }} */ (getAppInstance(gasket));
|
|
11
|
+
const app = getAppInstance(gasket);
|
|
15
12
|
|
|
16
13
|
// allow consuming apps to directly append options to their server
|
|
17
14
|
await gasket.exec('fastify', app);
|
|
18
15
|
|
|
19
16
|
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(Boolean);
|
|
20
|
-
postRenderingStacks.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
if (postRenderingStacks.length) {
|
|
18
|
+
// Fastify only supports one setErrorHandler per scope, so all Express-style
|
|
19
|
+
// error handlers (err, req, res, next) are chained manually through a single
|
|
20
|
+
// handler. request.raw and reply.raw expose the underlying Node.js
|
|
21
|
+
// IncomingMessage and ServerResponse that Express-style handlers expect.
|
|
22
|
+
app.setErrorHandler((error, request, reply) => {
|
|
23
|
+
let index = 0;
|
|
24
|
+
const next = (err) => {
|
|
25
|
+
const current = err || error;
|
|
26
|
+
if (index < postRenderingStacks.length) {
|
|
27
|
+
postRenderingStacks[index++](current, request.raw, reply.raw, next);
|
|
28
|
+
} else if (!reply.sent) {
|
|
29
|
+
reply.send(current);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
next(error);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
25
35
|
|
|
26
36
|
return {
|
|
27
37
|
...serverOpts,
|
package/lib/index.d.ts
CHANGED
|
@@ -2,8 +2,6 @@ import type { MaybeAsync, MaybeMultiple, Plugin } from '@gasket/core';
|
|
|
2
2
|
import type { Logger } from '@gasket/plugin-logger';
|
|
3
3
|
import type {
|
|
4
4
|
FastifyInstance,
|
|
5
|
-
FastifyRequest,
|
|
6
|
-
FastifyReply,
|
|
7
5
|
FastifyServerOptions,
|
|
8
6
|
FastifyBaseLogger,
|
|
9
7
|
FastifyTypeProviderDefault,
|
|
@@ -12,8 +10,6 @@ import type {
|
|
|
12
10
|
import type { IncomingMessage, ServerResponse } from 'http';
|
|
13
11
|
|
|
14
12
|
export interface FastifyConfig {
|
|
15
|
-
/** Enable compression */
|
|
16
|
-
compression?: boolean;
|
|
17
13
|
/** Trust proxy configuration */
|
|
18
14
|
trustProxy?: FastifyServerOptions['trustProxy'];
|
|
19
15
|
/** Fastify request logging per route */
|
|
@@ -36,11 +32,11 @@ declare module '@gasket/core' {
|
|
|
36
32
|
}
|
|
37
33
|
|
|
38
34
|
|
|
39
|
-
/** Error handler function type */
|
|
35
|
+
/** Error handler function type (Express-style, receives raw Node.js req/res) */
|
|
40
36
|
type ErrorHandler = (
|
|
41
37
|
error: Error,
|
|
42
|
-
req:
|
|
43
|
-
res:
|
|
38
|
+
req: IncomingMessage,
|
|
39
|
+
res: ServerResponse,
|
|
44
40
|
next: (error?: Error) => void
|
|
45
41
|
) => void;
|
|
46
42
|
|
package/lib/index.js
CHANGED
|
@@ -31,8 +31,7 @@ const plugin = {
|
|
|
31
31
|
method: 'exec',
|
|
32
32
|
description: 'Modify the Fastify instance to for adding endpoints',
|
|
33
33
|
link: 'README.md#express',
|
|
34
|
-
parent: 'createServers'
|
|
35
|
-
after: 'middleware'
|
|
34
|
+
parent: 'createServers'
|
|
36
35
|
},
|
|
37
36
|
{
|
|
38
37
|
name: 'errorMiddleware',
|
|
@@ -50,19 +49,6 @@ const plugin = {
|
|
|
50
49
|
link: 'README.md#configuration',
|
|
51
50
|
description: 'Fastify configuration object',
|
|
52
51
|
type: 'object'
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
name: 'fastify.compression',
|
|
56
|
-
link: 'README.md#configuration',
|
|
57
|
-
description: 'Automatic compression',
|
|
58
|
-
type: 'boolean',
|
|
59
|
-
default: true
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
name: 'fastify.excludedRoutesRegex',
|
|
63
|
-
link: 'README.md#configuration',
|
|
64
|
-
description: 'Routes to be excluded based on a regex',
|
|
65
|
-
type: 'RegExp'
|
|
66
52
|
}
|
|
67
53
|
]
|
|
68
54
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gasket/plugin-fastify",
|
|
3
|
-
"version": "8.0.0-next.
|
|
3
|
+
"version": "8.0.0-next.3",
|
|
4
4
|
"description": "Adds fastify support to your application",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
},
|
|
17
17
|
"./package.json": "./package.json"
|
|
18
18
|
},
|
|
19
|
-
"repository":
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "godaddy/gasket.git"
|
|
22
|
+
},
|
|
20
23
|
"publishConfig": {
|
|
21
24
|
"access": "public"
|
|
22
25
|
},
|
|
@@ -33,11 +36,11 @@
|
|
|
33
36
|
"@types/connect": "^3.4.38",
|
|
34
37
|
"fastify": "^5.0.0",
|
|
35
38
|
"vitest": "^3.2.0",
|
|
36
|
-
"@gasket/core": "^8.0.0-next.
|
|
39
|
+
"@gasket/core": "^8.0.0-next.2",
|
|
37
40
|
"@gasket/plugin-https": "^8.0.0-next.1",
|
|
38
41
|
"@gasket/plugin-logger": "^8.0.0-next.1",
|
|
39
42
|
"@gasket/plugin-metadata": "^8.0.0-next.1",
|
|
40
|
-
"create-gasket-app": "^8.0.0-next.
|
|
43
|
+
"create-gasket-app": "^8.0.0-next.2"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
46
|
"fastify": "^4.29.1 || ^5.0.0"
|