This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-zero-copy-tcp-corking-standard

Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking Standard

Enforces zero-copy stream piping and kernel-level socket corking (`TCP_CORK` / `TCP_NOPUSH`) for multi-gigabit reverse proxying and streaming ingest in Bun 1.2+. Prevents high-frequency V8/JSC buffer allocations and micro-chunk fragmentation that starve the event loop under heavy I/O backpressure.

Prohibited Anti-Pattern: Chunk-by-chunk user-space buffer allocation and un-corked streaming socket writes under backpressure

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking Standard - Enforces zero-copy stream piping and kernel-level socket corking (`TCP_CORK` / `TCP_NOPUSH`) for multi-gigabit reverse proxying and streaming ingest in Bun 1.2+. Prevents high-frequency V8/JSC buffer allocations and micro-chunk fragmentation that starve the event loop under heavy I/O backpressure.
globs: src/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-zero-copy-tcp-corking-standard"
---

# Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Chunk-by-chunk user-space buffer allocation and un-corked streaming socket writes under backpressure

## System Constraints
You are a Principal Systems Engineer enforcing Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking invariants. Your objective is to eliminate redundant user-space buffer allocations, prevent packet fragmentation across multi-gigabit workloads, and guarantee backpressure-safe TCP transfers using Bun.serve native streams and socket corking APIs.

## Strict Guidelines
- Engineers MUST ALWAYS use native Web Streams via `Response` piping or `Bun.serve`'s direct zero-copy stream forwarding instead of chunk-by-chunk manual iterations across user space.
- Engineers MUST ALWAYS invoke `socket.cork()` prior to dispatching multi-part framing or headers, and immediately pair it with a guaranteed `socket.uncork()` or callback boundary to leverage kernel-level `TCP_CORK` / `TCP_NOPUSH` batching.
- Engineers MUST NOT convert network streams into intermediate `Uint8Array`, `Buffer`, or `ArrayBuffer` instances inside hot reverse proxy or streaming transfer paths unless payload mutation is strictly required.
- Services MUST monitor socket write return values and handle backpressure explicitly; when `socket.write()` returns `false` or reports blocked bytes, producers MUST await the `drain` event before continuing execution.
- Engineers MUST configure `maxRequestBodySize` and idle TCP timeouts explicitly on all public and internal `Bun.serve` ingress instances to prevent event-loop exhaustion from slowloris and un-flushed corked buffers.

## Bad vs Good Implementations
### HTTP Reverse Proxy Stream Transfer

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Reading stream chunks into intermediate arrays triggers massive garbage collection pressure and forces the kernel to copy payload memory into JavaScript engine heap pages instead of routing socket descriptors via OS-level splice/sendfile paths.
```typescript
import { serve } from "bun";

serve({
  port: 8080,
  async fetch(req) {
    const upstream = await fetch("https://upstream.internal/data");
    if (!upstream.body) return new Response("No body", { status: 502 });

    // ANTI-PATTERN: Iterating over chunks in user-space triggers continuous
    // allocations in JSC heap, stalling the event loop and disabling kernel zero-copy bypass.
    const reader = upstream.body.getReader();
    const chunks: Uint8Array[] = [];
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      chunks.push(value);
    }
    const fullBody = Buffer.concat(chunks);
    return new Response(fullBody, {
      headers: { "content-type": upstream.headers.get("content-type") ?? "application/octet-stream" },
    });
  },
});
```

#### Production Standard (Correct)
> **Standard**: Passing the upstream stream directly into the outgoing Response allows Bun's native runtime to bypass user-space memory allocations entirely, streaming data directly across kernel socket buffers.
```typescript
import { serve } from "bun";

serve({
  port: 8080,
  async fetch(req) {
    const upstream = await fetch("https://upstream.internal/data", {
      headers: { host: "upstream.internal" },
      verbose: false,
    });

    if (!upstream.body) {
      return new Response("Upstream Unavailable", { status: 502 });
    }

    // INVARIANT: Directly pass the ReadableStream to Bun's Response.
    // Bun 1.2 bypasses JavaScript user-space allocations and streams directly
    // between the upstream socket and client socket using zero-copy OS pipes.
    return new Response(upstream.body, {
      status: upstream.status,
      headers: {
        "content-type": upstream.headers.get("content-type") ?? "application/octet-stream",
        "cache-control": "no-transform",
      },
    });
  },
});
```

