websocket server: initial commit. Add Asserts static class

This commit is contained in:
2025-09-27 13:43:30 -04:00
parent 804f2d990a
commit ff31848460
8 changed files with 186 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
save-exact=true
package-lock=false

View File

@@ -0,0 +1 @@
20

View File

@@ -0,0 +1,12 @@
{
"arrowParens": "avoid",
"bracketSameLine": true,
"bracketSpacing": false,
"printWidth": 160,
"semi": true,
"singleAttributePerLine": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
}

View File

@@ -0,0 +1,15 @@
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import json from '@eslint/json';
import markdown from '@eslint/markdown';
import css from '@eslint/css';
import {defineConfig} from 'eslint/config';
export default defineConfig([
{files: ['**/*.{js,mjs,cjs,ts,mts,cts}'], plugins: {js}, extends: ['js/recommended'], languageOptions: {globals: globals.node}},
tseslint.configs.recommended,
{files: ['**/*.json'], plugins: {json}, language: 'json/json', extends: ['json/recommended']},
{files: ['**/*.md'], plugins: {markdown}, language: 'markdown/commonmark', extends: ['markdown/recommended']},
{files: ['**/*.css'], plugins: {css}, language: 'css/css', extends: ['css/recommended']}
]);

View File

@@ -0,0 +1,31 @@
{
"name": "@techniker-me/websocket-server",
"version": "0.0.0",
"type": "module",
"scripts": {
"format": "prettier --write .",
"lint": "eslint --max-warnings 0 .",
"prelint:fix": "npm run format",
"lint:fix": "eslint --fix .",
"dev": "tsx watch --clear-screen=false",
"typecheck": "tsc"
},
"author": "Alexander Zinn",
"license": "ISC",
"description": "",
"devDependencies": {
"@eslint/css": "0.11.1",
"@eslint/js": "9.36.0",
"@eslint/json": "0.13.2",
"@eslint/markdown": "7.3.0",
"@types/node": "24.5.2",
"eslint": "9.36.0",
"globals": "16.4.0",
"jiti": "2.6.0",
"nodemon": "3.1.10",
"prettier": "3.6.2",
"tsx": "4.20.6",
"typescript": "5.9.2",
"typescript-eslint": "8.44.1"
}
}

View File

@@ -0,0 +1 @@
export default class WebSocketServer {}

View File

@@ -0,0 +1,111 @@
export default class Assert {
public static isUndefined(name: string, value: unknown): asserts value is undefined {
if (value !== undefined) {
throw new Error(`[${name}] must be undefined instead received [${typeof value}]`);
}
}
public static isNull(name: string, value: unknown): asserts value is null {
if (value !== null) {
throw new Error(`[${name}] must be null instead received [${typeof value}]`);
}
}
public static isDefined(name: string, value: unknown): asserts value is undefined | null {
if (value === undefined || value === null) {
throw new Error(`[${name}] must be defined instead received [${typeof value}]`);
}
}
public static isBoolean(name: string, value: unknown): asserts value is boolean {
if (!Assert._isBoolean(value)) {
throw new Error(`[${name}] must be a boolean instead received [${typeof value}]`);
}
}
public static isTrue(name: string, value: unknown): asserts value is true {
Assert.isBoolean(name, value);
if (!value) {
throw new Error(`[${name}] must be true`);
}
}
public static isString(name: string, value: unknown): asserts value is string {
if (!Assert._isString(value)) {
throw new Error(`[${name}] must be a string instead received [${typeof value}]`);
}
}
public static isNonEmptyString(name: string, value: unknown): asserts value is string {
if (!Assert._isNonEmptyString(value)) {
throw new Error(`[${name}] must be a non-empty string instead received [${typeof value}]`);
}
}
public static isNumber(name: string, value: unknown): asserts value is number {
if (!Assert._isNumber(value)) {
throw new Error(`[${name}] must be a number instead received [${typeof value}]`);
}
}
public static isInteger(name: string, value: unknown): asserts value is number {
Assert.isNumber(name, value);
if (!Number.isInteger(value)) {
throw new Error(`[${name}] must be an integer, received [${value}]`);
}
}
public static isPositiveNumber(name: string, value: unknown): asserts value is number {
if (!Assert._isNumberPositive(value)) {
throw new Error(`[${name}] must be a positive number instead received [${typeof value}]`);
}
}
public static isNumberInRange(name: string, lowerBound: number, upperBound: number, value: unknown): asserts value is number {
if (upperBound < lowerBound) {
throw new Error(`Invalid Range: [${name}] bounds are invalid, lower bound [${lowerBound}] must be less than upper bound [${upperBound}]`);
}
if (!(Assert._isNumber(value) && Assert._isNumberInRange(value, lowerBound, upperBound))) {
throw new Error(`[${name}] must have a value between [${lowerBound}] and [${upperBound}] instead received [${value}]`);
}
}
public static isObject<T>(name: string, value: unknown): asserts value is T {
if (value === null || typeof value !== 'object') {
throw new Error(`[${name}] must be an object, instead received [${typeof value}]`);
}
}
public static isEnumMember<T extends object>(name: string, enumObj: T, value: unknown): asserts value is T[keyof T] {
if (!Object.values(enumObj).includes(value as T[keyof T])) {
throw new Error(`[${name}] is not a member of the enum`);
}
}
private static _isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
}
private static _isString(value: unknown): value is string {
return typeof value === 'string';
}
private static _isNonEmptyString(value: unknown): value is string {
return Assert._isString(value) && value.length > 0;
}
private static _isNumber(value: unknown): value is number {
return typeof value === 'number';
}
private static _isNumberPositive(value: unknown): value is number {
return Assert._isNumber(value) && value > 0;
}
private static _isNumberInRange(value: unknown, lowerBound: number, upperBound: number): value is number {
return Assert._isNumber(value) && value >= lowerBound && value <= upperBound;
}
}

View File

@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "esnext",
"moduleDetection": "force",
"module": "Preserve",
"resolveJsonModule": true,
"allowJs": false,
"esModuleInterop": true,
"isolatedModules": true,
"strict": true,
"noEmit": true
}
}