update
This commit is contained in:
@@ -500,6 +500,41 @@ The extension acts as the intermediary. The service worker maintains the persist
|
||||
|
||||
The injected API that developers use is the final piece. It creates the BridgeSwarm class, handles all the JavaScript-side event emission, and manages communication with the extension through postMessage. The API is intentionally clean and simple, something that feels familiar to anyone who's used a networking library before.
|
||||
|
||||
## The Runtime: Why Bare Matters
|
||||
|
||||
The native host runs on Bare, a minimal JavaScript runtime that's dramatically smaller than Node.js. This was a deliberate choice for several reasons that affect both the security model and the practical deployment of BridgeSwarm.
|
||||
|
||||
Traditional Node.js clocked in at around sixty megabytes when you counted the runtime, its standard library, and all the dependencies needed to run even a simple application. For a tool that's meant to be installed on end-user machines, that's uncomfortably large. The installation process becomes complicated, updates are slow, and users reasonably question why they need to install an entire development environment just to run a browser extension.
|
||||
|
||||
Bare takes a fundamentally different approach. Instead of bundling everything you might possibly need, Bare provides only the essential primitives: process management, file system access, the network APIs, and a module loader. The entire runtime is under three megabytes. It starts instantly. It has almost no attack surface compared to the sprawling Node.js codebase. When you install BridgeSwarm, you're installing a tiny runtime that does exactly what it needs to do and nothing more.
|
||||
|
||||
The module loading system in Bare deserves special attention. It uses a hyperloader-based system that can load modules from various sources, including npm packages. This means we can use the same packages that work in Node.js, which gave us access to the entire Hyperswarm ecosystem without modification. The Hyperbee key-value store, Hyperdrive file system, Hypercore append-only log, Autobase multi-writer log, and Hyperdb database system all work identically in Bare as they do in Node.js. We get full compatibility with the established P2P stack without sacrificing the lightweight deployment that Bare enables.
|
||||
|
||||
Using Bare also simplifies the dependency story. The native host declares its dependencies in a package.json, Bare resolves those dependencies, and everything just works. There's no need to bundle, tree-shake, or compile anything. The installation script pulls down Bare if it's not present, resolves the package dependencies, and you're ready to go. This makes the installer dramatically simpler than it would be with a bundled Node.js solution.
|
||||
|
||||
The trade-off is that some Node.js APIs aren't available in Bare. If you need something from the extensive Node.js standard library, you might need to find an alternative package or implement it yourself. For BridgeSwarm's purposes, every module we need was either built for universal JavaScript or had a compatible alternative available. We never hit a situation where the Bare choice prevented us from doing something we needed to do.
|
||||
|
||||
One of the most compelling reasons to use Bare is that it runs consistently across platforms. The same JavaScript code that works on macOS works on Linux and Windows without modification. The native host doesn't care about your operating system, it just needs somewhere to run JavaScript. This makes the installation process universal rather than requiring different packages for different platforms.
|
||||
|
||||
### The Modules That Make It Work
|
||||
|
||||
The native host leverages several interconnected modules from the Hyper ecosystem to provide complete P2P functionality.
|
||||
|
||||
Hyperswarm is the networking layer that handles peer discovery and connection establishment. It uses a distributed hash table where peers announce their interest in specific topics. When your application calls join on a topic, Hyperswarm announces to the DHT that you're interested in that topic. Other peers doing the same will be discovered, and Hyperswarm attempts to establish direct connections. This discovery mechanism is entirely decentralized with no central server required.
|
||||
|
||||
Corestore provides the storage foundation that the other data modules build upon. It's essentially a system for managing multiple Hypercore instances, each with their own cryptographic key. When you need to store data in Hyperbee or Hyperdrive, Corestore creates and manages the underlying Hypercore that those systems use. It handles the key management so you don't have to think about it.
|
||||
|
||||
Hypercore is an append-only log, similar to a blockchain but without the proof-of-work consensus. Data is added in sequence, cryptographically linked to previous entries, and can be verified by anyone with the core's public key. It's the fundamental data structure that Hyperbee and Autobase build upon. For P2P applications, Hypercore provides tamper-evident logging that can be replicated between peers.
|
||||
|
||||
Hyperbee builds on Hypercore to provide a B-tree key-value store. Think of it like Redis but distributed and peer-to-peer. You put key-value pairs in, you get them out, and the data replicates automatically between connected peers. The B-tree structure makes lookups efficient even with millions of keys.
|
||||
|
||||
Hyperdrive is a P2P file system built on Hyperbee. You can create files and directories, read and write content, and everything syncs automatically between peers who are interested in the same drive. It's like having a shared filesystem that requires no server, where anyone with the drive key can read and write.
|
||||
|
||||
Autobase is a multi-writer version of Hypercore. Regular Hypercore has a single writer, but Autobase allows multiple peers to append to the same log while maintaining a consistent ordering through a linearization mechanism. This is crucial for collaborative applications where multiple users might make changes simultaneously.
|
||||
|
||||
Hyperdb adds schema and query capabilities on top of these primitives. Rather than just storing raw key-value pairs, you define collections with specific fields. It handles the complexity of replication, conflict resolution, and querying so you can work with a familiar database-like interface while the P2P magic happens underneath.
|
||||
|
||||
|
||||
## How Messages Actually Flow Through the System
|
||||
|
||||
The communication between extension and host uses Chrome's native messaging protocol, which itself is beautifully simple. Each message gets serialized as JSON, prefixed with a four-byte little-endian integer indicating the message length, then written to standard output. The receiving side reads the first four bytes to figure out how many more bytes to read, parses the JSON, and processes it.
|
||||
|
||||
Reference in New Issue
Block a user