### Low-Level TCP Socket Streaming with Kernel Corking and Backpressure

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Multiple un-corked socket writes cause TCP packet fragmentation (Nagle algorithm delay or tiny packet generation) and fails to inspect the return code of socket.write, causing memory bloat when downstream sockets stall.
```typescript
import { listen, type Socket } from "bun";

listen({
  hostname: "0.0.0.0",
  port: 9000,
  socket: {
    data(socket: Socket, data: Buffer) {
      // ANTI-PATTERN: Writing small segments immediately without corking emits tiny
      // packets (TCP fragmentation). Ignoring return value causes buffer bloat and OOM.
      socket.write("FRAME_START\n");
      socket.write(`PAYLOAD_LEN:${data.length}\n`);
      socket.write(data);
      socket.write("FRAME_END\n");
    },
  },
});
```

#### Production Standard (Correct)
> **Standard**: Wrapping multi-write sequences in `socket.cork()` sets the kernel `TCP_CORK`/`TCP_NOPUSH` socket option during execution, flushing all frames as a single coalesced TCP segment. It checks write buffer saturation to enforce backpressure handling via `drain`.
```typescript
import { listen, type Socket } from "bun";

interface SocketState {
  isDraining: boolean;
}

listen<SocketState>({
  hostname: "0.0.0.0",
  port: 9000,
  socket: {
    open(socket) {
      socket.data = { isDraining: false };
    },
    data(socket: Socket<SocketState>, chunk: Buffer) {
      if (socket.data.isDraining) {
        // Drop or pause upstream backpressure invariant
        return;
      }

      // INVARIANT: Use cork() to aggregate writes into a single kernel TCP packet
      // using TCP_CORK (Linux) or TCP_NOPUSH (BSD/macOS).
      socket.cork((s) => {
        s.write("FRAME_START\n");
        s.write(`PAYLOAD_LEN:${chunk.length}\n`);
        const flushed = s.write(chunk);
        s.write("FRAME_END\n");

        if (!flushed) {
          s.data.isDraining = true;
        }
      });
    },
    drain(socket: Socket<SocketState>) {
      socket.data.isDraining = false;
    },
  },
});
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking Standard

Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking Standard defines an industrial-grade behavioral standard for backend engineering. Enforces zero-copy stream piping and kernel-level socket corking (`TCP_CORK` / `TCP_NOPUSH`) for multi-gigabit reverse proxying and streaming ingest in Bun 1.2+. Prevents high-frequency V8/JSC buffer allocations and micro-chunk fragmentation that starve the event loop under heavy I/O backpressure. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Chunk-by-chunk user-space buffer allocation and un-corked streaming socket writes under backpressure Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftChunk-by-chunk user-space buffer allocation and un-corked streaming socket writes under backpressureCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS use native Web Streams via `Response` piping or `Bun.serve`'s direct zero-copy stream forwarding instead of chunk-by-chunk manual iterations across user space.

Engineers MUST ALWAYS use native Web Streams via `Response` piping or `Bun.serve`'s direct zero-copy stream forwarding instead of chunk-by-chunk manual iterations across user space.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST ALWAYS invoke `socket.cork()` prior to dispatching multi-part framing or headers, and immediately pair it with a guaranteed `socket.uncork()` or callback boundary to leverage kernel-level `TCP_CORK` / `TCP_NOPUSH` batching.

Engineers MUST ALWAYS invoke `socket.cork()` prior to dispatching multi-part framing or headers, and immediately pair it with a guaranteed `socket.uncork()` or callback boundary to leverage kernel-level `TCP_CORK` / `TCP_NOPUSH` batching.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT convert network streams into intermediate `Uint8Array`, `Buffer`, or `ArrayBuffer` instances inside hot reverse proxy or streaming transfer paths unless payload mutation is strictly required.

Engineers MUST NOT convert network streams into intermediate `Uint8Array`, `Buffer`, or `ArrayBuffer` instances inside hot reverse proxy or streaming transfer paths unless payload mutation is strictly required.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST monitor socket write return values and handle backpressure explicitly

Services MUST monitor socket write return values and handle backpressure explicitly; when `socket.write()` returns `false` or reports blocked bytes, producers MUST await the `drain` event before continuing execution.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Engineers MUST configure `maxRequestBodySize` and idle TCP timeouts explicitly on all public and internal `Bun.serve` ingress instances to prevent event-loop exhaustion from slowloris and un-flushed corked buffers.

Engineers MUST configure `maxRequestBodySize` and idle TCP timeouts explicitly on all public and internal `Bun.serve` ingress instances to prevent event-loop exhaustion from slowloris and un-flushed corked buffers.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Bun 1.2 Zero-Copy TCP Streaming & Kernel Corking Standard critical for AI coding assistants?

Enforces zero-copy stream piping and kernel-level socket corking (`TCP_CORK` / `TCP_NOPUSH`) for multi-gigabit reverse proxying and streaming ingest in Bun 1.2+. Prevents high-frequency V8/JSC buffer allocations and micro-chunk fragmentation that starve the event loop under heavy I/O backpressure. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Chunk-by-chunk user-space buffer allocation and un-corked streaming socket writes under backpressure) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Bun