1
1
import constants from './constants' ;
2
2
import type { IntDict } from './constants' ;
3
- import { enumToMap } from './utils' ;
4
3
5
4
type Encoding = 'none' | 'hex' ;
6
5
@@ -17,43 +16,33 @@ export class CHeaders {
17
16
18
17
res += '\n' ;
19
18
20
- const errorMap = enumToMap ( constants . ERROR ) ;
21
- const methodMap = enumToMap ( constants . METHODS ) ;
22
- const httpMethodMap = enumToMap ( constants . METHODS , constants . METHODS_HTTP , [
23
- constants . METHODS . PRI ,
24
- ] ) ;
25
- const rtspMethodMap = enumToMap ( constants . METHODS , constants . METHODS_RTSP ) ;
26
- const statusMap = enumToMap ( constants . STATUSES , constants . STATUSES_HTTP ) ;
27
-
28
- res += this . buildEnum ( 'llhttp_errno' , 'HPE' , errorMap ) ;
19
+ res += this . buildEnum ( 'llhttp_errno' , 'HPE' , constants . ERROR ) ;
29
20
res += '\n' ;
30
- res += this . buildEnum ( 'llhttp_flags' , 'F' , enumToMap ( constants . FLAGS ) ,
21
+ res += this . buildEnum ( 'llhttp_flags' , 'F' , constants . FLAGS ,
31
22
'hex' ) ;
32
23
res += '\n' ;
33
24
res += this . buildEnum ( 'llhttp_lenient_flags' , 'LENIENT' ,
34
- enumToMap ( constants . LENIENT_FLAGS ) , 'hex' ) ;
25
+ constants . LENIENT_FLAGS , 'hex' ) ;
35
26
res += '\n' ;
36
- res += this . buildEnum ( 'llhttp_type' , 'HTTP' ,
37
- enumToMap ( constants . TYPE ) ) ;
27
+ res += this . buildEnum ( 'llhttp_type' , 'HTTP' , constants . TYPE ) ;
38
28
res += '\n' ;
39
- res += this . buildEnum ( 'llhttp_finish' , 'HTTP_FINISH' ,
40
- enumToMap ( constants . FINISH ) ) ;
29
+ res += this . buildEnum ( 'llhttp_finish' , 'HTTP_FINISH' , constants . FINISH ) ;
41
30
res += '\n' ;
42
- res += this . buildEnum ( 'llhttp_method' , 'HTTP' , methodMap ) ;
31
+ res += this . buildEnum ( 'llhttp_method' , 'HTTP' , constants . METHODS ) ;
43
32
res += '\n' ;
44
- res += this . buildEnum ( 'llhttp_status' , 'HTTP_STATUS' , statusMap ) ;
33
+ res += this . buildEnum ( 'llhttp_status' , 'HTTP_STATUS' , constants . STATUSES ) ;
45
34
46
35
res += '\n' ;
47
36
48
- res += this . buildMap ( 'HTTP_ERRNO' , errorMap ) ;
37
+ res += this . buildMap ( 'HTTP_ERRNO' , constants . ERROR ) ;
49
38
res += '\n' ;
50
- res += this . buildMap ( 'HTTP_METHOD' , httpMethodMap ) ;
39
+ res += this . buildMap ( 'HTTP_METHOD' , constants . METHODS_HTTP1 ) ;
51
40
res += '\n' ;
52
- res += this . buildMap ( 'RTSP_METHOD' , rtspMethodMap ) ;
41
+ res += this . buildMap ( 'RTSP_METHOD' , constants . METHODS_RTSP ) ;
53
42
res += '\n' ;
54
- res += this . buildMap ( 'HTTP_ALL_METHOD' , methodMap ) ;
43
+ res += this . buildMap ( 'HTTP_ALL_METHOD' , constants . METHODS ) ;
55
44
res += '\n' ;
56
- res += this . buildMap ( 'HTTP_STATUS' , statusMap ) ;
45
+ res += this . buildMap ( 'HTTP_STATUS' , constants . STATUSES ) ;
57
46
58
47
res += '\n' ;
59
48
@@ -65,39 +54,32 @@ export class CHeaders {
65
54
return res ;
66
55
}
67
56
68
- private buildEnum ( name : string , prefix : string , map : IntDict ,
57
+ private buildEnum ( name : Lowercase < string > , prefix : string , map : IntDict ,
69
58
encoding : Encoding = 'none' ) : string {
70
59
let res = '' ;
71
60
72
- res += `enum ${ name } {\n` ;
73
- const keys = Object . keys ( map ) ;
74
- const keysLength = keys . length ;
75
- for ( let i = 0 ; i < keysLength ; i ++ ) {
76
- const key = keys [ i ] ;
77
- const isLast = i === keysLength - 1 ;
61
+ for ( const [ key , value ] of Object . entries ( map ) . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ) {
62
+ if ( res !== "" ) {
63
+ res += ',\n' ;
64
+ }
78
65
79
- let value : number | string = map [ key ] ;
66
+ res += ` ${ prefix } _ ${ key . replace ( / - / g , '' ) } = `
80
67
81
68
if ( encoding === 'hex' ) {
82
- value = `0x${ value . toString ( 16 ) } ` ;
83
- }
84
-
85
- res += ` ${ prefix } _${ key . replace ( / - / g, '' ) } = ${ value } ` ;
86
- if ( ! isLast ) {
87
- res += ',\n' ;
69
+ res += `0x${ value . toString ( 16 ) } ` ;
70
+ } else {
71
+ res += value ;
88
72
}
89
73
}
90
- res += '\n};\n' ;
91
- res += `typedef enum ${ name } ${ name } _t;\n` ;
92
74
93
- return res ;
75
+ return `enum ${ name } {\n ${ res } \n};\ntypedef enum ${ name } ${ name } _t;\n` ;
94
76
}
95
77
96
- private buildMap ( name : string , map : IntDict ) : string {
78
+ private buildMap ( name : Uppercase < string > , map : IntDict ) : string {
97
79
let res = '' ;
98
80
99
81
res += `#define ${ name } _MAP(XX) \\\n` ;
100
- for ( const [ key , value ] of Object . entries ( map ) ) {
82
+ for ( const [ key , value ] of Object . entries ( map ) . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ) {
101
83
res += ` XX(${ value } , ${ key . replace ( / - / g, '' ) } , ${ key } ) \\\n` ;
102
84
}
103
85
res += '\n' ;
0 commit comments