This is a web-based JavaScript playground that allows users to execute JavaScript code in a safe, isolated environment. It provides an interactive coding interface using the Monaco Editor and displays execution results, including output, execution time, and memory usage. The backend is built using Node.js with Express, and code execution is isolated using the isolated-vm
library (with an optional Web Worker option).
In addition, the project integrates various data structure definitions to help with common coding problems. These definitions include:
- ListNode: A linked list node structure with methods for traversing the list and creating lists from arrays.
- TreeNode: A binary tree node structure with methods for creating binary trees from arrays and performing various tree traversals (BFS, DFS).
These data structures are exposed to the user within the virtual environment, providing the flexibility to work with linked lists and trees in the executed code.
- Monaco Editor: Provides an interactive coding environment with syntax highlighting and autocompletion for JavaScript.
- Isolated Execution: Code is executed in a secure, isolated environment using
isolated-vm
to prevent malicious actions. You can also choose to run the code using a Web Worker for better performance and separation. - Rate Limiting: Limits the number of requests per IP to prevent abuse (applies in production).
- Performance Monitoring: Tracks execution time and memory usage for each code execution.
- Tree Visualization: Supports visualizing data structures like trees, if applicable in the code output.
Ensure you have the following installed on your system:
- Node.js (>= 16.0)
- npm (Node Package Manager)
-
Clone the repository:
git clone https://github.com/madhuragrawal/js-playground.git cd js-playground
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and set the necessary environment variables. Example:PORT=3005 NODE_ENV=development ENABLE_EXECUTE_API=true
ENABLE_EXECUTE_API=true
: Set this variable to true to enable the isolated-vm API. If set to false, the isolated-vm API will be disabled, and the code execution will default to using Web Worker for isolation instead.
-
Start the development server:
npm run dev
This will start the server using
nodemon
, which automatically restarts the server when changes are made.Alternatively, for production:
npm start
- The front-end is built with standard HTML, CSS, and JavaScript.
- Monaco Editor is used as the code editor for the user interface.
- When the user clicks the "Run" button, the code is sent to the backend via a POST request (
/execute
route). - The output is displayed in the
output-container
, along with the execution time and memory usage. - The tree visualization feature extracts specific data from the code output and renders a tree structure if applicable.
- The backend is built with Express, which handles incoming API requests.
- Code execution is isolated using the
isolated-vm
library to prevent unsafe operations like accessingprocess
,require
, or executing system commands. - The
/execute
API route accepts JavaScript code, validates it, executes it in a secure environment, and returns the results (including output, execution time, and memory usage).
- Code Validation: Before executing user-provided code, the system checks for unsafe patterns like
require
,process
,child_process
, andexec
to prevent malicious actions. - Isolated Execution: The code runs inside a separate VM environment with limited memory and resources, ensuring that it cannot affect the server or other users.
- To prevent abuse, rate limiting is applied on the
/execute
route in production. This limits each IP address to 5 requests per minute.
This endpoint allows the user to execute JavaScript code.
{
"code": "console.log('Hello World')"
}
{
"output": "Hello World",
"executionTime": "50.34 ms",
"memoryUsed": "1.23 MB"
}
{
"error": "Invalid code: unsafe operations are not allowed."
}
If the user exceeds the rate limit, they will receive the following response:
{
"error": "Too many requests from this IP, please try again later."
}
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Create a new Pull Request.
This project uses the following third-party libraries:
- Monaco Editor: A fast, feature-rich code editor for the web.
- Isolated VM: A high-performance virtual machine for running untrusted JavaScript code.
- Express: Web framework for Node.js.
- Rate Limit: Rate limiting middleware for Express.