Skip to content
Open
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
8 changes: 5 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const {
},
copyObject,
Dirent,
join,
relative,
getDirent,
getDirents,
getOptions,
Expand Down Expand Up @@ -1401,7 +1403,7 @@ function handleDirents({ result, currentPath, context }) {
for (let i = 0; i < length; i++) {
// Avoid excluding symlinks, as they are not directories.
// Refs: https://github.com/nodejs/node/issues/52663
const fullPath = pathModule.join(currentPath, names[i]);
const fullPath = join(currentPath, names[i]);
const dirent = getDirent(currentPath, names[i], types[i]);
ArrayPrototypePush(context.readdirResults, dirent);

Expand All @@ -1413,8 +1415,8 @@ function handleDirents({ result, currentPath, context }) {

function handleFilePaths({ result, currentPath, context }) {
for (let i = 0; i < result.length; i++) {
const resultPath = pathModule.join(currentPath, result[i]);
const relativeResultPath = pathModule.relative(context.basePath, resultPath);
const resultPath = join(currentPath, result[i]);
const relativeResultPath = relative(context.basePath, resultPath);
const stat = binding.internalModuleStat(resultPath);
ArrayPrototypePush(context.readdirResults, relativeResultPath);

Expand Down
32 changes: 32 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ function join(path, name) {
return Buffer.concat([pathBuffer, name]);
}

if (isUint8Array(path) && typeof name === 'string') {
const nameBuffer = Buffer.from(name);
return Buffer.concat([path, bufferSep, nameBuffer]);
}

if (typeof path === 'string' && typeof name === 'string') {
return pathModule.join(path, name);
}
Expand All @@ -241,6 +246,28 @@ function join(path, name) {
'path', ['string', 'Buffer'], path);
}

function relative(from, to) {
if (typeof from === 'string' && isUint8Array(to)) {
return pathModule.relative(from, Buffer.from(to));
}

if (isUint8Array(from) && typeof to === 'string') {
return Buffer.from(pathModule.relative(Buffer.from(from), to));
}

if (isUint8Array(from) && isUint8Array(to)) {
const fromPath = Buffer.from(from);
const toPath = Buffer.from(to);
return pathModule.relative(fromPath.toString(), toPath.toString());
}

if (typeof from === 'string' && typeof to === 'string') {
return pathModule.relative(from, to);
}

throw new ERR_INVALID_ARG_TYPE('from', ['string', 'Buffer'], from);
}

function getDirents(path, { 0: names, 1: types }, callback) {
let i;
if (typeof callback === 'function') {
Expand Down Expand Up @@ -719,6 +746,9 @@ const validatePath = hideStackFrames((path, propName = 'path') => {
);
});

/**
* @returns {string|Buffer}
*/
const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => {
const path = toPathIfFileURL(fileURLOrPath);
validatePath(path, propName);
Expand Down Expand Up @@ -925,6 +955,8 @@ module.exports = {
kReadFileUnknownBufferLength,
kWriteFileMaxChunkSize,
},
join,
relative,
assertEncoding,
BigIntStats, // for testing
copyObject,
Expand Down
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsString());
CHECK(args[0]->IsString() || args[0]->IsArrayBufferView());
BufferValue path(env->isolate(), args[0]);
CHECK_NOT_NULL(*path);
ToNamespacedPath(env, &path);
Expand Down
6 changes: 1 addition & 5 deletions test/parallel/test-fs-readdir-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ if (!common.isMacOS) {
common.skip('this tests works only on MacOS');
}

const assert = require('assert');

fs.readdir(
Buffer.from('/dev'),
{ withFileTypes: true, encoding: 'buffer' },
common.mustCall((e, d) => {
assert.strictEqual(e, null);
})
common.mustSucceed()
);
2 changes: 2 additions & 0 deletions test/parallel/test-fs-readdir-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ test('readdir should not recurse into Unix domain sockets', (t, done) => {
// The process should not crash
// See https://github.com/nodejs/node/issues/52159
fs.readdirSync(tmpdir.path, { recursive: true });
// Buffer with utf8 default encoding
fs.readdirSync(Buffer.from(tmpdir.path), { recursive: true });
server.close();
done();
}));
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-fs-readdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ files.forEach(function(currentFile) {
// Check the readdir Sync version
assert.deepStrictEqual(files, fs.readdirSync(readdirDir).sort());

// Check the readdir Sync version (Buffer with utf8 default encoding)
assert.deepStrictEqual(
files,
fs.readdirSync(Buffer.from(readdirDir)).sort()
);

// Check the readdir async version
fs.readdir(readdirDir, common.mustSucceed((f) => {
assert.deepStrictEqual(files, f.sort());
}));

// Check the readdir async version (Buffer with utf8 default encoding)
fs.readdir(
Buffer.from(readdirDir),
common.mustSucceed((f) => {
assert.deepStrictEqual(files, f.sort());
})
);

// readdir() on file should throw ENOTDIR
// https://github.com/joyent/node/issues/1869
assert.throws(function() {
Expand Down
Loading