diff options
author | 2022-05-11 19:08:46 -0700 | |
---|---|---|
committer | 2022-05-11 19:08:46 -0700 | |
commit | a15e66cc7beef3a38a9c2f9f5a36b36f199067d3 (patch) | |
tree | 53cf40bec99ea3a75d5d8e8753c8b113198339e6 | |
parent | 3eacfe231468d07aa737f4f2dc5c59ff9b910489 (diff) | |
download | bun-a15e66cc7beef3a38a9c2f9f5a36b36f199067d3.tar.gz bun-a15e66cc7beef3a38a9c2f9f5a36b36f199067d3.tar.zst bun-a15e66cc7beef3a38a9c2f9f5a36b36f199067d3.zip |
Add section about napi
-rw-r--r-- | README.md | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -1895,6 +1895,43 @@ await Bun.write("out.png", png); - callback functions - async functions +### Node-API (napi) + +Bun.js implements 90% of the APIs available in [Node-API](https://nodejs.org/api/n-api.html) (napi). + +You can see the status of [this here](https://github.com/Jarred-Sumner/bun/issues/158). + +Loading Node-API modules in Bun.js works the same as in Node.js: + +```js +const napi = require("./my-node-module.node"); +``` + +You can also use `process.dlopen`: + +```js +var mod = { exports: {} }; +process.dlopen("./my-node-module.node", mod); +``` + +As part of that work, Bun.js also polyfills the [`detect-libc`](npmjs.com/package/detect-libc) package, which is used by many Node-API modules to detect which `.node` binding to `require`. + +This implementation of Node-API is from scratch. It doesn't use any code from Node.js. + +Some implementation details: + +When requiring a `*.node` module, Bun's JavaScript transpiler transforms the `require` expression into call to `import.meta.require`: + +```js +// this is the input +require("./my-node-module.node"); + +// this is the output +import.meta.require("./my-node-module.node"); +``` + +Bun doesn't currently support dynamic requires, but `import.meta.require` is an escape hatch for that. It uses a [JavaScriptCore builtin function](https://github.com/Jarred-Sumner/bun/blob/aa87d40f4b7fdfb52575f44d151906ddba6a82d0/src/javascript/jsc/bindings/builtins/js/JSZigGlobalObject.js#L26). + ### `Bun.Transpiler` `Bun.Transpiler` lets you use Bun's transpiler from JavaScript (available in Bun.js) |