feat(server): add Schema validation system and binary encoding optimization (#421)
* feat(server): add distributed room support - Add DistributedRoomManager for multi-server room management - Add MemoryAdapter for testing and standalone mode - Add RedisAdapter for production multi-server deployments - Add LoadBalancedRouter with 5 load balancing strategies - Add distributed config option to createServer - Add $redirect message for cross-server player redirection - Add failover mechanism for automatic room recovery - Add room:migrated and server:draining event types - Update documentation (zh/en) * feat(server): add Schema validation system and binary encoding optimization ## Schema Validation System - Add lightweight schema validation system (s.object, s.string, s.number, etc.) - Support auto type inference with Infer<> generic - Integrate schema validation into API/message handlers - Add defineApiWithSchema and defineMsgWithSchema helpers ## Binary Encoding Optimization - Add native WebSocket binary frame support via sendBinary() - Add PacketType.Binary for efficient binary data transmission - Optimize ECSRoom.broadcastBinary() to use native binary ## Architecture Improvements - Extract BaseValidator to separate file to eliminate code duplication - Add ECSRoom export to main index.ts for better discoverability - Add Core.worldManager initialization check in ECSRoom constructor - Remove deprecated validate field from ApiDefinition (use schema instead) ## Documentation - Add Schema validation documentation in Chinese and English * fix(rpc): resolve ESLint warnings with proper types - Replace `any` with proper WebSocket type in connection.ts - Add IncomingMessage type for request handling in index.ts - Use Record<string, Handler> pattern instead of `any` casting - Replace `any` with `unknown` in ProtocolDef and type inference
This commit is contained in:
@@ -30,13 +30,73 @@
|
||||
export { createServer } from './core/server.js';
|
||||
|
||||
// Helpers
|
||||
export { defineApi, defineMsg, defineHttp } from './helpers/define.js';
|
||||
export {
|
||||
defineApi,
|
||||
defineMsg,
|
||||
defineHttp,
|
||||
defineApiWithSchema,
|
||||
defineMsgWithSchema,
|
||||
type ApiDefinitionWithSchema,
|
||||
type MsgDefinitionWithSchema
|
||||
} from './helpers/define.js';
|
||||
|
||||
// Schema Validation System
|
||||
export {
|
||||
// Schema Builder (main API)
|
||||
s,
|
||||
|
||||
// Primitive Validators
|
||||
string,
|
||||
number,
|
||||
boolean,
|
||||
literal,
|
||||
any,
|
||||
|
||||
// Composite Validators
|
||||
object,
|
||||
array,
|
||||
tuple,
|
||||
union,
|
||||
record,
|
||||
nativeEnum,
|
||||
|
||||
// Validator Classes (for extension)
|
||||
StringValidator,
|
||||
NumberValidator,
|
||||
BooleanValidator,
|
||||
LiteralValidator,
|
||||
AnyValidator,
|
||||
ObjectValidator,
|
||||
ArrayValidator,
|
||||
TupleValidator,
|
||||
UnionValidator,
|
||||
RecordValidator,
|
||||
EnumValidator,
|
||||
|
||||
// Helpers
|
||||
parse,
|
||||
safeParse,
|
||||
createGuard,
|
||||
|
||||
// Types
|
||||
type Validator,
|
||||
type ValidationResult,
|
||||
type ValidationSuccess,
|
||||
type ValidationFailure,
|
||||
type ValidationError,
|
||||
type Infer,
|
||||
type ObjectShape,
|
||||
type InferShape
|
||||
} from './schema/index.js';
|
||||
|
||||
// Room System
|
||||
export { Room, type RoomOptions } from './room/Room.js';
|
||||
export { Player, type IPlayer } from './room/Player.js';
|
||||
export { onMessage } from './room/decorators.js';
|
||||
|
||||
// ECS Room (for ECS-integrated games)
|
||||
export { ECSRoom, type ECSRoomConfig } from './ecs/ECSRoom.js';
|
||||
|
||||
// Types
|
||||
export type {
|
||||
ServerConfig,
|
||||
@@ -62,3 +122,36 @@ export type {
|
||||
|
||||
// Re-export useful types from @esengine/rpc
|
||||
export { RpcError, ErrorCode } from '@esengine/rpc';
|
||||
|
||||
// Distributed Room Support
|
||||
export {
|
||||
DistributedRoomManager,
|
||||
MemoryAdapter,
|
||||
RedisAdapter,
|
||||
createRedisAdapter,
|
||||
LoadBalancedRouter,
|
||||
createLoadBalancedRouter,
|
||||
type IDistributedAdapter,
|
||||
type MemoryAdapterConfig,
|
||||
type RedisAdapterConfig,
|
||||
type RedisClient,
|
||||
type RedisClientFactory,
|
||||
type ServerStatus,
|
||||
type ServerRegistration,
|
||||
type RoomRegistration,
|
||||
type RoomQuery,
|
||||
type RoomSnapshot,
|
||||
type DistributedEvent,
|
||||
type DistributedEventType,
|
||||
type DistributedEventHandler,
|
||||
type DistributedRoomManagerConfig,
|
||||
type DistributedConfig,
|
||||
type RoutingResult,
|
||||
type RoutingRequest,
|
||||
type LoadBalanceStrategy,
|
||||
type LoadBalancedRouterConfig
|
||||
} from './distributed/index.js';
|
||||
|
||||
// Room Manager (for extension)
|
||||
export { RoomManager, type RoomClass } from './room/RoomManager.js';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user