Files
KKRainbow e313ba8efb fix(wasi): reduce startup RSS, align ABI v4 and wire v3 (#2588)
* feat(easytier-go): accept data plane ABI v4

The core raised DATA_PLANE_ABI_VERSION to 4 in f26c2aa1 ("feat(wasi):
run EasyTier core on Cloudflare Workers and browsers") to advertise the
new guest exports easytier_data_plane_tcp_shutdown_write_submit and
easytier_data_plane_tcp_shutdown_write_result_take, which let a host
half-close guest TCP streams.

The Go host has no caller for half-close: net.Conn exposes only Close,
which already tears down both directions, so the v3 behavior is
preserved and no new plumbing is added. Without this bump, any artifact
rebuilt from current core source is rejected at instance creation with
"unsupported EasyTier data plane ABI version 4, want 3".

* fix(easytier-go): decode socket options wire v3, rebuild core artifact

38e2a621 ("refactor(ohos): 拆分 OHRS 包并按 socket 精细保护 VPN 流量")
raised the host socket options wire format from version 2 to 3: a
need_protect byte is appended after the purpose byte in TCP connect,
UDP bind, and TCP listen options, shifting bind_device one byte later.
The Go hostABI decoders were never updated, so every socket operation
from a HEAD-built core was rejected as "invalid options" and required
listeners failed to start.

Bump the accepted wire version to 3 and skip the need_protect byte in
all three decoders. The byte requests VPN socket protection, which only
Android-style VPN hosts can honor; on every other platform sockets are
already protected, so reading and discarding it is correct.

Regenerate the embedded core artifact and protobuf bindings from HEAD
(599e4eac) so the shipped artifact matches the Go host again. The proto
regeneration also picks up schema fields added since the last embed
(e.g. prefer_peer_relay).

* perf(easytier-go): release compiler garbage after host init

wazero's optimizing compiler allocates ~100MB of throwaway state on
the Go heap while compiling the embedded core. Go's runtime does not
return that memory to the OS after the initiating GC, so the process
retained the compilation peak for its entire lifetime: RSS sat at
~164MB before any instance or network activity.

Call debug.FreeOSMemory() once after the module is instantiated.
NewHost is a one-time initialization path outside the dataplane, so
the stop-the-world pass is safe here. Measured RSS after host.New
drops from ~164MB to ~64MB; no behavioral change.

* chore(easytier-js): bump toolchain dependencies

- vitest 2.1.9 -> 3.2.7 (all three packages)
- esbuild 0.25.9 -> 0.28.2 (browser bundler)
- wrangler 4.114.0 -> 4.134.0 (cloudflare + web example)
- @cloudflare/workers-types 5.20260724.1 -> 5.20260917.1
- binaryen 131.0.0 -> 132.0.0 (JS bindings only; the wasm build
  uses the standalone wasm-opt binary fetched by the build script)

Not bumped: typescript stays at 5.9.3 (latest 5.x; 7.0 is a major
jump not worth taking for this workspace), vite stays at 5.4.21
(web-example only; 5->8 spans three majors).

Verified with pnpm test (28 + 2 + 2 tests across runtime, browser,
cloudflare) and pnpm check (tsc + wrangler deploy --dry-run).
pnpm-workspace.yaml gained three minimumReleaseAgeExclude entries
recorded automatically by pnpm for freshly published versions.

* fix(easytier-js): align host wire format with core wire v3

The core socket options wire format moved to version 3 in 38e2a621
(need_protect byte after purpose). The JS websocket-host still
required version 2 and rejected every TCP bind from a HEAD-built core,
breaking browser port leases.

- websocket-host.ts: accept version 3, minimum length 49. The
  need_protect byte sits after purpose (offset 43) and is a no-op
  outside Android VPN hosts, so the decoder just skips it.
- websocket-host.test.ts: update the test encoder to emit v3.
- binaryen stays at 131.0.0 to match script/build-wasi-core.sh, which
  intentionally pins binaryen 131 for the Go-side embedded core.
  The npm binaryen package provides the wasm-opt binary used by
  build-wasm.mjs, so keeping both sides on the same version avoids
  divergent optimization output.
- pnpm-workspace.yaml: drop two stale minimumReleaseAgeExclude entries
  for @cloudflare/workers-types versions no longer in the lockfile.

* test(easytier-go): cover socket options wire v3 decoders

Direct unit tests for decodeTCPConnectOptions, decodeUDPBindOptions, and
decodeTCPListenOptions with wire v3 documents. Covers combinations of
socket mark, netns, bind device, and local address presence, plus
rejection of wire v2.

Resolves review feedback on PR #2588.
2026-09-19 00:13:47 +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.