Skip to content

Commit dc01fac

Browse files
feat: remove @gasket/plugin-middleware and migrate to express/fastify… (#1355)
* feat: remove @gasket/plugin-middleware and migrate to express/fastify hooks - Delete @gasket/plugin-middleware package entirely - Migrate @gasket/plugin-morgan from middleware hook to express and fastify lifecycle hooks using native Fastify syntax - Remove middleware lifecycle metadata and after:middleware ordering from express and fastify plugins - Remove compression, trustProxy, middlewareInclusionRegex, excludedRoutesRegex config types and docs - Update README, docs, and examples across the repo to reflect the removal - Clean up stale test mocks and dead link definitions Co-authored-by: Cursor <cursoragent@cursor.com> * revert: restore upgrade-to-7.md to original (immutable historical doc) Co-authored-by: Cursor <cursoragent@cursor.com> * fix: replace remaining middleware lifecycle references with express hook - Update parent metadata from 'middleware' to 'express' in gasket-plugin-intl and gasket-plugin-elastic-apm - Update EXAMPLES.md files across gasket-core, gasket-plugin-nextjs, gasket-plugin-intl, gasket-plugin-elastic-apm, gasket-plugin-data, and gasket-plugin-webpack to use express lifecycle instead of middleware hook Co-authored-by: Cursor <cursoragent@cursor.com> * update * fix: replace app.use() with native Fastify setErrorHandler for errorMiddleware app.use() on Fastify requires @fastify/express or @fastify/middie which was previously provided by the removed middleware plugin. Use setErrorHandler with request.raw/reply.raw to bridge Express-style 4-arg error handlers natively. Co-authored-by: Cursor <cursoragent@cursor.com> * fix: chain errorMiddleware handlers via single setErrorHandler in fastify Previously each handler would override the last via repeated setErrorHandler calls. Now all handlers are chained through a single setErrorHandler using request.raw/reply.raw to bridge the Express 4-arg error signature natively. Co-authored-by: Cursor <cursoragent@cursor.com> * fix: update fastify errorMiddleware docs, types, and inline comment - Fix ErrorHandler type: replace FastifyRequest/FastifyReply with IncomingMessage/ServerResponse to match what request.raw/reply.raw actually provides - Remove unused FastifyRequest and FastifyReply imports - Update errorMiddleware README section to describe setErrorHandler chaining approach - Remove stale @returns JSDoc from fastify hook example - Add inline comment explaining the single setErrorHandler chaining pattern Co-authored-by: Cursor <cursoragent@cursor.com> * docs: simplify errorMiddleware description in fastify README Co-authored-by: Cursor <cursoragent@cursor.com> * changeset * fix: ensure morgan express middleware runs before route handlers via timing Add timing: { first: true } to the express hook in @gasket/plugin-morgan so Morgan middleware is always registered before route handlers regardless of plugin order in gasket.ts. In Express, app.use() and route handlers share the same stack and are processed in registration order, so without this fix Morgan would not intercept requests if pluginMorgan appeared after route plugins. Update tests to reflect the new { timing, handler } hook shape. Co-authored-by: Cursor <cursoragent@cursor.com> * update docs --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 28ca134 commit dc01fac

File tree

54 files changed

+311
-1974
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+311
-1974
lines changed

.changeset/evil-pumas-yell.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"@gasket/template-nextjs-express": patch
3+
"@gasket/template-nextjs-pages": patch
4+
"@gasket/template-api-express": patch
5+
"@gasket/template-api-fastify": patch
6+
"@gasket/template-nextjs-app": patch
7+
"@gasket/plugin-elastic-apm": patch
8+
"@gasket/typescript-tests": patch
9+
"@gasket/plugin-express": patch
10+
"@gasket/plugin-fastify": patch
11+
"@gasket/plugin-swagger": patch
12+
"@gasket/plugin-webpack": patch
13+
"@gasket/plugin-morgan": patch
14+
"@gasket/plugin-nextjs": patch
15+
"@gasket/plugin-data": patch
16+
"@gasket/plugin-intl": patch
17+
"generate-docs-index": patch
18+
"@gasket/react-intl": patch
19+
"@gasket/assets": patch
20+
"@gasket/nextjs": patch
21+
"@gasket/core": patch
22+
"@gasket/data": patch
23+
---
24+
25+
remove middleware plugin

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ Available plugins
194194
| [@gasket/plugin-logger] | 8.0.0-next.0 | Gasket plugin for logging |
195195
| [@gasket/plugin-manifest] | 8.0.0-next.0 | The web app manifest for progressive Gasket applications |
196196
| [@gasket/plugin-metadata] | 8.0.0-next.0 | Adds metadata to gasket lifecycles |
197-
| [@gasket/plugin-middleware] | 8.0.0-next.0 | Handles common server engine setups for routing and executing lifecycles. |
198197
| [@gasket/plugin-mocha] | 8.0.0-next.0 | Integrates mocha based testing in to your Gasket application |
199198
| [@gasket/plugin-morgan] | 8.0.0-next.0 | Adds morgan request logger to your app |
200199
| [@gasket/plugin-nextjs] | 8.0.0-next.0 | Adds Next support to your application |
@@ -392,7 +391,6 @@ Available configuration options in the `gasket.js`
392391
[@gasket/plugin-logger]:/packages/gasket-plugin-logger/README.md
393392
[@gasket/plugin-manifest]:/packages/gasket-plugin-manifest/README.md
394393
[@gasket/plugin-metadata]:/packages/gasket-plugin-metadata/README.md
395-
[@gasket/plugin-middleware]:/packages/gasket-plugin-middleware/README.md
396394
[@gasket/plugin-morgan]:/packages/gasket-plugin-morgan/README.md
397395
[@gasket/plugin-nextjs]:/packages/gasket-plugin-nextjs/README.md
398396
[@gasket/plugin-redux]:/packages/gasket-plugin-redux/README.md

docs/authoring-plugins.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,19 @@ a `Promise`.
5050
If multiple plugins hook the same event, they'll run in parallel if async or in
5151
arbitrary order if synchronous. If a plugin wants to specify more specific
5252
sequencing, the hook should be an object with a timing property and handler
53-
function instead of a plain function. For example, this plugin contains a
54-
`middleware` hook that specifically runs *after* the `@gasket/plugin-nextjs` plugin:
53+
function instead of a plain function. For example, this plugin contains an
54+
`express` hook that specifically runs *after* the `@gasket/plugin-nextjs` plugin:
5555

5656
```js
5757
export default {
5858
name: 'will-run-after-nextjs-plugin',
5959
hooks: {
60-
middleware: {
60+
express: {
6161
timing: {
6262
after: ['@gasket/plugin-nextjs']
6363
},
64-
handler: async function middleware(gasket, app) {
64+
handler: async function express(gasket, app) {
6565
app.use((req, res, next) => {
66-
// custom middleware logic
6766
next();
6867
});
6968
}
@@ -104,7 +103,7 @@ functions available on a `Gasket` object [here].
104103
## Testing
105104
106105
Because Gasket plugins are just Objects of functions, it's fairly trivial to
107-
test them. For example, let's say we have this plugin which hooks the `middleware`
106+
test them. For example, let's say we have this plugin which hooks the `express`
108107
lifecycle.
109108
110109
```js
@@ -113,13 +112,12 @@ export default {
113112
description: 'An example plugin',
114113
version: '1.1.1',
115114
hooks: {
116-
middleware: {
115+
express: {
117116
timing: {
118117
after: ['@gasket/plugin-nextjs']
119118
},
120-
handler: async function middleware(gasket, app) {
119+
handler: async function express(gasket, app) {
121120
app.use((req, res, next) => {
122-
// custom middleware logic
123121
next();
124122
});
125123
}
@@ -136,11 +134,11 @@ import plugin from '/path/to/plugin';
136134
describe('example plugin', function () {
137135
it('hooks the correct lifecycles', function() {
138136
const hooks = plugin.hooks;
139-
expect(Object.keys(hooks)).equals(['middleware']);
137+
expect(Object.keys(hooks)).equals(['express']);
140138
});
141139

142-
it('has the correct middleware hook timings', function () {
143-
expect(plugin.hooks.middleware.timing.after).toEqual(['@gasket/plugin-nextjs']);
140+
it('has the correct express hook timings', function () {
141+
expect(plugin.hooks.express.timing.after).toEqual(['@gasket/plugin-nextjs']);
144142
});
145143

146144
it('has expected properties', function () {

docs/typescript.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,19 @@ import type {
5858
const plugin: Plugin = {
5959
name: 'my-plugin',
6060
hooks: {
61-
middleware(gasket, express) {
62-
return [
63-
async (req, res, next) => {
64-
const customHeader = await gasket.execWaterfall(
65-
'customHeader',
66-
gasket.config.customHeader
67-
);
68-
69-
if (customHeader) {
70-
res.set('x-silly-header', customHeader);
71-
}
72-
73-
next();
61+
express(gasket, app) {
62+
app.use(async (req, res, next) => {
63+
const customHeader = await gasket.execWaterfall(
64+
'customHeader',
65+
gasket.config.customHeader
66+
);
67+
68+
if (customHeader) {
69+
res.set('x-silly-header', customHeader);
7470
}
75-
];
71+
72+
next();
73+
});
7674
}
7775
}
7876
};

packages/gasket-core/EXAMPLES.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,24 @@ console.log(result); // 'Processed: HELLO'
197197
```js
198198
const gasket = makeGasket({
199199
plugins: [{
200-
name: 'middleware-plugin',
200+
name: 'express-plugin',
201201
hooks: {
202-
middleware(gasket) {
203-
return (req, res, next) => {
204-
console.log('Middleware executed');
202+
express(gasket, app) {
203+
app.use((req, res, next) => {
204+
console.log('Handler executed');
205205
next();
206-
};
206+
});
207207
}
208208
}
209209
}]
210210
});
211211

212212
// Apply custom logic to each plugin's hook
213-
const results = await gasket.engine.execApply(gasket, 'middleware', async (plugin, handler) => {
214-
console.log(`Processing middleware from ${plugin.name}`);
215-
const middleware = handler(gasket);
216-
// Could register middleware here
217-
return middleware;
213+
const results = await gasket.engine.execApply(gasket, 'express', async (plugin, handler) => {
214+
console.log(`Processing express hook from ${plugin.name}`);
215+
const result = handler(gasket);
216+
// Could perform additional setup here
217+
return result;
218218
});
219219
```
220220

packages/gasket-data/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ To add to the data exposed in `@gasket/data`, see the [@gasket/plugin-data] docu
4040

4141
<!-- LINKS -->
4242

43-
[middleware lifecycle]:/packages/gasket-plugin-express/README.md#middleware
4443
[script tag]:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
4544
[@gasket/plugin-data]:/packages/gasket-plugin-data/README.md
4645
[publicGasketData]:/packages/gasket-plugin-data#publicgasketdata

packages/gasket-plugin-data/EXAMPLES.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Get the complete Gasket data configuration.
1313
export default {
1414
name: 'example-plugin',
1515
hooks: {
16-
async middleware(gasket, app) {
16+
async express(gasket, app) {
1717
const gasketData = await gasket.actions.getGasketData();
1818
console.log('API URL:', gasketData.apiUrl);
1919
console.log('Private setting:', gasketData.privateKey);
@@ -35,16 +35,16 @@ async function getConfig() {
3535
Get public Gasket data for a specific request. This data is safe to expose to the client.
3636

3737
```js
38-
// In Express middleware
38+
// In Express lifecycle
3939
export default {
4040
name: 'example-plugin',
4141
hooks: {
42-
middleware(gasket) {
43-
return async (req, res, next) => {
42+
express(gasket, app) {
43+
app.use(async (req, res, next) => {
4444
const publicData = await gasket.actions.getPublicGasketData(req);
4545
console.log('Public data:', publicData);
4646
next();
47-
};
47+
});
4848
}
4949
}
5050
};

packages/gasket-plugin-elastic-apm/EXAMPLES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ apm.start({
4242
Access and decorate the current APM transaction:
4343

4444
```js
45-
// In a middleware or lifecycle
45+
// In the express lifecycle
4646
export default {
4747
name: 'my-plugin',
4848
hooks: {
49-
middleware(gasket, app) {
49+
express(gasket, app) {
5050
app.use(async (req, res, next) => {
5151
const transaction = await gasket.actions.getApmTransaction(req);
5252

packages/gasket-plugin-elastic-apm/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ To filter out other data, use the [APM Add Filter API].
130130
According to the [Elastic APM docs], the _Elastic APM agent for Node.js is a
131131
singleton_.
132132
This means that you can import and configure the singleton in various
133-
hooks of your Gasket app, such as with the [init] or [middleware] lifecycles.
133+
hooks of your Gasket app, such as with the [init] or [express]/[fastify] lifecycles.
134134

135135
## Actions
136136

@@ -221,7 +221,8 @@ such as for sensitive cookies.
221221
<!-- LINKS -->
222222

223223
[init]:/packages/gasket-core/README.md#init
224-
[middleware]:/packages/gasket-plugin-middleware/README.md#middleware
224+
[express]:/packages/gasket-plugin-express/README.md#express
225+
[fastify]:/packages/gasket-plugin-fastify/README.md#fastify
225226
[configuration options documentation]:https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html
226227
[start recommendations]:https://www.elastic.co/guide/en/apm/agent/nodejs/master/agent-api.html#apm-start
227228
[Elastic APM docs]:https://www.elastic.co/guide/en/apm/agent/nodejs/master/agent-api.html

packages/gasket-plugin-elastic-apm/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const plugin = {
4444
method: 'exec',
4545
description: 'Modify the APM transaction',
4646
link: 'README.md#apmtransaction',
47-
parent: 'middleware'
47+
parent: 'express'
4848
}
4949
]
5050
};

0 commit comments

Comments
 (0)