Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maks11060/deno-protocols
Implementation of simple protocols in typescript
https://github.com/maks11060/deno-protocols
deno stun typescript upnp websocket
Last synced: about 1 month ago
JSON representation
Implementation of simple protocols in typescript
- Host: GitHub
- URL: https://github.com/maks11060/deno-protocols
- Owner: MAKS11060
- Created: 2024-08-12T02:33:42.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-20T05:01:12.000Z (5 months ago)
- Last Synced: 2024-10-13T08:20:20.570Z (3 months ago)
- Topics: deno, stun, typescript, upnp, websocket
- Language: TypeScript
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Implementation of simple protocols in typescript
## [UPnP Client (RFC 6970)](https://datatracker.ietf.org/doc/html/rfc6970)
The UPnP Client provides a simple interface for managing network port mappings.
### Methods
- `getExternalIp()`: Returns the public IP address.
- `setMapping(options)`: Opens a port with the specified options.
- `getMapping()`: Returns a list of all current port mappings.
- `unmap(options)`: Removes a port mapping with the specified options.
- `unmapAll()`: Removes all port mappings.Usage:
```ts
#!/usr/bin/env -S deno run -A --unstable-netimport {UPnP} from 'https://raw.githubusercontent.com/MAKS11060/deno-protocols/main/upnp/upnp.ts'
const upnp = new UPnP()
// Get public address
console.log('my ip', await upnp.getExternalIp())// Open port
await upnp.setMapping({remotePort: 8000, ttl: 150})
console.log('upnp list', await upnp.getMapping())// Remove port
// await this.unmap({remotePort: 8000})// Remove all ports
// await this.unmapAll()
```## [STUN Client (RFC 5389)](https://datatracker.ietf.org/doc/html/rfc5389)
Usage:
```ts
#!/usr/bin/env -S deno run -A --unstable-netimport {STUN} from 'https://raw.githubusercontent.com/MAKS11060/deno-protocols/main/stun/stun.ts'
const stun = new STUN('stun.l.google.com:19302')
console.log(await stun.getMappedAddress()) // { hostname: "178.68.144.103", port: 49646, family: "IPv4" }
```## [WebSocket Stream Server (RFC 6455)](https://datatracker.ietf.org/doc/html/rfc6455)
Implementing Websocket as a [WebSocketStream](https://github.com/ricea/websocketstream-explainer) server using [StreamApi](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)
Usage:
```ts
#!/usr/bin/env -S deno run -A --unstable-hmrimport {handleWebSocketStream} from 'https://raw.githubusercontent.com/MAKS11060/deno-protocols/main/websocket/ws.ts'
const serve = async (handler: (conn: Deno.Conn) => Promise) => {
const listener = Deno.listen({port: 8000})
for await (const conn of listener) {
try {
handler(conn).catch((e) => {
console.error('err', e)
})
} catch (e) {
console.error(e)
}
}
}serve(async (conn) => {
const {readable, writable, headers, url} = await handleWebSocketStream(conn)
console.log({headers, url})
const writer = writable.getWriter()
for await (const data of readable) {
if (typeof data === 'string') {
console.log('server:', data)
} else {
console.log('server:', data.byteLength)
}
writer.write(data) // write to client received data
}
console.log('readable closed')writer.releaseLock()
writable.close()
})
```