@fastify/reply-from 9.0.2 → 9.2.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/README.md CHANGED
@@ -246,6 +246,15 @@ fastify.register(FastifyReplyFrom, {
246
246
 
247
247
  ---
248
248
 
249
+ #### `destroyAgent`
250
+
251
+ If set to `true`, it will destroy all agents when the Fastify is closed.
252
+ If set to `false`, it will not destroy the agents.
253
+
254
+ By Default: `false`
255
+
256
+ ---
257
+
249
258
  #### `maxRetriesOn503`
250
259
 
251
260
  This plugin will always retry on `GET` requests that returns 503 errors, _unless_ `retryMethods` does not contain `GET`.
package/index.js CHANGED
@@ -39,7 +39,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
39
39
  http2: opts.http2,
40
40
  base,
41
41
  undici: opts.undici,
42
- globalAgent: opts.globalAgent
42
+ globalAgent: opts.globalAgent,
43
+ destroyAgent: opts.destroyAgent
43
44
  })
44
45
  if (requestBuilt instanceof Error) {
45
46
  next(requestBuilt)
package/lib/request.js CHANGED
@@ -42,6 +42,7 @@ function buildRequest (opts) {
42
42
  const baseUrl = opts.base && new URL(opts.base).origin
43
43
  const undiciOpts = opts.undici || {}
44
44
  const globalAgent = opts.globalAgent
45
+ const destroyAgent = opts.destroyAgent
45
46
  let http2Client
46
47
  let undiciAgent
47
48
  let undiciInstance
@@ -85,7 +86,7 @@ function buildRequest (opts) {
85
86
  }
86
87
 
87
88
  function close () {
88
- if (globalAgent) {
89
+ if (globalAgent || destroyAgent === false) {
89
90
  return
90
91
  }
91
92
 
package/lib/utils.js CHANGED
@@ -57,6 +57,12 @@ function stripHttp1ConnectionHeaders (headers) {
57
57
  // issue ref: https://github.com/fastify/fast-proxy/issues/42
58
58
  function buildURL (source, reqBase) {
59
59
  let baseOrigin = reqBase ? new URL(reqBase).href : undefined
60
+
61
+ // To make sure we don't accidentally override the base path
62
+ if (baseOrigin && source.length > 1 && source[0] === '/' && source[1] === '/') {
63
+ source = '.' + source
64
+ }
65
+
60
66
  const dest = new URL(source, reqBase)
61
67
 
62
68
  // if base is specified, source url should not override it
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "9.0.2",
3
+ "version": "9.2.0",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -41,6 +41,7 @@
41
41
  "msgpack5": "^6.0.1",
42
42
  "nock": "^13.2.6",
43
43
  "proxyquire": "^2.1.3",
44
+ "semver": "^7.5.1",
44
45
  "simple-get": "^4.0.1",
45
46
  "snazzy": "^9.0.0",
46
47
  "split2": "^4.1.0",
@@ -42,12 +42,19 @@ test('should handle default port in base', (t) => {
42
42
  t.equal(url.href, 'https://localhost/hi')
43
43
  })
44
44
 
45
+ test('should append instead of override base', (t) => {
46
+ t.plan(2)
47
+ let url = buildURL('//10.0.0.10/hi', 'http://localhost')
48
+ t.equal(url.href, 'http://localhost//10.0.0.10/hi')
49
+
50
+ url = buildURL('//httpbin.org/hi', 'http://localhost')
51
+ t.equal(url.href, 'http://localhost//httpbin.org/hi')
52
+ })
53
+
45
54
  const errorInputs = [
46
- { source: '//10.0.0.10/hi', base: 'http://localhost' },
47
55
  { source: 'http://10.0.0.10/hi', base: 'http://localhost' },
48
56
  { source: 'https://10.0.0.10/hi', base: 'http://localhost' },
49
57
  { source: 'blah://10.0.0.10/hi', base: 'http://localhost' },
50
- { source: '//httpbin.org/hi', base: 'http://localhost' },
51
58
  { source: 'urn:foo:bar', base: 'http://localhost' },
52
59
  { source: 'http://localhost/private', base: 'http://localhost/exposed/' },
53
60
  { source: 'http://localhost/exposed-extra', base: 'http://localhost/exposed' },
@@ -5,6 +5,12 @@ const Fastify = require('fastify')
5
5
  const From = require('..')
6
6
  const http = require('http')
7
7
  const get = require('simple-get').concat
8
+ const semver = require('semver')
9
+
10
+ if (semver.gte(process.version, '20.2.0')) {
11
+ t.comment('skip this test on node >= 20.2.0 as it is current broken')
12
+ process.exit(0)
13
+ }
8
14
 
9
15
  const instance = Fastify()
10
16
 
@@ -0,0 +1,29 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('fastify')
5
+ const undici = require('undici')
6
+ const From = require('..')
7
+
8
+ test('destroyAgent false', async (t) => {
9
+ const mockAgent = new undici.Agent()
10
+ mockAgent.destroy = () => {
11
+ t.fail()
12
+ }
13
+ const instance = Fastify()
14
+
15
+ t.teardown(instance.close.bind(instance))
16
+
17
+ instance.get('/', (request, reply) => {
18
+ reply.from()
19
+ })
20
+
21
+ instance.register(From, {
22
+ base: 'http://localhost:4242',
23
+ undici: mockAgent,
24
+ destroyAgent: false
25
+ })
26
+
27
+ await instance.ready()
28
+ await instance.close()
29
+ })
package/types/index.d.ts CHANGED
@@ -94,6 +94,7 @@ declare namespace fastifyReplyFrom {
94
94
  maxRetriesOn503?: number;
95
95
  disableRequestLogging?: boolean;
96
96
  globalAgent?: boolean;
97
+ destroyAgent?: boolean;
97
98
  }
98
99
 
99
100
  export const fastifyReplyFrom: FastifyReplyFrom
@@ -45,6 +45,7 @@ const fullOptions: FastifyReplyFromOptions = {
45
45
  maxRetriesOn503: 10,
46
46
  disableRequestLogging: false,
47
47
  globalAgent: false,
48
+ destroyAgent: true
48
49
  };
49
50
  tap.autoend(false);
50
51