@@ -96,36 +96,59 @@ function assertCrypto() {
96
96
throw new errors . Error ( 'ERR_NO_CRYPTO' ) ;
97
97
}
98
98
99
- // The loop should only run at most twice, retrying with lowercased enc
100
- // if there is no match in the first pass.
101
- // We use a loop instead of branching to retry with a helper
102
- // function in order to avoid the performance hit.
103
99
// Return undefined if there is no match.
100
+ // Move the "slow cases" to a separate function to make sure this function gets
101
+ // inlined properly. That prioritizes the common case.
104
102
function normalizeEncoding ( enc ) {
105
- if ( enc == null || enc === '' ) return 'utf8' ;
106
- let retried ;
107
- while ( true ) {
108
- switch ( enc ) {
109
- case 'utf8' :
110
- case 'utf-8' :
111
- return 'utf8' ;
112
- case 'ucs2' :
113
- case 'ucs-2' :
114
- case 'utf16le' :
115
- case 'utf-16le' :
103
+ if ( enc == null || enc === 'utf8' || enc === 'utf-8' ) return 'utf8' ;
104
+ return slowCases ( enc ) ;
105
+ }
106
+
107
+ function slowCases ( enc ) {
108
+ switch ( enc . length ) {
109
+ case 4 :
110
+ if ( enc === 'UTF8' ) return 'utf8' ;
111
+ if ( enc === 'ucs2' || enc === 'UCS2' ) return 'utf16le' ;
112
+ enc = `${ enc } ` . toLowerCase ( ) ;
113
+ if ( enc === 'utf8' ) return 'utf8' ;
114
+ if ( enc === 'ucs2' || enc === 'UCS2' ) return 'utf16le' ;
115
+ break ;
116
+ case 3 :
117
+ if ( enc === 'hex' || enc === 'HEX' || `${ enc } ` . toLowerCase ( ) === 'hex' )
118
+ return 'hex' ;
119
+ break ;
120
+ case 5 :
121
+ if ( enc === 'ascii' ) return 'ascii' ;
122
+ if ( enc === 'ucs-2' ) return 'utf16le' ;
123
+ if ( enc === 'UTF-8' ) return 'utf8' ;
124
+ if ( enc === 'ASCII' ) return 'ascii' ;
125
+ if ( enc === 'UCS-2' ) return 'utf16le' ;
126
+ enc = `${ enc } ` . toLowerCase ( ) ;
127
+ if ( enc === 'utf-8' ) return 'utf8' ;
128
+ if ( enc === 'ascii' ) return 'ascii' ;
129
+ if ( enc === 'usc-2' ) return 'utf16le' ;
130
+ break ;
131
+ case 6 :
132
+ if ( enc === 'base64' ) return 'base64' ;
133
+ if ( enc === 'latin1' || enc === 'binary' ) return 'latin1' ;
134
+ if ( enc === 'BASE64' ) return 'base64' ;
135
+ if ( enc === 'LATIN1' || enc === 'BINARY' ) return 'latin1' ;
136
+ enc = `${ enc } ` . toLowerCase ( ) ;
137
+ if ( enc === 'base64' ) return 'base64' ;
138
+ if ( enc === 'latin1' || enc === 'binary' ) return 'latin1' ;
139
+ break ;
140
+ case 7 :
141
+ if ( enc === 'utf16le' || enc === 'UTF16LE' ||
142
+ `${ enc } ` . toLowerCase ( ) === 'utf16le' )
116
143
return 'utf16le' ;
117
- case 'latin1' :
118
- case 'binary' :
119
- return 'latin1' ;
120
- case 'base64' :
121
- case 'ascii' :
122
- case 'hex' :
123
- return enc ;
124
- default :
125
- if ( retried ) return ; // undefined
126
- enc = ( '' + enc ) . toLowerCase ( ) ;
127
- retried = true ;
128
- }
144
+ break ;
145
+ case 8 :
146
+ if ( enc === 'utf-16le' || enc === 'UTF-16LE' ||
147
+ `${ enc } ` . toLowerCase ( ) === 'utf-16le' )
148
+ return 'utf16le' ;
149
+ break ;
150
+ default :
151
+ if ( enc === '' ) return 'utf8' ;
129
152
}
130
153
}
131
154
0 commit comments