Ejemplo n.º 1
0
        //--------------------------
        static void TestNodeFeature_Internationalization_Example()
        {
            //https://nodejs.org/dist/latest-v11.x/docs/api/intl.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            //example1: just show value
            NodeJsEngineHelper.Run(() =>
            {
                return(@"                     
                    const january = new Date(9e8);
                    const english = new Intl.DateTimeFormat('en', { month: 'long' });
                    const spanish = new Intl.DateTimeFormat('es', { month: 'long' });

                    console.log(english.format(january));
                    // Prints 'January'
                    console.log(spanish.format(january));
                                    // Prints 'M01' on small-icu
                                    // Should print enero

                ");
            });
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void TestNodeJs_Buffer()
        {
            //https://nodejs.org/api/buffer.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            ////example2: get value from node js
            NodeBufferBridge myBuffer = new NodeBufferBridge();

            NodeJsEngineHelper.Run(ss =>
            {
                ss.SetExternalObj("myBuffer", myBuffer);
                return(@"                     
                    const buf1 = Buffer.alloc(20);

                    buf1.writeUInt8(0, 0);
                    buf1.writeUInt8(1, 1);
                    buf1.writeUInt8(2, 2);

                    //-----------
                    myBuffer.SetBuffer(buf1);
                    myBuffer.CopyBufferFromNodeJs();
                    console.log(buf1);    
                    ");
            });
            int buffLen = myBuffer.Length;

            string userInput = Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void TestNodeFeature_Url_Example()
        {
            //https://nodejs.org/dist/latest-v11.x/docs/api/url.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            //example1: just show value
            NodeJsEngineHelper.Run(() =>
            {
                return(@"            

                        const url = require('url');
                        const myURL1 =
                            url.parse('https://*****:*****@sub.example.com:8080/p/a/t/h?query=string#hash');

                        console.log(JSON.stringify(myURL1));

                        //
                        console.log('\r\n');
                        console.log('\r\n');
                        //
                        const myURL2 =
                            new URL('https://*****:*****@sub.example.com:8080/p/a/t/h?query=string#hash');
                        console.log(myURL2.host+'\r\n');
                        console.log(myURL2.href+'\r\n');
                        console.log(myURL2.hostname +'\r\n');
                ");
            });
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main()
        {
            //-----------------------------------
            //1.
            //after we build nodejs in dll version
            //we will get node.dll
            //then just copy it to another name 'libespr'
            string currentdir = System.IO.Directory.GetCurrentDirectory();


            string libEspr = @"../../../node-v13.5.0/out/Release/node.dll";
            //-----------------------------------
            //2. load node.dll
            //-----------------------------------

            IntPtr intptr     = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------
            MyApp myApp = new MyApp();
            NodeJsEngineHelper.Run(new string[] { "--inspect", "hello.espr" },
                                   ss =>
            {
                ss.SetExternalObj("myApp", myApp);

                return(@" const http2 = require('http2');
                    const fs = require('fs');
                    
                    const server = http2.createSecureServer({
                      key: fs.readFileSync('localhost-privkey.pem'),
                      cert: fs.readFileSync('localhost-cert.pem')
                    });
                    server.on('error', (err) => console.error(err));
                    server.on('socketError', (err) => console.error(err));

                    server.on('stream', (stream, headers) => {
                      // stream is a Duplex
                      //const method = headers[':method'];
                      //const path = headers[':path'];  
                       
                      //let result=  myApp.HandleRequest(method,path);
                      
                      //stream.respond({
                      //  'content-type': 'text/html',
                      //  ':status': 200
                      //});
                      stream.end('<h1>Hello World, EspressoND, node 12.11.1</h1>'+ myApp.GetMyName());
                      //stream.end(result);
                    });

                    server.listen(8443);
                    console.log('hello!');
                    ");
            });
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void Main()
        {
            //-----------------------------------
            //1.
            //after we build nodejs in dll version
            //we will get node.dll
            //then just copy it to another name 'libespr'
            string currentdir = System.IO.Directory.GetCurrentDirectory();

            string libEspr = @"../../../node-v11.12.0/Release/libespr.dll"; //previous version 8.4.0

            if (File.Exists(libEspr))
            {
                //delete the old one
                File.Delete(libEspr);
            }
            File.Copy(
                @"../../../node-v11.12.0/Release/node.dll", // //previous version 8.4.0
                libEspr);

            //-----------------------------------
            //2. load libespr.dll (node.dll)
            //-----------------------------------
            //string libEspr = "libespr.dll";
            IntPtr intptr     = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------
            NodeJsEngineHelper.Run(new string[] { "--inspect", "hello.espr" },
                                   ss => @" const http2 = require('http2');
                    const fs = require('fs');

                    const server = http2.createSecureServer({
                      key: fs.readFileSync('localhost-privkey.pem'),
                      cert: fs.readFileSync('localhost-cert.pem')
                    });
                    server.on('error', (err) => console.error(err));
                    server.on('socketError', (err) => console.error(err));

                    server.on('stream', (stream, headers) => {
                      // stream is a Duplex
                      stream.respond({
                        'content-type': 'text/html',
                        ':status': 200
                      });
                      stream.end('<h1>Hello World, EspressoND, node 11.12.0</h1>');
                    });

                    server.listen(8443);
                    ");
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 6
0
        static void TestNodeVM_Example()
        {
            //https://nodejs.org/dist/latest-v11.x/docs/api/vm.html
            //const vm = require('vm');

            //const x = 1;

            //const sandbox = { x: 2 };
            //vm.createContext(sandbox); // Contextify the sandbox.

            //const code = 'x += 40; var y = 17;';
            //// x and y are global variables in the sandboxed environment.
            //// Initially, x has the value 2 because that is the value of sandbox.x.
            //vm.runInContext(code, sandbox);

            //console.log(sandbox.x); // 42
            //console.log(sandbox.y); // 17

            //console.log(x); // 1; y is not defined.


            //-----------

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            NodeJsEngineHelper.Run(() =>
            {
                return(@"
                     
                    const vm = require('vm');

                    const x = 1;

                    const sandbox = { x: 2 };
                    vm.createContext(sandbox); // Contextify the sandbox.

                    const code = 'x += 40; var y = 17;';
                    // x and y are global variables in the sandboxed environment.
                    // Initially, x has the value 2 because that is the value of sandbox.x.
                    vm.runInContext(code, sandbox);

                    console.log(sandbox.x); // 42
                    console.log(sandbox.y); // 17

                    console.log(x); // 1; y is not defined.
                    
                    ");
            });
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void Main()
        {
            //-----------------------------------
            //1.
            //after we build nodejs in dll version
            //we will get node.dll
            //then just copy it to another name 'libespr'


            string libEspr = @"../../../node-v16.8.0/out/Release/node.dll";
            //-----------------------------------
            //2. load node.dll
            //-----------------------------------
            //string libEspr = "libespr.dll";
            IntPtr hModule    = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

#if DEBUG
            JsBridge.dbugTestCallbacks();
            IntPtr proc = GetProcAddress(hModule, "RunJsEngine");
            //RunJsEngineDel del1 = (RunJsEngineDel)System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(proc,
            //   typeof(RunJsEngineDel));
            //del1(3, new string[] { "node", "--inspect", "hello.espr" }, IntPtr.Zero, IntPtr.Zero);
#endif
            //------------
            NodeJsEngineHelper.Run(new string[] { "--inspect", "hello.espr" },
                                   ss => @"
                    const http2 = require('http2');
                    const fs = require('fs');

                    const server = http2.createSecureServer({
                      key: fs.readFileSync('localhost-privkey.pem'),
                      cert: fs.readFileSync('localhost-cert.pem')
                    });
                    server.on('error', (err) => console.error(err));
                    server.on('socketError', (err) => console.error(err));

                    server.on('stream', (stream, headers) => {
                      // stream is a Duplex
                      stream.respond({
                        'content-type': 'text/html',
                        ':status': 200
                      });
                      stream.end('<h1>Hello World, EspressoND, node 16.8.0</h1>');
                    });

                    server.listen(8443);
                    ");
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 8
0
        static void TestSocketIO_ChatExample()
        {
            //change working dir to target app and run
            //test with socket.io's chat sample
            System.IO.Directory.SetCurrentDirectory(@"../../../socket.io/examples/chat");

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            NodeJsEngineHelper.Run(ss => File.ReadAllText("index.js"));

            string userInput = Console.ReadLine();
        }
Ejemplo n.º 9
0
        static void TestNodeFeature_OS_Example1()
        {
            //https://nodejs.org/dist/latest-v11.x/docs/api/os.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            //example1: just show value
            NodeJsEngineHelper.Run(() =>
            {
                return(@"                     
                    const os = require('os'); 
                    console.log('arch = '+ os.arch());
                    console.log('cpus = '+ JSON.stringify(os.cpus()));
                    console.log('hostname='+ os.hostname());
                    ");
            });
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 10
0
        static void TestNodeJs_NApi()
        {
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif

            var test_instance = new MyNodeJsApiBridgeTestInstance();
            NodeJsEngineHelper.Run(ss =>
            {
                //for general v8
                //see more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint8

                test_instance.SetJsContext(ss.Context);
                ss.SetExternalObj("test_instance", test_instance);
                return(@"                  
                   
                    var arr= test_instance.CreateArrayFromDotnetSide();
                    console.log(arr);

                    var str= test_instance.CreateString('user_a001_test');
                    console.log(str);

                    var externalMem = test_instance.CreateExternalBuffer();
                    console.log(externalMem);
                    test_instance.RecheckExternalBuffer();
                    if(externalMem[1]==1){
                        console.log('hello2');
                    }
                    else{
                        console.log(externalMem);
                    }

                    externalMem=null;
                     
                    test_instance.TestRunScript();
                ");
            });

            string userInput = Console.ReadLine();
        }
Ejemplo n.º 11
0
        //--------------------------
        static void TestNodeFeature_DNS_Example()
        {
            //https://nodejs.org/dist/latest-v11.x/docs/api/dns.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            //example1: just show value
            NodeJsEngineHelper.Run(() =>
            {
                return(@"                     
                    const dns = require('dns');

                    dns.lookup('iana.org', (err, address, family) => {
                        console.log('address: %j family: IPv%s', address, family);
                    });
                    
                    ");
            });
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 12
0
        static void TestNodeFeature_OS_Example2()
        {
            //https://nodejs.org/dist/latest-v11.x/docs/api/os.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            ////example2: get value from node js
            OsInfo myOsInfo = new OsInfo();
            NodeJsEngineHelper.Run(ss =>
            {
                ss.SetExternalObj("my_osInfo", myOsInfo);

                return(@"                     
                    const os = require('os');                      
                    my_osInfo.Arch = os.arch();
                    my_osInfo.Hostname = os.hostname();
                    ");
            });
            Console.WriteLine("arch=" + myOsInfo.Arch);
            Console.WriteLine("hostname=" + myOsInfo.Hostname);
            string userInput = Console.ReadLine();
        }
Ejemplo n.º 13
0
        static void Main()
        {
            //-----------------------------------
            //1.
            //after we build nodejs in dll version
            //we will get node.dll
            //then just copy it to another name 'libespr'

            string libEspr = @"../../../node-v16.8.0/out/Release/node.dll";

            //-----------------------------------
            //2. load node.dll
            //-----------------------------------
            IntPtr intptr     = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------
            //http2 client

            //http2 client
            HttpResp httpResp = new HttpResp();

            NodeJsEngineHelper.Run(
                ss =>
            {
                ss.SetExternalObj("my_resp", httpResp);

                return(@"const http2 = require('http2');
                        const fs = require('fs');
                        const client = http2.connect('https://localhost:8443', {
                          ca: fs.readFileSync('localhost-cert.pem')
                        });
                        client.on('error', (err) => console.error(err));

                        //const req = client.request({ ':path': '/' });
                        const req = client.request({ ':path': '/' ,'body':'123456789'});

                        req.on('response', (headers, flags) => {
                          for (const name in headers) {
                            console.log(`${name}: ${headers[name]}`);
                          }
                        });

                        req.setEncoding('utf8');
                        let data = '';
                        req.on('data', (chunk) => { data += chunk; });
                        req.on('end', () => {
                          //console.log(`\n${data}`);
                          my_resp.Data=data;
                          client.close();
                        });
                        req.end();
                    ");
            });

            string userInput = Console.ReadLine();
            if (httpResp.Data != null)
            {
                Console.WriteLine(httpResp.Data);
            }
        }
Ejemplo n.º 14
0
        static void TestNodeJs_Buffer()
        {
            //https://nodejs.org/api/buffer.html
#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------

            ////example2: get value from node js
            MyBufferBridge myBuffer = new MyBufferBridge();

            //for nodejs's Buffer
            //NodeJsEngineHelper.Run(ss =>
            //{
            //    //for node js
            //    ss.SetExternalObj("myBuffer", myBuffer);
            //    return @"
            //        const buf1 = Buffer.alloc(20);
            //        buf1.writeUInt8(0, 0);
            //        buf1.writeUInt8(1, 1);
            //        buf1.writeUInt8(2, 2);
            //        //-----------
            //        myBuffer.SetBuffer(buf1);
            //        console.log('before:');
            //        console.log(buf1);

            //        if(myBuffer.HaveSomeNewUpdate()){
            //            myBuffer.UpdateBufferFromDotNetSide();
            //            console.log('after:');
            //            console.log(buf1);
            //        }else{
            //            console.log('no data');
            //        }
            //    ";
            //});

            //for v8
            NodeJsEngineHelper.Run(ss =>
            {
                //for general v8
                //see more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint8

                ss.SetExternalObj("myBuffer", myBuffer);
                return(@"                     
                    const buf1 = new ArrayBuffer(20);
                    const view = new DataView(buf1);
                    view.setUint8(0, 0); 
                    view.setUint8(1, 1);
                    view.setUint8(2, 2);
                    //-----------
                    myBuffer.SetBuffer(buf1);
                    console.log('before:');
                    console.log(buf1);

                    if(myBuffer.HaveSomeNewUpdate()){
                        myBuffer.UpdateBufferFromDotNetSide();
                        console.log('after:');
                        console.log(buf1);    
                    }else{
                        console.log('no data');
                    }                    
                ");
            });
            int buffLen = myBuffer.Length;

            string userInput = Console.ReadLine();
        }
Ejemplo n.º 15
0
        static void Main()
        {
            //-----------------------------------
            //1.
            //after we build nodejs in dll version
            //we will get node.dll
            //then just copy it to another name 'libespr'
            string currentdir = System.IO.Directory.GetCurrentDirectory();

            string libEspr = "libespr.dll";

            ////string libEspr = @"../../../node-v11.12.0/Release/libespr.dll"; //previous version 8.4.0
            //if (File.Exists(libEspr))
            //{
            //    //delete the old one
            //    File.Delete(libEspr);
            //}
            //File.Copy(
            //   @"../../../node-v11.12.0/Release/node.dll", // //previous version 8.4.0
            //   libEspr);

            //-----------------------------------
            //2. load libespr.dll (node.dll)
            //-----------------------------------
            //string libEspr = "libespr.dll";
            IntPtr intptr     = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //------------
            //http2 client
            NodeJsEngineHelper.Run(
                ss => @"const http2 = require('http2');
                        const fs = require('fs');
                        const client = http2.connect('https://localhost:8443', {
                          ca: fs.readFileSync('localhost-cert.pem')
                        });
                        client.on('error', (err) => console.error(err));

                        const req = client.request({ ':path': '/' });

                        req.on('response', (headers, flags) => {
                          for (const name in headers) {
                            console.log(`${name}: ${headers[name]}`);
                          }
                        });

                        req.setEncoding('utf8');
                        let data = '';
                        req.on('data', (chunk) => { data += chunk; });
                        req.on('end', () => {
                          console.log(`\n${data}`);
                          client.close();
                        });
                        req.end();
                    ");
            string userInput = Console.ReadLine();
        }