@@ -109,6 +109,31 @@ static void SafeGetenv(const FunctionCallbackInfo<Value>& args) {
109
109
args.GetReturnValue ().Set (result);
110
110
}
111
111
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
+ // Let's wrap SafeGetEnv since it returns true for empty string.
119
+ auto get_env = [&dir, &env](std::string_view key) {
120
+ USE (SafeGetenv (key.data (), &dir, env->env_vars ()));
121
+ return !dir.empty ();
122
+ };
123
+
124
+ // Try TMPDIR, TMP, and TEMP in that order.
125
+ if (!get_env (" TMPDIR" ) && !get_env (" TMP" ) && !get_env (" TEMP" )) {
126
+ return ;
127
+ }
128
+
129
+ if (dir.size () > 1 && dir.ends_with (" /" )) {
130
+ dir.pop_back ();
131
+ }
132
+
133
+ args.GetReturnValue ().Set (
134
+ ToV8Value (isolate->GetCurrentContext (), dir).ToLocalChecked ());
135
+ }
136
+
112
137
#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
113
138
114
139
static const uid_t uid_not_found = static_cast <uid_t >(-1 );
@@ -456,6 +481,7 @@ static void InitGroups(const FunctionCallbackInfo<Value>& args) {
456
481
457
482
void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
458
483
registry->Register (SafeGetenv);
484
+ registry->Register (GetTempDir);
459
485
460
486
#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
461
487
registry->Register (GetUid);
@@ -478,6 +504,7 @@ static void Initialize(Local<Object> target,
478
504
Local<Context> context,
479
505
void * priv) {
480
506
SetMethod (context, target, " safeGetenv" , SafeGetenv);
507
+ SetMethod (context, target, " getTempDir" , GetTempDir);
481
508
482
509
#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
483
510
Environment* env = Environment::GetCurrent (context);
0 commit comments