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.

Kirill Vasiltsov

September 18, 2021
Tweet

More Decks by Kirill Vasiltsov

Other Decks in Programming

Transcript

  1. Node.js in Rust:
    how to do it and what to
    expect from it
    Kirill Vasiltcov @virtualkirill

    View Slide

  2. in

    View Slide

  3. Why?

    View Slide

  4. in

    View Slide

  5. in

    View Slide

  6. Why
    Node.js?

    View Slide

  7. Node.js gives us
     Standard library (networking,
    file system access and so on)
     Module resolution
    and more!

    View Slide

  8. Why even bother?

    View Slide

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

    View Slide

  10. View Slide

  11. use node_modules

    View Slide

  12. use node_modules

    View Slide

  13. View Slide

  14. node_modules

    View Slide

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

    View Slide

  16. Breaking
    node_modules
    breaks important
    tools

    View Slide

  17. Breaking
    node_modules
    breaks important
    tools

    View Slide

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

    View Slide

  19. require()
    fs

    View Slide

  20. require()
    fs

    View Slide

  21. require()
    fs
    Content-
    addressable
    cache?

    View Slide

  22. Why Rust?

    View Slide

  23. View Slide

  24. View Slide

  25. * Not in active development

    View Slide

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

    View Slide

  27. git push CI

    View Slide

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

    View Slide

  29. Embedding Node.js
    in orogene

    View Slide

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

    View Slide

  31. • Run JavaScript without "shelling out“
    • Shim module loading (and resolution)
    logic
    🤔
    🤔
    Content-
    addressable
    cache?

    View Slide

  32. View Slide

  33. View Slide

  34. Node.js has an embedder API

    View Slide

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

    View Slide

  36. FFI
    Foreign
    Function
    Interface

    View Slide

  37. FFI

    View Slide

  38. FFI
    Only for C!

    View Slide

  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++

    View Slide

  40. for rustc
    Building Node.js

    View Slide

  41. View Slide

  42. Simple call signature
    = Easy C wrapper

    View Slide

  43. Inside C++ code

    View Slide

  44. Inside Rust code

    View Slide

  45. Inside Rust code
    C-style string

    View Slide

  46. Rust build.rs script

    View Slide

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

    View Slide

  48. Use the C wrapper from Rust

    View Slide

  49. Use the C wrapper from Rust

    View Slide

  50. View Slide

  51. View Slide

  52. View Slide

  53. View Slide

  54. and type checker…?

    View Slide

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

    View Slide

  56. Special Thanks
     Kat Marchán
     Felipe Seré
     Anna Henningsen
    and many others!

    View Slide

  57. Thank you for watching!

    View Slide