@fastify/express 2.3.0 → 3.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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Set default behavior to automatically convert line endings
2
+ * text=auto eol=lf
@@ -2,6 +2,11 @@ name: CI
2
2
 
3
3
  on:
4
4
  push:
5
+ branches:
6
+ - main
7
+ - master
8
+ - next
9
+ - 'v*'
5
10
  paths-ignore:
6
11
  - 'docs/**'
7
12
  - '*.md'
package/README.md CHANGED
@@ -143,7 +143,7 @@ async function subsystem (fastify, opts) {
143
143
  ### Hooks and middlewares
144
144
 
145
145
  Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important.
146
- Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Lifecycle/) documentation page to understand better how every request is executed.
146
+ Take a look at the [Lifecycle](https://fastify.dev/docs/latest/Reference/Lifecycle) documentation page to understand better how every request is executed.
147
147
 
148
148
  ```js
149
149
  const fastify = require('fastify')()
@@ -174,7 +174,7 @@ If you need to run a middleware only under certain path(s), just pass the path a
174
174
 
175
175
  ```js
176
176
  const fastify = require('fastify')()
177
- const path = require('path')
177
+ const path = require('node:path')
178
178
  const serveStatic = require('serve-static')
179
179
 
180
180
  fastify
package/index.js CHANGED
@@ -40,13 +40,19 @@ function fastifyExpress (fastify, options, next) {
40
40
  req.raw = new Proxy(req.raw, createProxyHandler(req))
41
41
  }
42
42
 
43
- req.raw.originalUrl = req.raw.url
43
+ const { url } = req.raw
44
+ req.raw.originalUrl = url
44
45
  req.raw.id = req.id
45
46
  req.raw.hostname = req.hostname
46
47
  req.raw.ip = req.ip
47
48
  req.raw.ips = req.ips
48
49
  req.raw.log = req.log
49
50
  reply.raw.log = req.log
51
+ reply.raw.send = function send (...args) {
52
+ // Restore req.raw.url to its original value https://github.com/fastify/fastify-express/issues/11
53
+ req.raw.url = url
54
+ return reply.send.apply(reply, args)
55
+ }
50
56
 
51
57
  // backward compatibility for body-parser
52
58
  if (req.body) {
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "2.3.0",
3
+ "version": "3.0.0",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
+ "type": "commonjs",
6
7
  "types": "types/index.d.ts",
7
8
  "scripts": {
8
9
  "lint": "standard",
@@ -34,14 +35,14 @@
34
35
  "body-parser": "^1.20.0",
35
36
  "cors": "^2.8.5",
36
37
  "fastify": "^4.0.0-rc.3",
37
- "helmet": "^6.0.0",
38
- "passport": "^0.6.0",
38
+ "helmet": "^7.0.0",
39
+ "passport": "^0.7.0",
39
40
  "passport-http-bearer": "^1.0.1",
40
41
  "serve-static": "^1.14.1",
41
42
  "simple-get": "^4.0.0",
42
43
  "standard": "^17.0.0",
43
44
  "tap": "^16.0.0",
44
- "tsd": "^0.25.0"
45
+ "tsd": "^0.31.0"
45
46
  },
46
47
  "dependencies": {
47
48
  "express": "^4.17.1",
@@ -578,7 +578,7 @@ test('Middleware chain (with errors) / 2', t => {
578
578
  })
579
579
 
580
580
  test('Send a response from a middleware', t => {
581
- t.plan(4)
581
+ t.plan(5)
582
582
 
583
583
  const fastify = Fastify()
584
584
 
@@ -602,8 +602,9 @@ test('Send a response from a middleware', t => {
602
602
  t.fail('We should not be here')
603
603
  })
604
604
 
605
- fastify.addHook('onSend', (req, reply, next) => {
606
- t.fail('We should not be here')
605
+ fastify.addHook('onSend', (req, reply, payload, next) => {
606
+ t.ok('called')
607
+ next(null, payload)
607
608
  })
608
609
 
609
610
  fastify.addHook('onResponse', (req, reply, next) => {
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { test } = require('tap')
4
4
  const Fastify = require('fastify')
5
+ const express = require('express')
5
6
  const expressPlugin = require('../index')
6
7
 
7
8
  test('onSend hook should receive valid request and reply objects if middleware fails', t => {
@@ -35,3 +36,37 @@ test('onSend hook should receive valid request and reply objects if middleware f
35
36
  t.equal(res.statusCode, 500)
36
37
  })
37
38
  })
39
+
40
+ test('request.url is not mutated between onRequest and onResponse', t => {
41
+ t.plan(4)
42
+ const fastify = Fastify()
43
+ const targetUrl = '/hubba/bubba'
44
+
45
+ fastify.addHook('onRequest', (request, _, next) => {
46
+ t.equal(request.url, targetUrl)
47
+ next()
48
+ })
49
+
50
+ fastify.addHook('onResponse', (request, _, next) => {
51
+ t.equal(request.url, targetUrl)
52
+ next()
53
+ })
54
+
55
+ fastify.register(expressPlugin).after(() => {
56
+ const mainRouter = express.Router()
57
+ const innerRouter = express.Router()
58
+ mainRouter.use('/hubba', innerRouter)
59
+ innerRouter.get('/bubba', (req, res) => {
60
+ res.sendStatus(200)
61
+ })
62
+ fastify.use(mainRouter)
63
+ })
64
+
65
+ fastify.inject({
66
+ method: 'GET',
67
+ url: targetUrl
68
+ }, (err, res) => {
69
+ t.error(err)
70
+ t.equal(res.statusCode, 200)
71
+ })
72
+ })
@@ -253,7 +253,7 @@ test('middlewares with prefix', t => {
253
253
  })
254
254
 
255
255
  test('res.end should block middleware execution', t => {
256
- t.plan(5)
256
+ t.plan(6)
257
257
 
258
258
  const instance = fastify()
259
259
  t.teardown(instance.close)
@@ -278,7 +278,8 @@ test('res.end should block middleware execution', t => {
278
278
  })
279
279
 
280
280
  instance.addHook('onSend', (req, reply, payload, next) => {
281
- t.fail('this should not be called')
281
+ t.ok('called')
282
+ next(null, payload)
282
283
  })
283
284
 
284
285
  instance.addHook('onResponse', (request, reply, next) => {