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

What's hot (20)

PDF
Benten: An experimental language server for CWL
Kaushik (koh-shik) Ghose (gHo-sh)
 
PDF
Reactive Programming by UniRx for Asynchronous & Event Processing
Yoshifumi Kawai
 
PPT
Synapse india dotnet framework development or c
Synapseindiappsdevelopment
 
PDF
Importing Code and Existing Containers to OpenShift - Minneapolis Docker Meet...
Keith Resar
 
PDF
ОЛЕГ МАЦЬКІВ «Crash course on Operator Framework» Lviv DevOps Conference 2019
UA DevOps Conference
 
PDF
Kompose
Suraj Narwade
 
PPTX
Swift @ IBM
Pushkar Kulkarni
 
PDF
Dockerize Spago Self Contained ML & NLP Library & Deploy on Okteto Cloud Usin...
sangam biradar
 
PPTX
Front end development
Chaowlert Chaisrichalermpol
 
PDF
CI with Gitlab & Docker
Joerg Henning
 
PDF
Docker + Tenserflow + GOlang - Golang singapore Meetup
sangam biradar
 
PDF
KubeCI - Cloud Native Continuous Delivery for Kubernetes
Tobias Schneck
 
PDF
Elasticsearch Monitoring in Openshift
Lukas Vlcek
 
PDF
Using GitLab CI
ColCh
 
PDF
Embedding Chromium into AGL demo platform with WAM
Igalia
 
PDF
Dockerfile for rust project
Hien Nguyen
 
PDF
Docker 101 - From production to development
Raül Pérez
 
PDF
Cicd pixelfederation
Juraj Hantak
 
PDF
From C++ to JS via Emscripten
TomNickson
 
PDF
Let's Try Every CRI Runtime Available for Kubernetes
Phil Estes
 
Benten: An experimental language server for CWL
Kaushik (koh-shik) Ghose (gHo-sh)
 
Reactive Programming by UniRx for Asynchronous & Event Processing
Yoshifumi Kawai
 
Synapse india dotnet framework development or c
Synapseindiappsdevelopment
 
Importing Code and Existing Containers to OpenShift - Minneapolis Docker Meet...
Keith Resar
 
ОЛЕГ МАЦЬКІВ «Crash course on Operator Framework» Lviv DevOps Conference 2019
UA DevOps Conference
 
Kompose
Suraj Narwade
 
Swift @ IBM
Pushkar Kulkarni
 
Dockerize Spago Self Contained ML & NLP Library & Deploy on Okteto Cloud Usin...
sangam biradar
 
Front end development
Chaowlert Chaisrichalermpol
 
CI with Gitlab & Docker
Joerg Henning
 
Docker + Tenserflow + GOlang - Golang singapore Meetup
sangam biradar
 
KubeCI - Cloud Native Continuous Delivery for Kubernetes
Tobias Schneck
 
Elasticsearch Monitoring in Openshift
Lukas Vlcek
 
Using GitLab CI
ColCh
 
Embedding Chromium into AGL demo platform with WAM
Igalia
 
Dockerfile for rust project
Hien Nguyen
 
Docker 101 - From production to development
Raül Pérez
 
Cicd pixelfederation
Juraj Hantak
 
From C++ to JS via Emscripten
TomNickson
 
Let's Try Every CRI Runtime Available for Kubernetes
Phil Estes
 

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

PPTX
Nodejs Native Add-Ons from zero to hero
Nicola Del Gobbo
 
PPTX
Next Generation N-API
Nicola Del Gobbo
 
PDF
Node.js Native AddOns from zero to hero - Nicola Del Gobbo - Codemotion Rome ...
Codemotion
 
PPTX
Nodejs overview
Nicola Del Gobbo
 
PPTX
N api - node interactive 2017
Michael Dawson
 
PPTX
Advance Android Application Development
Ramesh Prasad
 
PDF
C++ in our world
Juraj Michálek
 
PPTX
Getting started with the NDK
Kirill Kounik
 
PDF
Node js first look - 2016
Yauheni Nikanovich
 
PPTX
GOSIM 2024 - Porting Servo to OpenHarmony
GOSIM Foundation
 
PDF
Getting started with .net on openSUSE (asp.net with c#)
Alin Alifah
 
PDF
Android Native Development Kit
Peter R. Egli
 
PDF
Mac ruby deployment
Thilo Utke
 
PPTX
Android NDK
Sentinel Solutions Ltd
 
PDF
White Paper : ASP.NET Core AngularJs 2 and Prime
Hamida Rebai Trabelsi
 
PDF
Mobile development in 2020
Bogusz Jelinski
 
PPTX
Android ndk
Sentinel Solutions Ltd
 
PDF
WebAssembly - czy dzisiaj mi się to przyda do pracy?
Brainhub
 
PDF
An introduction to workflow-based programming with Node-RED
Boris Adryan
 
PPTX
A glance at the Rust SWC
Thien Ly
 
Nodejs Native Add-Ons from zero to hero
Nicola Del Gobbo
 
Next Generation N-API
Nicola Del Gobbo
 
Node.js Native AddOns from zero to hero - Nicola Del Gobbo - Codemotion Rome ...
Codemotion
 
Nodejs overview
Nicola Del Gobbo
 
N api - node interactive 2017
Michael Dawson
 
Advance Android Application Development
Ramesh Prasad
 
C++ in our world
Juraj Michálek
 
Getting started with the NDK
Kirill Kounik
 
Node js first look - 2016
Yauheni Nikanovich
 
GOSIM 2024 - Porting Servo to OpenHarmony
GOSIM Foundation
 
Getting started with .net on openSUSE (asp.net with c#)
Alin Alifah
 
Android Native Development Kit
Peter R. Egli
 
Mac ruby deployment
Thilo Utke
 
White Paper : ASP.NET Core AngularJs 2 and Prime
Hamida Rebai Trabelsi
 
Mobile development in 2020
Bogusz Jelinski
 
WebAssembly - czy dzisiaj mi się to przyda do pracy?
Brainhub
 
An introduction to workflow-based programming with Node-RED
Boris Adryan
 
A glance at the Rust SWC
Thien Ly
 
Ad

More from JSFestUA (20)

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

Recently uploaded (20)

PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 

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