diff --git a/src/node_file.cc b/src/node_file.cc index 5a50aacb1b939d..cffe42dbbed8ec 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1986,8 +1986,26 @@ static void ReadDir(const FunctionCallbackInfo& args) { BufferValue path(isolate, args[0]); CHECK_NOT_NULL(*path); +#ifdef _WIN32 + bool slashCheck = false; + if (path.ToStringView().ends_with("/") || + path.ToStringView().ends_with("\\")) { + slashCheck = true; + } +#endif + ToNamespacedPath(env, &path); +#ifdef _WIN32 + if (slashCheck) { + std::string result = path.ToString() + "\\"; + size_t new_length = result.size(); + path.AllocateSufficientStorage(new_length + 1); + path.SetLength(new_length); + memcpy(path.out(), result.c_str(), result.size() + 1); + } +#endif + const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); bool with_types = args[2]->IsTrue(); diff --git a/test/parallel/test-fs-readdir-pipe.js b/test/parallel/test-fs-readdir-pipe.js new file mode 100644 index 00000000000000..ae207f93e3cb58 --- /dev/null +++ b/test/parallel/test-fs-readdir-pipe.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { readdir, readdirSync } = require('fs'); + +if (!common.isWindows) { + common.skip('This test is specific to Windows to test enumerate pipes'); +} + +// Ref: https://github.com/nodejs/node/issues/56002 +// This test is specific to Windows. + +const pipe = '\\\\.\\pipe\\'; + +assert.ok(readdirSync(pipe).length >= 0); + +readdir(pipe, common.mustCall((err, files) => { + assert.ok(err == null); + assert.ok(files.length >= 0); +}));