2424const {
2525 Array,
2626 ArrayIsArray,
27+ ArrayPrototypeIncludes,
28+ ArrayPrototypeJoin,
29+ ArrayPrototypePush,
30+ ArrayPrototypeReduce,
31+ ArrayPrototypeSome,
2732 ObjectDefineProperty,
2833 ObjectFreeze,
34+ RegExpPrototypeTest,
2935 StringFromCharCode,
3036 StringPrototypeCharCodeAt,
37+ StringPrototypeEndsWith,
38+ StringPrototypeIncludes,
3139 StringPrototypeReplace,
40+ StringPrototypeSlice,
3241 StringPrototypeSplit,
42+ StringPrototypeStartsWith,
3343} = primordials ;
3444
3545const {
@@ -107,7 +117,7 @@ ObjectDefineProperty(exports, 'rootCertificates', {
107117// ("\x06spdy/2\x08http/1.1\x08http/1.0")
108118function convertProtocols ( protocols ) {
109119 const lens = new Array ( protocols . length ) ;
110- const buff = Buffer . allocUnsafe ( protocols . reduce ( ( p , c , i ) => {
120+ const buff = Buffer . allocUnsafe ( ArrayPrototypeReduce ( protocols , ( p , c , i ) => {
111121 const len = Buffer . byteLength ( c ) ;
112122 if ( len > 255 ) {
113123 throw new ERR_OUT_OF_RANGE ( 'The byte length of the protocol at index ' +
@@ -138,7 +148,7 @@ exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) {
138148} ;
139149
140150function unfqdn ( host ) {
141- return host . replace ( / [ . ] $ / , '' ) ;
151+ return StringPrototypeReplace ( host , / [ . ] $ / , '' ) ;
142152}
143153
144154// String#toLowerCase() is locale-sensitive so we use
@@ -165,15 +175,15 @@ function check(hostParts, pattern, wildcards) {
165175 return false ;
166176
167177 // Pattern has empty components, e.g. "bad..example.com".
168- if ( patternParts . includes ( '' ) )
178+ if ( ArrayPrototypeIncludes ( patternParts , '' ) )
169179 return false ;
170180
171181 // RFC 6125 allows IDNA U-labels (Unicode) in names but we have no
172182 // good way to detect their encoding or normalize them so we simply
173183 // reject them. Control characters and blanks are rejected as well
174184 // because nothing good can come from accepting them.
175- const isBad = ( s ) => / [ ^ \u0021 - \u007F ] / u. test ( s ) ;
176- if ( patternParts . some ( isBad ) )
185+ const isBad = ( s ) => RegExpPrototypeTest ( / [ ^ \u0021 - \u007F ] / u, s ) ;
186+ if ( ArrayPrototypeSome ( patternParts , isBad ) )
177187 return false ;
178188
179189 // Check host parts from right to left first.
@@ -184,12 +194,13 @@ function check(hostParts, pattern, wildcards) {
184194
185195 const hostSubdomain = hostParts [ 0 ] ;
186196 const patternSubdomain = patternParts [ 0 ] ;
187- const patternSubdomainParts = patternSubdomain . split ( '*' ) ;
197+ const patternSubdomainParts = StringPrototypeSplit ( patternSubdomain , '*' ) ;
188198
189199 // Short-circuit when the subdomain does not contain a wildcard.
190200 // RFC 6125 does not allow wildcard substitution for components
191201 // containing IDNA A-labels (Punycode) so match those verbatim.
192- if ( patternSubdomainParts . length === 1 || patternSubdomain . includes ( 'xn--' ) )
202+ if ( patternSubdomainParts . length === 1 ||
203+ StringPrototypeIncludes ( patternSubdomain , 'xn--' ) )
193204 return hostSubdomain === patternSubdomain ;
194205
195206 if ( ! wildcards )
@@ -208,10 +219,10 @@ function check(hostParts, pattern, wildcards) {
208219 if ( prefix . length + suffix . length > hostSubdomain . length )
209220 return false ;
210221
211- if ( ! hostSubdomain . startsWith ( prefix ) )
222+ if ( ! StringPrototypeStartsWith ( hostSubdomain , prefix ) )
212223 return false ;
213224
214- if ( ! hostSubdomain . endsWith ( suffix ) )
225+ if ( ! StringPrototypeEndsWith ( hostSubdomain , suffix ) )
215226 return false ;
216227
217228 return true ;
@@ -228,28 +239,30 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
228239 hostname = '' + hostname ;
229240
230241 if ( altNames ) {
231- for ( const name of altNames . split ( ', ' ) ) {
232- if ( name . startsWith ( 'DNS:' ) ) {
233- dnsNames . push ( name . slice ( 4 ) ) ;
234- } else if ( name . startsWith ( 'URI:' ) ) {
242+ for ( const name of StringPrototypeSplit ( altNames , ', ' ) ) {
243+ if ( StringPrototypeStartsWith ( name , 'DNS:' ) ) {
244+ ArrayPrototypePush ( dnsNames , StringPrototypeSlice ( name , 4 ) ) ;
245+ } else if ( StringPrototypeStartsWith ( name , 'URI:' ) ) {
235246 let uri ;
236247 try {
237- uri = new URL ( name . slice ( 4 ) ) ;
248+ uri = new URL ( StringPrototypeSlice ( name , 4 ) ) ;
238249 } catch {
239- uri = url . parse ( name . slice ( 4 ) ) ;
250+ const slicedName = StringPrototypeSlice ( name , 4 ) ;
251+ uri = url . parse ( slicedName ) ;
240252 if ( ! urlWarningEmitted && ! process . noDeprecation ) {
241253 urlWarningEmitted = true ;
242254 process . emitWarning (
243- `The URI ${ name . slice ( 4 ) } found in cert.subjectaltname ` +
255+ `The URI ${ slicedName } found in cert.subjectaltname ` +
244256 'is not a valid URI, and is supported in the tls module ' +
245257 'solely for compatibility.' ,
246258 'DeprecationWarning' , 'DEP0109' ) ;
247259 }
248260 }
249261
250- uriNames . push ( uri . hostname ) ; // TODO(bnoordhuis) Also use scheme.
251- } else if ( name . startsWith ( 'IP Address:' ) ) {
252- ips . push ( canonicalizeIP ( name . slice ( 11 ) ) ) ;
262+ // TODO(bnoordhuis) Also use scheme.
263+ ArrayPrototypePush ( uriNames , uri . hostname ) ;
264+ } else if ( StringPrototypeStartsWith ( name , 'IP Address:' ) ) {
265+ ArrayPrototypePush ( ips , canonicalizeIP ( StringPrototypeSlice ( name , 11 ) ) ) ;
253266 }
254267 }
255268 }
@@ -263,17 +276,19 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
263276 hostname = unfqdn ( hostname ) ; // Remove trailing dot for error messages.
264277
265278 if ( net . isIP ( hostname ) ) {
266- valid = ips . includes ( canonicalizeIP ( hostname ) ) ;
279+ valid = ArrayPrototypeIncludes ( ips , canonicalizeIP ( hostname ) ) ;
267280 if ( ! valid )
268- reason = `IP: ${ hostname } is not in the cert's list: ${ ips . join ( ', ' ) } ` ;
281+ reason = `IP: ${ hostname } is not in the cert's list: ` +
282+ ArrayPrototypeJoin ( ips , ', ' ) ;
269283 // TODO(bnoordhuis) Also check URI SANs that are IP addresses.
270284 } else if ( hasAltNames || subject ) {
271285 const hostParts = splitHost ( hostname ) ;
272286 const wildcard = ( pattern ) => check ( hostParts , pattern , true ) ;
273287
274288 if ( hasAltNames ) {
275289 const noWildcard = ( pattern ) => check ( hostParts , pattern , false ) ;
276- valid = dnsNames . some ( wildcard ) || uriNames . some ( noWildcard ) ;
290+ valid = ArrayPrototypeSome ( dnsNames , wildcard ) ||
291+ ArrayPrototypeSome ( uriNames , noWildcard ) ;
277292 if ( ! valid )
278293 reason =
279294 `Host: ${ hostname } . is not in the cert's altnames: ${ altNames } ` ;
@@ -282,7 +297,7 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
282297 const cn = subject . CN ;
283298
284299 if ( ArrayIsArray ( cn ) )
285- valid = cn . some ( wildcard ) ;
300+ valid = ArrayPrototypeSome ( cn , wildcard ) ;
286301 else if ( cn )
287302 valid = wildcard ( cn ) ;
288303
0 commit comments