Napa.js follows Node.js' convention to support modules, that means:
- Both JavaScript modules and C++ modules are supported.
- Module resolution follows the same algorithm, except instead of searching file extension
.node
for addons, Napa.JS searches.napa
. - Supports NPM, with the same way to create and publish modules.
- API of creating C++ modules (addons) are similar. Napa.JS introduced macros that the same source code can be compiled to produce both Napa.js addon and Node.js addon.
But there are also differences:
- C++ module that is designed/implemented for Napa.js can run on Node.JS (need different compile flags to produce '.napa' and '.node'). But not vice versa.
- Napa.js doesn't support all Node.js API. Node API are supported incrementally on the motivation of adding Node.js built-ins and core modules that are needed for computation heavy tasks. You can access full capabilities of Node exposed via Node zone.
- Napa.js doesn't provide
uv
functionalities, thus built-ins and core modules have its own implementation. To write async function in addon, methodsDoAsyncWork
/PostAsyncWork
are introduced to work for both Napa.js and Node.js. - Napa.js supports embed mode. C++ modules need separate compilation between Node mode and embed mode.
A quick glance at NPM will reveal that most modules are pure JavaScript. These are only a few reasons that you may want to create a C++ module.
- You want to expose JavaScript API for existing C/C++ functionalities.
- Code includes considerably amount of computation that is performance critical.
- Objects need to be shared across multiple JavaScript threads, marshalling/unmarshalling cost on these objects is not trivial (big payload size, complex structure, etc.), but it's reasonable cheap to expose JavaScript APIs from underlying native objects.
- In embed mode, you want to communicate with host process with native objects.
This post gives a good introduction on creating a JavaScript module. For creating a Napa.JS C++ module, please refer to the API section or checkout examples in the quick reference section.
Description | Transportable | Example code |
---|---|---|
Standard JavaScript module | Blog post | |
Share JavaScript object across isolates | X |
Description | ObjectWrap | Transportable | Async function | Example code |
---|---|---|---|---|
Export JavaScript function only | hello-world [.md .cpp test] | |||
Export JavaScript object (ObjectWrap) | X | plus-number [.md .cpp test] | ||
Share C++ object across isolates | X | X | allocator-wrap [.h .cpp] | |
Export asynchronous JavaScript function | X | X | async-number [.md .cpp test] |
See API reference.
TBD
TBD
TBD
TBD
TBD
TBD