How to wrap a c/c++ file into node.js file - javascript

I have a c++ header file. I need to wrap c++ and node.js and use the JS script for my testing. I am new to node.js and I need some help in understanding from where to start. From the analyzing i did so far it seems like we need to manually write the node.js file for calling the function (kind of declaring the functions and use it).
I have created a .js file and declared all my functions and variables in it and called the C++ API.
var testlib = ffi.Library(file.libtest,{ 'Test_pgm': ['int', ['int','string']] });
var result=testlib.test_pgm(20,"name");
Is there anyway to automate this process so that when i pass the C++ header file it should parse the functions and variables like above and should create output file, so that i can call the C++ Api's directly by importing the output node.js wrapper file.
Previously i have did python and c wrapping using CFFI. There i create a customized header file and passed that file to cffi. Is there anything similar to this method in node.js ?
Thanks in advance.

Related

How to expose python functions in javascript with cefpython with an external HTML file

I’m trying to write an HTML GUI for a project with cefpython. I’ve been able to create a working GUI in HTML, but I’d like to be able to expose python functions in javascript. There’s a tutorial on how to do this in the cefpython GitHub repo, but it involves writing your HTML in a string in the python program and converting it into a data URI. Is there a way to interface python and javascript while keeping your HTML in a separate file? Right now, I’ve passed cefpython the address of my HTML file as the URL to open, but I don’t necessarily need to use that method. I just want to have HTML in an external file and call a python function from javascript run on that page (so that I can call a python function by pressing a button on the page.)
Edit: My question is: how do I call a python function from javascript contained in an external HTML file, using the cefpython library?
from cefpython3 import cefpython as cef
import sys, os
sys.excepthook = cef.ExceptHook
cef.Initialize()
cef.CreateBrowserSync(url="file:///./main.html")
cef.MessageLoop()
cef.Shutdown()
Probably too late for an answer, but yeah..
Define a function to bind the objects/functions/properties in python you want to be accessed in javascript.
from cefpython3 import cefpython as cef
import sys, os
sys.excepthook = cef.ExceptHook
cef.Initialize()
browser = cef.CreateBrowserSync(url="file:///./main.html")
set_javascript_bindings(browser)
cef.MessageLoop()
cef.Shutdown()
def set_javascript_bindings(browser):
bindings = cef.JavascriptBindings(
bindToFrames=False, bindToPopups=False)
bindings.SetObject("object_name_in_js", your-object-in-python)
bindings.SetProperty("property_name_in_js", your-property-in-python)
bindings.SetFunction("function_name_in_js", your-function-in-python)
browser.SetJavascriptBindings(bindings)
Here, "object_name_in_js" and the like will be how you call your objects and functions in javascript. your-object-in-python and the like are their names in the python file.
Then, in your js file, (or in the script tag of your html file), refer them directly.
function_name_in_js()
property_name_in_js = ...
and so on.
Hope this helps someone!

How to access to functions/variables from GWT code in Javascript?

I am trying to use some functions from a code that has been obfuscated. So i have an html file that is calling a JS file thru the tag:
<script src="gwt_svg_viewer/gwt_svg_viewer.nocache.js"></script>
that file is defining a function called "onScriptdownloaded" which receives a string like this:
gwt_svg_viewer.onScriptDownloaded(["var $wnd = window.parent;function RE(){}"]);
So my question is how can i access to RE? in another JS file?
It seems that there was a kind of GWT code implemented, but i am not really familiar with that.
Variable names and functions in gwt will be obfuscated every time you compile your project, and variables and functions will be renamed in the process, in order to call a gwt code from javascript in a consistence manner you will need to use jsinterop to export java types. more information can be found in the gwt jsinterop documentation

Different path to NodeJS functions

