@lobehub/chat 1.36.2 → 1.36.3
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/CHANGELOG.md
CHANGED
@@ -2,6 +2,31 @@
|
|
2
2
|
|
3
3
|
# Changelog
|
4
4
|
|
5
|
+
### [Version 1.36.3](https://github.com/lobehub/lobe-chat/compare/v1.36.2...v1.36.3)
|
6
|
+
|
7
|
+
<sup>Released on **2024-12-08**</sup>
|
8
|
+
|
9
|
+
#### 🐛 Bug Fixes
|
10
|
+
|
11
|
+
- **misc**: Support request headers for chat.
|
12
|
+
|
13
|
+
<br/>
|
14
|
+
|
15
|
+
<details>
|
16
|
+
<summary><kbd>Improvements and Fixes</kbd></summary>
|
17
|
+
|
18
|
+
#### What's fixed
|
19
|
+
|
20
|
+
- **misc**: Support request headers for chat, closes [#4934](https://github.com/lobehub/lobe-chat/issues/4934) ([8cdc062](https://github.com/lobehub/lobe-chat/commit/8cdc062))
|
21
|
+
|
22
|
+
</details>
|
23
|
+
|
24
|
+
<div align="right">
|
25
|
+
|
26
|
+
[](#readme-top)
|
27
|
+
|
28
|
+
</div>
|
29
|
+
|
5
30
|
### [Version 1.36.2](https://github.com/lobehub/lobe-chat/compare/v1.36.1...v1.36.2)
|
6
31
|
|
7
32
|
<sup>Released on **2024-12-07**</sup>
|
package/changelog/v1.json
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@lobehub/chat",
|
3
|
-
"version": "1.36.
|
3
|
+
"version": "1.36.3",
|
4
4
|
"description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
|
5
5
|
"keywords": [
|
6
6
|
"framework",
|
@@ -96,7 +96,14 @@ export interface ChatStreamPayload {
|
|
96
96
|
|
97
97
|
export interface ChatCompetitionOptions {
|
98
98
|
callback?: ChatStreamCallbacks;
|
99
|
+
/**
|
100
|
+
* response headers
|
101
|
+
*/
|
99
102
|
headers?: Record<string, any>;
|
103
|
+
/**
|
104
|
+
* send the request to the ai api endpoint
|
105
|
+
*/
|
106
|
+
requestHeaders?: Record<string, any>;
|
100
107
|
signal?: AbortSignal;
|
101
108
|
/**
|
102
109
|
* userId for the chat completion
|
@@ -211,7 +211,7 @@ export const LobeOpenAICompatibleFactory = <T extends Record<string, any> = any>
|
|
211
211
|
},
|
212
212
|
{
|
213
213
|
// https://github.com/lobehub/lobe-chat/pull/318
|
214
|
-
headers: { Accept: '*/*' },
|
214
|
+
headers: { Accept: '*/*', ...options?.requestHeaders },
|
215
215
|
signal: options?.signal,
|
216
216
|
},
|
217
217
|
);
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
2
|
+
|
3
|
+
import { getClientIP } from './clientIP';
|
4
|
+
|
5
|
+
describe('getClientIP', () => {
|
6
|
+
// Helper function to create Headers object
|
7
|
+
const createHeaders = (entries: [string, string][]) => {
|
8
|
+
return new Headers(entries);
|
9
|
+
};
|
10
|
+
|
11
|
+
it('should return null when no IP headers are present', () => {
|
12
|
+
const headers = createHeaders([]);
|
13
|
+
expect(getClientIP(headers)).toBe('');
|
14
|
+
});
|
15
|
+
|
16
|
+
it('should handle Cloudflare IP header', () => {
|
17
|
+
const headers = createHeaders([['cf-connecting-ip', '1.2.3.4']]);
|
18
|
+
expect(getClientIP(headers)).toBe('1.2.3.4');
|
19
|
+
});
|
20
|
+
|
21
|
+
it('should handle x-forwarded-for with single IP', () => {
|
22
|
+
const headers = createHeaders([['x-forwarded-for', '5.6.7.8']]);
|
23
|
+
expect(getClientIP(headers)).toBe('5.6.7.8');
|
24
|
+
});
|
25
|
+
|
26
|
+
it('should handle x-forwarded-for with multiple IPs and return the first one', () => {
|
27
|
+
const headers = createHeaders([['x-forwarded-for', '9.10.11.12, 13.14.15.16, 17.18.19.20']]);
|
28
|
+
expect(getClientIP(headers)).toBe('9.10.11.12');
|
29
|
+
});
|
30
|
+
|
31
|
+
it('should handle x-real-ip header', () => {
|
32
|
+
const headers = createHeaders([['x-real-ip', '21.22.23.24']]);
|
33
|
+
expect(getClientIP(headers)).toBe('21.22.23.24');
|
34
|
+
});
|
35
|
+
|
36
|
+
it('should trim whitespace from IP addresses', () => {
|
37
|
+
const headers = createHeaders([['x-client-ip', ' 25.26.27.28 ']]);
|
38
|
+
expect(getClientIP(headers)).toBe('25.26.27.28');
|
39
|
+
});
|
40
|
+
|
41
|
+
it('should respect header priority order', () => {
|
42
|
+
const headers = createHeaders([
|
43
|
+
['x-forwarded-for', '1.1.1.1'],
|
44
|
+
['cf-connecting-ip', '2.2.2.2'], // Should take precedence
|
45
|
+
['x-real-ip', '3.3.3.3'],
|
46
|
+
]);
|
47
|
+
expect(getClientIP(headers)).toBe('2.2.2.2');
|
48
|
+
});
|
49
|
+
|
50
|
+
it('should handle empty x-forwarded-for value', () => {
|
51
|
+
const headers = createHeaders([['x-forwarded-for', '']]);
|
52
|
+
expect(getClientIP(headers)).toBe('');
|
53
|
+
});
|
54
|
+
});
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/**
|
2
|
+
* 获取客户端 IP
|
3
|
+
* @param headers HTTP 请求头
|
4
|
+
*/
|
5
|
+
export const getClientIP = (headers: Headers): string => {
|
6
|
+
// 按优先级顺序检查各种 IP 头
|
7
|
+
const ipHeaders = [
|
8
|
+
'cf-connecting-ip', // Cloudflare
|
9
|
+
'x-real-ip', // Nginx proxy
|
10
|
+
'x-forwarded-for', // 标准代理头
|
11
|
+
'x-client-ip', // Apache
|
12
|
+
'true-client-ip', // Akamai and Cloudflare
|
13
|
+
'x-cluster-client-ip', // 负载均衡
|
14
|
+
'forwarded', // RFC 7239
|
15
|
+
'fastly-client-ip', // Fastly CDN
|
16
|
+
'x-forwarded', // General forward
|
17
|
+
'x-original-forwarded-for', // Original forwarded
|
18
|
+
];
|
19
|
+
|
20
|
+
for (const header of ipHeaders) {
|
21
|
+
const value = headers.get(header);
|
22
|
+
if (!value) continue;
|
23
|
+
|
24
|
+
// 处理可能包含多个 IP 的情况(比如 x-forwarded-for)
|
25
|
+
if (header.toLowerCase() === 'x-forwarded-for') {
|
26
|
+
const firstIP = value.split(',')[0].trim();
|
27
|
+
if (firstIP) return firstIP;
|
28
|
+
}
|
29
|
+
|
30
|
+
return value.trim();
|
31
|
+
}
|
32
|
+
|
33
|
+
return '';
|
34
|
+
};
|