tiny-server-essentials
Version:
A good utility toolkit to unify Express v5 and Socket.IO v4 into a seamless development experience with modular helpers, server wrappers, and WebSocket tools.
64 lines (39 loc) โข 1.52 kB
Markdown
# ๐ ๏ธ `Utils` โ Utility Helpers for WebSocket and Audio Logic
A small collection of stateless, reusable utility functions used throughout the project โ mostly for IP parsing and client-side audio handling.
---
## ๐งช `extractIpList(userIp)` โ Smart IP Extractor
Normalizes and extracts a list of clean, unique IP addresses.
### โ
Supported Inputs:
* `string` โ a single IP (or multiple IPs comma-separated)
* `string[]` โ array of IPs
* `null` or `undefined` โ returns an empty list
### ๐ง It handles:
* IPv4: `192.168.1.1`
* IPv6: `::1`, `2001:0db8::ff00:42:8329`
* IPv4-mapped IPv6: `::ffff:192.168.1.1`
### ๐งน Cleans:
* Brackets (`[::1]`)
* `::ffff:` prefix
* Duplicate entries
### โจ Example:
```js
extractIpList('::ffff:192.168.1.1, ::1, 10.0.0.2');
// โ ['192.168.1.1', '::1', '10.0.0.2']
```
Returns only valid IPs after normalization.
---
## ๐ `micVolumeFilter(volume: number): number`
Safely filters a raw volume input, ensuring it's a number within the acceptable range (0โ100), and returns it normalized to a 0โ1 scale.
### ๐งช Parameters
| Name | Type | Description |
| ------ | ------ | ----------------------- |
| volume | number | Volume level (0 to 100) |
### ๐ Returns
* `number` โ Normalized volume (from `0.0` to `1.0`)
### โ ๏ธ Throws
* `Error` if input is not a number
* `RangeError` if input is `NaN`, not finite, or outside the 0โ100 range
### โ
Example
```js
const normalized = micVolumeFilter(75); // 0.75
```