Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/node_task_runner.h
1 #ifndef SRC_NODE_TASK_RUNNER_H_ 2 #define SRC_NODE_TASK_RUNNER_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "node_internals.h" 7 #include "simdjson.h" 8 #include "spawn_sync.h" 9 #include "uv.h" 10 11 #include <filesystem> 12 #include <optional> 13 #include <string_view> 14 #include <tuple> 15 16 namespace node::task_runner { 17 18 using PositionalArgs = std::vector<std::string_view>; 19 20 // ProcessRunner is the class responsible for running a process. 21 // A class instance is created for each process to be run. 22 // The class is responsible for spawning the process and handling its exit. 23 // The class also handles the environment variables and arguments. 24 class ProcessRunner { 25 public: 26 ProcessRunner(std::shared_ptr<InitializationResultImpl> result, 27 const std::filesystem::path& package_json_path, 28 std::string_view script_name, 29 std::string_view command, 30 std::string_view path_env_var, 31 const PositionalArgs& positional_args); 32 void Run(); 33 static void ExitCallback(uv_process_t* req, 34 int64_t exit_status, 35 int term_signal); 36 37 private: 38 uv_loop_t* loop_ = uv_default_loop(); 39 uv_process_t process_{}; 40 uv_process_options_t options_{}; 41 uv_stdio_container_t child_stdio_[3]{}; 42 std::shared_ptr<InitializationResultImpl> init_result_; 43 std::vector<std::string> command_args_{}; 44 std::vector<std::string> env_vars_{}; 45 std::unique_ptr<char* []> env_ {}; // memory for options_.env 46 std::unique_ptr<char* []> arg_ {}; // memory for options_.args 47 std::string cwd_; 48 49 // OnExit is the callback function that is called when the process exits. 50 void OnExit(int64_t exit_status, int term_signal); 51 void SetEnvironmentVariables(); 52 53 #ifdef _WIN32 54 std::string file_ = "cmd.exe"; 55 #else 56 std::string file_ = "/bin/sh"; 57 #endif // _WIN32 58 59 // Represents the absolute path to the package.json file. 60 std::filesystem::path package_json_path_; 61 // Represents the name of the script that is being run. 62 std::string script_name_; 63 // Represents PATH environment variable that contains 64 // all subdirectory paths appended with node_modules/.bin suffix. 65 std::string path_env_var_; 66 }; 67 68 // This function traverses up to the root directory. 69 // While traversing up, if it finds a package.json file, it reads its content. 70 // If it cannot find a package.json file, it returns std::nullopt. 71 // Otherwise, it returns a tuple of: 72 // - the path to the package.json file 73 // - package.json file content 74 // - `path_env_var` variable 75 // 76 // For example, on POSIX, it returns the following for `path_env_var`, 77 // if the current directory is `/anonrig`: 78 // `/anonrig/node_modules/.bin:/node_modules/.bin` 79 std::optional<std::tuple<std::filesystem::path, std::string, std::string>> 80 FindPackageJson(const std::filesystem::path& cwd); 81 82 void RunTask(const std::shared_ptr<InitializationResultImpl>& result, 83 std::string_view command_id, 84 const PositionalArgs& positional_args); 85 PositionalArgs GetPositionalArgs(const std::vector<std::string>& args); 86 std::string EscapeShell(std::string_view command); 87 88 } // namespace node::task_runner 89 90 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 91 92 #endif // SRC_NODE_TASK_RUNNER_H_