Architectural Invariant Design: Bun 1.2 Native WebSocket Streaming Backpressure and ArrayBuffer Detachment Guardrail
Bun 1.2 Native WebSocket Streaming Backpressure and ArrayBuffer Detachment Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces non-blocking backpressure flow control and detached buffer safety on high-throughput Bun 1.2 native WebSocket pipelines. Prevents unbounded kernel buffer queueing, silent zero-byte transmissions from reused detached ArrayBuffers, and EventLoop thread starvation under asymmetric client throughput. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
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 Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Unchecked ws.send() Returns Causing Unbounded Memory Growth and Mutating Detached ArrayBuffers in Pipeline | Critical | 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 Inconsistency | AI 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. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS evaluate the return value of `ws.send()` and check `ws.getBufferedAmount()` against an explicit high-water mark (e.g., 64KB) before dispatching binary or text frames.
Engineers MUST ALWAYS evaluate the return value of `ws.send()` and check `ws.getBufferedAmount()` against an explicit high-water mark (e.g., 64KB) before dispatching binary or text frames.
Engineers MUST pause upstream async iterators, streams, or message producers immediately when `ws.send()` returns -1 or when `ws.getBufferedAmount() > HIGH_WATER_MARK`.
Engineers MUST pause upstream async iterators, streams, or message producers immediately when `ws.send()` returns -1 or when `ws.getBufferedAmount() > HIGH_WATER_MARK`.
Engineers MUST resume upstream data production exclusively inside the WebSocket handler's `drain(ws)` hook when `ws.getBufferedAmount()` falls below the low-water mark.
Engineers MUST resume upstream data production exclusively inside the WebSocket handler's `drain(ws)` hook when `ws.getBufferedAmount()` falls below the low-water mark.
Engineers MUST NEVER access or resend an `ArrayBuffer` after passing it to a zero-copy transferable context or transferring it via workers
Engineers MUST NEVER access or resend an `ArrayBuffer` after passing it to a zero-copy transferable context or transferring it via workers; engineers MUST verify `.byteLength !== 0` or allocate defensively if buffers must be retained across frames.
Components NEVER store unsent binary payloads in unbounded unbounded JavaScript arrays, which bypasses V8/JSC memory tracking and causes fatal Out-Of-Memory termination.
Components NEVER store unsent binary payloads in unbounded unbounded JavaScript arrays, which bypasses V8/JSC memory tracking and causes fatal Out-Of-Memory termination.
Services MUST configure `backpressureLimit` and `closeOnBackpressureLimit` within `Bun.serve({ websocket
Services MUST configure `backpressureLimit` and `closeOnBackpressureLimit` within `Bun.serve({ websocket: { ... } })` options to protect the native runtime from hostile slowloris consumers.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Bun 1.2 Native WebSocket Streaming Backpressure and ArrayBuffer Detachment Guardrail critical for AI coding assistants?
Enforces non-blocking backpressure flow control and detached buffer safety on high-throughput Bun 1.2 native WebSocket pipelines. Prevents unbounded kernel buffer queueing, silent zero-byte transmissions from reused detached ArrayBuffers, and EventLoop thread starvation under asymmetric client throughput. 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 (Unchecked ws.send() Returns Causing Unbounded Memory Growth and Mutating Detached ArrayBuffers in Pipeline) 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.