11import  {  ServerlessAzureConfig ,  ResourceConfig  }  from  "../models/serverless" 
22import  {  Guard  }  from  "../shared/guard" 
33import  configConstants  from  "../config" ; 
4+ import  md5  from  "md5" ; 
45
56export  class  AzureNamingService  { 
67
@@ -45,25 +46,28 @@ export class AzureNamingService {
4546   * @param  forbidden Regex for characters to remove from name. Defaults to non-alpha-numerics 
4647   * @param  replaceWith String to replace forbidden characters. Defaults to empty string 
4748   */ 
48-   public  static  getSafeResourceName ( config : ServerlessAzureConfig ,  maxLength : number ,  resourceConfig ?: ResourceConfig ,  suffix : string  =  "" ,  forbidden : RegExp  =  / \W + / g,  replaceWith : string  =  "" )  { 
49+   public  static  getSafeResourceName ( config : ServerlessAzureConfig ,  maxLength : number ,  resourceConfig ?: ResourceConfig ,  suffix : string  =  "" ,  includeHash  =  false )  { 
50+     const  nonAlphaNumeric  =  / \W + / g; 
51+ 
4952    if  ( resourceConfig  &&  resourceConfig . name )  { 
5053      const  {  name }  =  resourceConfig ; 
5154
5255      if  ( name . length  >  maxLength )  { 
5356        throw  new  Error ( `Name '${ name }  ' invalid. Should be shorter than ${ maxLength }   characters` ) ; 
5457      } 
5558
56-       return  name . replace ( forbidden ,   replaceWith ) ; 
59+       return  name . replace ( nonAlphaNumeric ,   "" ) ; 
5760    } 
5861
5962    const  {  prefix,  region,  stage }  =  config . provider ; 
6063
61-     let  safePrefix  =  prefix . replace ( forbidden ,   replaceWith ) ; 
64+     let  safePrefix  =  prefix . replace ( nonAlphaNumeric ,   "" ) ; 
6265    const  safeRegion  =  this . createShortAzureRegionName ( region ) ; 
6366    let  safeStage  =  this . createShortStageName ( stage ) ; 
64-     let  safeSuffix  =  suffix . replace ( forbidden ,   replaceWith ) ; 
67+     let  safeSuffix  =  suffix . replace ( nonAlphaNumeric ,   "" ) ; 
6568
66-     const  remaining  =  maxLength  -  ( safePrefix . length  +  safeRegion . length  +  safeStage . length  +  safeSuffix . length ) ; 
69+     const  hashLength  =  ( includeHash )  ? configConstants . resourceGroupHashLength  : 0 ; 
70+     const  remaining  =  maxLength  -  ( safePrefix . length  +  safeRegion . length  +  safeStage . length  +  safeSuffix . length  +  hashLength ) ; 
6771
6872    // Dynamically adjust the substring based on space needed 
6973    if  ( remaining  <  0 )  { 
@@ -77,9 +81,17 @@ export class AzureNamingService {
7781      safeSuffix  =  safeSuffix . substr ( 0 ,  partLength ) ; 
7882    } 
7983
80-     return  [ safePrefix ,  safeRegion ,  safeStage ,  safeSuffix ] 
84+     const  safeHash  =  md5 ( config . provider . resourceGroup ) . substr ( 0 ,  hashLength ) ; 
85+ 
86+     const  name  =  [ safePrefix ,  safeRegion ,  safeStage ,  safeHash ,  safeSuffix ] 
8187      . join ( "" ) 
8288      . toLowerCase ( ) ; 
89+ 
90+     if  ( name . length  >  maxLength )  { 
91+       throw  new  Error ( `Name ${ name }   is too long. Should be shorter than ${ maxLength }   characters` ) 
92+     } 
93+ 
94+     return  name ; 
8395  } 
8496
8597  /** 
0 commit comments