This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Rust-based Node.js native addon that enables running ASGI-compatible Python applications within Node.js environments. It uses NAPI-RS to create Node.js bindings for Rust code that integrates with Python via PyO3.
# Install dependencies
npm install
# Build release version for current platform
npm run build
# Build debug version
npm run build:debug
# Run tests
npm test
# Lint code
npm run lintThe project implements a complete ASGI 3.0 server that bridges Node.js, Rust, and Python:
Core Components:
- Rust Backend (
/src/lib.rs): Main NAPI addon exposingPythonHandlerclass to JavaScript - ASGI Implementation (
/src/asgi/mod.rs): Complete ASGI protocol handler with HTTP, WebSocket, and Lifespan support - Protocol Modules (
/src/asgi/): Individual ASGI message types and connection scopes - Node.js Interface (
/index.js,/index.d.ts): Auto-generated NAPI bindings
Key Architecture Details:
- Uses PyO3 with
pyo3-async-runtimesfor Python async integration - Supports Python virtual environments via
VIRTUAL_ENVenvironment variable - Implements tokio runtime management for async Python code execution
- Cross-platform builds for macOS (arm64, x64) and Linux (x64-gnu)
ASGI Support:
- Full ASGI 3.0 specification implementation
- HTTP request/response handling with streaming support
- WebSocket connections (implementation in progress)
- Lifespan management for application startup/shutdown
Python Environment:
- Automatically detects and uses Python virtual environments
- Dynamically discovers Python site-packages directories
- Supports uvloop for improved async performance when available
- Handles Python symbol loading on Linux systems
Test Framework: Node.js built-in test runner Test Files:
/test/handler.test.mjs: Core ASGI functionality, HTTP methods, headers, error handling/test/concurrency.test.mjs: Concurrent request processing and performance testing/test/fixtures/: Python ASGI test applications for various scenarios
Test Applications:
main.py: Basic "Hello, world!" ASGI appecho_app.py: Request echo service with headers and body reflectionstatus_app.py: HTTP status code testingstream_app.py: Chunked response streamingerror_app.py: Exception handling testing
Async Runtime Management:
- Uses
pyo3_async_runtimes::tokioto bridge Rust tokio and Python asyncio - Event loop setup with uvloop preference for performance
- Handles Python coroutine execution within tokio runtime context
Request/Response Flow:
- Node.js HTTP request → Rust
Requeststruct - ASGI scope creation with HTTP connection details
- Python ASGI app invocation with scope, receive, send callables
- Async message passing between Rust and Python
- Response assembly from ASGI send messages
Error Handling:
- Comprehensive
HandlerErrorenum covering IO, Python, and HTTP errors - Proper error propagation from Python exceptions to Node.js
- Graceful handling of malformed ASGI responses
Key Rust Dependencies:
pyo3(0.25.1): Python integration with experimental async supportpyo3-async-runtimes(0.25.0): Tokio-Python async runtime bridgetokio(1.45.1): Async runtime with full feature setnapi(3.0.0-beta.8): Node.js addon interface
Git Dependencies:
http-handler: HTTP request/response primitiveshttp-rewriter: HTTP request transformation utilities
This is a production-ready implementation with:
- ✅ Complete ASGI 3.0 protocol implementation
- ✅ Full HTTP request/response handling
- ✅ Async Python code execution
- ✅ Virtual environment support
- ✅ Comprehensive test coverage
- ✅ Cross-platform builds
- ✅ Performance optimizations with uvloop
Build System: Uses PNPM for Node.js dependencies and Cargo for Rust compilation. License: MIT (package.json) / Apache-2.0 (Cargo.toml) - dual licensed