UNPKG

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
# ๐Ÿ› ๏ธ `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 ```