From e0a4a1edf2f097aecf6b96f8d0dabef4172ca2a2 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sat, 20 May 2017 19:26:59 +0430 Subject: [PATCH] cli: accept /dev/stdin as input file on Linux --- lib/module.js | 2 ++ src/node_file.cc | 12 ++++++++-- test/parallel/test-cli-arg-devstdin-posix.js | 23 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-cli-arg-devstdin-posix.js diff --git a/lib/module.js b/lib/module.js index 5455e68dd98b22..88d18bab24c416 100644 --- a/lib/module.js +++ b/lib/module.js @@ -195,6 +195,8 @@ Module._findPath = function(request, paths, isMain) { if (exts === undefined) exts = Object.keys(Module._extensions); filename = tryPackage(basePath, exts, isMain); + } else if (rc === 2) { + filename = basePath; } if (!filename) { diff --git a/src/node_file.cc b/src/node_file.cc index 7a3be9db541a16..828943b7b9cc35 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -546,7 +546,8 @@ static void InternalModuleReadFile(const FunctionCallbackInfo& args) { } // Used to speed up module loading. Returns 0 if the path refers to -// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.) +// a file, 1 when it's a directory, 2 when it's anything else, or < 0 on +// error (usually -ENOENT.) // The speedup comes from not creating thousands of Stat and Error objects. static void InternalModuleStat(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -558,7 +559,14 @@ static void InternalModuleStat(const FunctionCallbackInfo& args) { int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr); if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - rc = !!(s->st_mode & S_IFDIR); + + if (s->st_mode & S_IFREG) { + rc = 0; + } else if (s->st_mode & S_IFDIR) { + rc = 1; + } else { + rc = 2; + } } uv_fs_req_cleanup(&req); diff --git a/test/parallel/test-cli-arg-devstdin-posix.js b/test/parallel/test-cli-arg-devstdin-posix.js new file mode 100644 index 00000000000000..0aea6142926036 --- /dev/null +++ b/test/parallel/test-cli-arg-devstdin-posix.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); + +if (common.isWindows) { + common.skip('This test does not apply to Windows.'); + return; +} + +const assert = require('assert'); + +const expected = '--option-to-be-seen-on-child'; + +const { spawn } = require('child_process'); +const child = spawn(process.execPath, ['/dev/stdin', expected], { stdio: 'pipe' }); + +child.stdin.end('console.log(process.argv[2])'); + +let actual = ''; +child.stdout.setEncoding('utf8'); +child.stdout.on('data', (chunk) => actual += chunk); +child.stdout.on('end', common.mustCall(() => { + assert.strictEqual(actual.trim(), expected); +}));