Files
EasyTier/easytier-js/cloudflare
KKRainbow f26c2aa147 feat(wasi): run EasyTier core on Cloudflare Workers and browsers (#2548)
* fix(core): normalize secure keys for TOML instances

* feat(wasi): run core behind Cloudflare WebSockets

Introduce the Cloudflare Worker WASI host that runs the EasyTier core
behind host-upgraded WebSockets.

- Worker package scaffold (wrangler Durable Object, build-wasm script,
  vitest config) and core-runtime/websocket-host/data-plane runtime.
- WASI host WebSocket tunnel ABI (imports, adapter, runtime exports)
  with bounded receive memory and bounded admission queue.
- Route host sockets through the portable listener plan
  (HostListenerRegistration, listener queue, admission handler split).
- Build the WASM guest with the aes-gcm feature so secure peer
  sessions have their cipher available.

* feat(wasi): add outbound browser client runtime

Add the outbound-only WASI runtime and browser connector host so
browser pages can dial EasyTier peers through WebSocket relays.

- CoreConnectivityMode::{OutboundOnly, InboundOnly} gating for
  listeners, discovery, and direct connectivity modules.
- ExternalTunnelConnector plumbing through composite/connector_host/
  manual for browser WebSocket dials.
- Browser/Node smoke entries with shared helpers
  (smoke-shared.ts).

* feat(wasi): extend browser data plane with TCP half-close

Add the data-plane pieces the browser runtime needs for full-duplex
TCP streams behind host WebSockets:

- Guest TCP shutdown_write operation with submit/take ABI pair
  (DATA_PLANE_ABI_VERSION 3 -> 4) and smoltcp half-close support.
- Worker data-plane TCP listener/stream plumbing and core-runtime
  listener registration.
- Unit coverage for the new session ops and listener wiring.

* refactor(wasi): make host tunnel ABI transport-neutral

Replace WebSocket-specific core and WASI boundaries with a
message-oriented Host Tunnel interface. Keep WebSocket framing and text
rejection in the Cloudflare host while preserving payload boundaries,
ownership, cancellation, backpressure, and EOF behavior.

Rename feature flags and guest imports and exports to the Host Tunnel
ABI. Update both Worker profiles, tests, and architecture documentation.

* feat(web): split WASI hosts into publishable npm packages

Extract the shared JSPI, WASI, Host Tunnel, and data-plane runtime
into @easytier/runtime. Keep ABI handles, guest memory, TOML, and
operation broker details behind its adapter entry point.

Add typed, auto-starting @easytier/browser and factory-based
@easytier/cloudflare packages. Ship a matching Wasm profile with
each platform package and validate its capabilities before packing.

Persist Cloudflare instance identity in Durable Object storage,
centralize WebSocket admission ownership, and add package-level
coverage for the public interfaces.

* fix(web): make public packages portable

Embed the browser Wasm artifact in the published JavaScript entry
point. This lets esbuild consumers bundle the package without an asset
loader or a copied file.

Return Cloudflare's nominal Durable Object base type and document the
named subclass export required by generated Wrangler bindings.

* docs(web): add public package walkthrough

Expand both package READMEs with installation, configuration, local
validation, health checks, and deployment instructions.

Add a standalone Vite and Wrangler example that imports only the
public Browser and Cloudflare entries. Generate Worker bindings from
configuration and keep local secrets outside version control.

* chore(go): import EasyTier Go host

Add the standalone Go host runtime as a monorepo subtree without
carrying its development branch ancestry.

Preserve its API, tests, examples, generated protobuf bindings, and
embedded WASI artifacts.

* refactor(hosts): colocate Go and JavaScript runtimes

Move the browser, Cloudflare, shared runtime, and web example into
the easytier-js subtree. Update workspace metadata, build paths, and
documentation for the new layout.

Adopt github.com/EasyTier/EasyTier/easytier-go as the Go module path.
Resolve artifact and protobuf generation from the enclosing monorepo.

* build(web): isolate JavaScript host workspace

Keep public browser and Cloudflare packages outside the legacy frontend
workspace so root installs and cross-platform builds do not pull workerd.

Make each package build generate its required WASI artifact from a clean
checkout. Add a dedicated workflow that runs the same install and check
commands documented for contributors.

Move JavaScript dependencies into a scoped lockfile and restore the root
workspace lockfile to its pre-host state.
2026-09-06 13:35:02 +08:00
..

@easytier/cloudflare

Run an inbound EasyTier relay in a Cloudflare Durable Object. The package includes the matching EasyTier WebAssembly artifact and owns WebSocket admission, Guest lifecycle, and request routing.

Install

pnpm add @easytier/cloudflare
pnpm add --save-dev wrangler

Create the Worker

import { createEasyTierCloudflare } from "@easytier/cloudflare";

const easytier = createEasyTierCloudflare<Env>({
  namespace: (env) => env.EASYTIER_CORE,
  config: (env) => ({
    networkName: "office",
    networkSecret: env.EASYTIER_NETWORK_SECRET,
    instanceName: "edge-relay",
    encryption: true,
  }),
});

export class EasyTierCoreObject extends easytier.DurableObject {}
export default easytier;

Env is generated from wrangler.jsonc by wrangler types; applications do not need to maintain a parallel binding interface by hand.

Configure Wrangler

{
  "$schema": "node_modules/wrangler/config-schema.json",
  "name": "easytier-relay",
  "main": "src/worker.ts",
  "compatibility_date": "2026-09-05",
  "compatibility_flags": ["nodejs_compat"],
  "secrets": {
    "required": ["EASYTIER_NETWORK_SECRET"]
  },
  "durable_objects": {
    "bindings": [
      {
        "name": "EASYTIER_CORE",
        "class_name": "EasyTierCoreObject"
      }
    ]
  },
  "migrations": [
    {
      "tag": "v1",
      "new_sqlite_classes": ["EasyTierCoreObject"]
    }
  ]
}

Generate the environment type after changing the configuration:

pnpm wrangler types

For local development, put the secret in an untracked .dev.vars file:

EASYTIER_NETWORK_SECRET=replace-with-a-local-secret

Then start the Worker and check the EasyTier Instance:

pnpm wrangler dev --local
curl http://127.0.0.1:8787/health

The health response contains only the public Instance state and connection count:

{"ok":true,"state":"running","connections":0}

Set the production secret interactively before the first deployment:

pnpm wrangler secret put EASYTIER_NETWORK_SECRET
pnpm wrangler deploy

createEasyTierCloudflare() returns both the Durable Object base class and a fetch handler. The one-line named subclass gives Wrangler a concrete class and type to bind. Pass a custom objectName string or callback to route independent EasyTier networks to different named objects; the default is primary.

The Cloudflare Adapter is an inbound-only relay. Its public Interface does not expose TOML, WebAssembly, Host Tunnel handles, JSPI scheduling, or the socket admission sequence. GET /health returns only the Instance state and active connection count. Other non-WebSocket paths return 404.

The Durable Object intentionally uses the standard WebSocket API rather than hibernation. EasyTier's Wasm memory, Tokio executor, and peer graph are in-memory state and cannot be reconstructed from socket attachments alone.

For local development in the EasyTier repository:

cd easytier-js
pnpm install
pnpm --filter @easytier/web-example build:packages
pnpm --filter @easytier/web-example dev:cloudflare

See the complete Browser-to-Cloudflare walkthrough in easytier-js/examples/web.