1.1.2 • Published 2 months ago

@hazae41/kcp v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

KCP

Zero-copy KCP protocol for the web

npm i @hazae41/kcp

Node Package 📦

Features

Current features

  • 100% TypeScript and ESM
  • Zero-copy reading and writing
  • Works in the browser

Usage

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])))
1.1.1

2 months ago

1.1.2

2 months ago

1.1.0

2 months ago

1.0.11

2 months ago

1.0.10

2 months ago

1.0.9

2 months ago

1.0.8

2 months ago

1.0.7

2 months ago

1.0.6

2 months ago

1.0.2

2 months ago

1.0.1

2 months ago

1.0.0

2 months ago

1.0.5

2 months ago

1.0.4

2 months ago

0.2.43

3 months ago

0.2.42

3 months ago

0.2.41

3 months ago

0.2.40

5 months ago

0.2.27

10 months ago

0.2.39

5 months ago

0.2.30

8 months ago

0.2.38

5 months ago

0.2.37

6 months ago

0.2.36

6 months ago

0.2.35

7 months ago

0.2.34

7 months ago

0.2.33

7 months ago

0.2.32

7 months ago

0.2.31

8 months ago

0.2.29

8 months ago

0.2.28

10 months ago

0.2.26

11 months ago

0.2.25

11 months ago

0.2.24

11 months ago

0.2.23

11 months ago

0.2.22

11 months ago

0.2.21

11 months ago

0.2.20

11 months ago

0.2.19

11 months ago

0.2.18

11 months ago

0.2.17

11 months ago

0.2.16

11 months ago

0.2.15

11 months ago

0.2.14

11 months ago

0.2.13

11 months ago

0.2.12

11 months ago

0.2.11

11 months ago

0.2.10

11 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.2.7

11 months ago

0.2.6

12 months ago

0.2.9

11 months ago

0.2.8

11 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.5

12 months ago

0.2.4

12 months ago

0.1.1

1 year ago

0.1.0

1 year ago