Skip to content

path: add Buffer support to join #34053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,20 +370,48 @@ const win32 = {

let joined;
let firstPart;
let isType;
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = firstPart = arg;
else
joined += `\\${arg}`;
if (typeof arg == 'string'){
if (arg.length > 0) {
if (joined === undefined){
joined = firstPart = arg;
isType = 'string';
}
else{
if (isType == 'string'){
joined += `\\${arg}`;
} else {
const argBuffer = Buffer.from(`\\${arg}`);
joined = Buffer.concat([joined, argBuffer]);
}
}
}
} else if (typeof arg == 'buffer'){
if (arg.length > 0) {
if (joined === undefined){
joined = firstPart = arg;
isType = 'buffer';
}
else{
if (isType == 'string'){
const joinedBuffer = Buffer.from(joined);
joined = Buffer.concat([joinedBuffer,Buffer.from(pathModule.sep),arg]);
} else {
joined = Buffer.concat([joined,Buffer.from(pathModule.sep),arg]);
}
}
}
} else {
validateString(arg, 'path');
}
}

if (joined === undefined)
return '.';


// Make sure that the joined path doesn't start with two slashes, because
// normalize() will mistake it for a UNC path then.
//
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-readdir-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const fs = require('fs');

const common = require('../common');
if (common.isWindows) {
common.skip('windows doesnt support /dev');
}

const assert = require('assert');

fs.readdir(
Buffer.from("/dev"),
{withFileTypes: true, encoding: "buffer"},
common.mustCall((e,d) => {
assert.strictEqual(e, null);
})
);
51 changes: 51 additions & 0 deletions test/parallel/test-path-join-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
require('../common');
const assert = require('assert');
const path = require('path');

const failures = [];
const backslashRE = /\\/g;

const joinTests = [
[ [path.posix.join, path.win32.join],
// Arguments result
[[[Buffer.from('.'), Buffer.from('x/b'), '..', Buffer.from('/b/c.js')], 'x/b/c.js'],
[[], '.'],
[[Buffer.from('/.'), 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
[['/foo', Buffer.from('../../../bar')], '/bar'],
[['foo', '../../../bar'], '../../bar'],
[['/', Buffer.from(''), '/foo'], '/foo'],
[['', '/', Buffer.from('foo')], '/foo'],
[['', '/', '/foo'], '/foo']
]
]
];

joinTests.forEach((test) => {
if (!Array.isArray(test[0]))
test[0] = [test[0]];
test[0].forEach((join) => {
test[1].forEach((test) => {
const actual = join.apply(null, test[0]);
const expected = test[1];
// For non-Windows specific tests with the Windows join(), we need to try
// replacing the slashes since the non-Windows specific tests' `expected`
// use forward slashes
let actualAlt;
let os;
if (join === path.win32.join) {
actualAlt = actual.replace(backslashRE, '/');
os = 'win32';
} else {
os = 'posix';
}
if (actual !== expected && actualAlt !== expected) {
const delimiter = test[0].map(JSON.stringify).join(',');
const message = `path.${os}.join(${delimiter})\n expect=${
JSON.stringify(expected)}\n actual=${JSON.stringify(actual)}`;
failures.push(`\n${message}`);
}
});
});
});
assert.strictEqual(failures.length, 0, failures.join(''));