using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.AspNetCore.NodeServices.HostingModels;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.NodeServices
{
///
/// Describes options used to configure an instance.
///
public class NodeServicesOptions
{
internal const string TimeoutConfigPropertyName = nameof(InvocationTimeoutMilliseconds);
private const int DefaultInvocationTimeoutMilliseconds = 60 * 1000;
private const string LogCategoryName = "Microsoft.AspNetCore.NodeServices";
private static readonly string[] DefaultWatchFileExtensions = { ".js", ".jsx", ".ts", ".tsx", ".json", ".html" };
///
/// Creates a new instance of .
///
/// The .
public NodeServicesOptions(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof (serviceProvider));
}
EnvironmentVariables = new Dictionary();
InvocationTimeoutMilliseconds = DefaultInvocationTimeoutMilliseconds;
WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone();
var hostEnv = serviceProvider.GetService();
if (hostEnv != null)
{
// In an ASP.NET environment, we can use the IHostingEnvironment data to auto-populate a few
// things that you'd otherwise have to specify manually
ProjectPath = hostEnv.ContentRootPath;
EnvironmentVariables["NODE_ENV"] = hostEnv.IsDevelopment() ? "development" : "production"; // De-facto standard values for Node
}
else
{
ProjectPath = Directory.GetCurrentDirectory();
}
var applicationLifetime = serviceProvider.GetService();
if (applicationLifetime != null)
{
ApplicationStoppingToken = applicationLifetime.ApplicationStopping;
}
// If the DI system gives us a logger, use it. Otherwise, set up a default one.
var loggerFactory = serviceProvider.GetService();
NodeInstanceOutputLogger = loggerFactory != null
? loggerFactory.CreateLogger(LogCategoryName)
: NullLogger.Instance;
// By default, we use this package's built-in out-of-process-via-HTTP hosting/transport
this.UseHttpHosting();
}
///
/// Specifies how to construct Node.js instances. An encapsulates all details about
/// how Node.js instances are launched and communicated with. A new will be created
/// automatically if the previous instance has terminated (e.g., because a source file changed).
///
public Func NodeInstanceFactory { get; set; }
///
/// If set, overrides the path to the root of your application. This path is used when locating Node.js modules relative to your project.
///
public string ProjectPath { get; set; }
///
/// If set, the Node.js instance should restart when any matching file on disk within your project changes.
///
public string[] WatchFileExtensions { get; set; }
///
/// The Node.js instance's stdout/stderr will be redirected to this .
///
public ILogger NodeInstanceOutputLogger { get; set; }
///
/// If true, the Node.js instance will accept incoming V8 debugger connections (e.g., from node-inspector).
///
public bool LaunchWithDebugging { get; set; }
///
/// If is true, the Node.js instance will listen for V8 debugger connections on this port.
///
public int DebuggingPort { get; set; }
///
/// If set, starts the Node.js instance with the specified environment variables.
///
public IDictionary EnvironmentVariables { get; set; }
///
/// Specifies the maximum duration, in milliseconds, that your .NET code should wait for Node.js RPC calls to return.
///
public int InvocationTimeoutMilliseconds { get; set; }
///
/// A token that indicates when the host application is stopping.
///
public CancellationToken ApplicationStoppingToken { get; set; }
}
}