-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
os: add userInfo() method #6104
Conversation
Might it be a good idea to name this differently?
|
I agree that it should probably be named something else. I don't have any suggestions at the moment though. I suppose |
Alternatively, you could expose the individual properties as functions. If you stick with the object-with-properties approach, then the documentation should explain the difference between |
+1 to renaming, or separating out |
+1 to renaming. The change LGTM with that change made. |
78722ca
to
4abd014
Compare
Updated. I decided to rename to |
} | ||
|
||
entry->Set(env->homedir_string(), | ||
OneByteString(env->isolate(), pwd.homedir)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick nit: given the likelihood that this is running on linux, there's a possibility that this may not actually be a OneByteString. It complicates things just a bit, but it might be worthwhile allowing an optional options (e.g. {encoding:'utf8'}
to be passed in to GetUserInfo
so that the encoding of the path can be specified. This would make it consistent with the new Buffer support in the fs module.
LGTM. Left a comment that could be deferred to a separate PR if necessary. |
## os.userInfo() | ||
|
||
Returns a subset of the password file entry for the current effective user. The | ||
returned object includes the `username`, `uid`, `gid`, `shell`, and `homedir`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shell
and homedir
should be swapped here because that's the way they appear in passwd
.
I'm also worried about the duplication and the fact that it's not Windows compatible. Are there use cases where |
What about |
@silverwind ... as I understand it, yes. I cannot recall the exact issue number but there is an open request to be able to retrieve the actual user information separately from the environment variables in certain use cases. The reason is that the env var could actually be incorrect in those edge cases. The fact that this is not Windows compatible is not too concerning to me. We already have plenty of similar cases. |
I'd prefer keeping it to just |
How about adding |
👍 On OS X at least it contains the user's fullname, which I could use for my |
@jasnell I changed to UTF8 for consistency with |
re: |
In that edge case, the dev could pass encoding: 'buffer' and get back
|
Refs: #5582 |
@jasnell updated to include an encoding option. |
operating system. This differs from the result of `os.homedir()`, which queries | ||
several environment variables for the home directory before falling back to the | ||
operating system response. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: perhaps add a short note that if encoding
is passed as 'buffer'
the username
, homedir
, and shell
will be returned as Buffer
instances.
LGTM with a nit. |
nit addressed. CI is green: https://ci.nodejs.org/job/node-test-pull-request/2248/ |
os.userInfo() calls libuv's uv_os_get_passwd() function. It returns an object containing the current effective user's username, uid, gid, shell, and home directory. On Windows, the uid and gid are -1, and the shell is null. Refs: nodejs#5582 PR-URL: nodejs#6104 Reviewed-By: James M Snell <jasnell@gmail.com>
I made a polyfill for anyone needing this for existing Node.js versions (all the way back to 0.10): https://github.com/sindresorhus/user-info |
Can someone justify this please? Most of this doesn't make sense cross-platform and as @bnoordhuis has pointed out we already expose most of these properties. Can we please not just implement things here because libuv does, we need to have more discipline than that. Too many tiny new features are getting in just because we can without much attention paid to is it worth it. |
Adding a temporary
I'm not a fan of this and would personally prefer a revert. |
I too feel that the added functionality seems rather unnecessary and that it was landed too fast without enough consensus. |
Can't really agree with the "landed too fast" part. The PR was opened 8 days ago and landed 2 days ago. The standard rule in the collaborator's guide is 48/72 hours (weekday/weekend). We've had plenty of stuff land in far less time. While I can certainly understand regarding the not enough consensus part, given that I'm the only one who gave it an LGTM, it cannot be said that there wasn't enough time to review it. On the consensus part, this was looked at by six people, four of whom are CTC, none of whom objected. Again, we've had stuff land with less. That said, @rvagg makes a fair request with regards to use case. For my part, I consider this an ok fix for #5582 because it does not change the existing behavior of bash-3.2$ sudo -u root ./node -pe "require('os').userInfo()"
{ uid: 0,
gid: 0,
username: 'root',
homedir: '/var/root',
shell: '/bin/sh' }
bash-3.2$ sudo -u root ./node -pe "require('os').homedir()"
/Users/james
bash-3.2$ I agree that the |
(Sorry for joining post-merge as well) I feel that the new |
One thing to note is that, as it is currently implemented, Given the benchmark: 'use strict';
const common = require('../common.js');
const os = require('os');
const bench = common.createBenchmark(main, {
method: ['old', 'new'],
millions: [2]
});
function main(conf) {
const n = +conf.millions * 1e6;
var i = 0;
switch (conf.method) {
case 'old':
bench.start();
for (; i < n; i++) {
var m = {
user: process.env.USER,
shell: process.env.SHELL,
homedir: os.homedir(),
gid: process.getgid(),
uid: process.getuid()
}
}
bench.end(n / 1e6);
break;
case 'new':
bench.start();
for (; i < n; i++) {
os.userInfo();
}
bench.end(n / 1e6);
break;
default:
throw new Error('Unexpected');
}
} We see:
If I modify the const _userInfo = {};
exports.userInfo = function(options = {encoding: 'utf'}) {
if (typeof options === 'string')
options = {encoding: options};
if (!_userInfo[options.encoding]) {
_userInfo[options.encoding] = binding.getUserInfo(options);
}
return _userInfo[options.encoding];
} The benchmark changes to:
Given that it's not likely that this function will be called multiple times within a single process, however, the benchmark itself (and adding caching) may be of little value. |
This landed in v6, and as far as I can tell should not have. Should we issue a revert? (Getting in was a bug of our release process, by the sounds of it.) |
@Fishrock123 Strictly speaking, reverting a semver-minor feature would be a semver-major… Or is this feature broken? |
This feature is not broken, and I agree, fully removing this in v6 is probably not a good idea. |
We should deprecate it in the docs then. |
No-one is significantly using it yet, so we can do that immediately. |
It should go through the normal deprecation dance if anything. I certainly disagree with the sentiment that it shouldn't have landed in v6. It was part of master, it landed through the normal processes, there was no revert PR before v6. |
Checklist
Affected core subsystem(s)
os
Description of change
os.getpw()
calls libuv'suv_os_get_passwd()
function. It returns an object containing the current effective user'susername
,uid
,gid
,shell
, andhomedir
. On Windows, theuid
andgid
are-1
, and theshell
isnull
.