Skip to content

Commit

Permalink
path: update win32 toNamespacedPath to support device namespace paths
Browse files Browse the repository at this point in the history
  • Loading branch information
EarlyRiser42 committed Aug 16, 2024
1 parent 880c446 commit aed9a78
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ function glob(path, pattern, windows) {
});
}

// Regular expressions to identify special device names in Windows.
// COM to AUX (e.g., COM1, LPT1, NUL, CON, CONIN$, PRN, AUX) are reserved OS device names.
// therefore, Paths like C:\path\to\COM1 map to \\.\COM1, referencing hardware or system streams.
// Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
//
// PhysicalDrive to Changer (e.g., PhysicalDrive1, TAPE0, Changer0) are not reserved OS device names.
// Ref: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
const windowDevicePatterns = [
/([\\/])?(COM\d+)$/i,
/([\\/])?(LPT\d+)$/i,
/([\\/])?(NUL)$/i,
/([\\/])?(CON)$/i,
/([\\/])?(PRN)$/i,
/([\\/])?(AUX)$/i,
/([\\/])?(CONIN\$)$/i,
/([\\/])?(CONOUT\$)$/i,
/^(PHYSICALDRIVE\d+)$/i,
/^(PIPE\\.+)$/i,
/^(MAILSLOT\\.+)$/i,
/^(TAPE\d+)$/i,
/^(CHANGER\d+)$/i,
];

const win32 = {
/**
* path.resolve([from ...], to)
Expand Down Expand Up @@ -687,6 +710,19 @@ const win32 = {
if (typeof path !== 'string' || path.length === 0)
return path;

// Check if the path matches any device pattern
if (windowDevicePatterns.some((pattern) => pattern.test(path))) {
let deviceName;
if (/^(PIPE\\.+)$/i.test(path) || /^(MAILSLOT\\.+)$/i.test(path)) {
// If the path starts with PIPE\ or MAILSLOT\, keep it as is
deviceName = path;
} else {
// Extract the last component after the last slash or backslash
deviceName = path.split(/[\\/]/).pop();
}
return `\\\\.\\${deviceName}`;
}

const resolvedPath = win32.resolve(path);

if (resolvedPath.length <= 2)
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-path-makelong.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,34 @@ if (common.isWindows) {
assert.strictEqual(path.toNamespacedPath(
'\\\\?\\UNC\\someserver\\someshare\\somefile'),
'\\\\?\\UNC\\someserver\\someshare\\somefile');
// Device name tests
assert.strictEqual(path.toNamespacedPath('C:\\path\\COM1'),
'\\\\.\\COM1');
assert.strictEqual(path.toNamespacedPath('COM1'),
'\\\\.\\COM1');
assert.strictEqual(path.toNamespacedPath('LPT1'),
'\\\\.\\LPT1');
assert.strictEqual(path.toNamespacedPath('C:\\LPT1'),
'\\\\.\\LPT1');
assert.strictEqual(path.toNamespacedPath('PhysicalDrive0'),
'\\\\.\\PhysicalDrive0');
assert.strictEqual(path.toNamespacedPath('pipe\\mypipe'),
'\\\\.\\pipe\\mypipe');
assert.strictEqual(path.toNamespacedPath('MAILSLOT\\mySlot'),
'\\\\.\\MAILSLOT\\mySlot');
assert.strictEqual(path.toNamespacedPath('NUL'),
'\\\\.\\NUL');
assert.strictEqual(path.toNamespacedPath('Tape0'),
'\\\\.\\Tape0');
assert.strictEqual(path.toNamespacedPath('Changer0'),
'\\\\.\\Changer0');
// Test cases for inputs with "\\.\" prefix
assert.strictEqual(path.toNamespacedPath('\\\\.\\pipe\\somepipe'),
'\\\\.\\pipe\\somepipe');
assert.strictEqual(path.toNamespacedPath('\\\\.\\COM1'),
'\\\\.\\COM1');
assert.strictEqual(path.toNamespacedPath('\\\\.\\LPT1'),
'\\\\.\\LPT1');
}

assert.strictEqual(path.toNamespacedPath(''), '');
Expand Down

0 comments on commit aed9a78

Please sign in to comment.