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

Node.js Compiler: compiling your Node.js application into a single executable

Node.js Compiler: compiling your Node.js application into a single executable

Node.js Compiler:将您的 Node.js 应用编译成单个可执行文件

https://github.com/pmq20/node-compiler

Minqi Pan

March 01, 2017
Tweet

More Decks by Minqi Pan

Other Decks in Programming

Transcript

  1. Node.js Compiler
    Minqi Pan

    View Slide

  2. I’m Minqi Pan
    one of Node.js Collaborators

    View Slide

  3. before Node.js Compiler

    View Slide

  4. View Slide

  5. View Slide

  6. Slow Installation
    • “npm install fengdie” took 2m12.560s
    • Great-Wall’ed in China
    • Remembered to use npm -g with sudo?

    View Slide

  7. Too much complexities
    • “Should I care about those WARN’s?”
    • “or should I not?”

    View Slide

  8. Node.js Runtime?
    • Making sure your user used the correct Node.js
    • Could multiple Node.js versions co-exist?
    • Wish to use ECMAScript 2017?

    View Slide

  9. after Node.js Compiler

    View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. How to Compile

    View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. How it Works

    View Slide

  29. Introducing
    /__enclose_io_memfs__
    a “mounted” disk in RAM for your project

    View Slide

  30. View Slide

  31. Basic Idea
    System calls on paths starting with

    /__enclose_io_memfs__

    are redirected to the RAM while others to the FS

    View Slide

  32. /__enclose_io_memfs__/… others
    Image Curtesy: Evan-Amos

    View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. But there are so many API’s
    • require, require.resolve
    • fs.readFile, fs.readFileSync, fs.ReadStream
    • fs.readlink, fs.stat, fs.statSync, fs.readlinkSync
    • …

    View Slide

  37. Yet we did this in 8 files only

    View Slide

  38. That’s because…
    https://github.com/pmq20/libsquash
    libsquash
    https://github.com/pmq20/node-compiler
    Node.js Compiler

    View Slide

  39. Introducing SquashFS

    View Slide

  40. SquashFS
    • a compressed read-only file system
    • used by the Live CD versions of Arch
    Linux, Debian, Fedora, Gentoo, Mint,
    Salix, Ubuntu
    • used on OpenWrt and DD-WRT router
    firmware

    View Slide

  41. fengdie’s

    node_modules/
    148M
    fengdie.squashfs
    16M

    View Slide

  42. SquashFS
    • Introduced in 2009 with Linux 2.6.29
    • File format very stabilized
    • Unsquashfs and mksquashfs have win32 ports;
    7-Zip on win32 also supports SquashFS
    • Part of kernel; GPL Licensed

    View Slide

  43. Introducing libsquash

    View Slide

  44. https://github.com/pmq20/libsquash
    libsquash
    • MIT licensed
    • Super embeddable
    • 1 dependency only, which
    could be statically linked
    • Compiles on 3 platforms, even
    Windows XP with VC++ 2010
    • Introduces VFD - virtual file
    descriptor, intercepting
    system calls unobtrusively
    libsquash

    View Slide

  45. API of libsuqash
    mirroring system calls

    View Slide

  46. Virtual File Descriptor
    generated by a duplicating file descriptor 0

    View Slide

  47. Use libsquash unobtrusively
    Just include a header and it’s done!

    View Slide

  48. Use libsquash unobtrusively
    Win32 API works as well

    View Slide

  49. Hard-code an Entrance

    View Slide

  50. node ~/fengdie/bin/cli.js
    (argv[1] preset to /__enclose_io_memfs__/bin/cli.js)
    ./fengdie

    View Slide

  51. fengdie/*

    View Slide

  52. fengdie/*
    fengdie.squashfs
    mksquashfs

    View Slide

  53. fengdie/*
    fengdie.squashfs
    libsquash compile

    View Slide

  54. fengdie/*
    fengdie.squashfs
    libsquash
    Node.js Runtime compile

    View Slide

  55. fengdie/*
    fengdie.squashfs
    libsquash
    Node.js Runtime

    View Slide

  56. fengdie/*
    fengdie.squashfs
    libsquash
    Node.js Runtime

    View Slide

  57. fengdie/*
    fengdie.squashfs
    libsquash
    Node.js Runtime
    fengdie.exe
    Statically Link
    Statically Link

    View Slide

  58. fengdie/*
    fengdie.squashfs
    libsquash
    Node.js Runtime
    fengdie.exe
    Statically Link
    Statically Link

    View Slide

  59. fengdie.exe
    Distribute and Enjoy

    View Slide

  60. Download & Install

    View Slide

  61. nodec is also enclosed
    Operating
    System
    Architecture
    Download Link
    Windows x86
    https://sourceforge.net/projects/
    node-compiler/files/v0.9.4/
    nodec.exe/download
    macOS x86-64
    https://sourceforge.net/projects/
    node-compiler/files/v0.9.4/nodec-
    darwin-x64/download
    Linux x86-64
    https://sourceforge.net/projects/
    node-compiler/files/v0.9.4/nodec-
    linux-x64/download

    View Slide

  62. Install on Windows
    • SquashFS Tools 4.3
    • Python 2.6 or 2.7
    • Visual Studio 2015 Update 3, all editions including
    the Community edition (remember to select
    "Common Tools for Visual C++ 2015" feature
    during installation).
    • Then download the executable nodec.exe and run
    it from the VC++ or VS Command Prompt.

    View Slide

  63. Install on macOS
    • SquashFS Tools 4.3: brew install squashfs
    • Xcode + Command Line Tools
    • Python 2.6 or 2.7
    • GNU Make 3.81 or newer

    View Slide

  64. Install on macOS
    • curl -L https://sourceforge.net/projects/node-
    compiler/files/v0.9.4/nodec-darwin-x64/
    download > nodec
    • chmod +x nodec
    • ./nodec

    View Slide

  65. Install on Linux
    • SquashFS Tools 4.3
    • gcc and g++ 4.8 or newer, or
    • clang and clang++ 3.4 or newer
    • Python 2.6 or 2.7
    • GNU Make 3.81 or newer

    View Slide

  66. Install on Linux
    • curl -L https://sourceforge.net/projects/node-
    compiler/files/v0.9.4/nodec-linux-x64/download
    > nodec
    • chmod +x nodec
    • ./nodec

    View Slide

  67. Licensing

    View Slide

  68. Commercial-license-free

    View Slide

  69. Proprietary-license-free

    View Slide

  70. GPL-free

    View Slide

  71. Super Redistributable
    libsquash MIT
    Node.js MIT
    libuv MIT
    v8 BSD

    View Slide

  72. Super Redistributable
    zlib zlib License
    openssl
    Apache

    4-clause BSD
    HTTP Parser MIT
    c-ares MIT

    View Slide

  73. Applications

    View Slide

  74. Quicker Install / Upgrade
    • Quicker upgrading embedded node.js
    applications / plugins
    • Quicker upgrading embedded node.js
    applications / plugins
    • E.g. fengdie inside the Tea IDE

    View Slide

  75. Enabling Node.js Developers
    • Build softwares instead of modules
    • Distribute to non-technical users
    • Merchandise by building personal / enterprise
    softwares

    View Slide

  76. Code Protection
    • Enable code signing for the entire application;
    guarantee not being altered or corrupted
    • Prevent hacking by users modifying exposed
    Javascript source code
    • Applicable to NW.js applications (DingTalk) and
    Electron applications (Tea)

    View Slide

  77. Tips

    View Slide

  78. Use --tmpdir=/a/fixed/location

    View Slide

  79. Use CentOS release 5.8

    with gcc and g++ 4.8

    (possibly from devtoolset-2 of slc5-devtoolset)

    View Slide

  80. Use Windows

    with a big Virtual Memory

    View Slide

  81. Check dependencies using
    Dependency Walker, otool, ldd

    before releasing

    View Slide

  82. https://github.com/pmq20/node-compiler
    Thank You

    View Slide