open-index/open-npm huggingface dataset:
npm package: @lesomnus/channel
Description: TypeScript implementation of Go Channel
Author: Seunghyun Hwang
Latest version: 0.0.1

# channel

[![test](https://github.com/lesomnus/channel-ts/actions/workflows/test.yaml/badge.svg)](https://github.com/lesomnus/channel-ts/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/lesomnus/channel-ts/branch/main/graph/badge.svg?token=sZSDd8Zzd7)](https://codecov.io/gh/lesomnus/channel-ts)

TypeScript implementation of [Go channel](https://go.dev/ref/spec#Channel_types).

## Features

-   Select
-   Async iterators
-   Bounded/Unbounded channels

## Usage

### Send/Recv

```ts
import { Chan } from '@lesomnus/channel'

const c = new Chan<number>(0) // Bounded channel with 0 size buffer.

setTimeout(async () => {
	// Blocked until recv() since the size of the buffer is 0.
	await c.send(42)
}, 100)

// Blocked until send(42).
const answer = await c.recv()

// After 100ms...
answer === 42 // true
```

```go
/* Golang equivalent. */

c := make(chan int)

go func(){ c <- 42 }()

answer := <- c
```

### Select

```ts
import { UnChan } from '@lesomnus/channel'
import { recv, send, select } from '@lesomnus/channel'

const c1 = new Chan<number>(0)
const c2 = new Chan<number>(0)

setTimeout(async () => await c2.send(42), 100)

// After 100ms, "c2 received 42" will be logged.
await select([
	recv(c1, (v) => console.log('c1 received', v)),
	recv(c2, (v) => console.log('c2 received', v)),
])

// This blocks forever because the receive from c1 is effectively cancelled.
await c1.send(36)
```

```go
/* Golang equivalent. */

c1 := make(chan int)
c2 := make(chan int)

go func(){ c2 <- 42 }()

select {
	case v := <- c1: fmt.Println("c1 received", v)
	case v := <- c2: fmt.Println("c2 received", v)
}
```

### Async Iterate

```ts
const c = Chan.from([42, 36], 2)

// Logs
// "received 42"
// "received 36"
// then blocked.
for await (const v of c){
	console.log('received', v)
}
```

```go
/* Golang equivalent. */

c := make(chan int, 2)
c <- 42
c <- 36

for v := range c {
	fmt.Println("received", v)
}
```
npm package: @lesomnus/io
Description: Golang `io`-like I/O interfaces
Author: Seunghyun Hwang
Latest version: 0.1.0

# io

Provides a Golang `io`-like I/O interface with a JS stream API wrapper.

## Usage

```ts
import io from '@lesomnus/io'

io.fetch('http://worldtimeapi.org/api/timezone/Asia/Seoul').then(async r => {
	const b = io.make(512)
	const n = await io.readFull(r, b)
	if (n === null) {
		console.error(r)
		throw new Error('what is wrong?')
	}

	const body = new TextDecoder().decode(b.subarray(0, n).data)
	console.log(body)
	// {"abbreviation":"KST","datetime":"2024-02-27T01:09:43.098317+09:00" ...
})
```
npm package: @lesomnus/vanguard
Description: Signaling with data channel
Author: Seunghyun Hwang
Latest version: 0.0.2

# Vanguard

Make extra connections from the existing connection.

## Usage

```ts
import { SigChannel, SigMessage, offer } from 'vanguard'

class MySigChannel implements SigChannel {
	send(message: SigMessage): void {
		// Implement it.
	}

	close(): void {
		// Implement it.
	}

	get closed(): boolean {
		// Implement it.
	}
}

async function connect() {
	// Initial connection with third-party signalling server.
	const conn = new RTCPeerConnection()
	const peer = await offer(conn, new MySigChannel())

	// Need extra connection?
	// Signaling through existing connection!
	const extra_conn = new RTCPeerConnection()
	const extra_peer = await peer.offer(extra_conn)
}
```

## TODO

- [ ] Provide `SigChannel` for PeerJS.
- [ ] Handle renegotiation.