@robiki/proxy 1.0.2 → 1.0.4

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.zh.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # 🚀 Robiki 代理
2
2
 
3
- > 一个高性能、灵活的 HTTP/2 反向代理,支持 WebSocket、可配置路由、CORS 和请求验证。可作为 npm 包在 Node.js 应用程序中使用,也可作为独立的 Docker 容器使用。仅用作本地开发环境的域名代理。
3
+ > 一个高性能的 HTTP/2 反向代理,支持 WebSocket、可配置路由、CORS 和请求验证。可作为 npm 包或 Docker 容器用于本地开发环境。
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@robiki/proxy.svg)](https://www.npmjs.com/package/@robiki/proxy)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
7
 
8
- ## 🌍 语言 / Languages / Sprachen / 言語 / Języki / Idiomas / Языки
8
+ ## 🌍 语言
9
9
 
10
10
  [English](README.md) | [Deutsch](README.de.md) | [中文](README.zh.md) | [日本語](README.ja.md) | [Polski](README.pl.md) | [Español](README.es.md) | [Русский](README.ru.md)
11
11
 
@@ -18,54 +18,29 @@
18
18
  - **✅ 请求验证**:用于身份验证、速率限制等的自定义验证逻辑
19
19
  - **🔄 URL 重映射**:在转发到目标服务之前转换 URL
20
20
  - **📦 双重用途**:可作为 npm 包或 Docker 容器使用
21
+ - **⚙️ JavaScript 和 TypeScript 配置支持**:在 Docker 中使用带有函数的 `.js` 或 `.ts` 配置文件
21
22
  - **🎯 多端口支持**:同时监听多个端口
22
23
  - **⚡ 高性能**:基于 Node.js 原生 HTTP/2 实现
23
24
 
24
25
  ## 📦 安装
25
26
 
26
- ### 作为 npm 包
27
+ ### npm 包
27
28
 
28
29
  ```bash
29
30
  npm install @robiki/proxy
30
- ```
31
-
32
- ```bash
31
+ # 或
33
32
  yarn add @robiki/proxy
34
33
  ```
35
34
 
36
- ### 作为 Docker 容器
35
+ ### Docker
37
36
 
38
37
  ```bash
39
38
  docker pull robiki/proxy:latest
40
39
  ```
41
40
 
42
- ### 作为 Docker Compose 服务
43
-
44
- ```yaml
45
- services:
46
- proxy:
47
- image: robiki/proxy:latest
48
- container_name: robiki-proxy
49
- restart: unless-stopped
50
- ports:
51
- - '443:443'
52
- - '8080:8080'
53
- - '9229:9229'
54
- volumes:
55
- - ./proxy.config.json:/app/proxy.config.json:ro
56
- - ./certs:/app/certs:ro
57
- networks:
58
- - app-network
59
- ```
60
-
61
- ## 注意事项
62
-
63
- - 本地配置的主机应添加到本地 `hosts` 文件中。
64
- - 如果您使用自定义证书,需要将证书文件添加到 `certs` 目录。
65
-
66
41
  ## 🚀 快速开始
67
42
 
68
- ### 作为 npm 包使用
43
+ ### npm
69
44
 
70
45
  ```javascript
71
46
  import { createProxy } from '@robiki/proxy';
@@ -88,13 +63,11 @@ const proxy = await createProxy({
88
63
  },
89
64
  },
90
65
  });
91
-
92
- console.log('代理服务器正在运行!');
93
66
  ```
94
67
 
95
- ### 使用 Docker
68
+ ### Docker
96
69
 
97
- 1. 创建 `proxy.config.json` 文件:
70
+ 创建 `proxy.config.json`:
98
71
 
99
72
  ```json
100
73
  {
@@ -108,20 +81,14 @@ console.log('代理服务器正在运行!');
108
81
  "api.example.com": {
109
82
  "target": "backend-service:3000",
110
83
  "ssl": true
111
- },
112
- "example.com": {
113
- "target": "frontend-service:8080",
114
- "ssl": false
115
84
  }
116
85
  }
117
86
  }
118
87
  ```
119
88
 
120
- 2. 创建 `docker-compose.yml`:
89
+ 创建 `docker-compose.yml`:
121
90
 
122
91
  ```yaml
123
- version: '3.8'
124
-
125
92
  services:
126
93
  proxy:
127
94
  image: robiki/proxy:latest
@@ -133,25 +100,9 @@ services:
133
100
  - ./certs:/app/certs:ro
134
101
  environment:
135
102
  - PROXY_CONFIG=/app/proxy.config.json
136
- networks:
137
- - app-network
138
-
139
- backend-service:
140
- image: your-backend-image
141
- networks:
142
- - app-network
143
-
144
- frontend-service:
145
- image: your-frontend-image
146
- networks:
147
- - app-network
148
-
149
- networks:
150
- app-network:
151
- driver: bridge
152
103
  ```
153
104
 
154
- 3. 启动服务:
105
+ 启动:
155
106
 
156
107
  ```bash
157
108
  docker-compose up -d
@@ -159,9 +110,9 @@ docker-compose up -d
159
110
 
160
111
  ## 📖 配置
161
112
 
162
- ### 配置文件
113
+ ### JSON 配置
163
114
 
164
- 创建具有以下结构的 `proxy.config.json` 文件:
115
+ 简单的声明式配置:
165
116
 
166
117
  ```json
167
118
  {
@@ -169,24 +120,17 @@ docker-compose up -d
169
120
  "ssl": {
170
121
  "key": "./certs/key.pem",
171
122
  "cert": "./certs/cert.pem",
172
- "ca": "./certs/ca.pem",
173
123
  "allowHTTP1": true
174
124
  },
175
125
  "cors": {
176
126
  "origin": "*",
177
- "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
178
- "allowedHeaders": ["Content-Type", "Authorization"],
179
- "credentials": true,
180
- "maxAge": 86400
127
+ "methods": ["GET", "POST", "PUT", "DELETE"],
128
+ "credentials": true
181
129
  },
182
130
  "routes": {
183
131
  "api.example.com": {
184
- "target": "backend-service:3000",
185
- "ssl": true,
186
- "cors": {
187
- "origin": ["https://example.com"],
188
- "credentials": true
189
- }
132
+ "target": "backend:3000",
133
+ "ssl": true
190
134
  },
191
135
  "*.example.com": {
192
136
  "target": "wildcard-service:4000",
@@ -196,78 +140,32 @@ docker-compose up -d
196
140
  }
197
141
  ```
198
142
 
199
- ### 环境变量
200
-
201
- 您还可以使用环境变量配置代理:
202
-
203
- ```bash
204
- # SSL 配置
205
- SSL_KEY=/app/certs/key.pem
206
- SSL_CERT=/app/certs/cert.pem
207
- SSL_CA=/app/certs/ca.pem
208
- SSL_ALLOW_HTTP1=true
209
-
210
- # CORS 配置
211
- CORS_ORIGIN=*
212
- CORS_METHODS=GET,POST,PUT,DELETE,OPTIONS
213
- CORS_HEADERS=Content-Type,Authorization
214
- CORS_CREDENTIALS=true
215
-
216
- # 调试模式
217
- DEBUG=true # 启用代理连接和错误的详细日志记录
218
- ```
219
-
220
- ## 🎯 高级用法
221
-
222
- ### URL 重映射
223
-
224
- 在转发到目标服务之前转换 URL:
225
-
226
- ```javascript
227
- const config = {
228
- routes: {
229
- 'api.example.com': {
230
- target: 'backend:3000',
231
- ssl: true,
232
- remap: (url) => {
233
- // 删除 /api 前缀
234
- return url.replace(/^\/api/, '');
235
- },
236
- },
237
- },
238
- };
239
- ```
240
-
241
- ### 请求验证
143
+ ### JavaScript 配置
242
144
 
243
- 为身份验证、速率限制等添加自定义验证逻辑:
145
+ 用于高级功能,如 URL 重映射和验证:
244
146
 
245
147
  ```javascript
246
- const config = {
247
- // 全局验证
248
- validate: async (info) => {
249
- if (!info.headers.authorization) {
250
- return {
251
- status: false,
252
- code: 401,
253
- message: '未授权',
254
- headers: { 'www-authenticate': 'Bearer' },
255
- };
256
- }
257
- return { status: true };
148
+ // proxy.config.js
149
+ export default {
150
+ ports: [443, 8080],
151
+ ssl: {
152
+ key: './certs/key.pem',
153
+ cert: './certs/cert.pem',
154
+ allowHTTP1: true,
258
155
  },
259
156
  routes: {
260
157
  'api.example.com': {
261
158
  target: 'backend:3000',
262
159
  ssl: true,
263
- // 路由特定验证
160
+ // URL 重映射
161
+ remap: (url) => url.replace(/^\/api/, ''),
162
+ // 请求验证
264
163
  validate: async (info) => {
265
- const rateLimit = await checkRateLimit(info.remoteAddress);
266
- if (!rateLimit.allowed) {
164
+ if (!info.headers.authorization) {
267
165
  return {
268
166
  status: false,
269
- code: 429,
270
- message: '请求过多',
167
+ code: 401,
168
+ message: 'Unauthorized',
271
169
  };
272
170
  }
273
171
  return { status: true };
@@ -277,93 +175,80 @@ const config = {
277
175
  };
278
176
  ```
279
177
 
280
- ### 自定义 CORS 配置
178
+ ### TypeScript 配置
281
179
 
282
- 全局或按路由配置 CORS:
180
+ 类型安全的配置,完整的 IDE 支持:
283
181
 
284
- ```javascript
285
- const config = {
286
- // 全局 CORS
287
- cors: {
288
- origin: ['https://example.com', 'https://www.example.com'],
289
- methods: ['GET', 'POST', 'PUT', 'DELETE'],
290
- allowedHeaders: ['Content-Type', 'Authorization'],
291
- credentials: true,
292
- maxAge: 86400,
182
+ ```typescript
183
+ // proxy.config.ts
184
+ import type { ServerConfig, ConnectionInfo } from '@robiki/proxy';
185
+
186
+ const config: ServerConfig = {
187
+ ports: [443, 8080],
188
+ ssl: {
189
+ key: './certs/key.pem',
190
+ cert: './certs/cert.pem',
191
+ allowHTTP1: true,
293
192
  },
294
193
  routes: {
295
194
  'api.example.com': {
296
195
  target: 'backend:3000',
297
196
  ssl: true,
298
- // 路由特定 CORS(覆盖全局)
299
- cors: {
300
- origin: '*',
301
- credentials: false,
197
+ remap: (url: string) => url.replace(/^\/api/, ''),
198
+ validate: async (info: ConnectionInfo) => {
199
+ if (!info.headers['x-api-key']) {
200
+ return { status: false, code: 401, message: 'API Key Required' };
201
+ }
202
+ return { status: true };
302
203
  },
303
204
  },
304
205
  },
305
206
  };
306
- ```
307
207
 
308
- ### 自定义处理程序
208
+ export default config;
209
+ ```
309
210
 
310
- 为高级用例创建自定义请求处理程序:
211
+ ### 环境变量
311
212
 
312
- ```javascript
313
- import { createCustomProxy } from '@robiki/proxy';
213
+ ```bash
214
+ # SSL 配置
215
+ SSL_KEY=/app/certs/key.pem
216
+ SSL_CERT=/app/certs/cert.pem
217
+ SSL_ALLOW_HTTP1=true
314
218
 
315
- const customRestHandler = async (req, res) => {
316
- if (req.url === '/health') {
317
- res.writeHead(200, { 'content-type': 'application/json' });
318
- return res.end(JSON.stringify({ status: 'ok' }));
319
- }
320
- // 回退到默认代理行为
321
- const { restAPIProxyHandler } = await import('@robiki/proxy/connections');
322
- return restAPIProxyHandler(req, res);
323
- };
219
+ # CORS 配置
220
+ CORS_ORIGIN=*
221
+ CORS_METHODS=GET,POST,PUT,DELETE
222
+ CORS_CREDENTIALS=true
324
223
 
325
- const proxy = await createCustomProxy(config, {
326
- rest: customRestHandler,
327
- websocket: customWebSocketHandler,
328
- stream: customStreamHandler,
329
- });
224
+ # 调试模式
225
+ DEBUG=true
330
226
  ```
331
227
 
332
228
  ## 🔧 API 参考
333
229
 
334
- ### `createProxy(config: ServerConfig): Promise<ProxyServer>`
335
-
336
- 使用给定配置创建并启动代理服务器。
337
-
338
- **参数:**
339
-
340
- - `config`:服务器配置对象
341
-
342
- **返回:** 解析为 `ProxyServer` 实例的 Promise
230
+ ### `createProxy(config: ServerConfig)`
343
231
 
344
- ### `ProxyServer`
232
+ 创建并启动代理服务器。
345
233
 
346
- **方法:**
347
-
348
- - `start()`:启动代理服务器
349
- - `stop()`:停止代理服务器
350
- - `getConfig()`:获取当前配置
351
-
352
- ### 配置类型
353
-
354
- #### `ServerConfig`
234
+ **ServerConfig:**
355
235
 
356
236
  ```typescript
357
237
  interface ServerConfig {
358
238
  ports?: number[];
359
- ssl?: CertificateConfig;
239
+ ssl?: {
240
+ key: string;
241
+ cert: string;
242
+ ca?: string;
243
+ allowHTTP1?: boolean;
244
+ };
360
245
  routes: Record<string, RouteConfig>;
361
246
  cors?: CorsConfig;
362
247
  validate?: (info: ConnectionInfo) => Promise<ForwardValidationResult>;
363
248
  }
364
249
  ```
365
250
 
366
- #### `RouteConfig`
251
+ **RouteConfig:**
367
252
 
368
253
  ```typescript
369
254
  interface RouteConfig {
@@ -375,265 +260,86 @@ interface RouteConfig {
375
260
  }
376
261
  ```
377
262
 
378
- #### `CorsConfig`
379
-
380
- ```typescript
381
- interface CorsConfig {
382
- origin?: string | string[];
383
- methods?: string[];
384
- allowedHeaders?: string[];
385
- exposedHeaders?: string[];
386
- credentials?: boolean;
387
- maxAge?: number;
388
- }
389
- ```
390
-
391
- #### `ConnectionInfo`
392
-
393
- ```typescript
394
- interface ConnectionInfo {
395
- id: number;
396
- method: string;
397
- path: string;
398
- remoteAddress: string;
399
- scheme: string;
400
- authority: string;
401
- origin: string;
402
- headers: IncomingHttpHeaders;
403
- query: URLSearchParams;
404
- type: RequestType;
405
- }
406
- ```
407
-
408
263
  ## 🐳 Docker 使用
409
264
 
410
- ### 在另一个项目中使用
411
-
412
- 1. 将代理添加到您的 `docker-compose.yml`:
265
+ 挂载您的配置文件(JSON、.cjs 或 .ts):
413
266
 
414
267
  ```yaml
415
268
  services:
416
269
  proxy:
417
270
  image: robiki/proxy:latest
418
- ports:
419
- - '443:443'
420
- - '8080:8080'
421
271
  volumes:
422
- - ./proxy.config.json:/app/proxy.config.json:ro
272
+ - ./proxy.config.cjs:/app/proxy.config.cjs:ro
423
273
  - ./certs:/app/certs:ro
424
- networks:
425
- - your-network
426
-
427
- your-service:
428
- image: your-service-image
429
- networks:
430
- - your-network
431
- ```
432
-
433
- 2. 在 `proxy.config.json` 中配置路由以指向您的服务
434
-
435
- 3. 启动您的堆栈:
436
-
437
- ```bash
438
- docker-compose up -d
439
- ```
440
-
441
- ### 构建自定义镜像
442
-
443
- 创建自定义 Dockerfile:
444
-
445
- ```dockerfile
446
- FROM robiki/proxy:latest
447
-
448
- # 复制您的配置
449
- COPY proxy.config.json /app/proxy.config.json
450
- COPY certs /app/certs
451
-
452
- # 设置环境变量
453
- ENV PROXY_CONFIG=/app/proxy.config.json
274
+ environment:
275
+ - PROXY_CONFIG=/app/proxy.config.cjs
454
276
  ```
455
277
 
456
- ## 📚 示例
457
-
458
- 查看 `examples/` 目录以获取更多使用示例:
459
-
460
- - `basic-usage.js` - 简单的代理设置
461
- - `advanced-usage.js` - 高级功能(验证、CORS、重映射)
462
- - `custom-handlers.js` - 自定义请求处理程序
463
- - `docker-compose.example.yml` - 完整的 Docker 设置
464
-
465
- ## 🔐 SSL/TLS 证书
466
-
467
- ### 生成自签名证书
278
+ ## 🔐 SSL 证书
468
279
 
469
- 用于开发:
280
+ ### 开发环境(自签名)
470
281
 
471
282
  ```bash
472
283
  mkdir -p certs
473
284
  openssl req -x509 -newkey rsa:4096 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes
474
285
  ```
475
286
 
476
- ### 使用 Let's Encrypt
477
-
478
- 用于生产,使用 Let's Encrypt 证书:
287
+ ### 生产环境(Let's Encrypt
479
288
 
480
289
  ```bash
481
290
  certbot certonly --standalone -d example.com
482
291
  ```
483
292
 
484
- 然后在您的配置中引用它们:
485
-
486
- ```json
487
- {
488
- "ssl": {
489
- "key": "/etc/letsencrypt/live/example.com/privkey.pem",
490
- "cert": "/etc/letsencrypt/live/example.com/fullchain.pem"
491
- }
492
- }
493
- ```
494
-
495
- ## 🤝 贡献
496
-
497
- 欢迎贡献!请随时提交 Pull Request。
498
-
499
- ## 📄 许可证
500
-
501
- MIT © Robiki sp. z o.o.
502
-
503
- ## 🔗 链接
504
-
505
- - [GitHub 仓库](https://github.com/robiki-ai/robiki-proxy)
506
- - [npm 包](https://www.npmjs.com/package/@robiki/proxy)
507
- - [问题跟踪器](https://github.com/robiki-ai/robiki-proxy/issues)
508
-
509
- ## 💡 用例
510
-
511
- - **微服务架构**:根据域名/路径将请求路由到不同的服务
512
- - **开发环境**:用于测试多个服务的本地代理
513
- - **API 网关**:具有身份验证和速率限制的集中入口点
514
- - **SSL 终止**:在代理级别处理 SSL/TLS
515
- - **CORS 管理**:集中式 CORS 配置
516
- - **负载均衡**:在多个实例之间分配流量(使用自定义处理程序)
517
-
518
293
  ## 🛠️ 故障排除
519
294
 
520
295
  ### 调试模式
521
296
 
522
- 启用详细日志记录以排除连接问题:
297
+ 启用详细日志记录:
523
298
 
524
299
  ```bash
525
- # 启用调试模式
526
- DEBUG=true node your-proxy-script.js
527
-
528
- # 或使用 Docker
300
+ DEBUG=true node your-script.js
301
+ #
529
302
  docker run -e DEBUG=true robiki/proxy:latest
530
-
531
- # 或在 docker-compose.yml 中
532
- services:
533
- proxy:
534
- image: robiki/proxy:latest
535
- environment:
536
- - DEBUG=true
537
303
  ```
538
304
 
539
- 当 `DEBUG=true` 时,代理将记录:
540
- - 所有代理连接尝试(REST、WebSocket、HTTP/2 流)
541
- - 请求和响应详细信息
542
- - 连接错误和超时
543
- - 代理错误和客户端错误
544
-
545
305
  ### 端口已被占用
546
306
 
547
- 代理将自动尝试终止配置端口上的进程。如果失败,请手动释放端口:
548
-
549
307
  ```bash
550
308
  lsof -ti:443 | xargs kill -9
551
- lsof -ti:8080 | xargs kill -9
552
309
  ```
553
310
 
554
- ### SSL 证书错误
555
-
556
- 确保您的证书文件可读并且格式正确(PEM)。对于开发,使用自签名证书。
557
-
558
- ### WebSocket 连接问题
559
-
560
- 确保您的 WebSocket 路由配置了正确的协议(ws/wss),并且目标服务支持 WebSocket 连接。
561
-
562
311
  ## 🧪 测试
563
312
 
564
- Robiki Proxy 包含一个全面的测试套件,涵盖单元测试、集成测试和高级场景。
565
-
566
- ### 运行测试
567
-
568
313
  ```bash
569
314
  # 运行所有测试
570
315
  yarn test
571
316
 
572
- # 在监视模式下运行测试
573
- yarn test:watch
574
-
575
- # 运行带覆盖率的测试
317
+ # 带覆盖率
576
318
  yarn test:coverage
577
319
 
578
- # 使用 UI 运行测试
579
- yarn test:ui
580
- ```
581
-
582
- ### 测试覆盖率
583
-
584
- 测试套件包括:
585
-
586
- - **单元测试**:配置、实用程序、标头转换、CORS 处理
587
- - **集成测试**:HTTP 代理、路由解析、验证、配置加载
588
- - **高级测试**:WebSocket 代理、HTTP/2 流、并发连接
589
- - **Docker 测试**:容器构建、配置加载、运行时行为
590
-
591
- ### Docker 测试
592
-
593
- 运行 Docker 集成测试:
594
-
595
- ```bash
596
- # 完整的 Docker 集成测试
320
+ # Docker 测试
597
321
  yarn test:docker
598
-
599
- # 专门测试配置加载
600
- yarn test:docker:config
601
-
602
- # 运行所有测试(单元 + 集成 + Docker)
603
- yarn test:all
604
322
  ```
605
323
 
606
- 或使用 Make:
607
-
608
- ```bash
609
- # 快速 Docker 构建测试
610
- make test-docker
611
-
612
- # 完整的集成测试套件
613
- make test-docker-full
614
-
615
- # 配置加载测试
616
- make test-docker-config
324
+ ## 📚 示例
617
325
 
618
- # Docker Compose 测试
619
- make test-docker-compose
620
- ```
326
+ 查看 `examples/` 目录:
621
327
 
622
- 有关更多详细信息,请参阅 [Docker 测试 README](tests/docker/README.md)。
328
+ - `basic-usage.js` - 简单的代理设置
329
+ - `advanced-usage.js` - 验证、CORS、重映射
330
+ - `custom-handlers.js` - 自定义请求处理器
331
+ - `docker-compose.example.yml` - Docker 设置
623
332
 
624
- ## 📊 性能
333
+ ## 🤝 贡献
625
334
 
626
- 该代理基于 Node.js 原生 HTTP/2 实现,专为高性能而设计:
335
+ 欢迎贡献!详情请参见 [CONTRIBUTING.md](CONTRIBUTING.md)。
627
336
 
628
- - 高效的流处理
629
- - 最小的开销
630
- - 连接池
631
- - 自动 HTTP/1.1 回退
337
+ ## 📄 许可证
632
338
 
633
- 对于生产部署,请考虑:
339
+ MIT © Robiki sp. z o.o.
634
340
 
635
- - 使用进程管理器(PM2、systemd)
636
- - 为多核系统启用集群
637
- - 使用健康检查进行监控
638
- - 设置适当的日志记录
341
+ ## 🔗 链接
639
342
 
343
+ - [GitHub 仓库](https://github.com/robiki-ai/robiki-proxy)
344
+ - [npm 包](https://www.npmjs.com/package/@robiki/proxy)
345
+ - [问题跟踪](https://github.com/robiki-ai/robiki-proxy/issues)