Embedded Node execute .node file - c++

I've built an embedded instance of Node using the Embed_Test example from the Node repository.
Using this instance, I am trying to execute some C++ code.
I have a generated .node and .dll, and I have webpack'd my Javascript file.
When I run,
node dist/bundle.js
It runs fine. However, when I start up my embedded NodeJS application and attempt to run bundle.js, I get the following error (it's as if it cannot load the .node file?):
TypeError: Cannot read property 'testCppCode' of undefined
at evalmachine.<anonymous>:154:2305
at s.handle_request (evalmachine.<anonymous>:120:782)
at s (evalmachine.<anonymous>:113:879)
at p.dispatch (evalmachine.<anonymous>:113:901)
at s.handle_request (evalmachine.<anonymous>:120:782)
at evalmachine.<anonymous>:106:2533
at Function.v.process_params (evalmachine.<anonymous>:106:3436)
at g (evalmachine.<anonymous>:106:2476)
at evalmachine.<anonymous>:259:218
at s.handle_request (evalmachine.<anonymous>:120:782)
This is my App.js, before a webpack:
var myArgs = process.argv.slice(2);
var ip = myArgs.length >= 1 ? myArgs[0] : "localhost";
var port = myArgs.length >= 2 ? myArgs[1] : "5555";
var fs = require("fs");
var path = require("path");
const express = require("express");
var addon = require("../build/Release/test.node");
const server = express();
server.get("/", (req, res) => {
var res = addon.testCppCode("Sample", function (err, cpp) {
var capabilitiesResult = cpp;
if (err) {
console.log("Error!");
console.log(err);
console.log(cpp);
} else {
console.log("Success!");
}
});
});
I've tried sticking the test.node and test.dll in the same folder as the embedded instance of node, I've tried putting it in the same dir as the webpack, I've tried sticking it in a relative dir to the embedded node (so it would be at ../build/Release/) -- but the error is always the same. What do I need to do in order to get it to "see" the .node file?

Related

host.json not respected by Visual Studio or web job

I have a queue processing application which is workign fine. I am now trying to persuade the queue trigger to only process one item at a time. My host.json is set up correctly, I think:
But when I run the app (either in Azure as a web job, or locally in Visual Studio), I see this:
I suspect that I am missing something really obvious, so wondering whether anyone has come across this before. I have found a few articles, but nothing that gives me any insight into what I am doing wrong.
Adding the contents of program.cs. I have tried adding "host.json" after the AddAzureAppConfiguration entry, but that makes no difference.
class Program
{
static async Task Main()
{
//var builder = new HostBuilder();
var builder = Host.CreateDefaultBuilder();
builder.ConfigureLogging((context, a) =>
{
a.AddConsole();
});
builder.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddUserSecrets(
"5aa19112-5ff7-467b-b062-f37c3654872d"); // This is automatic for a web app, but not for a console app
var settings = config.Build();
var connectionString = settings.GetConnectionString("AzureAppConfiguration");
config.AddAzureAppConfiguration(connectionString);
});
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
});
builder.ConfigureServices((hostContext, services) =>
{
services.AddMemoryCache();
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
}
I think you are using the Azure WebJobs SDK v3.x. In v3.x, hosts.json does not work for WebJob.
Instead, version 3.x uses the standard ASP.NET Core APIs, so you need to configure it using the ConfigureWebJobs method:
static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage(a => {
a.BatchSize = 8;
a.NewBatchThreshold = 4;
a.MaxDequeueCount = 4;
a.MaxPollingInterval = TimeSpan.FromSeconds(15);
});
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
Docs: https://learn.microsoft.com/pt-pt/azure/app-service/webjobs-sdk-how-to#queue-storage-trigger-configuration

How can I see the file system nature of my Node.js Cloud Function environment?

When I deploy my Cloud Function to GCP (written in Node.js), how can I see my file system environment for debugging purposes? What if I want to know what my current directory is or what files are present alongside my application?
When we deploy a Cloud Function, the full Node.js environment is present. We can run arbitrary Node.js logic within. This includes logging information which will then show in the Stackdriver logs. We can thus log our current working directory path as well as a list of all the files in our current directory. We can use this as a diagnostic aid. Here is an example:
const fs = require('fs');
exports.helloWorld = (req, res) => {
console.log(`CWD: ${process.cwd()}`);
fs.readdir('.', function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
console.log(file);
});
res.status(200).send('Done!');
});
};
You can incorporate this logic in your own apps for testing.
And here is an alternate version which shows a recursive listing of all files and sub directories.
const fs = require('fs');
const walk = function(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(walk(file));
} else {
results.push(file);
}
});
return results;
}
exports.helloWorld = (req, res) => {
let message = req.query.message || req.body.message || 'Hello World!';
console.log(`CWD: ${process.cwd()}`);
console.log(`Dir Listing: ${walk('.')}`);
res.status(200).send('Done!');
};
All credit to the above algorithm to node.js fs.readdir recursive directory search.

