Node.js v18.12.1 documentation


Table of contents

C++ addons#

Addons are dynamically-linked shared objects written in C++. 是用C++编写的动态链接共享对象。The require() function can load addons as ordinary Node.js modules. require()函数可以将插件作为普通Node.js模块加载。Addons provide an interface between JavaScript and C/C++ libraries.插件提供了JavaScript和C/C++库之间的接口。

There are three options for implementing addons: Node-API, nan, or direct use of internal V8, libuv, and Node.js libraries. 实现插件有三种选择:Node API、nan,或者直接使用内部V8、libuv和Node.js库。Unless there is a need for direct access to functionality which is not exposed by Node-API, use Node-API. 除非需要直接访问节点API未公开的功能,否则请使用节点API。Refer to C/C++ addons with Node-API for more information on Node-API.有关节点API的更多信息,请参阅带有节点API的C/C++插件

When not using Node-API, implementing addons is complicated, involving knowledge of several components and APIs:当不使用节点API时,实现插件很复杂,涉及到几个组件和API的知识:

  • V8: the C++ library Node.js uses to provide the JavaScript implementation. :Node.js用于提供JavaScript实现的C++库。V8 provides the mechanisms for creating objects, calling functions, etc. V8提供了创建对象、调用函数等机制。V8's API is documented mostly in the v8.h header file (deps/v8/include/v8.h in the Node.js source tree), which is also available online.V8的API主要记录在v8.h头文件中(Node.js源树中的deps/v8/include/v8.h),该文件也可以在线获得。

  • libuv: The C library that implements the Node.js event loop, its worker threads and all of the asynchronous behaviors of the platform. 实现Node.js事件循环、其工作线程和平台的所有异步行为的C库。It also serves as a cross-platform abstraction library, giving easy, POSIX-like access across all major operating systems to many common system tasks, such as interacting with the filesystem, sockets, timers, and system events. 它还作为一个跨平台抽象库,让所有主要操作系统都可以轻松地访问许多常见的系统任务,例如与文件系统、套接字、计时器和系统事件交互。libuv also provides a threading abstraction similar to POSIX threads for more sophisticated asynchronous addons that need to move beyond the standard event loop. libuv还为需要超越标准事件循环的更复杂的异步插件提供了类似于POSIX线程的线程抽象。Addon authors should avoid blocking the event loop with I/O or other time-intensive tasks by offloading work via libuv to non-blocking system operations, worker threads, or a custom use of libuv threads.插件作者应避免通过libuv将工作卸载到非阻塞系统操作、工作线程或自定义使用libuv线程,从而通过I/O或其他时间密集型任务阻塞事件循环。

  • Internal Node.js libraries. 内部Node.js库。Node.js itself exports C++ APIs that addons can use, the most important of which is the node::ObjectWrap class.Node.js本身导出插件可以使用的C++API,其中最重要的是node::ObjectWrap类。

  • Node.js includes other statically linked libraries including OpenSSL. Node.js包括其他静态链接库,包括OpenSSL。These other libraries are located in the deps/ directory in the Node.js source tree. 这些其他库位于Node.js源树的deps/目录中。Only the libuv, OpenSSL, V8, and zlib symbols are purposefully re-exported by Node.js and may be used to various extents by addons. 只有libuv、OpenSSL、V8和zlib符号被Node.js有目的地重新导出,并且可以被插件使用到不同的范围。See Linking to libraries included with Node.js for additional information.有关更多信息,请参阅Node.js附带的链接库

All of the following examples are available for download and may be used as the starting-point for an addon.以下所有示例均可下载,并可用作插件的起点。