@@ -25,6 +25,7 @@ const {
2525 ArrayPrototypeJoin,
2626 ArrayPrototypeSlice,
2727 FunctionPrototypeBind,
28+ ObjectValues,
2829 StringPrototypeCharCodeAt,
2930 StringPrototypeIndexOf,
3031 StringPrototypeLastIndexOf,
@@ -180,6 +181,25 @@ function glob(path, pattern, windows) {
180181 } ) ;
181182}
182183
184+ // Regular expressions to identify special device names in Windows.
185+ // Ref: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
186+ // COM to AUX (e.g., COM1, LPT1, NUL, CON, PRN, AUX) are reserved OS device names.
187+ // Paths like C:\path\to\COM1 map to \\.\COM1, referencing hardware or system streams.
188+ // Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
189+ const windowDevicePatterns = {
190+ comPortRegex : / ( [ \\ / ] ) ? ( C O M \d + ) $ / i,
191+ lptPortRegex : / ( [ \\ / ] ) ? ( L P T \d + ) $ / i,
192+ nulDeviceRegex : / ( [ \\ / ] ) ? ( N U L ) $ / i,
193+ conDeviceRegex : / ( [ \\ / ] ) ? ( C O N ) $ / i,
194+ prnDeviceRegex : / ( [ \\ / ] ) ? ( P R N ) $ / i,
195+ auxDeviceRegex : / ( [ \\ / ] ) ? ( A U X ) $ / i,
196+ physicalDriveRegex : / ^ ( P H Y S I C A L D R I V E \d + ) $ / i,
197+ pipeRegex : / ^ ( P I P E \\ .+ ) $ / i,
198+ mailslotRegex : / ^ ( M A I L S L O T \\ .+ ) $ / i,
199+ tapeDriveRegex : / ^ ( T A P E \d + ) $ / i,
200+ changerDeviceRegex : / ^ ( C H A N G E R \d + ) $ / i,
201+ } ;
202+
183203const win32 = {
184204 /**
185205 * path.resolve([from ...], to)
@@ -687,6 +707,19 @@ const win32 = {
687707 if ( typeof path !== 'string' || path . length === 0 )
688708 return path ;
689709
710+ // Check if the path matches any device pattern
711+ if ( ObjectValues ( windowDevicePatterns ) . some ( ( pattern ) => pattern . test ( path ) ) ) {
712+ let deviceName ;
713+ if ( windowDevicePatterns . pipeRegex . test ( path ) || windowDevicePatterns . mailslotRegex . test ( path ) ) {
714+ // If the path starts with PIPE\ or MAILSLOT\, keep it as is
715+ deviceName = path ;
716+ } else {
717+ // Extract the last component after the last slash or backslash
718+ deviceName = path . split ( / [ \\ / ] / ) . pop ( ) ;
719+ }
720+ return `\\\\.\\${ deviceName } ` ;
721+ }
722+
690723 const resolvedPath = win32 . resolve ( path ) ;
691724
692725 if ( resolvedPath . length <= 2 )
0 commit comments