Using libphonenumber library in postgresql function - node.js

I need to parse phone numbers in a postgresql function. I am using waterline ORM and was basically doing this computation in javascript. Now I want to move the entire logic to postgresql stored function. But the bottleneck is the availability of libphonenumber library. It's available in Node.js but not sure how to use it in postgresql.
Any idea ?

There are C bindings available for libphonenumber already
CREATE EXTENSION pg_libphonenumber;
SELECT parse_phone_number('03 7010 1234', 'AU') = parse_phone_number('(03) 7010 1234', 'AU');
CREATE TABLE foo
AS
SELECT DISTINCT parse_phone_number(ph, 'AU')
FROM ( VALUES
('0370101234'),
('03 7010 1234'),
('(03) 7010 1234')
) AS t(ph);
Find my answer here for more information

Since it's available in C++ and JavaScript, you could:
Install and load PL/V8 and use it as a JavaScript library in a PL/V8 stored procedure; or
Write a C extension for PostgreSQL that exposes the functions as SQL-callable functions by wrapping the C++ functions in the C++ version of the library.
Needless to say, the former is probably easier, unless you're comfortable with C programming.

Related

permissions for Lua Scripts in C

Is it possible to give the lua script lower permissions so that it is not possible to execute OS functions ?
for example:
os.execute("mkdir ..")
I use the lua 5.3.5 library. Maybe it includes an option to allow or disallow some lua functions?
Yes it is possible, typically Lua initialization load all the standard library:
lua_State *LuaState = luaL_newstate();
luaL_openlibs(LuaState);
But you could adjust to include just what you need with the functions from lualib.h:
luaopen_package
luaopen_coroutine
luaopen_string
luaopen_utf8
luaopen_table
luaopen_math
luaopen_io
luaopen_os
luaopen_debug
In such case you could have the following initialization:
lua_State *LuaState = luaL_newstate();
luaopen_io(LuaState);
luaopen_table(LuaState);
luaopen_string(LuaState);
You can find more details in the chapter 6 of the documentation
http://www.lua.org/manual/5.3/manual.html#6
If you use luaL_openlibs to load Lua's standard libraries, just copy linit.c to your project and edit it to choose the libraries you want to export.
If you want to remove only a few functions, do this after loading the libraries:
luaL_dostring(L,"os.execute=nil");

Referring to a constant from library, twincat 3

Im trying to accomplish a twincat 3 library which does things using global constants defined in the main project, like creating arrays the size of those constants and cycling trough them. However I've been unsuccessful and I wonder if this can be done. I just get this error "Error 4 Border 'cPassedConstant' of array is no constant value" when I try to build the main project. The error comes from the array defined in the library.
I've tried making a GVL with a constant of the same name to the library and then setting the "external implementation" property true but that does not help.
My goal here is to make a IO management library with filtering and such. And then I could just add it to the main project and define some constants like "cDigitalIputsCount","cAnalogInputCount" and so on.
Maybe you can get along with the new ARRAY[*] feature instead, although it is still very limited. There is no other way than to define the constant in the library.
The library concept is the same as in other environments. A library provides you reusable components. Your main project depends on the library and not the other way around. Therefore your library cannot know a thing about the project where it is used.
A confusing thing in TwinCat3 is, that you can build projects successful with programming errors inside. The TwinCat3 compiler allows broken code inside a project as long as it is not called. Therefore when you ship libraries you should always use "Check all objects".
You should check Beckhoff's feature called Parameter List. By adding a parameter list to the library project, you can re-define library constants in the project that uses the library. The definition happens in the library manager.
Image from Beckhoff's site:
I think that should do it. Of course, the other option is to use the ARRAY[*] option, which is awesome too (for a PLC programming world). The problem with parameter lists is that it is a project-wide re-definition. Using the ARRAY[*] allows the size be changed dynamically.
I would suggest using a variable length ARRAY[*], as explained in the link below (and also in the Beckhoff/Infosys, section DataTypes/Array).
The point is that you should declare the ARRAY[1..cAINs] of FB_AnalogIO in your main program (it knows the FB_AnalogIO from your analog library and can declare it with a constant size).
The PRG_IO should then be changed to either a function or function block, so that it accepts the ARRAY[*] as a VAR_IN_OUT without knowing the exact size.
https://stefanhenneken.wordpress.com/2016/09/27/iec-61131-3-arrays-with-variable-length/

How to find a source file used in process.binding('..') in node source code?

I want to see source code of the file mentioned in process.binding() statement in nodeJS source code, have seen similar question on stackoverflow but most of them answers to specific cases like fs etc.
I want explanation based on which I should be able to find any file mentioned in process.bindings().
Since this question is lying here for very long I decided to give it another try to find the answer and found one very nice presentation online.
Process.bindings presentation
To summaries this, process.bindings() binds the javascript object with the C++ object which exposes the C++ functions to be executed from javascript, essentially it creates a javascript wrapper around the C++ functions to make it available from NodeJS.
To find which C++ function is bound to which javascript function you can dig into NodeJS source tree here NodeJs source tree . Names are straightforward to map with core module names and look for signature env->SetMethod(target, "<modulename>", <C++ function name>);
Example: env->SetMethod(target, "stat", Stat);

add a builtin function to nodejs

I would like to add a couple of functions that we use extensively to nodejs in such a way that they are always available, in the same way as the builtin functions, like Date, Array, Object etcetera.
So without having to do a 'require'.
Is this possible?
gr,
Coen.
You could hack at the v8 source code that node.js is based on.

Is it possible to include NodeJS as a scripting engine inside an executable?

I've an application written in C. I would like to execute user defined scripts written in JavaScript in this application and allow these scripts to work with my internal C variables mapped to JavaScript namespace.
Is it possible to use NodeJS to compile it as a scripting engine?
I know that I can do vice versa: run NodeJS and use my C code as a library for NodeJS with proper binding of C variables to JS. But this is not acceptable, since my application has a GUI and many other modules that are included as libs and it would be hard to rewrite the code to run as a NodeJS lib.
I also don't want to run NodeJS as an external executable each time I need to run a script due to performance reasons. I need to keep NodeJS in memory and run scripts in the same namespace during the whole process cycle.
Maybe there is some special edition of NodeJS intended for such purpose? Or I can compile it as such?
Here is an example executing some javascript using v8:
int main(int argc, char* argv[]) {
// Create a string containing the JavaScript source code.
String source = String::New("'Hello' + ', World'");
// Compile the source code.
Script script = Script::Compile(source);
// Run the script to get the result.
Value result = script->Run();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
From: https://developers.google.com/v8/get_started
What you need is V8 javascript engine. Take a look here for more details...
This document discusses these concepts further and introduces others that are key to embedding V8 within your own C++ application.
What you can use nodejs for is perhaps to look at it's source code on how to build on top of V8 engine.
And here is a quick example.
Why not explore the internals of Node and sort of "copy" what it's doing. Then you can build your app on top of V8. Node.JS is, after all, just a layer on top of V8 and several libraries (I/O) to provide additional functionality.

Resources