node.jsfirebasegoogle-cloud-functionsnode.js-addonnode.js-napi

firebase cloud function with N-API deployment failed


Trying to find out if it's possible to run firebase cloud functions with native code (using N-API). I have a simple "hello world" example which works fine under emulator, however when I try to deploy it I get INVALID_ARGUMENT error:

  status: {
   code:  3     
   message:  "INVALID_ARGUMENT"     
  }

That's is not very informative... Just wondering if someone could shed some light on the situation. Thanks!

here is the function:

'use strict';

const functions = require('firebase-functions');

exports.helloWorld = functions.https.onRequest(async(request, response) => {

console.time('Program runtime');

const testAddon = require('bindings')('testaddon.node')
const {promisify} = require('util');

module.exports = testAddon;
const asyncCommand = testAddon.hello();

try {
  const result = await asyncCommand;
  console.log('CONTENT:', result);
  response.send(result);
}
catch (err) {
    console.log('ERROR:', err);
    response.send('ERROR:', err);
}

console.timeEnd('Program runtime');
});

and corresponding C++ source:

#include <napi.h>
namespace functionexample {
  std::string hello();
  Napi::String HelloWrapped(const Napi::CallbackInfo& info);
  Napi::Object Init(Napi::Env env, Napi::Object exports);
}
#include "functionexample.h"
std::string functionexample::hello(){
  return "Hello World";
}
Napi::String functionexample::HelloWrapped(const Napi::CallbackInfo& info)
{
  Napi::Env env = info.Env();
  Napi::String returnValue = Napi::String::New(env, functionexample::hello());

  return returnValue;
}
Napi::Object functionexample::Init(Napi::Env env, Napi::Object exports)
{
  exports.Set(
"hello", Napi::Function::New(env, functionexample::HelloWrapped)
  );

  return exports;
}

Solution

  • It seems that the problem was with a version of the node engine. I've switched to node10 instead of node8 and my test function deploys properly and works as expected.