如何使用C++插件中内置的模块中的node.js
How to use node.js built in modules from C++ addons
我需要使用C++插件中模块'crypto'中内置的node.js。我试图找到使用内置模块的C++插件的例子,但失败了。我查看了node_crypto.h/.cc,与node.js加密文档、受保护的构造函数等相比,它有很多不同的函数签名。node_crypto.h包含带有一个参数的InitCrypto()声明,但node_cryptio.cc没有定义这样的函数。只有具有四个参数的InitCrypto。无论如何,我尝试使用带有一个参数的InitCrypto,结果得到了"符号查找错误"。
我可以将require('crypto')的结果传递给我的插件,然后处理这个对象,但这太不安全了。我们的JS代码在客户端的服务器上工作。
目前,我认为C++插件使用类似openssl-lib的smth而不是内置节点模块"crypto"更简单。
所以我需要一些C++插件的工作示例,它使用"crypto"模块或链接到一些关于这方面的文章。
任何使用C++插件中任何内置模块的例子都会很有帮助。
当需要在Nodejs插件中加密/解密数据时,我使用了相同的方法。
据我所知,node_crypto.h
中的类用于在Nodejs中进行本机绑定,我无法在我的插件中使用它们。
然后我尝试从Nodejs中使用OpenSSL,但无法做到,因为OpenSSL是静态链接到Nodejs可执行文件中的。
之后,我尝试从C++中调用JavaScript代码,最终得到了以下解决方案-从C++代码中调用Nodejs函数:
using namespace v8;
// persistent handle for the crypto module
static Persistent<Object> node_crypto;
// Addon startup procedure
void Init(Local<Object> exports, Local<Object> module)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
// get `require` function
Local<Function> require = module->Get(String::NewFromUtf8(isolate, "require")).As<Function>();
// call require('crypto')
Local<Value> args[] = { String::NewFromUtf8(isolate, "crypto") };
Local<Object> crypto = require->Call(module, 1, args).As<Object>();
// store crypto module in persistent handle for further use
node_crypto.Reset(isolate, crypto);
}
NODE_MODULE(addon, Init);
// must be invoked in then Node main thread since the function uses V8 API
std::string encrypt(std::string const& key, std::string const& text)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
// get local handle from persistent
Local<Object> crypto = Local<Object>::New(isolate, node_crypto);
// get `createCipher` function from the crypto module
Local<Function> createCipher = crypto->Get(String::NewFromUtf8(isolate, "createCipher")).As<Function>();
// call crypto.createCipher("aes256", key)
Local<Value> create_args[] =
{
String::NewFromUtf8(isolate, "aes256"),
String::NewFromUtf8(isolate, key.c_str())
};
Local<Object> cipher = createCipher->Call(crypto, 2, create_args).As<Object>();
// get update and final functions from the crypto module
Local<Function> update = cipher->Get(String::NewFromUtf8(isolate, "update")).As<Function>();
Local<Function> final = cipher->Get(String::NewFromUtf8(isolate, "final")).As<Function>();
// buf1 = cipher.update(text), buf2 = cipher.final()
Local<Value> update_args[] = { node::Buffer::New(isolate, text.data(), text.size()) };
Local<Value> buf1 = update->Call(cipher, 1, update_args);
Local<Value> buf2 = final->Call(cipher, 0, nullptr);
// concatenate update and final buffers into result string
char const* const data1 = node::Buffer::Data(buf1);
char const* const data2 = node::Buffer::Data(buf2);
size_t const size1 = node::Buffer::Length(buf1);
size_t const size2 = node::Buffer::Lenght(buf2);
std::string result;
result.reserve(size1 + size2);
result.append(data1, size1);
result.append(data2, size2);
return result;
}
std::string decrypt(std::string const& key, std::string const& text)
{
// similar as in encrypt, use createDecipher instead
}
正如您所看到的,带有V8 API的C++代码非常冗长。在实际项目中,我使用v8pp库中的实用程序函数来获取对象属性,并调用将数据转换为V8句柄的函数。
- Node.js正在更改应用程序以使用集群模块
- 如何使用Node.js在JavaScript模块文件之间使用事件处理程序
- 正在更改node.js模块变量
- 编写在浏览器和node.js中运行的模块化ECMAScript
- 在Node.js中使用异步模块时出现问题
- 使用Q节点模块时,Bluemix node.js应用程序部署失败
- 如何测试运行 node-fluent-ffmpeg(异步模块)的自定义模块
- 从 Node.js 模块中的缓冲区实例中逐行读取字符串
- http模块(node.js和browserfy的)没有't使用方法PATCH写入请求正文
- 我可以修改和发布npm模块(node.js)吗
- 错误:找不到模块'/node-v11-darwin-x64/node_sqlite3.node'
- 从导出的模块(node.js)访问函数
- 模块Node.js中的两个App.js
- 文件结构的最佳实践-使用请求模块(Node.js + Express.js)
- 加密模块-Node.js
- DNS缓存请求模块- node.js
- MeteorJS + npm:找不到模块'node-xmpp'
- SyntaxError:导入模块Node.js时的意外标识符
- 未在webpack中加载标准模块Node.JS
- 加载模块node.js的最佳方式