Skip to content

util: add guessHandleType method #2797

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions doc/api/util.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,19 @@ when the deprecated API is used. Configurable at run-time through the

`process.throwDeprecation` takes precedence over `process.traceDeprecation`.

## util.guessHandleType(fd)

Returns the assumed handle type for the given `fd`.

Possible values include:

* `'FILE'`
* `'TTY'`
* `'PIPE'`
* `'TCP'`
* `'UNKNOWN'`


## util.debug(string)

Stability: 0 - Deprecated: use console.error() instead.
Expand Down
14 changes: 14 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const uv = process.binding('uv');
const Buffer = require('buffer').Buffer;
const internalUtil = require('internal/util');
var Debug;
var TTYWrap;

const formatRegExp = /%[sdj%]/g;
exports.format = function(f) {
Expand Down Expand Up @@ -864,3 +865,16 @@ exports._exceptionWithHostPort = function(err,
}
return ex;
};

exports.guessHandleType = function(fd) {
if (!TTYWrap)
TTYWrap = process.binding('tty_wrap');

if (typeof fd !== 'number')
throw new TypeError('fd must be a number');

if (fd < 0)
throw new Error('fd cannot be less than 0');

return TTYWrap.guessHandleType(fd);
};
47 changes: 47 additions & 0 deletions test/sequential/test-util-guess-handle-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const util = require('util');
const fs = require('fs');
const path = require('path');
const net = require('net');

// Throw on non-numeric fd
assert.throws(function() {
util.guessHandleType('test');
}, /fd must be a number/);

// Throw on fd < 0
assert.throws(function() {
util.guessHandleType(-1);
}, /fd cannot be less than 0/);

// Check for FILE handle type
const filename = path.join(common.tmpDir, 'guess-handle');
common.refreshTmpDir();
fs.writeFileSync(filename, '', 'utf8');
const fd = fs.openSync(filename, 'r+');
assert.strictEqual(util.guessHandleType(fd), 'FILE');
fs.closeSync(fd);
fs.unlinkSync(filename);

// Check for TTY handle type
assert.strictEqual(util.guessHandleType(process.stdin.fd), 'TTY');

// Check for PIPE handle type
var server = net.createServer(assert.fail);
server.listen(common.PIPE, function() {
assert.strictEqual(util.guessHandleType(server._handle.fd), 'PIPE');
server.close();
});

// Check for TCP handle type
var server2 = net.createServer(assert.fail);
server2.listen(common.util, function() {
assert.strictEqual(util.guessHandleType(server2._handle.fd), 'TCP');
server2.close();
});

// Check for UNKNOWN handle type
assert.strictEqual(util.guessHandleType(123456), 'UNKNOWN');