11import constants from './constants' ;
22import type { IntDict } from './constants' ;
3- import { enumToMap } from './utils' ;
43
54type Encoding = 'none' | 'hex' ;
65
@@ -17,43 +16,33 @@ export class CHeaders {
1716
1817 res += '\n' ;
1918
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 ) ;
2920 res += '\n' ;
30- res += this . buildEnum ( 'llhttp_flags' , 'F' , enumToMap ( constants . FLAGS ) ,
21+ res += this . buildEnum ( 'llhttp_flags' , 'F' , constants . FLAGS ,
3122 'hex' ) ;
3223 res += '\n' ;
3324 res += this . buildEnum ( 'llhttp_lenient_flags' , 'LENIENT' ,
34- enumToMap ( constants . LENIENT_FLAGS ) , 'hex' ) ;
25+ constants . LENIENT_FLAGS , 'hex' ) ;
3526 res += '\n' ;
36- res += this . buildEnum ( 'llhttp_type' , 'HTTP' ,
37- enumToMap ( constants . TYPE ) ) ;
27+ res += this . buildEnum ( 'llhttp_type' , 'HTTP' , constants . TYPE ) ;
3828 res += '\n' ;
39- res += this . buildEnum ( 'llhttp_finish' , 'HTTP_FINISH' ,
40- enumToMap ( constants . FINISH ) ) ;
29+ res += this . buildEnum ( 'llhttp_finish' , 'HTTP_FINISH' , constants . FINISH ) ;
4130 res += '\n' ;
42- res += this . buildEnum ( 'llhttp_method' , 'HTTP' , methodMap ) ;
31+ res += this . buildEnum ( 'llhttp_method' , 'HTTP' , constants . METHODS ) ;
4332 res += '\n' ;
44- res += this . buildEnum ( 'llhttp_status' , 'HTTP_STATUS' , statusMap ) ;
33+ res += this . buildEnum ( 'llhttp_status' , 'HTTP_STATUS' , constants . STATUSES ) ;
4534
4635 res += '\n' ;
4736
48- res += this . buildMap ( 'HTTP_ERRNO' , errorMap ) ;
37+ res += this . buildMap ( 'HTTP_ERRNO' , constants . ERROR ) ;
4938 res += '\n' ;
50- res += this . buildMap ( 'HTTP_METHOD' , httpMethodMap ) ;
39+ res += this . buildMap ( 'HTTP_METHOD' , constants . METHODS_HTTP1 ) ;
5140 res += '\n' ;
52- res += this . buildMap ( 'RTSP_METHOD' , rtspMethodMap ) ;
41+ res += this . buildMap ( 'RTSP_METHOD' , constants . METHODS_RTSP ) ;
5342 res += '\n' ;
54- res += this . buildMap ( 'HTTP_ALL_METHOD' , methodMap ) ;
43+ res += this . buildMap ( 'HTTP_ALL_METHOD' , constants . METHODS ) ;
5544 res += '\n' ;
56- res += this . buildMap ( 'HTTP_STATUS' , statusMap ) ;
45+ res += this . buildMap ( 'HTTP_STATUS' , constants . STATUSES ) ;
5746
5847 res += '\n' ;
5948
@@ -65,39 +54,32 @@ export class CHeaders {
6554 return res ;
6655 }
6756
68- private buildEnum ( name : string , prefix : string , map : IntDict ,
57+ private buildEnum ( name : Lowercase < string > , prefix : string , map : IntDict ,
6958 encoding : Encoding = 'none' ) : string {
7059 let res = '' ;
7160
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+ }
7865
79- let value : number | string = map [ key ] ;
66+ res += ` ${ prefix } _ ${ key . replace ( / - / g , '' ) } = `
8067
8168 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 ;
8872 }
8973 }
90- res += '\n};\n' ;
91- res += `typedef enum ${ name } ${ name } _t;\n` ;
9274
93- return res ;
75+ return `enum ${ name } {\n ${ res } \n};\ntypedef enum ${ name } ${ name } _t;\n` ;
9476 }
9577
96- private buildMap ( name : string , map : IntDict ) : string {
78+ private buildMap ( name : Uppercase < string > , map : IntDict ) : string {
9779 let res = '' ;
9880
9981 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 ] ) ) {
10183 res += ` XX(${ value } , ${ key . replace ( / - / g, '' ) } , ${ key } ) \\\n` ;
10284 }
10385 res += '\n' ;
0 commit comments