public NodeProcess()

in packages/@jsii/dotnet-runtime/src/Amazon.JSII.Runtime/Services/NodeProcess.cs [29:115]


        public NodeProcess(IJsiiRuntimeProvider jsiiRuntimeProvider, ILoggerFactory loggerFactory)
        {
            loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _logger = loggerFactory.CreateLogger<NodeProcess>();

            var runtimePath = Environment.GetEnvironmentVariable(JsiiRuntime);
            if (string.IsNullOrWhiteSpace(runtimePath))
                runtimePath = jsiiRuntimeProvider.JsiiRuntimePath;

            var node = Environment.GetEnvironmentVariable(JsiiNode);
            if (string.IsNullOrWhiteSpace(node))
                node = "node";

            var utf8 = new UTF8Encoding(false /* no BOM */);
            var startInfo = new ProcessStartInfo
            {
                FileName = node,
                Arguments = $"--max-old-space-size=4096 {runtimePath}",
                RedirectStandardInput = true,
                StandardInputEncoding = utf8,
                RedirectStandardOutput = true,
                StandardOutputEncoding = utf8,
                RedirectStandardError = true,
                StandardErrorEncoding = utf8,
                UseShellExecute = false,
            };

            var assemblyVersion = GetAssemblyFileVersion();
            startInfo.EnvironmentVariables.Add(JsiiAgent,
                string.Format(CultureInfo.InvariantCulture, JsiiAgentVersionString, Environment.Version,
                    assemblyVersion.Item1, assemblyVersion.Item2));

            var debug = Environment.GetEnvironmentVariable(JsiiDebug);
            if (!string.IsNullOrWhiteSpace(debug) && !startInfo.EnvironmentVariables.ContainsKey(JsiiDebug))
                startInfo.EnvironmentVariables.Add(JsiiDebug, debug);

            _logger.LogDebug("Starting jsii runtime...");
            _logger.LogDebug($"{startInfo.FileName} {startInfo.Arguments}");

            // Registering shutdown hook to have JS process gracefully terminate.
            AppDomain.CurrentDomain.ProcessExit += (snd, evt) => { ((IDisposable)this).Dispose(); };

            _process = Process.Start(startInfo)!;

            StandardInput = _process.StandardInput;
            StandardOutput = _process.StandardOutput;

            _stderrSink = new Thread(StderrSink);
            _stderrSink.Name = "NodeProcess.StderrSink";
            // Background threads don't prevent the VM from exiting
            _stderrSink.IsBackground = true;
            _stderrSink.Start();

            void StderrSink()
            {
                string? line;
                using (var standardError = _process.StandardError)
                using (Stream stderr = Console.OpenStandardError())
                using (Stream stdout = Console.OpenStandardOutput())
                {
                    while ((line = standardError.ReadLine()) != null)
                    {
                        try
                        {
                            var entry = JsonConvert.DeserializeObject<ConsoleEntry>(line)
                                        ?? throw new Exception("Invalid JSON message");
                            if (entry.Stderr != null)
                            {
                                byte[] buffer = Convert.FromBase64String(entry.Stderr);
                                stderr.Write(buffer, 0, buffer.Length);
                            }

                            if (entry.Stdout != null)
                            {
                                byte[] buffer = Convert.FromBase64String(entry.Stdout);
                                stdout.Write(buffer, 0, buffer.Length);
                            }
                        }
                        catch
                        {
                            // Could not parse line - so just coying to stderr
                            Console.Error.WriteLine(line);
                        }
                    }
                }
            }
        }