Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js in Rust: How to do it and what to expect from it

Node.js in Rust: How to do it and what to expect from it

The main goal of this talk is to show that it is possible to embed Node.js runtime in your Rust project and to explain why it can be beneficial.I will guide you through all the crucial steps of embedding Node.js. We will look at the official Node.js C++ embedder API and what we can do with Rust’s FFI despite its lack of C++ support. Much of this is the result of the embedding experiments I did while helping build Orogene - a package manager for Node.js-compatible projects that is written in Rust.

78d39160a56e4a71b1c4a2d0cbfda223?s=128

Kirill Vasiltsov

September 18, 2021
Tweet

Transcript

  1. Node.js in Rust: how to do it and what to

    expect from it Kirill Vasiltcov @virtualkirill
  2. in

  3. Why?

  4. in

  5. in

  6. Why Node.js?

  7. Node.js gives us  Standard library (networking, file system access

    and so on)  Module resolution and more!
  8. Why even bother?

  9. Illustration by Icons 8 from Ouch! (https://icons8.com/illustrations/) Package managers benefit

    from Node.js APIs
  10. None
  11. use node_modules

  12. use node_modules

  13. None
  14. node_modules

  15. Many tools rely on Node.js APIs like require() require() relies

    on…node_modules!
  16. Breaking node_modules breaks important tools

  17. Breaking node_modules breaks important tools

  18. Illustration by Icons 8 from Ouch! (https://icons8.com/illustrations/) Can we do

    better?
  19. require() fs

  20. require() fs

  21. require() fs Content- addressable cache?

  22. Why Rust?

  23. None
  24. None
  25. * Not in active development

  26. Medium-sized project installation memory usage ~300MB → ~30MB

  27. git push CI

  28. git push CI npm install tons-of-packages 💰 RAM CPU 💰

  29. Embedding Node.js in orogene

  30. • Run JavaScript without "shelling out“ • Shim module loading

    (and resolution) logic
  31. • Run JavaScript without "shelling out“ • Shim module loading

    (and resolution) logic 🤔 🤔 Content- addressable cache?
  32. None
  33. None
  34. Node.js has an embedder API

  35. Node.js has an embedder API ...but in C++

  36. FFI Foreign Function Interface

  37. FFI

  38. FFI Only for C!

  39. Fortunately, • Node.js can be compiled as a library •

    Node.js embedder API example has an extremely simple call signature • It is easy to export C functions from C++
  40. for rustc Building Node.js

  41. None
  42. Simple call signature = Easy C wrapper

  43. Inside C++ code

  44. Inside Rust code

  45. Inside Rust code C-style string

  46. Rust build.rs script

  47. Rust build.rs script libnode.so.83

  48. Use the C wrapper from Rust

  49. Use the C wrapper from Rust

  50. None
  51. None
  52. None
  53. None
  54. and type checker…?

  55. https://github.com/jlkiri/nodeinrust

  56. Special Thanks  Kat Marchán  Felipe Seré  Anna

    Henningsen and many others!
  57. Thank you for watching!