Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            var container = new Container();

            container.Configure(config => {
                config.Scan(_ => {
                    _.AssembliesFromApplicationBaseDirectory();
                    _.RegisterConcreteTypesAgainstTheFirstInterface();
                    _.WithDefaultConventions();
                });
            });



            var services = new ServiceCollection();

            var serviceProvider = services.BuildServiceProvider();
            var options         = new NodeServicesOptions(serviceProvider);

            var assembly  = typeof(Program).GetTypeInfo().Assembly;
            var directory = Path.GetDirectoryName(assembly.Location).Replace("/bin/Debug/netcoreapp1.1", string.Empty);

            options.ProjectPath = directory;
            var nodeServices = NodeServicesFactory.CreateNodeServices(options);

            System.Console.WriteLine("Invoking");
            nodeServices.InvokeExportAsync <int>("./test", "addNumbers", 1, 2).ContinueWith(r => {
                System.Console.WriteLine($"Result is {r.Result}");
            });

            //var orchestrator = container.GetInstance<DeploymentOrchestrator>();

            System.Console.WriteLine("Press any key");
            System.Console.ReadLine();
        }
        /// <summary>
        /// Adds HTTPS support for <c>NodeServices</c>.
        /// </summary>
        /// <param name="options"><c>NodeServices</c> options.</param>
        /// <param name="configuration">Application configuration.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static NodeServicesOptions AddHttps(
            this NodeServicesOptions options,
            IConfiguration configuration
            )
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            options.EnvironmentVariables.Add("HttpsToHttpUrlTransform", "true");

            var serverUrls = GetServerUrls(configuration);

            var serverHttpsUrlsJson = JsonSerializer.Serialize(serverUrls.HttpsUrls);
            var serverHttpUrlsJson  = JsonSerializer.Serialize(serverUrls.HttpUrls);

            options.EnvironmentVariables.Add("ServerHttpsUrls", serverHttpsUrlsJson);
            options.EnvironmentVariables.Add("ServerHttpUrls", serverHttpUrlsJson);

            return(options);
        }
Ejemplo n.º 3
0
        public async Task BuiltInModuleResolved()
        {
            var stopTokenSource = new CancellationTokenSource();
            var options         = new NodeServicesOptions
            {
                ApplicationStoppingToken = stopTokenSource.Token,
                ProjectPath = AppDomain.CurrentDomain.BaseDirectory,
                NodeInstanceOutputLogger = Mock.Of <ILogger>(),
            };

            options.UseSocketHosting();

            using (var services = NodeServicesFactory.CreateNodeServices(options))
                using (var builtinModule = new StringAsTempFile("exports.add = function(a, b) {return a+b;}", stopTokenSource.Token))
                {
                    await services.InvokeExportAsync <object>("builtin-modules-init", "initialize",
                                                              new Dictionary <string, string>()
                    {
                        { "builtin", builtinModule.FileName }
                    });

                    var result = await services.InvokeExportAsync <int>("builtin-modules-test", "test", 1, 2);

                    stopTokenSource.Cancel();

                    Assert.Equal(3, result);
                }
        }
 private async Task CreateNodeInstances(NodeServicesOptions options)
 {
     for (int i = 0; i < _services.Length; ++i)
     {
         _services[i] = new HotNodeService(CreateNodeInstance(options));
         await WarmupService(_services[i], CancellationToken.None);
     }
 }
Ejemplo n.º 5
0
        private static NodeServicesOptions DefaultNodeServicesOptions()
        {
            var options = new NodeServicesOptions(new ServiceCollection().BuildServiceProvider())
            {
                ProjectPath = Directory.GetCurrentDirectory()
            };

            return(options);
        }
