Skip to content

Commit e0a4a1e

Browse files
committed
cli: accept /dev/stdin as input file on Linux
1 parent b5ae22d commit e0a4a1e

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ Module._findPath = function(request, paths, isMain) {
195195
if (exts === undefined)
196196
exts = Object.keys(Module._extensions);
197197
filename = tryPackage(basePath, exts, isMain);
198+
} else if (rc === 2) {
199+
filename = basePath;
198200
}
199201

200202
if (!filename) {

src/node_file.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
546546
}
547547

548548
// Used to speed up module loading. Returns 0 if the path refers to
549-
// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.)
549+
// a file, 1 when it's a directory, 2 when it's anything else, or < 0 on
550+
// error (usually -ENOENT.)
550551
// The speedup comes from not creating thousands of Stat and Error objects.
551552
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
552553
Environment* env = Environment::GetCurrent(args);
@@ -558,7 +559,14 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
558559
int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr);
559560
if (rc == 0) {
560561
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
561-
rc = !!(s->st_mode & S_IFDIR);
562+
563+
if (s->st_mode & S_IFREG) {
564+
rc = 0;
565+
} else if (s->st_mode & S_IFDIR) {
566+
rc = 1;
567+
} else {
568+
rc = 2;
569+
}
562570
}
563571
uv_fs_req_cleanup(&req);
564572

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (common.isWindows) {
5+
common.skip('This test does not apply to Windows.');
6+
return;
7+
}
8+
9+
const assert = require('assert');
10+
11+
const expected = '--option-to-be-seen-on-child';
12+
13+
const { spawn } = require('child_process');
14+
const child = spawn(process.execPath, ['/dev/stdin', expected], { stdio: 'pipe' });
15+
16+
child.stdin.end('console.log(process.argv[2])');
17+
18+
let actual = '';
19+
child.stdout.setEncoding('utf8');
20+
child.stdout.on('data', (chunk) => actual += chunk);
21+
child.stdout.on('end', common.mustCall(() => {
22+
assert.strictEqual(actual.trim(), expected);
23+
}));

0 commit comments

Comments
 (0)