unjs / pathe

🛣️ Drop-in replacement of the Node.js's path module module that ensures paths are normalized
Other
401 stars 16 forks source link

`path.join` should handle protocol with double slashes #178

Open cossssmin opened 2 months ago

cossssmin commented 2 months ago

Environment

pathe v1.1.2

Node.js v20.10.0, Windows 11

Reproduction

$ node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> const pe = require('pathe')
undefined
> pe.join('https://example.com', 'image.png')
'https:/example.com/image.png'
> const path = require('node:path')
undefined
> path.join('https://example.com', 'image.png')
'https:\\example.com\\image.png'
>

Describe the bug

When using join with a URL, the double // is reduced to a single /:

- https://example.com
+ https:/example.com

Additional context

No response

Logs

No response

ToshB commented 1 week ago

I encountered a similar case with UNC paths, where it seems like the deduplication of forward slashes also break UNC paths in a similar way. It works the first time, but after the leading slashes have been normalized, consequent calls to join replace them with a single slash.

const initialPath = '\\\\uncpath\\share';
const normalizedPath = pe.join(initialPath, 'subfolder'); // //uncpath/share/subfolder
const mangledPath = pe.join(normalizedPath, 'file.txt'); // /uncpath/share/subfolder/file.txt