@fastify/reply-from 12.3.0 → 12.3.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 CHANGED
@@ -320,7 +320,7 @@ Given example
320
320
  ```js
321
321
  const customRetryLogic = ({err, req, res, attempt, getDefaultRetry}) => {
322
322
  //If this block is not included all non 500 errors will not be retried
323
- const defaultDelay = getDefaultDelay();
323
+ const defaultDelay = getDefaultDelay(req, res, err, attempt);
324
324
  if (defaultDelay) return defaultDelay();
325
325
 
326
326
  //Custom retry logic
package/index.js CHANGED
@@ -47,6 +47,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
47
47
  globalAgent: opts.globalAgent,
48
48
  destroyAgent: opts.destroyAgent
49
49
  })
50
+
51
+ const isHttp2 = !!opts.http2
50
52
  if (requestBuilt instanceof Error) {
51
53
  next(requestBuilt)
52
54
  return
@@ -209,7 +211,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
209
211
  onError(this, { error: new BadGatewayError() })
210
212
  this.request.log.warn(err, 'response has invalid status code')
211
213
  }
212
- if (this.request.raw.aborted && res.stream) {
214
+ if (this.request.raw.aborted && isHttp2) {
213
215
  // the request could have been canceled before we got a response from the target
214
216
  // forward this to the upstream server and close the stream to prevent leaks
215
217
  res.stream.close(NGHTTP2_CANCEL)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "12.3.0",
3
+ "version": "12.3.1",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -55,8 +55,8 @@ test('a 500 status code with no custom handler should fail', async (t) => {
55
55
  })
56
56
 
57
57
  test("a server 500's with a custom handler and should revive", async (t) => {
58
- const customRetryLogic = ({ req, res, getDefaultDelay }) => {
59
- const defaultDelay = getDefaultDelay()
58
+ const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => {
59
+ const defaultDelay = getDefaultDelay(req, res, err, attempt)
60
60
  if (defaultDelay) return defaultDelay
61
61
 
62
62
  if (res && res.statusCode === 500 && req.method === 'GET') {
@@ -92,9 +92,9 @@ test('custom retry does not invoke the default delay causing a 501', async (t) =
92
92
  })
93
93
 
94
94
  test('custom retry delay functions can invoke the default delay', async (t) => {
95
- const customRetryLogic = ({ req, res, getDefaultDelay }) => {
95
+ const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => {
96
96
  // registering the default retry logic for non 500 errors if it occurs
97
- const defaultDelay = getDefaultDelay()
97
+ const defaultDelay = getDefaultDelay(req, res, err, attempt)
98
98
  if (defaultDelay) return defaultDelay
99
99
 
100
100
  if (res && res.statusCode === 500 && req.method === 'GET') {
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const test = require('tap').test
3
+ const test = require('node:test')
4
4
  const filterPseudoHeaders = require('../lib/utils').filterPseudoHeaders
5
5
 
6
6
  test('filterPseudoHeaders', t => {
@@ -11,7 +11,7 @@ test('filterPseudoHeaders', t => {
11
11
  ':method': 'GET'
12
12
  }
13
13
 
14
- t.strictSame(filterPseudoHeaders(headers), {
14
+ t.assert.deepStrictEqual(filterPseudoHeaders(headers), {
15
15
  accept: '*/*',
16
16
  'content-type': 'text/html; charset=UTF-8'
17
17
  })
package/types/index.d.ts CHANGED
@@ -54,7 +54,12 @@ declare namespace fastifyReplyFrom {
54
54
  res: FastifyReply<RouteGenericInterface, RawServerBase>;
55
55
  attempt: number;
56
56
  retriesCount: number;
57
- getDefaultDelay: () => number | null;
57
+ getDefaultDelay: (
58
+ req: FastifyRequest<RequestGenericInterface, RawServerBase>,
59
+ res: FastifyReply<RouteGenericInterface, RawServerBase>,
60
+ err: Error,
61
+ retries: number,
62
+ ) => number | null;
58
63
  }
59
64
 
60
65
  export type RawServerResponse<T extends RawServerBase> = RawReplyDefaultExpression<T> & {
@@ -64,7 +69,7 @@ declare namespace fastifyReplyFrom {
64
69
  export interface FastifyReplyFromHooks {
65
70
  queryString?: { [key: string]: unknown } | QueryStringFunction;
66
71
  contentType?: string;
67
- retryDelay?: (details: RetryDetails) => {} | null;
72
+ retryDelay?: (details: RetryDetails) => number | null;
68
73
  retriesCount?: number;
69
74
  onResponse?: (
70
75
  request: FastifyRequest<RequestGenericInterface, RawServerBase>,
@@ -103,8 +103,8 @@ async function main () {
103
103
  instance.get('/http2', (_request, reply) => {
104
104
  reply.from('/', {
105
105
  method: 'POST',
106
- retryDelay: ({ req, res, getDefaultDelay }) => {
107
- const defaultDelay = getDefaultDelay()
106
+ retryDelay: ({ req, res, err, attempt, getDefaultDelay }) => {
107
+ const defaultDelay = getDefaultDelay(req, res, err, attempt)
108
108
  if (defaultDelay) return defaultDelay
109
109
 
110
110
  if (res && res.statusCode === 500 && req.method === 'GET') {