Core components
Scramjet consists of four main components that work in harmony:Service Worker layer
The ScramjetServiceWorker is the heart of the proxy. It intercepts all network requests and performs the following tasks:- Intercepts fetch requests through the Service Worker API
- Decodes proxied URLs to real destination URLs
- Fetches content using a transport layer (via
BareClient) - Rewrites responses (HTML, JS, CSS, etc.) before returning them
- Manages cookies through an emulated cookie store
Client layer
The ScramjetClient runs in each proxied page (window or worker). It:- Intercepts DOM APIs and JavaScript APIs to rewrite URLs
- Maintains a reference to the real URL of the current page
- Handles navigation events and URL changes
- Provides utilities for wrapping functions and trapping properties
- Manages event callbacks to ensure proper interception
<head> section.
Controller layer
The ScramjetController is the high-level API for initializing and managing Scramjet. It:- Stores configuration in IndexedDB for persistence
- Provides methods to encode/decode URLs
- Creates
ScramjetFrameinstances for easy iframe management - Dispatches global events like downloads
- Allows runtime configuration updates
Frame abstraction
The ScramjetFrame provides a convenient API for managing proxied iframes:- Automatically handles frame name generation for internal tracking
- Provides navigation methods (
go(),back(),forward(),reload()) - Exposes the
ScramjetClientinstance for the iframe - Dispatches navigation events you can listen to
Request flow
Here’s what happens when a proxied page makes a request:URL encoding
The controller or client encodes the real URL into a proxied URL using the configured codec:
Service Worker interception
The Service Worker’s
fetch event handler intercepts the request. It checks if the URL matches the configured prefix.Fetch via transport
The Service Worker fetches the content using the BareClient transport layer, which handles the actual network request.
Response rewriting
Based on the content type, the Service Worker rewrites the response:
- HTML: Injects Scramjet scripts and rewrites URLs in attributes
- JavaScript: Rewrites using the oxc-based WASM rewriter
- CSS: Rewrites
url()references - Headers: Processes and rewrites cookies, CSP, etc.
Data flow
Scramjet maintains several stores to track state across the proxy:IndexedDB stores
Scramjet uses IndexedDB with the following object stores:- config: Stores the active
ScramjetConfig - cookies: Stores the serialized cookie jar
- redirectTrackers: Tracks redirect chains for referrer policy enforcement
- referrerPolicies: Stores referrer policy data per URL
- publicSuffixList: Caches the public suffix list for cookie domain validation
Message passing
The Service Worker and clients communicate viapostMessage:
scramjet$type field for routing and an optional scramjet$token for request/response pairing.
URL metadata
Throughout the rewriting process, Scramjet uses aURLMeta object to track context:
The
topFrameName and parentFrameName are critical for handling nested iframes correctly. Always use ScramjetFrame to create iframes instead of raw <iframe> elements.Component initialization order
- ScramjetController is instantiated with configuration
- Controller calls
init(), which:- Opens IndexedDB and stores config
- Sends config to the Service Worker
- Sets up message listeners
- ScramjetFrame instances are created as needed
- When a frame loads, ScramjetClient is injected and initialized
- Client hooks DOM and JavaScript APIs
- ScramjetServiceWorker begins intercepting requests for that client
Type definitions
Key interfaces defined insrc/types.ts:
Next steps
Service Worker
Learn how requests are intercepted and proxied
URL rewriting
Understand URL encoding, decoding, and rewriting
Configuration
Explore configuration options and flags
API Reference
Browse the complete API documentation