@@ -109,6 +109,27 @@ 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+ std::string dir;
117+
118+ // Try TMPDIR, TMP, and TEMP in that order.
119+ if (!SafeGetenv (" TMPDIR" , &dir, env->env_vars ()) &&
120+ !SafeGetenv (" TMP" , &dir, env->env_vars ()) &&
121+ !SafeGetenv (" TEMP" , &dir, env->env_vars ())) {
122+ return ;
123+ }
124+
125+ if (dir.size () > 1 && dir.ends_with (" /" )) {
126+ dir.pop_back ();
127+ }
128+
129+ args.GetReturnValue ().Set (
130+ ToV8Value (isolate->GetCurrentContext (), dir).ToLocalChecked ());
131+ }
132+
112133#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
113134
114135static const uid_t uid_not_found = static_cast <uid_t >(-1 );
@@ -456,6 +477,7 @@ static void InitGroups(const FunctionCallbackInfo<Value>& args) {
456477
457478void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
458479 registry->Register (SafeGetenv);
480+ registry->Register (GetTempDir);
459481
460482#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
461483 registry->Register (GetUid);
@@ -478,6 +500,7 @@ static void Initialize(Local<Object> target,
478500 Local<Context> context,
479501 void * priv) {
480502 SetMethod (context, target, " safeGetenv" , SafeGetenv);
503+ SetMethod (context, target, " getTempDir" , GetTempDir);
481504
482505#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
483506 Environment* env = Environment::GetCurrent (context);
0 commit comments