@fastify/reply-from 12.6.0 → 12.6.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/LICENSE CHANGED
@@ -1,9 +1,7 @@
1
1
  MIT License
2
2
 
3
3
  Copyright (c) 2017-present Matteo Collina
4
- Copyright (c) 2017-present The Fastify team
5
-
6
- The Fastify team members are listed at https://github.com/fastify/fastify#team.
4
+ Copyright (c) 2017-present The Fastify team <https://github.com/fastify/fastify#team>
7
5
 
8
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
9
7
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -66,10 +66,10 @@ target.listen({ port: 3001 }, (err) => {
66
66
  Set the base URL for all the forwarded requests. Will be required if `http2` is set to `true`
67
67
  Note that _every path will be discarded_.
68
68
 
69
- Set the base URL for all the forwarded requests.
70
- *String or String[]*:
69
+ Set the base URL for all the forwarded requests.
70
+ *String or String[]*:
71
71
 
72
- * **Single string** → a normal `undici.Pool` / `http.request` client is used.
72
+ * **Single string** → a normal `undici.Pool` / `http.request` client is used.
73
73
  * **Array with ≥ 2 elements** → **[`undici.BalancedPool`](https://undici.nodejs.org/#/docs/api/BalancedPool)** is automatically selected and requests are load-balanced round-robin across the given origins.
74
74
 
75
75
  When you provide an array, only the *origin* (`protocol://host:port`) part of each URL is considered; any path component is ignored.
package/index.js CHANGED
@@ -250,11 +250,13 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
250
250
 
251
251
  function getQueryString (search, reqUrl, opts, request) {
252
252
  if (typeof opts.queryString === 'function') {
253
- return '?' + opts.queryString(search, reqUrl, request)
253
+ const qs = opts.queryString(search, reqUrl, request)
254
+ return qs ? '?' + qs : ''
254
255
  }
255
256
 
256
257
  if (opts.queryString) {
257
- return '?' + querystring.stringify(opts.queryString)
258
+ const qs = querystring.stringify(opts.queryString)
259
+ return qs ? '?' + qs : ''
258
260
  }
259
261
 
260
262
  if (search.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "12.6.0",
3
+ "version": "12.6.1",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -63,7 +63,7 @@
63
63
  "@sinonjs/fake-timers": "^15.0.0",
64
64
  "@types/node": "^25.0.3",
65
65
  "@types/tap": "^18.0.0",
66
- "c8": "^10.1.3",
66
+ "c8": "^11.0.0",
67
67
  "eslint": "^9.17.0",
68
68
  "fastify": "^5.0.0",
69
69
  "form-data": "^4.0.0",
@@ -0,0 +1,41 @@
1
+ 'use strict'
2
+
3
+ const t = require('node:test')
4
+ const Fastify = require('fastify')
5
+ const { request } = require('undici')
6
+ const From = require('..')
7
+ const http = require('node:http')
8
+
9
+ const instance = Fastify()
10
+
11
+ t.test('queryString empty object should not append trailing ?', async (t) => {
12
+ t.plan(4)
13
+ t.after(() => instance.close())
14
+
15
+ const target = http.createServer((req, res) => {
16
+ t.assert.ok('request proxied')
17
+ t.assert.strictEqual(req.url, '/world')
18
+ res.statusCode = 200
19
+ res.setHeader('Content-Type', 'text/plain')
20
+ res.end('hello world')
21
+ })
22
+
23
+ instance.get('/hello', (_request, reply) => {
24
+ reply.from(`http://localhost:${target.address().port}/world`, {
25
+ queryString: {}
26
+ })
27
+ })
28
+
29
+ t.after(() => target.close())
30
+
31
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
32
+
33
+ instance.register(From)
34
+
35
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
36
+
37
+ const result = await request(`http://localhost:${instance.server.address().port}/hello`)
38
+
39
+ t.assert.strictEqual(result.statusCode, 200)
40
+ t.assert.strictEqual(await result.body.text(), 'hello world')
41
+ })
@@ -0,0 +1,43 @@
1
+ 'use strict'
2
+
3
+ const t = require('node:test')
4
+ const Fastify = require('fastify')
5
+ const { request } = require('undici')
6
+ const From = require('..')
7
+ const http = require('node:http')
8
+
9
+ const instance = Fastify()
10
+
11
+ t.test('queryString function returning empty string should not append trailing ?', async (t) => {
12
+ t.plan(4)
13
+ t.after(() => instance.close())
14
+
15
+ const target = http.createServer((req, res) => {
16
+ t.assert.ok('request proxied')
17
+ t.assert.strictEqual(req.url, '/world')
18
+ res.statusCode = 200
19
+ res.setHeader('Content-Type', 'text/plain')
20
+ res.end('hello world')
21
+ })
22
+
23
+ instance.get('/hello', (_request, reply) => {
24
+ reply.from(`http://localhost:${target.address().port}/world`, {
25
+ queryString () {
26
+ return ''
27
+ }
28
+ })
29
+ })
30
+
31
+ t.after(() => target.close())
32
+
33
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
34
+
35
+ instance.register(From)
36
+
37
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
38
+
39
+ const result = await request(`http://localhost:${instance.server.address().port}/hello`)
40
+
41
+ t.assert.strictEqual(result.statusCode, 200)
42
+ t.assert.strictEqual(await result.body.text(), 'hello world')
43
+ })
package/.github/stale.yml DELETED
@@ -1,21 +0,0 @@
1
- # Number of days of inactivity before an issue becomes stale
2
- daysUntilStale: 15
3
- # Number of days of inactivity before a stale issue is closed
4
- daysUntilClose: 7
5
- # Issues with these labels will never be considered stale
6
- exemptLabels:
7
- - "discussion"
8
- - "feature request"
9
- - "bug"
10
- - "help wanted"
11
- - "plugin suggestion"
12
- - "good first issue"
13
- # Label to use when marking an issue as stale
14
- staleLabel: stale
15
- # Comment to post when marking an issue as stale. Set to `false` to disable
16
- markComment: >
17
- This issue has been automatically marked as stale because it has not had
18
- recent activity. It will be closed if no further activity occurs. Thank you
19
- for your contributions.
20
- # Comment to post when closing a stale issue. Set to `false` to disable
21
- closeComment: false
package/.taprc DELETED
@@ -1,3 +0,0 @@
1
- disable-coverage: true
2
- files:
3
- - test/**/*.test.js
package/CLAUDE.md DELETED
@@ -1,66 +0,0 @@
1
- # CLAUDE.md
2
-
3
- This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
-
5
- ## Development Commands
6
-
7
- ### Testing
8
- - `npm test` - Run all tests (unit + TypeScript)
9
- - `npm run test:unit` - Run unit tests with coverage using c8 and Node.js test runner
10
- - `npm run test:typescript` - Run TypeScript definition tests using tsd
11
-
12
- ### Linting
13
- - `npm run lint` - Run ESLint to check code style
14
- - `npm run lint:fix` - Run ESLint with automatic fixes
15
- - ESLint configuration uses neostandard with TypeScript support
16
-
17
- ### Building
18
- - No build step required - this is a CommonJS library
19
-
20
- ## Architecture Overview
21
-
22
- This is a Fastify plugin for proxying HTTP requests to upstream servers. The plugin decorates the Fastify reply with a `from()` method.
23
-
24
- ### Key Components
25
-
26
- **Main Plugin (`index.js`)**
27
- - Entry point that registers the plugin using fastify-plugin
28
- - Decorates `reply.from(source, opts)` method
29
- - Handles request proxying, retries, caching, and error handling
30
- - Supports HTTP/1.1, HTTP/2, and Undici client strategies
31
-
32
- **Request Builder (`lib/request.js`)**
33
- - Factory for creating request handlers based on transport (HTTP/1.1, HTTP/2, Undici)
34
- - Handles different agent configurations (HTTP, HTTPS, Unix sockets)
35
- - Manages timeouts, connection pooling, and retry logic
36
- - Returns unified interface: `{ request, close, retryOnError }`
37
-
38
- **Utilities (`lib/utils.js`)**
39
- - `filterPseudoHeaders()` - Removes HTTP/2 pseudo-headers (starting with :)
40
- - `copyHeaders()` - Copies headers to Fastify reply
41
- - `stripHttp1ConnectionHeaders()` - Removes HTTP/1 connection-specific headers for HTTP/2
42
- - `buildURL()` - Constructs destination URLs with base path validation
43
-
44
- **Error Definitions (`lib/errors.js`)**
45
- - Standardized error types using @fastify/error
46
- - Maps different error conditions to appropriate HTTP status codes
47
- - Includes timeout, connection reset, and gateway errors
48
-
49
- ### Transport Strategies
50
-
51
- 1. **Undici (default)** - High-performance HTTP/1.1 client with connection pooling
52
- 2. **HTTP/2** - Native Node.js HTTP/2 client for modern protocols
53
- 3. **HTTP/1.1** - Node.js built-in http/https modules with custom agents
54
-
55
- ### Key Features
56
-
57
- - **Caching**: URL parsing cache using toad-cache LruMap (default 100 entries)
58
- - **Retries**: Configurable retry logic for socket hangups and 503 errors
59
- - **Headers**: Smart header rewriting for HTTP/1.1 ↔ HTTP/2 compatibility
60
- - **Body handling**: Supports streams, JSON encoding, and custom content types
61
- - **Unix sockets**: Special handling for unix+http: and unix+https: protocols
62
- - **Timeouts**: Per-request and session-level timeout configuration
63
-
64
- ### Configuration
65
-
66
- The plugin accepts extensive configuration for different transport methods, retry behavior, caching, and request/response transformation hooks. See README.md for full option documentation.