I hope I can make myself be understood...
I'm writing a CEP extension for Adobe products. The good thing is that they include NodeJS - which allows me, for example, to access the filesystem.
Now I want to use a js library that has dependencies on NodeJS (crypto, fs and path). The problem is that within the Adobe extension, all NodeJS functionality is accessible through cep_node.crypto (for example).
The js library that I want to use doesn't know that, naturally. So it tells me:
ERROR in ./node_modules/dynamsoft-javascript-barcode/dist/dbr.min.js
Module not found: Error: Can't resolve 'crypto' in '.\node_modules\dynamsoft-javascript-barcode\dist'
The question is - how can I kind of overwrite the reference 'crypto' for example - so it gets accessed by cep_node.crypto?
Note this answer is attempting to be helpful (without code in the question it would be impossible to give a direct answer to the problem)
I think it would help to have a good understanding of CEP before you begin incorporating node imports.
CEP extension has a few different elements to it.
The html. Here you build the interface and can load JS files. You would have to make sure node is activated to be able to import.
Js files. Here you can execute your node js files. Not that this area is separate from the actual scripting area (which runs on an antiquated JS engine, you CANNOT load node product with in that engine).
The JSX which runs with in the adobe scripting engine.
In html I load my cep.
Then in a js file I can load my node using the cep require:
FS as an example:
const fs = cep_node.require('fs');
const csiRun = new CSInterface();
csiRun.evalScript(`buildHtml()`, (returnedImg) => {
// buildHtml is a method in a JSX that runs with in the adobe scripting //engine. It cannot handle the node. I can return a value then do //something like this:
fs.writeFile()
// notice the node module used here.
})

Hiding API keys in JS setup

I'm creating a simple UI with an HTML+CSS+JS setup that uses a third-party API requiring a key and I'd like to keep the key in a file that I can gitignore and access from my Javascript file. I'm not going to be deploying this project, I just want to be able to push it to GitHub without temporarily deleting the variable before every commit.
All responses I've found are related to setups with Node.js, React/Webpack, or other server setups, but I don't have a server or transpiler and don't want to add a bunch of cruft and configurations just for this. Is there a way to do that? I tried storing it in a separate JS file and import/export, but either I couldn't get the syntax right or what I was trying needed a transpiler. Every attempt variation resulted in a syntax error or undefined variable.
Key information:
This is project run entirely locally--as in, just opening index.html in my browser--so I don't think I need to worry about the key being exposed in the client.
My setup pretty much just includes index.html, main.js, and style.css.
I want to push the project to GitHub, so I want to store the api key as a variable in a file separate from main.js that I can add to .gitignore but access in main.js.
I'm keeping this as simple as possible without frameworks, etc. Unless it's super simple and lightweight, I don't want to add libraries just to get this working.
Your best bet is to pull whatever secrets you need from the environment itself, either your environment variables or a secrets store.
The specific implementation will depend on what serverless provider you're using, but for example AWS Lambda lets you configure env vars:
https://docs.aws.amazon.com/lambda/latest/dg/env_variables.html
and you can use the Key Management Service or Parameter Store depending on your preference and requirements:
https://aws.amazon.com/blogs/mt/the-right-way-to-store-secrets-using-parameter-store/
Leaving the above in place in case folks find this via looking at the Serverless tag. Updates below based on the updated question.
So, if I understand the updated question correctly, you want to check in all your code to git except the API key, serve the files only on your local file system and have that local copy be able to access the API key.
The simplest way to do this would be to just have another .js file that defines the variable(s) in question like so:
// config.js
var config = { // this should be const instead if you're using ES6 standards
apiKey : '123456789XYZ'
}
Then, in index.html have another script tag that calls that before any scripts that need the config, e.g.:
<script src='./config.js'></script>
<script src='./main.js'></script>
</body>
In main.js you will then be able to reference the variable config for use, and similarly you can .gitignore 'config.js'.
If you use MVC, try to write in php variabel. Than open in script javasript with echo ...

How to go about utilizing acorn.js in another .js file?

I'm trying to write a Javascript translator using a Javascript parser called "acorn.js." I'm writing my code in Sublime using a Node build system. Calling the following line:
require("./acorn.js");
produces no errors, but whenever I attempt to access any functions within that file, an undefined error is thrown. Is there any way I can import or reference this library to gain access to its functions without having to utilize a JQuery import or something of the like?
Here is the answer:
var test = acorn.parse("var x;");
And if you're using this specific library, what is returned is an AST of the contents of what you parse. In this case, it's the string "var x;".
You have to assign the imported module to a variable. For ex :
var acorn = require("./acorn.js");
And access the methods from that variable :
acorn.parse(in,out);

Categories

Resources