@@ -157,6 +157,8 @@ export class Address {
157157 return o
158158 }
159159
160+ static strict = true
161+
160162 static from ( raw : Raw ) : Address {
161163 if ( raw instanceof Address ) return this . create ( raw . big , raw . family , raw . raw )
162164 if ( typeof raw === 'string' ) return this . fromString ( raw . toLowerCase ( ) )
@@ -334,18 +336,18 @@ export class Address {
334336 if ( addr === '0' ) return this . create ( 0n , 4 , addr )
335337
336338 return addr . includes ( ':' )
337- ? this . fromIPv6 ( addr )
339+ ? this . fromIPv6 ( addr , this . strict )
338340 : this . fromIPv4 ( addr )
339341 }
340342
341- private static fromIPv6 ( addr : string ) : Address {
343+ private static fromIPv6 ( addr : string , strict ?: boolean ) : Address {
342344 const al = addr . length
343345 const sep = addr . indexOf ( '::' )
344346 if (
345347 al > IPV6_LEN_LIM ||
346348 sep !== - 1 && addr . indexOf ( '::' , sep + 1 ) !== - 1 // only one '::' allowed
347349 )
348- throw new Error ( `Invalid address : ${ addr } ` )
350+ throw new Error ( `Invalid address0 : ${ addr } ` )
349351
350352 const groups : number [ ] = [ ]
351353 let p = 0 , gc = - 1
@@ -360,15 +362,16 @@ export class Address {
360362 if ( sep === - 1 || ( end !== sep && end !== sep + 1 + + last ) )
361363 throw new Error ( `Invalid address: ${ addr } ` )
362364 gc = groups . length
363- } else if ( last && v . includes ( '.' ) ) {
364- // embedded IPv4
365- if (
366- groups . length > 6 ||
365+ } else if ( last && v . includes ( '.' ) ) { // embedded IPv4
366+ if ( gc === - 1 ? groups . length !== 6 : groups . length > 5 )
367+ throw new Error ( `Invalid address: ${ addr } ` )
368+
369+ if ( strict && (
367370 gc === groups . length ||
368- ( gc === - 1 && groups . length !== 6 ) ||
369371 groups [ groups . length - 1 ] !== 0xffff ||
370372 groups . slice ( 0 , - 1 ) . some ( x => x !== 0 )
371- ) throw new Error ( `Invalid address: ${ addr } ` )
373+ ) )
374+ throw new Error ( `Invalid address: ${ addr } ` )
372375
373376 const long = Address . normalizeToLong ( v , true )
374377 if ( long === - 1 ) throw new Error ( `Invalid address: ${ addr } ` )
@@ -382,7 +385,7 @@ export class Address {
382385 p = i + 1
383386 }
384387 const offset = 8 - groups . length
385- if ( gc === - 1 ? offset !== 0 : offset < 1 ) throw new Error ( `Invalid address : ${ addr } ` )
388+ if ( gc === - 1 ? offset !== 0 : offset < 1 ) throw new Error ( `Invalid address4 : ${ addr } ` )
386389
387390 let big = 0n
388391 for ( let i = 0 ; i < 8 ; i ++ ) {
0 commit comments