88} = primordials ;
99
1010const { Buffer } = require ( 'buffer' ) ;
11+ const { posix : pathPosix } = require ( 'path' ) ;
1112const { VirtualProvider } = require ( 'internal/vfs/provider' ) ;
1213const { MemoryFileHandle } = require ( 'internal/vfs/file_handle' ) ;
1314const {
@@ -169,27 +170,14 @@ class MemoryProvider extends VirtualProvider {
169170 * @returns {string } Normalized path
170171 */
171172 _normalizePath ( path ) {
172- // Normalize slashes
173+ // Convert backslashes to forward slashes
173174 let normalized = path . replace ( / \\ / g, '/' ) ;
175+ // Ensure absolute path
174176 if ( ! normalized . startsWith ( '/' ) ) {
175177 normalized = '/' + normalized ;
176178 }
177-
178- // Split into segments and resolve . and ..
179- const segments = normalized . split ( '/' ) . filter ( ( s ) => s !== '' && s !== '.' ) ;
180- const resolved = [ ] ;
181- for ( const segment of segments ) {
182- if ( segment === '..' ) {
183- // Go up one level (but don't go above root)
184- if ( resolved . length > 0 ) {
185- resolved . pop ( ) ;
186- }
187- } else {
188- resolved . push ( segment ) ;
189- }
190- }
191-
192- return '/' + resolved . join ( '/' ) ;
179+ // Use path.posix.normalize to resolve . and ..
180+ return pathPosix . normalize ( normalized ) ;
193181 }
194182
195183 /**
@@ -213,11 +201,7 @@ class MemoryProvider extends VirtualProvider {
213201 if ( path === '/' ) {
214202 return null ;
215203 }
216- const lastSlash = path . lastIndexOf ( '/' ) ;
217- if ( lastSlash === 0 ) {
218- return '/' ;
219- }
220- return path . slice ( 0 , lastSlash ) ;
204+ return pathPosix . dirname ( path ) ;
221205 }
222206
223207 /**
@@ -226,8 +210,7 @@ class MemoryProvider extends VirtualProvider {
226210 * @returns {string } Base name
227211 */
228212 _getBaseName ( path ) {
229- const lastSlash = path . lastIndexOf ( '/' ) ;
230- return path . slice ( lastSlash + 1 ) ;
213+ return pathPosix . basename ( path ) ;
231214 }
232215
233216 /**
@@ -241,11 +224,8 @@ class MemoryProvider extends VirtualProvider {
241224 return this . _normalizePath ( target ) ;
242225 }
243226 // Relative target: resolve against symlink's parent directory
244- const parentPath = this . _getParentPath ( symlinkPath ) ;
245- if ( parentPath === null ) {
246- return this . _normalizePath ( '/' + target ) ;
247- }
248- return this . _normalizePath ( parentPath + '/' + target ) ;
227+ const parentPath = this . _getParentPath ( symlinkPath ) || '/' ;
228+ return this . _normalizePath ( pathPosix . join ( parentPath , target ) ) ;
249229 }
250230
251231 /**
@@ -264,7 +244,7 @@ class MemoryProvider extends VirtualProvider {
264244
265245 const segments = this . _splitPath ( normalized ) ;
266246 let current = this [ kRoot ] ;
267- let currentPath = '' ;
247+ let currentPath = '/ ' ;
268248
269249 for ( let i = 0 ; i < segments . length ; i ++ ) {
270250 const segment = segments [ i ] ;
@@ -291,14 +271,14 @@ class MemoryProvider extends VirtualProvider {
291271 }
292272
293273 // Ensure directory is populated before accessing children
294- this . _ensurePopulated ( current , currentPath || '/' ) ;
274+ this . _ensurePopulated ( current , currentPath ) ;
295275
296276 const entry = current . children . get ( segment ) ;
297277 if ( ! entry ) {
298278 return { entry : null , resolvedPath : null } ;
299279 }
300280
301- currentPath = currentPath + '/' + segment ;
281+ currentPath = pathPosix . join ( currentPath , segment ) ;
302282 current = entry ;
303283 }
304284
@@ -353,7 +333,7 @@ class MemoryProvider extends VirtualProvider {
353333
354334 // Follow symlinks in parent path
355335 if ( current . isSymbolicLink ( ) ) {
356- const currentPath = '/' + segments . slice ( 0 , i ) . join ( '/' ) ;
336+ const currentPath = pathPosix . join ( '/' , ... segments . slice ( 0 , i ) ) ;
357337 const targetPath = this . _resolveSymlinkTarget ( currentPath , current . target ) ;
358338 const result = this . _lookupEntry ( targetPath , true , 0 ) ;
359339 if ( ! result . entry ) {
@@ -367,8 +347,8 @@ class MemoryProvider extends VirtualProvider {
367347 }
368348
369349 // Ensure directory is populated before accessing children
370- const currentPath = '/' + segments . slice ( 0 , i ) . join ( '/' ) ;
371- this . _ensurePopulated ( current , currentPath || '/' ) ;
350+ const currentPath = pathPosix . join ( '/' , ... segments . slice ( 0 , i ) ) ;
351+ this . _ensurePopulated ( current , currentPath ) ;
372352
373353 let entry = current . children . get ( segment ) ;
374354 if ( ! entry ) {
@@ -388,7 +368,7 @@ class MemoryProvider extends VirtualProvider {
388368 }
389369
390370 // Ensure final directory is populated
391- const finalPath = '/' + segments . join ( '/' ) ;
371+ const finalPath = pathPosix . join ( '/' , ... segments ) ;
392372 this . _ensurePopulated ( current , finalPath ) ;
393373
394374 return current ;
@@ -578,10 +558,10 @@ class MemoryProvider extends VirtualProvider {
578558 // Create all parent directories
579559 const segments = this . _splitPath ( normalized ) ;
580560 let current = this [ kRoot ] ;
581- let currentPath = '' ;
561+ let currentPath = '/ ' ;
582562
583563 for ( const segment of segments ) {
584- currentPath = currentPath + '/' + segment ;
564+ currentPath = pathPosix . join ( currentPath , segment ) ;
585565 let entry = current . children . get ( segment ) ;
586566 if ( ! entry ) {
587567 entry = new MemoryEntry ( TYPE_DIR , { mode : options ?. mode } ) ;
0 commit comments