utils-homedir
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -7,2 +7,3 @@ 'use strict'; | ||
isWindows = require( 'check-if-windows' ), | ||
platform = require( 'utils-platform' ), | ||
os = require( 'os' ); | ||
@@ -17,15 +18,33 @@ | ||
* | ||
* @returns {String} directory | ||
* @returns {String|Null} directory | ||
*/ | ||
function homedir() { | ||
if ( isFunction( os.homedir ) ) { | ||
return os.homedir(); | ||
} | ||
// Node < v4 | ||
var env = process.env, | ||
home, | ||
user; | ||
if ( isWindows ) { | ||
// https://github.com/libuv/libuv/blob/764877fd9e4ea67c0cbe27bf81b2b294ed33b0f5/src/win/util.c#L1170 | ||
return process.env[ 'USERPROFILE' ]; | ||
// https://en.wikipedia.org/wiki/Environment_variable#Windows | ||
home = env[ 'USERPROFILE' ] || env[ 'HOMEDRIVE' ]+env[ 'HOMEPATH' ] || env[ 'HOME' ]; | ||
return ( home ) ? home : null; | ||
} | ||
// https://github.com/libuv/libuv/blob/9fbcca048181b927cfcdb5c6c49e5bdff173aad5/src/unix/core.c#L1030 | ||
return process.env[ 'HOME' ]; | ||
home = env[ 'HOME' ]; | ||
if ( home ) { | ||
return home; | ||
} | ||
// Get the current user account (https://docs.python.org/2/library/getpass.html): | ||
user = env[ 'LOGNAME' ] || env[ 'USER' ] || env[ 'LNAME' ] || env[ 'USERNAME' ]; | ||
// If on Mac OSX, use the Mac path convention (http://apple.stackexchange.com/questions/119230/what-is-standard-for-os-x-filesystem-e-g-opt-vs-usr)... | ||
if ( platform === 'darwin' ) { | ||
return ( user ) ? '/Users/'+user : null; | ||
} | ||
// Check if running as 'root' (https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard)... | ||
if ( process.getuid() === 0 ) { | ||
return '/root'; | ||
} | ||
// If on Linux, use the Linux path convention (https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard)... | ||
return ( user ) ? '/home/'+user : null; | ||
} // end FUNCTION homedir() | ||
@@ -36,2 +55,2 @@ | ||
module.exports = homedir; | ||
module.exports = isFunction( os.homedir ) ? os.homedir : homedir; |
{ | ||
"name": "utils-homedir", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Returns the current user's home directory.", | ||
@@ -51,2 +51,3 @@ "author": { | ||
"pkginfo": "^0.3.0", | ||
"utils-platform": "^1.0.0", | ||
"validate.io-function": "^1.0.2" | ||
@@ -53,0 +54,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
Home Directory | ||
Home | ||
=== | ||
@@ -27,6 +27,18 @@ [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependencies][dependencies-image]][dependencies-url] | ||
var home = homedir(); | ||
// returns <dirpath> | ||
// => e.g., /Users/<username> | ||
``` | ||
If unable to locate a `home` directory, the module returns `null`. | ||
``` javascript | ||
var home = homedir(); | ||
// returns null | ||
``` | ||
## Notes | ||
* This module primarily checks various [environment variables](https://en.wikipedia.org/wiki/Environment_variable) to locate a `home` directory. Note that this approach has __security vulnerabilities__, as attackers can tamper with [environment variables](https://en.wikipedia.org/wiki/Environment_variable). | ||
## Examples | ||
@@ -72,2 +84,3 @@ | ||
$ homedir | ||
# => e.g., /Users/<username> | ||
``` | ||
@@ -74,0 +87,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
8895
57
156
1
5
+ Addedutils-platform@^1.0.0