browsermesh-netway
v0.1.1
Published
Virtual networking layer with BSD-socket-like abstractions for browser environments
Maintainers
Readme
browsermesh-netway
Virtual networking layer with BSD-socket-like abstractions for browser environments. Provides TCP-like streams, UDP-like datagrams, DNS resolution, and capability-based policy enforcement -- all running in-memory or proxied through a remote gateway server.
Install
npm install browsermesh-netwayOr via CDN:
<script type="module">
import { VirtualNetwork } from 'https://esm.sh/browsermesh-netway';
</script>Quick Start
import { VirtualNetwork, CAPABILITY } from 'browsermesh-netway';
// Create a network (comes with in-memory loopback for mem:// and loop://)
const net = new VirtualNetwork();
// Listen and connect over the loopback backend
const listener = await net.listen('mem://localhost:8080');
const client = await net.connect('mem://localhost:8080');
const server = await listener.accept();
await client.write(new TextEncoder().encode('hello'));
const chunk = await server.read(); // Uint8Array: "hello"
// Scoped policy enforcement
const sandbox = net.scope({ capabilities: [CAPABILITY.LOOPBACK] });
await sandbox.connect('mem://localhost:8080'); // allowed
// sandbox.connect('tcp://example.com:80'); // throws PolicyDeniedError
await net.close();API Overview
Constants & Errors
DEFAULTS-- default configuration valuesCAPABILITY-- capability tags (LOOPBACK,NET,DNS,RAW)GATEWAY_ERROR-- gateway error codesNetwayError-- base error classConnectionRefusedError,PolicyDeniedError,AddressInUseError,QueueFullError,UnknownSchemeError,SocketClosedError,OperationTimeoutError
Core Abstractions
StreamSocket-- reliable ordered byte stream (TCP-like), withcreatePair()for paired socketsDatagramSocket-- unreliable message socket (UDP-like)Listener-- server-side accept queue for incoming connections
Policy & Routing
PolicyEngine-- capability-based access control engineRouter-- address parsing and scheme-to-backend dispatchparseAddress(url)-- parse a URL into{ scheme, host, port }componentsOperationQueue-- offline operation buffer with deferred drain
Backends
Backend-- abstract base class for network backendsLoopbackBackend-- in-memory backend formem://andloop://schemesGatewayBackend-- wsh-proxied backend for real TCP/UDP/DNSServiceBackend--svc://scheme backend using a service registryChaosBackendWrapper-- wraps any backend with fault injection (latency, drops, partitions)FsServiceBackend-- filesystem service routing backend
Network
VirtualNetwork-- top-level facade composing all of the aboveScopedNetwork-- capability-restricted view of aVirtualNetwork
License
MIT
