Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mmis1000/flat-js
flat a bunch of js code into a while loop and switch case
https://github.com/mmis1000/flat-js
Last synced: about 2 months ago
JSON representation
flat a bunch of js code into a while loop and switch case
- Host: GitHub
- URL: https://github.com/mmis1000/flat-js
- Owner: mmis1000
- Created: 2016-11-19T05:10:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T04:20:44.000Z (about 2 years ago)
- Last Synced: 2024-10-02T09:21:57.264Z (4 months ago)
- Language: TypeScript
- Size: 9.53 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Flat js
flow flattening for javascript
================
## Motivation
1. Learn how javascript internally works, and evaluate whether did I learn it correctly.
2. Bypass the csp `eval` restriction with minimal size overhead.
3. Obfuscate codes across all functions for obfuscation purpose
4. Learn how do debuggers work and try to implement it![Example debugger](./example.jpg)
## To run this project
```sh
npm install
npx tsc --watch
```## Usage
The example
```sh
node ./lib/cli.js ./src/__tests__/fixures/loader.js > ./example/loader.js
node ./lib/cli.js --json ./src/__tests__/fixures/bad-code.js > ./example/bad-code.json
node ./lib/cli.js --json ./src/__tests__/fixures/jquery.js > ./example/jquery.json
```## Design
Emulate a javascript interpreter that interpret intermediate product of javascript parser.
1. read code
2. parsing into AST (typescript)
3. extract functions
4. resolve the variable and what scope it belongs to
5. generation instructions from AST
1. offset is not known yet, only the length of instruction)
6. resolving offset of instructions
1. concat the all together and get the offset of the instructions
7. generate opcode and data session from instructions
8. generate output and send to stdout
1. combine with interpreter and interpreted by interpreter
2. or as a json as an payloadThe interpreter didn't execute code directly.
It execute opcodes generated by above process instead.
So the size can be pretty small because you don't need the whole parser to parse JavaScript.