gpt4 book ai didi

node.js - Node JS 插件 - NAN 与 N-API?

转载 作者:IT老高 更新时间:2023-10-28 23:25:19 35 4
gpt4 key购买 nike

我正在寻找使用带有 C++ 的 Node js 插件的项目。我遇到了两个可以使用的抽象库 NAN 和 N-API。但是我无法决定应该使用哪一个。我无法在这两个库之间找到适当的比较。

两者的优缺点是什么?如何选择?

到目前为止,我发现 NAN 有更多关于异步调用的在线教程/文章。但是 N-API 得到了 Node 的官方支持(并且是在 NAN 之后创建的,作为更好的选择,虽然不确定。)

最佳答案

我的理解是这样的:

Node-API (以前的 N-API)在 v8.0.0 中被添加到核心 node.js 接口(interface)中。 “它旨在将插件与底层 JavaScript 引擎的变化隔离开来……”引用文档。它还为缓冲区和异步工作等事物提供了一些其他包装器(这应该有助于避免在其 Implications of ABI stability 部分中提到的一些底层不稳定 API)。

nan (Native Abstractions for Node)确实更旧,因此也支持旧版本的 node.js — 回到 node.js 0.8!现在尽管它的作者声称 back in 2017 :

As I mentioned somewhere else, N-API is not meant to be directly used for anything. Where has this notion come from? It is an (effectively internal) low-level infrastructure layer meant to offer ABI stability. There will be another layer on top.

…我在 Node.js 官方附加文档中没有看到太多关于这种效果的警告。也许 this other comment更有见地:

Yes, you should still use NAN for production use. It covers every relevant version of Node.js. Also note that N-API is not intended for end users. You should eventually use https://github.com/nodejs/node-addon-api.

再一次,那是 2017 年 6 月,当时 nan 的维护者。与此同时,node-addon-api 似乎已经成熟并保持活跃。其实我找到了a comment in the -addon-api repo目前只有一个月大:

…part of the goal was to make it easy to transition from nan.

所以我认为答案是:

  • 使用 nan如果您想要成熟且非常向后兼容的东西
  • 使用 node-addon-api如果你想要 C++ 中具有前瞻性的东西
  • 如果您习惯使用 C 语言并处理可能的低级问题,请使用 Node-API/N-API

关于node.js - Node JS 插件 - NAN 与 N-API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54740947/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com