Ejemplo n.º 6
0
        public NodeServicesEngine()
        {
            var options = new NodeServicesOptions(new EmptyServiceProvider())
            {
                EnvironmentVariables = { ["NODE_ENV"] = "production" }
            };

            _nodeServices = NodeServicesFactory.CreateNodeServices(options);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Create an <see cref="INodeServices" /> instance according to the supplied options.
        /// </summary>
        /// <param name="options">Options for creating the <see cref="INodeServices" /> instance.</param>
        /// <returns>An <see cref="INodeServices" /> instance.</returns>
        public static INodeServices CreateNodeServices(NodeServicesOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(new NodeServicesImpl(options.NodeInstanceFactory));
        }
Ejemplo n.º 8
0
        public HandlerFactoryFixture()
        {
            var options = new NodeServicesOptions(new ServiceCollection().BuildServiceProvider())
            {
                ProjectPath = Directory.GetCurrentDirectory()
            };
            var nodeServices = NodeServicesFactory.CreateNodeServices(options);

            Factory = new HandlerFactory(nodeServices, typeof(string).GetTypeInfo().Assembly); // mscorlib
        }
Ejemplo n.º 9
0
        public NodeServicesPool(int serviceCount, IServiceProvider sp)
        {
            _serviceBag = new ConcurrentBag <INodeServices>();
            var options = new NodeServicesOptions(sp);

            for (int i = 0; i < serviceCount; i++)
            {
                _serviceBag.Add(NodeServicesFactory.CreateNodeServices(options));
            }
            _semaphore = new SemaphoreSlim(serviceCount);
        }
        public static void AddDebugNodeServices(this IServiceCollection services, int startPort = 9229)
        {
            var port = startPort;

            services.AddTransient <INodeServices>(serviceProvider => {
                var options = new NodeServicesOptions(serviceProvider);
                options.LaunchWithDebugging = true;
                options.DebuggingPort       = port;
                port++;
                return(NodeServicesFactory.CreateNodeServices(options));
            });
        }
Ejemplo n.º 11
0
        public Visualizer(string baseDir)
        {
            _baseDir = baseDir;
            var services = new ServiceCollection();

            services.AddNodeServices();
            var options = new NodeServicesOptions(services.BuildServiceProvider())
            {
                ProjectPath = Directory.GetCurrentDirectory()
            };

            _node = NodeServicesFactory.CreateNodeServices(options);
        }
        private static INodeServices CreateNodeServicesInstance(
            IApplicationBuilder appBuilder, string sourcePath)
        {
            // Unlike other consumers of NodeServices, AngularCliMiddleware dosen't share Node instances, nor does it
            // use your DI configuration. It's important for AngularCliMiddleware to have its own private Node instance
            // because it must *not* restart when files change (it's designed to watch for changes and rebuild).
            var nodeServicesOptions = new NodeServicesOptions(appBuilder.ApplicationServices)
            {
                WatchFileExtensions = new string[] { }, // Don't watch anything
                ProjectPath         = Path.Combine(Directory.GetCurrentDirectory(), sourcePath),
            };

            return(NodeServicesFactory.CreateNodeServices(nodeServicesOptions));
        }
Ejemplo n.º 13
0
        public HandlerFactoryFixture()
        {
            var resolvers = new List <IMetadataReferenceResolver> {
                new DefaultMetadataReferenceResolver()
            };
            var builder = new DefaultAssemblyBuilder(new DefaultAssemblyLoadContextProvider(), resolvers);

            var options = new NodeServicesOptions(new ServiceCollection().BuildServiceProvider())
            {
                ProjectPath = Directory.GetCurrentDirectory()
            };
            var nodeServices = NodeServicesFactory.CreateNodeServices(options);

            Factory = new HandlerFactory(builder, nodeServices, typeof(string).GetTypeInfo().Assembly);             // mscorlib
        }
Ejemplo n.º 14
0
 public HttpNodeInstance(NodeServicesOptions options, int port = 0)
     : base(
         EmbeddedResourceReader.Read(typeof(OutOfProcessNodeInstance), "/Content/Node/entrypoint-http.js"),
         options.ProjectPath,
         options.WatchFileExtensions,
         MakeCommandLineOptions(port),
         options.ApplicationStoppingToken,
         options.NodeInstanceOutputLogger,
         options.EnvironmentVariables,
         options.InvocationTimeoutMilliseconds,
         options.LaunchWithDebugging,
         options.DebuggingPort)
 {
     _client         = new HttpClient();
     _client.Timeout = TimeSpan.FromMilliseconds(options.InvocationTimeoutMilliseconds + 1000);
 }
Ejemplo n.º 15
0
        public static void Validate(string typescriptSource)
        {
            if (s_nodeServices == null)
            {
                var options = new NodeServicesOptions(NullServiceProvider.Instance);
                s_nodeServices = NodeServicesFactory.CreateNodeServices(options);
            }

            var task   = s_nodeServices.InvokeAsync <string>("TypeScriptValidator.js", typescriptSource);
            var result = task.GetAwaiter().GetResult();

            if (result != "success")
            {
                Assert.Fail("\n" + typescriptSource + "\n\nErrors:\n" + result);
            }
        }
Ejemplo n.º 16
0
 public SocketNodeInstance(NodeServicesOptions options, string socketAddress)
     : base(
         EmbeddedResourceReader.Read(
             typeof(SocketNodeInstance),
             "/Content/Node/entrypoint-socket.js"),
         options.ProjectPath,
         options.WatchFileExtensions,
         MakeNewCommandLineOptions(socketAddress),
         options.ApplicationStoppingToken,
         options.NodeInstanceOutputLogger,
         options.EnvironmentVariables,
         options.InvocationTimeoutMilliseconds,
         options.LaunchWithDebugging,
         options.DebuggingPort)
 {
     _socketAddress = socketAddress;
 }
        /// <summary>
        /// Adds NodeServices support to the <paramref name="serviceCollection"/>.
        /// </summary>
        /// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
        /// <param name="setupAction">A callback that will be invoked to populate the <see cref="NodeServicesOptions"/>.</param>
        public static void AddNodeServices(this IServiceCollection serviceCollection, Action <NodeServicesOptions> setupAction)
        {
            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider =>
            {
                // First we let NodeServicesOptions take its defaults from the IServiceProvider,
                // then we let the developer override those options
                var options = new NodeServicesOptions(serviceProvider);
                setupAction(options);

                return(NodeServicesFactory.CreateNodeServices(options));
            });
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="NodeRenderEngine" /> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="options">The options.</param>
        public NodeRenderEngine(IServiceProvider serviceProvider, NodeRenderEngineOptions options)
        {
            _options = options;

            var nodeOptions = new NodeServicesOptions(serviceProvider)
            {
                HostingModel        = options.NodeHostingModel,
                ProjectPath         = options.ProjectDirectory,
                WatchFileExtensions = options.WatchFileExtensions
            };

            if (options.NodeInstanceOutputLogger != null)
            {
                nodeOptions.NodeInstanceOutputLogger = options.NodeInstanceOutputLogger;
            }

            _nodeServices = NodeServicesFactory.CreateNodeServices(nodeOptions);
        }
        public static IApplicationBuilder UseParcelBundler(this IApplicationBuilder app, ParcelBundlerOptions options)
        {
            var environment = app.ApplicationServices.GetService <IHostingEnvironment>();

            var nodeServiceOptions = new NodeServicesOptions(app.ApplicationServices)
            {
                WatchFileExtensions = new string[] { }
            };

            string content = LoadScriptContent();
            var    script  = new StringAsTempFile(content, nodeServiceOptions.ApplicationStoppingToken);

            var node = NodeServicesFactory.CreateNodeServices(nodeServiceOptions);

            var args = new
            {
                entryPoints = options.EntryPoints,
                options     = new
                {
                    outDir         = Path.Combine(environment.WebRootPath, options.OutDir ?? ""),
                    outFile        = options.OutFile,
                    publicUrl      = options.PublicUrl,
                    watch          = options.Watch,
                    cache          = options.Cache,
                    cacheDir       = options.CacheDir,
                    contentHash    = options.ContentHash,
                    minify         = options.Minify,
                    scopeHoist     = options.ScopeHoist,
                    target         = options.Target?.ToString().ToLower(),
                    https          = options.Https?.ToParcelOptions(),
                    hmr            = options.HotModuleReload?.IsEnabled,
                    hmrPort        = options.HotModuleReload?.Port,
                    sourcemaps     = options.SourceMaps,
                    hmrHostname    = options.HotModuleReload?.Hostname,
                    detailedReport = options.DetailedReport
                }
            };

            var response = node.InvokeExportAsync <ParcelServerInfo>(script.FileName, "runParcelBundler", JsonConvert.SerializeObject(args, SerializerSettings))
                           .Result;

            return(app);
        }
Ejemplo n.º 20
0
        public PugRenderingTestsFixture()
        {
            var optionsMock = new Mock <IOptions <PugzorViewEngineOptions> >();

            optionsMock.SetupGet(q => q.Value)
            .Returns(new PugzorViewEngineOptions
            {
                // true: makes a lot of tests to fail
                Pretty = false
            });

            var mockServices       = new Mock <IServiceProvider>();
            var nodeServiceOptions = new NodeServicesOptions(mockServices.Object)
            {
                ProjectPath = TemporaryDirectoryHelper.CreateTemporaryDirectory(true)
            };
            var nodeServices = NodeServicesFactory.CreateNodeServices(nodeServiceOptions);

            Renderer = new PugRendering(nodeServices, optionsMock.Object);
        }
Ejemplo n.º 21
0
        public NodeBindingTests()
        {
            var options = new NodeServicesOptions
            {
                ApplicationStoppingToken = stopTokenSource.Token,
                ProjectPath = AppDomain.CurrentDomain.BaseDirectory,
                NodeInstanceOutputLogger = Mock.Of <ILogger>(),
                DebuggingPort            = 9000,
                LaunchWithDebugging      = true
            };

            options.UseSocketHosting();
            nodeServices = NodeServicesFactory.CreateNodeServices(options);

            rpcHost = new JsonRpcBindingHost(js => new LineDelimitedJsonConnection(js));
            rpcHost.Repository.AddBinding("test", new TestBound());

            var initTask = nodeServices.InvokeExportAsync <Stream>("binding-init", "initialize", rpcHost.Repository.Objects);

            this.initTask = initTask.ContinueWith(t => (rpcHost.Connection as LineDelimitedJsonConnection)?.Initialize(t.Result, t.Result));
        }
Ejemplo n.º 22
0
        public static async Task <TResult> RunAsync <TData, TResult>(string module, TData data)
        {
            var services = new ServiceCollection();

            services.AddNodeServices(a =>
            {
                // a.ProjectPath = Directory.GetCurrentDirectory();
                // Set any properties that you want on 'options' here
            });

            var serviceProvider = services.BuildServiceProvider();
            var options         = new NodeServicesOptions(serviceProvider)
            {
                ProjectPath = Directory.GetCurrentDirectory() + "/tmphost",
                InvocationTimeoutMilliseconds = 60000 * 5,
            };
            var nodeServices = NodeServicesFactory.CreateNodeServices(options);



            var sb = new StringBuilder();

            // sb.AppendLine("return function (data,callback){");
            sb.AppendLine("var requirejs = require(\"requirejs\");");
            sb.AppendLine($"var r = requirejs.config({{   baseUrl:'{Path.Combine(Directory.GetCurrentDirectory(), "artifacts/app").Replace("\\", "/")}'}});");

            sb.AppendLine("module.exports= function (callback,data){ try{");
            sb.AppendLine($"r([\"{module}\"], function (program) {{ program.default(data, callback); }},function(err){{ console.log('host failed'); callback(err,null) }})");



            sb.AppendLine("}catch(error){console.log('host catch');callback(error,null); }}");

            Directory.CreateDirectory("tmphost");
            File.WriteAllText("tmphost/run.js", sb.ToString());

            return(await nodeServices.InvokeAsync <TResult>("./run", data));

            //  return JToken.FromObject(await Edge.Func(sb.ToString())(data)).ToObject<TResult>();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="NodeRenderEngine" /> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="options">The options.</param>
        public NodeRenderEngine(IServiceProvider serviceProvider, NodeRenderEngineOptions options)
        {
            _options = options;

            var nodeOptions = new NodeServicesOptions(serviceProvider ?? new EmptyServiceProvider())
            {
                ProjectPath          = options.ProjectDirectory,
                WatchFileExtensions  = options.WatchFileExtensions,
                EnvironmentVariables = options.EnvironmentVariables,
                DebuggingPort        = options.DebuggingPort,
                LaunchWithDebugging  = options.LaunchWithDebugging
            };

            if (options.NodeInstanceFactory != null)
            {
                nodeOptions.NodeInstanceFactory = options.NodeInstanceFactory;
            }
            if (options.NodeInstanceOutputLogger != null)
            {
                nodeOptions.NodeInstanceOutputLogger = options.NodeInstanceOutputLogger;
            }

            _nodeServices = NodeServicesFactory.CreateNodeServices(nodeOptions);
        }
Ejemplo n.º 24
0
        private async Task <int> OnExecuteAsync(CommandLineApplication app)
        {
            if (string.IsNullOrEmpty(Url) && string.IsNullOrEmpty(RemotePageLocation) && !HelloWorld)
            {
                app.ShowHelp();
                return(0);
            }
            Console.WriteLine(HostFolder);

            Directory.CreateDirectory(HostFolder);
            Directory.CreateDirectory("output");
            await InstallNPMPackage(HostFolder, "requirejs");
            await  InstallNPMPackage(HostFolder, "puppeteer");
            await InstallNPMPackage(HostFolder, "websocket");
            await InstallNPMPackage(HostFolder, "isomorphic-fetch");
            await InstallNPMPackage(HostFolder, "earthml-temply-headless");


            var engine = new RazorLightEngineBuilder()
                         .UseEmbeddedResourcesProject(typeof(Program))
                         .UseMemoryCachingProvider()
                         .Build();

            Console.WriteLine(string.Join(" ", app.RemainingArguments));

            var args = new JObject();

            for (var j = 0; j < app.RemainingArguments.Count; j += 2)
            {
                args[app.RemainingArguments[j].Trim('-')] = app.RemainingArguments[j + 1];
            }

            IWebHost host = new WebHostBuilder()
                            .UseKestrel()

                            .Configure(appbuilder =>
            {
                appbuilder.Use(async(context, next) =>
                {
                    Console.WriteLine(context.Request.Path);

                    if (context.Request.Path == "/installDependencies")
                    {
                        var dps = JToken.Parse(await new StreamReader(context.Request.Body).ReadToEndAsync());
                        foreach (JProperty dependency in dps.SelectToken("$.dependencies"))
                        {
                            await InstallNPMPackage(HostFolder, dependency.Name, dependency.Value.ToString());
                        }

                        context.Response.StatusCode = 200;
                        return;
                    }

                    if (context.Request.Path == "/hello-world")
                    {
                        await context.Response.WriteAsync(@"<html><head><script src=""node_modules/requirejs/require.js""></script></head><body><script type=""text/javascript"">require.config({paths:{'earthml-temply-headless':'node_modules/earthml-temply-headless/artifacts/src'}}); require(['earthml-temply-headless/remotepage/remotepage'],(RemotePageModule)=>{console.log(RemotePageModule);let rp = new RemotePageModule.RemotePage(); rp.helloWorld(); })</script></body></html>");
                        return;
                    }

                    if (context.Request.Path == "/")
                    {
                        try
                        {
                            string result = await engine.CompileRenderAsync("GenericRemoteHost.cshtml", new { Name = "John Doe" });

                            await context.Response.WriteAsync(result);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                        return;
                    }


                    await next();

                    Console.WriteLine($"{context.Request.Path} {context.Response.StatusCode}");
                });
                foreach (var volumne in Volumes)
                {
                    appbuilder.Map(volumne.Split(':').Last(), (b) =>
                    {
                        b.Use(async(ctx, next) =>
                        {
                            await next();

                            Console.WriteLine($"{ctx.Request.Path} {ctx.Response.StatusCode}");
                        });
                        b.UseStaticFiles(new StaticFileOptions {
                            FileProvider = new PhysicalFileProvider(string.Join(":", volumne.Split(':').Reverse().Skip(1).Reverse()))
                        });
                    });
                }

                //appbuilder.Map("/appinsight", (b) =>
                //{
                //    b.UseStaticFiles(new StaticFileOptions { FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(@"C:\dev\EarthML\EarthML.TemplyHeadless\samples\ApplicationInsightsGraphGenerator\wwwroot") });

                //});

                //appbuilder.Map("/AscendRegistration", (b) =>
                //{
                //    b.UseStaticFiles(new StaticFileOptions { FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(@"C:\dev\Ascend\AscendRecording\AscendRecording\www\libs\AscendRegistration") });

                //});

                //appbuilder.Map("/node_modules/earthml-temply-headless/artifacts/src", (b) =>
                //{
                //    b.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider("C:/dev/EarthML/EarthML.TemplyHeadless/src/EarthML.TemplyHeadless/artifacts/src") });
                //});

                appbuilder.UseStaticFiles(new StaticFileOptions {
                    FileProvider = new PhysicalFileProvider(HostFolder)
                });
            })
                            .Build();


            await host.StartAsync();;

            if (HelloWorld)
            {
                Url = host.ServerFeatures.Get <IServerAddressesFeature>().Addresses
                      .Select(a => a.Replace("://+", "://localhost").Replace("[::]", "127.0.0.1")).FirstOrDefault() + "/hello-world";
            }

            if (!string.IsNullOrEmpty(RemotePageLocation))
            {
                Url = host.ServerFeatures.Get <IServerAddressesFeature>().Addresses
                      .Select(a => a.Replace("://+", "://localhost").Replace("[::]", "127.0.0.1")).FirstOrDefault() + $"?remote-main={RemotePageMain}&remote-location={WebUtility.UrlEncode(RemotePageLocation)}";
            }


            {
                var services = new ServiceCollection();
                services.AddNodeServices(a =>
                {
                    // a.ProjectPath = Directory.GetCurrentDirectory();
                    // Set any properties that you want on 'options' here
                });

                var serviceProvider = services.BuildServiceProvider();
                var options         = new NodeServicesOptions(serviceProvider)
                {
                    ProjectPath = HostFolder,
                    InvocationTimeoutMilliseconds = 60000 * 10,
                    WatchFileExtensions           = new string[] { }
                };
                var _nodeServices = NodeServicesFactory.CreateNodeServices(options);

                try
                {
                    var data = await RequireJS.RunAsync <object, JToken>(_nodeServices, HostFolder, "earthml-temply-headless/Headless/headless", new
                    {
                        url          = Url,
                        headlessHost = host.ServerFeatures.Get <IServerAddressesFeature>().Addresses
                                       .Select(a => a.Replace("://+", "://localhost").Replace("[::]", "127.0.0.1")).FirstOrDefault(),
                        headless = true,
                        size     = new
                        {
                            width  = Width,
                            height = Height
                        },
                        data         = args,
                        inputPrefix  = InputFolder,
                        outputPrefix = OutputFolder
                    });


                    Console.WriteLine(data.ToString());
                }catch (Exception ex)
                {
                    Console.WriteLine(ex);

                    await Task.Delay(60000);
                }
            }
            if (host != null)
            {
                await host.StopAsync();
            }
            return(0);
        }
 private INodeInstance CreateNodeInstance(NodeServicesOptions options)
 {
     return(new HttpNodeInstance(options, /* port */ 0));
 }
 /// <summary>
 /// Configures the <see cref="INodeServices"/> service so that it will use out-of-process
 /// Node.js instances and perform RPC calls over HTTP.
 /// </summary>
 public static void UseHttpHosting(this NodeServicesOptions options)
 {
     options.NodeInstanceFactory = () => new HttpNodeInstance(options);
 }
Ejemplo n.º 27
0
        /// <summary>
        /// Enables Webpack dev middleware support. This hosts an instance of the Webpack compiler in memory
        /// in your application so that you can always serve up-to-date Webpack-built resources without having
        /// to run the compiler manually. Since the Webpack compiler instance is retained in memory, incremental
        /// compilation is vastly faster that re-running the compiler from scratch.
        ///
        /// Incoming requests that match Webpack-built files will be handled by returning the Webpack compiler
        /// output directly, regardless of files on disk. If compilation is in progress when the request arrives,
        /// the response will pause until updated compiler output is ready.
        /// </summary>
        /// <param name="appBuilder">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="options">Options for configuring the Webpack compiler instance.</param>
        public static void UseWebpackDevMiddleware(
            this IApplicationBuilder appBuilder,
            WebpackDevMiddlewareOptions options = null)
        {
            // Prepare options
            if (options == null)
            {
                options = new WebpackDevMiddlewareOptions();
            }

            // Validate options
            if (options.ReactHotModuleReplacement && !options.HotModuleReplacement)
            {
                throw new ArgumentException(
                          "To enable ReactHotModuleReplacement, you must also enable HotModuleReplacement.");
            }

            // Unlike other consumers of NodeServices, WebpackDevMiddleware dosen't share Node instances, nor does it
            // use your DI configuration. It's important for WebpackDevMiddleware to have its own private Node instance
            // because it must *not* restart when files change (if it did, you'd lose all the benefits of Webpack
            // middleware). And since this is a dev-time-only feature, it doesn't matter if the default transport isn't
            // as fast as some theoretical future alternative.
            var nodeServicesOptions = new NodeServicesOptions(appBuilder.ApplicationServices);

            nodeServicesOptions.WatchFileExtensions = new string[] { }; // Don't watch anything
            if (!string.IsNullOrEmpty(options.ProjectPath))
            {
                nodeServicesOptions.ProjectPath = options.ProjectPath;
            }

            if (options.EnvironmentVariables != null)
            {
                foreach (var kvp in options.EnvironmentVariables)
                {
                    nodeServicesOptions.EnvironmentVariables[kvp.Key] = kvp.Value;
                }
            }

            var nodeServices = NodeServicesFactory.CreateNodeServices(nodeServicesOptions);

            // Get a filename matching the middleware Node script
            var script = EmbeddedResourceReader.Read(typeof(WebpackDevMiddleware),
                                                     "/Content/Node/webpack-dev-middleware.js");
            var nodeScript = new StringAsTempFile(script, nodeServicesOptions.ApplicationStoppingToken); // Will be cleaned up on process exit

            // Ideally, this would be relative to the application's PathBase (so it could work in virtual directories)
            // but it's not clear that such information exists during application startup, as opposed to within the context
            // of a request.
            var hmrEndpoint = !string.IsNullOrEmpty(options.HotModuleReplacementEndpoint)
                ? options.HotModuleReplacementEndpoint
                : "/__webpack_hmr"; // Matches webpack's built-in default

            // Tell Node to start the server hosting webpack-dev-middleware
            var devServerOptions = new
            {
                webpackConfigPath = Path.Combine(nodeServicesOptions.ProjectPath, options.ConfigFile ?? DefaultConfigFile),
                suppliedOptions   = options,
                understandsMultiplePublicPaths  = true,
                hotModuleReplacementEndpointUrl = hmrEndpoint
            };
            var devServerInfo =
                nodeServices.InvokeExportAsync <WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer",
                                                                      JsonConvert.SerializeObject(devServerOptions, jsonSerializerSettings)).Result;

            // If we're talking to an older version of aspnet-webpack, it will return only a single PublicPath,
            // not an array of PublicPaths. Handle that scenario.
            if (devServerInfo.PublicPaths == null)
            {
                devServerInfo.PublicPaths = new[] { devServerInfo.PublicPath };
            }

            // Proxy the corresponding requests through ASP.NET and into the Node listener
            // Anything under /<publicpath> (e.g., /dist) is proxied as a normal HTTP request with a typical timeout (100s is the default from HttpClient),
            // plus /__webpack_hmr is proxied with infinite timeout, because it's an EventSource (long-lived request).
            foreach (var publicPath in devServerInfo.PublicPaths)
            {
                appBuilder.UseProxyToLocalWebpackDevMiddleware(publicPath + hmrEndpoint, devServerInfo.Port, Timeout.InfiniteTimeSpan);
                appBuilder.UseProxyToLocalWebpackDevMiddleware(publicPath, devServerInfo.Port, TimeSpan.FromSeconds(100));
            }
        }
        /// <summary>
        /// Configures the <see cref="INodeServices"/> service so that it will use out-of-process
        /// Node.js instances and perform RPC calls over binary sockets (on Windows, this is
        /// implemented as named pipes; on other platforms it uses domain sockets).
        /// </summary>
        public static void UseSocketHosting(this NodeServicesOptions options)
        {
            var pipeName = "pni-" + Guid.NewGuid().ToString("D"); // Arbitrary non-clashing string

            options.NodeInstanceFactory = () => new SocketNodeInstance(options, pipeName);
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeRenderEngineOptions"/> class.
 /// </summary>
 public NodeRenderEngineOptions()
 {
     WatchFileExtensions = new NodeServicesOptions().WatchFileExtensions;
     NodeHostingModel    = NodeHostingModel.Http;
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Sends a message using Gmail API
        /// </summary>
        /// <param name="msg">Message to send</param>
        /// <returns>JSON serialized string of a result object</returns>
        public string Send(EmailMessage msg)
        {
            string emailTemplate = GetMsgTemplate(msg.template.templateId);

            if (null == emailTemplate)
            {
                _logger.LogError($"Unable to load Email template with id {msg.template.templateId}");
                return(null);
            }

            string htmlBody = null;
            string subject  = null;

            ServiceCollection services = new ServiceCollection();

            services.AddNodeServices();

            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                NodeServicesOptions options = new NodeServicesOptions(serviceProvider) /* Assign/override any other options here */ }
                {
                    ;
                    using (INodeServices nodeServices = NodeServicesFactory.CreateNodeServices(options))
                    {
                        htmlBody = nodeServices.InvokeAsync <string>("pugcompile", emailTemplate, null == msg.bodyModel ? null: JsonConvert.DeserializeObject(msg.bodyModel)).Result;
                        subject  = nodeServices.InvokeAsync <string>("pugcompile", msg.template.subjectTemplate, null == msg.subjectModel ? null : JsonConvert.DeserializeObject(msg.subjectModel)).Result;
                    }
            }

            services.Clear();

            if (null == subject)
            {
                _logger.LogError($"Unable to load email subject");
                return(null);
            }

            if (null == emailTemplate)
            {
                _logger.LogError($"Unable to load Email template with id {msg.template.templateId}");
                return(null);
            }

            UserCredential credential;

            string[] scopes = { GmailService.Scope.GmailSend };

            using (FileStream stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;

                _logger.LogInformation("Google credential file saved to: " + credPath);
            }

            // Create Gmail API service.
            using (GmailService service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "MailGmail",
            }))
            {
                Google.Apis.Gmail.v1.Data.Message message       = CreateGmailMessage(msg.from, msg.to, subject, htmlBody);
                Google.Apis.Gmail.v1.Data.Message messageResult = service.Users.Messages.Send(message, "me").Execute();

                return(JsonConvert.SerializeObject(messageResult));
            }
        }