SlideShare a Scribd company logo
Node.js N-API for Rust
Alexey Orlenko
@aqrln
github.com/aqrln/
jsfest2019-napi-rust
N-API is an API for building native
addons that is independent from the
underlying JavaScript runtime and is
maintained as part of Node.js itself. It
also allows modules compiled for one
major version of Node.js to run on
later ones without recompiling.
See https://nodejs.org/dist/latest-v13.x/docs/api/n-api.html
Pure C API allows to
write native addons in
virtually any compiled
programming language,
from C++ and Rust to
OCaml and Haskell.
* although using languages with
managed memory and their own
runtimes might be a bit tricky,
probably
In fact, there’s even an official
C++ wrapper around N-API:
https://github.com/
nodejs/node-addon-api
Rust:
- fast
- modern
- integrates seamlessly with C APIs
with no overhead
- great ecosystem and even greater
community
https://neon-bindings.com
+ stable and mature
+ good documentation
- N-API backend is not ready yet
- and maintaining own C wrappers
for raw C++ Node.js API has certain
drawbacks
N-API
+ can be used directly from Rust
literally the same way one would
use it from C
- too low-level to write application
code directly in terms of its API
+ but flexible enough to build a nice
type-safe Rust API on top of it
If you want to follow the presentation
on your own machine or to tweak with
the code later, clone the repo
(github.com/aqrln/jsfest2019-napi-rust)
and then either (a, b or c):
a) If you use VS Code, install the
Remote Development plugin
(ms-vscode-remote.vscode-remote-
extensionpack) and choose to open
the folder in container.
b) If you don’t use VS Code, but you do
use Docker, run:
$ docker build . -t napi-rust
$ docker run --it -v
$(pwd):/workspaces/napi-rust
napi-rust bash
c) Alternatively, install the required
software on your host machine:
- Node.js
- Rust
- npm install node-gyp benchmark
- cargo install bindgen
Our plan:
1. Get started with using N-API in
general, discuss JavaScript values
representation and error handling
2. Learn how to generate Rust
bindings from C headers
Our plan:
3. Implement type-safe convenience
API on top of N-API
4. Utilize heavy metaprogramming in
form of procedural macros to
eliminate boilerplate code
Our plan:
5. Do some parallel computations and
benchmark what we got
6. Release a new version of
https://crates.io/crates/napi and
pretend it didn’t take almost two
years
Now let’s switch
to the editor
Recap:
- N-API, initially conceived to make
Node.js API VM-agnostic and
forward-compatible (both API and
ABI wise), has a nice side-effect of
allowing to use languages other
than C++ effortlessly
Recap:
- Rust can interoperate with C APIs
directly with no overhead. We only
used a tiny bit of C to integrate
with node-gyp for the sake of
simplicity, but that is not strictly
necessary: Neon has its own build
tool in pure Rust, for example.
Recap:
- Procedural macros in Rust 2018
allowed us to to declare N-API
callbacks with automatic
parameters validation and all
necessary error handling
extremely concisely
(cont)
Rust 2015 only supported procedural
macros that automatically derive
traits for structs, so we had to pack
all parameters of a callback into a
structure and create a wrapper
function with a separate macro
PSA:
Contact me if
- you might want to be a maintainer
of https://crates.io/crates/napi or
- you are learning Rust and you are
looking for a task and a mentor
@aqrln
GitHub/Twitter

More Related Content

PPT
Asp.Net(2)
PDF
Angular v2 et plus : le futur du développement d'applications en entreprise
PDF
OpenDataPlane Testing in Travis
PDF
TDC2018SP | Trilha Containers - CI/CD com Docker e Drone
PDF
[UDS] Cloud Computing "pour les nuls" (Exemple avec LinShare)
PDF
Rabix Benten: A language server for Common Workflow Language
PPTX
PPTX
Lando - AddWeb Solution
Asp.Net(2)
Angular v2 et plus : le futur du développement d'applications en entreprise
OpenDataPlane Testing in Travis
TDC2018SP | Trilha Containers - CI/CD com Docker e Drone
[UDS] Cloud Computing "pour les nuls" (Exemple avec LinShare)
Rabix Benten: A language server for Common Workflow Language
Lando - AddWeb Solution

What's hot (20)

