1
1
import { createServer , request } from 'node:http' ;
2
2
3
- const getBody = incomingMessage => {
3
+ const getBody = ( incomingMessage , encoding = 'utf-8' ) => {
4
4
return new Promise ( resolve => {
5
- let body = '' ;
5
+ let body = [ ] ;
6
6
incomingMessage . on ( 'data' , chunk => {
7
- body += chunk ;
7
+ body . push ( chunk ) ;
8
8
} ) ;
9
9
incomingMessage . on ( 'end' , ( ) => {
10
- resolve ( body ) ;
10
+ const buffer = Buffer . concat ( body ) ;
11
+ const content = buffer . toString ( encoding ) ;
12
+ resolve ( content ) ;
11
13
} ) ;
12
14
} ) ;
13
15
} ;
@@ -17,34 +19,33 @@ export const createProxy = options => proxyOptions => {
17
19
const port = options ?. port || 3030 ;
18
20
const host = options ?. host || '127.0.0.1' ;
19
21
const server = createServer ( async ( incomingMessage , serverResponse ) => {
20
- const path = incomingMessage . url ;
22
+ const url = incomingMessage . url ;
21
23
const option = proxyOptions . find ( option => {
22
- return option . pattern . test ( path )
24
+ return option . pattern . test ( url )
23
25
} ) ;
26
+ const encoding = 'utf8' ;
24
27
if ( ! option ) {
25
28
serverResponse . statusCode = 404 ;
26
- serverResponse . end ( 'NO ROUTE' ) ;
29
+ serverResponse . end ( 'NO ROUTE' , encoding ) ;
27
30
return ;
28
31
}
32
+ const match = url . match ( option . pattern ) ;
33
+ const groups = match ?. groups ;
34
+ const path = groups ?. path ;
29
35
console . table ( '__INCOMING_MESSAGE__' ) ;
30
36
console . table ( [
31
37
{
32
38
protocol,
33
39
host,
34
40
port,
35
- path,
36
41
method : incomingMessage . method ,
37
- pattern : option . pattern
38
42
}
39
43
] ) ;
44
+ console . table ( {
45
+ path
46
+ } ) ;
40
47
console . group ( ) ;
41
48
const body = await getBody ( incomingMessage ) ;
42
- const content = JSON . parse ( body || '{}' ) ;
43
- console . log ( '__INCOMING_MESSAGE_BODY__' ) ;
44
- console . table ( [
45
- content
46
- ] ) ;
47
-
48
49
const clientRequest = request ( {
49
50
host : option . host ,
50
51
port : option . port ,
@@ -55,26 +56,23 @@ export const createProxy = options => proxyOptions => {
55
56
} , async response => {
56
57
console . group ( ) ;
57
58
const body = await getBody ( response ) ;
58
- const content = JSON . parse ( body || '{}' ) ;
59
59
console . log ( '__SERVER_RESPONSE__' ) ;
60
60
console . table ( [
61
61
{
62
62
protocol,
63
63
host : option . host ,
64
64
port : option . port ,
65
- path,
66
65
method : incomingMessage . method ,
67
- pattern : option . pattern
66
+ status : response . statusCode
68
67
}
69
68
] ) ;
70
- console . log ( '__SERVER_RESPONSE_BODY__' ) ;
71
- console . table ( [
72
- content
73
- ] ) ;
74
- serverResponse . end ( body ) ;
69
+ console . table ( {
70
+ path
71
+ } ) ;
72
+ serverResponse . end ( body , encoding ) ;
75
73
console . groupEnd ( ) ;
76
74
} ) ;
77
- clientRequest . end ( body ) ;
75
+ clientRequest . end ( body , encoding ) ;
78
76
console . groupEnd ( ) ;
79
77
} ) ;
80
78
@@ -106,7 +104,7 @@ export const createProxy = options => proxyOptions => {
106
104
close ( ) ;
107
105
} ) ;
108
106
109
- server . on ( 'clientError' , error => {
107
+ server . on ( 'clientError' , ( error , socket ) => {
110
108
console . log ( error ) ;
111
109
socket . end ( 'HTTP/1.1 400 Bad Request\r\n\r\n' ) ;
112
110
} ) ;
0 commit comments