Skip to content
This repository has been archived by the owner on Feb 24, 2018. It is now read-only.

Commit

Permalink
updates for io.js 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Aug 9, 2015
1 parent a499b3f commit b954acf
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 86 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Continuation Labs
Copyright (c) 2014-2015 Continuation Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ Returns the current username. Calls `GetUserName()` on Windows, and `getlogin_r(

### `getHomeDirectory()`

** Deprecated ** - As of Node/io.js v2.3.0, `os.homedir()` provides this functionality.

- Arguments
- None
- Returns
- string - The current user's home directory.

Returns the current user's home directory. Calls `SHGetKnownFolderPath()` on Windows, and `getlogin_r()` followed by `getpwnam()` otherwise. Throws if the home directory cannot be retrieved.
Returns the current user's home directory. Calls `SHGetKnownFolderPath()` on Windows, and `getlogin_r()` followed by `getpwnam()` otherwise. Throws if the home directory cannot be retrieved. Currently, only the current user's home directory can be retrieved, due to Windows limitations.

## Command Line Interface (CLI)

Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"targets": [
{
"target_name": "userinfo",
"win_delay_load_hook": "false",
"sources": [
"src/userinfo.cc"
],
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "userinfo",
"version": "1.1.1",
"description": "cross platform user info",
"author": "Continuation Labs",
"author": "Continuation Labs <[email protected]> (http://continuation.io/)",
"homepage": "https://github.com/continuationlabs/userinfo",
"repository": {
"type": "git",
Expand All @@ -17,16 +17,17 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "node node_modules/lab/bin/lab -v -L -t 100 -a code",
"test": "semistandard && lab -v -t 100 -a code",
"rebuild": "node-gyp rebuild"
},
"dependencies": {
"bossy": "1.x.x",
"nan": "1.x.x"
"nan": "2.x.x"
},
"devDependencies": {
"code": "1.x.x",
"lab": "5.x.x"
"lab": "5.x.x",
"semistandard": "7.x.x"
},
"bin": {
"userinfo": "./bin/userinfo"
Expand Down
37 changes: 12 additions & 25 deletions src/unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,46 @@
using namespace v8;


NAN_METHOD(WhoAmI) {
NanScope();
void WhoAmI(const Nan::FunctionCallbackInfo<Value>& info) {
char buf[BUFSIZ];
int r = getlogin_r(buf, BUFSIZ);

if (r != 0) {
strerror_r(r, buf, BUFSIZ);
NanThrowError(buf);
NanReturnUndefined();
Nan::ThrowError(buf);
return;
}

NanReturnValue(NanNew(buf));
info.GetReturnValue().Set(Nan::New(buf).ToLocalChecked());
}


NAN_METHOD(GetHomeDirectory) {
NanScope();

/* Only support looking up the current user for Windows compatibility
if (!args[0]->IsString()) {
NanThrowTypeError("username must be string");
NanReturnUndefined();
}
NanUtf8String arg0(args[0]);
char* name = *arg0;
*/

void GetHomeDirectory(const Nan::FunctionCallbackInfo<Value>& info) {
char* buf = (char*) malloc(sizeof(char) * BUFSIZ);

if (buf == NULL) {
NanThrowError("out of memory");
NanReturnUndefined();
Nan::ThrowError("out of memory");
return;
}

int r = getlogin_r(buf, BUFSIZ);

if (r != 0) {
strerror_r(r, buf, BUFSIZ);
NanThrowError(buf);
Nan::ThrowError(buf);
free(buf);
NanReturnUndefined();
return;
}

struct passwd* pw = getpwnam((const char*) buf);

if (pw == NULL) {
strerror_r(errno, buf, BUFSIZ);
NanThrowError(buf);
Nan::ThrowError(buf);
free(buf);
NanReturnUndefined();
return;
}

info.GetReturnValue().Set(Nan::New(pw->pw_dir).ToLocalChecked());
free(buf);
NanReturnValue(NanNew(pw->pw_dir));
}
10 changes: 5 additions & 5 deletions src/userinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using namespace v8;


void Init(Handle<Object> exports) {
void Init(Local<Object> exports) {
exports->Set(
NanNew("whoami"),
NanNew<FunctionTemplate>(WhoAmI)->GetFunction()
Nan::New("whoami").ToLocalChecked(),
Nan::New<FunctionTemplate>(WhoAmI)->GetFunction()
);

exports->Set(
NanNew("getHomeDirectory"),
NanNew<FunctionTemplate>(GetHomeDirectory)->GetFunction()
Nan::New("getHomeDirectory").ToLocalChecked(),
Nan::New<FunctionTemplate>(GetHomeDirectory)->GetFunction()
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/userinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using namespace v8;


NAN_METHOD(WhoAmI);
NAN_METHOD(GetHomeDirectory);
void Init(Handle<Object> exports);
void WhoAmI(const Nan::FunctionCallbackInfo<Value>& info);
void GetHomeDirectory(const Nan::FunctionCallbackInfo<Value>& info);
void Init(Local<Object> exports);


#endif
25 changes: 11 additions & 14 deletions src/win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
using namespace v8;


NAN_METHOD(WhoAmI) {
NanScope();
void WhoAmI(const Nan::FunctionCallbackInfo<Value>& info) {
DWORD size = UNLEN + 1;
char buf[UNLEN + 1];
DWORD r = GetUserName(buf, &size);
Expand All @@ -22,17 +21,15 @@ NAN_METHOD(WhoAmI) {
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, UNLEN + 1, NULL);
NanThrowError(buf);
NanReturnUndefined();
Nan::ThrowError(buf);
return;
}

NanReturnValue(NanNew(buf));
info.GetReturnValue().Set(Nan::New(buf).ToLocalChecked());
}


NAN_METHOD(GetHomeDirectory) {
NanScope();

void GetHomeDirectory(const Nan::FunctionCallbackInfo<Value>& info) {
wchar_t* path;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Profile, 0, NULL, &path);

Expand All @@ -41,20 +38,20 @@ NAN_METHOD(GetHomeDirectory) {
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, UNLEN + 1, NULL);
NanThrowError(buf);
NanReturnUndefined();
Nan::ThrowError(buf);
return;
}

size_t len = wcslen((const wchar_t*) path);
char* p = (char*) malloc(sizeof(char) * len + 1);

if (p == NULL) {
NanThrowError("out of memory");
NanReturnUndefined();
Nan::ThrowError("out of memory");
return;
}

int r = wcstombs(p, path, len);
size_t r = wcstombs(p, path, len);
p[r] = '\0';
CoTaskMemFree(path);
NanReturnValue(NanNew(p));
info.GetReturnValue().Set(Nan::New(p).ToLocalChecked());
}
40 changes: 7 additions & 33 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ var expect = Code.expect;
var describe = lab.describe;
var it = lab.it;

describe('UserInfo', function() {
describe('whoami()', function() {
it('returns the current username', function(done) {
describe('UserInfo', function () {
describe('whoami()', function () {
it('returns the current username', function (done) {
var user;

expect(function() {
expect(function () {
user = UserInfo.whoami();
}).not.to.throw();

Expand All @@ -23,9 +23,9 @@ describe('UserInfo', function() {
});
});

describe('getHomeDirectory()', function() {
it('returns the current user\'s home directory', function(done) {
expect(function() {
describe('getHomeDirectory()', function () {
it('returns the current user\'s home directory', function (done) {
expect(function () {
var home = UserInfo.getHomeDirectory();

expect(home).to.be.a.string();
Expand All @@ -34,31 +34,5 @@ describe('UserInfo', function() {

done();
});

/* Currently not supporting passing in a username because of Windows
it('throws if username is not a string', function(done) {
expect(function() {
UserInfo.getHomeDirectory();
}).to.throw(TypeError);
expect(function() {
UserInfo.getHomeDirectory(1);
}).to.throw(TypeError);
expect(function() {
UserInfo.getHomeDirectory(true);
}).to.throw(TypeError);
expect(function() {
UserInfo.getHomeDirectory({});
}).to.throw(TypeError);
expect(function() {
UserInfo.getHomeDirectory(null);
}).to.throw(TypeError);
done();
});
*/
});
});

0 comments on commit b954acf

Please sign in to comment.