How to generate icon for onesignal using ionic

I am using ionic 2.
I need generate the icon for one signal notification.
I tried to this
Add a file to your hooks directory inside the after_prepare folder called 030_copy_android_notification_icons.js
Put the following code in it:
var filestocopy = [{
"resources/android/icon/drawable-hdpi-icon.png":
"platforms/android/res/drawable-hdpi/ic_stat_onesignal_default.png"
}, {
"resources/android/icon/drawable-mdpi-icon.png":
"platforms/android/res/drawable-mdpi/ic_stat_onesignal_default.png"
}, {
"resources/android/icon/drawable-xhdpi-icon.png":
"platforms/android/res/drawable-xhdpi/ic_stat_onesignal_default.png"
}, {
"resources/android/icon/drawable-xxhdpi-icon.png":
"platforms/android/res/drawable-xxhdpi/ic_stat_onesignal_default.png"
}, {
"resources/android/icon/drawable-xxxhdpi-icon.png":
"platforms/android/res/drawable-xxxhdpi/ic_stat_onesignal_default.png"
} ];
var fs = require('fs');
var path = require('path');
// no need to configure below
var rootdir = process.argv[2];
filestocopy.forEach(function(obj) {
Object.keys(obj).forEach(function(key) {
var val = obj[key];
var srcfile = path.join(rootdir, key);
var destfile = path.join(rootdir, val);
//console.log("copying "+srcfile+" to "+destfile);
var destdir = path.dirname(destfile);
if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
fs.createReadStream(srcfile).pipe(
fs.createWriteStream(destfile));
}
});
});
I have no idea.
Kindly advice me,
Thanks
I have faced with same issue. Your way is correct, putting 030_copy_android_notification_icons.js file under {root}/hooks/after_prepare. Also note that, filename is not important.
Then to run script you need to run below comment:
ionic cordova prepare android
With this, your script will be run. But maybe your problem may be similar to mine. If you use windows, while coping files from resources/android/icon/ to platforms/android/res/, because of missing of target folders, script is not able to copy operation. That's why a simple code should be added to code.
var destdir = path.dirname(destfile);
if (!fs.existsSync(destdir)){
fs.mkdirSync(destdir);
}

node-gyp c++ leak test with valgrind

Quite simply, how can I do a leak test for a node-gyp c++ module I've just written? Most of the imported c++ code has been tested in different projects, but I would like to make sure there are no leaks.
AFAIK, the only execution mode I've seen is via node:
var wv = require('./build/Release/word_vec.node');
var json = JSON.parse(require('fs').readFileSync('amazon.json', 'utf8'));
var res = wv.convert_sparse(json, 5, 6, 0);
var fs = require('fs');
fs.writeFile("output", JSON.stringify(res), function(err) {
if(err) {
return console.log(err);
}
});
The function I want checked is convert_sparse which resides in word_vec.cpp and is compiled in ./build/Release/word_vec.node or the equivalent debug sub-directory.

Properly babel transpile a file in ember-cli-build withing additional tree

I have some files in my project that I would like to move out of the normal app tree and only load in certain situations. Currently I used broccoli-stew to move the file and broccoli-babel-transpiler to transpile the destination file. However when I do this I end up with an extra default object on the imported files.
this code gets added to the top
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _Ember = _interopRequireDefault(_ember);
and this causes me to have to write the source file with references to ember as Ember["default"].Object etc. Would like to not have any odd references in the source files that makes it harder for other developers to understand.
This is my current ember-cli-build.js file
/* global require, module */
var stew = require('broccoli-stew');
var esTranspiler = require('broccoli-babel-transpiler');
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
storeConfigInMeta: false
});
var additionalTrees = [];
var appTree = app.appAndDependencies();
if (EmberApp.env() !== "production") {
var jQuery = stew.find(appTree, "bower_components/jquery/dist/jquery.min.js");
jQuery = stew.mv(jQuery, "bower_components/jquery/dist/jquery.min.js", "assets/jquery.js");
additionalTrees.push(jQuery);
}
function extractRouter(fileName) {
var router = stew.find(appTree, 'mobile-web/'+ fileName + '.js');
router = esTranspiler(router, {
modules: "amd",
moduleIds: true,
moduleId: "mobile-web/router"
});
router = stew.mv(router, 'mobile-web/'+ fileName + '.js', 'assets/'+ fileName + '.js');
additionalTrees.push(router);
}
extractRouter('router');
extractRouter('secure-router');
return app.toTree(additionalTrees);
};
Try configure your esTranspiler to use modules: "amdStrict":
router = esTranspiler(router, {
modules: "amdStrict",// here
moduleIds: true,
moduleId: "mobile-web/router"
});
It's similar to "commonStrict" as described in the docs. The amdScrict exists in the source code here.