@@ -78,6 +78,16 @@ export function sanitizeForFilePath(s: string) {
7878 return s . replace ( / [ \x00 - \x2C \x2E - \x2F \x3A - \x40 \x5B - \x60 \x7B - \x7F ] + / g, '-' ) ;
7979}
8080
81+ export function trimLongString ( s : string , length = 100 ) {
82+ if ( s . length <= length )
83+ return s ;
84+ const hash = calculateSha1 ( s ) ;
85+ const middle = `-${ hash . substring ( 0 , 5 ) } -` ;
86+ const start = Math . floor ( ( length - middle . length ) / 2 ) ;
87+ const end = length - middle . length - start ;
88+ return s . substring ( 0 , start ) + middle + s . slice ( - end ) ;
89+ }
90+
8191export function isPathInside ( root : string , candidate : string ) : boolean {
8292 const resolvedRoot = path . resolve ( root ) ;
8393 const resolvedCandidate = path . resolve ( candidate ) ;
@@ -104,16 +114,24 @@ export function toPosixPath(aPath: string): string {
104114 return aPath . split ( path . sep ) . join ( path . posix . sep ) ;
105115}
106116
117+ // macOS sun_path is 104 bytes (Linux is 108) including the NUL terminator. Use the lower bound.
118+ const UNIX_SOCKET_PATH_MAX = 103 ;
119+
107120export function makeSocketPath ( domain : string , name : string ) : string {
108121 const userNameHash = calculateSha1 ( process . env . USERNAME || process . env . USER || 'default' ) . slice ( 0 , 8 ) ;
109122 if ( process . platform === 'win32' ) {
110- const socketsDir = process . env . PLAYWRIGHT_SOCKETS_DIR ;
123+ const socketsDir = process . env . PWTEST_SOCKETS_DIR ;
111124 const suffix = socketsDir ? `-${ calculateSha1 ( socketsDir ) . slice ( 0 , 8 ) } ` : '' ;
112125 return `\\\\.\\pipe\\pw-${ userNameHash } -${ domain } -${ name } ${ suffix } ` ;
113126 }
114- const baseDir = process . env . PLAYWRIGHT_SOCKETS_DIR || path . join ( os . tmpdir ( ) , `pw-${ userNameHash } ` ) ;
127+ const baseDir = process . env . PWTEST_SOCKETS_DIR || path . join ( os . tmpdir ( ) , `pw-${ userNameHash } ` ) ;
115128 const dir = path . join ( baseDir , domain ) ;
116- const result = path . join ( dir , `${ name } .sock` ) ;
129+ const suffix = '.sock' ;
130+ const maxNameLength = UNIX_SOCKET_PATH_MAX - dir . length - path . sep . length - suffix . length ;
131+ if ( maxNameLength < 1 )
132+ throw new Error ( `Socket directory path is too long (${ dir . length } chars); set PWTEST_SOCKETS_DIR to a shorter location.` ) ;
133+ const fsFriendlyName = trimLongString ( sanitizeForFilePath ( name ) , maxNameLength ) ;
134+ const result = path . join ( dir , `${ fsFriendlyName } ${ suffix } ` ) ;
117135 fs . mkdirSync ( dir , { recursive : true } ) ;
118136 return result ;
119137}
0 commit comments