<commit_before>using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.NodeServices {
    /**
     * Class responsible for launching the Node child process, determining when it is ready to accept invocations,
     * and finally killing it when the parent process exits. Also it restarts the child process if it dies.
     */
    public abstract class OutOfProcessNodeInstance : INodeServices {
        private object _childProcessLauncherLock;
        private bool disposed;
        private StringAsTempFile _entryPointScript;
        private string _projectPath;
        private string _commandLineArguments;
        private Process _nodeProcess;
        private TaskCompletionSource<bool> _nodeProcessIsReadySource;

        protected Process NodeProcess {
            get {
                // This is only exposed to support the unreliable OutOfProcessNodeRunner, which is just to verify that
                // other hosting/transport mechanisms are possible. This shouldn't really be exposed.
                return this._nodeProcess;
            }
        }

        public OutOfProcessNodeInstance(string entryPointScript, string projectPath, string commandLineArguments = null)
        {
            this._childProcessLauncherLock = new object();
            this._entryPointScript = new StringAsTempFile(entryPointScript);
            this._projectPath = projectPath;
            this._commandLineArguments = commandLineArguments ?? string.Empty;
        }

        public abstract Task<T> Invoke<T>(NodeInvocationInfo invocationInfo);

        public Task<T> Invoke<T>(string moduleName, params object[] args) {
            return this.InvokeExport<T>(moduleName, null, args);
        }

        public async Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args) {
            return await this.Invoke<T>(new NodeInvocationInfo {
                ModuleName = moduleName,
                ExportedFunctionName = exportedFunctionName,
                Args = args
            });
        }

        protected async Task EnsureReady() {
            lock (this._childProcessLauncherLock) {
                if (this._nodeProcess == null || this._nodeProcess.HasExited) {
                    var startInfo = new ProcessStartInfo("node") {
                        Arguments = "\"" + this._entryPointScript.FileName + "\" " + this._commandLineArguments,
                        UseShellExecute = false,
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        WorkingDirectory = this._projectPath
                    };

                    // Append projectPath to NODE_PATH so it can locate node_modules
                    var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
                    if (existingNodePath != string.Empty) {
                        existingNodePath += ":";
                    }

                    var nodePathValue = existingNodePath + Path.Combine(this._projectPath, "node_modules");
                    #if NET45
                    startInfo.EnvironmentVariables.Add("NODE_PATH", nodePathValue);
                    #else
                    startInfo.Environment.Add("NODE_PATH", nodePathValue);
                    #endif

                    this.OnBeforeLaunchProcess();
                    this._nodeProcess = Process.Start(startInfo);<commit_msg>Setting nodepath correctly. Resolves #4<commit_after>using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.NodeServices {
    /**
     * Class responsible for launching the Node child process, determining when it is ready to accept invocations,
     * and finally killing it when the parent process exits. Also it restarts the child process if it dies.
     */
    public abstract class OutOfProcessNodeInstance : INodeServices {
        private object _childProcessLauncherLock;
        private bool disposed;
        private StringAsTempFile _entryPointScript;
        private string _projectPath;
        private string _commandLineArguments;
        private Process _nodeProcess;
        private TaskCompletionSource<bool> _nodeProcessIsReadySource;

        protected Process NodeProcess {
            get {
                // This is only exposed to support the unreliable OutOfProcessNodeRunner, which is just to verify that
                // other hosting/transport mechanisms are possible. This shouldn't really be exposed.
                return this._nodeProcess;
            }
        }

        public OutOfProcessNodeInstance(string entryPointScript, string projectPath, string commandLineArguments = null)
        {
            this._childProcessLauncherLock = new object();
            this._entryPointScript = new StringAsTempFile(entryPointScript);
            this._projectPath = projectPath;
            this._commandLineArguments = commandLineArguments ?? string.Empty;
        }

        public abstract Task<T> Invoke<T>(NodeInvocationInfo invocationInfo);

        public Task<T> Invoke<T>(string moduleName, params object[] args) {
            return this.InvokeExport<T>(moduleName, null, args);
        }

        public async Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args) {
            return await this.Invoke<T>(new NodeInvocationInfo {
                ModuleName = moduleName,
                ExportedFunctionName = exportedFunctionName,
                Args = args
            });
        }

        protected async Task EnsureReady() {
            lock (this._childProcessLauncherLock) {
                if (this._nodeProcess == null || this._nodeProcess.HasExited) {
                    var startInfo = new ProcessStartInfo("node") {
                        Arguments = "\"" + this._entryPointScript.FileName + "\" " + this._commandLineArguments,
                        UseShellExecute = false,
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        WorkingDirectory = this._projectPath
                    };

                    // Append projectPath to NODE_PATH so it can locate node_modules
                    var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
                    if (existingNodePath != string.Empty) {
                        existingNodePath += ":";
                    }

                    var nodePathValue = existingNodePath + Path.Combine(this._projectPath, "node_modules");
                    #if NET45
                    startInfo.EnvironmentVariables["NODE_PATH"] = nodePathValue;
                    #else
                    startInfo.Environment["NODE_PATH"] = nodePathValue;
                    #endif

                    this.OnBeforeLaunchProcess();
                    this._nodeProcess = Process.Start(startInfo);
{'fraction_non_alphanumeric': 0.06812447434819176, 'fraction_numerical': 0.0007008690776562938, 'mean_word_length': 2.0105485232067513, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5015297', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1533, 'n_words': 435}