@@ -109,6 +109,29 @@ static void SafeGetenv(const FunctionCallbackInfo<Value>& args) {
109109 args.GetReturnValue ().Set (result);
110110}
111111
112+ static void GetTempDir (const FunctionCallbackInfo<Value>& args) {
113+ Environment* env = Environment::GetCurrent (args);
114+ Isolate* isolate = env->isolate ();
115+
116+ // safeGetenv('TMPDIR') ||
117+ // safeGetenv('TMP') ||
118+ // safeGetenv('TEMP') ||
119+ std::string dir;
120+ if (!SafeGetenv (" TMPDIR" , &dir, env->env_vars ()) &&
121+ !SafeGetenv (" TMP" , &dir, env->env_vars ()) &&
122+ !SafeGetenv (" TEMP" , &dir, env->env_vars ())) {
123+ dir = " /tmp" ;
124+ }
125+
126+ if (dir.size () > 1 && dir.ends_with (" /" )) {
127+ dir.pop_back ();
128+ }
129+
130+ Local<Value> result =
131+ ToV8Value (isolate->GetCurrentContext (), dir).ToLocalChecked ();
132+ args.GetReturnValue ().Set (result);
133+ }
134+
112135#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
113136
114137static const uid_t uid_not_found = static_cast <uid_t >(-1 );
@@ -456,6 +479,7 @@ static void InitGroups(const FunctionCallbackInfo<Value>& args) {
456479
457480void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
458481 registry->Register (SafeGetenv);
482+ registry->Register (GetTempDir);
459483
460484#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
461485 registry->Register (GetUid);
@@ -478,6 +502,7 @@ static void Initialize(Local<Object> target,
478502 Local<Context> context,
479503 void * priv) {
480504 SetMethod (context, target, " safeGetenv" , SafeGetenv);
505+ SetMethod (context, target, " getTempDir" , GetTempDir);
481506
482507#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
483508 Environment* env = Environment::GetCurrent (context);
0 commit comments