r3x5ur / little-byte

Node.js bytecode compiler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

little-byte

Compile Node.js code into bytecode.

中文文档 | Chinese Docs

typescript npm version Test Suite

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

About

Node.js bytecode compiler

License:MIT License


Languages

Language:TypeScript 67.0%Language:JavaScript 33.0%