import * as crypto from 'crypto' ; const MAX_DNS_NAME_LEN = 63 ; const VALIDATE = /^[0-9a-z-]+$/ ; const HASH_LEN = 8 ; /**
 * Utilities for generating unique and stable names.
 */ export class Names { link param path param maxLen throws public static toDnsLabel ( path , maxLen = MAX_DNS_NAME_LEN ) { if ( maxLen < HASH_LEN ) { throw new Error ( `minimum max length for object names is ${ HASH_LEN } (required for hash)` ) ; } const components = path . split ( '/' ) ; for ( const comp of components ) { if ( ! VALIDATE . test ( comp ) ) { throw new Error ( `"${ comp }" is not a valid object name. The characters allowed in names are: digits (0-9), lower case letters (a-z), -, and .` ) ; } if ( comp . length > maxLen ) { throw new Error ( `Object name "${ comp }" is too long. Maximum allowed length is ${ maxLen }` ) ; } } if ( components . length === 1 ) { return components [ 0 ] ; } components . push ( calcHash ( path , HASH_LEN ) ) ; return components . reverse ( ) . join ( '/' ) . slice ( 0 , maxLen ) . split ( '/' ) . reverse ( ) . filter ( x => x ) . join ( '-' ) ; } private constructor ( ) { return ; } } function calcHash ( path , maxLen ) { const hash = crypto . createHash ( 'sha256' ) ; hash . update ( path ) ; return hash . digest ( 'hex' ) . slice ( 0 , maxLen ) ; }