Exemple #1
0
        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 utf8 = new UTF8Encoding(false /* no BOM */);

            _process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = "node",
                    Arguments              = $"--max-old-space-size=4096 {runtimePath}",
                    RedirectStandardInput  = true,
                    StandardInputEncoding  = utf8,
                    RedirectStandardOutput = true,
                    StandardOutputEncoding = utf8,
                    RedirectStandardError  = true,
                    StandardErrorEncoding  = utf8
                }
            };

            var assemblyVersion = GetAssemblyFileVersion();

            _process.StartInfo.EnvironmentVariables.Add(JsiiAgent,
                                                        string.Format(CultureInfo.InvariantCulture, JsiiAgentVersionString, Environment.Version,
                                                                      assemblyVersion.Item1, assemblyVersion.Item2));

            var debug = Environment.GetEnvironmentVariable(JsiiDebug);

            if (!string.IsNullOrWhiteSpace(debug) && !_process.StartInfo.EnvironmentVariables.ContainsKey(JsiiDebug))
            {
                _process.StartInfo.EnvironmentVariables.Add(JsiiDebug, debug);
            }

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

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

            _process.Start();
        }
Exemple #2
0
        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;
            }

            _process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = "node",
                    Arguments              = "--max-old-space-size=4096 " + runtimePath,
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                }
            };

            var assemblyVersion = GetAssemblyFileVersion();

            _process.StartInfo.EnvironmentVariables.Add(JsiiAgent,
                                                        string.Format(JsiiAgentVersionString, Environment.Version, assemblyVersion.Item1, assemblyVersion.Item2));

            var debug = Environment.GetEnvironmentVariable(JsiiDebug);

            if (!string.IsNullOrWhiteSpace(debug) && !_process.StartInfo.EnvironmentVariables.ContainsKey(JsiiDebug))
            {
                _process.StartInfo.EnvironmentVariables.Add(JsiiDebug, debug);
            }

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

            _process.Start();
        }
Exemple #3
0
        public NodeProcess(IJsiiRuntimeProvider jsiiRuntimeProvider, ILoggerFactory loggerFactory)
        {
            loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _logger       = loggerFactory.CreateLogger <NodeProcess>();

            _process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = "node",
                    Arguments              = "--max-old-space-size=4096 " + jsiiRuntimeProvider.JsiiRuntimePath,
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                }
            };

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

            _process.Start();
        }
Exemple #4
0
        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 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);
                                    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);
                                }
                            }
                        }
            }
        }