little-byte

Node.js bytecode compiler

MIT License

Downloads
49
Stars
47
Committers
2

little-byte

Compile Node.js code into bytecode.

中文文档 | Chinese Docs

Install

$ npm install --save-dev little-byte

Compile App

Prepare Build Script

Create build/index.js

const { walker } = require('little-byte').default;
const path = require('path');

walker.start({
  inputDir: path.join(__dirname, '../src'),
  outputDir: path.join(__dirname, '../dist'),
  onFile(fileInfo, defaultAction) {
    if (fileInfo.relativePath.startsWith('foobar/')) {
      return 'ignore';
    }

    if (fileInfo.ext === '.jpg') {
      return 'ignore';
    }

    if (fileInfo.name === 'my-dog.txt') {
      return 'ignore';
    }

    if (fileInfo.isScript) {
      return 'compile';
    } else {
      // copy none-js files to dist folder
      return 'copy';
    }
  }
});

Build

$ node build/index.js

Run Compiled App

Create app-entry.js


require('little-byte');

// now you can require *.bytecode
require('./dist/index');

Limitations

Using same Node.js version for building and running bytecode

The format of bytecode might change over Node.js versions.

Bytecode does not protect constant values

It's possible to recover constant strings from bytecode with hex editor.

API


littleByte.compiler.compileFile(filePath: string, outputDir?: string): Promise<void>

littleByte.loader.loadBytecode(filePath: string): vm.Script;

littleByte.loader.execByteCode(filePath: string): any;


type WalkAction = 'ignore' | 'compile' | 'copy';

type FileInfo = {
    path: string;
    relativePath: string;
    name: string;
    ext: string;
    isScript: boolean;
};

interface WalkOptions {
    silent?: boolean;
    inputDir: string;
    outputDir: string;
    onFile: (fileinfo: FileInfo, defaultAction: WalkAction) => WalkAction;
}

littleByte.walker.start(options: WalkOptions): Promise<void>;

Related Articles

Principles of protecting Node.js source code through bytecode

Source code and Bytecode performance test

Node.js bytecode source related issues completed

Powered By Google Translate