0.1.4 • Published 2 years ago

little-byte v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

0.1.4

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago