@fastify/reply-from 11.0.1 → 11.0.2

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.
@@ -17,7 +17,7 @@ on:
17
17
 
18
18
  jobs:
19
19
  test:
20
- uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5.0.0
20
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
21
21
  with:
22
22
  license-check: true
23
23
  lint: true
package/index.js CHANGED
@@ -20,7 +20,8 @@ const {
20
20
  ConnectionResetError,
21
21
  ConnectTimeoutError,
22
22
  UndiciSocketError,
23
- InternalServerError
23
+ InternalServerError,
24
+ BadGatewayError
24
25
  } = require('./lib/errors')
25
26
 
26
27
  const fastifyReplyFrom = fp(function from (fastify, opts, next) {
@@ -199,7 +200,13 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
199
200
  } else {
200
201
  copyHeaders(rewriteHeaders(res.headers, this.request), this)
201
202
  }
202
- this.code(res.statusCode)
203
+ try {
204
+ this.code(res.statusCode)
205
+ } catch (err) {
206
+ // Since we know `FST_ERR_BAD_STATUS_CODE` will be recieved
207
+ onError(this, { error: new BadGatewayError() })
208
+ this.request.log.warn(err, 'response has invalid status code')
209
+ }
203
210
  if (onResponse) {
204
211
  onResponse(this.request, this, res)
205
212
  } else {
package/lib/errors.js CHANGED
@@ -12,3 +12,4 @@ module.exports.ConnectionResetError = createError('ECONNRESET', 'Connection Rese
12
12
  module.exports.ConnectTimeoutError = createError('UND_ERR_CONNECT_TIMEOUT', 'Connect Timeout Error', 500)
13
13
  module.exports.UndiciSocketError = createError('UND_ERR_SOCKET', 'Undici Socket Error', 500)
14
14
  module.exports.InternalServerError = createError('FST_REPLY_FROM_INTERNAL_SERVER_ERROR', '%s', 500)
15
+ module.exports.BadGatewayError = createError('FST_REPLY_FROM_BAD_GATEWAY', 'Bad Gateway', 502)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "11.0.1",
3
+ "version": "11.0.2",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -56,7 +56,7 @@
56
56
  "end-of-stream": "^1.4.4",
57
57
  "fast-content-type-parse": "^2.0.0",
58
58
  "fast-querystring": "^1.1.2",
59
- "fastify-plugin": "^4.5.1",
59
+ "fastify-plugin": "^5.0.1",
60
60
  "toad-cache": "^3.7.0",
61
61
  "undici": "^6.11.1"
62
62
  },
@@ -0,0 +1,52 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const Fastify = require('fastify')
5
+ const From = require('..')
6
+ const http = require('node:http')
7
+ const get = require('simple-get').concat
8
+
9
+ const instance = Fastify()
10
+ instance.register(From)
11
+
12
+ t.plan(8)
13
+ t.teardown(instance.close.bind(instance))
14
+
15
+ const target = http.createServer((req, res) => {
16
+ t.pass('request proxied')
17
+ t.equal(req.method, 'GET')
18
+ res.statusCode = 888
19
+ res.end('non-standard status code')
20
+ })
21
+
22
+ instance.get('/', (_, reply) => {
23
+ reply.from(`http://localhost:${target.address().port}`, {
24
+ onResponse: (_, reply, res) => {
25
+ t.equal(res.statusCode, 888)
26
+ }
27
+ })
28
+ })
29
+
30
+ t.teardown(target.close.bind(target))
31
+
32
+ instance.listen({ port: 0 }, (err) => {
33
+ t.error(err)
34
+
35
+ target.listen({ port: 0 }, (err) => {
36
+ t.error(err)
37
+
38
+ get(
39
+ `http://localhost:${instance.server.address().port}`,
40
+ (err, res, data) => {
41
+ t.error(err)
42
+ t.equal(res.statusCode, 502)
43
+ t.same(JSON.parse(data), {
44
+ statusCode: 502,
45
+ code: 'FST_REPLY_FROM_BAD_GATEWAY',
46
+ error: 'Bad Gateway',
47
+ message: 'Bad Gateway'
48
+ })
49
+ }
50
+ )
51
+ })
52
+ })