PDF
Benten: An experimental language server for CWL
PDF
Reactive Programming by UniRx for Asynchronous & Event Processing
PPT
Synapse india dotnet framework development or c
PDF
Importing Code and Existing Containers to OpenShift - Minneapolis Docker Meet...
PDF
ОЛЕГ МАЦЬКІВ «Crash course on Operator Framework» Lviv DevOps Conference 2019
PDF
Kompose
PPTX
Swift @ IBM
PDF
Dockerize Spago Self Contained ML & NLP Library & Deploy on Okteto Cloud Usin...
PPTX
Front end development
PDF
CI with Gitlab & Docker
PDF
Docker + Tenserflow + GOlang - Golang singapore Meetup
PDF
KubeCI - Cloud Native Continuous Delivery for Kubernetes
PDF
Elasticsearch Monitoring in Openshift
PDF
Using GitLab CI
PDF
Embedding Chromium into AGL demo platform with WAM
PDF
Dockerfile for rust project
PDF
Docker 101 - From production to development
PDF
Cicd pixelfederation
PDF
From C++ to JS via Emscripten
PDF
Let's Try Every CRI Runtime Available for Kubernetes
Benten: An experimental language server for CWL
Reactive Programming by UniRx for Asynchronous & Event Processing
Synapse india dotnet framework development or c
Importing Code and Existing Containers to OpenShift - Minneapolis Docker Meet...
ОЛЕГ МАЦЬКІВ «Crash course on Operator Framework» Lviv DevOps Conference 2019
Kompose
Swift @ IBM
Dockerize Spago Self Contained ML & NLP Library & Deploy on Okteto Cloud Usin...
Front end development
CI with Gitlab & Docker
Docker + Tenserflow + GOlang - Golang singapore Meetup
KubeCI - Cloud Native Continuous Delivery for Kubernetes
Elasticsearch Monitoring in Openshift
Using GitLab CI
Embedding Chromium into AGL demo platform with WAM
Dockerfile for rust project
Docker 101 - From production to development
Cicd pixelfederation
From C++ to JS via Emscripten
Let's Try Every CRI Runtime Available for Kubernetes
Ad

Similar to JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust (20)

PPTX
Next Generation N-API
PDF
N api-node summit-2017-final
PPTX
N-API NodeSummit-2017
PDF
Rust: Systems Programming for Everyone
PPTX
N api - node interactive 2017
PDF
The Rust Programming Language
PDF
Rust: Reach Further (from QCon Sao Paolo 2018)
PDF
Rust Cologne/Bonn Meetup 29.07.2015
PPTX
Rust Melbourne MeetUp - Rust Web Development
PDF
Short intro to the Rust language
PPTX
Nodejs overview
PDF
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
PPTX
GOSIM 2024 - GenUI: Declarative Rust Cross-platform Framework Based on Makepad
PDF
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
KEY
The Architect Way
PDF
Building a Cross-Platform Mobile SDK in Rust.pdf
PDF
Embedded Rust
PDF
Practical Rust 1x Cookbook Rustacean Team
PDF
Node.js Native AddOns from zero to hero - Nicola Del Gobbo - Codemotion Rome ...
PDF
Errbit & Gitlab
Next Generation N-API
N api-node summit-2017-final
N-API NodeSummit-2017
Rust: Systems Programming for Everyone
N api - node interactive 2017
The Rust Programming Language
Rust: Reach Further (from QCon Sao Paolo 2018)
Rust Cologne/Bonn Meetup 29.07.2015
Rust Melbourne MeetUp - Rust Web Development
Short intro to the Rust language
Nodejs overview
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
GOSIM 2024 - GenUI: Declarative Rust Cross-platform Framework Based on Makepad
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
The Architect Way
Building a Cross-Platform Mobile SDK in Rust.pdf
Embedded Rust
Practical Rust 1x Cookbook Rustacean Team
Node.js Native AddOns from zero to hero - Nicola Del Gobbo - Codemotion Rome ...
Errbit & Gitlab
Ad

More from JSFestUA (20)

PDF
JS Fest 2019/Autumn. Роман Савіцький. Webcomponents & lit-element in production
PDF
JS Fest 2019/Autumn. Erick Wendel. 10 secrets to improve Javascript Performance
PDF
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
PDF
JS Fest 2019/Autumn. Anton Cherednikov. Choreographic or orchestral architect...
PDF
JS Fest 2019/Autumn. Adam Leos. So why do you need to know Algorithms and Dat...
PDF
JS Fest 2019/Autumn. Marko Letic. Saving the world with JavaScript: A Data Vi...
PPTX
JS Fest 2019/Autumn. Александр Товмач. JAMstack
PPTX
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
PPTX
JS Fest 2019/Autumn. Дмитрий Жарков. Blockchainize your SPA or Integrate Java...
PPTX
JS Fest 2019/Autumn. Maciej Treder. Angular Schematics - Develop for developers
PPTX
JS Fest 2019/Autumn. Kyle Boss. A Tinder Love Story: Create a Wordpress Blog ...
PPTX
JS Fest 2019/Autumn. Андрей Старовойт. Зачем нужен тип "true" в TypeScript?
PPTX
JS Fest 2019/Autumn. Eyal Eizenberg. Tipping the Scale
PDF
JS Fest 2019/Autumn. Sota Ohara. Сreate own server less CMS from scratch
PPTX
JS Fest 2019/Autumn. Джордж Евтушенко. Как стать программистом, которого хотят
PDF
JS Fest 2019/Autumn. Daniel Ostrovsky. Falling in love with decorators ES6/Ty...
PPTX
JS Fest 2019/Autumn. Андрей Андрийко. Гексагональна архітектура в Nodejs проекті
PPTX
JS Fest 2019/Autumn. Борис Могила. Svelte. Почему нам не нужно run-time ядро
PPTX
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
PDF
JS Fest 2019. Виктор Турский. 6 способов взломать твое JavaScript приложение
JS Fest 2019/Autumn. Роман Савіцький. Webcomponents & lit-element in production
JS Fest 2019/Autumn. Erick Wendel. 10 secrets to improve Javascript Performance
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JS Fest 2019/Autumn. Anton Cherednikov. Choreographic or orchestral architect...
JS Fest 2019/Autumn. Adam Leos. So why do you need to know Algorithms and Dat...
JS Fest 2019/Autumn. Marko Letic. Saving the world with JavaScript: A Data Vi...
JS Fest 2019/Autumn. Александр Товмач. JAMstack
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Дмитрий Жарков. Blockchainize your SPA or Integrate Java...
JS Fest 2019/Autumn. Maciej Treder. Angular Schematics - Develop for developers
JS Fest 2019/Autumn. Kyle Boss. A Tinder Love Story: Create a Wordpress Blog ...
JS Fest 2019/Autumn. Андрей Старовойт. Зачем нужен тип "true" в TypeScript?
JS Fest 2019/Autumn. Eyal Eizenberg. Tipping the Scale
JS Fest 2019/Autumn. Sota Ohara. Сreate own server less CMS from scratch
JS Fest 2019/Autumn. Джордж Евтушенко. Как стать программистом, которого хотят
JS Fest 2019/Autumn. Daniel Ostrovsky. Falling in love with decorators ES6/Ty...
JS Fest 2019/Autumn. Андрей Андрийко. Гексагональна архітектура в Nodejs проекті
JS Fest 2019/Autumn. Борис Могила. Svelte. Почему нам не нужно run-time ядро
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019. Виктор Турский. 6 способов взломать твое JavaScript приложение

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Complications of Minimal Access Surgery at WLH
PDF
Business Ethics Teaching Materials for college
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Module 4: Burden of Disease Tutorial Slides S2 2025
Complications of Minimal Access Surgery at WLH
Business Ethics Teaching Materials for college
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet

JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust

  • 1. Node.js N-API for Rust Alexey Orlenko @aqrln
  • 3. N-API is an API for building native addons that is independent from the underlying JavaScript runtime and is maintained as part of Node.js itself. It also allows modules compiled for one major version of Node.js to run on later ones without recompiling. See https://nodejs.org/dist/latest-v13.x/docs/api/n-api.html
  • 4. Pure C API allows to write native addons in virtually any compiled programming language, from C++ and Rust to OCaml and Haskell.
  • 5. * although using languages with managed memory and their own runtimes might be a bit tricky, probably
  • 6. In fact, there’s even an official C++ wrapper around N-API: https://github.com/ nodejs/node-addon-api
  • 7. Rust: - fast - modern - integrates seamlessly with C APIs with no overhead - great ecosystem and even greater community
  • 8. https://neon-bindings.com + stable and mature + good documentation - N-API backend is not ready yet - and maintaining own C wrappers for raw C++ Node.js API has certain drawbacks
  • 9. N-API + can be used directly from Rust literally the same way one would use it from C - too low-level to write application code directly in terms of its API + but flexible enough to build a nice type-safe Rust API on top of it
  • 10. If you want to follow the presentation on your own machine or to tweak with the code later, clone the repo (github.com/aqrln/jsfest2019-napi-rust) and then either (a, b or c):
  • 11. a) If you use VS Code, install the Remote Development plugin (ms-vscode-remote.vscode-remote- extensionpack) and choose to open the folder in container.
  • 12. b) If you don’t use VS Code, but you do use Docker, run: $ docker build . -t napi-rust $ docker run --it -v $(pwd):/workspaces/napi-rust napi-rust bash
  • 13. c) Alternatively, install the required software on your host machine: - Node.js - Rust - npm install node-gyp benchmark - cargo install bindgen
  • 14. Our plan: 1. Get started with using N-API in general, discuss JavaScript values representation and error handling 2. Learn how to generate Rust bindings from C headers
  • 15. Our plan: 3. Implement type-safe convenience API on top of N-API 4. Utilize heavy metaprogramming in form of procedural macros to eliminate boilerplate code
  • 16. Our plan: 5. Do some parallel computations and benchmark what we got 6. Release a new version of https://crates.io/crates/napi and pretend it didn’t take almost two years
  • 17. Now let’s switch to the editor
  • 18. Recap: - N-API, initially conceived to make Node.js API VM-agnostic and forward-compatible (both API and ABI wise), has a nice side-effect of allowing to use languages other than C++ effortlessly
  • 19. Recap: - Rust can interoperate with C APIs directly with no overhead. We only used a tiny bit of C to integrate with node-gyp for the sake of simplicity, but that is not strictly necessary: Neon has its own build tool in pure Rust, for example.
  • 20. Recap: - Procedural macros in Rust 2018 allowed us to to declare N-API callbacks with automatic parameters validation and all necessary error handling extremely concisely
  • 21. (cont) Rust 2015 only supported procedural macros that automatically derive traits for structs, so we had to pack all parameters of a callback into a structure and create a wrapper function with a separate macro
  • 22. PSA: Contact me if - you might want to be a maintainer of https://crates.io/crates/napi or - you are learning Rust and you are looking for a task and a mentor