# @hazae41/kcp

> Zero-copy KCP protocol for the web

Latest version **1.1.6** (published 2025-08-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hazae41/kcp
pnpm add @hazae41/kcp
yarn add @hazae41/kcp
bun add @hazae41/kcp
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2025-08-15 |
| First published | 2023-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 76.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | hazae41 |
| Maintainers | hazae41 |
| Keywords | kcp, tcp, udp, smux, protocol, browser, buffer, stream, streaming, zero-copy, packets, binary, encoding, decoding, typescript, esmodules |

## Links

- npm: https://www.npmjs.com/package/@hazae41/kcp
- Repository: https://github.com/hazae41/kcp
- Issues: https://github.com/hazae41/kcp/issues
- npm.io page: https://npm.io/package/@hazae41/kcp

## Dependencies (4)

- [@hazae41/binary](https://npm.io/package/@hazae41/binary.md) ^1.3.11
- [@hazae41/cursor](https://npm.io/package/@hazae41/cursor.md) ^2.0.7
- [@hazae41/future](https://npm.io/package/@hazae41/future.md) ^2.0.1
- [@hazae41/cascade](https://npm.io/package/@hazae41/cascade.md) ^2.2.3

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.1.6 (latest) — 2025-08-15
- 1.1.5 — 2025-08-15
- 1.1.4 — 2025-08-13
- 1.1.3 — 2024-09-01
- 1.1.2 — 2024-03-14
- 1.1.1 — 2024-03-14
- 1.1.0 — 2024-03-11
- 1.0.11 — 2024-03-11
- 1.0.10 — 2024-03-11
- 1.0.9 — 2024-03-10
- 1.0.8 — 2024-03-10
- 1.0.7 — 2024-03-10
- 1.0.6 — 2024-03-10
- 1.0.5 — 2024-03-10
- 1.0.4 — 2024-03-10
- … 49 more at https://npm.io/package/@hazae41/kcp/versions

## README

# KCP

Zero-copy KCP protocol for the web

```bash
npm i @hazae41/kcp
```

[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/kcp)

## Features

### Current features
- 100% TypeScript and ESM
- Zero-copy reading and writing
- Works in the browser

## Usage

```typescript
import { KcpDuplex } from "@hazae41/kcp"
import { Opaque, Writable } from "@hazae41/binary"
import { SuperTransformStream } from "@hazae41/cascade"
import { None } from "@hazae41/kcp"

const conversation = 12345

function pipeToKcp(raw: { outer: ReadableWritablePair<Opaque, Writable> }): { outer: ReadableWritablePair<Opaque, Writable> } {
  const kcp = new KcpDuplex({ conversation })

  raw.outer.readable
    .pipeTo(kcp.inner.writable)
    .catch(() => { })

  kcp.inner.readable
    .pipeTo(raw.outer.writable)
    .catch(() => { })

  return kcp
}

function pipeToDummy(kcp: { outer: ReadableWritablePair<Opaque, Writable> }) {
  const dummy = new Dummy()

  kcp.outer.readable
    .pipeTo(dummy.inner.writable)
    .catch(() => { })

  dummy.inner.readable
    .pipeTo(kcp.outer.writable)
    .catch(() => { })

  return dummy
}

class Dummy extends HalfDuplex<Opaque, Writable> {

  constructor() {
    super()
  }

  send(data: Writable) {
    this.output.enqueue(data)
  }

}

const forward = new TransformStream<Writable, Opaque>({ transform: (chunk, controller) => controller.enqueue(Opaque.writeFromOrThrow(chunk)) })
const backward = new TransformStream<Writable, Opaque>({ transform: (chunk, controller) => controller.enqueue(Opaque.writeFromOrThrow(chunk)) })

const rawA = { outer: { readable: forward.readable, writable: backward.writable } }
const rawB = { outer: { readable: backward.readable, writable: forward.writable } }

const kcpA = pipeToKcp(rawA)
const kcpB = pipeToKcp(rawB)

const dummyA = pipeToDummy(kcpA)
const dummyB = pipeToDummy(kcpB)

dummyB.input.events.on("message", (data) => {
  console.log("b", data.bytes)
  return new None()
})

dummyA.input.events.on("message", (data) => {
  console.log("a", data.bytes)
  return new None()
})

dummyA.send(new Opaque(new Uint8Array([1, 2, 3])))
dummyB.send(new Opaque(new Uint8Array([4, 5, 6])))

```

---
_Source: https://npm.io/package/@hazae41/kcp · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
