Compare commits
25 Commits
@esengine/
...
@esengine/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61da38faf5 | ||
|
|
f333b81298 | ||
|
|
69bb6bd946 | ||
|
|
3b6fc8266f | ||
|
|
db22bd3028 | ||
|
|
b80e967829 | ||
|
|
9e87eb39b9 | ||
|
|
ff549f3c2a | ||
|
|
15c1d98305 | ||
|
|
4a3d8c3962 | ||
|
|
0f5aa633d8 | ||
|
|
85171a0a5c | ||
|
|
35d81880a7 | ||
|
|
71022abc99 | ||
|
|
87f71e2251 | ||
|
|
b9ea8d14cf | ||
|
|
10d0fb1d5c | ||
|
|
71e111415f | ||
|
|
0de45279e6 | ||
|
|
cc6f12d470 | ||
|
|
902c0a1074 | ||
|
|
d3e489aad3 | ||
|
|
12051d987f | ||
|
|
b38fe5ebf4 | ||
|
|
f01ce1e320 |
3
.github/workflows/release-changesets.yml
vendored
3
.github/workflows/release-changesets.yml
vendored
@@ -57,6 +57,9 @@ jobs:
|
||||
pnpm --filter "@esengine/rpc" build
|
||||
pnpm --filter "@esengine/network" build
|
||||
pnpm --filter "@esengine/server" build
|
||||
pnpm --filter "@esengine/database-drivers" build
|
||||
pnpm --filter "@esengine/database" build
|
||||
pnpm --filter "@esengine/transaction" build
|
||||
pnpm --filter "@esengine/cli" build
|
||||
pnpm --filter "create-esengine-server" build
|
||||
|
||||
|
||||
@@ -267,6 +267,7 @@ export default defineConfig({
|
||||
{ label: '概述', slug: 'modules/network', translations: { en: 'Overview' } },
|
||||
{ label: '客户端', slug: 'modules/network/client', translations: { en: 'Client' } },
|
||||
{ label: '服务器', slug: 'modules/network/server', translations: { en: 'Server' } },
|
||||
{ label: 'HTTP 路由', slug: 'modules/network/http', translations: { en: 'HTTP Routing' } },
|
||||
{ label: '认证系统', slug: 'modules/network/auth', translations: { en: 'Authentication' } },
|
||||
{ label: '速率限制', slug: 'modules/network/rate-limit', translations: { en: 'Rate Limiting' } },
|
||||
{ label: '状态同步', slug: 'modules/network/sync', translations: { en: 'State Sync' } },
|
||||
@@ -287,6 +288,25 @@ export default defineConfig({
|
||||
{ label: '分布式事务', slug: 'modules/transaction/distributed', translations: { en: 'Distributed' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '数据库',
|
||||
translations: { en: 'Database' },
|
||||
items: [
|
||||
{ label: '概述', slug: 'modules/database', translations: { en: 'Overview' } },
|
||||
{ label: '仓储模式', slug: 'modules/database/repository', translations: { en: 'Repository' } },
|
||||
{ label: '用户仓储', slug: 'modules/database/user', translations: { en: 'User Repository' } },
|
||||
{ label: '查询构建器', slug: 'modules/database/query', translations: { en: 'Query Builder' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '数据库驱动',
|
||||
translations: { en: 'Database Drivers' },
|
||||
items: [
|
||||
{ label: '概述', slug: 'modules/database-drivers', translations: { en: 'Overview' } },
|
||||
{ label: 'MongoDB', slug: 'modules/database-drivers/mongo', translations: { en: 'MongoDB' } },
|
||||
{ label: 'Redis', slug: 'modules/database-drivers/redis', translations: { en: 'Redis' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '世界流式加载',
|
||||
translations: { en: 'World Streaming' },
|
||||
|
||||
@@ -182,6 +182,70 @@ export class IsHealthLow implements INodeExecutor {
|
||||
}
|
||||
```
|
||||
|
||||
## Using Custom Executors in BehaviorTreeBuilder
|
||||
|
||||
After defining a custom executor with `@NodeExecutorMetadata`, use the `.action()` method in the builder:
|
||||
|
||||
```typescript
|
||||
import { BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
|
||||
|
||||
// Use custom executor in behavior tree
|
||||
const tree = BehaviorTreeBuilder.create('CombatAI')
|
||||
.defineBlackboardVariable('health', 100)
|
||||
.defineBlackboardVariable('target', null)
|
||||
.selector('Root')
|
||||
.sequence('AttackSequence')
|
||||
// Use custom action - matches implementationType in decorator
|
||||
.action('AttackAction', 'Attack', { damage: 25 })
|
||||
.action('MoveToTarget', 'Chase')
|
||||
.end()
|
||||
.action('WaitAction', 'Idle', { duration: 1000 })
|
||||
.end()
|
||||
.build();
|
||||
|
||||
// Start the behavior tree
|
||||
const entity = scene.createEntity('Enemy');
|
||||
BehaviorTreeStarter.start(entity, tree);
|
||||
```
|
||||
|
||||
### Builder Methods for Custom Nodes
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `.action(type, name?, config?)` | Add custom action node |
|
||||
| `.condition(type, name?, config?)` | Add custom condition node |
|
||||
| `.executeAction(name)` | Use blackboard function `action_{name}` |
|
||||
| `.executeCondition(name)` | Use blackboard function `condition_{name}` |
|
||||
|
||||
### Complete Example
|
||||
|
||||
```typescript
|
||||
// 1. Define custom executor
|
||||
@NodeExecutorMetadata({
|
||||
implementationType: 'AttackAction',
|
||||
nodeType: NodeType.Action,
|
||||
displayName: 'Attack',
|
||||
category: 'Combat',
|
||||
configSchema: {
|
||||
damage: { type: 'number', default: 10, supportBinding: true }
|
||||
}
|
||||
})
|
||||
class AttackAction implements INodeExecutor {
|
||||
execute(context: NodeExecutionContext): TaskStatus {
|
||||
const damage = BindingHelper.getValue<number>(context, 'damage', 10);
|
||||
console.log(`Attacking with ${damage} damage!`);
|
||||
return TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Build and use
|
||||
const tree = BehaviorTreeBuilder.create('AI')
|
||||
.selector('Root')
|
||||
.action('AttackAction', 'Attack', { damage: 50 })
|
||||
.end()
|
||||
.build();
|
||||
```
|
||||
|
||||
## Registering Custom Executors
|
||||
|
||||
Executors are auto-registered via the decorator. To manually register:
|
||||
|
||||
136
docs/src/content/docs/en/modules/database-drivers/index.md
Normal file
136
docs/src/content/docs/en/modules/database-drivers/index.md
Normal file
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "Database Drivers"
|
||||
description: "MongoDB, Redis connection management and driver abstraction"
|
||||
---
|
||||
|
||||
`@esengine/database-drivers` is ESEngine's database connection management layer, providing unified connection management for MongoDB, Redis, and more.
|
||||
|
||||
## Features
|
||||
|
||||
- **Connection Pool** - Automatic connection pool management
|
||||
- **Auto Reconnect** - Automatic reconnection on disconnect
|
||||
- **Event Notification** - Connection state change events
|
||||
- **Type Decoupling** - Simplified interfaces, no dependency on native driver types
|
||||
- **Shared Connections** - Single connection shared across modules
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @esengine/database-drivers
|
||||
```
|
||||
|
||||
**Peer Dependencies:**
|
||||
```bash
|
||||
npm install mongodb # For MongoDB support
|
||||
npm install ioredis # For Redis support
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ @esengine/database-drivers (Layer 1) │
|
||||
│ ┌─────────────────────┐ ┌─────────────────────┐ │
|
||||
│ │ MongoConnection │ │ RedisConnection │ │
|
||||
│ │ - Pool management │ │ - Auto-reconnect │ │
|
||||
│ │ - Auto-reconnect │ │ - Key prefix │ │
|
||||
│ │ - Event emitter │ │ - Event emitter │ │
|
||||
│ └──────────┬──────────┘ └─────────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────▼──────────┐ │
|
||||
│ │ IMongoCollection<T> │ ← Type-safe interface │
|
||||
│ │ (Adapter pattern) │ decoupled from mongodb types │
|
||||
│ └─────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌───────────────────────┐ ┌───────────────────────┐
|
||||
│ @esengine/database │ │ @esengine/transaction │
|
||||
│ (Repository pattern) │ │ (Distributed tx) │
|
||||
└───────────────────────┘ └───────────────────────┘
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### MongoDB Connection
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
// Create connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20
|
||||
},
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
// Listen to events
|
||||
mongo.on('connected', () => console.log('MongoDB connected'))
|
||||
mongo.on('disconnected', () => console.log('MongoDB disconnected'))
|
||||
mongo.on('error', (e) => console.error('Error:', e.error))
|
||||
|
||||
// Connect
|
||||
await mongo.connect()
|
||||
|
||||
// Use collections
|
||||
const users = mongo.collection<User>('users')
|
||||
await users.insertOne({ name: 'John', score: 100 })
|
||||
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
// Disconnect when done
|
||||
await mongo.disconnect()
|
||||
```
|
||||
|
||||
### Redis Connection
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
await redis.connect()
|
||||
|
||||
// Basic operations
|
||||
await redis.set('session:123', 'data', 3600) // With TTL
|
||||
const value = await redis.get('session:123')
|
||||
|
||||
await redis.disconnect()
|
||||
```
|
||||
|
||||
## Service Container Integration
|
||||
|
||||
```typescript
|
||||
import { ServiceContainer } from '@esengine/ecs-framework'
|
||||
import {
|
||||
createMongoConnection,
|
||||
MongoConnectionToken,
|
||||
RedisConnectionToken
|
||||
} from '@esengine/database-drivers'
|
||||
|
||||
const services = new ServiceContainer()
|
||||
|
||||
// Register connections
|
||||
const mongo = createMongoConnection({ uri: '...', database: 'game' })
|
||||
await mongo.connect()
|
||||
services.register(MongoConnectionToken, mongo)
|
||||
|
||||
// Retrieve in other modules
|
||||
const connection = services.get(MongoConnectionToken)
|
||||
const users = connection.collection('users')
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- [MongoDB Connection](/en/modules/database-drivers/mongo/) - MongoDB configuration details
|
||||
- [Redis Connection](/en/modules/database-drivers/redis/) - Redis configuration details
|
||||
- [Service Tokens](/en/modules/database-drivers/tokens/) - Dependency injection integration
|
||||
265
docs/src/content/docs/en/modules/database-drivers/mongo.md
Normal file
265
docs/src/content/docs/en/modules/database-drivers/mongo.md
Normal file
@@ -0,0 +1,265 @@
|
||||
---
|
||||
title: "MongoDB Connection"
|
||||
description: "MongoDB connection management, connection pooling, auto-reconnect"
|
||||
---
|
||||
|
||||
## Configuration Options
|
||||
|
||||
```typescript
|
||||
interface MongoConnectionConfig {
|
||||
/** MongoDB connection URI */
|
||||
uri: string
|
||||
|
||||
/** Database name */
|
||||
database: string
|
||||
|
||||
/** Connection pool configuration */
|
||||
pool?: {
|
||||
minSize?: number // Minimum connections
|
||||
maxSize?: number // Maximum connections
|
||||
acquireTimeout?: number // Connection acquire timeout (ms)
|
||||
maxLifetime?: number // Maximum connection lifetime (ms)
|
||||
}
|
||||
|
||||
/** Auto-reconnect (default true) */
|
||||
autoReconnect?: boolean
|
||||
|
||||
/** Reconnect interval (ms, default 5000) */
|
||||
reconnectInterval?: number
|
||||
|
||||
/** Maximum reconnect attempts (default 10) */
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
```
|
||||
|
||||
## Complete Example
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection, MongoConnectionToken } from '@esengine/database-drivers'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20,
|
||||
acquireTimeout: 5000,
|
||||
maxLifetime: 300000
|
||||
},
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10
|
||||
})
|
||||
|
||||
// Event listeners
|
||||
mongo.on('connected', () => {
|
||||
console.log('MongoDB connected')
|
||||
})
|
||||
|
||||
mongo.on('disconnected', () => {
|
||||
console.log('MongoDB disconnected')
|
||||
})
|
||||
|
||||
mongo.on('reconnecting', () => {
|
||||
console.log('MongoDB reconnecting...')
|
||||
})
|
||||
|
||||
mongo.on('reconnected', () => {
|
||||
console.log('MongoDB reconnected')
|
||||
})
|
||||
|
||||
mongo.on('error', (event) => {
|
||||
console.error('MongoDB error:', event.error)
|
||||
})
|
||||
|
||||
// Connect
|
||||
await mongo.connect()
|
||||
|
||||
// Check status
|
||||
console.log('Connected:', mongo.isConnected())
|
||||
console.log('Ping:', await mongo.ping())
|
||||
```
|
||||
|
||||
## IMongoConnection Interface
|
||||
|
||||
```typescript
|
||||
interface IMongoConnection {
|
||||
/** Connection ID */
|
||||
readonly id: string
|
||||
|
||||
/** Connection state */
|
||||
readonly state: ConnectionState
|
||||
|
||||
/** Establish connection */
|
||||
connect(): Promise<void>
|
||||
|
||||
/** Disconnect */
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/** Check if connected */
|
||||
isConnected(): boolean
|
||||
|
||||
/** Test connection */
|
||||
ping(): Promise<boolean>
|
||||
|
||||
/** Get typed collection */
|
||||
collection<T extends object>(name: string): IMongoCollection<T>
|
||||
|
||||
/** Get database interface */
|
||||
getDatabase(): IMongoDatabase
|
||||
|
||||
/** Get native client (advanced usage) */
|
||||
getNativeClient(): MongoClientType
|
||||
|
||||
/** Get native database (advanced usage) */
|
||||
getNativeDatabase(): Db
|
||||
}
|
||||
```
|
||||
|
||||
## IMongoCollection Interface
|
||||
|
||||
Type-safe collection interface, decoupled from native MongoDB types:
|
||||
|
||||
```typescript
|
||||
interface IMongoCollection<T extends object> {
|
||||
readonly name: string
|
||||
|
||||
// Query
|
||||
findOne(filter: object, options?: FindOptions): Promise<T | null>
|
||||
find(filter: object, options?: FindOptions): Promise<T[]>
|
||||
countDocuments(filter?: object): Promise<number>
|
||||
|
||||
// Insert
|
||||
insertOne(doc: T): Promise<InsertOneResult>
|
||||
insertMany(docs: T[]): Promise<InsertManyResult>
|
||||
|
||||
// Update
|
||||
updateOne(filter: object, update: object): Promise<UpdateResult>
|
||||
updateMany(filter: object, update: object): Promise<UpdateResult>
|
||||
findOneAndUpdate(
|
||||
filter: object,
|
||||
update: object,
|
||||
options?: FindOneAndUpdateOptions
|
||||
): Promise<T | null>
|
||||
|
||||
// Delete
|
||||
deleteOne(filter: object): Promise<DeleteResult>
|
||||
deleteMany(filter: object): Promise<DeleteResult>
|
||||
|
||||
// Index
|
||||
createIndex(
|
||||
spec: Record<string, 1 | -1>,
|
||||
options?: IndexOptions
|
||||
): Promise<string>
|
||||
}
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic CRUD
|
||||
|
||||
```typescript
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
score: number
|
||||
}
|
||||
|
||||
const users = mongo.collection<User>('users')
|
||||
|
||||
// Insert
|
||||
await users.insertOne({
|
||||
id: '1',
|
||||
name: 'John',
|
||||
email: 'john@example.com',
|
||||
score: 100
|
||||
})
|
||||
|
||||
// Query
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
const topUsers = await users.find(
|
||||
{ score: { $gte: 100 } },
|
||||
{ sort: { score: -1 }, limit: 10 }
|
||||
)
|
||||
|
||||
// Update
|
||||
await users.updateOne(
|
||||
{ id: '1' },
|
||||
{ $inc: { score: 10 } }
|
||||
)
|
||||
|
||||
// Delete
|
||||
await users.deleteOne({ id: '1' })
|
||||
```
|
||||
|
||||
### Batch Operations
|
||||
|
||||
```typescript
|
||||
// Batch insert
|
||||
await users.insertMany([
|
||||
{ id: '1', name: 'Alice', email: 'alice@example.com', score: 100 },
|
||||
{ id: '2', name: 'Bob', email: 'bob@example.com', score: 200 },
|
||||
{ id: '3', name: 'Carol', email: 'carol@example.com', score: 150 }
|
||||
])
|
||||
|
||||
// Batch update
|
||||
await users.updateMany(
|
||||
{ score: { $lt: 100 } },
|
||||
{ $set: { status: 'inactive' } }
|
||||
)
|
||||
|
||||
// Batch delete
|
||||
await users.deleteMany({ status: 'inactive' })
|
||||
```
|
||||
|
||||
### Index Management
|
||||
|
||||
```typescript
|
||||
// Create indexes
|
||||
await users.createIndex({ email: 1 }, { unique: true })
|
||||
await users.createIndex({ score: -1 })
|
||||
await users.createIndex({ name: 1, score: -1 })
|
||||
```
|
||||
|
||||
## Integration with Other Modules
|
||||
|
||||
### With @esengine/database
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { UserRepository, createRepository } from '@esengine/database'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// Use UserRepository
|
||||
const userRepo = new UserRepository(mongo)
|
||||
await userRepo.register({ username: 'john', password: '123456' })
|
||||
|
||||
// Use generic repository
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
```
|
||||
|
||||
### With @esengine/transaction
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// Create transaction storage (shared connection)
|
||||
const storage = createMongoStorage(mongo)
|
||||
await storage.ensureIndexes()
|
||||
|
||||
const txManager = new TransactionManager({ storage })
|
||||
```
|
||||
228
docs/src/content/docs/en/modules/database-drivers/redis.md
Normal file
228
docs/src/content/docs/en/modules/database-drivers/redis.md
Normal file
@@ -0,0 +1,228 @@
|
||||
---
|
||||
title: "Redis Connection"
|
||||
description: "Redis connection management, auto-reconnect, key prefix"
|
||||
---
|
||||
|
||||
## Configuration Options
|
||||
|
||||
```typescript
|
||||
interface RedisConnectionConfig {
|
||||
/** Redis host */
|
||||
host?: string
|
||||
|
||||
/** Redis port */
|
||||
port?: number
|
||||
|
||||
/** Authentication password */
|
||||
password?: string
|
||||
|
||||
/** Database number */
|
||||
db?: number
|
||||
|
||||
/** Key prefix */
|
||||
keyPrefix?: string
|
||||
|
||||
/** Auto-reconnect (default true) */
|
||||
autoReconnect?: boolean
|
||||
|
||||
/** Reconnect interval (ms, default 5000) */
|
||||
reconnectInterval?: number
|
||||
|
||||
/** Maximum reconnect attempts (default 10) */
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
```
|
||||
|
||||
## Complete Example
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection, RedisConnectionToken } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
password: 'your-password',
|
||||
db: 0,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10
|
||||
})
|
||||
|
||||
// Event listeners
|
||||
redis.on('connected', () => {
|
||||
console.log('Redis connected')
|
||||
})
|
||||
|
||||
redis.on('disconnected', () => {
|
||||
console.log('Redis disconnected')
|
||||
})
|
||||
|
||||
redis.on('error', (event) => {
|
||||
console.error('Redis error:', event.error)
|
||||
})
|
||||
|
||||
// Connect
|
||||
await redis.connect()
|
||||
|
||||
// Check status
|
||||
console.log('Connected:', redis.isConnected())
|
||||
console.log('Ping:', await redis.ping())
|
||||
```
|
||||
|
||||
## IRedisConnection Interface
|
||||
|
||||
```typescript
|
||||
interface IRedisConnection {
|
||||
/** Connection ID */
|
||||
readonly id: string
|
||||
|
||||
/** Connection state */
|
||||
readonly state: ConnectionState
|
||||
|
||||
/** Establish connection */
|
||||
connect(): Promise<void>
|
||||
|
||||
/** Disconnect */
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/** Check if connected */
|
||||
isConnected(): boolean
|
||||
|
||||
/** Test connection */
|
||||
ping(): Promise<boolean>
|
||||
|
||||
/** Get value */
|
||||
get(key: string): Promise<string | null>
|
||||
|
||||
/** Set value (optional TTL in seconds) */
|
||||
set(key: string, value: string, ttl?: number): Promise<void>
|
||||
|
||||
/** Delete key */
|
||||
del(key: string): Promise<boolean>
|
||||
|
||||
/** Check if key exists */
|
||||
exists(key: string): Promise<boolean>
|
||||
|
||||
/** Set expiration (seconds) */
|
||||
expire(key: string, seconds: number): Promise<boolean>
|
||||
|
||||
/** Get remaining TTL (seconds) */
|
||||
ttl(key: string): Promise<number>
|
||||
|
||||
/** Get native client (advanced usage) */
|
||||
getNativeClient(): Redis
|
||||
}
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Operations
|
||||
|
||||
```typescript
|
||||
// Set value
|
||||
await redis.set('user:1:name', 'John')
|
||||
|
||||
// Set value with expiration (1 hour)
|
||||
await redis.set('session:abc123', 'user-data', 3600)
|
||||
|
||||
// Get value
|
||||
const name = await redis.get('user:1:name')
|
||||
|
||||
// Check if key exists
|
||||
const exists = await redis.exists('user:1:name')
|
||||
|
||||
// Delete key
|
||||
await redis.del('user:1:name')
|
||||
|
||||
// Get remaining TTL
|
||||
const ttl = await redis.ttl('session:abc123')
|
||||
```
|
||||
|
||||
### Key Prefix
|
||||
|
||||
When `keyPrefix` is configured, all operations automatically add the prefix:
|
||||
|
||||
```typescript
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
keyPrefix: 'game:'
|
||||
})
|
||||
|
||||
// Actual key is 'game:user:1'
|
||||
await redis.set('user:1', 'data')
|
||||
|
||||
// Actual key queried is 'game:user:1'
|
||||
const data = await redis.get('user:1')
|
||||
```
|
||||
|
||||
### Advanced Operations
|
||||
|
||||
Use native client for advanced operations:
|
||||
|
||||
```typescript
|
||||
const client = redis.getNativeClient()
|
||||
|
||||
// Using Pipeline
|
||||
const pipeline = client.pipeline()
|
||||
pipeline.set('key1', 'value1')
|
||||
pipeline.set('key2', 'value2')
|
||||
pipeline.set('key3', 'value3')
|
||||
await pipeline.exec()
|
||||
|
||||
// Using Transactions
|
||||
const multi = client.multi()
|
||||
multi.incr('counter')
|
||||
multi.get('counter')
|
||||
const results = await multi.exec()
|
||||
|
||||
// Using Lua Scripts
|
||||
const result = await client.eval(
|
||||
`return redis.call('get', KEYS[1])`,
|
||||
1,
|
||||
'mykey'
|
||||
)
|
||||
```
|
||||
|
||||
## Integration with Transaction System
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
import { RedisStorage, TransactionManager } from '@esengine/transaction'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'tx:'
|
||||
})
|
||||
await redis.connect()
|
||||
|
||||
// Create transaction storage
|
||||
const storage = new RedisStorage({
|
||||
factory: () => redis.getNativeClient(),
|
||||
prefix: 'tx:'
|
||||
})
|
||||
|
||||
const txManager = new TransactionManager({ storage })
|
||||
```
|
||||
|
||||
## Connection State
|
||||
|
||||
```typescript
|
||||
type ConnectionState =
|
||||
| 'disconnected' // Not connected
|
||||
| 'connecting' // Connecting
|
||||
| 'connected' // Connected
|
||||
| 'disconnecting' // Disconnecting
|
||||
| 'error' // Error state
|
||||
```
|
||||
|
||||
## Events
|
||||
|
||||
| Event | Description |
|
||||
|-------|-------------|
|
||||
| `connected` | Connection established |
|
||||
| `disconnected` | Connection closed |
|
||||
| `reconnecting` | Reconnecting |
|
||||
| `reconnected` | Reconnection successful |
|
||||
| `error` | Error occurred |
|
||||
217
docs/src/content/docs/en/modules/database/index.md
Normal file
217
docs/src/content/docs/en/modules/database/index.md
Normal file
@@ -0,0 +1,217 @@
|
||||
---
|
||||
title: "Database Repository"
|
||||
description: "Repository pattern database layer with CRUD, pagination, and soft delete"
|
||||
---
|
||||
|
||||
`@esengine/database` is ESEngine's database operation layer, providing type-safe CRUD operations based on the Repository pattern.
|
||||
|
||||
## Features
|
||||
|
||||
- **Repository Pattern** - Generic CRUD operations with type safety
|
||||
- **Pagination** - Built-in pagination support
|
||||
- **Soft Delete** - Optional soft delete with restore
|
||||
- **User Management** - Ready-to-use UserRepository
|
||||
- **Password Security** - Secure password hashing with scrypt
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @esengine/database @esengine/database-drivers
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Basic Repository
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { Repository, createRepository } from '@esengine/database'
|
||||
|
||||
// Define entity
|
||||
interface Player {
|
||||
id: string
|
||||
name: string
|
||||
score: number
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
// Create connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// Create repository
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// CRUD operations
|
||||
const player = await playerRepo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
|
||||
const found = await playerRepo.findById(player.id)
|
||||
|
||||
await playerRepo.update(player.id, { score: 100 })
|
||||
|
||||
await playerRepo.delete(player.id)
|
||||
```
|
||||
|
||||
### Custom Repository
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
import type { IMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
rank?: string
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players')
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
|
||||
async findByRank(rank: string): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
where: { rank }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Usage
|
||||
const playerRepo = new PlayerRepository(mongo)
|
||||
const topPlayers = await playerRepo.findTopPlayers(5)
|
||||
```
|
||||
|
||||
### User Repository
|
||||
|
||||
```typescript
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
|
||||
// Register new user
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com'
|
||||
})
|
||||
|
||||
// Authenticate
|
||||
const authenticated = await userRepo.authenticate('john', 'securePassword123')
|
||||
if (authenticated) {
|
||||
console.log('Login successful:', authenticated.username)
|
||||
}
|
||||
|
||||
// Change password
|
||||
await userRepo.changePassword(user.id, 'securePassword123', 'newPassword456')
|
||||
|
||||
// Role management
|
||||
await userRepo.addRole(user.id, 'admin')
|
||||
await userRepo.removeRole(user.id, 'admin')
|
||||
|
||||
// Find users
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
const john = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
### Pagination
|
||||
|
||||
```typescript
|
||||
const result = await playerRepo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { rank: 'gold' },
|
||||
sort: { score: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
console.log(result.data) // Player[]
|
||||
console.log(result.total) // Total count
|
||||
console.log(result.totalPages) // Total pages
|
||||
console.log(result.hasNext) // Has next page
|
||||
console.log(result.hasPrev) // Has previous page
|
||||
```
|
||||
|
||||
### Soft Delete
|
||||
|
||||
```typescript
|
||||
// Enable soft delete
|
||||
const playerRepo = createRepository<Player>(mongo, 'players', true)
|
||||
|
||||
// Delete (marks as deleted)
|
||||
await playerRepo.delete(playerId)
|
||||
|
||||
// Find excludes soft-deleted by default
|
||||
const players = await playerRepo.findMany()
|
||||
|
||||
// Include soft-deleted records
|
||||
const allPlayers = await playerRepo.findMany({
|
||||
includeSoftDeleted: true
|
||||
})
|
||||
|
||||
// Restore soft-deleted record
|
||||
await playerRepo.restore(playerId)
|
||||
```
|
||||
|
||||
### Query Options
|
||||
|
||||
```typescript
|
||||
// Complex queries
|
||||
const players = await playerRepo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] },
|
||||
name: { $like: 'John%' }
|
||||
},
|
||||
sort: {
|
||||
score: 'desc',
|
||||
name: 'asc'
|
||||
},
|
||||
limit: 10,
|
||||
offset: 0
|
||||
})
|
||||
|
||||
// OR conditions
|
||||
const players = await playerRepo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ score: { $gte: 1000 } },
|
||||
{ rank: 'legendary' }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Query Operators
|
||||
|
||||
| Operator | Description | Example |
|
||||
|----------|-------------|---------|
|
||||
| `$eq` | Equal | `{ score: { $eq: 100 } }` |
|
||||
| `$ne` | Not equal | `{ status: { $ne: 'banned' } }` |
|
||||
| `$gt` | Greater than | `{ score: { $gt: 50 } }` |
|
||||
| `$gte` | Greater or equal | `{ level: { $gte: 10 } }` |
|
||||
| `$lt` | Less than | `{ age: { $lt: 18 } }` |
|
||||
| `$lte` | Less or equal | `{ price: { $lte: 100 } }` |
|
||||
| `$in` | In array | `{ rank: { $in: ['gold', 'platinum'] } }` |
|
||||
| `$nin` | Not in array | `{ status: { $nin: ['banned'] } }` |
|
||||
| `$like` | Pattern match | `{ name: { $like: '%john%' } }` |
|
||||
| `$regex` | Regex match | `{ email: { $regex: '@gmail.com$' } }` |
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Repository API](/en/modules/database/repository/) - Repository detailed API
|
||||
- [User Management](/en/modules/database/user/) - UserRepository usage
|
||||
- [Query Syntax](/en/modules/database/query/) - Query condition syntax
|
||||
185
docs/src/content/docs/en/modules/database/query.md
Normal file
185
docs/src/content/docs/en/modules/database/query.md
Normal file
@@ -0,0 +1,185 @@
|
||||
---
|
||||
title: "Query Syntax"
|
||||
description: "Query condition operators and syntax"
|
||||
---
|
||||
|
||||
## Basic Queries
|
||||
|
||||
### Exact Match
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
name: 'John',
|
||||
status: 'active'
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Using Operators
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Query Operators
|
||||
|
||||
| Operator | Description | Example |
|
||||
|----------|-------------|---------|
|
||||
| `$eq` | Equal | `{ score: { $eq: 100 } }` |
|
||||
| `$ne` | Not equal | `{ status: { $ne: 'banned' } }` |
|
||||
| `$gt` | Greater than | `{ score: { $gt: 50 } }` |
|
||||
| `$gte` | Greater than or equal | `{ level: { $gte: 10 } }` |
|
||||
| `$lt` | Less than | `{ age: { $lt: 18 } }` |
|
||||
| `$lte` | Less than or equal | `{ price: { $lte: 100 } }` |
|
||||
| `$in` | In array | `{ rank: { $in: ['gold', 'platinum'] } }` |
|
||||
| `$nin` | Not in array | `{ status: { $nin: ['banned', 'suspended'] } }` |
|
||||
| `$like` | Pattern match | `{ name: { $like: '%john%' } }` |
|
||||
| `$regex` | Regex match | `{ email: { $regex: '@gmail.com$' } }` |
|
||||
|
||||
## Logical Operators
|
||||
|
||||
### $or
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ score: { $gte: 1000 } },
|
||||
{ rank: 'legendary' }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### $and
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
$and: [
|
||||
{ score: { $gte: 100 } },
|
||||
{ score: { $lte: 500 } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Combined Usage
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
$or: [
|
||||
{ rank: 'gold' },
|
||||
{ score: { $gte: 1000 } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Pattern Matching
|
||||
|
||||
### $like Syntax
|
||||
|
||||
- `%` - Matches any sequence of characters
|
||||
- `_` - Matches single character
|
||||
|
||||
```typescript
|
||||
// Starts with 'John'
|
||||
{ name: { $like: 'John%' } }
|
||||
|
||||
// Ends with 'son'
|
||||
{ name: { $like: '%son' } }
|
||||
|
||||
// Contains 'oh'
|
||||
{ name: { $like: '%oh%' } }
|
||||
|
||||
// Second character is 'o'
|
||||
{ name: { $like: '_o%' } }
|
||||
```
|
||||
|
||||
### $regex Syntax
|
||||
|
||||
Uses standard regular expressions:
|
||||
|
||||
```typescript
|
||||
// Starts with 'John' (case insensitive)
|
||||
{ name: { $regex: '^john' } }
|
||||
|
||||
// Gmail email
|
||||
{ email: { $regex: '@gmail\\.com$' } }
|
||||
|
||||
// Contains numbers
|
||||
{ username: { $regex: '\\d+' } }
|
||||
```
|
||||
|
||||
## Sorting
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
sort: {
|
||||
score: 'desc', // Descending
|
||||
name: 'asc' // Ascending
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Pagination
|
||||
|
||||
### Using limit/offset
|
||||
|
||||
```typescript
|
||||
// First page
|
||||
await repo.findMany({
|
||||
limit: 20,
|
||||
offset: 0
|
||||
})
|
||||
|
||||
// Second page
|
||||
await repo.findMany({
|
||||
limit: 20,
|
||||
offset: 20
|
||||
})
|
||||
```
|
||||
|
||||
### Using findPaginated
|
||||
|
||||
```typescript
|
||||
const result = await repo.findPaginated(
|
||||
{ page: 2, pageSize: 20 },
|
||||
{ sort: { createdAt: 'desc' } }
|
||||
)
|
||||
```
|
||||
|
||||
## Complete Examples
|
||||
|
||||
```typescript
|
||||
// Find active gold players with scores between 100-1000
|
||||
// Sort by score descending, get top 10
|
||||
const players = await repo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
rank: 'gold',
|
||||
score: { $gte: 100, $lte: 1000 }
|
||||
},
|
||||
sort: { score: 'desc' },
|
||||
limit: 10
|
||||
})
|
||||
|
||||
// Search for users with 'john' in username or gmail email
|
||||
const users = await repo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ username: { $like: '%john%' } },
|
||||
{ email: { $regex: '@gmail\\.com$' } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
244
docs/src/content/docs/en/modules/database/repository.md
Normal file
244
docs/src/content/docs/en/modules/database/repository.md
Normal file
@@ -0,0 +1,244 @@
|
||||
---
|
||||
title: "Repository API"
|
||||
description: "Generic repository interface, CRUD operations, pagination, soft delete"
|
||||
---
|
||||
|
||||
## Creating a Repository
|
||||
|
||||
### Using Factory Function
|
||||
|
||||
```typescript
|
||||
import { createRepository } from '@esengine/database'
|
||||
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// Enable soft delete
|
||||
const playerRepo = createRepository<Player>(mongo, 'players', true)
|
||||
```
|
||||
|
||||
### Extending Repository
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players', false) // Third param: enable soft delete
|
||||
}
|
||||
|
||||
// Add custom methods
|
||||
async findTopPlayers(limit: number): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## BaseEntity Interface
|
||||
|
||||
All entities must extend `BaseEntity`:
|
||||
|
||||
```typescript
|
||||
interface BaseEntity {
|
||||
id: string
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
deletedAt?: Date // Used for soft delete
|
||||
}
|
||||
```
|
||||
|
||||
## Query Methods
|
||||
|
||||
### findById
|
||||
|
||||
```typescript
|
||||
const player = await repo.findById('player-123')
|
||||
```
|
||||
|
||||
### findOne
|
||||
|
||||
```typescript
|
||||
const player = await repo.findOne({
|
||||
where: { name: 'John' }
|
||||
})
|
||||
|
||||
const topPlayer = await repo.findOne({
|
||||
sort: { score: 'desc' }
|
||||
})
|
||||
```
|
||||
|
||||
### findMany
|
||||
|
||||
```typescript
|
||||
// Simple query
|
||||
const players = await repo.findMany({
|
||||
where: { rank: 'gold' }
|
||||
})
|
||||
|
||||
// Complex query
|
||||
const players = await repo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] }
|
||||
},
|
||||
sort: { score: 'desc', name: 'asc' },
|
||||
limit: 10,
|
||||
offset: 0
|
||||
})
|
||||
```
|
||||
|
||||
### findPaginated
|
||||
|
||||
```typescript
|
||||
const result = await repo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { rank: 'gold' },
|
||||
sort: { score: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
console.log(result.data) // Player[]
|
||||
console.log(result.total) // Total count
|
||||
console.log(result.totalPages) // Total pages
|
||||
console.log(result.hasNext) // Has next page
|
||||
console.log(result.hasPrev) // Has previous page
|
||||
```
|
||||
|
||||
### count
|
||||
|
||||
```typescript
|
||||
const count = await repo.count({
|
||||
where: { rank: 'gold' }
|
||||
})
|
||||
```
|
||||
|
||||
### exists
|
||||
|
||||
```typescript
|
||||
const exists = await repo.exists({
|
||||
where: { email: 'john@example.com' }
|
||||
})
|
||||
```
|
||||
|
||||
## Create Methods
|
||||
|
||||
### create
|
||||
|
||||
```typescript
|
||||
const player = await repo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
// Automatically generates id, createdAt, updatedAt
|
||||
```
|
||||
|
||||
### createMany
|
||||
|
||||
```typescript
|
||||
const players = await repo.createMany([
|
||||
{ name: 'Alice', score: 100 },
|
||||
{ name: 'Bob', score: 200 },
|
||||
{ name: 'Carol', score: 150 }
|
||||
])
|
||||
```
|
||||
|
||||
## Update Methods
|
||||
|
||||
### update
|
||||
|
||||
```typescript
|
||||
const updated = await repo.update('player-123', {
|
||||
score: 200,
|
||||
rank: 'gold'
|
||||
})
|
||||
// Automatically updates updatedAt
|
||||
```
|
||||
|
||||
## Delete Methods
|
||||
|
||||
### delete
|
||||
|
||||
```typescript
|
||||
// Hard delete
|
||||
await repo.delete('player-123')
|
||||
|
||||
// Soft delete (if enabled)
|
||||
// Actually sets the deletedAt field
|
||||
```
|
||||
|
||||
### deleteMany
|
||||
|
||||
```typescript
|
||||
const count = await repo.deleteMany({
|
||||
where: { score: { $lt: 10 } }
|
||||
})
|
||||
```
|
||||
|
||||
## Soft Delete
|
||||
|
||||
### Enabling Soft Delete
|
||||
|
||||
```typescript
|
||||
const repo = createRepository<Player>(mongo, 'players', true)
|
||||
```
|
||||
|
||||
### Query Behavior
|
||||
|
||||
```typescript
|
||||
// Excludes soft-deleted records by default
|
||||
const players = await repo.findMany()
|
||||
|
||||
// Include soft-deleted records
|
||||
const allPlayers = await repo.findMany({
|
||||
includeSoftDeleted: true
|
||||
})
|
||||
```
|
||||
|
||||
### Restore Records
|
||||
|
||||
```typescript
|
||||
await repo.restore('player-123')
|
||||
```
|
||||
|
||||
## QueryOptions
|
||||
|
||||
```typescript
|
||||
interface QueryOptions<T> {
|
||||
/** Query conditions */
|
||||
where?: WhereCondition<T>
|
||||
|
||||
/** Sorting */
|
||||
sort?: Partial<Record<keyof T, 'asc' | 'desc'>>
|
||||
|
||||
/** Limit count */
|
||||
limit?: number
|
||||
|
||||
/** Offset */
|
||||
offset?: number
|
||||
|
||||
/** Include soft-deleted records (only when soft delete is enabled) */
|
||||
includeSoftDeleted?: boolean
|
||||
}
|
||||
```
|
||||
|
||||
## PaginatedResult
|
||||
|
||||
```typescript
|
||||
interface PaginatedResult<T> {
|
||||
data: T[]
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
totalPages: number
|
||||
hasNext: boolean
|
||||
hasPrev: boolean
|
||||
}
|
||||
```
|
||||
277
docs/src/content/docs/en/modules/database/user.md
Normal file
277
docs/src/content/docs/en/modules/database/user.md
Normal file
@@ -0,0 +1,277 @@
|
||||
---
|
||||
title: "User Management"
|
||||
description: "UserRepository for user registration, authentication, and role management"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
`UserRepository` provides out-of-the-box user management features:
|
||||
|
||||
- User registration and authentication
|
||||
- Password hashing (using scrypt)
|
||||
- Role management
|
||||
- Account status management
|
||||
|
||||
## Quick Start
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
```
|
||||
|
||||
## User Registration
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com', // Optional
|
||||
displayName: 'John Doe', // Optional
|
||||
roles: ['player'] // Optional, defaults to []
|
||||
})
|
||||
|
||||
console.log(user)
|
||||
// {
|
||||
// id: 'uuid-...',
|
||||
// username: 'john',
|
||||
// email: 'john@example.com',
|
||||
// displayName: 'John Doe',
|
||||
// roles: ['player'],
|
||||
// status: 'active',
|
||||
// createdAt: Date,
|
||||
// updatedAt: Date
|
||||
// }
|
||||
```
|
||||
|
||||
**Note**: `register` returns a `SafeUser` which excludes the password hash.
|
||||
|
||||
## User Authentication
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.authenticate('john', 'securePassword123')
|
||||
|
||||
if (user) {
|
||||
console.log('Login successful:', user.username)
|
||||
} else {
|
||||
console.log('Invalid username or password')
|
||||
}
|
||||
```
|
||||
|
||||
## Password Management
|
||||
|
||||
### Change Password
|
||||
|
||||
```typescript
|
||||
const success = await userRepo.changePassword(
|
||||
userId,
|
||||
'oldPassword123',
|
||||
'newPassword456'
|
||||
)
|
||||
|
||||
if (success) {
|
||||
console.log('Password changed successfully')
|
||||
} else {
|
||||
console.log('Invalid current password')
|
||||
}
|
||||
```
|
||||
|
||||
### Reset Password
|
||||
|
||||
```typescript
|
||||
// Admin directly resets password
|
||||
const success = await userRepo.resetPassword(userId, 'newPassword123')
|
||||
```
|
||||
|
||||
## Role Management
|
||||
|
||||
### Add Role
|
||||
|
||||
```typescript
|
||||
await userRepo.addRole(userId, 'admin')
|
||||
await userRepo.addRole(userId, 'moderator')
|
||||
```
|
||||
|
||||
### Remove Role
|
||||
|
||||
```typescript
|
||||
await userRepo.removeRole(userId, 'moderator')
|
||||
```
|
||||
|
||||
### Query Roles
|
||||
|
||||
```typescript
|
||||
// Find all admins
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
|
||||
// Check if user has a role
|
||||
const user = await userRepo.findById(userId)
|
||||
const isAdmin = user?.roles.includes('admin')
|
||||
```
|
||||
|
||||
## Querying Users
|
||||
|
||||
### Find by Username
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
### Find by Email
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.findByEmail('john@example.com')
|
||||
```
|
||||
|
||||
### Find by Role
|
||||
|
||||
```typescript
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
```
|
||||
|
||||
### Using Inherited Methods
|
||||
|
||||
```typescript
|
||||
// Paginated query
|
||||
const result = await userRepo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { status: 'active' },
|
||||
sort: { createdAt: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
// Complex query
|
||||
const users = await userRepo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
roles: { $in: ['admin', 'moderator'] }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Account Status
|
||||
|
||||
```typescript
|
||||
type UserStatus = 'active' | 'inactive' | 'banned' | 'suspended'
|
||||
```
|
||||
|
||||
### Update Status
|
||||
|
||||
```typescript
|
||||
await userRepo.update(userId, { status: 'banned' })
|
||||
```
|
||||
|
||||
### Query by Status
|
||||
|
||||
```typescript
|
||||
const activeUsers = await userRepo.findMany({
|
||||
where: { status: 'active' }
|
||||
})
|
||||
|
||||
const bannedUsers = await userRepo.findMany({
|
||||
where: { status: 'banned' }
|
||||
})
|
||||
```
|
||||
|
||||
## Type Definitions
|
||||
|
||||
### UserEntity
|
||||
|
||||
```typescript
|
||||
interface UserEntity extends BaseEntity {
|
||||
username: string
|
||||
passwordHash: string
|
||||
email?: string
|
||||
displayName?: string
|
||||
roles: string[]
|
||||
status: UserStatus
|
||||
lastLoginAt?: Date
|
||||
}
|
||||
```
|
||||
|
||||
### SafeUser
|
||||
|
||||
```typescript
|
||||
type SafeUser = Omit<UserEntity, 'passwordHash'>
|
||||
```
|
||||
|
||||
### CreateUserParams
|
||||
|
||||
```typescript
|
||||
interface CreateUserParams {
|
||||
username: string
|
||||
password: string
|
||||
email?: string
|
||||
displayName?: string
|
||||
roles?: string[]
|
||||
}
|
||||
```
|
||||
|
||||
## Password Utilities
|
||||
|
||||
Standalone password utility functions:
|
||||
|
||||
```typescript
|
||||
import { hashPassword, verifyPassword } from '@esengine/database'
|
||||
|
||||
// Hash password
|
||||
const hash = await hashPassword('myPassword123')
|
||||
|
||||
// Verify password
|
||||
const isValid = await verifyPassword('myPassword123', hash)
|
||||
```
|
||||
|
||||
### Security Notes
|
||||
|
||||
- Uses Node.js built-in `scrypt` algorithm
|
||||
- Automatically generates random salt
|
||||
- Uses secure iteration parameters by default
|
||||
- Hash format: `salt:hash` (both hex encoded)
|
||||
|
||||
## Extending UserRepository
|
||||
|
||||
```typescript
|
||||
import { UserRepository, UserEntity } from '@esengine/database'
|
||||
|
||||
interface GameUser extends UserEntity {
|
||||
level: number
|
||||
experience: number
|
||||
coins: number
|
||||
}
|
||||
|
||||
class GameUserRepository extends UserRepository {
|
||||
// Override collection name
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'game_users')
|
||||
}
|
||||
|
||||
// Add game-related methods
|
||||
async addExperience(userId: string, amount: number): Promise<GameUser | null> {
|
||||
const user = await this.findById(userId) as GameUser | null
|
||||
if (!user) return null
|
||||
|
||||
const newExp = user.experience + amount
|
||||
const newLevel = Math.floor(newExp / 1000) + 1
|
||||
|
||||
return this.update(userId, {
|
||||
experience: newExp,
|
||||
level: newLevel
|
||||
}) as Promise<GameUser | null>
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<GameUser[]> {
|
||||
return this.findMany({
|
||||
sort: { level: 'desc', experience: 'desc' },
|
||||
limit
|
||||
}) as Promise<GameUser[]>
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -36,6 +36,13 @@ ESEngine provides a rich set of modules that can be imported as needed.
|
||||
| [Network](/en/modules/network/) | `@esengine/network` | Multiplayer game networking |
|
||||
| [Transaction](/en/modules/transaction/) | `@esengine/transaction` | Game transactions with distributed support |
|
||||
|
||||
### Database
|
||||
|
||||
| Module | Package | Description |
|
||||
|--------|---------|-------------|
|
||||
| [Database Drivers](/en/modules/database-drivers/) | `@esengine/database-drivers` | MongoDB, Redis connection management |
|
||||
| [Database Repository](/en/modules/database/) | `@esengine/database` | Repository pattern data operations |
|
||||
|
||||
## Installation
|
||||
|
||||
All modules can be installed independently:
|
||||
|
||||
@@ -92,6 +92,355 @@ const token = jwtProvider.sign({
|
||||
const payload = jwtProvider.decode(token)
|
||||
```
|
||||
|
||||
### Custom Provider
|
||||
|
||||
You can create custom authentication providers by implementing the `IAuthProvider` interface to integrate with any authentication system (OAuth, LDAP, custom database auth, etc.).
|
||||
|
||||
#### IAuthProvider Interface
|
||||
|
||||
```typescript
|
||||
interface IAuthProvider<TUser = unknown, TCredentials = unknown> {
|
||||
/** Provider name */
|
||||
readonly name: string;
|
||||
|
||||
/** Verify credentials */
|
||||
verify(credentials: TCredentials): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** Refresh token (optional) */
|
||||
refresh?(token: string): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** Revoke token (optional) */
|
||||
revoke?(token: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
interface AuthResult<TUser> {
|
||||
success: boolean;
|
||||
user?: TUser;
|
||||
error?: string;
|
||||
errorCode?: AuthErrorCode;
|
||||
token?: string;
|
||||
expiresAt?: number;
|
||||
}
|
||||
|
||||
type AuthErrorCode =
|
||||
| 'INVALID_CREDENTIALS'
|
||||
| 'EXPIRED_TOKEN'
|
||||
| 'INVALID_TOKEN'
|
||||
| 'USER_NOT_FOUND'
|
||||
| 'ACCOUNT_DISABLED'
|
||||
| 'RATE_LIMITED'
|
||||
| 'INSUFFICIENT_PERMISSIONS';
|
||||
```
|
||||
|
||||
#### Custom Provider Examples
|
||||
|
||||
**Example 1: Database Password Authentication**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
username: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface PasswordCredentials {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
class DatabaseAuthProvider implements IAuthProvider<User, PasswordCredentials> {
|
||||
readonly name = 'database'
|
||||
|
||||
async verify(credentials: PasswordCredentials): Promise<AuthResult<User>> {
|
||||
const { username, password } = credentials
|
||||
|
||||
// Query user from database
|
||||
const user = await db.users.findByUsername(username)
|
||||
if (!user) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'User not found',
|
||||
errorCode: 'USER_NOT_FOUND'
|
||||
}
|
||||
}
|
||||
|
||||
// Verify password (using bcrypt or similar)
|
||||
const isValid = await bcrypt.compare(password, user.passwordHash)
|
||||
if (!isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Invalid password',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// Check account status
|
||||
if (user.disabled) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Account is disabled',
|
||||
errorCode: 'ACCOUNT_DISABLED'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Example 2: OAuth/Third-party Authentication**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface OAuthUser {
|
||||
id: string
|
||||
email: string
|
||||
name: string
|
||||
provider: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface OAuthCredentials {
|
||||
provider: 'google' | 'github' | 'discord'
|
||||
accessToken: string
|
||||
}
|
||||
|
||||
class OAuthProvider implements IAuthProvider<OAuthUser, OAuthCredentials> {
|
||||
readonly name = 'oauth'
|
||||
|
||||
async verify(credentials: OAuthCredentials): Promise<AuthResult<OAuthUser>> {
|
||||
const { provider, accessToken } = credentials
|
||||
|
||||
try {
|
||||
// Verify token with provider
|
||||
const profile = await this.fetchUserProfile(provider, accessToken)
|
||||
|
||||
// Find or create local user
|
||||
let user = await db.users.findByOAuth(provider, profile.id)
|
||||
if (!user) {
|
||||
user = await db.users.create({
|
||||
oauthProvider: provider,
|
||||
oauthId: profile.id,
|
||||
email: profile.email,
|
||||
name: profile.name,
|
||||
roles: ['player']
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
provider,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'OAuth verification failed',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchUserProfile(provider: string, token: string) {
|
||||
switch (provider) {
|
||||
case 'google':
|
||||
return fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
case 'github':
|
||||
return fetch('https://api.github.com/user', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
// Other providers...
|
||||
default:
|
||||
throw new Error(`Unsupported provider: ${provider}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Example 3: API Key Authentication**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface ApiUser {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
rateLimit: number
|
||||
}
|
||||
|
||||
class ApiKeyAuthProvider implements IAuthProvider<ApiUser, string> {
|
||||
readonly name = 'api-key'
|
||||
|
||||
private revokedKeys = new Set<string>()
|
||||
|
||||
async verify(apiKey: string): Promise<AuthResult<ApiUser>> {
|
||||
if (!apiKey || !apiKey.startsWith('sk_')) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Invalid API Key format',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
if (this.revokedKeys.has(apiKey)) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key has been revoked',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
// Query API Key from database
|
||||
const keyData = await db.apiKeys.findByKey(apiKey)
|
||||
if (!keyData) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key not found',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// Check expiration
|
||||
if (keyData.expiresAt && keyData.expiresAt < Date.now()) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key has expired',
|
||||
errorCode: 'EXPIRED_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: keyData.userId,
|
||||
name: keyData.name,
|
||||
roles: keyData.roles,
|
||||
rateLimit: keyData.rateLimit
|
||||
},
|
||||
expiresAt: keyData.expiresAt
|
||||
}
|
||||
}
|
||||
|
||||
async revoke(apiKey: string): Promise<boolean> {
|
||||
this.revokedKeys.add(apiKey)
|
||||
await db.apiKeys.revoke(apiKey)
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Using Custom Providers
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
import { withAuth } from '@esengine/server/auth'
|
||||
|
||||
// Create custom provider
|
||||
const dbAuthProvider = new DatabaseAuthProvider()
|
||||
|
||||
// Or use OAuth provider
|
||||
const oauthProvider = new OAuthProvider()
|
||||
|
||||
// Use custom provider
|
||||
const server = withAuth(await createServer({ port: 3000 }), {
|
||||
provider: dbAuthProvider, // or oauthProvider
|
||||
|
||||
// Extract credentials from WebSocket connection request
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
|
||||
// For database auth: get from query params
|
||||
const username = url.searchParams.get('username')
|
||||
const password = url.searchParams.get('password')
|
||||
if (username && password) {
|
||||
return { username, password }
|
||||
}
|
||||
|
||||
// For OAuth: get from token param
|
||||
const provider = url.searchParams.get('provider')
|
||||
const accessToken = url.searchParams.get('access_token')
|
||||
if (provider && accessToken) {
|
||||
return { provider, accessToken }
|
||||
}
|
||||
|
||||
// For API Key: get from header
|
||||
const apiKey = req.headers['x-api-key']
|
||||
if (apiKey) {
|
||||
return apiKey as string
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
onAuthFailure: (conn, error) => {
|
||||
console.log(`Auth failed: ${error.errorCode} - ${error.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
await server.start()
|
||||
```
|
||||
|
||||
#### Combining Multiple Providers
|
||||
|
||||
You can create a composite provider to support multiple authentication methods:
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface MultiAuthCredentials {
|
||||
type: 'jwt' | 'oauth' | 'apikey' | 'password'
|
||||
data: unknown
|
||||
}
|
||||
|
||||
class MultiAuthProvider implements IAuthProvider<User, MultiAuthCredentials> {
|
||||
readonly name = 'multi'
|
||||
|
||||
constructor(
|
||||
private jwtProvider: JwtAuthProvider<User>,
|
||||
private oauthProvider: OAuthProvider,
|
||||
private apiKeyProvider: ApiKeyAuthProvider,
|
||||
private dbProvider: DatabaseAuthProvider
|
||||
) {}
|
||||
|
||||
async verify(credentials: MultiAuthCredentials): Promise<AuthResult<User>> {
|
||||
switch (credentials.type) {
|
||||
case 'jwt':
|
||||
return this.jwtProvider.verify(credentials.data as string)
|
||||
case 'oauth':
|
||||
return this.oauthProvider.verify(credentials.data as OAuthCredentials)
|
||||
case 'apikey':
|
||||
return this.apiKeyProvider.verify(credentials.data as string)
|
||||
case 'password':
|
||||
return this.dbProvider.verify(credentials.data as PasswordCredentials)
|
||||
default:
|
||||
return {
|
||||
success: false,
|
||||
error: 'Unsupported authentication type',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Session Provider
|
||||
|
||||
Use server-side sessions for stateful authentication:
|
||||
|
||||
441
docs/src/content/docs/en/modules/network/distributed.md
Normal file
441
docs/src/content/docs/en/modules/network/distributed.md
Normal file
@@ -0,0 +1,441 @@
|
||||
---
|
||||
title: "Distributed Rooms"
|
||||
description: "Multi-server room management with DistributedRoomManager"
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Distributed room support allows multiple server instances to share a room registry, enabling cross-server player routing and failover.
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ Server A Server B Server C │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
|
||||
│ │ Room 1 │ │ Room 3 │ │ Room 5 │ │
|
||||
│ │ Room 2 │ │ Room 4 │ │ Room 6 │ │
|
||||
│ └────┬────┘ └────┬────┘ └────┬────┘ │
|
||||
│ │ │ │ │
|
||||
│ └─────────────────┼─────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────▼──────────┐ │
|
||||
│ │ IDistributedAdapter │ │
|
||||
│ │ (Redis / Memory) │ │
|
||||
│ └─────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Single Server Mode (Testing)
|
||||
|
||||
```typescript
|
||||
import {
|
||||
DistributedRoomManager,
|
||||
MemoryAdapter,
|
||||
Room
|
||||
} from '@esengine/server';
|
||||
|
||||
// Define room type
|
||||
class GameRoom extends Room {
|
||||
maxPlayers = 4;
|
||||
}
|
||||
|
||||
// Create adapter and manager
|
||||
const adapter = new MemoryAdapter();
|
||||
const manager = new DistributedRoomManager(adapter, {
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3000
|
||||
}, (conn, type, data) => conn.send(JSON.stringify({ type, data })));
|
||||
|
||||
// Register room type
|
||||
manager.define('game', GameRoom);
|
||||
|
||||
// Start manager
|
||||
await manager.start();
|
||||
|
||||
// Distributed join/create room
|
||||
const result = await manager.joinOrCreateDistributed('game', 'player-1', conn);
|
||||
if ('redirect' in result) {
|
||||
// Player should connect to another server
|
||||
console.log(`Redirect to: ${result.redirect}`);
|
||||
} else {
|
||||
// Player joined local room
|
||||
const { room, player } = result;
|
||||
}
|
||||
|
||||
// Graceful shutdown
|
||||
await manager.stop(true);
|
||||
```
|
||||
|
||||
### Multi-Server Mode (Production)
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { DistributedRoomManager, RedisAdapter } from '@esengine/server';
|
||||
|
||||
const adapter = new RedisAdapter({
|
||||
factory: () => new Redis({
|
||||
host: 'redis.example.com',
|
||||
port: 6379
|
||||
}),
|
||||
prefix: 'game:',
|
||||
serverTtl: 30,
|
||||
snapshotTtl: 86400
|
||||
});
|
||||
|
||||
const manager = new DistributedRoomManager(adapter, {
|
||||
serverId: process.env.SERVER_ID,
|
||||
serverAddress: process.env.PUBLIC_IP,
|
||||
serverPort: 3000,
|
||||
heartbeatInterval: 5000,
|
||||
snapshotInterval: 30000,
|
||||
enableFailover: true,
|
||||
capacity: 100
|
||||
}, sendFn);
|
||||
```
|
||||
|
||||
## DistributedRoomManager
|
||||
|
||||
### Configuration Options
|
||||
|
||||
| Property | Type | Default | Description |
|
||||
|----------|------|---------|-------------|
|
||||
| `serverId` | `string` | required | Unique server identifier |
|
||||
| `serverAddress` | `string` | required | Public address for client connections |
|
||||
| `serverPort` | `number` | required | Server port |
|
||||
| `heartbeatInterval` | `number` | `5000` | Heartbeat interval (ms) |
|
||||
| `snapshotInterval` | `number` | `30000` | State snapshot interval, 0 to disable |
|
||||
| `migrationTimeout` | `number` | `10000` | Room migration timeout |
|
||||
| `enableFailover` | `boolean` | `true` | Enable automatic failover |
|
||||
| `capacity` | `number` | `100` | Max rooms on this server |
|
||||
|
||||
### Lifecycle Methods
|
||||
|
||||
#### start()
|
||||
|
||||
Start the distributed room manager. Connects to adapter, registers server, starts heartbeat.
|
||||
|
||||
```typescript
|
||||
await manager.start();
|
||||
```
|
||||
|
||||
#### stop(graceful?)
|
||||
|
||||
Stop the manager. If `graceful=true`, marks server as draining and saves all room snapshots.
|
||||
|
||||
```typescript
|
||||
await manager.stop(true);
|
||||
```
|
||||
|
||||
### Routing Methods
|
||||
|
||||
#### joinOrCreateDistributed()
|
||||
|
||||
Join or create a room with distributed awareness. Returns `{ room, player }` for local rooms or `{ redirect: string }` for remote rooms.
|
||||
|
||||
```typescript
|
||||
const result = await manager.joinOrCreateDistributed('game', 'player-1', conn);
|
||||
|
||||
if ('redirect' in result) {
|
||||
// Client should redirect to another server
|
||||
res.json({ redirect: result.redirect });
|
||||
} else {
|
||||
// Player joined local room
|
||||
const { room, player } = result;
|
||||
}
|
||||
```
|
||||
|
||||
#### route()
|
||||
|
||||
Route a player to the appropriate room/server.
|
||||
|
||||
```typescript
|
||||
const result = await manager.route({
|
||||
roomType: 'game',
|
||||
playerId: 'p1'
|
||||
});
|
||||
|
||||
switch (result.type) {
|
||||
case 'local': // Room is on this server
|
||||
break;
|
||||
case 'redirect': // Room is on another server
|
||||
// result.serverAddress contains target server
|
||||
break;
|
||||
case 'create': // No room exists, need to create
|
||||
break;
|
||||
case 'unavailable': // Cannot find or create room
|
||||
// result.reason contains error message
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
### State Management
|
||||
|
||||
#### saveSnapshot()
|
||||
|
||||
Manually save a room's state snapshot.
|
||||
|
||||
```typescript
|
||||
await manager.saveSnapshot(roomId);
|
||||
```
|
||||
|
||||
#### restoreFromSnapshot()
|
||||
|
||||
Restore a room from its saved snapshot.
|
||||
|
||||
```typescript
|
||||
const success = await manager.restoreFromSnapshot(roomId);
|
||||
```
|
||||
|
||||
### Query Methods
|
||||
|
||||
#### getServers()
|
||||
|
||||
Get all online servers.
|
||||
|
||||
```typescript
|
||||
const servers = await manager.getServers();
|
||||
```
|
||||
|
||||
#### queryDistributedRooms()
|
||||
|
||||
Query rooms across all servers.
|
||||
|
||||
```typescript
|
||||
const rooms = await manager.queryDistributedRooms({
|
||||
roomType: 'game',
|
||||
hasSpace: true,
|
||||
notLocked: true
|
||||
});
|
||||
```
|
||||
|
||||
## IDistributedAdapter
|
||||
|
||||
Interface for distributed backends. Implement this to add support for Redis, message queues, etc.
|
||||
|
||||
### Built-in Adapters
|
||||
|
||||
#### MemoryAdapter
|
||||
|
||||
In-memory implementation for testing and single-server mode.
|
||||
|
||||
```typescript
|
||||
const adapter = new MemoryAdapter({
|
||||
serverTtl: 15000, // Server offline after no heartbeat (ms)
|
||||
enableTtlCheck: true, // Enable automatic TTL checking
|
||||
ttlCheckInterval: 5000 // TTL check interval (ms)
|
||||
});
|
||||
```
|
||||
|
||||
#### RedisAdapter
|
||||
|
||||
Redis-based implementation for production multi-server deployments.
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { RedisAdapter } from '@esengine/server';
|
||||
|
||||
const adapter = new RedisAdapter({
|
||||
factory: () => new Redis('redis://localhost:6379'),
|
||||
prefix: 'game:', // Key prefix (default: 'dist:')
|
||||
serverTtl: 30, // Server TTL in seconds (default: 30)
|
||||
roomTtl: 0, // Room TTL, 0 = never expire (default: 0)
|
||||
snapshotTtl: 86400, // Snapshot TTL in seconds (default: 24h)
|
||||
channel: 'game:events' // Pub/Sub channel (default: 'distributed:events')
|
||||
});
|
||||
```
|
||||
|
||||
**RedisAdapter Configuration:**
|
||||
|
||||
| Property | Type | Default | Description |
|
||||
|----------|------|---------|-------------|
|
||||
| `factory` | `() => RedisClient` | required | Redis client factory (lazy connection) |
|
||||
| `prefix` | `string` | `'dist:'` | Key prefix for all Redis keys |
|
||||
| `serverTtl` | `number` | `30` | Server TTL in seconds |
|
||||
| `roomTtl` | `number` | `0` | Room TTL in seconds, 0 = no expiry |
|
||||
| `snapshotTtl` | `number` | `86400` | Snapshot TTL in seconds |
|
||||
| `channel` | `string` | `'distributed:events'` | Pub/Sub channel name |
|
||||
|
||||
**Features:**
|
||||
- Server registry with automatic heartbeat TTL
|
||||
- Room registry with cross-server lookup
|
||||
- State snapshots with configurable TTL
|
||||
- Pub/Sub for cross-server events
|
||||
- Distributed locks using Redis SET NX
|
||||
|
||||
### Custom Adapters
|
||||
|
||||
```typescript
|
||||
import type { IDistributedAdapter } from '@esengine/server';
|
||||
|
||||
class MyAdapter implements IDistributedAdapter {
|
||||
// Lifecycle
|
||||
async connect(): Promise<void> { }
|
||||
async disconnect(): Promise<void> { }
|
||||
isConnected(): boolean { return true; }
|
||||
|
||||
// Server Registry
|
||||
async registerServer(server: ServerRegistration): Promise<void> { }
|
||||
async unregisterServer(serverId: string): Promise<void> { }
|
||||
async heartbeat(serverId: string): Promise<void> { }
|
||||
async getServers(): Promise<ServerRegistration[]> { return []; }
|
||||
|
||||
// Room Registry
|
||||
async registerRoom(room: RoomRegistration): Promise<void> { }
|
||||
async unregisterRoom(roomId: string): Promise<void> { }
|
||||
async queryRooms(query: RoomQuery): Promise<RoomRegistration[]> { return []; }
|
||||
async findAvailableRoom(roomType: string): Promise<RoomRegistration | null> { return null; }
|
||||
|
||||
// State Snapshots
|
||||
async saveSnapshot(snapshot: RoomSnapshot): Promise<void> { }
|
||||
async loadSnapshot(roomId: string): Promise<RoomSnapshot | null> { return null; }
|
||||
|
||||
// Pub/Sub
|
||||
async publish(event: DistributedEvent): Promise<void> { }
|
||||
async subscribe(pattern: string, handler: Function): Promise<() => void> { return () => {}; }
|
||||
|
||||
// Distributed Locks
|
||||
async acquireLock(key: string, ttlMs: number): Promise<boolean> { return true; }
|
||||
async releaseLock(key: string): Promise<void> { }
|
||||
}
|
||||
```
|
||||
|
||||
## Player Routing Flow
|
||||
|
||||
```
|
||||
Client Server A Server B
|
||||
│ │ │
|
||||
│─── joinOrCreate ────────►│ │
|
||||
│ │ │
|
||||
│ │── findAvailableRoom() ───►│
|
||||
│ │◄──── room on Server B ────│
|
||||
│ │ │
|
||||
│◄─── redirect: B:3001 ────│ │
|
||||
│ │ │
|
||||
│───────────────── connect to Server B ───────────────►│
|
||||
│ │ │
|
||||
│◄─────────────────────────────── joined ─────────────│
|
||||
```
|
||||
|
||||
## Event Types
|
||||
|
||||
The distributed system publishes these events:
|
||||
|
||||
| Event | Description |
|
||||
|-------|-------------|
|
||||
| `server:online` | Server came online |
|
||||
| `server:offline` | Server went offline |
|
||||
| `server:draining` | Server is draining |
|
||||
| `room:created` | Room was created |
|
||||
| `room:disposed` | Room was disposed |
|
||||
| `room:updated` | Room info updated |
|
||||
| `room:message` | Cross-server room message |
|
||||
| `room:migrated` | Room migrated to another server |
|
||||
| `player:joined` | Player joined room |
|
||||
| `player:left` | Player left room |
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use Unique Server IDs** - Use hostname, container ID, or UUID
|
||||
|
||||
2. **Configure Proper Heartbeat** - Balance between freshness and network overhead
|
||||
|
||||
3. **Enable Snapshots for Stateful Rooms** - Ensure room state survives server restarts
|
||||
|
||||
4. **Handle Redirects Gracefully** - Client should reconnect to target server
|
||||
```typescript
|
||||
// Client handling redirect
|
||||
if (response.redirect) {
|
||||
await client.disconnect();
|
||||
await client.connect(response.redirect);
|
||||
await client.joinRoom(roomId);
|
||||
}
|
||||
```
|
||||
|
||||
5. **Use Distributed Locks** - Prevent race conditions in joinOrCreate
|
||||
|
||||
## Using createServer Integration
|
||||
|
||||
The simplest way to use distributed rooms is through `createServer`'s `distributed` config:
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server';
|
||||
import { RedisAdapter, Room } from '@esengine/server';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
class GameRoom extends Room {
|
||||
maxPlayers = 4;
|
||||
}
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
distributed: {
|
||||
enabled: true,
|
||||
adapter: new RedisAdapter({ factory: () => new Redis() }),
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'ws://192.168.1.100',
|
||||
serverPort: 3000,
|
||||
enableFailover: true,
|
||||
capacity: 100
|
||||
}
|
||||
});
|
||||
|
||||
server.define('game', GameRoom);
|
||||
await server.start();
|
||||
```
|
||||
|
||||
When clients call the `JoinRoom` API, the server will automatically:
|
||||
1. Find available rooms (local or remote)
|
||||
2. If room is on another server, send `$redirect` message to client
|
||||
3. Client receives redirect and connects to target server
|
||||
|
||||
## Load Balancing
|
||||
|
||||
Use `LoadBalancedRouter` for server selection:
|
||||
|
||||
```typescript
|
||||
import { LoadBalancedRouter, createLoadBalancedRouter } from '@esengine/server';
|
||||
|
||||
// Using factory function
|
||||
const router = createLoadBalancedRouter('least-players');
|
||||
|
||||
// Or create directly
|
||||
const router = new LoadBalancedRouter({
|
||||
strategy: 'least-rooms', // Select server with fewest rooms
|
||||
preferLocal: true // Prefer local server
|
||||
});
|
||||
|
||||
// Available strategies
|
||||
// - 'round-robin': Round robin selection
|
||||
// - 'least-rooms': Fewest rooms
|
||||
// - 'least-players': Fewest players
|
||||
// - 'random': Random selection
|
||||
// - 'weighted': Weighted by capacity usage
|
||||
```
|
||||
|
||||
## Failover
|
||||
|
||||
When a server goes offline with `enableFailover` enabled, the system will automatically:
|
||||
|
||||
1. Detect server offline (via heartbeat timeout)
|
||||
2. Query all rooms on that server
|
||||
3. Use distributed lock to prevent multiple servers recovering same room
|
||||
4. Restore room state from snapshot
|
||||
5. Publish `room:migrated` event to notify other servers
|
||||
|
||||
```typescript
|
||||
// Ensure periodic snapshots
|
||||
const manager = new DistributedRoomManager(adapter, {
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3000,
|
||||
snapshotInterval: 30000, // Save snapshot every 30 seconds
|
||||
enableFailover: true // Enable failover
|
||||
}, sendFn);
|
||||
```
|
||||
|
||||
## Future Releases
|
||||
|
||||
- Redis Cluster support
|
||||
- More load balancing strategies (geo-location, latency-aware)
|
||||
679
docs/src/content/docs/en/modules/network/http.md
Normal file
679
docs/src/content/docs/en/modules/network/http.md
Normal file
@@ -0,0 +1,679 @@
|
||||
---
|
||||
title: "HTTP Routing"
|
||||
description: "HTTP REST API routing with WebSocket port sharing support"
|
||||
---
|
||||
|
||||
`@esengine/server` includes a lightweight HTTP routing feature that can share the same port with WebSocket services, making it easy to implement REST APIs.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Inline Route Definition
|
||||
|
||||
The simplest way is to define HTTP routes directly when creating the server:
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
http: {
|
||||
'/api/health': (req, res) => {
|
||||
res.json({ status: 'ok', time: Date.now() })
|
||||
},
|
||||
'/api/users': {
|
||||
GET: (req, res) => {
|
||||
res.json({ users: [] })
|
||||
},
|
||||
POST: async (req, res) => {
|
||||
const body = req.body as { name: string }
|
||||
res.status(201).json({ id: '1', name: body.name })
|
||||
}
|
||||
}
|
||||
},
|
||||
cors: true // Enable CORS
|
||||
})
|
||||
|
||||
await server.start()
|
||||
```
|
||||
|
||||
### File-based Routing
|
||||
|
||||
For larger projects, file-based routing is recommended. Create a `src/http` directory where each file corresponds to a route:
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
interface LoginBody {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export default defineHttp<LoginBody>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body as LoginBody
|
||||
|
||||
// Validate user...
|
||||
if (username === 'admin' && password === '123456') {
|
||||
res.json({ token: 'jwt-token-here', userId: 'user-1' })
|
||||
} else {
|
||||
res.error(401, 'Invalid username or password')
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
// server.ts
|
||||
import { createServer } from '@esengine/server'
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
httpDir: './src/http', // HTTP routes directory
|
||||
httpPrefix: '/api', // Route prefix
|
||||
cors: true
|
||||
})
|
||||
|
||||
await server.start()
|
||||
// Route: POST /api/login
|
||||
```
|
||||
|
||||
## defineHttp Definition
|
||||
|
||||
`defineHttp` is used to define type-safe HTTP handlers:
|
||||
|
||||
```typescript
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
interface CreateUserBody {
|
||||
username: string
|
||||
email: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export default defineHttp<CreateUserBody>({
|
||||
// HTTP method (default POST)
|
||||
method: 'POST',
|
||||
|
||||
// Handler function
|
||||
handler(req, res) {
|
||||
const body = req.body as CreateUserBody
|
||||
// Handle request...
|
||||
res.status(201).json({ id: 'new-user-id' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Supported HTTP Methods
|
||||
|
||||
```typescript
|
||||
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
|
||||
```
|
||||
|
||||
## HttpRequest Object
|
||||
|
||||
The HTTP request object contains the following properties:
|
||||
|
||||
```typescript
|
||||
interface HttpRequest {
|
||||
/** Raw Node.js IncomingMessage */
|
||||
raw: IncomingMessage
|
||||
|
||||
/** HTTP method */
|
||||
method: string
|
||||
|
||||
/** Request path */
|
||||
path: string
|
||||
|
||||
/** Route parameters (extracted from URL path, e.g., /users/:id) */
|
||||
params: Record<string, string>
|
||||
|
||||
/** Query parameters */
|
||||
query: Record<string, string>
|
||||
|
||||
/** Request headers */
|
||||
headers: Record<string, string | string[] | undefined>
|
||||
|
||||
/** Parsed request body */
|
||||
body: unknown
|
||||
|
||||
/** Client IP */
|
||||
ip: string
|
||||
}
|
||||
```
|
||||
|
||||
### Usage Examples
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// Get query parameters
|
||||
const page = parseInt(req.query.page ?? '1')
|
||||
const limit = parseInt(req.query.limit ?? '10')
|
||||
|
||||
// Get request headers
|
||||
const authHeader = req.headers.authorization
|
||||
|
||||
// Get client IP
|
||||
console.log('Request from:', req.ip)
|
||||
|
||||
res.json({ page, limit })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Body Parsing
|
||||
|
||||
The request body is automatically parsed based on `Content-Type`:
|
||||
|
||||
- `application/json` - Parsed as JSON object
|
||||
- `application/x-www-form-urlencoded` - Parsed as key-value object
|
||||
- Others - Kept as raw string
|
||||
|
||||
```typescript
|
||||
export default defineHttp<{ name: string; age: number }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
// body is already parsed
|
||||
const { name, age } = req.body as { name: string; age: number }
|
||||
res.json({ received: { name, age } })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## HttpResponse Object
|
||||
|
||||
The HTTP response object provides a chainable API:
|
||||
|
||||
```typescript
|
||||
interface HttpResponse {
|
||||
/** Raw Node.js ServerResponse */
|
||||
raw: ServerResponse
|
||||
|
||||
/** Set status code */
|
||||
status(code: number): HttpResponse
|
||||
|
||||
/** Set response header */
|
||||
header(name: string, value: string): HttpResponse
|
||||
|
||||
/** Send JSON response */
|
||||
json(data: unknown): void
|
||||
|
||||
/** Send text response */
|
||||
text(data: string): void
|
||||
|
||||
/** Send error response */
|
||||
error(code: number, message: string): void
|
||||
}
|
||||
```
|
||||
|
||||
### Usage Examples
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
// Set status code and custom headers
|
||||
res
|
||||
.status(201)
|
||||
.header('X-Custom-Header', 'value')
|
||||
.json({ created: true })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// Send error response
|
||||
res.error(404, 'Resource not found')
|
||||
// Equivalent to: res.status(404).json({ error: 'Resource not found' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// Send plain text
|
||||
res.text('Hello, World!')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## File Routing Conventions
|
||||
|
||||
### Name Conversion
|
||||
|
||||
File names are automatically converted to route paths:
|
||||
|
||||
| File Path | Route Path (prefix=/api) |
|
||||
|-----------|-------------------------|
|
||||
| `login.ts` | `/api/login` |
|
||||
| `users/profile.ts` | `/api/users/profile` |
|
||||
| `users/[id].ts` | `/api/users/:id` |
|
||||
| `game/room/[roomId].ts` | `/api/game/room/:roomId` |
|
||||
|
||||
### Dynamic Route Parameters
|
||||
|
||||
Use `[param]` syntax to define dynamic parameters:
|
||||
|
||||
```typescript
|
||||
// src/http/users/[id].ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// Get route parameter directly from params
|
||||
const { id } = req.params
|
||||
res.json({ userId: id })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Multiple parameters:
|
||||
|
||||
```typescript
|
||||
// src/http/users/[userId]/posts/[postId].ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
const { userId, postId } = req.params
|
||||
res.json({ userId, postId })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Skip Rules
|
||||
|
||||
The following files are automatically skipped:
|
||||
|
||||
- Files starting with `_` (e.g., `_helper.ts`)
|
||||
- `index.ts` / `index.js` files
|
||||
- Non `.ts` / `.js` / `.mts` / `.mjs` files
|
||||
|
||||
### Directory Structure Example
|
||||
|
||||
```
|
||||
src/
|
||||
└── http/
|
||||
├── _utils.ts # Skipped (underscore prefix)
|
||||
├── index.ts # Skipped (index file)
|
||||
├── health.ts # GET /api/health
|
||||
├── login.ts # POST /api/login
|
||||
├── register.ts # POST /api/register
|
||||
└── users/
|
||||
├── index.ts # Skipped
|
||||
├── list.ts # GET /api/users/list
|
||||
└── [id].ts # GET /api/users/:id
|
||||
```
|
||||
|
||||
## CORS Configuration
|
||||
|
||||
### Quick Enable
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
cors: true // Use default configuration
|
||||
})
|
||||
```
|
||||
|
||||
### Custom Configuration
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
cors: {
|
||||
// Allowed origins
|
||||
origin: ['http://localhost:5173', 'https://myapp.com'],
|
||||
// Or use wildcard
|
||||
// origin: '*',
|
||||
// origin: true, // Reflect request origin
|
||||
|
||||
// Allowed HTTP methods
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
||||
|
||||
// Allowed request headers
|
||||
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
|
||||
|
||||
// Allow credentials (cookies)
|
||||
credentials: true,
|
||||
|
||||
// Preflight cache max age (seconds)
|
||||
maxAge: 86400
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### CorsOptions Type
|
||||
|
||||
```typescript
|
||||
interface CorsOptions {
|
||||
/** Allowed origins: string, string array, true (reflect) or '*' */
|
||||
origin?: string | string[] | boolean
|
||||
|
||||
/** Allowed HTTP methods */
|
||||
methods?: string[]
|
||||
|
||||
/** Allowed request headers */
|
||||
allowedHeaders?: string[]
|
||||
|
||||
/** Allow credentials */
|
||||
credentials?: boolean
|
||||
|
||||
/** Preflight cache max age (seconds) */
|
||||
maxAge?: number
|
||||
}
|
||||
```
|
||||
|
||||
## Route Merging
|
||||
|
||||
File routes and inline routes can be used together, with inline routes having higher priority:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
httpDir: './src/http',
|
||||
httpPrefix: '/api',
|
||||
|
||||
// Inline routes merge with file routes
|
||||
http: {
|
||||
'/health': (req, res) => res.json({ status: 'ok' }),
|
||||
'/api/special': (req, res) => res.json({ special: true })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Sharing Port with WebSocket
|
||||
|
||||
HTTP routes automatically share the same port with WebSocket services:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
// WebSocket related config
|
||||
apiDir: './src/api',
|
||||
msgDir: './src/msg',
|
||||
|
||||
// HTTP related config
|
||||
httpDir: './src/http',
|
||||
httpPrefix: '/api',
|
||||
cors: true
|
||||
})
|
||||
|
||||
await server.start()
|
||||
|
||||
// Same port 3000:
|
||||
// - WebSocket: ws://localhost:3000
|
||||
// - HTTP API: http://localhost:3000/api/*
|
||||
```
|
||||
|
||||
## Complete Examples
|
||||
|
||||
### Game Server Login API
|
||||
|
||||
```typescript
|
||||
// src/http/auth/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
import { createJwtAuthProvider } from '@esengine/server/auth'
|
||||
|
||||
interface LoginRequest {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
interface LoginResponse {
|
||||
token: string
|
||||
userId: string
|
||||
expiresAt: number
|
||||
}
|
||||
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600
|
||||
})
|
||||
|
||||
export default defineHttp<LoginRequest>({
|
||||
method: 'POST',
|
||||
async handler(req, res) {
|
||||
const { username, password } = req.body as LoginRequest
|
||||
|
||||
// Validate user
|
||||
const user = await db.users.findByUsername(username)
|
||||
if (!user || !await verifyPassword(password, user.passwordHash)) {
|
||||
res.error(401, 'Invalid username or password')
|
||||
return
|
||||
}
|
||||
|
||||
// Generate JWT
|
||||
const token = jwtProvider.sign({
|
||||
sub: user.id,
|
||||
name: user.username,
|
||||
roles: user.roles
|
||||
})
|
||||
|
||||
const response: LoginResponse = {
|
||||
token,
|
||||
userId: user.id,
|
||||
expiresAt: Date.now() + 3600 * 1000
|
||||
}
|
||||
|
||||
res.json(response)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Game Data Query API
|
||||
|
||||
```typescript
|
||||
// src/http/game/leaderboard.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
async handler(req, res) {
|
||||
const limit = parseInt(req.query.limit ?? '10')
|
||||
const offset = parseInt(req.query.offset ?? '0')
|
||||
|
||||
const players = await db.players.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit,
|
||||
offset
|
||||
})
|
||||
|
||||
res.json({
|
||||
data: players,
|
||||
pagination: { limit, offset }
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Middleware
|
||||
|
||||
### Middleware Type
|
||||
|
||||
Middleware are functions that execute before and after route handlers:
|
||||
|
||||
```typescript
|
||||
type HttpMiddleware = (
|
||||
req: HttpRequest,
|
||||
res: HttpResponse,
|
||||
next: () => Promise<void>
|
||||
) => void | Promise<void>
|
||||
```
|
||||
|
||||
### Built-in Middleware
|
||||
|
||||
```typescript
|
||||
import {
|
||||
requestLogger,
|
||||
bodyLimit,
|
||||
responseTime,
|
||||
requestId,
|
||||
securityHeaders
|
||||
} from '@esengine/server'
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
http: { /* ... */ },
|
||||
// Global middleware configured via createHttpRouter
|
||||
})
|
||||
```
|
||||
|
||||
#### requestLogger - Request Logging
|
||||
|
||||
```typescript
|
||||
import { requestLogger } from '@esengine/server'
|
||||
|
||||
// Log request and response time
|
||||
requestLogger()
|
||||
|
||||
// Also log request body
|
||||
requestLogger({ logBody: true })
|
||||
```
|
||||
|
||||
#### bodyLimit - Request Body Size Limit
|
||||
|
||||
```typescript
|
||||
import { bodyLimit } from '@esengine/server'
|
||||
|
||||
// Limit request body to 1MB
|
||||
bodyLimit(1024 * 1024)
|
||||
```
|
||||
|
||||
#### responseTime - Response Time Header
|
||||
|
||||
```typescript
|
||||
import { responseTime } from '@esengine/server'
|
||||
|
||||
// Automatically add X-Response-Time header
|
||||
responseTime()
|
||||
```
|
||||
|
||||
#### requestId - Request ID
|
||||
|
||||
```typescript
|
||||
import { requestId } from '@esengine/server'
|
||||
|
||||
// Auto-generate and add X-Request-ID header
|
||||
requestId()
|
||||
|
||||
// Custom header name
|
||||
requestId('X-Trace-ID')
|
||||
```
|
||||
|
||||
#### securityHeaders - Security Headers
|
||||
|
||||
```typescript
|
||||
import { securityHeaders } from '@esengine/server'
|
||||
|
||||
// Add common security response headers
|
||||
securityHeaders()
|
||||
|
||||
// Custom configuration
|
||||
securityHeaders({
|
||||
hidePoweredBy: true,
|
||||
frameOptions: 'DENY',
|
||||
noSniff: true
|
||||
})
|
||||
```
|
||||
|
||||
### Custom Middleware
|
||||
|
||||
```typescript
|
||||
import type { HttpMiddleware } from '@esengine/server'
|
||||
|
||||
// Authentication middleware
|
||||
const authMiddleware: HttpMiddleware = async (req, res, next) => {
|
||||
const token = req.headers.authorization?.replace('Bearer ', '')
|
||||
|
||||
if (!token) {
|
||||
res.error(401, 'Unauthorized')
|
||||
return // Don't call next(), terminate request
|
||||
}
|
||||
|
||||
// Validate token...
|
||||
(req as any).userId = 'decoded-user-id'
|
||||
|
||||
await next() // Continue to next middleware and handler
|
||||
}
|
||||
```
|
||||
|
||||
### Using Middleware
|
||||
|
||||
#### With createHttpRouter
|
||||
|
||||
```typescript
|
||||
import { createHttpRouter, requestLogger, bodyLimit } from '@esengine/server'
|
||||
|
||||
const router = createHttpRouter({
|
||||
'/api/users': (req, res) => res.json([]),
|
||||
'/api/admin': {
|
||||
GET: {
|
||||
handler: (req, res) => res.json({ admin: true }),
|
||||
middlewares: [adminAuthMiddleware] // Route-level middleware
|
||||
}
|
||||
}
|
||||
}, {
|
||||
middlewares: [requestLogger(), bodyLimit(1024 * 1024)], // Global middleware
|
||||
timeout: 30000 // Global timeout 30 seconds
|
||||
})
|
||||
```
|
||||
|
||||
## Request Timeout
|
||||
|
||||
### Global Timeout
|
||||
|
||||
```typescript
|
||||
import { createHttpRouter } from '@esengine/server'
|
||||
|
||||
const router = createHttpRouter({
|
||||
'/api/data': async (req, res) => {
|
||||
// If processing exceeds 30 seconds, auto-return 408 Request Timeout
|
||||
await someSlowOperation()
|
||||
res.json({ data: 'result' })
|
||||
}
|
||||
}, {
|
||||
timeout: 30000 // 30 seconds
|
||||
})
|
||||
```
|
||||
|
||||
### Route-level Timeout
|
||||
|
||||
```typescript
|
||||
const router = createHttpRouter({
|
||||
'/api/quick': (req, res) => res.json({ fast: true }),
|
||||
|
||||
'/api/slow': {
|
||||
POST: {
|
||||
handler: async (req, res) => {
|
||||
await verySlowOperation()
|
||||
res.json({ done: true })
|
||||
},
|
||||
timeout: 120000 // This route allows 2 minutes
|
||||
}
|
||||
}
|
||||
}, {
|
||||
timeout: 10000 // Global 10 seconds (overridden by route-level)
|
||||
})
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use defineHttp** - Get better type hints and code organization
|
||||
2. **Unified Error Handling** - Use `res.error()` to return consistent error format
|
||||
3. **Enable CORS** - Required for frontend-backend separation
|
||||
4. **Directory Organization** - Organize HTTP route files by functional modules
|
||||
5. **Validate Input** - Always validate `req.body` and `req.query` content
|
||||
6. **Status Code Standards** - Follow HTTP status code conventions (200, 201, 400, 401, 404, 500, etc.)
|
||||
7. **Use Middleware** - Implement cross-cutting concerns like auth, logging, rate limiting via middleware
|
||||
8. **Set Timeouts** - Prevent slow requests from blocking the server
|
||||
@@ -147,6 +147,7 @@ service.on('chat', (data) => {
|
||||
|
||||
- [Client Usage](/en/modules/network/client/) - NetworkPlugin, components and systems
|
||||
- [Server Side](/en/modules/network/server/) - GameServer and Room management
|
||||
- [Distributed Rooms](/en/modules/network/distributed/) - Multi-server room management and player routing
|
||||
- [State Sync](/en/modules/network/sync/) - Interpolation and snapshot buffering
|
||||
- [Client Prediction](/en/modules/network/prediction/) - Input prediction and server reconciliation
|
||||
- [Area of Interest (AOI)](/en/modules/network/aoi/) - View filtering and bandwidth optimization
|
||||
|
||||
@@ -79,10 +79,33 @@ await server.start()
|
||||
| `tickRate` | `number` | `20` | Global tick rate (Hz) |
|
||||
| `apiDir` | `string` | `'src/api'` | API handlers directory |
|
||||
| `msgDir` | `string` | `'src/msg'` | Message handlers directory |
|
||||
| `httpDir` | `string` | `'src/http'` | HTTP routes directory |
|
||||
| `httpPrefix` | `string` | `'/api'` | HTTP routes prefix |
|
||||
| `cors` | `boolean \| CorsOptions` | - | CORS configuration |
|
||||
| `onStart` | `(port) => void` | - | Start callback |
|
||||
| `onConnect` | `(conn) => void` | - | Connection callback |
|
||||
| `onDisconnect` | `(conn) => void` | - | Disconnect callback |
|
||||
|
||||
## HTTP Routing
|
||||
|
||||
Supports HTTP API sharing the same port with WebSocket, ideal for login, registration, and similar scenarios.
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
httpDir: './src/http', // HTTP routes directory
|
||||
httpPrefix: '/api', // Route prefix
|
||||
cors: true,
|
||||
|
||||
// Or inline definition
|
||||
http: {
|
||||
'/health': (req, res) => res.json({ status: 'ok' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
> For detailed documentation, see [HTTP Routing](/en/modules/network/http)
|
||||
|
||||
## Room System
|
||||
|
||||
Room is the base class for game rooms, managing players and game state.
|
||||
@@ -243,6 +266,122 @@ class GameRoom extends Room {
|
||||
}
|
||||
```
|
||||
|
||||
## Schema Validation
|
||||
|
||||
Use the built-in Schema validation system for runtime type validation:
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import { s, defineApiWithSchema } from '@esengine/server'
|
||||
|
||||
// Define schema
|
||||
const MoveSchema = s.object({
|
||||
x: s.number(),
|
||||
y: s.number(),
|
||||
speed: s.number().optional()
|
||||
})
|
||||
|
||||
// Auto type inference
|
||||
type Move = s.infer<typeof MoveSchema> // { x: number; y: number; speed?: number }
|
||||
|
||||
// Use schema to define API (auto validation)
|
||||
export default defineApiWithSchema(MoveSchema, {
|
||||
handler(req, ctx) {
|
||||
// req is validated, type-safe
|
||||
console.log(req.x, req.y)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Validator Types
|
||||
|
||||
| Type | Example | Description |
|
||||
|------|---------|-------------|
|
||||
| `s.string()` | `s.string().min(1).max(50)` | String with length constraints |
|
||||
| `s.number()` | `s.number().min(0).int()` | Number with range and integer constraints |
|
||||
| `s.boolean()` | `s.boolean()` | Boolean |
|
||||
| `s.literal()` | `s.literal('admin')` | Literal type |
|
||||
| `s.object()` | `s.object({ name: s.string() })` | Object |
|
||||
| `s.array()` | `s.array(s.number())` | Array |
|
||||
| `s.enum()` | `s.enum(['a', 'b'] as const)` | Enum |
|
||||
| `s.union()` | `s.union([s.string(), s.number()])` | Union type |
|
||||
| `s.record()` | `s.record(s.any())` | Record type |
|
||||
|
||||
### Modifiers
|
||||
|
||||
```typescript
|
||||
// Optional field
|
||||
s.string().optional()
|
||||
|
||||
// Default value
|
||||
s.number().default(0)
|
||||
|
||||
// Nullable
|
||||
s.string().nullable()
|
||||
|
||||
// String validation
|
||||
s.string().min(1).max(100).email().url().regex(/^[a-z]+$/)
|
||||
|
||||
// Number validation
|
||||
s.number().min(0).max(100).int().positive()
|
||||
|
||||
// Array validation
|
||||
s.array(s.string()).min(1).max(10).nonempty()
|
||||
|
||||
// Object validation
|
||||
s.object({ ... }).strict() // No extra fields allowed
|
||||
s.object({ ... }).partial() // All fields optional
|
||||
s.object({ ... }).pick('name', 'age') // Pick fields
|
||||
s.object({ ... }).omit('password') // Omit fields
|
||||
```
|
||||
|
||||
### Message Validation
|
||||
|
||||
```typescript
|
||||
import { s, defineMsgWithSchema } from '@esengine/server'
|
||||
|
||||
const InputSchema = s.object({
|
||||
keys: s.array(s.string()),
|
||||
timestamp: s.number()
|
||||
})
|
||||
|
||||
export default defineMsgWithSchema(InputSchema, {
|
||||
handler(msg, ctx) {
|
||||
// msg is validated
|
||||
console.log(msg.keys, msg.timestamp)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Manual Validation
|
||||
|
||||
```typescript
|
||||
import { s, parse, safeParse, createGuard } from '@esengine/server'
|
||||
|
||||
const UserSchema = s.object({
|
||||
name: s.string(),
|
||||
age: s.number().int().min(0)
|
||||
})
|
||||
|
||||
// Throws on error
|
||||
const user = parse(UserSchema, data)
|
||||
|
||||
// Returns result object
|
||||
const result = safeParse(UserSchema, data)
|
||||
if (result.success) {
|
||||
console.log(result.data)
|
||||
} else {
|
||||
console.error(result.error)
|
||||
}
|
||||
|
||||
// Type guard
|
||||
const isUser = createGuard(UserSchema)
|
||||
if (isUser(data)) {
|
||||
// data is User type
|
||||
}
|
||||
```
|
||||
|
||||
## Protocol Definition
|
||||
|
||||
Define shared types in `src/shared/protocol.ts`:
|
||||
|
||||
@@ -125,23 +125,24 @@ tx:data:{key} - Business data
|
||||
|
||||
## MongoStorage
|
||||
|
||||
MongoDB storage, suitable for scenarios requiring persistence and complex queries. Uses factory pattern with lazy connection.
|
||||
MongoDB storage, suitable for scenarios requiring persistence and complex queries. Uses shared connection from `@esengine/database-drivers`.
|
||||
|
||||
```typescript
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { MongoStorage } from '@esengine/transaction';
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// Factory pattern: lazy connection, connects on first operation
|
||||
const storage = new MongoStorage({
|
||||
factory: async () => {
|
||||
const client = new MongoClient('mongodb://localhost:27017');
|
||||
await client.connect();
|
||||
return client;
|
||||
},
|
||||
database: 'game',
|
||||
transactionCollection: 'transactions', // Transaction log collection
|
||||
dataCollection: 'transaction_data', // Business data collection
|
||||
lockCollection: 'transaction_locks', // Lock collection
|
||||
// Create shared connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// Create storage using shared connection
|
||||
const storage = createMongoStorage(mongo, {
|
||||
transactionCollection: 'transactions', // Transaction log collection (optional)
|
||||
dataCollection: 'transaction_data', // Business data collection (optional)
|
||||
lockCollection: 'transaction_locks', // Lock collection (optional)
|
||||
});
|
||||
|
||||
// Create indexes (run on first startup)
|
||||
@@ -149,11 +150,14 @@ await storage.ensureIndexes();
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
|
||||
// Close connection when done
|
||||
// Close storage (does not close shared connection)
|
||||
await storage.close();
|
||||
|
||||
// Or use await using for automatic cleanup (TypeScript 5.2+)
|
||||
await using storage = new MongoStorage({ ... });
|
||||
// Shared connection can continue to be used by other modules
|
||||
const userRepo = new UserRepository(mongo); // @esengine/database
|
||||
|
||||
// Finally close the shared connection
|
||||
await mongo.disconnect();
|
||||
```
|
||||
|
||||
### Characteristics
|
||||
|
||||
@@ -606,6 +606,107 @@ export class RetryDecorator implements INodeExecutor {
|
||||
}
|
||||
```
|
||||
|
||||
## 在代码中使用自定义执行器
|
||||
|
||||
定义了自定义执行器后,可以通过 `BehaviorTreeBuilder` 的 `.action()` 和 `.condition()` 方法在代码中使用:
|
||||
|
||||
### 使用 action() 方法
|
||||
|
||||
```typescript
|
||||
import { BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
|
||||
|
||||
// 使用自定义执行器构建行为树
|
||||
const tree = BehaviorTreeBuilder.create('CombatAI')
|
||||
.defineBlackboardVariable('health', 100)
|
||||
.defineBlackboardVariable('target', null)
|
||||
.selector('Root')
|
||||
.sequence('AttackSequence')
|
||||
// 使用自定义动作 - implementationType 匹配装饰器中的定义
|
||||
.action('AttackAction', 'Attack', { damage: 25 })
|
||||
.action('MoveToPosition', 'Chase', { speed: 10 })
|
||||
.end()
|
||||
.action('DelayAction', 'Idle', { duration: 1.0 })
|
||||
.end()
|
||||
.build();
|
||||
|
||||
// 启动行为树
|
||||
const entity = scene.createEntity('Enemy');
|
||||
BehaviorTreeStarter.start(entity, tree);
|
||||
```
|
||||
|
||||
### 使用 condition() 方法
|
||||
|
||||
```typescript
|
||||
const tree = BehaviorTreeBuilder.create('AI')
|
||||
.selector('Root')
|
||||
.sequence('AttackBranch')
|
||||
// 使用自定义条件
|
||||
.condition('CheckHealth', 'IsHealthy', { threshold: 50, operator: 'greater' })
|
||||
.action('AttackAction', 'Attack')
|
||||
.end()
|
||||
.end()
|
||||
.build();
|
||||
```
|
||||
|
||||
### Builder 方法对照表
|
||||
|
||||
| 方法 | 说明 | 使用场景 |
|
||||
|------|------|----------|
|
||||
| `.action(type, name?, config?)` | 使用自定义动作执行器 | 自定义 Action 类 |
|
||||
| `.condition(type, name?, config?)` | 使用自定义条件执行器 | 自定义 Condition 类 |
|
||||
| `.executeAction(name)` | 调用黑板函数 `action_{name}` | 简单逻辑、快速原型 |
|
||||
| `.executeCondition(name)` | 调用黑板函数 `condition_{name}` | 简单条件判断 |
|
||||
|
||||
### 完整示例
|
||||
|
||||
```typescript
|
||||
import {
|
||||
BehaviorTreeBuilder,
|
||||
BehaviorTreeStarter,
|
||||
NodeExecutorMetadata,
|
||||
INodeExecutor,
|
||||
NodeExecutionContext,
|
||||
TaskStatus,
|
||||
NodeType,
|
||||
BindingHelper
|
||||
} from '@esengine/behavior-tree';
|
||||
|
||||
// 1. 定义自定义执行器
|
||||
@NodeExecutorMetadata({
|
||||
implementationType: 'AttackAction',
|
||||
nodeType: NodeType.Action,
|
||||
displayName: '攻击',
|
||||
category: 'Combat',
|
||||
configSchema: {
|
||||
damage: { type: 'number', default: 10, supportBinding: true }
|
||||
}
|
||||
})
|
||||
class AttackAction implements INodeExecutor {
|
||||
execute(context: NodeExecutionContext): TaskStatus {
|
||||
const damage = BindingHelper.getValue<number>(context, 'damage', 10);
|
||||
console.log(`执行攻击,造成 ${damage} 点伤害!`);
|
||||
return TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 构建行为树
|
||||
const enemyAI = BehaviorTreeBuilder.create('EnemyAI')
|
||||
.defineBlackboardVariable('health', 100)
|
||||
.defineBlackboardVariable('target', null)
|
||||
.selector('MainBehavior')
|
||||
.sequence('AttackBranch')
|
||||
.condition('CheckHealth', 'HasEnoughHealth', { threshold: 20, operator: 'greater' })
|
||||
.action('AttackAction', 'Attack', { damage: 50 })
|
||||
.end()
|
||||
.log('逃跑', 'Flee')
|
||||
.end()
|
||||
.build();
|
||||
|
||||
// 3. 启动行为树
|
||||
const entity = scene.createEntity('Enemy');
|
||||
BehaviorTreeStarter.start(entity, enemyAI);
|
||||
```
|
||||
|
||||
## 注册执行器
|
||||
|
||||
### 自动注册
|
||||
|
||||
136
docs/src/content/docs/modules/database-drivers/index.md
Normal file
136
docs/src/content/docs/modules/database-drivers/index.md
Normal file
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "数据库驱动"
|
||||
description: "MongoDB、Redis 等数据库的连接管理和驱动封装"
|
||||
---
|
||||
|
||||
`@esengine/database-drivers` 是 ESEngine 的数据库连接管理层,提供 MongoDB、Redis 等数据库的统一连接管理。
|
||||
|
||||
## 特性
|
||||
|
||||
- **连接池管理** - 自动管理连接池,优化资源使用
|
||||
- **自动重连** - 连接断开时自动重连
|
||||
- **事件通知** - 连接状态变化事件
|
||||
- **类型解耦** - 简化接口,不依赖原生驱动类型
|
||||
- **共享连接** - 单一连接可供多个模块共享
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm install @esengine/database-drivers
|
||||
```
|
||||
|
||||
**对等依赖:**
|
||||
```bash
|
||||
npm install mongodb # MongoDB 支持
|
||||
npm install ioredis # Redis 支持
|
||||
```
|
||||
|
||||
## 架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ @esengine/database-drivers (Layer 1) │
|
||||
│ ┌─────────────────────┐ ┌─────────────────────┐ │
|
||||
│ │ MongoConnection │ │ RedisConnection │ │
|
||||
│ │ - 连接池管理 │ │ - 自动重连 │ │
|
||||
│ │ - 自动重连 │ │ - Key 前缀 │ │
|
||||
│ │ - 事件发射器 │ │ - 事件发射器 │ │
|
||||
│ └──────────┬──────────┘ └─────────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────▼──────────┐ │
|
||||
│ │ IMongoCollection<T> │ ← 类型安全接口 │
|
||||
│ │ (适配器模式) │ 与 mongodb 类型解耦 │
|
||||
│ └─────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌───────────────────────┐ ┌───────────────────────┐
|
||||
│ @esengine/database │ │ @esengine/transaction │
|
||||
│ (仓库模式) │ │ (分布式事务) │
|
||||
└───────────────────────┘ └───────────────────────┘
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### MongoDB 连接
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
// 创建连接
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20
|
||||
},
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
// 监听事件
|
||||
mongo.on('connected', () => console.log('MongoDB 已连接'))
|
||||
mongo.on('disconnected', () => console.log('MongoDB 已断开'))
|
||||
mongo.on('error', (e) => console.error('错误:', e.error))
|
||||
|
||||
// 建立连接
|
||||
await mongo.connect()
|
||||
|
||||
// 使用集合
|
||||
const users = mongo.collection<User>('users')
|
||||
await users.insertOne({ name: 'John', score: 100 })
|
||||
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
// 完成后断开连接
|
||||
await mongo.disconnect()
|
||||
```
|
||||
|
||||
### Redis 连接
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
await redis.connect()
|
||||
|
||||
// 基本操作
|
||||
await redis.set('session:123', 'data', 3600) // 带 TTL
|
||||
const value = await redis.get('session:123')
|
||||
|
||||
await redis.disconnect()
|
||||
```
|
||||
|
||||
## 服务容器集成
|
||||
|
||||
```typescript
|
||||
import { ServiceContainer } from '@esengine/ecs-framework'
|
||||
import {
|
||||
createMongoConnection,
|
||||
MongoConnectionToken,
|
||||
RedisConnectionToken
|
||||
} from '@esengine/database-drivers'
|
||||
|
||||
const services = new ServiceContainer()
|
||||
|
||||
// 注册连接
|
||||
const mongo = createMongoConnection({ uri: '...', database: 'game' })
|
||||
await mongo.connect()
|
||||
services.register(MongoConnectionToken, mongo)
|
||||
|
||||
// 在其他模块中获取
|
||||
const connection = services.get(MongoConnectionToken)
|
||||
const users = connection.collection('users')
|
||||
```
|
||||
|
||||
## 文档
|
||||
|
||||
- [MongoDB 连接](/modules/database-drivers/mongo/) - MongoDB 连接详细配置
|
||||
- [Redis 连接](/modules/database-drivers/redis/) - Redis 连接详细配置
|
||||
- [服务令牌](/modules/database-drivers/tokens/) - 依赖注入集成
|
||||
265
docs/src/content/docs/modules/database-drivers/mongo.md
Normal file
265
docs/src/content/docs/modules/database-drivers/mongo.md
Normal file
@@ -0,0 +1,265 @@
|
||||
---
|
||||
title: "MongoDB 连接"
|
||||
description: "MongoDB 连接管理、连接池、自动重连"
|
||||
---
|
||||
|
||||
## 配置选项
|
||||
|
||||
```typescript
|
||||
interface MongoConnectionConfig {
|
||||
/** MongoDB 连接 URI */
|
||||
uri: string
|
||||
|
||||
/** 数据库名称 */
|
||||
database: string
|
||||
|
||||
/** 连接池配置 */
|
||||
pool?: {
|
||||
minSize?: number // 最小连接数
|
||||
maxSize?: number // 最大连接数
|
||||
acquireTimeout?: number // 获取连接超时(毫秒)
|
||||
maxLifetime?: number // 连接最大生命周期(毫秒)
|
||||
}
|
||||
|
||||
/** 是否自动重连(默认 true) */
|
||||
autoReconnect?: boolean
|
||||
|
||||
/** 重连间隔(毫秒,默认 5000) */
|
||||
reconnectInterval?: number
|
||||
|
||||
/** 最大重连次数(默认 10) */
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection, MongoConnectionToken } from '@esengine/database-drivers'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20,
|
||||
acquireTimeout: 5000,
|
||||
maxLifetime: 300000
|
||||
},
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10
|
||||
})
|
||||
|
||||
// 事件监听
|
||||
mongo.on('connected', () => {
|
||||
console.log('MongoDB 已连接')
|
||||
})
|
||||
|
||||
mongo.on('disconnected', () => {
|
||||
console.log('MongoDB 已断开')
|
||||
})
|
||||
|
||||
mongo.on('reconnecting', () => {
|
||||
console.log('MongoDB 正在重连...')
|
||||
})
|
||||
|
||||
mongo.on('reconnected', () => {
|
||||
console.log('MongoDB 重连成功')
|
||||
})
|
||||
|
||||
mongo.on('error', (event) => {
|
||||
console.error('MongoDB 错误:', event.error)
|
||||
})
|
||||
|
||||
// 连接
|
||||
await mongo.connect()
|
||||
|
||||
// 检查状态
|
||||
console.log('已连接:', mongo.isConnected())
|
||||
console.log('Ping:', await mongo.ping())
|
||||
```
|
||||
|
||||
## IMongoConnection 接口
|
||||
|
||||
```typescript
|
||||
interface IMongoConnection {
|
||||
/** 连接 ID */
|
||||
readonly id: string
|
||||
|
||||
/** 连接状态 */
|
||||
readonly state: ConnectionState
|
||||
|
||||
/** 建立连接 */
|
||||
connect(): Promise<void>
|
||||
|
||||
/** 断开连接 */
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/** 检查是否已连接 */
|
||||
isConnected(): boolean
|
||||
|
||||
/** 测试连接 */
|
||||
ping(): Promise<boolean>
|
||||
|
||||
/** 获取类型化集合 */
|
||||
collection<T extends object>(name: string): IMongoCollection<T>
|
||||
|
||||
/** 获取数据库接口 */
|
||||
getDatabase(): IMongoDatabase
|
||||
|
||||
/** 获取原生客户端(高级用法) */
|
||||
getNativeClient(): MongoClientType
|
||||
|
||||
/** 获取原生数据库(高级用法) */
|
||||
getNativeDatabase(): Db
|
||||
}
|
||||
```
|
||||
|
||||
## IMongoCollection 接口
|
||||
|
||||
类型安全的集合接口,与原生 MongoDB 类型解耦:
|
||||
|
||||
```typescript
|
||||
interface IMongoCollection<T extends object> {
|
||||
readonly name: string
|
||||
|
||||
// 查询
|
||||
findOne(filter: object, options?: FindOptions): Promise<T | null>
|
||||
find(filter: object, options?: FindOptions): Promise<T[]>
|
||||
countDocuments(filter?: object): Promise<number>
|
||||
|
||||
// 插入
|
||||
insertOne(doc: T): Promise<InsertOneResult>
|
||||
insertMany(docs: T[]): Promise<InsertManyResult>
|
||||
|
||||
// 更新
|
||||
updateOne(filter: object, update: object): Promise<UpdateResult>
|
||||
updateMany(filter: object, update: object): Promise<UpdateResult>
|
||||
findOneAndUpdate(
|
||||
filter: object,
|
||||
update: object,
|
||||
options?: FindOneAndUpdateOptions
|
||||
): Promise<T | null>
|
||||
|
||||
// 删除
|
||||
deleteOne(filter: object): Promise<DeleteResult>
|
||||
deleteMany(filter: object): Promise<DeleteResult>
|
||||
|
||||
// 索引
|
||||
createIndex(
|
||||
spec: Record<string, 1 | -1>,
|
||||
options?: IndexOptions
|
||||
): Promise<string>
|
||||
}
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基本 CRUD
|
||||
|
||||
```typescript
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
score: number
|
||||
}
|
||||
|
||||
const users = mongo.collection<User>('users')
|
||||
|
||||
// 插入
|
||||
await users.insertOne({
|
||||
id: '1',
|
||||
name: 'John',
|
||||
email: 'john@example.com',
|
||||
score: 100
|
||||
})
|
||||
|
||||
// 查询
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
const topUsers = await users.find(
|
||||
{ score: { $gte: 100 } },
|
||||
{ sort: { score: -1 }, limit: 10 }
|
||||
)
|
||||
|
||||
// 更新
|
||||
await users.updateOne(
|
||||
{ id: '1' },
|
||||
{ $inc: { score: 10 } }
|
||||
)
|
||||
|
||||
// 删除
|
||||
await users.deleteOne({ id: '1' })
|
||||
```
|
||||
|
||||
### 批量操作
|
||||
|
||||
```typescript
|
||||
// 批量插入
|
||||
await users.insertMany([
|
||||
{ id: '1', name: 'Alice', email: 'alice@example.com', score: 100 },
|
||||
{ id: '2', name: 'Bob', email: 'bob@example.com', score: 200 },
|
||||
{ id: '3', name: 'Carol', email: 'carol@example.com', score: 150 }
|
||||
])
|
||||
|
||||
// 批量更新
|
||||
await users.updateMany(
|
||||
{ score: { $lt: 100 } },
|
||||
{ $set: { status: 'inactive' } }
|
||||
)
|
||||
|
||||
// 批量删除
|
||||
await users.deleteMany({ status: 'inactive' })
|
||||
```
|
||||
|
||||
### 索引管理
|
||||
|
||||
```typescript
|
||||
// 创建索引
|
||||
await users.createIndex({ email: 1 }, { unique: true })
|
||||
await users.createIndex({ score: -1 })
|
||||
await users.createIndex({ name: 1, score: -1 })
|
||||
```
|
||||
|
||||
## 与其他模块集成
|
||||
|
||||
### 与 @esengine/database 集成
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { UserRepository, createRepository } from '@esengine/database'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// 使用 UserRepository
|
||||
const userRepo = new UserRepository(mongo)
|
||||
await userRepo.register({ username: 'john', password: '123456' })
|
||||
|
||||
// 使用通用仓库
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
```
|
||||
|
||||
### 与 @esengine/transaction 集成
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// 创建事务存储(共享连接)
|
||||
const storage = createMongoStorage(mongo)
|
||||
await storage.ensureIndexes()
|
||||
|
||||
const txManager = new TransactionManager({ storage })
|
||||
```
|
||||
228
docs/src/content/docs/modules/database-drivers/redis.md
Normal file
228
docs/src/content/docs/modules/database-drivers/redis.md
Normal file
@@ -0,0 +1,228 @@
|
||||
---
|
||||
title: "Redis 连接"
|
||||
description: "Redis 连接管理、自动重连、键前缀"
|
||||
---
|
||||
|
||||
## 配置选项
|
||||
|
||||
```typescript
|
||||
interface RedisConnectionConfig {
|
||||
/** Redis 主机 */
|
||||
host?: string
|
||||
|
||||
/** Redis 端口 */
|
||||
port?: number
|
||||
|
||||
/** 认证密码 */
|
||||
password?: string
|
||||
|
||||
/** 数据库编号 */
|
||||
db?: number
|
||||
|
||||
/** 键前缀 */
|
||||
keyPrefix?: string
|
||||
|
||||
/** 是否自动重连(默认 true) */
|
||||
autoReconnect?: boolean
|
||||
|
||||
/** 重连间隔(毫秒,默认 5000) */
|
||||
reconnectInterval?: number
|
||||
|
||||
/** 最大重连次数(默认 10) */
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection, RedisConnectionToken } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
password: 'your-password',
|
||||
db: 0,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10
|
||||
})
|
||||
|
||||
// 事件监听
|
||||
redis.on('connected', () => {
|
||||
console.log('Redis 已连接')
|
||||
})
|
||||
|
||||
redis.on('disconnected', () => {
|
||||
console.log('Redis 已断开')
|
||||
})
|
||||
|
||||
redis.on('error', (event) => {
|
||||
console.error('Redis 错误:', event.error)
|
||||
})
|
||||
|
||||
// 连接
|
||||
await redis.connect()
|
||||
|
||||
// 检查状态
|
||||
console.log('已连接:', redis.isConnected())
|
||||
console.log('Ping:', await redis.ping())
|
||||
```
|
||||
|
||||
## IRedisConnection 接口
|
||||
|
||||
```typescript
|
||||
interface IRedisConnection {
|
||||
/** 连接 ID */
|
||||
readonly id: string
|
||||
|
||||
/** 连接状态 */
|
||||
readonly state: ConnectionState
|
||||
|
||||
/** 建立连接 */
|
||||
connect(): Promise<void>
|
||||
|
||||
/** 断开连接 */
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/** 检查是否已连接 */
|
||||
isConnected(): boolean
|
||||
|
||||
/** 测试连接 */
|
||||
ping(): Promise<boolean>
|
||||
|
||||
/** 获取值 */
|
||||
get(key: string): Promise<string | null>
|
||||
|
||||
/** 设置值(可选 TTL,单位秒) */
|
||||
set(key: string, value: string, ttl?: number): Promise<void>
|
||||
|
||||
/** 删除键 */
|
||||
del(key: string): Promise<boolean>
|
||||
|
||||
/** 检查键是否存在 */
|
||||
exists(key: string): Promise<boolean>
|
||||
|
||||
/** 设置过期时间(秒) */
|
||||
expire(key: string, seconds: number): Promise<boolean>
|
||||
|
||||
/** 获取剩余过期时间(秒) */
|
||||
ttl(key: string): Promise<number>
|
||||
|
||||
/** 获取原生客户端(高级用法) */
|
||||
getNativeClient(): Redis
|
||||
}
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基本操作
|
||||
|
||||
```typescript
|
||||
// 设置值
|
||||
await redis.set('user:1:name', 'John')
|
||||
|
||||
// 设置带过期时间的值(1 小时)
|
||||
await redis.set('session:abc123', 'user-data', 3600)
|
||||
|
||||
// 获取值
|
||||
const name = await redis.get('user:1:name')
|
||||
|
||||
// 检查键是否存在
|
||||
const exists = await redis.exists('user:1:name')
|
||||
|
||||
// 删除键
|
||||
await redis.del('user:1:name')
|
||||
|
||||
// 获取剩余过期时间
|
||||
const ttl = await redis.ttl('session:abc123')
|
||||
```
|
||||
|
||||
### 键前缀
|
||||
|
||||
配置 `keyPrefix` 后,所有操作自动添加前缀:
|
||||
|
||||
```typescript
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
keyPrefix: 'game:'
|
||||
})
|
||||
|
||||
// 实际操作的键是 'game:user:1'
|
||||
await redis.set('user:1', 'data')
|
||||
|
||||
// 实际查询的键是 'game:user:1'
|
||||
const data = await redis.get('user:1')
|
||||
```
|
||||
|
||||
### 高级操作
|
||||
|
||||
使用原生客户端进行高级操作:
|
||||
|
||||
```typescript
|
||||
const client = redis.getNativeClient()
|
||||
|
||||
// 使用 Pipeline
|
||||
const pipeline = client.pipeline()
|
||||
pipeline.set('key1', 'value1')
|
||||
pipeline.set('key2', 'value2')
|
||||
pipeline.set('key3', 'value3')
|
||||
await pipeline.exec()
|
||||
|
||||
// 使用事务
|
||||
const multi = client.multi()
|
||||
multi.incr('counter')
|
||||
multi.get('counter')
|
||||
const results = await multi.exec()
|
||||
|
||||
// 使用 Lua 脚本
|
||||
const result = await client.eval(
|
||||
`return redis.call('get', KEYS[1])`,
|
||||
1,
|
||||
'mykey'
|
||||
)
|
||||
```
|
||||
|
||||
## 与事务系统集成
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
import { RedisStorage, TransactionManager } from '@esengine/transaction'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'tx:'
|
||||
})
|
||||
await redis.connect()
|
||||
|
||||
// 创建事务存储
|
||||
const storage = new RedisStorage({
|
||||
factory: () => redis.getNativeClient(),
|
||||
prefix: 'tx:'
|
||||
})
|
||||
|
||||
const txManager = new TransactionManager({ storage })
|
||||
```
|
||||
|
||||
## 连接状态
|
||||
|
||||
```typescript
|
||||
type ConnectionState =
|
||||
| 'disconnected' // 未连接
|
||||
| 'connecting' // 连接中
|
||||
| 'connected' // 已连接
|
||||
| 'disconnecting' // 断开中
|
||||
| 'error' // 错误状态
|
||||
```
|
||||
|
||||
## 事件
|
||||
|
||||
| 事件 | 描述 |
|
||||
|------|------|
|
||||
| `connected` | 连接成功 |
|
||||
| `disconnected` | 连接断开 |
|
||||
| `reconnecting` | 正在重连 |
|
||||
| `reconnected` | 重连成功 |
|
||||
| `error` | 发生错误 |
|
||||
140
docs/src/content/docs/modules/database/index.md
Normal file
140
docs/src/content/docs/modules/database/index.md
Normal file
@@ -0,0 +1,140 @@
|
||||
---
|
||||
title: "数据库仓库"
|
||||
description: "Repository 模式的数据库操作层,支持 CRUD、分页、软删除"
|
||||
---
|
||||
|
||||
`@esengine/database` 是 ESEngine 的数据库操作层,基于 Repository 模式提供类型安全的 CRUD 操作。
|
||||
|
||||
## 特性
|
||||
|
||||
- **Repository 模式** - 泛型 CRUD 操作,类型安全
|
||||
- **分页查询** - 内置分页支持
|
||||
- **软删除** - 可选的软删除与恢复
|
||||
- **用户管理** - 开箱即用的 UserRepository
|
||||
- **密码安全** - 使用 scrypt 的密码哈希工具
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm install @esengine/database @esengine/database-drivers
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 基本仓库
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { Repository, createRepository } from '@esengine/database'
|
||||
|
||||
// 定义实体
|
||||
interface Player {
|
||||
id: string
|
||||
name: string
|
||||
score: number
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
// 创建连接
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// 创建仓库
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// CRUD 操作
|
||||
const player = await playerRepo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
|
||||
const found = await playerRepo.findById(player.id)
|
||||
|
||||
await playerRepo.update(player.id, { score: 100 })
|
||||
|
||||
await playerRepo.delete(player.id)
|
||||
```
|
||||
|
||||
### 自定义仓库
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
import type { IMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
rank?: string
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players')
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
|
||||
async findByRank(rank: string): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
where: { rank }
|
||||
})
|
||||
}
|
||||
|
||||
async incrementScore(playerId: string, amount: number): Promise<Player | null> {
|
||||
const player = await this.findById(playerId)
|
||||
if (!player) return null
|
||||
return this.update(playerId, { score: player.score + amount })
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
const playerRepo = new PlayerRepository(mongo)
|
||||
const topPlayers = await playerRepo.findTopPlayers(5)
|
||||
```
|
||||
|
||||
### 用户仓库
|
||||
|
||||
```typescript
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
|
||||
// 注册新用户
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com'
|
||||
})
|
||||
|
||||
// 认证
|
||||
const authenticated = await userRepo.authenticate('john', 'securePassword123')
|
||||
if (authenticated) {
|
||||
console.log('登录成功:', authenticated.username)
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
await userRepo.changePassword(user.id, 'securePassword123', 'newPassword456')
|
||||
|
||||
// 角色管理
|
||||
await userRepo.addRole(user.id, 'admin')
|
||||
await userRepo.removeRole(user.id, 'admin')
|
||||
|
||||
// 查询用户
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
const john = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
## 文档
|
||||
|
||||
- [仓库 API](/modules/database/repository/) - Repository 详细 API
|
||||
- [用户管理](/modules/database/user/) - UserRepository 用法
|
||||
- [查询语法](/modules/database/query/) - 查询条件语法
|
||||
185
docs/src/content/docs/modules/database/query.md
Normal file
185
docs/src/content/docs/modules/database/query.md
Normal file
@@ -0,0 +1,185 @@
|
||||
---
|
||||
title: "查询语法"
|
||||
description: "查询条件操作符和语法"
|
||||
---
|
||||
|
||||
## 基本查询
|
||||
|
||||
### 精确匹配
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
name: 'John',
|
||||
status: 'active'
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 使用操作符
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 查询操作符
|
||||
|
||||
| 操作符 | 描述 | 示例 |
|
||||
|--------|------|------|
|
||||
| `$eq` | 等于 | `{ score: { $eq: 100 } }` |
|
||||
| `$ne` | 不等于 | `{ status: { $ne: 'banned' } }` |
|
||||
| `$gt` | 大于 | `{ score: { $gt: 50 } }` |
|
||||
| `$gte` | 大于等于 | `{ level: { $gte: 10 } }` |
|
||||
| `$lt` | 小于 | `{ age: { $lt: 18 } }` |
|
||||
| `$lte` | 小于等于 | `{ price: { $lte: 100 } }` |
|
||||
| `$in` | 在数组中 | `{ rank: { $in: ['gold', 'platinum'] } }` |
|
||||
| `$nin` | 不在数组中 | `{ status: { $nin: ['banned', 'suspended'] } }` |
|
||||
| `$like` | 模式匹配 | `{ name: { $like: '%john%' } }` |
|
||||
| `$regex` | 正则匹配 | `{ email: { $regex: '@gmail.com$' } }` |
|
||||
|
||||
## 逻辑操作符
|
||||
|
||||
### $or
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ score: { $gte: 1000 } },
|
||||
{ rank: 'legendary' }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### $and
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
$and: [
|
||||
{ score: { $gte: 100 } },
|
||||
{ score: { $lte: 500 } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 组合使用
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
$or: [
|
||||
{ rank: 'gold' },
|
||||
{ score: { $gte: 1000 } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 模式匹配
|
||||
|
||||
### $like 语法
|
||||
|
||||
- `%` - 匹配任意字符序列
|
||||
- `_` - 匹配单个字符
|
||||
|
||||
```typescript
|
||||
// 以 'John' 开头
|
||||
{ name: { $like: 'John%' } }
|
||||
|
||||
// 以 'son' 结尾
|
||||
{ name: { $like: '%son' } }
|
||||
|
||||
// 包含 'oh'
|
||||
{ name: { $like: '%oh%' } }
|
||||
|
||||
// 第二个字符是 'o'
|
||||
{ name: { $like: '_o%' } }
|
||||
```
|
||||
|
||||
### $regex 语法
|
||||
|
||||
使用标准正则表达式:
|
||||
|
||||
```typescript
|
||||
// 以 'John' 开头(大小写不敏感)
|
||||
{ name: { $regex: '^john' } }
|
||||
|
||||
// Gmail 邮箱
|
||||
{ email: { $regex: '@gmail\\.com$' } }
|
||||
|
||||
// 包含数字
|
||||
{ username: { $regex: '\\d+' } }
|
||||
```
|
||||
|
||||
## 排序
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
sort: {
|
||||
score: 'desc', // 降序
|
||||
name: 'asc' // 升序
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 分页
|
||||
|
||||
### 使用 limit/offset
|
||||
|
||||
```typescript
|
||||
// 第一页
|
||||
await repo.findMany({
|
||||
limit: 20,
|
||||
offset: 0
|
||||
})
|
||||
|
||||
// 第二页
|
||||
await repo.findMany({
|
||||
limit: 20,
|
||||
offset: 20
|
||||
})
|
||||
```
|
||||
|
||||
### 使用 findPaginated
|
||||
|
||||
```typescript
|
||||
const result = await repo.findPaginated(
|
||||
{ page: 2, pageSize: 20 },
|
||||
{ sort: { createdAt: 'desc' } }
|
||||
)
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
// 查找活跃的金牌玩家,分数在 100-1000 之间
|
||||
// 按分数降序排列,取前 10 个
|
||||
const players = await repo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
rank: 'gold',
|
||||
score: { $gte: 100, $lte: 1000 }
|
||||
},
|
||||
sort: { score: 'desc' },
|
||||
limit: 10
|
||||
})
|
||||
|
||||
// 搜索用户名包含 'john' 或邮箱是 gmail 的用户
|
||||
const users = await repo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ username: { $like: '%john%' } },
|
||||
{ email: { $regex: '@gmail\\.com$' } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
244
docs/src/content/docs/modules/database/repository.md
Normal file
244
docs/src/content/docs/modules/database/repository.md
Normal file
@@ -0,0 +1,244 @@
|
||||
---
|
||||
title: "Repository API"
|
||||
description: "泛型仓库接口,CRUD 操作、分页、软删除"
|
||||
---
|
||||
|
||||
## 创建仓库
|
||||
|
||||
### 使用工厂函数
|
||||
|
||||
```typescript
|
||||
import { createRepository } from '@esengine/database'
|
||||
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// 启用软删除
|
||||
const playerRepo = createRepository<Player>(mongo, 'players', true)
|
||||
```
|
||||
|
||||
### 继承 Repository
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players', false) // 第三个参数:启用软删除
|
||||
}
|
||||
|
||||
// 添加自定义方法
|
||||
async findTopPlayers(limit: number): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## BaseEntity 接口
|
||||
|
||||
所有实体必须继承 `BaseEntity`:
|
||||
|
||||
```typescript
|
||||
interface BaseEntity {
|
||||
id: string
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
deletedAt?: Date // 软删除时使用
|
||||
}
|
||||
```
|
||||
|
||||
## 查询方法
|
||||
|
||||
### findById
|
||||
|
||||
```typescript
|
||||
const player = await repo.findById('player-123')
|
||||
```
|
||||
|
||||
### findOne
|
||||
|
||||
```typescript
|
||||
const player = await repo.findOne({
|
||||
where: { name: 'John' }
|
||||
})
|
||||
|
||||
const topPlayer = await repo.findOne({
|
||||
sort: { score: 'desc' }
|
||||
})
|
||||
```
|
||||
|
||||
### findMany
|
||||
|
||||
```typescript
|
||||
// 简单查询
|
||||
const players = await repo.findMany({
|
||||
where: { rank: 'gold' }
|
||||
})
|
||||
|
||||
// 复杂查询
|
||||
const players = await repo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] }
|
||||
},
|
||||
sort: { score: 'desc', name: 'asc' },
|
||||
limit: 10,
|
||||
offset: 0
|
||||
})
|
||||
```
|
||||
|
||||
### findPaginated
|
||||
|
||||
```typescript
|
||||
const result = await repo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { rank: 'gold' },
|
||||
sort: { score: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
console.log(result.data) // Player[]
|
||||
console.log(result.total) // 总数量
|
||||
console.log(result.totalPages) // 总页数
|
||||
console.log(result.hasNext) // 是否有下一页
|
||||
console.log(result.hasPrev) // 是否有上一页
|
||||
```
|
||||
|
||||
### count
|
||||
|
||||
```typescript
|
||||
const count = await repo.count({
|
||||
where: { rank: 'gold' }
|
||||
})
|
||||
```
|
||||
|
||||
### exists
|
||||
|
||||
```typescript
|
||||
const exists = await repo.exists({
|
||||
where: { email: 'john@example.com' }
|
||||
})
|
||||
```
|
||||
|
||||
## 创建方法
|
||||
|
||||
### create
|
||||
|
||||
```typescript
|
||||
const player = await repo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
// 自动生成 id, createdAt, updatedAt
|
||||
```
|
||||
|
||||
### createMany
|
||||
|
||||
```typescript
|
||||
const players = await repo.createMany([
|
||||
{ name: 'Alice', score: 100 },
|
||||
{ name: 'Bob', score: 200 },
|
||||
{ name: 'Carol', score: 150 }
|
||||
])
|
||||
```
|
||||
|
||||
## 更新方法
|
||||
|
||||
### update
|
||||
|
||||
```typescript
|
||||
const updated = await repo.update('player-123', {
|
||||
score: 200,
|
||||
rank: 'gold'
|
||||
})
|
||||
// 自动更新 updatedAt
|
||||
```
|
||||
|
||||
## 删除方法
|
||||
|
||||
### delete
|
||||
|
||||
```typescript
|
||||
// 普通删除
|
||||
await repo.delete('player-123')
|
||||
|
||||
// 软删除(如果启用)
|
||||
// 实际是设置 deletedAt 字段
|
||||
```
|
||||
|
||||
### deleteMany
|
||||
|
||||
```typescript
|
||||
const count = await repo.deleteMany({
|
||||
where: { score: { $lt: 10 } }
|
||||
})
|
||||
```
|
||||
|
||||
## 软删除
|
||||
|
||||
### 启用软删除
|
||||
|
||||
```typescript
|
||||
const repo = createRepository<Player>(mongo, 'players', true)
|
||||
```
|
||||
|
||||
### 查询行为
|
||||
|
||||
```typescript
|
||||
// 默认排除软删除记录
|
||||
const players = await repo.findMany()
|
||||
|
||||
// 包含软删除记录
|
||||
const allPlayers = await repo.findMany({
|
||||
includeSoftDeleted: true
|
||||
})
|
||||
```
|
||||
|
||||
### 恢复记录
|
||||
|
||||
```typescript
|
||||
await repo.restore('player-123')
|
||||
```
|
||||
|
||||
## QueryOptions
|
||||
|
||||
```typescript
|
||||
interface QueryOptions<T> {
|
||||
/** 查询条件 */
|
||||
where?: WhereCondition<T>
|
||||
|
||||
/** 排序 */
|
||||
sort?: Partial<Record<keyof T, 'asc' | 'desc'>>
|
||||
|
||||
/** 限制数量 */
|
||||
limit?: number
|
||||
|
||||
/** 偏移量 */
|
||||
offset?: number
|
||||
|
||||
/** 包含软删除记录(仅在启用软删除时有效) */
|
||||
includeSoftDeleted?: boolean
|
||||
}
|
||||
```
|
||||
|
||||
## PaginatedResult
|
||||
|
||||
```typescript
|
||||
interface PaginatedResult<T> {
|
||||
data: T[]
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
totalPages: number
|
||||
hasNext: boolean
|
||||
hasPrev: boolean
|
||||
}
|
||||
```
|
||||
277
docs/src/content/docs/modules/database/user.md
Normal file
277
docs/src/content/docs/modules/database/user.md
Normal file
@@ -0,0 +1,277 @@
|
||||
---
|
||||
title: "用户管理"
|
||||
description: "UserRepository 用户注册、认证、角色管理"
|
||||
---
|
||||
|
||||
## 概述
|
||||
|
||||
`UserRepository` 提供开箱即用的用户管理功能:
|
||||
|
||||
- 用户注册与认证
|
||||
- 密码哈希(使用 scrypt)
|
||||
- 角色管理
|
||||
- 账户状态管理
|
||||
|
||||
## 快速开始
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
```
|
||||
|
||||
## 用户注册
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com', // 可选
|
||||
displayName: 'John Doe', // 可选
|
||||
roles: ['player'] // 可选,默认 []
|
||||
})
|
||||
|
||||
console.log(user)
|
||||
// {
|
||||
// id: 'uuid-...',
|
||||
// username: 'john',
|
||||
// email: 'john@example.com',
|
||||
// displayName: 'John Doe',
|
||||
// roles: ['player'],
|
||||
// status: 'active',
|
||||
// createdAt: Date,
|
||||
// updatedAt: Date
|
||||
// }
|
||||
```
|
||||
|
||||
**注意**:`register` 返回的 `SafeUser` 不包含密码哈希。
|
||||
|
||||
## 用户认证
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.authenticate('john', 'securePassword123')
|
||||
|
||||
if (user) {
|
||||
console.log('登录成功:', user.username)
|
||||
} else {
|
||||
console.log('用户名或密码错误')
|
||||
}
|
||||
```
|
||||
|
||||
## 密码管理
|
||||
|
||||
### 修改密码
|
||||
|
||||
```typescript
|
||||
const success = await userRepo.changePassword(
|
||||
userId,
|
||||
'oldPassword123',
|
||||
'newPassword456'
|
||||
)
|
||||
|
||||
if (success) {
|
||||
console.log('密码修改成功')
|
||||
} else {
|
||||
console.log('原密码错误')
|
||||
}
|
||||
```
|
||||
|
||||
### 重置密码
|
||||
|
||||
```typescript
|
||||
// 管理员直接重置密码
|
||||
const success = await userRepo.resetPassword(userId, 'newPassword123')
|
||||
```
|
||||
|
||||
## 角色管理
|
||||
|
||||
### 添加角色
|
||||
|
||||
```typescript
|
||||
await userRepo.addRole(userId, 'admin')
|
||||
await userRepo.addRole(userId, 'moderator')
|
||||
```
|
||||
|
||||
### 移除角色
|
||||
|
||||
```typescript
|
||||
await userRepo.removeRole(userId, 'moderator')
|
||||
```
|
||||
|
||||
### 查询角色
|
||||
|
||||
```typescript
|
||||
// 查找所有管理员
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
|
||||
// 检查用户是否有某角色
|
||||
const user = await userRepo.findById(userId)
|
||||
const isAdmin = user?.roles.includes('admin')
|
||||
```
|
||||
|
||||
## 查询用户
|
||||
|
||||
### 按用户名查找
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
### 按邮箱查找
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.findByEmail('john@example.com')
|
||||
```
|
||||
|
||||
### 按角色查找
|
||||
|
||||
```typescript
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
```
|
||||
|
||||
### 使用继承的方法
|
||||
|
||||
```typescript
|
||||
// 分页查询
|
||||
const result = await userRepo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { status: 'active' },
|
||||
sort: { createdAt: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
// 复杂查询
|
||||
const users = await userRepo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
roles: { $in: ['admin', 'moderator'] }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 账户状态
|
||||
|
||||
```typescript
|
||||
type UserStatus = 'active' | 'inactive' | 'banned' | 'suspended'
|
||||
```
|
||||
|
||||
### 更新状态
|
||||
|
||||
```typescript
|
||||
await userRepo.update(userId, { status: 'banned' })
|
||||
```
|
||||
|
||||
### 查询特定状态
|
||||
|
||||
```typescript
|
||||
const activeUsers = await userRepo.findMany({
|
||||
where: { status: 'active' }
|
||||
})
|
||||
|
||||
const bannedUsers = await userRepo.findMany({
|
||||
where: { status: 'banned' }
|
||||
})
|
||||
```
|
||||
|
||||
## 类型定义
|
||||
|
||||
### UserEntity
|
||||
|
||||
```typescript
|
||||
interface UserEntity extends BaseEntity {
|
||||
username: string
|
||||
passwordHash: string
|
||||
email?: string
|
||||
displayName?: string
|
||||
roles: string[]
|
||||
status: UserStatus
|
||||
lastLoginAt?: Date
|
||||
}
|
||||
```
|
||||
|
||||
### SafeUser
|
||||
|
||||
```typescript
|
||||
type SafeUser = Omit<UserEntity, 'passwordHash'>
|
||||
```
|
||||
|
||||
### CreateUserParams
|
||||
|
||||
```typescript
|
||||
interface CreateUserParams {
|
||||
username: string
|
||||
password: string
|
||||
email?: string
|
||||
displayName?: string
|
||||
roles?: string[]
|
||||
}
|
||||
```
|
||||
|
||||
## 密码工具
|
||||
|
||||
独立的密码工具函数:
|
||||
|
||||
```typescript
|
||||
import { hashPassword, verifyPassword } from '@esengine/database'
|
||||
|
||||
// 哈希密码
|
||||
const hash = await hashPassword('myPassword123')
|
||||
|
||||
// 验证密码
|
||||
const isValid = await verifyPassword('myPassword123', hash)
|
||||
```
|
||||
|
||||
### 安全说明
|
||||
|
||||
- 使用 Node.js 内置的 `scrypt` 算法
|
||||
- 自动生成随机盐值
|
||||
- 默认使用安全的迭代参数
|
||||
- 哈希格式:`salt:hash`(均为 hex 编码)
|
||||
|
||||
## 扩展 UserRepository
|
||||
|
||||
```typescript
|
||||
import { UserRepository, UserEntity } from '@esengine/database'
|
||||
|
||||
interface GameUser extends UserEntity {
|
||||
level: number
|
||||
experience: number
|
||||
coins: number
|
||||
}
|
||||
|
||||
class GameUserRepository extends UserRepository {
|
||||
// 重写集合名
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'game_users')
|
||||
}
|
||||
|
||||
// 添加游戏相关方法
|
||||
async addExperience(userId: string, amount: number): Promise<GameUser | null> {
|
||||
const user = await this.findById(userId) as GameUser | null
|
||||
if (!user) return null
|
||||
|
||||
const newExp = user.experience + amount
|
||||
const newLevel = Math.floor(newExp / 1000) + 1
|
||||
|
||||
return this.update(userId, {
|
||||
experience: newExp,
|
||||
level: newLevel
|
||||
}) as Promise<GameUser | null>
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<GameUser[]> {
|
||||
return this.findMany({
|
||||
sort: { level: 'desc', experience: 'desc' },
|
||||
limit
|
||||
}) as Promise<GameUser[]>
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -37,6 +37,13 @@ ESEngine 提供了丰富的功能模块,可以按需引入到你的项目中
|
||||
| [网络同步](/modules/network/) | `@esengine/network` | 多人游戏网络同步 |
|
||||
| [事务系统](/modules/transaction/) | `@esengine/transaction` | 游戏事务处理,支持分布式事务 |
|
||||
|
||||
### 数据库模块
|
||||
|
||||
| 模块 | 包名 | 描述 |
|
||||
|------|------|------|
|
||||
| [数据库驱动](/modules/database-drivers/) | `@esengine/database-drivers` | MongoDB、Redis 连接管理 |
|
||||
| [数据库仓库](/modules/database/) | `@esengine/database` | Repository 模式数据操作 |
|
||||
|
||||
## 安装
|
||||
|
||||
所有模块都可以独立安装:
|
||||
|
||||
@@ -92,6 +92,355 @@ const token = jwtProvider.sign({
|
||||
const payload = jwtProvider.decode(token)
|
||||
```
|
||||
|
||||
### 自定义提供者
|
||||
|
||||
你可以通过实现 `IAuthProvider` 接口来创建自定义认证提供者,以集成任何认证系统(如 OAuth、LDAP、自定义数据库认证等)。
|
||||
|
||||
#### IAuthProvider 接口
|
||||
|
||||
```typescript
|
||||
interface IAuthProvider<TUser = unknown, TCredentials = unknown> {
|
||||
/** 提供者名称 */
|
||||
readonly name: string;
|
||||
|
||||
/** 验证凭证 */
|
||||
verify(credentials: TCredentials): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** 刷新令牌(可选) */
|
||||
refresh?(token: string): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** 撤销令牌(可选) */
|
||||
revoke?(token: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
interface AuthResult<TUser> {
|
||||
success: boolean;
|
||||
user?: TUser;
|
||||
error?: string;
|
||||
errorCode?: AuthErrorCode;
|
||||
token?: string;
|
||||
expiresAt?: number;
|
||||
}
|
||||
|
||||
type AuthErrorCode =
|
||||
| 'INVALID_CREDENTIALS'
|
||||
| 'EXPIRED_TOKEN'
|
||||
| 'INVALID_TOKEN'
|
||||
| 'USER_NOT_FOUND'
|
||||
| 'ACCOUNT_DISABLED'
|
||||
| 'RATE_LIMITED'
|
||||
| 'INSUFFICIENT_PERMISSIONS';
|
||||
```
|
||||
|
||||
#### 自定义提供者示例
|
||||
|
||||
**示例 1:数据库密码认证**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
username: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface PasswordCredentials {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
class DatabaseAuthProvider implements IAuthProvider<User, PasswordCredentials> {
|
||||
readonly name = 'database'
|
||||
|
||||
async verify(credentials: PasswordCredentials): Promise<AuthResult<User>> {
|
||||
const { username, password } = credentials
|
||||
|
||||
// 从数据库查询用户
|
||||
const user = await db.users.findByUsername(username)
|
||||
if (!user) {
|
||||
return {
|
||||
success: false,
|
||||
error: '用户不存在',
|
||||
errorCode: 'USER_NOT_FOUND'
|
||||
}
|
||||
}
|
||||
|
||||
// 验证密码(使用 bcrypt 等库)
|
||||
const isValid = await bcrypt.compare(password, user.passwordHash)
|
||||
if (!isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: '密码错误',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// 检查账号状态
|
||||
if (user.disabled) {
|
||||
return {
|
||||
success: false,
|
||||
error: '账号已禁用',
|
||||
errorCode: 'ACCOUNT_DISABLED'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**示例 2:OAuth/第三方认证**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface OAuthUser {
|
||||
id: string
|
||||
email: string
|
||||
name: string
|
||||
provider: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface OAuthCredentials {
|
||||
provider: 'google' | 'github' | 'discord'
|
||||
accessToken: string
|
||||
}
|
||||
|
||||
class OAuthProvider implements IAuthProvider<OAuthUser, OAuthCredentials> {
|
||||
readonly name = 'oauth'
|
||||
|
||||
async verify(credentials: OAuthCredentials): Promise<AuthResult<OAuthUser>> {
|
||||
const { provider, accessToken } = credentials
|
||||
|
||||
try {
|
||||
// 根据提供商验证 token
|
||||
const profile = await this.fetchUserProfile(provider, accessToken)
|
||||
|
||||
// 查找或创建本地用户
|
||||
let user = await db.users.findByOAuth(provider, profile.id)
|
||||
if (!user) {
|
||||
user = await db.users.create({
|
||||
oauthProvider: provider,
|
||||
oauthId: profile.id,
|
||||
email: profile.email,
|
||||
name: profile.name,
|
||||
roles: ['player']
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
provider,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'OAuth 验证失败',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchUserProfile(provider: string, token: string) {
|
||||
switch (provider) {
|
||||
case 'google':
|
||||
return fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
case 'github':
|
||||
return fetch('https://api.github.com/user', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
// 其他提供商...
|
||||
default:
|
||||
throw new Error(`不支持的提供商: ${provider}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**示例 3:API Key 认证**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface ApiUser {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
rateLimit: number
|
||||
}
|
||||
|
||||
class ApiKeyAuthProvider implements IAuthProvider<ApiUser, string> {
|
||||
readonly name = 'api-key'
|
||||
|
||||
private revokedKeys = new Set<string>()
|
||||
|
||||
async verify(apiKey: string): Promise<AuthResult<ApiUser>> {
|
||||
if (!apiKey || !apiKey.startsWith('sk_')) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 格式无效',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
if (this.revokedKeys.has(apiKey)) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 已被撤销',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
// 从数据库查询 API Key
|
||||
const keyData = await db.apiKeys.findByKey(apiKey)
|
||||
if (!keyData) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 不存在',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// 检查过期
|
||||
if (keyData.expiresAt && keyData.expiresAt < Date.now()) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 已过期',
|
||||
errorCode: 'EXPIRED_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: keyData.userId,
|
||||
name: keyData.name,
|
||||
roles: keyData.roles,
|
||||
rateLimit: keyData.rateLimit
|
||||
},
|
||||
expiresAt: keyData.expiresAt
|
||||
}
|
||||
}
|
||||
|
||||
async revoke(apiKey: string): Promise<boolean> {
|
||||
this.revokedKeys.add(apiKey)
|
||||
await db.apiKeys.revoke(apiKey)
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 使用自定义提供者
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
import { withAuth } from '@esengine/server/auth'
|
||||
|
||||
// 创建自定义提供者
|
||||
const dbAuthProvider = new DatabaseAuthProvider()
|
||||
|
||||
// 或使用 OAuth 提供者
|
||||
const oauthProvider = new OAuthProvider()
|
||||
|
||||
// 使用自定义提供者
|
||||
const server = withAuth(await createServer({ port: 3000 }), {
|
||||
provider: dbAuthProvider, // 或 oauthProvider
|
||||
|
||||
// 从 WebSocket 连接请求中提取凭证
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
|
||||
// 对于数据库认证:从查询参数获取
|
||||
const username = url.searchParams.get('username')
|
||||
const password = url.searchParams.get('password')
|
||||
if (username && password) {
|
||||
return { username, password }
|
||||
}
|
||||
|
||||
// 对于 OAuth:从 token 参数获取
|
||||
const provider = url.searchParams.get('provider')
|
||||
const accessToken = url.searchParams.get('access_token')
|
||||
if (provider && accessToken) {
|
||||
return { provider, accessToken }
|
||||
}
|
||||
|
||||
// 对于 API Key:从请求头获取
|
||||
const apiKey = req.headers['x-api-key']
|
||||
if (apiKey) {
|
||||
return apiKey as string
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
onAuthFailure: (conn, error) => {
|
||||
console.log(`认证失败: ${error.errorCode} - ${error.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
await server.start()
|
||||
```
|
||||
|
||||
#### 组合多个提供者
|
||||
|
||||
你可以创建一个复合提供者来支持多种认证方式:
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface MultiAuthCredentials {
|
||||
type: 'jwt' | 'oauth' | 'apikey' | 'password'
|
||||
data: unknown
|
||||
}
|
||||
|
||||
class MultiAuthProvider implements IAuthProvider<User, MultiAuthCredentials> {
|
||||
readonly name = 'multi'
|
||||
|
||||
constructor(
|
||||
private jwtProvider: JwtAuthProvider<User>,
|
||||
private oauthProvider: OAuthProvider,
|
||||
private apiKeyProvider: ApiKeyAuthProvider,
|
||||
private dbProvider: DatabaseAuthProvider
|
||||
) {}
|
||||
|
||||
async verify(credentials: MultiAuthCredentials): Promise<AuthResult<User>> {
|
||||
switch (credentials.type) {
|
||||
case 'jwt':
|
||||
return this.jwtProvider.verify(credentials.data as string)
|
||||
case 'oauth':
|
||||
return this.oauthProvider.verify(credentials.data as OAuthCredentials)
|
||||
case 'apikey':
|
||||
return this.apiKeyProvider.verify(credentials.data as string)
|
||||
case 'password':
|
||||
return this.dbProvider.verify(credentials.data as PasswordCredentials)
|
||||
default:
|
||||
return {
|
||||
success: false,
|
||||
error: '不支持的认证类型',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Session 提供者
|
||||
|
||||
使用服务端会话实现有状态认证:
|
||||
|
||||
441
docs/src/content/docs/modules/network/distributed.md
Normal file
441
docs/src/content/docs/modules/network/distributed.md
Normal file
@@ -0,0 +1,441 @@
|
||||
---
|
||||
title: "分布式房间"
|
||||
description: "使用 DistributedRoomManager 实现多服务器房间管理"
|
||||
---
|
||||
|
||||
## 概述
|
||||
|
||||
分布式房间支持允许多个服务器实例共享房间注册表,实现跨服务器玩家路由和故障转移。
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ Server A Server B Server C │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
|
||||
│ │ Room 1 │ │ Room 3 │ │ Room 5 │ │
|
||||
│ │ Room 2 │ │ Room 4 │ │ Room 6 │ │
|
||||
│ └────┬────┘ └────┬────┘ └────┬────┘ │
|
||||
│ │ │ │ │
|
||||
│ └─────────────────┼─────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────▼──────────┐ │
|
||||
│ │ IDistributedAdapter │ │
|
||||
│ │ (Redis / Memory) │ │
|
||||
│ └─────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 单机模式(测试用)
|
||||
|
||||
```typescript
|
||||
import {
|
||||
DistributedRoomManager,
|
||||
MemoryAdapter,
|
||||
Room
|
||||
} from '@esengine/server';
|
||||
|
||||
// 定义房间类型
|
||||
class GameRoom extends Room {
|
||||
maxPlayers = 4;
|
||||
}
|
||||
|
||||
// 创建适配器和管理器
|
||||
const adapter = new MemoryAdapter();
|
||||
const manager = new DistributedRoomManager(adapter, {
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3000
|
||||
}, (conn, type, data) => conn.send(JSON.stringify({ type, data })));
|
||||
|
||||
// 注册房间类型
|
||||
manager.define('game', GameRoom);
|
||||
|
||||
// 启动管理器
|
||||
await manager.start();
|
||||
|
||||
// 分布式加入/创建房间
|
||||
const result = await manager.joinOrCreateDistributed('game', 'player-1', conn);
|
||||
if ('redirect' in result) {
|
||||
// 玩家应连接到其他服务器
|
||||
console.log(`重定向到: ${result.redirect}`);
|
||||
} else {
|
||||
// 玩家加入本地房间
|
||||
const { room, player } = result;
|
||||
}
|
||||
|
||||
// 优雅关闭
|
||||
await manager.stop(true);
|
||||
```
|
||||
|
||||
### 多服务器模式(生产用)
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { DistributedRoomManager, RedisAdapter } from '@esengine/server';
|
||||
|
||||
const adapter = new RedisAdapter({
|
||||
factory: () => new Redis({
|
||||
host: 'redis.example.com',
|
||||
port: 6379
|
||||
}),
|
||||
prefix: 'game:',
|
||||
serverTtl: 30,
|
||||
snapshotTtl: 86400
|
||||
});
|
||||
|
||||
const manager = new DistributedRoomManager(adapter, {
|
||||
serverId: process.env.SERVER_ID,
|
||||
serverAddress: process.env.PUBLIC_IP,
|
||||
serverPort: 3000,
|
||||
heartbeatInterval: 5000,
|
||||
snapshotInterval: 30000,
|
||||
enableFailover: true,
|
||||
capacity: 100
|
||||
}, sendFn);
|
||||
```
|
||||
|
||||
## DistributedRoomManager
|
||||
|
||||
### 配置选项
|
||||
|
||||
| 属性 | 类型 | 默认值 | 描述 |
|
||||
|------|------|--------|------|
|
||||
| `serverId` | `string` | 必填 | 服务器唯一标识 |
|
||||
| `serverAddress` | `string` | 必填 | 客户端连接的公开地址 |
|
||||
| `serverPort` | `number` | 必填 | 服务器端口 |
|
||||
| `heartbeatInterval` | `number` | `5000` | 心跳间隔(毫秒) |
|
||||
| `snapshotInterval` | `number` | `30000` | 状态快照间隔,0 禁用 |
|
||||
| `migrationTimeout` | `number` | `10000` | 房间迁移超时 |
|
||||
| `enableFailover` | `boolean` | `true` | 启用自动故障转移 |
|
||||
| `capacity` | `number` | `100` | 本服务器最大房间数 |
|
||||
|
||||
### 生命周期方法
|
||||
|
||||
#### start()
|
||||
|
||||
启动分布式房间管理器。连接适配器、注册服务器、启动心跳。
|
||||
|
||||
```typescript
|
||||
await manager.start();
|
||||
```
|
||||
|
||||
#### stop(graceful?)
|
||||
|
||||
停止管理器。如果 `graceful=true`,将服务器标记为 draining 并保存所有房间快照。
|
||||
|
||||
```typescript
|
||||
await manager.stop(true);
|
||||
```
|
||||
|
||||
### 路由方法
|
||||
|
||||
#### joinOrCreateDistributed()
|
||||
|
||||
分布式感知的加入或创建房间。返回本地房间的 `{ room, player }` 或远程房间的 `{ redirect: string }`。
|
||||
|
||||
```typescript
|
||||
const result = await manager.joinOrCreateDistributed('game', 'player-1', conn);
|
||||
|
||||
if ('redirect' in result) {
|
||||
// 客户端应重定向到其他服务器
|
||||
res.json({ redirect: result.redirect });
|
||||
} else {
|
||||
// 玩家加入了本地房间
|
||||
const { room, player } = result;
|
||||
}
|
||||
```
|
||||
|
||||
#### route()
|
||||
|
||||
将玩家路由到合适的房间/服务器。
|
||||
|
||||
```typescript
|
||||
const result = await manager.route({
|
||||
roomType: 'game',
|
||||
playerId: 'p1'
|
||||
});
|
||||
|
||||
switch (result.type) {
|
||||
case 'local': // 房间在本服务器
|
||||
break;
|
||||
case 'redirect': // 房间在其他服务器
|
||||
// result.serverAddress 包含目标服务器地址
|
||||
break;
|
||||
case 'create': // 没有可用房间,需要创建
|
||||
break;
|
||||
case 'unavailable': // 无法找到或创建房间
|
||||
// result.reason 包含错误信息
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
### 状态管理
|
||||
|
||||
#### saveSnapshot()
|
||||
|
||||
手动保存房间状态快照。
|
||||
|
||||
```typescript
|
||||
await manager.saveSnapshot(roomId);
|
||||
```
|
||||
|
||||
#### restoreFromSnapshot()
|
||||
|
||||
从保存的快照恢复房间。
|
||||
|
||||
```typescript
|
||||
const success = await manager.restoreFromSnapshot(roomId);
|
||||
```
|
||||
|
||||
### 查询方法
|
||||
|
||||
#### getServers()
|
||||
|
||||
获取所有在线服务器。
|
||||
|
||||
```typescript
|
||||
const servers = await manager.getServers();
|
||||
```
|
||||
|
||||
#### queryDistributedRooms()
|
||||
|
||||
查询所有服务器上的房间。
|
||||
|
||||
```typescript
|
||||
const rooms = await manager.queryDistributedRooms({
|
||||
roomType: 'game',
|
||||
hasSpace: true,
|
||||
notLocked: true
|
||||
});
|
||||
```
|
||||
|
||||
## IDistributedAdapter
|
||||
|
||||
分布式后端的接口。实现此接口以支持 Redis、消息队列等。
|
||||
|
||||
### 内置适配器
|
||||
|
||||
#### MemoryAdapter
|
||||
|
||||
用于测试和单机模式的内存实现。
|
||||
|
||||
```typescript
|
||||
const adapter = new MemoryAdapter({
|
||||
serverTtl: 15000, // 无心跳后服务器离线时间(毫秒)
|
||||
enableTtlCheck: true, // 启用自动 TTL 检查
|
||||
ttlCheckInterval: 5000 // TTL 检查间隔(毫秒)
|
||||
});
|
||||
```
|
||||
|
||||
#### RedisAdapter
|
||||
|
||||
用于生产环境多服务器部署的 Redis 实现。
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { RedisAdapter } from '@esengine/server';
|
||||
|
||||
const adapter = new RedisAdapter({
|
||||
factory: () => new Redis('redis://localhost:6379'),
|
||||
prefix: 'game:', // 键前缀(默认: 'dist:')
|
||||
serverTtl: 30, // 服务器 TTL(秒,默认: 30)
|
||||
roomTtl: 0, // 房间 TTL,0 = 永不过期(默认: 0)
|
||||
snapshotTtl: 86400, // 快照 TTL(秒,默认: 24 小时)
|
||||
channel: 'game:events' // Pub/Sub 频道(默认: 'distributed:events')
|
||||
});
|
||||
```
|
||||
|
||||
**RedisAdapter 配置:**
|
||||
|
||||
| 属性 | 类型 | 默认值 | 描述 |
|
||||
|------|------|--------|------|
|
||||
| `factory` | `() => RedisClient` | 必填 | Redis 客户端工厂(惰性连接) |
|
||||
| `prefix` | `string` | `'dist:'` | 所有 Redis 键的前缀 |
|
||||
| `serverTtl` | `number` | `30` | 服务器 TTL(秒) |
|
||||
| `roomTtl` | `number` | `0` | 房间 TTL(秒),0 = 不过期 |
|
||||
| `snapshotTtl` | `number` | `86400` | 快照 TTL(秒) |
|
||||
| `channel` | `string` | `'distributed:events'` | Pub/Sub 频道名 |
|
||||
|
||||
**功能特性:**
|
||||
- 带自动心跳 TTL 的服务器注册
|
||||
- 跨服务器查找的房间注册
|
||||
- 可配置 TTL 的状态快照
|
||||
- 跨服务器事件的 Pub/Sub
|
||||
- 使用 Redis SET NX 的分布式锁
|
||||
|
||||
### 自定义适配器
|
||||
|
||||
```typescript
|
||||
import type { IDistributedAdapter } from '@esengine/server';
|
||||
|
||||
class MyAdapter implements IDistributedAdapter {
|
||||
// 生命周期
|
||||
async connect(): Promise<void> { }
|
||||
async disconnect(): Promise<void> { }
|
||||
isConnected(): boolean { return true; }
|
||||
|
||||
// 服务器注册
|
||||
async registerServer(server: ServerRegistration): Promise<void> { }
|
||||
async unregisterServer(serverId: string): Promise<void> { }
|
||||
async heartbeat(serverId: string): Promise<void> { }
|
||||
async getServers(): Promise<ServerRegistration[]> { return []; }
|
||||
|
||||
// 房间注册
|
||||
async registerRoom(room: RoomRegistration): Promise<void> { }
|
||||
async unregisterRoom(roomId: string): Promise<void> { }
|
||||
async queryRooms(query: RoomQuery): Promise<RoomRegistration[]> { return []; }
|
||||
async findAvailableRoom(roomType: string): Promise<RoomRegistration | null> { return null; }
|
||||
|
||||
// 状态快照
|
||||
async saveSnapshot(snapshot: RoomSnapshot): Promise<void> { }
|
||||
async loadSnapshot(roomId: string): Promise<RoomSnapshot | null> { return null; }
|
||||
|
||||
// 发布/订阅
|
||||
async publish(event: DistributedEvent): Promise<void> { }
|
||||
async subscribe(pattern: string, handler: Function): Promise<() => void> { return () => {}; }
|
||||
|
||||
// 分布式锁
|
||||
async acquireLock(key: string, ttlMs: number): Promise<boolean> { return true; }
|
||||
async releaseLock(key: string): Promise<void> { }
|
||||
}
|
||||
```
|
||||
|
||||
## 玩家路由流程
|
||||
|
||||
```
|
||||
客户端 服务器 A 服务器 B
|
||||
│ │ │
|
||||
│─── joinOrCreate ────────►│ │
|
||||
│ │ │
|
||||
│ │── findAvailableRoom() ───►│
|
||||
│ │◄──── 服务器 B 上有房间 ────│
|
||||
│ │ │
|
||||
│◄─── redirect: B:3001 ────│ │
|
||||
│ │ │
|
||||
│───────────────── 连接到服务器 B ────────────────────►│
|
||||
│ │ │
|
||||
│◄─────────────────────────────── 已加入 ─────────────│
|
||||
```
|
||||
|
||||
## 事件类型
|
||||
|
||||
分布式系统发布以下事件:
|
||||
|
||||
| 事件 | 描述 |
|
||||
|------|------|
|
||||
| `server:online` | 服务器上线 |
|
||||
| `server:offline` | 服务器离线 |
|
||||
| `server:draining` | 服务器正在排空 |
|
||||
| `room:created` | 房间已创建 |
|
||||
| `room:disposed` | 房间已销毁 |
|
||||
| `room:updated` | 房间信息已更新 |
|
||||
| `room:message` | 跨服务器房间消息 |
|
||||
| `room:migrated` | 房间已迁移到其他服务器 |
|
||||
| `player:joined` | 玩家加入房间 |
|
||||
| `player:left` | 玩家离开房间 |
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **使用唯一服务器 ID** - 使用主机名、容器 ID 或 UUID
|
||||
|
||||
2. **配置合适的心跳** - 在新鲜度和网络开销之间平衡
|
||||
|
||||
3. **为有状态房间启用快照** - 确保房间状态在服务器重启后存活
|
||||
|
||||
4. **优雅处理重定向** - 客户端应重新连接到目标服务器
|
||||
```typescript
|
||||
// 客户端处理重定向
|
||||
if (response.redirect) {
|
||||
await client.disconnect();
|
||||
await client.connect(response.redirect);
|
||||
await client.joinRoom(roomId);
|
||||
}
|
||||
```
|
||||
|
||||
5. **使用分布式锁** - 防止 joinOrCreate 中的竞态条件
|
||||
|
||||
## 使用 createServer 集成
|
||||
|
||||
最简单的使用方式是通过 `createServer` 的 `distributed` 配置:
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server';
|
||||
import { RedisAdapter, Room } from '@esengine/server';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
class GameRoom extends Room {
|
||||
maxPlayers = 4;
|
||||
}
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
distributed: {
|
||||
enabled: true,
|
||||
adapter: new RedisAdapter({ factory: () => new Redis() }),
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'ws://192.168.1.100',
|
||||
serverPort: 3000,
|
||||
enableFailover: true,
|
||||
capacity: 100
|
||||
}
|
||||
});
|
||||
|
||||
server.define('game', GameRoom);
|
||||
await server.start();
|
||||
```
|
||||
|
||||
当客户端调用 `JoinRoom` API 时,服务器会自动:
|
||||
1. 查找可用房间(本地或远程)
|
||||
2. 如果房间在其他服务器,发送 `$redirect` 消息给客户端
|
||||
3. 客户端收到重定向消息后连接到目标服务器
|
||||
|
||||
## 负载均衡
|
||||
|
||||
使用 `LoadBalancedRouter` 进行服务器选择:
|
||||
|
||||
```typescript
|
||||
import { LoadBalancedRouter, createLoadBalancedRouter } from '@esengine/server';
|
||||
|
||||
// 使用工厂函数
|
||||
const router = createLoadBalancedRouter('least-players');
|
||||
|
||||
// 或直接创建
|
||||
const router = new LoadBalancedRouter({
|
||||
strategy: 'least-rooms', // 选择房间数最少的服务器
|
||||
preferLocal: true // 优先选择本地服务器
|
||||
});
|
||||
|
||||
// 可用策略
|
||||
// - 'round-robin': 轮询
|
||||
// - 'least-rooms': 最少房间数
|
||||
// - 'least-players': 最少玩家数
|
||||
// - 'random': 随机选择
|
||||
// - 'weighted': 权重(基于容量使用率)
|
||||
```
|
||||
|
||||
## 故障转移
|
||||
|
||||
当服务器离线时,启用 `enableFailover` 后系统会自动:
|
||||
|
||||
1. 检测到服务器离线(通过心跳超时)
|
||||
2. 查询该服务器上的所有房间
|
||||
3. 使用分布式锁防止多服务器同时恢复
|
||||
4. 从快照恢复房间状态
|
||||
5. 发布 `room:migrated` 事件通知其他服务器
|
||||
|
||||
```typescript
|
||||
// 确保定期保存快照
|
||||
const manager = new DistributedRoomManager(adapter, {
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3000,
|
||||
snapshotInterval: 30000, // 每 30 秒保存快照
|
||||
enableFailover: true // 启用故障转移
|
||||
}, sendFn);
|
||||
```
|
||||
|
||||
## 后续版本
|
||||
|
||||
- Redis Cluster 支持
|
||||
- 更多负载均衡策略(地理位置、延迟感知)
|
||||
679
docs/src/content/docs/modules/network/http.md
Normal file
679
docs/src/content/docs/modules/network/http.md
Normal file
@@ -0,0 +1,679 @@
|
||||
---
|
||||
title: "HTTP 路由"
|
||||
description: "HTTP REST API 路由功能,支持与 WebSocket 共用端口"
|
||||
---
|
||||
|
||||
`@esengine/server` 内置了轻量级的 HTTP 路由功能,可以与 WebSocket 服务共用同一端口,方便实现 REST API。
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 内联路由定义
|
||||
|
||||
最简单的方式是在创建服务器时直接定义 HTTP 路由:
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
http: {
|
||||
'/api/health': (req, res) => {
|
||||
res.json({ status: 'ok', time: Date.now() })
|
||||
},
|
||||
'/api/users': {
|
||||
GET: (req, res) => {
|
||||
res.json({ users: [] })
|
||||
},
|
||||
POST: async (req, res) => {
|
||||
const body = req.body as { name: string }
|
||||
res.status(201).json({ id: '1', name: body.name })
|
||||
}
|
||||
}
|
||||
},
|
||||
cors: true // 启用 CORS
|
||||
})
|
||||
|
||||
await server.start()
|
||||
```
|
||||
|
||||
### 文件路由
|
||||
|
||||
对于较大的项目,推荐使用文件路由。创建 `src/http` 目录,每个文件对应一个路由:
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
interface LoginBody {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export default defineHttp<LoginBody>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body as LoginBody
|
||||
|
||||
// 验证用户...
|
||||
if (username === 'admin' && password === '123456') {
|
||||
res.json({ token: 'jwt-token-here', userId: 'user-1' })
|
||||
} else {
|
||||
res.error(401, '用户名或密码错误')
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
// server.ts
|
||||
import { createServer } from '@esengine/server'
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
httpDir: './src/http', // HTTP 路由目录
|
||||
httpPrefix: '/api', // 路由前缀
|
||||
cors: true
|
||||
})
|
||||
|
||||
await server.start()
|
||||
// 路由: POST /api/login
|
||||
```
|
||||
|
||||
## defineHttp 定义
|
||||
|
||||
`defineHttp` 用于定义类型安全的 HTTP 处理器:
|
||||
|
||||
```typescript
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
interface CreateUserBody {
|
||||
username: string
|
||||
email: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export default defineHttp<CreateUserBody>({
|
||||
// HTTP 方法(默认 POST)
|
||||
method: 'POST',
|
||||
|
||||
// 处理函数
|
||||
handler(req, res) {
|
||||
const body = req.body as CreateUserBody
|
||||
// 处理请求...
|
||||
res.status(201).json({ id: 'new-user-id' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 支持的 HTTP 方法
|
||||
|
||||
```typescript
|
||||
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
|
||||
```
|
||||
|
||||
## HttpRequest 对象
|
||||
|
||||
HTTP 请求对象包含以下属性:
|
||||
|
||||
```typescript
|
||||
interface HttpRequest {
|
||||
/** 原始 Node.js IncomingMessage */
|
||||
raw: IncomingMessage
|
||||
|
||||
/** HTTP 方法 */
|
||||
method: string
|
||||
|
||||
/** 请求路径 */
|
||||
path: string
|
||||
|
||||
/** 路由参数(从 URL 路径提取,如 /users/:id) */
|
||||
params: Record<string, string>
|
||||
|
||||
/** 查询参数 */
|
||||
query: Record<string, string>
|
||||
|
||||
/** 请求头 */
|
||||
headers: Record<string, string | string[] | undefined>
|
||||
|
||||
/** 解析后的请求体 */
|
||||
body: unknown
|
||||
|
||||
/** 客户端 IP */
|
||||
ip: string
|
||||
}
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// 获取查询参数
|
||||
const page = parseInt(req.query.page ?? '1')
|
||||
const limit = parseInt(req.query.limit ?? '10')
|
||||
|
||||
// 获取请求头
|
||||
const authHeader = req.headers.authorization
|
||||
|
||||
// 获取客户端 IP
|
||||
console.log('Request from:', req.ip)
|
||||
|
||||
res.json({ page, limit })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 请求体解析
|
||||
|
||||
请求体会根据 `Content-Type` 自动解析:
|
||||
|
||||
- `application/json` - 解析为 JSON 对象
|
||||
- `application/x-www-form-urlencoded` - 解析为键值对对象
|
||||
- 其他 - 保持原始字符串
|
||||
|
||||
```typescript
|
||||
export default defineHttp<{ name: string; age: number }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
// body 已自动解析
|
||||
const { name, age } = req.body as { name: string; age: number }
|
||||
res.json({ received: { name, age } })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## HttpResponse 对象
|
||||
|
||||
HTTP 响应对象提供链式 API:
|
||||
|
||||
```typescript
|
||||
interface HttpResponse {
|
||||
/** 原始 Node.js ServerResponse */
|
||||
raw: ServerResponse
|
||||
|
||||
/** 设置状态码 */
|
||||
status(code: number): HttpResponse
|
||||
|
||||
/** 设置响应头 */
|
||||
header(name: string, value: string): HttpResponse
|
||||
|
||||
/** 发送 JSON 响应 */
|
||||
json(data: unknown): void
|
||||
|
||||
/** 发送文本响应 */
|
||||
text(data: string): void
|
||||
|
||||
/** 发送错误响应 */
|
||||
error(code: number, message: string): void
|
||||
}
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
// 设置状态码和自定义头
|
||||
res
|
||||
.status(201)
|
||||
.header('X-Custom-Header', 'value')
|
||||
.json({ created: true })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// 发送错误响应
|
||||
res.error(404, '资源不存在')
|
||||
// 等价于: res.status(404).json({ error: '资源不存在' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// 发送纯文本
|
||||
res.text('Hello, World!')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 文件路由规范
|
||||
|
||||
### 命名转换
|
||||
|
||||
文件名会自动转换为路由路径:
|
||||
|
||||
| 文件路径 | 路由路径(prefix=/api) |
|
||||
|---------|----------------------|
|
||||
| `login.ts` | `/api/login` |
|
||||
| `users/profile.ts` | `/api/users/profile` |
|
||||
| `users/[id].ts` | `/api/users/:id` |
|
||||
| `game/room/[roomId].ts` | `/api/game/room/:roomId` |
|
||||
|
||||
### 动态路由参数
|
||||
|
||||
使用 `[param]` 语法定义动态参数:
|
||||
|
||||
```typescript
|
||||
// src/http/users/[id].ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
// 直接从 params 获取路由参数
|
||||
const { id } = req.params
|
||||
res.json({ userId: id })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
多个参数的情况:
|
||||
|
||||
```typescript
|
||||
// src/http/users/[userId]/posts/[postId].ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
handler(req, res) {
|
||||
const { userId, postId } = req.params
|
||||
res.json({ userId, postId })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 跳过规则
|
||||
|
||||
以下文件会被自动跳过:
|
||||
|
||||
- 以 `_` 开头的文件(如 `_helper.ts`)
|
||||
- `index.ts` / `index.js` 文件
|
||||
- 非 `.ts` / `.js` / `.mts` / `.mjs` 文件
|
||||
|
||||
### 目录结构示例
|
||||
|
||||
```
|
||||
src/
|
||||
└── http/
|
||||
├── _utils.ts # 跳过(下划线开头)
|
||||
├── index.ts # 跳过(index 文件)
|
||||
├── health.ts # GET /api/health
|
||||
├── login.ts # POST /api/login
|
||||
├── register.ts # POST /api/register
|
||||
└── users/
|
||||
├── index.ts # 跳过
|
||||
├── list.ts # GET /api/users/list
|
||||
└── [id].ts # GET /api/users/:id
|
||||
```
|
||||
|
||||
## CORS 配置
|
||||
|
||||
### 快速启用
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
cors: true // 使用默认配置
|
||||
})
|
||||
```
|
||||
|
||||
### 自定义配置
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
cors: {
|
||||
// 允许的来源
|
||||
origin: ['http://localhost:5173', 'https://myapp.com'],
|
||||
// 或使用通配符
|
||||
// origin: '*',
|
||||
// origin: true, // 反射请求来源
|
||||
|
||||
// 允许的 HTTP 方法
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
||||
|
||||
// 允许的请求头
|
||||
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
|
||||
|
||||
// 是否允许携带凭证(cookies)
|
||||
credentials: true,
|
||||
|
||||
// 预检请求缓存时间(秒)
|
||||
maxAge: 86400
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### CorsOptions 类型
|
||||
|
||||
```typescript
|
||||
interface CorsOptions {
|
||||
/** 允许的来源:字符串、字符串数组、true(反射)或 '*' */
|
||||
origin?: string | string[] | boolean
|
||||
|
||||
/** 允许的 HTTP 方法 */
|
||||
methods?: string[]
|
||||
|
||||
/** 允许的请求头 */
|
||||
allowedHeaders?: string[]
|
||||
|
||||
/** 是否允许携带凭证 */
|
||||
credentials?: boolean
|
||||
|
||||
/** 预检请求缓存时间(秒) */
|
||||
maxAge?: number
|
||||
}
|
||||
```
|
||||
|
||||
## 路由合并
|
||||
|
||||
文件路由和内联路由可以同时使用,内联路由优先级更高:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
httpDir: './src/http',
|
||||
httpPrefix: '/api',
|
||||
|
||||
// 内联路由会与文件路由合并
|
||||
http: {
|
||||
'/health': (req, res) => res.json({ status: 'ok' }),
|
||||
'/api/special': (req, res) => res.json({ special: true })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 与 WebSocket 共用端口
|
||||
|
||||
HTTP 路由与 WebSocket 服务自动共用同一端口:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
// WebSocket 相关配置
|
||||
apiDir: './src/api',
|
||||
msgDir: './src/msg',
|
||||
|
||||
// HTTP 相关配置
|
||||
httpDir: './src/http',
|
||||
httpPrefix: '/api',
|
||||
cors: true
|
||||
})
|
||||
|
||||
await server.start()
|
||||
|
||||
// 同一端口 3000:
|
||||
// - WebSocket: ws://localhost:3000
|
||||
// - HTTP API: http://localhost:3000/api/*
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
### 游戏服务器登录 API
|
||||
|
||||
```typescript
|
||||
// src/http/auth/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
import { createJwtAuthProvider } from '@esengine/server/auth'
|
||||
|
||||
interface LoginRequest {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
interface LoginResponse {
|
||||
token: string
|
||||
userId: string
|
||||
expiresAt: number
|
||||
}
|
||||
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600
|
||||
})
|
||||
|
||||
export default defineHttp<LoginRequest>({
|
||||
method: 'POST',
|
||||
async handler(req, res) {
|
||||
const { username, password } = req.body as LoginRequest
|
||||
|
||||
// 验证用户
|
||||
const user = await db.users.findByUsername(username)
|
||||
if (!user || !await verifyPassword(password, user.passwordHash)) {
|
||||
res.error(401, '用户名或密码错误')
|
||||
return
|
||||
}
|
||||
|
||||
// 生成 JWT
|
||||
const token = jwtProvider.sign({
|
||||
sub: user.id,
|
||||
name: user.username,
|
||||
roles: user.roles
|
||||
})
|
||||
|
||||
const response: LoginResponse = {
|
||||
token,
|
||||
userId: user.id,
|
||||
expiresAt: Date.now() + 3600 * 1000
|
||||
}
|
||||
|
||||
res.json(response)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 游戏数据查询 API
|
||||
|
||||
```typescript
|
||||
// src/http/game/leaderboard.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp({
|
||||
method: 'GET',
|
||||
async handler(req, res) {
|
||||
const limit = parseInt(req.query.limit ?? '10')
|
||||
const offset = parseInt(req.query.offset ?? '0')
|
||||
|
||||
const players = await db.players.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit,
|
||||
offset
|
||||
})
|
||||
|
||||
res.json({
|
||||
data: players,
|
||||
pagination: { limit, offset }
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 中间件
|
||||
|
||||
### 中间件类型
|
||||
|
||||
中间件是在路由处理前后执行的函数:
|
||||
|
||||
```typescript
|
||||
type HttpMiddleware = (
|
||||
req: HttpRequest,
|
||||
res: HttpResponse,
|
||||
next: () => Promise<void>
|
||||
) => void | Promise<void>
|
||||
```
|
||||
|
||||
### 内置中间件
|
||||
|
||||
```typescript
|
||||
import {
|
||||
requestLogger,
|
||||
bodyLimit,
|
||||
responseTime,
|
||||
requestId,
|
||||
securityHeaders
|
||||
} from '@esengine/server'
|
||||
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
http: { /* ... */ },
|
||||
// 全局中间件通过 createHttpRouter 配置
|
||||
})
|
||||
```
|
||||
|
||||
#### requestLogger - 请求日志
|
||||
|
||||
```typescript
|
||||
import { requestLogger } from '@esengine/server'
|
||||
|
||||
// 记录请求和响应时间
|
||||
requestLogger()
|
||||
|
||||
// 同时记录请求体
|
||||
requestLogger({ logBody: true })
|
||||
```
|
||||
|
||||
#### bodyLimit - 请求体大小限制
|
||||
|
||||
```typescript
|
||||
import { bodyLimit } from '@esengine/server'
|
||||
|
||||
// 限制请求体为 1MB
|
||||
bodyLimit(1024 * 1024)
|
||||
```
|
||||
|
||||
#### responseTime - 响应时间头
|
||||
|
||||
```typescript
|
||||
import { responseTime } from '@esengine/server'
|
||||
|
||||
// 自动添加 X-Response-Time 响应头
|
||||
responseTime()
|
||||
```
|
||||
|
||||
#### requestId - 请求 ID
|
||||
|
||||
```typescript
|
||||
import { requestId } from '@esengine/server'
|
||||
|
||||
// 自动生成并添加 X-Request-ID 响应头
|
||||
requestId()
|
||||
|
||||
// 自定义头名称
|
||||
requestId('X-Trace-ID')
|
||||
```
|
||||
|
||||
#### securityHeaders - 安全头
|
||||
|
||||
```typescript
|
||||
import { securityHeaders } from '@esengine/server'
|
||||
|
||||
// 添加常用安全响应头
|
||||
securityHeaders()
|
||||
|
||||
// 自定义配置
|
||||
securityHeaders({
|
||||
hidePoweredBy: true,
|
||||
frameOptions: 'DENY',
|
||||
noSniff: true
|
||||
})
|
||||
```
|
||||
|
||||
### 自定义中间件
|
||||
|
||||
```typescript
|
||||
import type { HttpMiddleware } from '@esengine/server'
|
||||
|
||||
// 认证中间件
|
||||
const authMiddleware: HttpMiddleware = async (req, res, next) => {
|
||||
const token = req.headers.authorization?.replace('Bearer ', '')
|
||||
|
||||
if (!token) {
|
||||
res.error(401, 'Unauthorized')
|
||||
return // 不调用 next(),终止请求
|
||||
}
|
||||
|
||||
// 验证 token...
|
||||
(req as any).userId = 'decoded-user-id'
|
||||
|
||||
await next() // 继续执行后续中间件和处理器
|
||||
}
|
||||
```
|
||||
|
||||
### 使用中间件
|
||||
|
||||
#### 使用 createHttpRouter
|
||||
|
||||
```typescript
|
||||
import { createHttpRouter, requestLogger, bodyLimit } from '@esengine/server'
|
||||
|
||||
const router = createHttpRouter({
|
||||
'/api/users': (req, res) => res.json([]),
|
||||
'/api/admin': {
|
||||
GET: {
|
||||
handler: (req, res) => res.json({ admin: true }),
|
||||
middlewares: [adminAuthMiddleware] // 路由级中间件
|
||||
}
|
||||
}
|
||||
}, {
|
||||
middlewares: [requestLogger(), bodyLimit(1024 * 1024)], // 全局中间件
|
||||
timeout: 30000 // 全局超时 30 秒
|
||||
})
|
||||
```
|
||||
|
||||
## 请求超时
|
||||
|
||||
### 全局超时
|
||||
|
||||
```typescript
|
||||
import { createHttpRouter } from '@esengine/server'
|
||||
|
||||
const router = createHttpRouter({
|
||||
'/api/data': async (req, res) => {
|
||||
// 如果处理超过 30 秒,自动返回 408 Request Timeout
|
||||
await someSlowOperation()
|
||||
res.json({ data: 'result' })
|
||||
}
|
||||
}, {
|
||||
timeout: 30000 // 30 秒
|
||||
})
|
||||
```
|
||||
|
||||
### 路由级超时
|
||||
|
||||
```typescript
|
||||
const router = createHttpRouter({
|
||||
'/api/quick': (req, res) => res.json({ fast: true }),
|
||||
|
||||
'/api/slow': {
|
||||
POST: {
|
||||
handler: async (req, res) => {
|
||||
await verySlowOperation()
|
||||
res.json({ done: true })
|
||||
},
|
||||
timeout: 120000 // 这个路由允许 2 分钟
|
||||
}
|
||||
}
|
||||
}, {
|
||||
timeout: 10000 // 全局 10 秒(被路由级覆盖)
|
||||
})
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **使用 defineHttp** - 获得更好的类型提示和代码组织
|
||||
2. **统一错误处理** - 使用 `res.error()` 返回一致的错误格式
|
||||
3. **启用 CORS** - 前后端分离时必须配置
|
||||
4. **目录组织** - 按功能模块组织 HTTP 路由文件
|
||||
5. **验证输入** - 始终验证 `req.body` 和 `req.query` 的内容
|
||||
6. **状态码规范** - 遵循 HTTP 状态码规范(200、201、400、401、404、500 等)
|
||||
7. **使用中间件** - 通过中间件实现认证、日志、限流等横切关注点
|
||||
8. **设置超时** - 避免慢请求阻塞服务器
|
||||
@@ -147,6 +147,7 @@ service.on('chat', (data) => {
|
||||
|
||||
- [客户端使用](/modules/network/client/) - NetworkPlugin、组件和系统
|
||||
- [服务器端](/modules/network/server/) - GameServer 和 Room 管理
|
||||
- [分布式房间](/modules/network/distributed/) - 多服务器房间管理和玩家路由
|
||||
- [状态同步](/modules/network/sync/) - 插值和快照缓冲
|
||||
- [客户端预测](/modules/network/prediction/) - 输入预测和服务器校正
|
||||
- [兴趣区域 (AOI)](/modules/network/aoi/) - 视野过滤和带宽优化
|
||||
|
||||
@@ -79,10 +79,47 @@ await server.start()
|
||||
| `tickRate` | `number` | `20` | 全局 Tick 频率 (Hz) |
|
||||
| `apiDir` | `string` | `'src/api'` | API 处理器目录 |
|
||||
| `msgDir` | `string` | `'src/msg'` | 消息处理器目录 |
|
||||
| `httpDir` | `string` | `'src/http'` | HTTP 路由目录 |
|
||||
| `httpPrefix` | `string` | `'/api'` | HTTP 路由前缀 |
|
||||
| `cors` | `boolean \| CorsOptions` | - | CORS 配置 |
|
||||
| `onStart` | `(port) => void` | - | 启动回调 |
|
||||
| `onConnect` | `(conn) => void` | - | 连接回调 |
|
||||
| `onDisconnect` | `(conn) => void` | - | 断开回调 |
|
||||
|
||||
## HTTP 路由
|
||||
|
||||
支持 HTTP API 与 WebSocket 共用端口,适用于登录、注册等场景。
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 3000,
|
||||
httpDir: './src/http', // HTTP 路由目录
|
||||
httpPrefix: '/api', // 路由前缀
|
||||
cors: true,
|
||||
|
||||
// 或内联定义
|
||||
http: {
|
||||
'/health': (req, res) => res.json({ status: 'ok' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
export default defineHttp<{ username: string; password: string }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body
|
||||
// 验证并返回 token...
|
||||
res.json({ token: '...' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
> 详细文档请参考 [HTTP 路由](/modules/network/http)
|
||||
|
||||
## Room 系统
|
||||
|
||||
Room 是游戏房间的基类,管理玩家和游戏状态。
|
||||
@@ -243,6 +280,122 @@ class GameRoom extends Room {
|
||||
}
|
||||
```
|
||||
|
||||
## Schema 验证
|
||||
|
||||
使用内置的 Schema 验证系统进行运行时类型验证:
|
||||
|
||||
### 基础用法
|
||||
|
||||
```typescript
|
||||
import { s, defineApiWithSchema } from '@esengine/server'
|
||||
|
||||
// 定义 Schema
|
||||
const MoveSchema = s.object({
|
||||
x: s.number(),
|
||||
y: s.number(),
|
||||
speed: s.number().optional()
|
||||
})
|
||||
|
||||
// 类型自动推断
|
||||
type Move = s.infer<typeof MoveSchema> // { x: number; y: number; speed?: number }
|
||||
|
||||
// 使用 Schema 定义 API(自动验证)
|
||||
export default defineApiWithSchema(MoveSchema, {
|
||||
handler(req, ctx) {
|
||||
// req 已验证,类型安全
|
||||
console.log(req.x, req.y)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 验证器类型
|
||||
|
||||
| 类型 | 示例 | 描述 |
|
||||
|------|------|------|
|
||||
| `s.string()` | `s.string().min(1).max(50)` | 字符串,支持长度限制 |
|
||||
| `s.number()` | `s.number().min(0).int()` | 数字,支持范围和整数限制 |
|
||||
| `s.boolean()` | `s.boolean()` | 布尔值 |
|
||||
| `s.literal()` | `s.literal('admin')` | 字面量类型 |
|
||||
| `s.object()` | `s.object({ name: s.string() })` | 对象 |
|
||||
| `s.array()` | `s.array(s.number())` | 数组 |
|
||||
| `s.enum()` | `s.enum(['a', 'b'] as const)` | 枚举 |
|
||||
| `s.union()` | `s.union([s.string(), s.number()])` | 联合类型 |
|
||||
| `s.record()` | `s.record(s.any())` | 记录类型 |
|
||||
|
||||
### 修饰符
|
||||
|
||||
```typescript
|
||||
// 可选字段
|
||||
s.string().optional()
|
||||
|
||||
// 默认值
|
||||
s.number().default(0)
|
||||
|
||||
// 可为 null
|
||||
s.string().nullable()
|
||||
|
||||
// 字符串验证
|
||||
s.string().min(1).max(100).email().url().regex(/^[a-z]+$/)
|
||||
|
||||
// 数字验证
|
||||
s.number().min(0).max(100).int().positive()
|
||||
|
||||
// 数组验证
|
||||
s.array(s.string()).min(1).max(10).nonempty()
|
||||
|
||||
// 对象验证
|
||||
s.object({ ... }).strict() // 不允许额外字段
|
||||
s.object({ ... }).partial() // 所有字段可选
|
||||
s.object({ ... }).pick('name', 'age') // 选择字段
|
||||
s.object({ ... }).omit('password') // 排除字段
|
||||
```
|
||||
|
||||
### 消息验证
|
||||
|
||||
```typescript
|
||||
import { s, defineMsgWithSchema } from '@esengine/server'
|
||||
|
||||
const InputSchema = s.object({
|
||||
keys: s.array(s.string()),
|
||||
timestamp: s.number()
|
||||
})
|
||||
|
||||
export default defineMsgWithSchema(InputSchema, {
|
||||
handler(msg, ctx) {
|
||||
// msg 已验证
|
||||
console.log(msg.keys, msg.timestamp)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 手动验证
|
||||
|
||||
```typescript
|
||||
import { s, parse, safeParse, createGuard } from '@esengine/server'
|
||||
|
||||
const UserSchema = s.object({
|
||||
name: s.string(),
|
||||
age: s.number().int().min(0)
|
||||
})
|
||||
|
||||
// 抛出错误
|
||||
const user = parse(UserSchema, data)
|
||||
|
||||
// 返回结果对象
|
||||
const result = safeParse(UserSchema, data)
|
||||
if (result.success) {
|
||||
console.log(result.data)
|
||||
} else {
|
||||
console.error(result.error)
|
||||
}
|
||||
|
||||
// 类型守卫
|
||||
const isUser = createGuard(UserSchema)
|
||||
if (isUser(data)) {
|
||||
// data 是 User 类型
|
||||
}
|
||||
```
|
||||
|
||||
## 协议定义
|
||||
|
||||
在 `src/shared/protocol.ts` 中定义客户端和服务端共享的类型:
|
||||
|
||||
@@ -125,23 +125,24 @@ tx:data:{key} - 业务数据
|
||||
|
||||
## MongoStorage
|
||||
|
||||
MongoDB 存储,适用于需要持久化和复杂查询的场景。使用工厂模式实现惰性连接。
|
||||
MongoDB 存储,适用于需要持久化和复杂查询的场景。使用 `@esengine/database-drivers` 的共享连接。
|
||||
|
||||
```typescript
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { MongoStorage } from '@esengine/transaction';
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// 工厂模式:惰性连接,首次操作时才创建连接
|
||||
const storage = new MongoStorage({
|
||||
factory: async () => {
|
||||
const client = new MongoClient('mongodb://localhost:27017');
|
||||
await client.connect();
|
||||
return client;
|
||||
},
|
||||
database: 'game',
|
||||
transactionCollection: 'transactions', // 事务日志集合
|
||||
dataCollection: 'transaction_data', // 业务数据集合
|
||||
lockCollection: 'transaction_locks', // 锁集合
|
||||
// 创建共享连接
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// 使用共享连接创建存储
|
||||
const storage = createMongoStorage(mongo, {
|
||||
transactionCollection: 'transactions', // 事务日志集合(可选)
|
||||
dataCollection: 'transaction_data', // 业务数据集合(可选)
|
||||
lockCollection: 'transaction_locks', // 锁集合(可选)
|
||||
});
|
||||
|
||||
// 创建索引(首次运行时执行)
|
||||
@@ -149,11 +150,14 @@ await storage.ensureIndexes();
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
|
||||
// 使用后关闭连接
|
||||
// 关闭存储(不会关闭共享连接)
|
||||
await storage.close();
|
||||
|
||||
// 或使用 await using 自动关闭 (TypeScript 5.2+)
|
||||
await using storage = new MongoStorage({ ... });
|
||||
// 共享连接可继续用于其他模块
|
||||
const userRepo = new UserRepository(mongo); // @esengine/database
|
||||
|
||||
// 最后关闭共享连接
|
||||
await mongo.disconnect();
|
||||
```
|
||||
|
||||
### 特点
|
||||
|
||||
Submodule examples/lawn-mower-demo updated: 6c5d682f3a...3f0695f59b
@@ -74,6 +74,7 @@
|
||||
"lint:fix": "turbo run lint:fix",
|
||||
"build:wasm": "cd packages/rust/engine && wasm-pack build --dev --out-dir pkg",
|
||||
"build:wasm:release": "cd packages/rust/engine && wasm-pack build --release --out-dir pkg",
|
||||
"build:rapier2d": "node scripts/build-rapier2d.mjs",
|
||||
"copy-modules": "node scripts/copy-engine-modules.mjs"
|
||||
},
|
||||
"author": "yhh",
|
||||
|
||||
@@ -8,12 +8,23 @@ Before running the editor, ensure you have the following installed:
|
||||
|
||||
- **Node.js** >= 18.x
|
||||
- **pnpm** >= 10.x
|
||||
- **Rust** >= 1.70 (for Tauri)
|
||||
- **Rust** >= 1.70 (for Tauri and WASM builds)
|
||||
- **wasm-pack** (for building Rapier2D physics engine)
|
||||
- **Platform-specific dependencies**:
|
||||
- **Windows**: Microsoft Visual Studio C++ Build Tools
|
||||
- **macOS**: Xcode Command Line Tools (`xcode-select --install`)
|
||||
- **Linux**: See [Tauri prerequisites](https://tauri.app/v1/guides/getting-started/prerequisites)
|
||||
|
||||
### Installing wasm-pack
|
||||
|
||||
```bash
|
||||
# Using cargo
|
||||
cargo install wasm-pack
|
||||
|
||||
# Or using the official installer script (Linux/macOS)
|
||||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Clone and Install
|
||||
@@ -24,7 +35,23 @@ cd esengine
|
||||
pnpm install
|
||||
```
|
||||
|
||||
### 2. Build Dependencies
|
||||
### 2. Build Rapier2D WASM
|
||||
|
||||
The editor depends on Rapier2D physics engine WASM artifacts. First-time setup only requires one command:
|
||||
|
||||
```bash
|
||||
pnpm build:rapier2d
|
||||
```
|
||||
|
||||
This command automatically:
|
||||
1. Prepares the Rust project
|
||||
2. Builds WASM
|
||||
3. Copies artifacts to `packages/physics/rapier2d/pkg`
|
||||
4. Generates TypeScript source code
|
||||
|
||||
> **Note**: Requires Rust and wasm-pack to be installed.
|
||||
|
||||
### 3. Build Editor
|
||||
|
||||
From the project root:
|
||||
|
||||
@@ -32,7 +59,7 @@ From the project root:
|
||||
pnpm build:editor
|
||||
```
|
||||
|
||||
### 3. Run Editor
|
||||
### 4. Run Editor
|
||||
|
||||
```bash
|
||||
cd packages/editor/editor-app
|
||||
@@ -43,6 +70,8 @@ pnpm tauri:dev
|
||||
|
||||
| Script | Description |
|
||||
|--------|-------------|
|
||||
| `pnpm build:rapier2d` | Build Rapier2D WASM (required for first-time setup) |
|
||||
| `pnpm build:editor` | Build editor and all dependencies |
|
||||
| `pnpm tauri:dev` | Run editor in development mode with hot-reload |
|
||||
| `pnpm tauri:build` | Build production application |
|
||||
| `pnpm build:sdk` | Build editor-runtime SDK |
|
||||
@@ -62,6 +91,17 @@ editor-app/
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Rapier2D WASM Build Failed
|
||||
|
||||
**Error**: `Could not resolve "../pkg/rapier_wasm2d"`
|
||||
|
||||
**Cause**: Missing Rapier2D WASM artifacts.
|
||||
|
||||
**Solution**:
|
||||
1. Ensure `wasm-pack` is installed: `cargo install wasm-pack`
|
||||
2. Run `pnpm build:rapier2d`
|
||||
3. Verify `packages/physics/rapier2d/pkg/` directory exists and contains `rapier_wasm2d_bg.wasm` file
|
||||
|
||||
### Build Errors
|
||||
|
||||
```bash
|
||||
@@ -76,6 +116,12 @@ pnpm build:editor
|
||||
rustup update
|
||||
```
|
||||
|
||||
### Windows Users Building WASM
|
||||
|
||||
The `pnpm build:rapier2d` script works directly on Windows. If you encounter issues:
|
||||
1. Use Git Bash or WSL
|
||||
2. Or download pre-built WASM artifacts from [Releases](https://github.com/esengine/esengine/releases)
|
||||
|
||||
## Documentation
|
||||
|
||||
- [ESEngine Documentation](https://esengine.cn/)
|
||||
|
||||
@@ -8,12 +8,23 @@
|
||||
|
||||
- **Node.js** >= 18.x
|
||||
- **pnpm** >= 10.x
|
||||
- **Rust** >= 1.70 (Tauri 需要)
|
||||
- **Rust** >= 1.70 (Tauri 和 WASM 构建需要)
|
||||
- **wasm-pack** (构建 Rapier2D 物理引擎需要)
|
||||
- **平台相关依赖**:
|
||||
- **Windows**: Microsoft Visual Studio C++ Build Tools
|
||||
- **macOS**: Xcode Command Line Tools (`xcode-select --install`)
|
||||
- **Linux**: 参考 [Tauri 环境配置](https://tauri.app/v1/guides/getting-started/prerequisites)
|
||||
|
||||
### 安装 wasm-pack
|
||||
|
||||
```bash
|
||||
# 使用 cargo 安装
|
||||
cargo install wasm-pack
|
||||
|
||||
# 或使用官方安装脚本 (Linux/macOS)
|
||||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 克隆并安装
|
||||
@@ -24,7 +35,23 @@ cd esengine
|
||||
pnpm install
|
||||
```
|
||||
|
||||
### 2. 构建依赖
|
||||
### 2. 构建 Rapier2D WASM
|
||||
|
||||
编辑器依赖 Rapier2D 物理引擎的 WASM 产物。首次构建只需执行一条命令:
|
||||
|
||||
```bash
|
||||
pnpm build:rapier2d
|
||||
```
|
||||
|
||||
该命令会自动完成以下步骤:
|
||||
1. 准备 Rust 项目
|
||||
2. 构建 WASM
|
||||
3. 复制产物到 `packages/physics/rapier2d/pkg`
|
||||
4. 生成 TypeScript 源码
|
||||
|
||||
> **注意**:需要已安装 Rust 和 wasm-pack。
|
||||
|
||||
### 3. 构建编辑器
|
||||
|
||||
在项目根目录执行:
|
||||
|
||||
@@ -32,7 +59,7 @@ pnpm install
|
||||
pnpm build:editor
|
||||
```
|
||||
|
||||
### 3. 启动编辑器
|
||||
### 4. 启动编辑器
|
||||
|
||||
```bash
|
||||
cd packages/editor/editor-app
|
||||
@@ -43,6 +70,8 @@ pnpm tauri:dev
|
||||
|
||||
| 脚本 | 说明 |
|
||||
|------|------|
|
||||
| `pnpm build:rapier2d` | 构建 Rapier2D WASM(首次构建必须执行)|
|
||||
| `pnpm build:editor` | 构建编辑器及所有依赖 |
|
||||
| `pnpm tauri:dev` | 开发模式运行编辑器(支持热重载)|
|
||||
| `pnpm tauri:build` | 构建生产版本应用 |
|
||||
| `pnpm build:sdk` | 构建 editor-runtime SDK |
|
||||
@@ -62,6 +91,17 @@ editor-app/
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Rapier2D WASM 构建失败
|
||||
|
||||
**错误**: `Could not resolve "../pkg/rapier_wasm2d"`
|
||||
|
||||
**原因**: 缺少 Rapier2D 的 WASM 产物。
|
||||
|
||||
**解决方案**:
|
||||
1. 确保已安装 `wasm-pack`:`cargo install wasm-pack`
|
||||
2. 执行 `pnpm build:rapier2d`
|
||||
3. 确认 `packages/physics/rapier2d/pkg/` 目录存在且包含 `rapier_wasm2d_bg.wasm` 文件
|
||||
|
||||
### 构建错误
|
||||
|
||||
```bash
|
||||
@@ -76,6 +116,12 @@ pnpm build:editor
|
||||
rustup update
|
||||
```
|
||||
|
||||
### Windows 用户构建 WASM
|
||||
|
||||
`pnpm build:rapier2d` 脚本在 Windows 上可以直接运行。如果遇到问题:
|
||||
1. 使用 Git Bash 或 WSL
|
||||
2. 或从 [Releases](https://github.com/esengine/esengine/releases) 下载预编译的 WASM 产物
|
||||
|
||||
## 文档
|
||||
|
||||
- [ESEngine 文档](https://esengine.cn/)
|
||||
|
||||
@@ -8,7 +8,10 @@
|
||||
"rootDir": "./src",
|
||||
"jsx": "react-jsx",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler"
|
||||
"moduleResolution": "bundler",
|
||||
"paths": {
|
||||
"@esengine/asset-system": ["../../../engine/asset-system/src"]
|
||||
}
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
||||
|
||||
@@ -2,8 +2,7 @@ import { defineConfig } from 'tsup';
|
||||
import { editorOnlyPreset } from '../../../tools/build-config/src/presets/plugin-tsup';
|
||||
|
||||
export default defineConfig({
|
||||
...editorOnlyPreset({
|
||||
external: ['@esengine/asset-system']
|
||||
}),
|
||||
tsconfig: 'tsconfig.build.json'
|
||||
...editorOnlyPreset({}),
|
||||
tsconfig: 'tsconfig.build.json',
|
||||
noExternal: ['@esengine/asset-system']
|
||||
});
|
||||
|
||||
@@ -1,5 +1,54 @@
|
||||
# @esengine/behavior-tree
|
||||
|
||||
## 4.2.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#408](https://github.com/esengine/esengine/pull/408) [`b9ea8d1`](https://github.com/esengine/esengine/commit/b9ea8d14cf38e1480f638c229f9ee150b65f0c60) Thanks [@esengine](https://github.com/esengine)! - feat: add action() and condition() methods to BehaviorTreeBuilder
|
||||
|
||||
Added new methods to support custom executor types directly in the builder:
|
||||
- `action(implementationType, name?, config?)` - Use custom action executors registered via `@NodeExecutorMetadata`
|
||||
- `condition(implementationType, name?, config?)` - Use custom condition executors
|
||||
|
||||
This provides a cleaner API for using custom node executors compared to the existing `executeAction()` which only supports blackboard functions.
|
||||
|
||||
Example:
|
||||
|
||||
```typescript
|
||||
// Define custom executor
|
||||
@NodeExecutorMetadata({
|
||||
implementationType: 'AttackAction',
|
||||
nodeType: NodeType.Action,
|
||||
displayName: 'Attack',
|
||||
category: 'Combat'
|
||||
})
|
||||
class AttackAction implements INodeExecutor {
|
||||
execute(context: NodeExecutionContext): TaskStatus {
|
||||
return TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
// Use in builder
|
||||
const tree = BehaviorTreeBuilder.create('AI')
|
||||
.selector('Root')
|
||||
.action('AttackAction', 'Attack', { damage: 50 })
|
||||
.end()
|
||||
.build();
|
||||
```
|
||||
|
||||
## 4.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#406](https://github.com/esengine/esengine/pull/406) [`0de4527`](https://github.com/esengine/esengine/commit/0de45279e612c04ae9be7fbd65ce496e4797a43c) Thanks [@esengine](https://github.com/esengine)! - fix(behavior-tree): export NodeExecutorMetadata as value instead of type
|
||||
|
||||
Fixed the export of `NodeExecutorMetadata` decorator in `execution/index.ts`.
|
||||
Previously it was exported as `export type { NodeExecutorMetadata }` which only
|
||||
exported the type signature, not the actual function. This caused runtime errors
|
||||
in Cocos Creator: "TypeError: (intermediate value) is not a function".
|
||||
|
||||
Changed to `export { NodeExecutorMetadata }` to properly export the decorator function.
|
||||
|
||||
## 4.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@esengine/behavior-tree",
|
||||
"version": "4.1.1",
|
||||
"version": "4.2.0",
|
||||
"description": "ECS-based AI behavior tree system - works with any ECS framework (ESEngine, Cocos, Laya, etc.)",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
@@ -29,7 +29,8 @@
|
||||
"clean": "rimraf dist tsconfig.tsbuildinfo",
|
||||
"build": "tsup",
|
||||
"build:watch": "tsup --watch",
|
||||
"type-check": "tsc --noEmit"
|
||||
"type-check": "tsc --noEmit",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
||||
},
|
||||
"author": "yhh",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -181,12 +181,73 @@ export class BehaviorTreeBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加执行动作
|
||||
* 添加执行动作(通过黑板函数)
|
||||
*
|
||||
* @zh 使用黑板中的 action_{actionName} 函数执行动作
|
||||
* @en Execute action using action_{actionName} function from blackboard
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* BehaviorTreeBuilder.create("AI")
|
||||
* .defineBlackboardVariable("action_Attack", (entity) => TaskStatus.Success)
|
||||
* .selector("Root")
|
||||
* .executeAction("Attack")
|
||||
* .end()
|
||||
* .build();
|
||||
* ```
|
||||
*/
|
||||
executeAction(actionName: string, name?: string): BehaviorTreeBuilder {
|
||||
return this.addActionNode('ExecuteAction', name || 'ExecuteAction', { actionName });
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加自定义动作节点
|
||||
*
|
||||
* @zh 直接使用注册的执行器类型(通过 @NodeExecutorMetadata 装饰器注册的类)
|
||||
* @en Use a registered executor type directly (class registered via @NodeExecutorMetadata decorator)
|
||||
*
|
||||
* @param implementationType - 执行器类型名称(@NodeExecutorMetadata 中的 implementationType)
|
||||
* @param name - 节点显示名称
|
||||
* @param config - 节点配置参数
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 1. 定义自定义执行器
|
||||
* @NodeExecutorMetadata({
|
||||
* implementationType: 'AttackAction',
|
||||
* nodeType: NodeType.Action,
|
||||
* displayName: '攻击动作',
|
||||
* category: 'Action'
|
||||
* })
|
||||
* class AttackAction implements INodeExecutor {
|
||||
* execute(context: NodeExecutionContext): TaskStatus {
|
||||
* console.log("执行攻击!");
|
||||
* return TaskStatus.Success;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // 2. 在行为树中使用
|
||||
* BehaviorTreeBuilder.create("AI")
|
||||
* .selector("Root")
|
||||
* .action("AttackAction", "Attack")
|
||||
* .end()
|
||||
* .build();
|
||||
* ```
|
||||
*/
|
||||
action(implementationType: string, name?: string, config?: Record<string, any>): BehaviorTreeBuilder {
|
||||
return this.addActionNode(implementationType, name || implementationType, config || {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加自定义条件节点
|
||||
*
|
||||
* @zh 直接使用注册的条件执行器类型
|
||||
* @en Use a registered condition executor type directly
|
||||
*/
|
||||
condition(implementationType: string, name?: string, config?: Record<string, any>): BehaviorTreeBuilder {
|
||||
return this.addConditionNode(implementationType, name || implementationType, config || {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加黑板比较条件
|
||||
*/
|
||||
|
||||
@@ -5,7 +5,7 @@ export { BehaviorTreeAssetManager } from './BehaviorTreeAssetManager';
|
||||
export type { INodeExecutor, NodeExecutionContext } from './NodeExecutor';
|
||||
export { NodeExecutorRegistry, BindingHelper } from './NodeExecutor';
|
||||
export { BehaviorTreeExecutionSystem } from './BehaviorTreeExecutionSystem';
|
||||
export type { NodeMetadata, ConfigFieldDefinition, NodeExecutorMetadata } from './NodeMetadata';
|
||||
export { NodeMetadataRegistry } from './NodeMetadata';
|
||||
export type { NodeMetadata, ConfigFieldDefinition } from './NodeMetadata';
|
||||
export { NodeMetadataRegistry, NodeExecutorMetadata } from './NodeMetadata';
|
||||
|
||||
export * from './Executors';
|
||||
|
||||
@@ -742,6 +742,7 @@ export class Core {
|
||||
if (!this._instance) return;
|
||||
|
||||
this._instance._debugManager?.stop();
|
||||
this._instance._sceneManager.destroy();
|
||||
this._instance._serviceContainer.clear();
|
||||
Core._logger.info('Core destroyed');
|
||||
this._instance = null;
|
||||
|
||||
57
packages/framework/database-drivers/CHANGELOG.md
Normal file
57
packages/framework/database-drivers/CHANGELOG.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# @esengine/database-drivers
|
||||
|
||||
## 1.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#412](https://github.com/esengine/esengine/pull/412) [`85171a0`](https://github.com/esengine/esengine/commit/85171a0a5c073ef7883705ee4daaca8bb0218f20) Thanks [@esengine](https://github.com/esengine)! - fix: include dist directory in npm package
|
||||
|
||||
Previous 1.1.0 release was missing the compiled dist directory.
|
||||
|
||||
## 1.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#410](https://github.com/esengine/esengine/pull/410) [`71022ab`](https://github.com/esengine/esengine/commit/71022abc99ad4a1b349f19f4ccf1e0a2a0923dfa) Thanks [@esengine](https://github.com/esengine)! - feat: add database layer architecture
|
||||
|
||||
Added new database packages with layered architecture:
|
||||
|
||||
**@esengine/database-drivers (Layer 1)**
|
||||
- MongoDB connection with pool management, auto-reconnect, events
|
||||
- Redis connection with auto-reconnect, key prefix
|
||||
- Type-safe `IMongoCollection<T>` interface decoupled from mongodb types
|
||||
- Service tokens for dependency injection (`MongoConnectionToken`, `RedisConnectionToken`)
|
||||
|
||||
**@esengine/database (Layer 2)**
|
||||
- Generic `Repository<T>` with CRUD, pagination, soft delete
|
||||
- `UserRepository` with registration, authentication, role management
|
||||
- Password hashing utilities using scrypt
|
||||
- Query operators: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$in`, `$nin`, `$like`, `$regex`
|
||||
|
||||
**@esengine/transaction**
|
||||
- Refactored `MongoStorage` to use shared connection from `@esengine/database-drivers`
|
||||
- Removed factory pattern in favor of shared connection (breaking change)
|
||||
- Simplified API: `createMongoStorage(connection, options?)`
|
||||
|
||||
Example usage:
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { UserRepository } from '@esengine/database';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// Create shared connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// Use for database operations
|
||||
const userRepo = new UserRepository(mongo);
|
||||
await userRepo.register({ username: 'john', password: '123456' });
|
||||
|
||||
// Use for transactions (same connection)
|
||||
const storage = createMongoStorage(mongo);
|
||||
const txManager = new TransactionManager({ storage });
|
||||
```
|
||||
23
packages/framework/database-drivers/module.json
Normal file
23
packages/framework/database-drivers/module.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"id": "database-drivers",
|
||||
"name": "@esengine/database-drivers",
|
||||
"globalKey": "database-drivers",
|
||||
"displayName": "Database Drivers",
|
||||
"description": "数据库连接驱动,提供 MongoDB、Redis 等数据库的连接管理 | Database connection drivers with connection pooling for MongoDB, Redis, etc.",
|
||||
"version": "1.0.0",
|
||||
"category": "Infrastructure",
|
||||
"icon": "Database",
|
||||
"tags": ["database", "mongodb", "redis", "connection"],
|
||||
"isCore": false,
|
||||
"defaultEnabled": true,
|
||||
"isEngineModule": false,
|
||||
"canContainContent": false,
|
||||
"platforms": ["server"],
|
||||
"dependencies": [],
|
||||
"exports": {
|
||||
"components": [],
|
||||
"systems": []
|
||||
},
|
||||
"requiresWasm": false,
|
||||
"outputPath": "dist/index.js"
|
||||
}
|
||||
48
packages/framework/database-drivers/package.json
Normal file
48
packages/framework/database-drivers/package.json
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "@esengine/database-drivers",
|
||||
"version": "1.1.1",
|
||||
"description": "Database connection drivers for ESEngine | ESEngine 数据库连接驱动",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"module.json"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"build:watch": "tsup --watch",
|
||||
"type-check": "tsc --noEmit",
|
||||
"clean": "rimraf dist"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"mongodb": ">=6.0.0",
|
||||
"ioredis": ">=5.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"mongodb": {
|
||||
"optional": true
|
||||
},
|
||||
"ioredis": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"mongodb": "^6.12.0",
|
||||
"ioredis": "^5.3.0",
|
||||
"tsup": "^8.0.0",
|
||||
"typescript": "^5.8.0",
|
||||
"rimraf": "^5.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* @zh MongoDB 集合适配器
|
||||
* @en MongoDB collection adapter
|
||||
*
|
||||
* @zh 将 MongoDB 原生 Collection 适配为简化接口
|
||||
* @en Adapts native MongoDB Collection to simplified interface
|
||||
*/
|
||||
|
||||
import type { Collection, Db } from 'mongodb'
|
||||
import type {
|
||||
DeleteResult,
|
||||
FindOneAndUpdateOptions,
|
||||
FindOptions,
|
||||
IMongoCollection,
|
||||
IMongoDatabase,
|
||||
IndexOptions,
|
||||
InsertManyResult,
|
||||
InsertOneResult,
|
||||
UpdateResult
|
||||
} from '../interfaces/IMongoCollection.js'
|
||||
|
||||
/**
|
||||
* @zh MongoDB 集合适配器
|
||||
* @en MongoDB collection adapter
|
||||
*/
|
||||
export class MongoCollectionAdapter<T extends object> implements IMongoCollection<T> {
|
||||
readonly name: string
|
||||
|
||||
constructor(private readonly _collection: Collection<T>) {
|
||||
this.name = _collection.collectionName
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 查询 | Query
|
||||
// =========================================================================
|
||||
|
||||
async findOne(filter: object, options?: FindOptions): Promise<T | null> {
|
||||
const doc = await this._collection.findOne(
|
||||
filter as Parameters<typeof this._collection.findOne>[0],
|
||||
{
|
||||
sort: options?.sort as Parameters<typeof this._collection.findOne>[1] extends { sort?: infer S } ? S : never,
|
||||
projection: options?.projection
|
||||
}
|
||||
)
|
||||
return doc ? this._stripId(doc) : null
|
||||
}
|
||||
|
||||
async find(filter: object, options?: FindOptions): Promise<T[]> {
|
||||
let cursor = this._collection.find(
|
||||
filter as Parameters<typeof this._collection.find>[0]
|
||||
)
|
||||
|
||||
if (options?.sort) {
|
||||
cursor = cursor.sort(options.sort as Parameters<typeof cursor.sort>[0])
|
||||
}
|
||||
|
||||
if (options?.skip) {
|
||||
cursor = cursor.skip(options.skip)
|
||||
}
|
||||
|
||||
if (options?.limit) {
|
||||
cursor = cursor.limit(options.limit)
|
||||
}
|
||||
|
||||
if (options?.projection) {
|
||||
cursor = cursor.project(options.projection)
|
||||
}
|
||||
|
||||
const docs = await cursor.toArray()
|
||||
return docs.map(doc => this._stripId(doc))
|
||||
}
|
||||
|
||||
async countDocuments(filter?: object): Promise<number> {
|
||||
return this._collection.countDocuments(
|
||||
(filter ?? {}) as Parameters<typeof this._collection.countDocuments>[0]
|
||||
)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 创建 | Create
|
||||
// =========================================================================
|
||||
|
||||
async insertOne(doc: T): Promise<InsertOneResult> {
|
||||
const result = await this._collection.insertOne(
|
||||
doc as Parameters<typeof this._collection.insertOne>[0]
|
||||
)
|
||||
return {
|
||||
insertedId: result.insertedId,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async insertMany(docs: T[]): Promise<InsertManyResult> {
|
||||
const result = await this._collection.insertMany(
|
||||
docs as Parameters<typeof this._collection.insertMany>[0]
|
||||
)
|
||||
return {
|
||||
insertedCount: result.insertedCount,
|
||||
insertedIds: result.insertedIds as Record<number, unknown>,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 更新 | Update
|
||||
// =========================================================================
|
||||
|
||||
async updateOne(filter: object, update: object): Promise<UpdateResult> {
|
||||
const result = await this._collection.updateOne(
|
||||
filter as Parameters<typeof this._collection.updateOne>[0],
|
||||
update as Parameters<typeof this._collection.updateOne>[1]
|
||||
)
|
||||
return {
|
||||
matchedCount: result.matchedCount,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount,
|
||||
upsertedId: result.upsertedId,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async updateMany(filter: object, update: object): Promise<UpdateResult> {
|
||||
const result = await this._collection.updateMany(
|
||||
filter as Parameters<typeof this._collection.updateMany>[0],
|
||||
update as Parameters<typeof this._collection.updateMany>[1]
|
||||
)
|
||||
return {
|
||||
matchedCount: result.matchedCount,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount,
|
||||
upsertedId: result.upsertedId,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async findOneAndUpdate(
|
||||
filter: object,
|
||||
update: object,
|
||||
options?: FindOneAndUpdateOptions
|
||||
): Promise<T | null> {
|
||||
const result = await this._collection.findOneAndUpdate(
|
||||
filter as Parameters<typeof this._collection.findOneAndUpdate>[0],
|
||||
update as Parameters<typeof this._collection.findOneAndUpdate>[1],
|
||||
{
|
||||
returnDocument: options?.returnDocument ?? 'after',
|
||||
upsert: options?.upsert
|
||||
}
|
||||
)
|
||||
return result ? this._stripId(result) : null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 删除 | Delete
|
||||
// =========================================================================
|
||||
|
||||
async deleteOne(filter: object): Promise<DeleteResult> {
|
||||
const result = await this._collection.deleteOne(
|
||||
filter as Parameters<typeof this._collection.deleteOne>[0]
|
||||
)
|
||||
return {
|
||||
deletedCount: result.deletedCount,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async deleteMany(filter: object): Promise<DeleteResult> {
|
||||
const result = await this._collection.deleteMany(
|
||||
filter as Parameters<typeof this._collection.deleteMany>[0]
|
||||
)
|
||||
return {
|
||||
deletedCount: result.deletedCount,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 索引 | Index
|
||||
// =========================================================================
|
||||
|
||||
async createIndex(
|
||||
spec: Record<string, 1 | -1>,
|
||||
options?: IndexOptions
|
||||
): Promise<string> {
|
||||
return this._collection.createIndex(spec, options)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 内部方法 | Internal Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 移除 MongoDB 的 _id 字段
|
||||
* @en Remove MongoDB's _id field
|
||||
*/
|
||||
private _stripId<D extends object>(doc: D): D {
|
||||
const { _id, ...rest } = doc as { _id?: unknown } & Record<string, unknown>
|
||||
return rest as D
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh MongoDB 数据库适配器
|
||||
* @en MongoDB database adapter
|
||||
*/
|
||||
export class MongoDatabaseAdapter implements IMongoDatabase {
|
||||
readonly name: string
|
||||
private _collections = new Map<string, MongoCollectionAdapter<object>>()
|
||||
|
||||
constructor(private readonly _db: Db) {
|
||||
this.name = _db.databaseName
|
||||
}
|
||||
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T> {
|
||||
if (!this._collections.has(name)) {
|
||||
const nativeCollection = this._db.collection<T>(name)
|
||||
this._collections.set(
|
||||
name,
|
||||
new MongoCollectionAdapter(nativeCollection) as MongoCollectionAdapter<object>
|
||||
)
|
||||
}
|
||||
return this._collections.get(name) as IMongoCollection<T>
|
||||
}
|
||||
|
||||
async listCollections(): Promise<string[]> {
|
||||
const collections = await this._db.listCollections().toArray()
|
||||
return collections.map(c => c.name)
|
||||
}
|
||||
|
||||
async dropCollection(name: string): Promise<boolean> {
|
||||
try {
|
||||
await this._db.dropCollection(name)
|
||||
this._collections.delete(name)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
/**
|
||||
* @zh MongoDB 连接驱动
|
||||
* @en MongoDB connection driver
|
||||
*
|
||||
* @zh 提供 MongoDB 数据库的连接管理、自动重连和事件通知
|
||||
* @en Provides MongoDB connection management, auto-reconnect, and event notification
|
||||
*/
|
||||
|
||||
import type { Db, MongoClient as MongoClientType, MongoClientOptions } from 'mongodb'
|
||||
import { randomUUID } from 'crypto'
|
||||
import {
|
||||
ConnectionError,
|
||||
type ConnectionEvent,
|
||||
type ConnectionEventListener,
|
||||
type ConnectionEventType,
|
||||
type ConnectionState,
|
||||
type IEventableConnection,
|
||||
type MongoConnectionConfig
|
||||
} from '../types.js'
|
||||
import type { IMongoCollection, IMongoDatabase } from '../interfaces/IMongoCollection.js'
|
||||
import { MongoDatabaseAdapter } from '../adapters/MongoCollectionAdapter.js'
|
||||
|
||||
/**
|
||||
* @zh MongoDB 连接接口
|
||||
* @en MongoDB connection interface
|
||||
*/
|
||||
export interface IMongoConnection extends IEventableConnection {
|
||||
/**
|
||||
* @zh 获取数据库接口
|
||||
* @en Get database interface
|
||||
*/
|
||||
getDatabase(): IMongoDatabase
|
||||
|
||||
/**
|
||||
* @zh 获取原生客户端(高级用法)
|
||||
* @en Get native client (advanced usage)
|
||||
*/
|
||||
getNativeClient(): MongoClientType
|
||||
|
||||
/**
|
||||
* @zh 获取原生数据库(高级用法)
|
||||
* @en Get native database (advanced usage)
|
||||
*/
|
||||
getNativeDatabase(): Db
|
||||
|
||||
/**
|
||||
* @zh 获取集合
|
||||
* @en Get collection
|
||||
*/
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh MongoDB 连接实现
|
||||
* @en MongoDB connection implementation
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const mongo = new MongoConnection({
|
||||
* uri: 'mongodb://localhost:27017',
|
||||
* database: 'game',
|
||||
* autoReconnect: true,
|
||||
* })
|
||||
*
|
||||
* mongo.on('connected', () => console.log('Connected!'))
|
||||
* mongo.on('error', (e) => console.error('Error:', e.error))
|
||||
*
|
||||
* await mongo.connect()
|
||||
*
|
||||
* const users = mongo.collection('users')
|
||||
* await users.insertOne({ name: 'test' })
|
||||
*
|
||||
* await mongo.disconnect()
|
||||
* ```
|
||||
*/
|
||||
export class MongoConnection implements IMongoConnection {
|
||||
readonly id: string
|
||||
private _state: ConnectionState = 'disconnected'
|
||||
private _client: MongoClientType | null = null
|
||||
private _db: Db | null = null
|
||||
private _config: MongoConnectionConfig
|
||||
private _listeners = new Map<ConnectionEventType, Set<ConnectionEventListener>>()
|
||||
private _reconnectAttempts = 0
|
||||
private _reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
constructor(config: MongoConnectionConfig) {
|
||||
this.id = randomUUID()
|
||||
this._config = {
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10,
|
||||
...config
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 状态 | State
|
||||
// =========================================================================
|
||||
|
||||
get state(): ConnectionState {
|
||||
return this._state
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this._state === 'connected' && this._client !== null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 连接管理 | Connection Management
|
||||
// =========================================================================
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this._state === 'connected') {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._state === 'connecting') {
|
||||
throw new ConnectionError('Connection already in progress')
|
||||
}
|
||||
|
||||
this._state = 'connecting'
|
||||
|
||||
try {
|
||||
const { MongoClient } = await import('mongodb')
|
||||
|
||||
const options: MongoClientOptions = {}
|
||||
if (this._config.pool) {
|
||||
if (this._config.pool.minSize) {
|
||||
options.minPoolSize = this._config.pool.minSize
|
||||
}
|
||||
if (this._config.pool.maxSize) {
|
||||
options.maxPoolSize = this._config.pool.maxSize
|
||||
}
|
||||
if (this._config.pool.acquireTimeout) {
|
||||
options.waitQueueTimeoutMS = this._config.pool.acquireTimeout
|
||||
}
|
||||
if (this._config.pool.maxLifetime) {
|
||||
options.maxIdleTimeMS = this._config.pool.maxLifetime
|
||||
}
|
||||
}
|
||||
|
||||
this._client = new MongoClient(this._config.uri, options)
|
||||
await this._client.connect()
|
||||
this._db = this._client.db(this._config.database)
|
||||
|
||||
this._state = 'connected'
|
||||
this._reconnectAttempts = 0
|
||||
this._emit('connected')
|
||||
|
||||
this._setupClientEvents()
|
||||
} catch (error) {
|
||||
this._state = 'error'
|
||||
const connError = new ConnectionError(
|
||||
`Failed to connect to MongoDB: ${(error as Error).message}`,
|
||||
'CONNECTION_FAILED',
|
||||
error as Error
|
||||
)
|
||||
this._emit('error', connError)
|
||||
throw connError
|
||||
}
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (this._state === 'disconnected') {
|
||||
return
|
||||
}
|
||||
|
||||
this._clearReconnectTimer()
|
||||
this._state = 'disconnecting'
|
||||
|
||||
try {
|
||||
if (this._client) {
|
||||
await this._client.close()
|
||||
this._client = null
|
||||
this._db = null
|
||||
}
|
||||
|
||||
this._state = 'disconnected'
|
||||
this._emit('disconnected')
|
||||
} catch (error) {
|
||||
this._state = 'error'
|
||||
throw new ConnectionError(
|
||||
`Failed to disconnect: ${(error as Error).message}`,
|
||||
'CONNECTION_FAILED',
|
||||
error as Error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async ping(): Promise<boolean> {
|
||||
if (!this._db) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
await this._db.command({ ping: 1 })
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 数据库访问 | Database Access
|
||||
// =========================================================================
|
||||
|
||||
private _dbAdapter: MongoDatabaseAdapter | null = null
|
||||
|
||||
getDatabase(): IMongoDatabase {
|
||||
if (!this._db) {
|
||||
throw new ConnectionError('Not connected to database', 'CONNECTION_CLOSED')
|
||||
}
|
||||
if (!this._dbAdapter) {
|
||||
this._dbAdapter = new MongoDatabaseAdapter(this._db)
|
||||
}
|
||||
return this._dbAdapter
|
||||
}
|
||||
|
||||
getNativeDatabase(): Db {
|
||||
if (!this._db) {
|
||||
throw new ConnectionError('Not connected to database', 'CONNECTION_CLOSED')
|
||||
}
|
||||
return this._db
|
||||
}
|
||||
|
||||
getNativeClient(): MongoClientType {
|
||||
if (!this._client) {
|
||||
throw new ConnectionError('Not connected to database', 'CONNECTION_CLOSED')
|
||||
}
|
||||
return this._client
|
||||
}
|
||||
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T> {
|
||||
return this.getDatabase().collection<T>(name)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 事件 | Events
|
||||
// =========================================================================
|
||||
|
||||
on(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
if (!this._listeners.has(event)) {
|
||||
this._listeners.set(event, new Set())
|
||||
}
|
||||
this._listeners.get(event)!.add(listener)
|
||||
}
|
||||
|
||||
off(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
this._listeners.get(event)?.delete(listener)
|
||||
}
|
||||
|
||||
once(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
const wrapper: ConnectionEventListener = (e) => {
|
||||
this.off(event, wrapper)
|
||||
listener(e)
|
||||
}
|
||||
this.on(event, wrapper)
|
||||
}
|
||||
|
||||
private _emit(type: ConnectionEventType, error?: Error): void {
|
||||
const event: ConnectionEvent = {
|
||||
type,
|
||||
connectionId: this.id,
|
||||
timestamp: Date.now(),
|
||||
error
|
||||
}
|
||||
|
||||
const listeners = this._listeners.get(type)
|
||||
if (listeners) {
|
||||
for (const listener of listeners) {
|
||||
try {
|
||||
listener(event)
|
||||
} catch {
|
||||
// Ignore listener errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 内部方法 | Internal Methods
|
||||
// =========================================================================
|
||||
|
||||
private _setupClientEvents(): void {
|
||||
if (!this._client) return
|
||||
|
||||
this._client.on('close', () => {
|
||||
if (this._state === 'connected') {
|
||||
this._state = 'disconnected'
|
||||
this._emit('disconnected')
|
||||
this._scheduleReconnect()
|
||||
}
|
||||
})
|
||||
|
||||
this._client.on('error', (error) => {
|
||||
this._emit('error', error)
|
||||
})
|
||||
}
|
||||
|
||||
private _scheduleReconnect(): void {
|
||||
if (!this._config.autoReconnect) return
|
||||
if (this._reconnectAttempts >= (this._config.maxReconnectAttempts ?? 10)) {
|
||||
return
|
||||
}
|
||||
|
||||
this._clearReconnectTimer()
|
||||
this._emit('reconnecting')
|
||||
|
||||
this._reconnectTimer = setTimeout(async () => {
|
||||
this._reconnectAttempts++
|
||||
try {
|
||||
await this.connect()
|
||||
this._emit('reconnected')
|
||||
} catch {
|
||||
this._scheduleReconnect()
|
||||
}
|
||||
}, this._config.reconnectInterval ?? 5000)
|
||||
}
|
||||
|
||||
private _clearReconnectTimer(): void {
|
||||
if (this._reconnectTimer) {
|
||||
clearTimeout(this._reconnectTimer)
|
||||
this._reconnectTimer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建 MongoDB 连接
|
||||
* @en Create MongoDB connection
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const mongo = createMongoConnection({
|
||||
* uri: process.env.MONGODB_URI!,
|
||||
* database: 'game',
|
||||
* })
|
||||
* await mongo.connect()
|
||||
* ```
|
||||
*/
|
||||
export function createMongoConnection(config: MongoConnectionConfig): MongoConnection {
|
||||
return new MongoConnection(config)
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
/**
|
||||
* @zh Redis 连接驱动
|
||||
* @en Redis connection driver
|
||||
*
|
||||
* @zh 提供 Redis 数据库的连接管理、自动重连和事件通知
|
||||
* @en Provides Redis connection management, auto-reconnect, and event notification
|
||||
*/
|
||||
|
||||
import type { Redis as RedisClientType, RedisOptions } from 'ioredis'
|
||||
import { randomUUID } from 'crypto'
|
||||
import {
|
||||
ConnectionError,
|
||||
type ConnectionEvent,
|
||||
type ConnectionEventListener,
|
||||
type ConnectionEventType,
|
||||
type ConnectionState,
|
||||
type IEventableConnection,
|
||||
type RedisConnectionConfig
|
||||
} from '../types.js'
|
||||
|
||||
/**
|
||||
* @zh Redis 连接接口
|
||||
* @en Redis connection interface
|
||||
*/
|
||||
export interface IRedisConnection extends IEventableConnection {
|
||||
/**
|
||||
* @zh 获取原生客户端
|
||||
* @en Get native client
|
||||
*/
|
||||
getClient(): RedisClientType
|
||||
|
||||
/**
|
||||
* @zh 获取键值
|
||||
* @en Get value by key
|
||||
*/
|
||||
get(key: string): Promise<string | null>
|
||||
|
||||
/**
|
||||
* @zh 设置键值
|
||||
* @en Set key value
|
||||
*/
|
||||
set(key: string, value: string, ttl?: number): Promise<void>
|
||||
|
||||
/**
|
||||
* @zh 删除键
|
||||
* @en Delete key
|
||||
*/
|
||||
del(key: string): Promise<boolean>
|
||||
|
||||
/**
|
||||
* @zh 检查键是否存在
|
||||
* @en Check if key exists
|
||||
*/
|
||||
exists(key: string): Promise<boolean>
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh Redis 连接实现
|
||||
* @en Redis connection implementation
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const redis = new RedisConnection({
|
||||
* host: 'localhost',
|
||||
* port: 6379,
|
||||
* keyPrefix: 'game:',
|
||||
* })
|
||||
*
|
||||
* await redis.connect()
|
||||
*
|
||||
* await redis.set('player:1:score', '100', 3600)
|
||||
* const score = await redis.get('player:1:score')
|
||||
*
|
||||
* await redis.disconnect()
|
||||
* ```
|
||||
*/
|
||||
export class RedisConnection implements IRedisConnection {
|
||||
readonly id: string
|
||||
private _state: ConnectionState = 'disconnected'
|
||||
private _client: RedisClientType | null = null
|
||||
private _config: RedisConnectionConfig
|
||||
private _listeners = new Map<ConnectionEventType, Set<ConnectionEventListener>>()
|
||||
|
||||
constructor(config: RedisConnectionConfig) {
|
||||
this.id = randomUUID()
|
||||
this._config = {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
autoReconnect: true,
|
||||
...config
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 状态 | State
|
||||
// =========================================================================
|
||||
|
||||
get state(): ConnectionState {
|
||||
return this._state
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this._state === 'connected' && this._client !== null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 连接管理 | Connection Management
|
||||
// =========================================================================
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this._state === 'connected') {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._state === 'connecting') {
|
||||
throw new ConnectionError('Connection already in progress')
|
||||
}
|
||||
|
||||
this._state = 'connecting'
|
||||
|
||||
try {
|
||||
const Redis = (await import('ioredis')).default
|
||||
|
||||
const options: RedisOptions = {
|
||||
host: this._config.host,
|
||||
port: this._config.port,
|
||||
password: this._config.password,
|
||||
db: this._config.db,
|
||||
keyPrefix: this._config.keyPrefix,
|
||||
retryStrategy: this._config.autoReconnect
|
||||
? (times) => Math.min(times * 100, 3000)
|
||||
: () => null,
|
||||
lazyConnect: true
|
||||
}
|
||||
|
||||
if (this._config.url) {
|
||||
this._client = new Redis(this._config.url, options)
|
||||
} else {
|
||||
this._client = new Redis(options)
|
||||
}
|
||||
|
||||
this._setupClientEvents()
|
||||
await this._client.connect()
|
||||
|
||||
this._state = 'connected'
|
||||
this._emit('connected')
|
||||
} catch (error) {
|
||||
this._state = 'error'
|
||||
const connError = new ConnectionError(
|
||||
`Failed to connect to Redis: ${(error as Error).message}`,
|
||||
'CONNECTION_FAILED',
|
||||
error as Error
|
||||
)
|
||||
this._emit('error', connError)
|
||||
throw connError
|
||||
}
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (this._state === 'disconnected') {
|
||||
return
|
||||
}
|
||||
|
||||
this._state = 'disconnecting'
|
||||
|
||||
try {
|
||||
if (this._client) {
|
||||
await this._client.quit()
|
||||
this._client = null
|
||||
}
|
||||
|
||||
this._state = 'disconnected'
|
||||
this._emit('disconnected')
|
||||
} catch (error) {
|
||||
this._state = 'error'
|
||||
throw new ConnectionError(
|
||||
`Failed to disconnect: ${(error as Error).message}`,
|
||||
'CONNECTION_FAILED',
|
||||
error as Error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async ping(): Promise<boolean> {
|
||||
if (!this._client) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this._client.ping()
|
||||
return result === 'PONG'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 数据操作 | Data Operations
|
||||
// =========================================================================
|
||||
|
||||
getClient(): RedisClientType {
|
||||
if (!this._client) {
|
||||
throw new ConnectionError('Not connected to Redis', 'CONNECTION_CLOSED')
|
||||
}
|
||||
return this._client
|
||||
}
|
||||
|
||||
async get(key: string): Promise<string | null> {
|
||||
return this.getClient().get(key)
|
||||
}
|
||||
|
||||
async set(key: string, value: string, ttl?: number): Promise<void> {
|
||||
const client = this.getClient()
|
||||
if (ttl) {
|
||||
await client.setex(key, ttl, value)
|
||||
} else {
|
||||
await client.set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
async del(key: string): Promise<boolean> {
|
||||
const result = await this.getClient().del(key)
|
||||
return result > 0
|
||||
}
|
||||
|
||||
async exists(key: string): Promise<boolean> {
|
||||
const result = await this.getClient().exists(key)
|
||||
return result > 0
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 事件 | Events
|
||||
// =========================================================================
|
||||
|
||||
on(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
if (!this._listeners.has(event)) {
|
||||
this._listeners.set(event, new Set())
|
||||
}
|
||||
this._listeners.get(event)!.add(listener)
|
||||
}
|
||||
|
||||
off(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
this._listeners.get(event)?.delete(listener)
|
||||
}
|
||||
|
||||
once(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
const wrapper: ConnectionEventListener = (e) => {
|
||||
this.off(event, wrapper)
|
||||
listener(e)
|
||||
}
|
||||
this.on(event, wrapper)
|
||||
}
|
||||
|
||||
private _emit(type: ConnectionEventType, error?: Error): void {
|
||||
const event: ConnectionEvent = {
|
||||
type,
|
||||
connectionId: this.id,
|
||||
timestamp: Date.now(),
|
||||
error
|
||||
}
|
||||
|
||||
const listeners = this._listeners.get(type)
|
||||
if (listeners) {
|
||||
for (const listener of listeners) {
|
||||
try {
|
||||
listener(event)
|
||||
} catch {
|
||||
// Ignore listener errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _setupClientEvents(): void {
|
||||
if (!this._client) return
|
||||
|
||||
this._client.on('close', () => {
|
||||
if (this._state === 'connected') {
|
||||
this._state = 'disconnected'
|
||||
this._emit('disconnected')
|
||||
}
|
||||
})
|
||||
|
||||
this._client.on('error', (error) => {
|
||||
this._emit('error', error)
|
||||
})
|
||||
|
||||
this._client.on('reconnecting', () => {
|
||||
this._emit('reconnecting')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建 Redis 连接
|
||||
* @en Create Redis connection
|
||||
*/
|
||||
export function createRedisConnection(config: RedisConnectionConfig): RedisConnection {
|
||||
return new RedisConnection(config)
|
||||
}
|
||||
29
packages/framework/database-drivers/src/drivers/index.ts
Normal file
29
packages/framework/database-drivers/src/drivers/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @zh 数据库驱动导出
|
||||
* @en Database drivers export
|
||||
*/
|
||||
|
||||
export {
|
||||
MongoConnection,
|
||||
createMongoConnection,
|
||||
type IMongoConnection
|
||||
} from './MongoConnection.js'
|
||||
|
||||
export {
|
||||
RedisConnection,
|
||||
createRedisConnection,
|
||||
type IRedisConnection
|
||||
} from './RedisConnection.js'
|
||||
|
||||
// Re-export interfaces
|
||||
export type {
|
||||
IMongoCollection,
|
||||
IMongoDatabase,
|
||||
InsertOneResult,
|
||||
InsertManyResult,
|
||||
UpdateResult,
|
||||
DeleteResult,
|
||||
FindOptions,
|
||||
FindOneAndUpdateOptions,
|
||||
IndexOptions
|
||||
} from '../interfaces/IMongoCollection.js'
|
||||
117
packages/framework/database-drivers/src/index.ts
Normal file
117
packages/framework/database-drivers/src/index.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* @zh @esengine/database-drivers 数据库连接驱动
|
||||
* @en @esengine/database-drivers Database Connection Drivers
|
||||
*
|
||||
* @zh 提供 MongoDB、Redis 等数据库的连接管理,支持连接池、自动重连和事件通知
|
||||
* @en Provides connection management for MongoDB, Redis, etc. with pooling, auto-reconnect, and events
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import {
|
||||
* createMongoConnection,
|
||||
* createRedisConnection,
|
||||
* MongoConnectionToken,
|
||||
* RedisConnectionToken,
|
||||
* } from '@esengine/database-drivers'
|
||||
*
|
||||
* // 创建 MongoDB 连接
|
||||
* const mongo = createMongoConnection({
|
||||
* uri: 'mongodb://localhost:27017',
|
||||
* database: 'game',
|
||||
* pool: { minSize: 5, maxSize: 20 },
|
||||
* autoReconnect: true,
|
||||
* })
|
||||
*
|
||||
* mongo.on('connected', () => console.log('MongoDB connected'))
|
||||
* mongo.on('error', (e) => console.error('Error:', e.error))
|
||||
*
|
||||
* await mongo.connect()
|
||||
*
|
||||
* // 直接使用
|
||||
* const users = mongo.collection('users')
|
||||
* await users.insertOne({ name: 'test' })
|
||||
*
|
||||
* // 或注册到服务容器供其他模块使用
|
||||
* services.register(MongoConnectionToken, mongo)
|
||||
*
|
||||
* // 创建 Redis 连接
|
||||
* const redis = createRedisConnection({
|
||||
* host: 'localhost',
|
||||
* port: 6379,
|
||||
* keyPrefix: 'game:',
|
||||
* })
|
||||
*
|
||||
* await redis.connect()
|
||||
* await redis.set('session:123', 'data', 3600)
|
||||
*
|
||||
* // 断开连接
|
||||
* await mongo.disconnect()
|
||||
* await redis.disconnect()
|
||||
* ```
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
// Types | 类型
|
||||
// =============================================================================
|
||||
|
||||
export type {
|
||||
ConnectionState,
|
||||
IConnection,
|
||||
IEventableConnection,
|
||||
ConnectionEventType,
|
||||
ConnectionEventListener,
|
||||
ConnectionEvent,
|
||||
PoolConfig,
|
||||
MongoConnectionConfig,
|
||||
RedisConnectionConfig,
|
||||
DatabaseErrorCode
|
||||
} from './types.js'
|
||||
|
||||
export {
|
||||
DatabaseError,
|
||||
ConnectionError,
|
||||
DuplicateKeyError
|
||||
} from './types.js'
|
||||
|
||||
// =============================================================================
|
||||
// Drivers | 驱动
|
||||
// =============================================================================
|
||||
|
||||
export {
|
||||
MongoConnection,
|
||||
createMongoConnection,
|
||||
type IMongoConnection
|
||||
} from './drivers/index.js'
|
||||
|
||||
export {
|
||||
RedisConnection,
|
||||
createRedisConnection,
|
||||
type IRedisConnection
|
||||
} from './drivers/index.js'
|
||||
|
||||
// =============================================================================
|
||||
// Interfaces | 接口
|
||||
// =============================================================================
|
||||
|
||||
export type {
|
||||
IMongoCollection,
|
||||
IMongoDatabase,
|
||||
InsertOneResult,
|
||||
InsertManyResult,
|
||||
UpdateResult,
|
||||
DeleteResult,
|
||||
FindOptions,
|
||||
FindOneAndUpdateOptions,
|
||||
IndexOptions
|
||||
} from './drivers/index.js'
|
||||
|
||||
// =============================================================================
|
||||
// Tokens | 服务令牌
|
||||
// =============================================================================
|
||||
|
||||
export {
|
||||
MongoConnectionToken,
|
||||
RedisConnectionToken,
|
||||
createServiceToken,
|
||||
type ServiceToken
|
||||
} from './tokens.js'
|
||||
@@ -0,0 +1,237 @@
|
||||
/**
|
||||
* @zh MongoDB 集合简化接口
|
||||
* @en MongoDB collection simplified interface
|
||||
*
|
||||
* @zh 提供与 MongoDB 解耦的类型安全接口
|
||||
* @en Provides type-safe interface decoupled from MongoDB
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
// 查询结果 | Query Results
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 插入结果
|
||||
* @en Insert result
|
||||
*/
|
||||
export interface InsertOneResult {
|
||||
insertedId: unknown
|
||||
acknowledged: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 批量插入结果
|
||||
* @en Insert many result
|
||||
*/
|
||||
export interface InsertManyResult {
|
||||
insertedCount: number
|
||||
insertedIds: Record<number, unknown>
|
||||
acknowledged: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 更新结果
|
||||
* @en Update result
|
||||
*/
|
||||
export interface UpdateResult {
|
||||
matchedCount: number
|
||||
modifiedCount: number
|
||||
upsertedCount: number
|
||||
upsertedId?: unknown
|
||||
acknowledged: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 删除结果
|
||||
* @en Delete result
|
||||
*/
|
||||
export interface DeleteResult {
|
||||
deletedCount: number
|
||||
acknowledged: boolean
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 查询选项 | Query Options
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 排序方向
|
||||
* @en Sort direction
|
||||
*/
|
||||
export type SortDirection = 1 | -1 | 'asc' | 'desc'
|
||||
|
||||
/**
|
||||
* @zh 排序定义
|
||||
* @en Sort definition
|
||||
*/
|
||||
export type Sort = Record<string, SortDirection>
|
||||
|
||||
/**
|
||||
* @zh 查找选项
|
||||
* @en Find options
|
||||
*/
|
||||
export interface FindOptions {
|
||||
sort?: Sort
|
||||
limit?: number
|
||||
skip?: number
|
||||
projection?: Record<string, 0 | 1>
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 查找并更新选项
|
||||
* @en Find and update options
|
||||
*/
|
||||
export interface FindOneAndUpdateOptions {
|
||||
returnDocument?: 'before' | 'after'
|
||||
upsert?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 索引选项
|
||||
* @en Index options
|
||||
*/
|
||||
export interface IndexOptions {
|
||||
unique?: boolean
|
||||
sparse?: boolean
|
||||
expireAfterSeconds?: number
|
||||
name?: string
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 集合接口 | Collection Interface
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh MongoDB 集合接口
|
||||
* @en MongoDB collection interface
|
||||
*
|
||||
* @zh 简化的集合操作接口,与 MongoDB 原生类型解耦
|
||||
* @en Simplified collection interface, decoupled from MongoDB native types
|
||||
*/
|
||||
export interface IMongoCollection<T extends object> {
|
||||
/**
|
||||
* @zh 集合名称
|
||||
* @en Collection name
|
||||
*/
|
||||
readonly name: string
|
||||
|
||||
// =========================================================================
|
||||
// 查询 | Query
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 查找单条记录
|
||||
* @en Find one document
|
||||
*/
|
||||
findOne(filter: object, options?: FindOptions): Promise<T | null>
|
||||
|
||||
/**
|
||||
* @zh 查找多条记录
|
||||
* @en Find documents
|
||||
*/
|
||||
find(filter: object, options?: FindOptions): Promise<T[]>
|
||||
|
||||
/**
|
||||
* @zh 统计记录数
|
||||
* @en Count documents
|
||||
*/
|
||||
countDocuments(filter?: object): Promise<number>
|
||||
|
||||
// =========================================================================
|
||||
// 创建 | Create
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 插入单条记录
|
||||
* @en Insert one document
|
||||
*/
|
||||
insertOne(doc: T): Promise<InsertOneResult>
|
||||
|
||||
/**
|
||||
* @zh 批量插入
|
||||
* @en Insert many documents
|
||||
*/
|
||||
insertMany(docs: T[]): Promise<InsertManyResult>
|
||||
|
||||
// =========================================================================
|
||||
// 更新 | Update
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 更新单条记录
|
||||
* @en Update one document
|
||||
*/
|
||||
updateOne(filter: object, update: object): Promise<UpdateResult>
|
||||
|
||||
/**
|
||||
* @zh 批量更新
|
||||
* @en Update many documents
|
||||
*/
|
||||
updateMany(filter: object, update: object): Promise<UpdateResult>
|
||||
|
||||
/**
|
||||
* @zh 查找并更新
|
||||
* @en Find one and update
|
||||
*/
|
||||
findOneAndUpdate(
|
||||
filter: object,
|
||||
update: object,
|
||||
options?: FindOneAndUpdateOptions
|
||||
): Promise<T | null>
|
||||
|
||||
// =========================================================================
|
||||
// 删除 | Delete
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 删除单条记录
|
||||
* @en Delete one document
|
||||
*/
|
||||
deleteOne(filter: object): Promise<DeleteResult>
|
||||
|
||||
/**
|
||||
* @zh 批量删除
|
||||
* @en Delete many documents
|
||||
*/
|
||||
deleteMany(filter: object): Promise<DeleteResult>
|
||||
|
||||
// =========================================================================
|
||||
// 索引 | Index
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 创建索引
|
||||
* @en Create index
|
||||
*/
|
||||
createIndex(spec: Record<string, 1 | -1>, options?: IndexOptions): Promise<string>
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh MongoDB 数据库接口
|
||||
* @en MongoDB database interface
|
||||
*/
|
||||
export interface IMongoDatabase {
|
||||
/**
|
||||
* @zh 数据库名称
|
||||
* @en Database name
|
||||
*/
|
||||
readonly name: string
|
||||
|
||||
/**
|
||||
* @zh 获取集合
|
||||
* @en Get collection
|
||||
*/
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T>
|
||||
|
||||
/**
|
||||
* @zh 列出所有集合
|
||||
* @en List all collections
|
||||
*/
|
||||
listCollections(): Promise<string[]>
|
||||
|
||||
/**
|
||||
* @zh 删除集合
|
||||
* @en Drop collection
|
||||
*/
|
||||
dropCollection(name: string): Promise<boolean>
|
||||
}
|
||||
56
packages/framework/database-drivers/src/tokens.ts
Normal file
56
packages/framework/database-drivers/src/tokens.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @zh 数据库驱动服务令牌
|
||||
* @en Database driver service tokens
|
||||
*
|
||||
* @zh 用于依赖注入的服务令牌定义
|
||||
* @en Service token definitions for dependency injection
|
||||
*/
|
||||
|
||||
import type { IMongoConnection } from './drivers/MongoConnection.js'
|
||||
import type { IRedisConnection } from './drivers/RedisConnection.js'
|
||||
|
||||
// =============================================================================
|
||||
// 服务令牌类型 | Service Token Type
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 服务令牌
|
||||
* @en Service token
|
||||
*/
|
||||
export interface ServiceToken<T> {
|
||||
readonly id: string
|
||||
readonly _type?: T
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建服务令牌
|
||||
* @en Create service token
|
||||
*/
|
||||
export function createServiceToken<T>(id: string): ServiceToken<T> {
|
||||
return { id }
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 连接令牌 | Connection Tokens
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh MongoDB 连接令牌
|
||||
* @en MongoDB connection token
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 注册
|
||||
* services.register(MongoConnectionToken, mongoConnection)
|
||||
*
|
||||
* // 获取
|
||||
* const mongo = services.get(MongoConnectionToken)
|
||||
* ```
|
||||
*/
|
||||
export const MongoConnectionToken = createServiceToken<IMongoConnection>('database:mongo')
|
||||
|
||||
/**
|
||||
* @zh Redis 连接令牌
|
||||
* @en Redis connection token
|
||||
*/
|
||||
export const RedisConnectionToken = createServiceToken<IRedisConnection>('database:redis')
|
||||
338
packages/framework/database-drivers/src/types.ts
Normal file
338
packages/framework/database-drivers/src/types.ts
Normal file
@@ -0,0 +1,338 @@
|
||||
/**
|
||||
* @zh 数据库驱动核心类型定义
|
||||
* @en Database driver core type definitions
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
// 连接状态 | Connection State
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 连接状态
|
||||
* @en Connection state
|
||||
*/
|
||||
export type ConnectionState =
|
||||
| 'disconnected' // 未连接 | Not connected
|
||||
| 'connecting' // 连接中 | Connecting
|
||||
| 'connected' // 已连接 | Connected
|
||||
| 'disconnecting' // 断开中 | Disconnecting
|
||||
| 'error' // 错误 | Error
|
||||
|
||||
// =============================================================================
|
||||
// 基础连接接口 | Base Connection Interface
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 数据库连接基础接口
|
||||
* @en Base database connection interface
|
||||
*/
|
||||
export interface IConnection {
|
||||
/**
|
||||
* @zh 连接唯一标识
|
||||
* @en Connection unique identifier
|
||||
*/
|
||||
readonly id: string
|
||||
|
||||
/**
|
||||
* @zh 当前连接状态
|
||||
* @en Current connection state
|
||||
*/
|
||||
readonly state: ConnectionState
|
||||
|
||||
/**
|
||||
* @zh 建立连接
|
||||
* @en Establish connection
|
||||
*/
|
||||
connect(): Promise<void>
|
||||
|
||||
/**
|
||||
* @zh 断开连接
|
||||
* @en Disconnect
|
||||
*/
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/**
|
||||
* @zh 检查是否已连接
|
||||
* @en Check if connected
|
||||
*/
|
||||
isConnected(): boolean
|
||||
|
||||
/**
|
||||
* @zh 健康检查
|
||||
* @en Health check
|
||||
*/
|
||||
ping(): Promise<boolean>
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 连接事件 | Connection Events
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 连接事件类型
|
||||
* @en Connection event types
|
||||
*/
|
||||
export type ConnectionEventType =
|
||||
| 'connected'
|
||||
| 'disconnected'
|
||||
| 'error'
|
||||
| 'reconnecting'
|
||||
| 'reconnected'
|
||||
|
||||
/**
|
||||
* @zh 连接事件监听器
|
||||
* @en Connection event listener
|
||||
*/
|
||||
export type ConnectionEventListener = (event: ConnectionEvent) => void
|
||||
|
||||
/**
|
||||
* @zh 连接事件
|
||||
* @en Connection event
|
||||
*/
|
||||
export interface ConnectionEvent {
|
||||
/**
|
||||
* @zh 事件类型
|
||||
* @en Event type
|
||||
*/
|
||||
type: ConnectionEventType
|
||||
|
||||
/**
|
||||
* @zh 连接 ID
|
||||
* @en Connection ID
|
||||
*/
|
||||
connectionId: string
|
||||
|
||||
/**
|
||||
* @zh 时间戳
|
||||
* @en Timestamp
|
||||
*/
|
||||
timestamp: number
|
||||
|
||||
/**
|
||||
* @zh 错误信息(如果有)
|
||||
* @en Error message (if any)
|
||||
*/
|
||||
error?: Error
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 可监听事件的连接接口
|
||||
* @en Connection interface with event support
|
||||
*/
|
||||
export interface IEventableConnection extends IConnection {
|
||||
/**
|
||||
* @zh 添加事件监听
|
||||
* @en Add event listener
|
||||
*/
|
||||
on(event: ConnectionEventType, listener: ConnectionEventListener): void
|
||||
|
||||
/**
|
||||
* @zh 移除事件监听
|
||||
* @en Remove event listener
|
||||
*/
|
||||
off(event: ConnectionEventType, listener: ConnectionEventListener): void
|
||||
|
||||
/**
|
||||
* @zh 一次性事件监听
|
||||
* @en One-time event listener
|
||||
*/
|
||||
once(event: ConnectionEventType, listener: ConnectionEventListener): void
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 连接池配置 | Connection Pool Configuration
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 连接池配置
|
||||
* @en Connection pool configuration
|
||||
*/
|
||||
export interface PoolConfig {
|
||||
/**
|
||||
* @zh 最小连接数
|
||||
* @en Minimum connections
|
||||
*/
|
||||
minSize?: number
|
||||
|
||||
/**
|
||||
* @zh 最大连接数
|
||||
* @en Maximum connections
|
||||
*/
|
||||
maxSize?: number
|
||||
|
||||
/**
|
||||
* @zh 获取连接超时时间(毫秒)
|
||||
* @en Acquire connection timeout in milliseconds
|
||||
*/
|
||||
acquireTimeout?: number
|
||||
|
||||
/**
|
||||
* @zh 空闲连接超时时间(毫秒)
|
||||
* @en Idle connection timeout in milliseconds
|
||||
*/
|
||||
idleTimeout?: number
|
||||
|
||||
/**
|
||||
* @zh 连接最大生存时间(毫秒)
|
||||
* @en Maximum connection lifetime in milliseconds
|
||||
*/
|
||||
maxLifetime?: number
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 数据库特定配置 | Database Specific Configuration
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh MongoDB 连接配置
|
||||
* @en MongoDB connection configuration
|
||||
*/
|
||||
export interface MongoConnectionConfig {
|
||||
/**
|
||||
* @zh 连接字符串
|
||||
* @en Connection string
|
||||
*
|
||||
* @example "mongodb://localhost:27017"
|
||||
* @example "mongodb+srv://user:pass@cluster.mongodb.net"
|
||||
*/
|
||||
uri: string
|
||||
|
||||
/**
|
||||
* @zh 数据库名称
|
||||
* @en Database name
|
||||
*/
|
||||
database: string
|
||||
|
||||
/**
|
||||
* @zh 连接池配置
|
||||
* @en Pool configuration
|
||||
*/
|
||||
pool?: PoolConfig
|
||||
|
||||
/**
|
||||
* @zh 自动重连
|
||||
* @en Auto reconnect
|
||||
*/
|
||||
autoReconnect?: boolean
|
||||
|
||||
/**
|
||||
* @zh 重连间隔(毫秒)
|
||||
* @en Reconnect interval in milliseconds
|
||||
*/
|
||||
reconnectInterval?: number
|
||||
|
||||
/**
|
||||
* @zh 最大重连次数
|
||||
* @en Maximum reconnect attempts
|
||||
*/
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh Redis 连接配置
|
||||
* @en Redis connection configuration
|
||||
*/
|
||||
export interface RedisConnectionConfig {
|
||||
/**
|
||||
* @zh 主机地址
|
||||
* @en Host address
|
||||
*/
|
||||
host?: string
|
||||
|
||||
/**
|
||||
* @zh 端口
|
||||
* @en Port
|
||||
*/
|
||||
port?: number
|
||||
|
||||
/**
|
||||
* @zh 密码
|
||||
* @en Password
|
||||
*/
|
||||
password?: string
|
||||
|
||||
/**
|
||||
* @zh 数据库索引
|
||||
* @en Database index
|
||||
*/
|
||||
db?: number
|
||||
|
||||
/**
|
||||
* @zh 连接字符串(优先于其他配置)
|
||||
* @en Connection URL (takes precedence over other options)
|
||||
*/
|
||||
url?: string
|
||||
|
||||
/**
|
||||
* @zh 键前缀
|
||||
* @en Key prefix
|
||||
*/
|
||||
keyPrefix?: string
|
||||
|
||||
/**
|
||||
* @zh 自动重连
|
||||
* @en Auto reconnect
|
||||
*/
|
||||
autoReconnect?: boolean
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 错误类型 | Error Types
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 数据库错误代码
|
||||
* @en Database error codes
|
||||
*/
|
||||
export type DatabaseErrorCode =
|
||||
| 'CONNECTION_FAILED'
|
||||
| 'CONNECTION_TIMEOUT'
|
||||
| 'CONNECTION_CLOSED'
|
||||
| 'AUTHENTICATION_FAILED'
|
||||
| 'POOL_EXHAUSTED'
|
||||
| 'QUERY_FAILED'
|
||||
| 'DUPLICATE_KEY'
|
||||
| 'NOT_FOUND'
|
||||
| 'VALIDATION_ERROR'
|
||||
| 'UNKNOWN'
|
||||
|
||||
/**
|
||||
* @zh 数据库错误
|
||||
* @en Database error
|
||||
*/
|
||||
export class DatabaseError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly code: DatabaseErrorCode,
|
||||
public readonly cause?: Error
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'DatabaseError'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 连接错误
|
||||
* @en Connection error
|
||||
*/
|
||||
export class ConnectionError extends DatabaseError {
|
||||
constructor(message: string, code: DatabaseErrorCode = 'CONNECTION_FAILED', cause?: Error) {
|
||||
super(message, code, cause)
|
||||
this.name = 'ConnectionError'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 重复键错误
|
||||
* @en Duplicate key error
|
||||
*/
|
||||
export class DuplicateKeyError extends DatabaseError {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly key: string,
|
||||
cause?: Error
|
||||
) {
|
||||
super(message, 'DUPLICATE_KEY', cause)
|
||||
this.name = 'DuplicateKeyError'
|
||||
}
|
||||
}
|
||||
10
packages/framework/database-drivers/tsconfig.json
Normal file
10
packages/framework/database-drivers/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"declarationDir": "./dist"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
11
packages/framework/database-drivers/tsup.config.ts
Normal file
11
packages/framework/database-drivers/tsup.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineConfig } from 'tsup';
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.ts'],
|
||||
format: ['esm'],
|
||||
dts: true,
|
||||
clean: true,
|
||||
sourcemap: true,
|
||||
external: ['mongodb', 'ioredis'],
|
||||
treeshake: true,
|
||||
});
|
||||
65
packages/framework/database/CHANGELOG.md
Normal file
65
packages/framework/database/CHANGELOG.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# @esengine/database
|
||||
|
||||
## 1.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#412](https://github.com/esengine/esengine/pull/412) [`85171a0`](https://github.com/esengine/esengine/commit/85171a0a5c073ef7883705ee4daaca8bb0218f20) Thanks [@esengine](https://github.com/esengine)! - fix: include dist directory in npm package
|
||||
|
||||
Previous 1.1.0 release was missing the compiled dist directory.
|
||||
|
||||
- Updated dependencies [[`85171a0`](https://github.com/esengine/esengine/commit/85171a0a5c073ef7883705ee4daaca8bb0218f20)]:
|
||||
- @esengine/database-drivers@1.1.1
|
||||
|
||||
## 1.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#410](https://github.com/esengine/esengine/pull/410) [`71022ab`](https://github.com/esengine/esengine/commit/71022abc99ad4a1b349f19f4ccf1e0a2a0923dfa) Thanks [@esengine](https://github.com/esengine)! - feat: add database layer architecture
|
||||
|
||||
Added new database packages with layered architecture:
|
||||
|
||||
**@esengine/database-drivers (Layer 1)**
|
||||
- MongoDB connection with pool management, auto-reconnect, events
|
||||
- Redis connection with auto-reconnect, key prefix
|
||||
- Type-safe `IMongoCollection<T>` interface decoupled from mongodb types
|
||||
- Service tokens for dependency injection (`MongoConnectionToken`, `RedisConnectionToken`)
|
||||
|
||||
**@esengine/database (Layer 2)**
|
||||
- Generic `Repository<T>` with CRUD, pagination, soft delete
|
||||
- `UserRepository` with registration, authentication, role management
|
||||
- Password hashing utilities using scrypt
|
||||
- Query operators: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$in`, `$nin`, `$like`, `$regex`
|
||||
|
||||
**@esengine/transaction**
|
||||
- Refactored `MongoStorage` to use shared connection from `@esengine/database-drivers`
|
||||
- Removed factory pattern in favor of shared connection (breaking change)
|
||||
- Simplified API: `createMongoStorage(connection, options?)`
|
||||
|
||||
Example usage:
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { UserRepository } from '@esengine/database';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// Create shared connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// Use for database operations
|
||||
const userRepo = new UserRepository(mongo);
|
||||
await userRepo.register({ username: 'john', password: '123456' });
|
||||
|
||||
// Use for transactions (same connection)
|
||||
const storage = createMongoStorage(mongo);
|
||||
const txManager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`71022ab`](https://github.com/esengine/esengine/commit/71022abc99ad4a1b349f19f4ccf1e0a2a0923dfa)]:
|
||||
- @esengine/database-drivers@1.1.0
|
||||
23
packages/framework/database/module.json
Normal file
23
packages/framework/database/module.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"id": "database",
|
||||
"name": "@esengine/database",
|
||||
"globalKey": "database",
|
||||
"displayName": "Database",
|
||||
"description": "数据库 CRUD 操作和仓库模式,支持用户管理、通用数据存储 | Database CRUD operations and repository pattern with user management and generic data storage",
|
||||
"version": "1.0.0",
|
||||
"category": "Infrastructure",
|
||||
"icon": "Database",
|
||||
"tags": ["database", "crud", "repository", "user"],
|
||||
"isCore": false,
|
||||
"defaultEnabled": true,
|
||||
"isEngineModule": false,
|
||||
"canContainContent": false,
|
||||
"platforms": ["server"],
|
||||
"dependencies": ["database-drivers"],
|
||||
"exports": {
|
||||
"components": [],
|
||||
"systems": []
|
||||
},
|
||||
"requiresWasm": false,
|
||||
"outputPath": "dist/index.js"
|
||||
}
|
||||
37
packages/framework/database/package.json
Normal file
37
packages/framework/database/package.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@esengine/database",
|
||||
"version": "1.1.1",
|
||||
"description": "Database CRUD operations and repositories for ESEngine | ESEngine 数据库 CRUD 操作和仓库",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"module.json"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"build:watch": "tsup --watch",
|
||||
"type-check": "tsc --noEmit",
|
||||
"clean": "rimraf dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"@esengine/database-drivers": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"tsup": "^8.0.0",
|
||||
"typescript": "^5.8.0",
|
||||
"rimraf": "^5.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
313
packages/framework/database/src/Repository.ts
Normal file
313
packages/framework/database/src/Repository.ts
Normal file
@@ -0,0 +1,313 @@
|
||||
/**
|
||||
* @zh MongoDB 仓库实现
|
||||
* @en MongoDB repository implementation
|
||||
*
|
||||
* @zh 基于 MongoDB 的通用仓库,支持 CRUD、分页、软删除
|
||||
* @en Generic MongoDB repository with CRUD, pagination, and soft delete support
|
||||
*/
|
||||
|
||||
import { randomUUID } from 'crypto'
|
||||
import type { IMongoConnection, IMongoCollection } from '@esengine/database-drivers'
|
||||
import type {
|
||||
BaseEntity,
|
||||
IRepository,
|
||||
PaginatedResult,
|
||||
PaginationParams,
|
||||
QueryOptions,
|
||||
WhereCondition
|
||||
} from './types.js'
|
||||
|
||||
/**
|
||||
* @zh MongoDB 仓库基类
|
||||
* @en MongoDB repository base class
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* interface Player extends BaseEntity {
|
||||
* name: string
|
||||
* score: number
|
||||
* }
|
||||
*
|
||||
* class PlayerRepository extends Repository<Player> {
|
||||
* constructor(connection: IMongoConnection) {
|
||||
* super(connection, 'players')
|
||||
* }
|
||||
*
|
||||
* async findTopPlayers(limit: number): Promise<Player[]> {
|
||||
* return this.findMany({
|
||||
* sort: { score: 'desc' },
|
||||
* limit,
|
||||
* })
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class Repository<T extends BaseEntity> implements IRepository<T> {
|
||||
protected readonly _collection: IMongoCollection<T>
|
||||
|
||||
constructor(
|
||||
protected readonly connection: IMongoConnection,
|
||||
public readonly collectionName: string,
|
||||
protected readonly enableSoftDelete: boolean = false
|
||||
) {
|
||||
this._collection = connection.collection<T>(collectionName)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 查询 | Query
|
||||
// =========================================================================
|
||||
|
||||
async findById(id: string): Promise<T | null> {
|
||||
const filter = this._buildFilter({ where: { id } as WhereCondition<T> })
|
||||
return this._collection.findOne(filter)
|
||||
}
|
||||
|
||||
async findOne(options?: QueryOptions<T>): Promise<T | null> {
|
||||
const filter = this._buildFilter(options)
|
||||
const sort = this._buildSort(options)
|
||||
return this._collection.findOne(filter, { sort })
|
||||
}
|
||||
|
||||
async findMany(options?: QueryOptions<T>): Promise<T[]> {
|
||||
const filter = this._buildFilter(options)
|
||||
const sort = this._buildSort(options)
|
||||
return this._collection.find(filter, {
|
||||
sort,
|
||||
skip: options?.offset,
|
||||
limit: options?.limit
|
||||
})
|
||||
}
|
||||
|
||||
async findPaginated(
|
||||
pagination: PaginationParams,
|
||||
options?: Omit<QueryOptions<T>, 'limit' | 'offset'>
|
||||
): Promise<PaginatedResult<T>> {
|
||||
const { page, pageSize } = pagination
|
||||
const offset = (page - 1) * pageSize
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.findMany({ ...options, limit: pageSize, offset }),
|
||||
this.count(options)
|
||||
])
|
||||
|
||||
const totalPages = Math.ceil(total / pageSize)
|
||||
|
||||
return {
|
||||
data,
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1
|
||||
}
|
||||
}
|
||||
|
||||
async count(options?: QueryOptions<T>): Promise<number> {
|
||||
const filter = this._buildFilter(options)
|
||||
return this._collection.countDocuments(filter)
|
||||
}
|
||||
|
||||
async exists(options: QueryOptions<T>): Promise<boolean> {
|
||||
const count = await this.count({ ...options, limit: 1 })
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 创建 | Create
|
||||
// =========================================================================
|
||||
|
||||
async create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'> & { id?: string }): Promise<T> {
|
||||
const now = new Date()
|
||||
const entity = {
|
||||
...data,
|
||||
id: data.id || randomUUID(),
|
||||
createdAt: now,
|
||||
updatedAt: now
|
||||
} as T
|
||||
|
||||
await this._collection.insertOne(entity)
|
||||
return entity
|
||||
}
|
||||
|
||||
async createMany(
|
||||
data: Array<Omit<T, 'id' | 'createdAt' | 'updatedAt'> & { id?: string }>
|
||||
): Promise<T[]> {
|
||||
if (data.length === 0) return []
|
||||
|
||||
const now = new Date()
|
||||
const entities = data.map(item => ({
|
||||
...item,
|
||||
id: item.id || randomUUID(),
|
||||
createdAt: now,
|
||||
updatedAt: now
|
||||
})) as T[]
|
||||
|
||||
await this._collection.insertMany(entities)
|
||||
return entities
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 更新 | Update
|
||||
// =========================================================================
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
data: Partial<Omit<T, 'id' | 'createdAt' | 'updatedAt'>>
|
||||
): Promise<T | null> {
|
||||
const filter = this._buildFilter({ where: { id } as WhereCondition<T> })
|
||||
return this._collection.findOneAndUpdate(
|
||||
filter,
|
||||
{ $set: { ...data, updatedAt: new Date() } },
|
||||
{ returnDocument: 'after' }
|
||||
)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 删除 | Delete
|
||||
// =========================================================================
|
||||
|
||||
async delete(id: string): Promise<boolean> {
|
||||
if (this.enableSoftDelete) {
|
||||
const result = await this._collection.updateOne(
|
||||
{ id },
|
||||
{ $set: { deletedAt: new Date(), updatedAt: new Date() } }
|
||||
)
|
||||
return result.modifiedCount > 0
|
||||
}
|
||||
|
||||
const result = await this._collection.deleteOne({ id })
|
||||
return result.deletedCount > 0
|
||||
}
|
||||
|
||||
async deleteMany(options: QueryOptions<T>): Promise<number> {
|
||||
const filter = this._buildFilter(options)
|
||||
|
||||
if (this.enableSoftDelete) {
|
||||
const result = await this._collection.updateMany(filter, {
|
||||
$set: { deletedAt: new Date(), updatedAt: new Date() }
|
||||
})
|
||||
return result.modifiedCount
|
||||
}
|
||||
|
||||
const result = await this._collection.deleteMany(filter)
|
||||
return result.deletedCount
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 软删除恢复 | Soft Delete Recovery
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 恢复软删除的记录
|
||||
* @en Restore soft deleted record
|
||||
*/
|
||||
async restore(id: string): Promise<T | null> {
|
||||
if (!this.enableSoftDelete) {
|
||||
throw new Error('Soft delete is not enabled for this repository')
|
||||
}
|
||||
|
||||
return this._collection.findOneAndUpdate(
|
||||
{ id, deletedAt: { $ne: null } },
|
||||
{ $set: { deletedAt: null, updatedAt: new Date() } },
|
||||
{ returnDocument: 'after' }
|
||||
)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 内部方法 | Internal Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 构建过滤条件
|
||||
* @en Build filter
|
||||
*/
|
||||
protected _buildFilter(options?: QueryOptions<T>): object {
|
||||
const filter: Record<string, unknown> = {}
|
||||
|
||||
if (this.enableSoftDelete && !options?.includeSoftDeleted) {
|
||||
filter['deletedAt'] = null
|
||||
}
|
||||
|
||||
if (!options?.where) {
|
||||
return filter
|
||||
}
|
||||
|
||||
return { ...filter, ...this._convertWhere(options.where) }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 转换 where 条件
|
||||
* @en Convert where condition
|
||||
*/
|
||||
protected _convertWhere(where: WhereCondition<T>): object {
|
||||
const result: Record<string, unknown> = {}
|
||||
|
||||
for (const [key, value] of Object.entries(where)) {
|
||||
if (key === '$or' && Array.isArray(value)) {
|
||||
result['$or'] = value.map(v => this._convertWhere(v as WhereCondition<T>))
|
||||
continue
|
||||
}
|
||||
|
||||
if (key === '$and' && Array.isArray(value)) {
|
||||
result['$and'] = value.map(v => this._convertWhere(v as WhereCondition<T>))
|
||||
continue
|
||||
}
|
||||
|
||||
if (value === undefined) continue
|
||||
|
||||
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
||||
const ops = value as Record<string, unknown>
|
||||
const mongoOps: Record<string, unknown> = {}
|
||||
|
||||
if ('$eq' in ops) mongoOps['$eq'] = ops.$eq
|
||||
if ('$ne' in ops) mongoOps['$ne'] = ops.$ne
|
||||
if ('$gt' in ops) mongoOps['$gt'] = ops.$gt
|
||||
if ('$gte' in ops) mongoOps['$gte'] = ops.$gte
|
||||
if ('$lt' in ops) mongoOps['$lt'] = ops.$lt
|
||||
if ('$lte' in ops) mongoOps['$lte'] = ops.$lte
|
||||
if ('$in' in ops) mongoOps['$in'] = ops.$in
|
||||
if ('$nin' in ops) mongoOps['$nin'] = ops.$nin
|
||||
if ('$like' in ops) {
|
||||
const pattern = (ops.$like as string).replace(/%/g, '.*').replace(/_/g, '.')
|
||||
mongoOps['$regex'] = new RegExp(`^${pattern}$`, 'i')
|
||||
}
|
||||
if ('$regex' in ops) {
|
||||
mongoOps['$regex'] = new RegExp(ops.$regex as string, 'i')
|
||||
}
|
||||
|
||||
result[key] = Object.keys(mongoOps).length > 0 ? mongoOps : value
|
||||
} else {
|
||||
result[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 构建排序条件
|
||||
* @en Build sort condition
|
||||
*/
|
||||
protected _buildSort(options?: QueryOptions<T>): Record<string, 1 | -1> | undefined {
|
||||
if (!options?.sort) return undefined
|
||||
|
||||
const result: Record<string, 1 | -1> = {}
|
||||
for (const [key, direction] of Object.entries(options.sort)) {
|
||||
result[key] = direction === 'desc' ? -1 : 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建仓库实例
|
||||
* @en Create repository instance
|
||||
*/
|
||||
export function createRepository<T extends BaseEntity>(
|
||||
connection: IMongoConnection,
|
||||
collectionName: string,
|
||||
enableSoftDelete = false
|
||||
): Repository<T> {
|
||||
return new Repository<T>(connection, collectionName, enableSoftDelete)
|
||||
}
|
||||
335
packages/framework/database/src/UserRepository.ts
Normal file
335
packages/framework/database/src/UserRepository.ts
Normal file
@@ -0,0 +1,335 @@
|
||||
/**
|
||||
* @zh 用户仓库
|
||||
* @en User repository
|
||||
*
|
||||
* @zh 提供用户管理的常用方法,包括注册、登录、角色管理等
|
||||
* @en Provides common user management methods including registration, login, role management
|
||||
*/
|
||||
|
||||
import type { IMongoConnection } from '@esengine/database-drivers'
|
||||
import { Repository } from './Repository.js'
|
||||
import { hashPassword, verifyPassword } from './password.js'
|
||||
import type { UserEntity } from './types.js'
|
||||
|
||||
/**
|
||||
* @zh 创建用户参数
|
||||
* @en Create user parameters
|
||||
*/
|
||||
export interface CreateUserParams {
|
||||
/**
|
||||
* @zh 用户名
|
||||
* @en Username
|
||||
*/
|
||||
username: string
|
||||
|
||||
/**
|
||||
* @zh 明文密码
|
||||
* @en Plain text password
|
||||
*/
|
||||
password: string
|
||||
|
||||
/**
|
||||
* @zh 邮箱
|
||||
* @en Email
|
||||
*/
|
||||
email?: string
|
||||
|
||||
/**
|
||||
* @zh 角色列表
|
||||
* @en Role list
|
||||
*/
|
||||
roles?: string[]
|
||||
|
||||
/**
|
||||
* @zh 额外数据
|
||||
* @en Additional metadata
|
||||
*/
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 用户信息(不含密码)
|
||||
* @en User info (without password)
|
||||
*/
|
||||
export type SafeUser = Omit<UserEntity, 'passwordHash'>
|
||||
|
||||
/**
|
||||
* @zh 用户仓库
|
||||
* @en User repository
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const mongo = createMongoConnection({ uri: '...', database: 'game' })
|
||||
* await mongo.connect()
|
||||
*
|
||||
* const userRepo = new UserRepository(mongo)
|
||||
*
|
||||
* // 注册用户
|
||||
* const user = await userRepo.register({
|
||||
* username: 'player1',
|
||||
* password: 'securePassword123',
|
||||
* email: 'player1@example.com',
|
||||
* })
|
||||
*
|
||||
* // 验证登录
|
||||
* const result = await userRepo.authenticate('player1', 'securePassword123')
|
||||
* if (result) {
|
||||
* console.log('登录成功:', result.username)
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class UserRepository extends Repository<UserEntity> {
|
||||
constructor(connection: IMongoConnection, collectionName = 'users') {
|
||||
super(connection, collectionName, true)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 查询 | Query
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 根据用户名查找用户
|
||||
* @en Find user by username
|
||||
*/
|
||||
async findByUsername(username: string): Promise<UserEntity | null> {
|
||||
return this.findOne({ where: { username } })
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 根据邮箱查找用户
|
||||
* @en Find user by email
|
||||
*/
|
||||
async findByEmail(email: string): Promise<UserEntity | null> {
|
||||
return this.findOne({ where: { email } })
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查用户名是否存在
|
||||
* @en Check if username exists
|
||||
*/
|
||||
async usernameExists(username: string): Promise<boolean> {
|
||||
return this.exists({ where: { username } })
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查邮箱是否存在
|
||||
* @en Check if email exists
|
||||
*/
|
||||
async emailExists(email: string): Promise<boolean> {
|
||||
return this.exists({ where: { email } })
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 注册与认证 | Registration & Authentication
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 注册新用户
|
||||
* @en Register new user
|
||||
*
|
||||
* @param params - @zh 创建用户参数 @en Create user parameters
|
||||
* @returns @zh 创建的用户(不含密码哈希)@en Created user (without password hash)
|
||||
* @throws @zh 如果用户名已存在 @en If username already exists
|
||||
*/
|
||||
async register(params: CreateUserParams): Promise<SafeUser> {
|
||||
const { username, password, email, roles, metadata } = params
|
||||
|
||||
if (await this.usernameExists(username)) {
|
||||
throw new Error('Username already exists')
|
||||
}
|
||||
|
||||
if (email && (await this.emailExists(email))) {
|
||||
throw new Error('Email already exists')
|
||||
}
|
||||
|
||||
const passwordHash = await hashPassword(password)
|
||||
|
||||
const user = await this.create({
|
||||
username,
|
||||
passwordHash,
|
||||
email,
|
||||
roles: roles ?? ['user'],
|
||||
isActive: true,
|
||||
metadata
|
||||
})
|
||||
|
||||
return this.toSafeUser(user)
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 验证用户登录
|
||||
* @en Authenticate user login
|
||||
*
|
||||
* @param username - @zh 用户名 @en Username
|
||||
* @param password - @zh 明文密码 @en Plain text password
|
||||
* @returns @zh 验证成功返回用户信息(不含密码),失败返回 null @en Returns user info on success, null on failure
|
||||
*/
|
||||
async authenticate(username: string, password: string): Promise<SafeUser | null> {
|
||||
const user = await this.findByUsername(username)
|
||||
if (!user || !user.isActive) {
|
||||
return null
|
||||
}
|
||||
|
||||
const isValid = await verifyPassword(password, user.passwordHash)
|
||||
if (!isValid) {
|
||||
return null
|
||||
}
|
||||
|
||||
await this.update(user.id, { lastLoginAt: new Date() })
|
||||
|
||||
return this.toSafeUser(user)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 密码管理 | Password Management
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 修改密码
|
||||
* @en Change password
|
||||
*
|
||||
* @param userId - @zh 用户 ID @en User ID
|
||||
* @param oldPassword - @zh 旧密码 @en Old password
|
||||
* @param newPassword - @zh 新密码 @en New password
|
||||
* @returns @zh 是否修改成功 @en Whether change was successful
|
||||
*/
|
||||
async changePassword(
|
||||
userId: string,
|
||||
oldPassword: string,
|
||||
newPassword: string
|
||||
): Promise<boolean> {
|
||||
const user = await this.findById(userId)
|
||||
if (!user) {
|
||||
return false
|
||||
}
|
||||
|
||||
const isValid = await verifyPassword(oldPassword, user.passwordHash)
|
||||
if (!isValid) {
|
||||
return false
|
||||
}
|
||||
|
||||
const newHash = await hashPassword(newPassword)
|
||||
const result = await this.update(userId, { passwordHash: newHash })
|
||||
return result !== null
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 重置密码(管理员操作)
|
||||
* @en Reset password (admin operation)
|
||||
*
|
||||
* @param userId - @zh 用户 ID @en User ID
|
||||
* @param newPassword - @zh 新密码 @en New password
|
||||
*/
|
||||
async resetPassword(userId: string, newPassword: string): Promise<boolean> {
|
||||
const user = await this.findById(userId)
|
||||
if (!user) {
|
||||
return false
|
||||
}
|
||||
|
||||
const newHash = await hashPassword(newPassword)
|
||||
const result = await this.update(userId, { passwordHash: newHash })
|
||||
return result !== null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 角色管理 | Role Management
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 添加角色
|
||||
* @en Add role to user
|
||||
*/
|
||||
async addRole(userId: string, role: string): Promise<boolean> {
|
||||
const user = await this.findById(userId)
|
||||
if (!user) {
|
||||
return false
|
||||
}
|
||||
|
||||
const roles = user.roles ?? []
|
||||
if (!roles.includes(role)) {
|
||||
roles.push(role)
|
||||
await this.update(userId, { roles })
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 移除角色
|
||||
* @en Remove role from user
|
||||
*/
|
||||
async removeRole(userId: string, role: string): Promise<boolean> {
|
||||
const user = await this.findById(userId)
|
||||
if (!user) {
|
||||
return false
|
||||
}
|
||||
|
||||
const roles = (user.roles ?? []).filter(r => r !== role)
|
||||
await this.update(userId, { roles })
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查用户是否拥有角色
|
||||
* @en Check if user has role
|
||||
*/
|
||||
async hasRole(userId: string, role: string): Promise<boolean> {
|
||||
const user = await this.findById(userId)
|
||||
return user?.roles?.includes(role) ?? false
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查用户是否拥有任一角色
|
||||
* @en Check if user has any of the roles
|
||||
*/
|
||||
async hasAnyRole(userId: string, roles: string[]): Promise<boolean> {
|
||||
const user = await this.findById(userId)
|
||||
if (!user?.roles) return false
|
||||
return roles.some(role => user.roles.includes(role))
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 状态管理 | Status Management
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 禁用用户
|
||||
* @en Deactivate user
|
||||
*/
|
||||
async deactivate(userId: string): Promise<boolean> {
|
||||
const result = await this.update(userId, { isActive: false })
|
||||
return result !== null
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 启用用户
|
||||
* @en Activate user
|
||||
*/
|
||||
async activate(userId: string): Promise<boolean> {
|
||||
const result = await this.update(userId, { isActive: true })
|
||||
return result !== null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 内部方法 | Internal Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 移除密码哈希
|
||||
* @en Remove password hash
|
||||
*/
|
||||
private toSafeUser(user: UserEntity): SafeUser {
|
||||
const { passwordHash, ...safeUser } = user
|
||||
return safeUser
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建用户仓库
|
||||
* @en Create user repository
|
||||
*/
|
||||
export function createUserRepository(
|
||||
connection: IMongoConnection,
|
||||
collectionName = 'users'
|
||||
): UserRepository {
|
||||
return new UserRepository(connection, collectionName)
|
||||
}
|
||||
152
packages/framework/database/src/index.ts
Normal file
152
packages/framework/database/src/index.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* @zh @esengine/database 数据库操作层
|
||||
* @en @esengine/database Database Operations Layer
|
||||
*
|
||||
* @zh 提供通用的数据库 CRUD 操作、仓库模式、用户管理等功能
|
||||
* @en Provides generic database CRUD operations, repository pattern, user management
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { createMongoConnection } from '@esengine/database-drivers'
|
||||
* import {
|
||||
* Repository,
|
||||
* UserRepository,
|
||||
* createUserRepository,
|
||||
* hashPassword,
|
||||
* verifyPassword,
|
||||
* } from '@esengine/database'
|
||||
*
|
||||
* // 1. 创建连接(来自 database-drivers)
|
||||
* const mongo = createMongoConnection({
|
||||
* uri: 'mongodb://localhost:27017',
|
||||
* database: 'game',
|
||||
* })
|
||||
* await mongo.connect()
|
||||
*
|
||||
* // 2. 使用用户仓库
|
||||
* const userRepo = createUserRepository(mongo)
|
||||
*
|
||||
* // 注册
|
||||
* const user = await userRepo.register({
|
||||
* username: 'player1',
|
||||
* password: 'securePassword123',
|
||||
* })
|
||||
*
|
||||
* // 登录
|
||||
* const authUser = await userRepo.authenticate('player1', 'securePassword123')
|
||||
*
|
||||
* // 3. 自定义仓库
|
||||
* interface Player extends BaseEntity {
|
||||
* name: string
|
||||
* score: number
|
||||
* level: number
|
||||
* }
|
||||
*
|
||||
* class PlayerRepository extends Repository<Player> {
|
||||
* constructor(connection: IMongoConnection) {
|
||||
* super(connection, 'players')
|
||||
* }
|
||||
*
|
||||
* async findTopPlayers(limit = 10): Promise<Player[]> {
|
||||
* return this.findMany({
|
||||
* sort: { score: 'desc' },
|
||||
* limit,
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* async addScore(playerId: string, points: number): Promise<Player | null> {
|
||||
* const player = await this.findById(playerId)
|
||||
* if (!player) return null
|
||||
* return this.update(playerId, { score: player.score + points })
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // 4. 分页查询
|
||||
* const result = await userRepo.findPaginated(
|
||||
* { page: 1, pageSize: 20 },
|
||||
* { where: { isActive: true }, sort: { createdAt: 'desc' } }
|
||||
* )
|
||||
* console.log(`第 ${result.page}/${result.totalPages} 页,共 ${result.total} 条`)
|
||||
* ```
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
// Types | 类型
|
||||
// =============================================================================
|
||||
|
||||
export type {
|
||||
BaseEntity,
|
||||
SoftDeleteEntity,
|
||||
ComparisonOperators,
|
||||
WhereCondition,
|
||||
SortDirection,
|
||||
SortCondition,
|
||||
QueryOptions,
|
||||
PaginationParams,
|
||||
PaginatedResult,
|
||||
IRepository,
|
||||
UserEntity
|
||||
} from './types.js'
|
||||
|
||||
// =============================================================================
|
||||
// Repository | 仓库
|
||||
// =============================================================================
|
||||
|
||||
export { Repository, createRepository } from './Repository.js'
|
||||
|
||||
// =============================================================================
|
||||
// User Repository | 用户仓库
|
||||
// =============================================================================
|
||||
|
||||
export {
|
||||
UserRepository,
|
||||
createUserRepository,
|
||||
type CreateUserParams,
|
||||
type SafeUser
|
||||
} from './UserRepository.js'
|
||||
|
||||
// =============================================================================
|
||||
// Password | 密码工具
|
||||
// =============================================================================
|
||||
|
||||
export {
|
||||
hashPassword,
|
||||
verifyPassword,
|
||||
checkPasswordStrength,
|
||||
type PasswordHashConfig,
|
||||
type PasswordStrength,
|
||||
type PasswordStrengthResult
|
||||
} from './password.js'
|
||||
|
||||
// =============================================================================
|
||||
// Tokens | 服务令牌
|
||||
// =============================================================================
|
||||
|
||||
export {
|
||||
MongoConnectionToken,
|
||||
RedisConnectionToken,
|
||||
UserRepositoryToken,
|
||||
createServiceToken,
|
||||
type ServiceToken
|
||||
} from './tokens.js'
|
||||
|
||||
// =============================================================================
|
||||
// Re-exports from database-drivers | 从 database-drivers 重新导出
|
||||
// =============================================================================
|
||||
|
||||
export type {
|
||||
IMongoConnection,
|
||||
IRedisConnection,
|
||||
MongoConnectionConfig,
|
||||
RedisConnectionConfig,
|
||||
ConnectionState,
|
||||
DatabaseErrorCode
|
||||
} from '@esengine/database-drivers'
|
||||
|
||||
export {
|
||||
createMongoConnection,
|
||||
createRedisConnection,
|
||||
DatabaseError,
|
||||
ConnectionError,
|
||||
DuplicateKeyError
|
||||
} from '@esengine/database-drivers'
|
||||
189
packages/framework/database/src/password.ts
Normal file
189
packages/framework/database/src/password.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
/**
|
||||
* @zh 密码加密工具
|
||||
* @en Password hashing utilities
|
||||
*
|
||||
* @zh 使用 Node.js 内置的 crypto 模块实现安全的密码哈希
|
||||
* @en Uses Node.js built-in crypto module for secure password hashing
|
||||
*/
|
||||
|
||||
import { randomBytes, scrypt, timingSafeEqual } from 'crypto'
|
||||
import { promisify } from 'util'
|
||||
|
||||
const scryptAsync = promisify(scrypt)
|
||||
|
||||
/**
|
||||
* @zh 密码哈希配置
|
||||
* @en Password hash configuration
|
||||
*/
|
||||
export interface PasswordHashConfig {
|
||||
/**
|
||||
* @zh 盐的字节长度(默认 16)
|
||||
* @en Salt length in bytes (default 16)
|
||||
*/
|
||||
saltLength?: number
|
||||
|
||||
/**
|
||||
* @zh scrypt 密钥长度(默认 64)
|
||||
* @en scrypt key length (default 64)
|
||||
*/
|
||||
keyLength?: number
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: Required<PasswordHashConfig> = {
|
||||
saltLength: 16,
|
||||
keyLength: 64
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 对密码进行哈希处理
|
||||
* @en Hash a password
|
||||
*
|
||||
* @param password - @zh 明文密码 @en Plain text password
|
||||
* @param config - @zh 哈希配置 @en Hash configuration
|
||||
* @returns @zh 格式为 "salt:hash" 的哈希字符串 @en Hash string in "salt:hash" format
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const hashedPassword = await hashPassword('myPassword123')
|
||||
* // 存储 hashedPassword 到数据库
|
||||
* ```
|
||||
*/
|
||||
export async function hashPassword(
|
||||
password: string,
|
||||
config?: PasswordHashConfig
|
||||
): Promise<string> {
|
||||
const { saltLength, keyLength } = { ...DEFAULT_CONFIG, ...config }
|
||||
|
||||
const salt = randomBytes(saltLength).toString('hex')
|
||||
const derivedKey = (await scryptAsync(password, salt, keyLength)) as Buffer
|
||||
|
||||
return `${salt}:${derivedKey.toString('hex')}`
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 验证密码是否正确
|
||||
* @en Verify if a password is correct
|
||||
*
|
||||
* @param password - @zh 明文密码 @en Plain text password
|
||||
* @param hashedPassword - @zh 存储的哈希密码 @en Stored hashed password
|
||||
* @param config - @zh 哈希配置 @en Hash configuration
|
||||
* @returns @zh 密码是否匹配 @en Whether the password matches
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const isValid = await verifyPassword('myPassword123', storedHash)
|
||||
* if (isValid) {
|
||||
* // 登录成功
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export async function verifyPassword(
|
||||
password: string,
|
||||
hashedPassword: string,
|
||||
config?: PasswordHashConfig
|
||||
): Promise<boolean> {
|
||||
const { keyLength } = { ...DEFAULT_CONFIG, ...config }
|
||||
|
||||
const [salt, storedHash] = hashedPassword.split(':')
|
||||
if (!salt || !storedHash) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
const derivedKey = (await scryptAsync(password, salt, keyLength)) as Buffer
|
||||
const storedBuffer = Buffer.from(storedHash, 'hex')
|
||||
|
||||
return timingSafeEqual(derivedKey, storedBuffer)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 密码强度等级
|
||||
* @en Password strength level
|
||||
*/
|
||||
export type PasswordStrength = 'weak' | 'fair' | 'good' | 'strong'
|
||||
|
||||
/**
|
||||
* @zh 密码强度检查结果
|
||||
* @en Password strength check result
|
||||
*/
|
||||
export interface PasswordStrengthResult {
|
||||
/**
|
||||
* @zh 强度分数 (0-6)
|
||||
* @en Strength score (0-6)
|
||||
*/
|
||||
score: number
|
||||
|
||||
/**
|
||||
* @zh 强度等级
|
||||
* @en Strength level
|
||||
*/
|
||||
level: PasswordStrength
|
||||
|
||||
/**
|
||||
* @zh 改进建议
|
||||
* @en Improvement suggestions
|
||||
*/
|
||||
feedback: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查密码强度
|
||||
* @en Check password strength
|
||||
*
|
||||
* @param password - @zh 明文密码 @en Plain text password
|
||||
* @returns @zh 密码强度信息 @en Password strength information
|
||||
*/
|
||||
export function checkPasswordStrength(password: string): PasswordStrengthResult {
|
||||
const feedback: string[] = []
|
||||
let score = 0
|
||||
|
||||
if (password.length >= 8) {
|
||||
score += 1
|
||||
} else {
|
||||
feedback.push('Password should be at least 8 characters')
|
||||
}
|
||||
|
||||
if (password.length >= 12) {
|
||||
score += 1
|
||||
}
|
||||
|
||||
if (/[a-z]/.test(password)) {
|
||||
score += 1
|
||||
} else {
|
||||
feedback.push('Password should contain lowercase letters')
|
||||
}
|
||||
|
||||
if (/[A-Z]/.test(password)) {
|
||||
score += 1
|
||||
} else {
|
||||
feedback.push('Password should contain uppercase letters')
|
||||
}
|
||||
|
||||
if (/[0-9]/.test(password)) {
|
||||
score += 1
|
||||
} else {
|
||||
feedback.push('Password should contain numbers')
|
||||
}
|
||||
|
||||
if (/[^a-zA-Z0-9]/.test(password)) {
|
||||
score += 1
|
||||
} else {
|
||||
feedback.push('Password should contain special characters')
|
||||
}
|
||||
|
||||
let level: PasswordStrength
|
||||
if (score <= 2) {
|
||||
level = 'weak'
|
||||
} else if (score <= 3) {
|
||||
level = 'fair'
|
||||
} else if (score <= 4) {
|
||||
level = 'good'
|
||||
} else {
|
||||
level = 'strong'
|
||||
}
|
||||
|
||||
return { score, level, feedback }
|
||||
}
|
||||
17
packages/framework/database/src/tokens.ts
Normal file
17
packages/framework/database/src/tokens.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @zh 数据库服务令牌
|
||||
* @en Database service tokens
|
||||
*/
|
||||
|
||||
import type { ServiceToken, createServiceToken as createToken } from '@esengine/database-drivers'
|
||||
import type { UserRepository } from './UserRepository.js'
|
||||
|
||||
// Re-export from database-drivers for convenience
|
||||
export { MongoConnectionToken, RedisConnectionToken, createServiceToken } from '@esengine/database-drivers'
|
||||
export type { ServiceToken } from '@esengine/database-drivers'
|
||||
|
||||
/**
|
||||
* @zh 用户仓库令牌
|
||||
* @en User repository token
|
||||
*/
|
||||
export const UserRepositoryToken: ServiceToken<UserRepository> = { id: 'database:userRepository' }
|
||||
333
packages/framework/database/src/types.ts
Normal file
333
packages/framework/database/src/types.ts
Normal file
@@ -0,0 +1,333 @@
|
||||
/**
|
||||
* @zh 数据库核心类型定义
|
||||
* @en Database core type definitions
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
// 实体类型 | Entity Types
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 基础实体接口
|
||||
* @en Base entity interface
|
||||
*/
|
||||
export interface BaseEntity {
|
||||
/**
|
||||
* @zh 实体唯一标识
|
||||
* @en Entity unique identifier
|
||||
*/
|
||||
id: string
|
||||
|
||||
/**
|
||||
* @zh 创建时间
|
||||
* @en Creation timestamp
|
||||
*/
|
||||
createdAt?: Date
|
||||
|
||||
/**
|
||||
* @zh 更新时间
|
||||
* @en Update timestamp
|
||||
*/
|
||||
updatedAt?: Date
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 软删除实体接口
|
||||
* @en Soft delete entity interface
|
||||
*/
|
||||
export interface SoftDeleteEntity extends BaseEntity {
|
||||
/**
|
||||
* @zh 删除时间(null 表示未删除)
|
||||
* @en Deletion timestamp (null means not deleted)
|
||||
*/
|
||||
deletedAt?: Date | null
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 查询类型 | Query Types
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 比较操作符
|
||||
* @en Comparison operators
|
||||
*/
|
||||
export interface ComparisonOperators<T> {
|
||||
$eq?: T
|
||||
$ne?: T
|
||||
$gt?: T
|
||||
$gte?: T
|
||||
$lt?: T
|
||||
$lte?: T
|
||||
$in?: T[]
|
||||
$nin?: T[]
|
||||
$like?: string
|
||||
$regex?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 查询条件
|
||||
* @en Query condition
|
||||
*/
|
||||
export type WhereCondition<T> = {
|
||||
[K in keyof T]?: T[K] | ComparisonOperators<T[K]>
|
||||
} & {
|
||||
$or?: WhereCondition<T>[]
|
||||
$and?: WhereCondition<T>[]
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 排序方向
|
||||
* @en Sort direction
|
||||
*/
|
||||
export type SortDirection = 'asc' | 'desc'
|
||||
|
||||
/**
|
||||
* @zh 排序条件
|
||||
* @en Sort condition
|
||||
*/
|
||||
export type SortCondition<T> = {
|
||||
[K in keyof T]?: SortDirection
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 查询选项
|
||||
* @en Query options
|
||||
*/
|
||||
export interface QueryOptions<T> {
|
||||
/**
|
||||
* @zh 过滤条件
|
||||
* @en Filter conditions
|
||||
*/
|
||||
where?: WhereCondition<T>
|
||||
|
||||
/**
|
||||
* @zh 排序条件
|
||||
* @en Sort conditions
|
||||
*/
|
||||
sort?: SortCondition<T>
|
||||
|
||||
/**
|
||||
* @zh 限制返回数量
|
||||
* @en Limit number of results
|
||||
*/
|
||||
limit?: number
|
||||
|
||||
/**
|
||||
* @zh 跳过记录数
|
||||
* @en Number of records to skip
|
||||
*/
|
||||
offset?: number
|
||||
|
||||
/**
|
||||
* @zh 是否包含软删除记录
|
||||
* @en Whether to include soft deleted records
|
||||
*/
|
||||
includeSoftDeleted?: boolean
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 分页类型 | Pagination Types
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 分页参数
|
||||
* @en Pagination parameters
|
||||
*/
|
||||
export interface PaginationParams {
|
||||
/**
|
||||
* @zh 页码(从 1 开始)
|
||||
* @en Page number (starts from 1)
|
||||
*/
|
||||
page: number
|
||||
|
||||
/**
|
||||
* @zh 每页数量
|
||||
* @en Items per page
|
||||
*/
|
||||
pageSize: number
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 分页结果
|
||||
* @en Pagination result
|
||||
*/
|
||||
export interface PaginatedResult<T> {
|
||||
/**
|
||||
* @zh 数据列表
|
||||
* @en Data list
|
||||
*/
|
||||
data: T[]
|
||||
|
||||
/**
|
||||
* @zh 总记录数
|
||||
* @en Total count
|
||||
*/
|
||||
total: number
|
||||
|
||||
/**
|
||||
* @zh 当前页码
|
||||
* @en Current page
|
||||
*/
|
||||
page: number
|
||||
|
||||
/**
|
||||
* @zh 每页数量
|
||||
* @en Page size
|
||||
*/
|
||||
pageSize: number
|
||||
|
||||
/**
|
||||
* @zh 总页数
|
||||
* @en Total pages
|
||||
*/
|
||||
totalPages: number
|
||||
|
||||
/**
|
||||
* @zh 是否有下一页
|
||||
* @en Whether has next page
|
||||
*/
|
||||
hasNext: boolean
|
||||
|
||||
/**
|
||||
* @zh 是否有上一页
|
||||
* @en Whether has previous page
|
||||
*/
|
||||
hasPrev: boolean
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 仓库接口 | Repository Interface
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 仓库接口
|
||||
* @en Repository interface
|
||||
*/
|
||||
export interface IRepository<T extends BaseEntity> {
|
||||
/**
|
||||
* @zh 集合名称
|
||||
* @en Collection name
|
||||
*/
|
||||
readonly collectionName: string
|
||||
|
||||
/**
|
||||
* @zh 根据 ID 查找
|
||||
* @en Find by ID
|
||||
*/
|
||||
findById(id: string): Promise<T | null>
|
||||
|
||||
/**
|
||||
* @zh 查找单条记录
|
||||
* @en Find one record
|
||||
*/
|
||||
findOne(options?: QueryOptions<T>): Promise<T | null>
|
||||
|
||||
/**
|
||||
* @zh 查找多条记录
|
||||
* @en Find many records
|
||||
*/
|
||||
findMany(options?: QueryOptions<T>): Promise<T[]>
|
||||
|
||||
/**
|
||||
* @zh 分页查询
|
||||
* @en Paginated query
|
||||
*/
|
||||
findPaginated(
|
||||
pagination: PaginationParams,
|
||||
options?: Omit<QueryOptions<T>, 'limit' | 'offset'>
|
||||
): Promise<PaginatedResult<T>>
|
||||
|
||||
/**
|
||||
* @zh 统计记录数
|
||||
* @en Count records
|
||||
*/
|
||||
count(options?: QueryOptions<T>): Promise<number>
|
||||
|
||||
/**
|
||||
* @zh 检查记录是否存在
|
||||
* @en Check if record exists
|
||||
*/
|
||||
exists(options: QueryOptions<T>): Promise<boolean>
|
||||
|
||||
/**
|
||||
* @zh 创建记录
|
||||
* @en Create record
|
||||
*/
|
||||
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'> & { id?: string }): Promise<T>
|
||||
|
||||
/**
|
||||
* @zh 批量创建
|
||||
* @en Bulk create
|
||||
*/
|
||||
createMany(data: Array<Omit<T, 'id' | 'createdAt' | 'updatedAt'> & { id?: string }>): Promise<T[]>
|
||||
|
||||
/**
|
||||
* @zh 更新记录
|
||||
* @en Update record
|
||||
*/
|
||||
update(id: string, data: Partial<Omit<T, 'id' | 'createdAt' | 'updatedAt'>>): Promise<T | null>
|
||||
|
||||
/**
|
||||
* @zh 删除记录
|
||||
* @en Delete record
|
||||
*/
|
||||
delete(id: string): Promise<boolean>
|
||||
|
||||
/**
|
||||
* @zh 批量删除
|
||||
* @en Bulk delete
|
||||
*/
|
||||
deleteMany(options: QueryOptions<T>): Promise<number>
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 用户实体 | User Entity
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* @zh 用户实体
|
||||
* @en User entity
|
||||
*/
|
||||
export interface UserEntity extends SoftDeleteEntity {
|
||||
/**
|
||||
* @zh 用户名
|
||||
* @en Username
|
||||
*/
|
||||
username: string
|
||||
|
||||
/**
|
||||
* @zh 密码哈希
|
||||
* @en Password hash
|
||||
*/
|
||||
passwordHash: string
|
||||
|
||||
/**
|
||||
* @zh 邮箱
|
||||
* @en Email
|
||||
*/
|
||||
email?: string
|
||||
|
||||
/**
|
||||
* @zh 用户角色
|
||||
* @en User roles
|
||||
*/
|
||||
roles: string[]
|
||||
|
||||
/**
|
||||
* @zh 是否启用
|
||||
* @en Is active
|
||||
*/
|
||||
isActive: boolean
|
||||
|
||||
/**
|
||||
* @zh 最后登录时间
|
||||
* @en Last login timestamp
|
||||
*/
|
||||
lastLoginAt?: Date
|
||||
|
||||
/**
|
||||
* @zh 额外数据
|
||||
* @en Additional metadata
|
||||
*/
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
10
packages/framework/database/tsconfig.json
Normal file
10
packages/framework/database/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"declarationDir": "./dist"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
11
packages/framework/database/tsup.config.ts
Normal file
11
packages/framework/database/tsup.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineConfig } from 'tsup';
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.ts'],
|
||||
format: ['esm'],
|
||||
dts: true,
|
||||
clean: true,
|
||||
sourcemap: true,
|
||||
external: ['@esengine/database-drivers'],
|
||||
treeshake: true,
|
||||
});
|
||||
@@ -1,5 +1,19 @@
|
||||
# @esengine/network
|
||||
|
||||
## 5.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`902c0a1`](https://github.com/esengine/esengine/commit/902c0a10749f80bd8f499b44154646379d359704)]:
|
||||
- @esengine/rpc@1.1.3
|
||||
|
||||
## 5.0.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies []:
|
||||
- @esengine/rpc@1.1.2
|
||||
|
||||
## 5.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@esengine/network",
|
||||
"version": "5.0.1",
|
||||
"version": "5.0.3",
|
||||
"description": "Network synchronization for multiplayer games",
|
||||
"esengine": {
|
||||
"plugin": true,
|
||||
|
||||
@@ -1,5 +1,93 @@
|
||||
# @esengine/rpc
|
||||
|
||||
## 1.1.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#404](https://github.com/esengine/esengine/pull/404) [`902c0a1`](https://github.com/esengine/esengine/commit/902c0a10749f80bd8f499b44154646379d359704) Thanks [@esengine](https://github.com/esengine)! - feat(server): add HTTP file-based routing support / 添加 HTTP 文件路由支持
|
||||
|
||||
New feature that allows organizing HTTP routes in separate files, similar to API and message handlers.
|
||||
新功能:支持将 HTTP 路由组织在独立文件中,类似于 API 和消息处理器的文件路由方式。
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server';
|
||||
|
||||
export default defineHttp<{ username: string; password: string }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body;
|
||||
res.json({ token: '...', userId: '...' });
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Server configuration / 服务器配置:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
httpDir: 'src/http', // HTTP routes directory / HTTP 路由目录
|
||||
httpPrefix: '/api', // Route prefix / 路由前缀
|
||||
cors: true
|
||||
});
|
||||
```
|
||||
|
||||
File naming convention / 文件命名规则:
|
||||
- `login.ts` → POST /api/login
|
||||
- `users/profile.ts` → POST /api/users/profile
|
||||
- `users/[id].ts` → POST /api/users/:id (dynamic routes / 动态路由)
|
||||
- Set `method: 'GET'` in defineHttp for GET requests / 在 defineHttp 中设置 `method: 'GET'` 以处理 GET 请求
|
||||
|
||||
Also includes / 还包括:
|
||||
- `defineHttp<TBody>()` helper for type-safe route definitions / 类型安全的路由定义辅助函数
|
||||
- Support for merging file routes with inline `http` config / 支持文件路由与内联 `http` 配置合并
|
||||
- RPC server supports attaching to existing HTTP server via `server` option / RPC 服务器支持通过 `server` 选项附加到现有 HTTP 服务器
|
||||
|
||||
## 1.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- feat(server): add HTTP file-based routing support
|
||||
|
||||
New feature that allows organizing HTTP routes in separate files, similar to API and message handlers:
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server';
|
||||
|
||||
export default defineHttp<{ username: string; password: string }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body;
|
||||
// ... authentication logic
|
||||
res.json({ token: '...', userId: '...' });
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Server configuration:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
httpDir: 'src/http', // HTTP routes directory
|
||||
httpPrefix: '/api', // Route prefix
|
||||
cors: true
|
||||
});
|
||||
```
|
||||
|
||||
File naming convention:
|
||||
- `login.ts` → POST /api/login
|
||||
- `users/profile.ts` → POST /api/users/profile
|
||||
- `users/[id].ts` → POST /api/users/:id (dynamic routes)
|
||||
- Set `method: 'GET'` in defineHttp for GET requests
|
||||
|
||||
Also includes:
|
||||
- `defineHttp<TBody>()` helper function for type-safe route definitions
|
||||
- Support for merging file routes with inline `http` config
|
||||
- RPC server now supports attaching to existing HTTP server via `server` option
|
||||
|
||||
## 1.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@esengine/rpc",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.3",
|
||||
"description": "Elegant type-safe RPC library for ESEngine",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
@@ -32,6 +32,8 @@
|
||||
"build": "tsup && tsc --emitDeclarationOnly",
|
||||
"build:watch": "tsup --watch",
|
||||
"type-check": "tsc --noEmit",
|
||||
"lint": "eslint src --max-warnings 0",
|
||||
"lint:fix": "eslint src --fix",
|
||||
"clean": "rimraf dist"
|
||||
},
|
||||
"dependencies": {},
|
||||
|
||||
@@ -11,11 +11,11 @@ import type {
|
||||
ApiOutput,
|
||||
MsgData,
|
||||
Packet,
|
||||
ConnectionStatus,
|
||||
} from '../types'
|
||||
import { RpcError, ErrorCode } from '../types'
|
||||
import { json } from '../codec/json'
|
||||
import type { Codec } from '../codec/types'
|
||||
ConnectionStatus
|
||||
} from '../types';
|
||||
import { RpcError, ErrorCode } from '../types';
|
||||
import { json } from '../codec/json';
|
||||
import type { Codec } from '../codec/types';
|
||||
|
||||
// ============================================================================
|
||||
// Re-exports | 类型重导出
|
||||
@@ -29,9 +29,9 @@ export type {
|
||||
ApiOutput,
|
||||
MsgData,
|
||||
ConnectionStatus,
|
||||
Codec,
|
||||
}
|
||||
export { RpcError, ErrorCode }
|
||||
Codec
|
||||
};
|
||||
export { RpcError, ErrorCode };
|
||||
|
||||
// ============================================================================
|
||||
// Types | 类型定义
|
||||
@@ -133,11 +133,11 @@ const PacketType = {
|
||||
ApiResponse: 1,
|
||||
ApiError: 2,
|
||||
Message: 3,
|
||||
Heartbeat: 9,
|
||||
} as const
|
||||
Heartbeat: 9
|
||||
} as const;
|
||||
|
||||
const defaultWebSocketFactory: WebSocketFactory = (url) =>
|
||||
new WebSocket(url) as unknown as WebSocketAdapter
|
||||
new WebSocket(url) as unknown as WebSocketAdapter;
|
||||
|
||||
// ============================================================================
|
||||
// RpcClient Class | RPC 客户端类
|
||||
@@ -164,34 +164,34 @@ interface PendingCall {
|
||||
* ```
|
||||
*/
|
||||
export class RpcClient<P extends ProtocolDef> {
|
||||
private readonly _url: string
|
||||
private readonly _codec: Codec
|
||||
private readonly _timeout: number
|
||||
private readonly _reconnectInterval: number
|
||||
private readonly _wsFactory: WebSocketFactory
|
||||
private readonly _options: RpcClientOptions
|
||||
private readonly _url: string;
|
||||
private readonly _codec: Codec;
|
||||
private readonly _timeout: number;
|
||||
private readonly _reconnectInterval: number;
|
||||
private readonly _wsFactory: WebSocketFactory;
|
||||
private readonly _options: RpcClientOptions;
|
||||
|
||||
private _ws: WebSocketAdapter | null = null
|
||||
private _status: ConnectionStatus = 'closed'
|
||||
private _callIdCounter = 0
|
||||
private _shouldReconnect: boolean
|
||||
private _reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private _ws: WebSocketAdapter | null = null;
|
||||
private _status: ConnectionStatus = 'closed';
|
||||
private _callIdCounter = 0;
|
||||
private _shouldReconnect: boolean;
|
||||
private _reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
private readonly _pendingCalls = new Map<number, PendingCall>()
|
||||
private readonly _msgHandlers = new Map<string, Set<(data: unknown) => void>>()
|
||||
private readonly _pendingCalls = new Map<number, PendingCall>();
|
||||
private readonly _msgHandlers = new Map<string, Set<(data: unknown) => void>>();
|
||||
|
||||
constructor(
|
||||
_protocol: P,
|
||||
url: string,
|
||||
options: RpcClientOptions = {}
|
||||
) {
|
||||
this._url = url
|
||||
this._options = options
|
||||
this._codec = options.codec ?? json()
|
||||
this._timeout = options.timeout ?? 30000
|
||||
this._shouldReconnect = options.autoReconnect ?? true
|
||||
this._reconnectInterval = options.reconnectInterval ?? 3000
|
||||
this._wsFactory = options.webSocketFactory ?? defaultWebSocketFactory
|
||||
this._url = url;
|
||||
this._options = options;
|
||||
this._codec = options.codec ?? json();
|
||||
this._timeout = options.timeout ?? 30000;
|
||||
this._shouldReconnect = options.autoReconnect ?? true;
|
||||
this._reconnectInterval = options.reconnectInterval ?? 3000;
|
||||
this._wsFactory = options.webSocketFactory ?? defaultWebSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,7 +199,7 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
* @en Connection status
|
||||
*/
|
||||
get status(): ConnectionStatus {
|
||||
return this._status
|
||||
return this._status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,7 +207,7 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
* @en Whether connected
|
||||
*/
|
||||
get isConnected(): boolean {
|
||||
return this._status === 'open'
|
||||
return this._status === 'open';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,38 +217,38 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
connect(): Promise<this> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this._status === 'open' || this._status === 'connecting') {
|
||||
resolve(this)
|
||||
return
|
||||
resolve(this);
|
||||
return;
|
||||
}
|
||||
|
||||
this._status = 'connecting'
|
||||
this._ws = this._wsFactory(this._url)
|
||||
this._status = 'connecting';
|
||||
this._ws = this._wsFactory(this._url);
|
||||
|
||||
this._ws.onopen = () => {
|
||||
this._status = 'open'
|
||||
this._options.onConnect?.()
|
||||
resolve(this)
|
||||
}
|
||||
this._status = 'open';
|
||||
this._options.onConnect?.();
|
||||
resolve(this);
|
||||
};
|
||||
|
||||
this._ws.onclose = (e) => {
|
||||
this._status = 'closed'
|
||||
this._rejectAllPending()
|
||||
this._options.onDisconnect?.(e.reason)
|
||||
this._scheduleReconnect()
|
||||
}
|
||||
this._status = 'closed';
|
||||
this._rejectAllPending();
|
||||
this._options.onDisconnect?.(e.reason);
|
||||
this._scheduleReconnect();
|
||||
};
|
||||
|
||||
this._ws.onerror = () => {
|
||||
const err = new Error('WebSocket error')
|
||||
this._options.onError?.(err)
|
||||
const err = new Error('WebSocket error');
|
||||
this._options.onError?.(err);
|
||||
if (this._status === 'connecting') {
|
||||
reject(err)
|
||||
reject(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this._ws.onmessage = (e) => {
|
||||
this._handleMessage(e.data as string | ArrayBuffer)
|
||||
}
|
||||
})
|
||||
this._handleMessage(e.data as string | ArrayBuffer);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -256,12 +256,12 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
* @en Disconnect
|
||||
*/
|
||||
disconnect(): void {
|
||||
this._shouldReconnect = false
|
||||
this._clearReconnectTimer()
|
||||
this._shouldReconnect = false;
|
||||
this._clearReconnectTimer();
|
||||
if (this._ws) {
|
||||
this._status = 'closing'
|
||||
this._ws.close()
|
||||
this._ws = null
|
||||
this._status = 'closing';
|
||||
this._ws.close();
|
||||
this._ws = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,25 +275,25 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
): Promise<ApiOutput<P['api'][K]>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this._status !== 'open') {
|
||||
reject(new RpcError(ErrorCode.CONNECTION_CLOSED, 'Not connected'))
|
||||
return
|
||||
reject(new RpcError(ErrorCode.CONNECTION_CLOSED, 'Not connected'));
|
||||
return;
|
||||
}
|
||||
|
||||
const id = ++this._callIdCounter
|
||||
const id = ++this._callIdCounter;
|
||||
const timer = setTimeout(() => {
|
||||
this._pendingCalls.delete(id)
|
||||
reject(new RpcError(ErrorCode.TIMEOUT, 'Request timeout'))
|
||||
}, this._timeout)
|
||||
this._pendingCalls.delete(id);
|
||||
reject(new RpcError(ErrorCode.TIMEOUT, 'Request timeout'));
|
||||
}, this._timeout);
|
||||
|
||||
this._pendingCalls.set(id, {
|
||||
resolve: resolve as (v: unknown) => void,
|
||||
reject,
|
||||
timer,
|
||||
})
|
||||
timer
|
||||
});
|
||||
|
||||
const packet: Packet = [PacketType.ApiRequest, id, name as string, input]
|
||||
this._ws!.send(this._codec.encode(packet) as string | ArrayBuffer)
|
||||
})
|
||||
const packet: Packet = [PacketType.ApiRequest, id, name as string, input];
|
||||
this._ws!.send(this._codec.encode(packet) as string | ArrayBuffer);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,9 +301,9 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
* @en Send message
|
||||
*/
|
||||
send<K extends MsgNames<P>>(name: K, data: MsgData<P['msg'][K]>): void {
|
||||
if (this._status !== 'open') return
|
||||
const packet: Packet = [PacketType.Message, name as string, data]
|
||||
this._ws!.send(this._codec.encode(packet) as string | ArrayBuffer)
|
||||
if (this._status !== 'open') return;
|
||||
const packet: Packet = [PacketType.Message, name as string, data];
|
||||
this._ws!.send(this._codec.encode(packet) as string | ArrayBuffer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,14 +314,14 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
name: K,
|
||||
handler: (data: MsgData<P['msg'][K]>) => void
|
||||
): this {
|
||||
const key = name as string
|
||||
let handlers = this._msgHandlers.get(key)
|
||||
const key = name as string;
|
||||
let handlers = this._msgHandlers.get(key);
|
||||
if (!handlers) {
|
||||
handlers = new Set()
|
||||
this._msgHandlers.set(key, handlers)
|
||||
handlers = new Set();
|
||||
this._msgHandlers.set(key, handlers);
|
||||
}
|
||||
handlers.add(handler as (data: unknown) => void)
|
||||
return this
|
||||
handlers.add(handler as (data: unknown) => void);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,13 +332,13 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
name: K,
|
||||
handler?: (data: MsgData<P['msg'][K]>) => void
|
||||
): this {
|
||||
const key = name as string
|
||||
const key = name as string;
|
||||
if (handler) {
|
||||
this._msgHandlers.get(key)?.delete(handler as (data: unknown) => void)
|
||||
this._msgHandlers.get(key)?.delete(handler as (data: unknown) => void);
|
||||
} else {
|
||||
this._msgHandlers.delete(key)
|
||||
this._msgHandlers.delete(key);
|
||||
}
|
||||
return this
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -350,10 +350,10 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
handler: (data: MsgData<P['msg'][K]>) => void
|
||||
): this {
|
||||
const wrapper = (data: MsgData<P['msg'][K]>) => {
|
||||
this.off(name, wrapper)
|
||||
handler(data)
|
||||
}
|
||||
return this.on(name, wrapper)
|
||||
this.off(name, wrapper);
|
||||
handler(data);
|
||||
};
|
||||
return this.on(name, wrapper);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
@@ -362,52 +362,52 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
|
||||
private _handleMessage(raw: string | ArrayBuffer): void {
|
||||
try {
|
||||
const data = typeof raw === 'string' ? raw : new Uint8Array(raw)
|
||||
const packet = this._codec.decode(data)
|
||||
const type = packet[0]
|
||||
const data = typeof raw === 'string' ? raw : new Uint8Array(raw);
|
||||
const packet = this._codec.decode(data);
|
||||
const type = packet[0];
|
||||
|
||||
switch (type) {
|
||||
case PacketType.ApiResponse:
|
||||
this._handleApiResponse(packet as [number, number, unknown])
|
||||
break
|
||||
this._handleApiResponse(packet as [number, number, unknown]);
|
||||
break;
|
||||
case PacketType.ApiError:
|
||||
this._handleApiError(packet as [number, number, string, string])
|
||||
break
|
||||
this._handleApiError(packet as [number, number, string, string]);
|
||||
break;
|
||||
case PacketType.Message:
|
||||
this._handleMsg(packet as [number, string, unknown])
|
||||
break
|
||||
this._handleMsg(packet as [number, string, unknown]);
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
this._options.onError?.(err as Error)
|
||||
this._options.onError?.(err as Error);
|
||||
}
|
||||
}
|
||||
|
||||
private _handleApiResponse([, id, result]: [number, number, unknown]): void {
|
||||
const pending = this._pendingCalls.get(id)
|
||||
const pending = this._pendingCalls.get(id);
|
||||
if (pending) {
|
||||
clearTimeout(pending.timer)
|
||||
this._pendingCalls.delete(id)
|
||||
pending.resolve(result)
|
||||
clearTimeout(pending.timer);
|
||||
this._pendingCalls.delete(id);
|
||||
pending.resolve(result);
|
||||
}
|
||||
}
|
||||
|
||||
private _handleApiError([, id, code, message]: [number, number, string, string]): void {
|
||||
const pending = this._pendingCalls.get(id)
|
||||
const pending = this._pendingCalls.get(id);
|
||||
if (pending) {
|
||||
clearTimeout(pending.timer)
|
||||
this._pendingCalls.delete(id)
|
||||
pending.reject(new RpcError(code, message))
|
||||
clearTimeout(pending.timer);
|
||||
this._pendingCalls.delete(id);
|
||||
pending.reject(new RpcError(code, message));
|
||||
}
|
||||
}
|
||||
|
||||
private _handleMsg([, path, data]: [number, string, unknown]): void {
|
||||
const handlers = this._msgHandlers.get(path)
|
||||
const handlers = this._msgHandlers.get(path);
|
||||
if (handlers) {
|
||||
for (const handler of handlers) {
|
||||
try {
|
||||
handler(data)
|
||||
handler(data);
|
||||
} catch (err) {
|
||||
this._options.onError?.(err as Error)
|
||||
this._options.onError?.(err as Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -415,25 +415,25 @@ export class RpcClient<P extends ProtocolDef> {
|
||||
|
||||
private _rejectAllPending(): void {
|
||||
for (const [, pending] of this._pendingCalls) {
|
||||
clearTimeout(pending.timer)
|
||||
pending.reject(new RpcError(ErrorCode.CONNECTION_CLOSED, 'Connection closed'))
|
||||
clearTimeout(pending.timer);
|
||||
pending.reject(new RpcError(ErrorCode.CONNECTION_CLOSED, 'Connection closed'));
|
||||
}
|
||||
this._pendingCalls.clear()
|
||||
this._pendingCalls.clear();
|
||||
}
|
||||
|
||||
private _scheduleReconnect(): void {
|
||||
if (this._shouldReconnect && !this._reconnectTimer) {
|
||||
this._reconnectTimer = setTimeout(() => {
|
||||
this._reconnectTimer = null
|
||||
this.connect().catch(() => {})
|
||||
}, this._reconnectInterval)
|
||||
this._reconnectTimer = null;
|
||||
this.connect().catch(() => {});
|
||||
}, this._reconnectInterval);
|
||||
}
|
||||
}
|
||||
|
||||
private _clearReconnectTimer(): void {
|
||||
if (this._reconnectTimer) {
|
||||
clearTimeout(this._reconnectTimer)
|
||||
this._reconnectTimer = null
|
||||
clearTimeout(this._reconnectTimer);
|
||||
this._reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -457,5 +457,5 @@ export function connect<P extends ProtocolDef>(
|
||||
url: string,
|
||||
options: RpcClientOptions = {}
|
||||
): Promise<RpcClient<P>> {
|
||||
return new RpcClient(protocol, url, options).connect()
|
||||
return new RpcClient(protocol, url, options).connect();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* @en Codec Module
|
||||
*/
|
||||
|
||||
export type { Codec } from './types'
|
||||
export { json } from './json'
|
||||
export { msgpack } from './msgpack'
|
||||
export { textEncode, textDecode } from './polyfill'
|
||||
export type { Codec } from './types';
|
||||
export { json } from './json';
|
||||
export { msgpack } from './msgpack';
|
||||
export { textEncode, textDecode } from './polyfill';
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
* @en JSON Codec
|
||||
*/
|
||||
|
||||
import type { Packet } from '../types'
|
||||
import type { Codec } from './types'
|
||||
import { textDecode } from './polyfill'
|
||||
import type { Packet } from '../types';
|
||||
import type { Codec } from './types';
|
||||
import { textDecode } from './polyfill';
|
||||
|
||||
/**
|
||||
* @zh 创建 JSON 编解码器
|
||||
@@ -17,14 +17,14 @@ import { textDecode } from './polyfill'
|
||||
export function json(): Codec {
|
||||
return {
|
||||
encode(packet: Packet): string {
|
||||
return JSON.stringify(packet)
|
||||
return JSON.stringify(packet);
|
||||
},
|
||||
|
||||
decode(data: string | Uint8Array): Packet {
|
||||
const str = typeof data === 'string'
|
||||
? data
|
||||
: textDecode(data)
|
||||
return JSON.parse(str) as Packet
|
||||
},
|
||||
}
|
||||
: textDecode(data);
|
||||
return JSON.parse(str) as Packet;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
* @en MessagePack Codec
|
||||
*/
|
||||
|
||||
import { Packr, Unpackr } from 'msgpackr'
|
||||
import type { Packet } from '../types'
|
||||
import type { Codec } from './types'
|
||||
import { textEncode } from './polyfill'
|
||||
import { Packr, Unpackr } from 'msgpackr';
|
||||
import type { Packet } from '../types';
|
||||
import type { Codec } from './types';
|
||||
import { textEncode } from './polyfill';
|
||||
|
||||
/**
|
||||
* @zh 创建 MessagePack 编解码器
|
||||
@@ -16,19 +16,19 @@ import { textEncode } from './polyfill'
|
||||
* @en Suitable for production, smaller size and faster speed
|
||||
*/
|
||||
export function msgpack(): Codec {
|
||||
const encoder = new Packr({ structuredClone: true })
|
||||
const decoder = new Unpackr({ structuredClone: true })
|
||||
const encoder = new Packr({ structuredClone: true });
|
||||
const decoder = new Unpackr({ structuredClone: true });
|
||||
|
||||
return {
|
||||
encode(packet: Packet): Uint8Array {
|
||||
return encoder.pack(packet)
|
||||
return encoder.pack(packet);
|
||||
},
|
||||
|
||||
decode(data: string | Uint8Array): Packet {
|
||||
const buf = typeof data === 'string'
|
||||
? textEncode(data)
|
||||
: data
|
||||
return decoder.unpack(buf) as Packet
|
||||
},
|
||||
}
|
||||
: data;
|
||||
return decoder.unpack(buf) as Packet;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,38 +12,38 @@
|
||||
*/
|
||||
function getTextEncoder(): { encode(str: string): Uint8Array } {
|
||||
if (typeof TextEncoder !== 'undefined') {
|
||||
return new TextEncoder()
|
||||
return new TextEncoder();
|
||||
}
|
||||
return {
|
||||
encode(str: string): Uint8Array {
|
||||
const utf8: number[] = []
|
||||
const utf8: number[] = [];
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
let charCode = str.charCodeAt(i)
|
||||
let charCode = str.charCodeAt(i);
|
||||
if (charCode < 0x80) {
|
||||
utf8.push(charCode)
|
||||
utf8.push(charCode);
|
||||
} else if (charCode < 0x800) {
|
||||
utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f))
|
||||
utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
|
||||
} else if (charCode >= 0xd800 && charCode <= 0xdbff) {
|
||||
i++
|
||||
const low = str.charCodeAt(i)
|
||||
charCode = 0x10000 + ((charCode - 0xd800) << 10) + (low - 0xdc00)
|
||||
i++;
|
||||
const low = str.charCodeAt(i);
|
||||
charCode = 0x10000 + ((charCode - 0xd800) << 10) + (low - 0xdc00);
|
||||
utf8.push(
|
||||
0xf0 | (charCode >> 18),
|
||||
0x80 | ((charCode >> 12) & 0x3f),
|
||||
0x80 | ((charCode >> 6) & 0x3f),
|
||||
0x80 | (charCode & 0x3f)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
utf8.push(
|
||||
0xe0 | (charCode >> 12),
|
||||
0x80 | ((charCode >> 6) & 0x3f),
|
||||
0x80 | (charCode & 0x3f)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return new Uint8Array(utf8)
|
||||
},
|
||||
}
|
||||
return new Uint8Array(utf8);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,55 +52,55 @@ function getTextEncoder(): { encode(str: string): Uint8Array } {
|
||||
*/
|
||||
function getTextDecoder(): { decode(data: Uint8Array): string } {
|
||||
if (typeof TextDecoder !== 'undefined') {
|
||||
return new TextDecoder()
|
||||
return new TextDecoder();
|
||||
}
|
||||
return {
|
||||
decode(data: Uint8Array): string {
|
||||
let str = ''
|
||||
let i = 0
|
||||
let str = '';
|
||||
let i = 0;
|
||||
while (i < data.length) {
|
||||
const byte1 = data[i++]
|
||||
const byte1 = data[i++];
|
||||
if (byte1 < 0x80) {
|
||||
str += String.fromCharCode(byte1)
|
||||
str += String.fromCharCode(byte1);
|
||||
} else if ((byte1 & 0xe0) === 0xc0) {
|
||||
const byte2 = data[i++]
|
||||
str += String.fromCharCode(((byte1 & 0x1f) << 6) | (byte2 & 0x3f))
|
||||
const byte2 = data[i++];
|
||||
str += String.fromCharCode(((byte1 & 0x1f) << 6) | (byte2 & 0x3f));
|
||||
} else if ((byte1 & 0xf0) === 0xe0) {
|
||||
const byte2 = data[i++]
|
||||
const byte3 = data[i++]
|
||||
const byte2 = data[i++];
|
||||
const byte3 = data[i++];
|
||||
str += String.fromCharCode(
|
||||
((byte1 & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f)
|
||||
)
|
||||
);
|
||||
} else if ((byte1 & 0xf8) === 0xf0) {
|
||||
const byte2 = data[i++]
|
||||
const byte3 = data[i++]
|
||||
const byte4 = data[i++]
|
||||
const byte2 = data[i++];
|
||||
const byte3 = data[i++];
|
||||
const byte4 = data[i++];
|
||||
const codePoint =
|
||||
((byte1 & 0x07) << 18) |
|
||||
((byte2 & 0x3f) << 12) |
|
||||
((byte3 & 0x3f) << 6) |
|
||||
(byte4 & 0x3f)
|
||||
const offset = codePoint - 0x10000
|
||||
(byte4 & 0x3f);
|
||||
const offset = codePoint - 0x10000;
|
||||
str += String.fromCharCode(
|
||||
0xd800 + (offset >> 10),
|
||||
0xdc00 + (offset & 0x3ff)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return str
|
||||
},
|
||||
}
|
||||
return str;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const encoder = getTextEncoder()
|
||||
const decoder = getTextDecoder()
|
||||
const encoder = getTextEncoder();
|
||||
const decoder = getTextDecoder();
|
||||
|
||||
/**
|
||||
* @zh 将字符串编码为 UTF-8 字节数组
|
||||
* @en Encode string to UTF-8 byte array
|
||||
*/
|
||||
export function textEncode(str: string): Uint8Array {
|
||||
return encoder.encode(str)
|
||||
return encoder.encode(str);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,5 +108,5 @@ export function textEncode(str: string): Uint8Array {
|
||||
* @en Decode UTF-8 byte array to string
|
||||
*/
|
||||
export function textDecode(data: Uint8Array): string {
|
||||
return decoder.decode(data)
|
||||
return decoder.decode(data);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* @en Codec Type Definitions
|
||||
*/
|
||||
|
||||
import type { Packet } from '../types'
|
||||
import type { Packet } from '../types';
|
||||
|
||||
/**
|
||||
* @zh 编解码器接口
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* @en Protocol Definition Module
|
||||
*/
|
||||
|
||||
import type { ApiDef, MsgDef, ProtocolDef } from './types'
|
||||
import type { ApiDef, MsgDef, ProtocolDef } from './types';
|
||||
|
||||
/**
|
||||
* @zh 创建 API 定义
|
||||
@@ -15,7 +15,7 @@ import type { ApiDef, MsgDef, ProtocolDef } from './types'
|
||||
* ```
|
||||
*/
|
||||
function api<TInput = void, TOutput = void>(): ApiDef<TInput, TOutput> {
|
||||
return { _type: 'api' } as ApiDef<TInput, TOutput>
|
||||
return { _type: 'api' } as ApiDef<TInput, TOutput>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,7 +28,7 @@ function api<TInput = void, TOutput = void>(): ApiDef<TInput, TOutput> {
|
||||
* ```
|
||||
*/
|
||||
function msg<TData = void>(): MsgDef<TData> {
|
||||
return { _type: 'msg' } as MsgDef<TData>
|
||||
return { _type: 'msg' } as MsgDef<TData>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,7 +49,7 @@ function msg<TData = void>(): MsgDef<TData> {
|
||||
* ```
|
||||
*/
|
||||
function define<T extends ProtocolDef>(protocol: T): T {
|
||||
return protocol
|
||||
return protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,5 +59,5 @@ function define<T extends ProtocolDef>(protocol: T): T {
|
||||
export const rpc = {
|
||||
define,
|
||||
api,
|
||||
msg,
|
||||
} as const
|
||||
msg
|
||||
} as const;
|
||||
|
||||
@@ -38,9 +38,9 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
export { rpc } from './define'
|
||||
export * from './types'
|
||||
export { rpc } from './define';
|
||||
export * from './types';
|
||||
|
||||
// Re-export client for browser/bundler compatibility
|
||||
export { RpcClient, connect } from './client/index'
|
||||
export type { RpcClientOptions, WebSocketAdapter, WebSocketFactory } from './client/index'
|
||||
export { RpcClient, connect } from './client/index';
|
||||
export type { RpcClientOptions, WebSocketAdapter, WebSocketFactory } from './client/index';
|
||||
|
||||
@@ -3,37 +3,38 @@
|
||||
* @en Server Connection Module
|
||||
*/
|
||||
|
||||
import type { Connection, ConnectionStatus } from '../types'
|
||||
import type { WebSocket } from 'ws';
|
||||
import type { Connection, ConnectionStatus } from '../types';
|
||||
|
||||
/**
|
||||
* @zh 服务端连接实现
|
||||
* @en Server connection implementation
|
||||
*/
|
||||
export class ServerConnection<TData = unknown> implements Connection<TData> {
|
||||
readonly id: string
|
||||
readonly ip: string
|
||||
data: TData
|
||||
readonly id: string;
|
||||
readonly ip: string;
|
||||
data: TData;
|
||||
|
||||
private _status: ConnectionStatus = 'open'
|
||||
private _socket: any
|
||||
private _onClose?: () => void
|
||||
private _status: ConnectionStatus = 'open';
|
||||
private _socket: WebSocket;
|
||||
private _onClose?: () => void;
|
||||
|
||||
constructor(options: {
|
||||
id: string
|
||||
ip: string
|
||||
socket: any
|
||||
socket: WebSocket
|
||||
initialData: TData
|
||||
onClose?: () => void
|
||||
}) {
|
||||
this.id = options.id
|
||||
this.ip = options.ip
|
||||
this.data = options.initialData
|
||||
this._socket = options.socket
|
||||
this._onClose = options.onClose
|
||||
this.id = options.id;
|
||||
this.ip = options.ip;
|
||||
this.data = options.initialData;
|
||||
this._socket = options.socket;
|
||||
this._onClose = options.onClose;
|
||||
}
|
||||
|
||||
get status(): ConnectionStatus {
|
||||
return this._status
|
||||
return this._status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,8 +42,20 @@ export class ServerConnection<TData = unknown> implements Connection<TData> {
|
||||
* @en Send raw data
|
||||
*/
|
||||
send(data: string | Uint8Array): void {
|
||||
if (this._status !== 'open') return
|
||||
this._socket.send(data)
|
||||
if (this._status !== 'open') return;
|
||||
this._socket.send(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 发送二进制数据(原生 WebSocket 二进制帧)
|
||||
* @en Send binary data (native WebSocket binary frame)
|
||||
*
|
||||
* @zh 直接发送 Uint8Array,不经过 JSON 编码,效率更高
|
||||
* @en Directly sends Uint8Array without JSON encoding, more efficient
|
||||
*/
|
||||
sendBinary(data: Uint8Array): void {
|
||||
if (this._status !== 'open') return;
|
||||
this._socket.send(data, { binary: true });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,12 +63,12 @@ export class ServerConnection<TData = unknown> implements Connection<TData> {
|
||||
* @en Close connection
|
||||
*/
|
||||
close(reason?: string): void {
|
||||
if (this._status !== 'open') return
|
||||
if (this._status !== 'open') return;
|
||||
|
||||
this._status = 'closing'
|
||||
this._socket.close(1000, reason)
|
||||
this._status = 'closed'
|
||||
this._onClose?.()
|
||||
this._status = 'closing';
|
||||
this._socket.close(1000, reason);
|
||||
this._status = 'closed';
|
||||
this._onClose?.();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,6 +76,6 @@ export class ServerConnection<TData = unknown> implements Connection<TData> {
|
||||
* @en Mark connection as closed (internal use)
|
||||
*/
|
||||
_markClosed(): void {
|
||||
this._status = 'closed'
|
||||
this._status = 'closed';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
* @en RPC Server Module
|
||||
*/
|
||||
|
||||
import { WebSocketServer, WebSocket } from 'ws'
|
||||
import { WebSocketServer, WebSocket } from 'ws';
|
||||
import type { Server as HttpServer } from 'node:http';
|
||||
import type {
|
||||
ProtocolDef,
|
||||
ApiNames,
|
||||
@@ -12,13 +13,13 @@ import type {
|
||||
ApiOutput,
|
||||
MsgData,
|
||||
Packet,
|
||||
PacketType,
|
||||
Connection,
|
||||
} from '../types'
|
||||
import { RpcError, ErrorCode } from '../types'
|
||||
import { json } from '../codec/json'
|
||||
import type { Codec } from '../codec/types'
|
||||
import { ServerConnection } from './connection'
|
||||
Connection
|
||||
} from '../types';
|
||||
import type { IncomingMessage } from 'node:http';
|
||||
import { RpcError, ErrorCode } from '../types';
|
||||
import { json } from '../codec/json';
|
||||
import type { Codec } from '../codec/types';
|
||||
import { ServerConnection } from './connection';
|
||||
|
||||
// ============ Types ============
|
||||
|
||||
@@ -66,10 +67,19 @@ type MsgHandlers<P extends ProtocolDef, TConnData> = {
|
||||
*/
|
||||
export interface ServeOptions<P extends ProtocolDef, TConnData = unknown> {
|
||||
/**
|
||||
* @zh 监听端口
|
||||
* @en Listen port
|
||||
* @zh 监听端口(与 server 二选一)
|
||||
* @en Listen port (mutually exclusive with server)
|
||||
*/
|
||||
port: number
|
||||
port?: number
|
||||
|
||||
/**
|
||||
* @zh 已有的 HTTP 服务器(与 port 二选一)
|
||||
* @en Existing HTTP server (mutually exclusive with port)
|
||||
*
|
||||
* @zh 使用此选项可以在同一端口同时支持 HTTP 和 WebSocket
|
||||
* @en Use this option to support both HTTP and WebSocket on the same port
|
||||
*/
|
||||
server?: HttpServer
|
||||
|
||||
/**
|
||||
* @zh API 处理器
|
||||
@@ -172,8 +182,8 @@ const PT = {
|
||||
ApiResponse: 1,
|
||||
ApiError: 2,
|
||||
Message: 3,
|
||||
Heartbeat: 9,
|
||||
} as const
|
||||
Heartbeat: 9
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* @zh 创建 RPC 服务器
|
||||
@@ -196,16 +206,22 @@ export function serve<P extends ProtocolDef, TConnData = unknown>(
|
||||
_protocol: P,
|
||||
options: ServeOptions<P, TConnData>
|
||||
): RpcServer<P, TConnData> {
|
||||
const codec = options.codec ?? json()
|
||||
const connections: ServerConnection<TConnData>[] = []
|
||||
let wss: WebSocketServer | null = null
|
||||
let connIdCounter = 0
|
||||
const codec = options.codec ?? json();
|
||||
const connections: ServerConnection<TConnData>[] = [];
|
||||
let wss: WebSocketServer | null = null;
|
||||
let connIdCounter = 0;
|
||||
|
||||
const getClientIp = (ws: WebSocket, req: any): string => {
|
||||
return req?.headers?.['x-forwarded-for']?.split(',')[0]?.trim()
|
||||
const getClientIp = (_ws: WebSocket, req: IncomingMessage | undefined): string => {
|
||||
const forwarded = req?.headers?.['x-forwarded-for'];
|
||||
const forwardedIp = typeof forwarded === 'string'
|
||||
? forwarded.split(',')[0]?.trim()
|
||||
: Array.isArray(forwarded)
|
||||
? forwarded[0]?.split(',')[0]?.trim()
|
||||
: undefined;
|
||||
return forwardedIp
|
||||
|| req?.socket?.remoteAddress
|
||||
|| 'unknown'
|
||||
}
|
||||
|| 'unknown';
|
||||
};
|
||||
|
||||
const handleMessage = async (
|
||||
conn: ServerConnection<TConnData>,
|
||||
@@ -214,23 +230,23 @@ export function serve<P extends ProtocolDef, TConnData = unknown>(
|
||||
try {
|
||||
const packet = codec.decode(
|
||||
typeof data === 'string' ? data : new Uint8Array(data)
|
||||
)
|
||||
);
|
||||
|
||||
const type = packet[0]
|
||||
const type = packet[0];
|
||||
|
||||
if (type === PT.ApiRequest) {
|
||||
const [, id, path, input] = packet as [number, number, string, unknown]
|
||||
await handleApiRequest(conn, id, path, input)
|
||||
const [, id, path, input] = packet as [number, number, string, unknown];
|
||||
await handleApiRequest(conn, id, path, input);
|
||||
} else if (type === PT.Message) {
|
||||
const [, path, msgData] = packet as [number, string, unknown]
|
||||
await handleMsg(conn, path, msgData)
|
||||
const [, path, msgData] = packet as [number, string, unknown];
|
||||
await handleMsg(conn, path, msgData);
|
||||
} else if (type === PT.Heartbeat) {
|
||||
conn.send(codec.encode([PT.Heartbeat]))
|
||||
conn.send(codec.encode([PT.Heartbeat]));
|
||||
}
|
||||
} catch (err) {
|
||||
options.onError?.(err as Error, conn)
|
||||
options.onError?.(err as Error, conn);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleApiRequest = async (
|
||||
conn: ServerConnection<TConnData>,
|
||||
@@ -238,54 +254,65 @@ export function serve<P extends ProtocolDef, TConnData = unknown>(
|
||||
path: string,
|
||||
input: unknown
|
||||
): Promise<void> => {
|
||||
const handler = (options.api as any)[path]
|
||||
const apiHandlers = options.api as Record<string, ApiHandler<unknown, unknown, TConnData> | undefined>;
|
||||
const handler = apiHandlers[path];
|
||||
|
||||
if (!handler) {
|
||||
const errPacket: Packet = [PT.ApiError, id, ErrorCode.NOT_FOUND, `API not found: ${path}`]
|
||||
conn.send(codec.encode(errPacket))
|
||||
return
|
||||
const errPacket: Packet = [PT.ApiError, id, ErrorCode.NOT_FOUND, `API not found: ${path}`];
|
||||
conn.send(codec.encode(errPacket));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await handler(input, conn)
|
||||
const resPacket: Packet = [PT.ApiResponse, id, result]
|
||||
conn.send(codec.encode(resPacket))
|
||||
const result = await handler(input, conn);
|
||||
const resPacket: Packet = [PT.ApiResponse, id, result];
|
||||
conn.send(codec.encode(resPacket));
|
||||
} catch (err) {
|
||||
if (err instanceof RpcError) {
|
||||
const errPacket: Packet = [PT.ApiError, id, err.code, err.message]
|
||||
conn.send(codec.encode(errPacket))
|
||||
const errPacket: Packet = [PT.ApiError, id, err.code, err.message];
|
||||
conn.send(codec.encode(errPacket));
|
||||
} else {
|
||||
const errPacket: Packet = [PT.ApiError, id, ErrorCode.INTERNAL_ERROR, 'Internal server error']
|
||||
conn.send(codec.encode(errPacket))
|
||||
options.onError?.(err as Error, conn)
|
||||
const errPacket: Packet = [PT.ApiError, id, ErrorCode.INTERNAL_ERROR, 'Internal server error'];
|
||||
conn.send(codec.encode(errPacket));
|
||||
options.onError?.(err as Error, conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleMsg = async (
|
||||
conn: ServerConnection<TConnData>,
|
||||
path: string,
|
||||
data: unknown
|
||||
): Promise<void> => {
|
||||
const handler = options.msg?.[path as MsgNames<P>]
|
||||
const msgHandlers = options.msg as Record<string, MsgHandler<unknown, TConnData> | undefined> | undefined;
|
||||
const handler = msgHandlers?.[path];
|
||||
if (handler) {
|
||||
await (handler as any)(data, conn)
|
||||
await handler(data, conn);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const server: RpcServer<P, TConnData> = {
|
||||
get connections() {
|
||||
return connections as ReadonlyArray<Connection<TConnData>>
|
||||
return connections as ReadonlyArray<Connection<TConnData>>;
|
||||
},
|
||||
|
||||
async start() {
|
||||
return new Promise((resolve) => {
|
||||
wss = new WebSocketServer({ port: options.port })
|
||||
// 根据配置创建 WebSocketServer
|
||||
if (options.server) {
|
||||
// 附加到已有的 HTTP 服务器
|
||||
wss = new WebSocketServer({ server: options.server });
|
||||
} else if (options.port) {
|
||||
// 独立创建
|
||||
wss = new WebSocketServer({ port: options.port });
|
||||
} else {
|
||||
throw new Error('Either port or server must be provided');
|
||||
}
|
||||
|
||||
wss.on('connection', async (ws, req) => {
|
||||
const id = String(++connIdCounter)
|
||||
const ip = getClientIp(ws, req)
|
||||
const initialData = options.createConnData?.() ?? ({} as TConnData)
|
||||
const id = String(++connIdCounter);
|
||||
const ip = getClientIp(ws, req);
|
||||
const initialData = options.createConnData?.() ?? ({} as TConnData);
|
||||
|
||||
const conn = new ServerConnection<TConnData>({
|
||||
id,
|
||||
@@ -293,64 +320,70 @@ export function serve<P extends ProtocolDef, TConnData = unknown>(
|
||||
socket: ws,
|
||||
initialData,
|
||||
onClose: () => {
|
||||
const idx = connections.indexOf(conn)
|
||||
if (idx !== -1) connections.splice(idx, 1)
|
||||
},
|
||||
})
|
||||
const idx = connections.indexOf(conn);
|
||||
if (idx !== -1) connections.splice(idx, 1);
|
||||
}
|
||||
});
|
||||
|
||||
connections.push(conn)
|
||||
connections.push(conn);
|
||||
|
||||
ws.on('message', (data) => {
|
||||
handleMessage(conn, data as string | Buffer)
|
||||
})
|
||||
handleMessage(conn, data as string | Buffer);
|
||||
});
|
||||
|
||||
ws.on('close', async (code, reason) => {
|
||||
conn._markClosed()
|
||||
const idx = connections.indexOf(conn)
|
||||
if (idx !== -1) connections.splice(idx, 1)
|
||||
await options.onDisconnect?.(conn, reason?.toString())
|
||||
})
|
||||
conn._markClosed();
|
||||
const idx = connections.indexOf(conn);
|
||||
if (idx !== -1) connections.splice(idx, 1);
|
||||
await options.onDisconnect?.(conn, reason?.toString());
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
options.onError?.(err, conn)
|
||||
})
|
||||
options.onError?.(err, conn);
|
||||
});
|
||||
|
||||
await options.onConnect?.(conn)
|
||||
})
|
||||
await options.onConnect?.(conn);
|
||||
});
|
||||
|
||||
wss.on('listening', () => {
|
||||
options.onStart?.(options.port)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
// 如果使用已有的 HTTP 服务器,WebSocketServer 不会触发 listening 事件
|
||||
if (options.server) {
|
||||
options.onStart?.(0); // 端口由 HTTP 服务器管理
|
||||
resolve();
|
||||
} else {
|
||||
wss.on('listening', () => {
|
||||
options.onStart?.(options.port!);
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
async stop() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!wss) {
|
||||
resolve()
|
||||
return
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const conn of connections) {
|
||||
conn.close('Server shutting down')
|
||||
conn.close('Server shutting down');
|
||||
}
|
||||
|
||||
wss.close((err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
})
|
||||
})
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
send(conn, name, data) {
|
||||
const packet: Packet = [PT.Message, name as string, data]
|
||||
;(conn as ServerConnection<TConnData>).send(codec.encode(packet))
|
||||
;(conn as ServerConnection<TConnData>).send(codec.encode(packet));
|
||||
},
|
||||
|
||||
broadcast(name, data, opts) {
|
||||
const packet: Packet = [PT.Message, name as string, data]
|
||||
const encoded = codec.encode(packet)
|
||||
const packet: Packet = [PT.Message, name as string, data];
|
||||
const encoded = codec.encode(packet);
|
||||
|
||||
const excludeSet = new Set(
|
||||
Array.isArray(opts?.exclude)
|
||||
@@ -358,15 +391,15 @@ export function serve<P extends ProtocolDef, TConnData = unknown>(
|
||||
: opts?.exclude
|
||||
? [opts.exclude]
|
||||
: []
|
||||
)
|
||||
);
|
||||
|
||||
for (const conn of connections) {
|
||||
if (!excludeSet.has(conn)) {
|
||||
conn.send(encoded)
|
||||
conn.send(encoded);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return server
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ export interface MsgDef<TData = unknown> {
|
||||
* @en Protocol definition
|
||||
*/
|
||||
export interface ProtocolDef {
|
||||
readonly api: Record<string, ApiDef<any, any>>
|
||||
readonly msg: Record<string, MsgDef<any>>
|
||||
readonly api: Record<string, ApiDef<unknown, unknown>>
|
||||
readonly msg: Record<string, MsgDef<unknown>>
|
||||
}
|
||||
|
||||
// ============ Type Inference ============
|
||||
@@ -39,13 +39,13 @@ export interface ProtocolDef {
|
||||
* @zh 提取 API 输入类型
|
||||
* @en Extract API input type
|
||||
*/
|
||||
export type ApiInput<T> = T extends ApiDef<infer I, any> ? I : never
|
||||
export type ApiInput<T> = T extends ApiDef<infer I, unknown> ? I : never
|
||||
|
||||
/**
|
||||
* @zh 提取 API 输出类型
|
||||
* @en Extract API output type
|
||||
*/
|
||||
export type ApiOutput<T> = T extends ApiDef<any, infer O> ? O : never
|
||||
export type ApiOutput<T> = T extends ApiDef<unknown, infer O> ? O : never
|
||||
|
||||
/**
|
||||
* @zh 提取消息数据类型
|
||||
@@ -120,8 +120,9 @@ export const PacketType = {
|
||||
ApiResponse: 1,
|
||||
ApiError: 2,
|
||||
Message: 3,
|
||||
Heartbeat: 9,
|
||||
} as const
|
||||
Binary: 4,
|
||||
Heartbeat: 9
|
||||
} as const;
|
||||
|
||||
export type PacketType = typeof PacketType[keyof typeof PacketType]
|
||||
|
||||
@@ -173,6 +174,19 @@ export type MessagePacket = [
|
||||
*/
|
||||
export type HeartbeatPacket = [type: typeof PacketType.Heartbeat]
|
||||
|
||||
/**
|
||||
* @zh 二进制数据包
|
||||
* @en Binary data packet
|
||||
*
|
||||
* @zh 用于传输原始二进制数据,如 ECS 状态同步
|
||||
* @en Used for raw binary data transmission, such as ECS state sync
|
||||
*/
|
||||
export type BinaryPacket = [
|
||||
type: typeof PacketType.Binary,
|
||||
channel: number,
|
||||
data: Uint8Array
|
||||
]
|
||||
|
||||
/**
|
||||
* @zh 所有数据包类型
|
||||
* @en All packet types
|
||||
@@ -182,6 +196,7 @@ export type Packet =
|
||||
| ApiResponsePacket
|
||||
| ApiErrorPacket
|
||||
| MessagePacket
|
||||
| BinaryPacket
|
||||
| HeartbeatPacket
|
||||
|
||||
// ============ Error Types ============
|
||||
@@ -196,8 +211,8 @@ export class RpcError extends Error {
|
||||
message: string,
|
||||
public readonly details?: unknown
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'RpcError'
|
||||
super(message);
|
||||
this.name = 'RpcError';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,7 +226,7 @@ export const ErrorCode = {
|
||||
UNAUTHORIZED: 'UNAUTHORIZED',
|
||||
INTERNAL_ERROR: 'INTERNAL_ERROR',
|
||||
TIMEOUT: 'TIMEOUT',
|
||||
CONNECTION_CLOSED: 'CONNECTION_CLOSED',
|
||||
} as const
|
||||
CONNECTION_CLOSED: 'CONNECTION_CLOSED'
|
||||
} as const;
|
||||
|
||||
export type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode]
|
||||
|
||||
@@ -1,5 +1,160 @@
|
||||
# @esengine/server
|
||||
|
||||
## 4.5.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#421](https://github.com/esengine/esengine/pull/421) [`f333b81`](https://github.com/esengine/esengine/commit/f333b81298a386a812b2428d3dcdce03d257fef8) Thanks [@esengine](https://github.com/esengine)! - feat(server): 添加分布式房间支持 | Add distributed room support
|
||||
|
||||
**@esengine/server** - 新增分布式房间管理功能 | Added distributed room management features
|
||||
- 新增 `DistributedRoomManager` 支持多服务器房间管理 | Added `DistributedRoomManager` for multi-server room management
|
||||
- 新增 `MemoryAdapter` 用于测试和单机模式 | Added `MemoryAdapter` for testing and standalone mode
|
||||
- 新增 `RedisAdapter` 用于生产环境多服务器部署 | Added `RedisAdapter` for production multi-server deployments
|
||||
- 新增 `LoadBalancedRouter` 支持 5 种负载均衡策略 | Added `LoadBalancedRouter` with 5 load balancing strategies
|
||||
- round-robin: 轮询 | Round robin
|
||||
- least-rooms: 最少房间数 | Fewest rooms
|
||||
- least-players: 最少玩家数 | Fewest players
|
||||
- random: 随机选择 | Random selection
|
||||
- weighted: 权重(基于容量使用率)| Weighted by capacity usage
|
||||
- `createServer` 新增 `distributed` 配置选项 | Added `distributed` config option to `createServer`
|
||||
- 新增 `$redirect` 消息用于跨服务器玩家重定向 | Added `$redirect` message for cross-server player redirection
|
||||
- 新增故障转移机制,服务器离线时自动恢复房间 | Added failover mechanism for automatic room recovery on server offline
|
||||
- 新增 `room:migrated` 和 `server:draining` 事件类型 | Added `room:migrated` and `server:draining` event types
|
||||
|
||||
## 4.4.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#419](https://github.com/esengine/esengine/pull/419) [`3b6fc82`](https://github.com/esengine/esengine/commit/3b6fc8266fa8e4d43058a44b48bf9169f78de068) Thanks [@esengine](https://github.com/esengine)! - feat(server): HTTP 路由增强 | HTTP router enhancement
|
||||
|
||||
**新功能 | New Features**
|
||||
- 路由参数支持:`/users/:id` → `req.params.id` | Route parameters: `/users/:id` → `req.params.id`
|
||||
- 中间件支持:全局和路由级中间件 | Middleware support: global and route-level
|
||||
- 请求超时控制:全局和路由级超时 | Request timeout: global and route-level
|
||||
|
||||
**内置中间件 | Built-in Middleware**
|
||||
- `requestLogger()` - 请求日志 | Request logging
|
||||
- `bodyLimit()` - 请求体大小限制 | Body size limit
|
||||
- `responseTime()` - 响应时间头 | Response time header
|
||||
- `requestId()` - 请求 ID | Request ID
|
||||
- `securityHeaders()` - 安全头 | Security headers
|
||||
|
||||
## 4.3.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#417](https://github.com/esengine/esengine/pull/417) [`b80e967`](https://github.com/esengine/esengine/commit/b80e96782991b0f5dea65949e5c55325d2775132) Thanks [@esengine](https://github.com/esengine)! - feat(server): HTTP 路由增强 | HTTP router enhancement
|
||||
|
||||
**新功能 | New Features**
|
||||
- 路由参数支持:`/users/:id` → `req.params.id` | Route parameters: `/users/:id` → `req.params.id`
|
||||
- 中间件支持:全局和路由级中间件 | Middleware support: global and route-level
|
||||
- 请求超时控制:全局和路由级超时 | Request timeout: global and route-level
|
||||
|
||||
**内置中间件 | Built-in Middleware**
|
||||
- `requestLogger()` - 请求日志 | Request logging
|
||||
- `bodyLimit()` - 请求体大小限制 | Body size limit
|
||||
- `responseTime()` - 响应时间头 | Response time header
|
||||
- `requestId()` - 请求 ID | Request ID
|
||||
- `securityHeaders()` - 安全头 | Security headers
|
||||
|
||||
## 4.2.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#404](https://github.com/esengine/esengine/pull/404) [`902c0a1`](https://github.com/esengine/esengine/commit/902c0a10749f80bd8f499b44154646379d359704) Thanks [@esengine](https://github.com/esengine)! - feat(server): add HTTP file-based routing support / 添加 HTTP 文件路由支持
|
||||
|
||||
New feature that allows organizing HTTP routes in separate files, similar to API and message handlers.
|
||||
新功能:支持将 HTTP 路由组织在独立文件中,类似于 API 和消息处理器的文件路由方式。
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server';
|
||||
|
||||
export default defineHttp<{ username: string; password: string }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body;
|
||||
res.json({ token: '...', userId: '...' });
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Server configuration / 服务器配置:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
httpDir: 'src/http', // HTTP routes directory / HTTP 路由目录
|
||||
httpPrefix: '/api', // Route prefix / 路由前缀
|
||||
cors: true
|
||||
});
|
||||
```
|
||||
|
||||
File naming convention / 文件命名规则:
|
||||
- `login.ts` → POST /api/login
|
||||
- `users/profile.ts` → POST /api/users/profile
|
||||
- `users/[id].ts` → POST /api/users/:id (dynamic routes / 动态路由)
|
||||
- Set `method: 'GET'` in defineHttp for GET requests / 在 defineHttp 中设置 `method: 'GET'` 以处理 GET 请求
|
||||
|
||||
Also includes / 还包括:
|
||||
- `defineHttp<TBody>()` helper for type-safe route definitions / 类型安全的路由定义辅助函数
|
||||
- Support for merging file routes with inline `http` config / 支持文件路由与内联 `http` 配置合并
|
||||
- RPC server supports attaching to existing HTTP server via `server` option / RPC 服务器支持通过 `server` 选项附加到现有 HTTP 服务器
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`902c0a1`](https://github.com/esengine/esengine/commit/902c0a10749f80bd8f499b44154646379d359704)]:
|
||||
- @esengine/rpc@1.1.3
|
||||
|
||||
## 4.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- feat(server): add HTTP file-based routing support
|
||||
|
||||
New feature that allows organizing HTTP routes in separate files, similar to API and message handlers:
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server';
|
||||
|
||||
export default defineHttp<{ username: string; password: string }>({
|
||||
method: 'POST',
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body;
|
||||
// ... authentication logic
|
||||
res.json({ token: '...', userId: '...' });
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Server configuration:
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
httpDir: 'src/http', // HTTP routes directory
|
||||
httpPrefix: '/api', // Route prefix
|
||||
cors: true
|
||||
});
|
||||
```
|
||||
|
||||
File naming convention:
|
||||
- `login.ts` → POST /api/login
|
||||
- `users/profile.ts` → POST /api/users/profile
|
||||
- `users/[id].ts` → POST /api/users/:id (dynamic routes)
|
||||
- Set `method: 'GET'` in defineHttp for GET requests
|
||||
|
||||
Also includes:
|
||||
- `defineHttp<TBody>()` helper function for type-safe route definitions
|
||||
- Support for merging file routes with inline `http` config
|
||||
- RPC server now supports attaching to existing HTTP server via `server` option
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies []:
|
||||
- @esengine/rpc@1.1.2
|
||||
|
||||
## 4.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@esengine/server",
|
||||
"version": "4.0.0",
|
||||
"version": "4.5.0",
|
||||
"description": "Game server framework for ESEngine with file-based routing",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
@@ -46,23 +46,19 @@
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@esengine/rpc": "workspace:*"
|
||||
"@esengine/rpc": "workspace:*",
|
||||
"@esengine/ecs-framework": "workspace:*"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"ws": ">=8.0.0",
|
||||
"jsonwebtoken": ">=9.0.0",
|
||||
"@esengine/ecs-framework": ">=2.7.1"
|
||||
"jsonwebtoken": ">=9.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"jsonwebtoken": {
|
||||
"optional": true
|
||||
},
|
||||
"@esengine/ecs-framework": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@esengine/ecs-framework": "workspace:*",
|
||||
"@types/jsonwebtoken": "^9.0.0",
|
||||
"@types/node": "^20.0.0",
|
||||
"@types/ws": "^8.5.13",
|
||||
|
||||
@@ -168,9 +168,9 @@ describe('MockAuthProvider', () => {
|
||||
it('should get all users', () => {
|
||||
const users = provider.getUsers();
|
||||
expect(users).toHaveLength(3);
|
||||
expect(users.map(u => u.id)).toContain('1');
|
||||
expect(users.map(u => u.id)).toContain('2');
|
||||
expect(users.map(u => u.id)).toContain('3');
|
||||
expect(users.map((u) => u.id)).toContain('1');
|
||||
expect(users.map((u) => u.id)).toContain('2');
|
||||
expect(users.map((u) => u.id)).toContain('3');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { JwtAuthProvider, createJwtAuthProvider } from '../providers/JwtAuthProvider';
|
||||
import { SessionAuthProvider, createSessionAuthProvider, type ISessionStorage } from '../providers/SessionAuthProvider';
|
||||
|
||||
@@ -125,7 +125,7 @@ describe('JwtAuthProvider', () => {
|
||||
const token = provider.sign({ sub: '123', name: 'Alice' });
|
||||
|
||||
// Wait a bit so iat changes
|
||||
await new Promise(resolve => setTimeout(resolve, 1100));
|
||||
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||
|
||||
const result = await provider.refresh(token);
|
||||
|
||||
@@ -239,7 +239,7 @@ describe('SessionAuthProvider', () => {
|
||||
it('should validate user on verify', async () => {
|
||||
const validatingProvider = createSessionAuthProvider({
|
||||
storage,
|
||||
validateUser: (user) => user.id !== 'banned'
|
||||
validateUser: (user: { id: string; name?: string }) => user.id !== 'banned'
|
||||
});
|
||||
|
||||
const sessionId = await validatingProvider.createSession({ id: 'banned', name: 'Bad User' });
|
||||
@@ -252,7 +252,7 @@ describe('SessionAuthProvider', () => {
|
||||
it('should pass validation for valid user', async () => {
|
||||
const validatingProvider = createSessionAuthProvider({
|
||||
storage,
|
||||
validateUser: (user) => user.id !== 'banned'
|
||||
validateUser: (user: { id: string; name?: string }) => user.id !== 'banned'
|
||||
});
|
||||
|
||||
const sessionId = await validatingProvider.createSession({ id: '123', name: 'Good User' });
|
||||
@@ -269,7 +269,7 @@ describe('SessionAuthProvider', () => {
|
||||
const session1 = await provider.getSession(sessionId);
|
||||
const lastActive1 = session1?.lastActiveAt;
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
const result = await provider.refresh(sessionId);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
@@ -138,7 +138,7 @@ export class AuthContext<TUser = unknown> implements IAuthContext<TUser> {
|
||||
* @en Check if has any of specified roles
|
||||
*/
|
||||
hasAnyRole(roles: string[]): boolean {
|
||||
return roles.some(role => this._roles.includes(role));
|
||||
return roles.some((role) => this._roles.includes(role));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +146,7 @@ export class AuthContext<TUser = unknown> implements IAuthContext<TUser> {
|
||||
* @en Check if has all specified roles
|
||||
*/
|
||||
hasAllRoles(roles: string[]): boolean {
|
||||
return roles.every(role => this._roles.includes(role));
|
||||
return roles.every((role) => this._roles.includes(role));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import type { ServerConnection, GameServer } from '../../types/index.js';
|
||||
import { createLogger } from '../../logger.js';
|
||||
import type {
|
||||
IAuthProvider,
|
||||
AuthResult,
|
||||
@@ -14,6 +15,8 @@ import type {
|
||||
} from '../types.js';
|
||||
import { AuthContext } from '../context.js';
|
||||
|
||||
const logger = createLogger('Auth');
|
||||
|
||||
/**
|
||||
* @zh 认证数据键
|
||||
* @en Auth data key
|
||||
@@ -155,7 +158,7 @@ export function withAuth<TUser = unknown>(
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Auth] Error during auto-authentication:', error);
|
||||
logger.error('Error during auto-authentication:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,12 @@
|
||||
|
||||
import type { Room, Player } from '../../room/index.js';
|
||||
import type { IAuthContext, AuthRoomConfig } from '../types.js';
|
||||
import { createLogger } from '../../logger.js';
|
||||
import { getAuthContext } from './withAuth.js';
|
||||
import { createGuestContext } from '../context.js';
|
||||
|
||||
const logger = createLogger('AuthRoom');
|
||||
|
||||
/**
|
||||
* @zh 带认证的玩家
|
||||
* @en Player with authentication
|
||||
@@ -181,7 +184,7 @@ export function withRoomAuth<TUser = unknown, TBase extends new (...args: any[])
|
||||
: createGuestContext<TUser>();
|
||||
|
||||
if (requireAuth && !authContext.isAuthenticated) {
|
||||
console.warn(`[AuthRoom] Rejected unauthenticated player: ${player.id}`);
|
||||
logger.warn(`Rejected unauthenticated player: ${player.id}`);
|
||||
this.kick(player as any, 'Authentication required');
|
||||
return;
|
||||
}
|
||||
@@ -192,7 +195,7 @@ export function withRoomAuth<TUser = unknown, TBase extends new (...args: any[])
|
||||
: authContext.hasAllRoles(allowedRoles);
|
||||
|
||||
if (!hasRole) {
|
||||
console.warn(`[AuthRoom] Rejected player ${player.id}: insufficient roles`);
|
||||
logger.warn(`Rejected player ${player.id}: insufficient roles`);
|
||||
this.kick(player as any, 'Insufficient permissions');
|
||||
return;
|
||||
}
|
||||
@@ -204,12 +207,12 @@ export function withRoomAuth<TUser = unknown, TBase extends new (...args: any[])
|
||||
try {
|
||||
const allowed = await this.onAuth(authPlayer);
|
||||
if (!allowed) {
|
||||
console.warn(`[AuthRoom] Rejected player ${player.id}: onAuth returned false`);
|
||||
logger.warn(`Rejected player ${player.id}: onAuth returned false`);
|
||||
this.kick(player as any, 'Authentication rejected');
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[AuthRoom] Error in onAuth for player ${player.id}:`, error);
|
||||
logger.error(`Error in onAuth for player ${player.id}:`, error);
|
||||
this.kick(player as any, 'Authentication error');
|
||||
return;
|
||||
}
|
||||
@@ -242,7 +245,7 @@ export function withRoomAuth<TUser = unknown, TBase extends new (...args: any[])
|
||||
* @en Get players by role
|
||||
*/
|
||||
getPlayersByRole(role: string): AuthPlayer<TUser>[] {
|
||||
return this.getAuthPlayers().filter(p => p.auth?.hasRole(role));
|
||||
return this.getAuthPlayers().filter((p) => p.auth?.hasRole(role));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -250,7 +253,7 @@ export function withRoomAuth<TUser = unknown, TBase extends new (...args: any[])
|
||||
* @en Get player by user ID
|
||||
*/
|
||||
getPlayerByUserId(userId: string): AuthPlayer<TUser> | undefined {
|
||||
return this.getAuthPlayers().find(p => p.auth?.userId === userId);
|
||||
return this.getAuthPlayers().find((p) => p.auth?.userId === userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,7 +284,7 @@ export function withRoomAuth<TUser = unknown, TBase extends new (...args: any[])
|
||||
* ```
|
||||
*/
|
||||
export abstract class AuthRoomBase<TUser = unknown, TState = any, TPlayerData = Record<string, unknown>>
|
||||
implements IAuthRoom<TUser> {
|
||||
implements IAuthRoom<TUser> {
|
||||
|
||||
/**
|
||||
* @zh 认证配置(子类可覆盖)
|
||||
|
||||
@@ -77,7 +77,7 @@ export interface MockAuthConfig {
|
||||
* ```
|
||||
*/
|
||||
export class MockAuthProvider<TUser extends MockUser = MockUser>
|
||||
implements IAuthProvider<TUser, string> {
|
||||
implements IAuthProvider<TUser, string> {
|
||||
|
||||
readonly name = 'mock';
|
||||
|
||||
@@ -102,7 +102,7 @@ export class MockAuthProvider<TUser extends MockUser = MockUser>
|
||||
*/
|
||||
private async _delay(): Promise<void> {
|
||||
if (this._config.delay && this._config.delay > 0) {
|
||||
await new Promise(resolve => setTimeout(resolve, this._config.delay));
|
||||
await new Promise((resolve) => setTimeout(resolve, this._config.delay));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
* @en Game server core
|
||||
*/
|
||||
|
||||
import * as path from 'node:path'
|
||||
import { serve, type RpcServer } from '@esengine/rpc/server'
|
||||
import { rpc } from '@esengine/rpc'
|
||||
import * as path from 'node:path';
|
||||
import { createServer as createHttpServer, type Server as HttpServer } from 'node:http';
|
||||
import { serve, type RpcServer } from '@esengine/rpc/server';
|
||||
import { rpc } from '@esengine/rpc';
|
||||
import { createLogger } from '../logger.js';
|
||||
import type {
|
||||
ServerConfig,
|
||||
ServerConnection,
|
||||
@@ -14,20 +16,28 @@ import type {
|
||||
MsgContext,
|
||||
LoadedApiHandler,
|
||||
LoadedMsgHandler,
|
||||
} from '../types/index.js'
|
||||
import { loadApiHandlers, loadMsgHandlers } from '../router/loader.js'
|
||||
import { RoomManager, type RoomClass, type Room } from '../room/index.js'
|
||||
LoadedHttpHandler
|
||||
} from '../types/index.js';
|
||||
import type { HttpRoutes, HttpHandler } from '../http/types.js';
|
||||
import type { Validator } from '../schema/index.js';
|
||||
import { loadApiHandlers, loadMsgHandlers, loadHttpHandlers } from '../router/loader.js';
|
||||
import { RoomManager, type RoomClass, type Room } from '../room/index.js';
|
||||
import { createHttpRouter } from '../http/router.js';
|
||||
import { DistributedRoomManager } from '../distributed/DistributedRoomManager.js';
|
||||
import { MemoryAdapter } from '../distributed/adapters/MemoryAdapter.js';
|
||||
|
||||
/**
|
||||
* @zh 默认配置
|
||||
* @en Default configuration
|
||||
*/
|
||||
const DEFAULT_CONFIG: Required<Omit<ServerConfig, 'onStart' | 'onConnect' | 'onDisconnect'>> = {
|
||||
const DEFAULT_CONFIG: Required<Omit<ServerConfig, 'onStart' | 'onConnect' | 'onDisconnect' | 'http' | 'cors' | 'httpDir' | 'httpPrefix' | 'distributed'>> & { httpDir: string; httpPrefix: string } = {
|
||||
port: 3000,
|
||||
apiDir: 'src/api',
|
||||
msgDir: 'src/msg',
|
||||
tickRate: 20,
|
||||
}
|
||||
httpDir: 'src/http',
|
||||
httpPrefix: '/api',
|
||||
tickRate: 20
|
||||
};
|
||||
|
||||
/**
|
||||
* @zh 创建游戏服务器
|
||||
@@ -49,63 +59,147 @@ const DEFAULT_CONFIG: Required<Omit<ServerConfig, 'onStart' | 'onConnect' | 'onD
|
||||
* ```
|
||||
*/
|
||||
export async function createServer(config: ServerConfig = {}): Promise<GameServer> {
|
||||
const opts = { ...DEFAULT_CONFIG, ...config }
|
||||
const cwd = process.cwd()
|
||||
const opts = { ...DEFAULT_CONFIG, ...config };
|
||||
const cwd = process.cwd();
|
||||
const logger = createLogger('Server');
|
||||
|
||||
// 加载文件路由处理器
|
||||
const apiHandlers = await loadApiHandlers(path.resolve(cwd, opts.apiDir))
|
||||
const msgHandlers = await loadMsgHandlers(path.resolve(cwd, opts.msgDir))
|
||||
const apiHandlers = await loadApiHandlers(path.resolve(cwd, opts.apiDir));
|
||||
const msgHandlers = await loadMsgHandlers(path.resolve(cwd, opts.msgDir));
|
||||
|
||||
// 加载 HTTP 文件路由
|
||||
const httpDir = config.httpDir ?? opts.httpDir;
|
||||
const httpPrefix = config.httpPrefix ?? opts.httpPrefix;
|
||||
const httpHandlers = await loadHttpHandlers(path.resolve(cwd, httpDir), httpPrefix);
|
||||
|
||||
if (apiHandlers.length > 0) {
|
||||
console.log(`[Server] Loaded ${apiHandlers.length} API handlers`)
|
||||
logger.info(`Loaded ${apiHandlers.length} API handlers`);
|
||||
}
|
||||
if (msgHandlers.length > 0) {
|
||||
console.log(`[Server] Loaded ${msgHandlers.length} message handlers`)
|
||||
logger.info(`Loaded ${msgHandlers.length} message handlers`);
|
||||
}
|
||||
if (httpHandlers.length > 0) {
|
||||
logger.info(`Loaded ${httpHandlers.length} HTTP handlers`);
|
||||
}
|
||||
|
||||
// 合并 HTTP 路由(文件路由 + 内联路由)
|
||||
const mergedHttpRoutes: HttpRoutes = {};
|
||||
|
||||
// 先添加文件路由
|
||||
for (const handler of httpHandlers) {
|
||||
const existingRoute = mergedHttpRoutes[handler.route];
|
||||
if (existingRoute && typeof existingRoute !== 'function') {
|
||||
(existingRoute as Record<string, HttpHandler>)[handler.method] = handler.definition.handler;
|
||||
} else {
|
||||
mergedHttpRoutes[handler.route] = {
|
||||
[handler.method]: handler.definition.handler
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 再添加内联路由(覆盖文件路由)
|
||||
if (config.http) {
|
||||
for (const [route, handlerOrMethods] of Object.entries(config.http)) {
|
||||
if (typeof handlerOrMethods === 'function') {
|
||||
mergedHttpRoutes[route] = handlerOrMethods;
|
||||
} else {
|
||||
const existing = mergedHttpRoutes[route];
|
||||
if (existing && typeof existing !== 'function') {
|
||||
Object.assign(existing, handlerOrMethods);
|
||||
} else {
|
||||
mergedHttpRoutes[route] = handlerOrMethods;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const hasHttpRoutes = Object.keys(mergedHttpRoutes).length > 0;
|
||||
|
||||
// 分布式模式配置
|
||||
const distributedConfig = config.distributed;
|
||||
const isDistributed = distributedConfig?.enabled ?? false;
|
||||
|
||||
// 动态构建协议
|
||||
const apiDefs: Record<string, ReturnType<typeof rpc.api>> = {
|
||||
// 内置 API
|
||||
JoinRoom: rpc.api(),
|
||||
LeaveRoom: rpc.api(),
|
||||
}
|
||||
LeaveRoom: rpc.api()
|
||||
};
|
||||
const msgDefs: Record<string, ReturnType<typeof rpc.msg>> = {
|
||||
// 内置消息(房间消息透传)
|
||||
RoomMessage: rpc.msg(),
|
||||
}
|
||||
// 分布式重定向消息
|
||||
$redirect: rpc.msg()
|
||||
};
|
||||
|
||||
for (const handler of apiHandlers) {
|
||||
apiDefs[handler.name] = rpc.api()
|
||||
apiDefs[handler.name] = rpc.api();
|
||||
}
|
||||
for (const handler of msgHandlers) {
|
||||
msgDefs[handler.name] = rpc.msg()
|
||||
msgDefs[handler.name] = rpc.msg();
|
||||
}
|
||||
|
||||
const protocol = rpc.define({
|
||||
api: apiDefs,
|
||||
msg: msgDefs,
|
||||
})
|
||||
msg: msgDefs
|
||||
});
|
||||
|
||||
// 服务器状态
|
||||
let currentTick = 0
|
||||
let tickInterval: ReturnType<typeof setInterval> | null = null
|
||||
let rpcServer: RpcServer<typeof protocol, Record<string, unknown>> | null = null
|
||||
let currentTick = 0;
|
||||
let tickInterval: ReturnType<typeof setInterval> | null = null;
|
||||
let rpcServer: RpcServer<typeof protocol, Record<string, unknown>> | null = null;
|
||||
let httpServer: HttpServer | null = null;
|
||||
|
||||
// 发送函数(延迟绑定,因为 rpcServer 在 start() 后才创建)
|
||||
const sendFn = (conn: any, type: string, data: unknown) => {
|
||||
rpcServer?.send(conn, 'RoomMessage' as any, { type, data } as any);
|
||||
};
|
||||
|
||||
// 二进制发送函数(使用原生 WebSocket 二进制帧,效率更高)
|
||||
const sendBinaryFn = (conn: any, data: Uint8Array) => {
|
||||
if (conn && typeof conn.sendBinary === 'function') {
|
||||
conn.sendBinary(data);
|
||||
}
|
||||
};
|
||||
|
||||
// 房间管理器(立即初始化,以便 define() 可在 start() 前调用)
|
||||
const roomManager = new RoomManager((conn, type, data) => {
|
||||
rpcServer?.send(conn, 'RoomMessage' as any, { type, data } as any)
|
||||
})
|
||||
let roomManager: RoomManager | DistributedRoomManager;
|
||||
let distributedManager: DistributedRoomManager | null = null;
|
||||
|
||||
if (isDistributed && distributedConfig) {
|
||||
// 分布式模式
|
||||
const adapter = distributedConfig.adapter ?? new MemoryAdapter();
|
||||
distributedManager = new DistributedRoomManager(
|
||||
adapter,
|
||||
{
|
||||
serverId: distributedConfig.serverId,
|
||||
serverAddress: distributedConfig.serverAddress,
|
||||
serverPort: distributedConfig.serverPort ?? opts.port,
|
||||
heartbeatInterval: distributedConfig.heartbeatInterval,
|
||||
snapshotInterval: distributedConfig.snapshotInterval,
|
||||
enableFailover: distributedConfig.enableFailover,
|
||||
capacity: distributedConfig.capacity
|
||||
},
|
||||
sendFn,
|
||||
sendBinaryFn
|
||||
);
|
||||
roomManager = distributedManager;
|
||||
logger.info(`Distributed mode enabled (serverId: ${distributedConfig.serverId})`);
|
||||
} else {
|
||||
// 单机模式
|
||||
roomManager = new RoomManager(sendFn, sendBinaryFn);
|
||||
}
|
||||
|
||||
// 构建 API 处理器映射
|
||||
const apiMap: Record<string, LoadedApiHandler> = {}
|
||||
const apiMap: Record<string, LoadedApiHandler> = {};
|
||||
for (const handler of apiHandlers) {
|
||||
apiMap[handler.name] = handler
|
||||
apiMap[handler.name] = handler;
|
||||
}
|
||||
|
||||
// 构建消息处理器映射
|
||||
const msgMap: Record<string, LoadedMsgHandler> = {}
|
||||
const msgMap: Record<string, LoadedMsgHandler> = {};
|
||||
for (const handler of msgHandlers) {
|
||||
msgMap[handler.name] = handler
|
||||
msgMap[handler.name] = handler;
|
||||
}
|
||||
|
||||
// 游戏服务器实例
|
||||
@@ -113,15 +207,15 @@ export async function createServer(config: ServerConfig = {}): Promise<GameServe
|
||||
rooms: RoomManager
|
||||
} = {
|
||||
get connections() {
|
||||
return (rpcServer?.connections ?? []) as ReadonlyArray<ServerConnection>
|
||||
return (rpcServer?.connections ?? []) as ReadonlyArray<ServerConnection>;
|
||||
},
|
||||
|
||||
get tick() {
|
||||
return currentTick
|
||||
return currentTick;
|
||||
},
|
||||
|
||||
get rooms() {
|
||||
return roomManager
|
||||
return roomManager;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -129,12 +223,12 @@ export async function createServer(config: ServerConfig = {}): Promise<GameServe
|
||||
* @en Define room type
|
||||
*/
|
||||
define(name: string, roomClass: new () => unknown): void {
|
||||
roomManager.define(name, roomClass as RoomClass)
|
||||
roomManager.define(name, roomClass as RoomClass);
|
||||
},
|
||||
|
||||
async start() {
|
||||
// 构建 API handlers
|
||||
const apiHandlersObj: Record<string, (input: unknown, conn: any) => Promise<unknown>> = {}
|
||||
const apiHandlersObj: Record<string, (input: unknown, conn: any) => Promise<unknown>> = {};
|
||||
|
||||
// 内置 JoinRoom API
|
||||
apiHandlersObj['JoinRoom'] = async (input: any, conn) => {
|
||||
@@ -142,112 +236,227 @@ export async function createServer(config: ServerConfig = {}): Promise<GameServe
|
||||
roomType?: string
|
||||
roomId?: string
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
};
|
||||
|
||||
if (roomId) {
|
||||
const result = await roomManager.joinById(roomId, conn.id, conn)
|
||||
const result = await roomManager.joinById(roomId, conn.id, conn);
|
||||
if (!result) {
|
||||
throw new Error('Failed to join room')
|
||||
throw new Error('Failed to join room');
|
||||
}
|
||||
return { roomId: result.room.id, playerId: result.player.id }
|
||||
return { roomId: result.room.id, playerId: result.player.id };
|
||||
}
|
||||
|
||||
if (roomType) {
|
||||
const result = await roomManager.joinOrCreate(roomType, conn.id, conn, options)
|
||||
if (!result) {
|
||||
throw new Error('Failed to join or create room')
|
||||
// 分布式模式:使用 joinOrCreateDistributed
|
||||
if (distributedManager) {
|
||||
const result = await distributedManager.joinOrCreateDistributed(
|
||||
roomType,
|
||||
conn.id,
|
||||
conn,
|
||||
options
|
||||
);
|
||||
if (!result) {
|
||||
throw new Error('Failed to join or create room');
|
||||
}
|
||||
if ('redirect' in result) {
|
||||
// 发送重定向消息给客户端
|
||||
rpcServer?.send(conn, '$redirect' as any, {
|
||||
address: result.redirect,
|
||||
roomType
|
||||
} as any);
|
||||
return { redirect: result.redirect };
|
||||
}
|
||||
return { roomId: result.room.id, playerId: result.player.id };
|
||||
}
|
||||
return { roomId: result.room.id, playerId: result.player.id }
|
||||
|
||||
// 单机模式
|
||||
const result = await roomManager.joinOrCreate(roomType, conn.id, conn, options);
|
||||
if (!result) {
|
||||
throw new Error('Failed to join or create room');
|
||||
}
|
||||
return { roomId: result.room.id, playerId: result.player.id };
|
||||
}
|
||||
|
||||
throw new Error('roomType or roomId required')
|
||||
}
|
||||
throw new Error('roomType or roomId required');
|
||||
};
|
||||
|
||||
// 内置 LeaveRoom API
|
||||
apiHandlersObj['LeaveRoom'] = async (_input, conn) => {
|
||||
await roomManager.leave(conn.id)
|
||||
return { success: true }
|
||||
}
|
||||
await roomManager.leave(conn.id);
|
||||
return { success: true };
|
||||
};
|
||||
|
||||
// 文件路由 API
|
||||
for (const [name, handler] of Object.entries(apiMap)) {
|
||||
apiHandlersObj[name] = async (input, conn) => {
|
||||
const ctx: ApiContext = {
|
||||
conn: conn as ServerConnection,
|
||||
server: gameServer,
|
||||
server: gameServer
|
||||
};
|
||||
|
||||
const definition = handler.definition as { schema?: Validator<unknown> };
|
||||
if (definition.schema) {
|
||||
const result = definition.schema.validate(input);
|
||||
if (!result.success) {
|
||||
const pathStr = result.error.path.length > 0
|
||||
? ` at "${result.error.path.join('.')}"`
|
||||
: '';
|
||||
throw new Error(`Validation failed${pathStr}: ${result.error.message}`);
|
||||
}
|
||||
return handler.definition.handler(result.data, ctx);
|
||||
}
|
||||
return handler.definition.handler(input, ctx)
|
||||
}
|
||||
|
||||
return handler.definition.handler(input, ctx);
|
||||
};
|
||||
}
|
||||
|
||||
// 构建消息 handlers
|
||||
const msgHandlersObj: Record<string, (data: unknown, conn: any) => void | Promise<void>> = {}
|
||||
const msgHandlersObj: Record<string, (data: unknown, conn: any) => void | Promise<void>> = {};
|
||||
|
||||
// 内置 RoomMessage 处理
|
||||
msgHandlersObj['RoomMessage'] = async (data: any, conn) => {
|
||||
const { type, data: payload } = data as { type: string; data: unknown }
|
||||
roomManager.handleMessage(conn.id, type, payload)
|
||||
}
|
||||
const { type, data: payload } = data as { type: string; data: unknown };
|
||||
roomManager.handleMessage(conn.id, type, payload);
|
||||
};
|
||||
|
||||
// 文件路由消息
|
||||
for (const [name, handler] of Object.entries(msgMap)) {
|
||||
msgHandlersObj[name] = async (data, conn) => {
|
||||
const ctx: MsgContext = {
|
||||
conn: conn as ServerConnection,
|
||||
server: gameServer,
|
||||
server: gameServer
|
||||
};
|
||||
|
||||
const definition = handler.definition as { schema?: Validator<unknown> };
|
||||
if (definition.schema) {
|
||||
const result = definition.schema.validate(data);
|
||||
if (!result.success) {
|
||||
const pathStr = result.error.path.length > 0
|
||||
? ` at "${result.error.path.join('.')}"`
|
||||
: '';
|
||||
logger.warn(`Message validation failed for ${name}${pathStr}: ${result.error.message}`);
|
||||
return;
|
||||
}
|
||||
await handler.definition.handler(result.data, ctx);
|
||||
return;
|
||||
}
|
||||
await handler.definition.handler(data, ctx)
|
||||
}
|
||||
|
||||
await handler.definition.handler(data, ctx);
|
||||
};
|
||||
}
|
||||
|
||||
rpcServer = serve(protocol, {
|
||||
port: opts.port,
|
||||
createConnData: () => ({}),
|
||||
onStart: (p) => {
|
||||
console.log(`[Server] Started on ws://localhost:${p}`)
|
||||
opts.onStart?.(p)
|
||||
},
|
||||
onConnect: async (conn) => {
|
||||
await config.onConnect?.(conn as ServerConnection)
|
||||
},
|
||||
onDisconnect: async (conn) => {
|
||||
// 玩家断线时自动离开房间
|
||||
await roomManager?.leave(conn.id, 'disconnected')
|
||||
await config.onDisconnect?.(conn as ServerConnection)
|
||||
},
|
||||
api: apiHandlersObj as any,
|
||||
msg: msgHandlersObj as any,
|
||||
})
|
||||
// 如果有 HTTP 路由,创建 HTTP 服务器
|
||||
if (hasHttpRoutes) {
|
||||
const httpRouter = createHttpRouter(mergedHttpRoutes, {
|
||||
cors: config.cors ?? true
|
||||
});
|
||||
|
||||
await rpcServer.start()
|
||||
httpServer = createHttpServer(async (req, res) => {
|
||||
// 先尝试 HTTP 路由
|
||||
const handled = await httpRouter(req, res);
|
||||
if (!handled) {
|
||||
// 未匹配的请求返回 404
|
||||
res.statusCode = 404;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||
}
|
||||
});
|
||||
|
||||
// 使用 HTTP 服务器创建 RPC
|
||||
rpcServer = serve(protocol, {
|
||||
server: httpServer,
|
||||
createConnData: () => ({}),
|
||||
onStart: () => {
|
||||
logger.info(`Started on http://localhost:${opts.port}`);
|
||||
opts.onStart?.(opts.port);
|
||||
},
|
||||
onConnect: async (conn) => {
|
||||
await config.onConnect?.(conn as ServerConnection);
|
||||
},
|
||||
onDisconnect: async (conn) => {
|
||||
await roomManager?.leave(conn.id, 'disconnected');
|
||||
await config.onDisconnect?.(conn as ServerConnection);
|
||||
},
|
||||
api: apiHandlersObj as any,
|
||||
msg: msgHandlersObj as any
|
||||
});
|
||||
|
||||
await rpcServer.start();
|
||||
|
||||
// 启动 HTTP 服务器
|
||||
await new Promise<void>((resolve) => {
|
||||
httpServer!.listen(opts.port, () => resolve());
|
||||
});
|
||||
} else {
|
||||
// 仅 WebSocket 模式
|
||||
rpcServer = serve(protocol, {
|
||||
port: opts.port,
|
||||
createConnData: () => ({}),
|
||||
onStart: (p) => {
|
||||
logger.info(`Started on ws://localhost:${p}`);
|
||||
opts.onStart?.(p);
|
||||
},
|
||||
onConnect: async (conn) => {
|
||||
await config.onConnect?.(conn as ServerConnection);
|
||||
},
|
||||
onDisconnect: async (conn) => {
|
||||
await roomManager?.leave(conn.id, 'disconnected');
|
||||
await config.onDisconnect?.(conn as ServerConnection);
|
||||
},
|
||||
api: apiHandlersObj as any,
|
||||
msg: msgHandlersObj as any
|
||||
});
|
||||
|
||||
await rpcServer.start();
|
||||
}
|
||||
|
||||
// 启动分布式管理器
|
||||
if (distributedManager) {
|
||||
await distributedManager.start();
|
||||
}
|
||||
|
||||
// 启动 tick 循环
|
||||
if (opts.tickRate > 0) {
|
||||
tickInterval = setInterval(() => {
|
||||
currentTick++
|
||||
}, 1000 / opts.tickRate)
|
||||
currentTick++;
|
||||
}, 1000 / opts.tickRate);
|
||||
}
|
||||
},
|
||||
|
||||
async stop() {
|
||||
if (tickInterval) {
|
||||
clearInterval(tickInterval)
|
||||
tickInterval = null
|
||||
clearInterval(tickInterval);
|
||||
tickInterval = null;
|
||||
}
|
||||
|
||||
// 停止分布式管理器(优雅关闭)
|
||||
if (distributedManager) {
|
||||
await distributedManager.stop(true);
|
||||
}
|
||||
|
||||
if (rpcServer) {
|
||||
await rpcServer.stop()
|
||||
rpcServer = null
|
||||
await rpcServer.stop();
|
||||
rpcServer = null;
|
||||
}
|
||||
if (httpServer) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
httpServer!.close((err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
httpServer = null;
|
||||
}
|
||||
},
|
||||
|
||||
broadcast(name, data) {
|
||||
rpcServer?.broadcast(name as any, data as any)
|
||||
rpcServer?.broadcast(name as any, data as any);
|
||||
},
|
||||
|
||||
send(conn, name, data) {
|
||||
rpcServer?.send(conn as any, name as any, data as any)
|
||||
},
|
||||
}
|
||||
rpcServer?.send(conn as any, name as any, data as any);
|
||||
}
|
||||
};
|
||||
|
||||
return gameServer as GameServer
|
||||
return gameServer as GameServer;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,707 @@
|
||||
/**
|
||||
* @zh 分布式房间管理器
|
||||
* @en Distributed room manager
|
||||
*
|
||||
* @zh 继承 RoomManager,添加分布式功能支持。包括跨服务器房间注册、
|
||||
* 玩家路由、状态同步和故障转移。
|
||||
* @en Extends RoomManager with distributed features. Includes cross-server room
|
||||
* registration, player routing, state synchronization, and failover.
|
||||
*/
|
||||
|
||||
import { RoomManager } from '../room/RoomManager.js';
|
||||
import { Room, type RoomOptions } from '../room/Room.js';
|
||||
import type { Player } from '../room/Player.js';
|
||||
import type { IDistributedAdapter } from './adapters/IDistributedAdapter.js';
|
||||
import type {
|
||||
DistributedRoomManagerConfig,
|
||||
RoomRegistration,
|
||||
RoutingResult,
|
||||
RoutingRequest,
|
||||
ServerRegistration,
|
||||
DistributedEvent,
|
||||
Unsubscribe
|
||||
} from './types.js';
|
||||
import { createLogger } from '../logger.js';
|
||||
|
||||
const logger = createLogger('DistributedRoom');
|
||||
|
||||
/**
|
||||
* @zh 分布式房间管理器配置(内部使用)
|
||||
* @en Distributed room manager configuration (internal use)
|
||||
*/
|
||||
interface InternalConfig extends Required<Omit<DistributedRoomManagerConfig, 'metadata'>> {
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 分布式房间管理器
|
||||
* @en Distributed room manager
|
||||
*
|
||||
* @zh 扩展基础 RoomManager,添加以下功能:
|
||||
* - 服务器注册和心跳
|
||||
* - 跨服务器房间注册
|
||||
* - 玩家路由和重定向
|
||||
* - 状态快照和恢复
|
||||
* - 分布式锁防止竞态
|
||||
* @en Extends base RoomManager with:
|
||||
* - Server registration and heartbeat
|
||||
* - Cross-server room registration
|
||||
* - Player routing and redirection
|
||||
* - State snapshots and recovery
|
||||
* - Distributed locks to prevent race conditions
|
||||
*/
|
||||
export class DistributedRoomManager extends RoomManager {
|
||||
private readonly _adapter: IDistributedAdapter;
|
||||
private readonly _config: InternalConfig;
|
||||
private readonly _serverId: string;
|
||||
|
||||
private _heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private _snapshotTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private _subscriptions: Unsubscribe[] = [];
|
||||
private _isShuttingDown = false;
|
||||
|
||||
/**
|
||||
* @zh 创建分布式房间管理器
|
||||
* @en Create distributed room manager
|
||||
*
|
||||
* @param adapter - 分布式适配器 | Distributed adapter
|
||||
* @param config - 配置 | Configuration
|
||||
* @param sendFn - 消息发送函数 | Message send function
|
||||
* @param sendBinaryFn - 二进制发送函数 | Binary send function
|
||||
*/
|
||||
constructor(
|
||||
adapter: IDistributedAdapter,
|
||||
config: DistributedRoomManagerConfig,
|
||||
sendFn: (conn: any, type: string, data: unknown) => void,
|
||||
sendBinaryFn?: (conn: any, data: Uint8Array) => void
|
||||
) {
|
||||
super(sendFn, sendBinaryFn);
|
||||
|
||||
this._adapter = adapter;
|
||||
this._serverId = config.serverId;
|
||||
|
||||
this._config = {
|
||||
serverId: config.serverId,
|
||||
serverAddress: config.serverAddress,
|
||||
serverPort: config.serverPort,
|
||||
heartbeatInterval: config.heartbeatInterval ?? 5000,
|
||||
snapshotInterval: config.snapshotInterval ?? 30000,
|
||||
migrationTimeout: config.migrationTimeout ?? 10000,
|
||||
enableFailover: config.enableFailover ?? true,
|
||||
capacity: config.capacity ?? 100,
|
||||
metadata: config.metadata ?? {}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取服务器 ID
|
||||
* @en Get server ID
|
||||
*/
|
||||
get serverId(): string {
|
||||
return this._serverId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取分布式适配器
|
||||
* @en Get distributed adapter
|
||||
*/
|
||||
get adapter(): IDistributedAdapter {
|
||||
return this._adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取配置
|
||||
* @en Get configuration
|
||||
*/
|
||||
get config(): Readonly<InternalConfig> {
|
||||
return this._config;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 启动分布式房间管理器
|
||||
* @en Start distributed room manager
|
||||
*/
|
||||
async start(): Promise<void> {
|
||||
if (!this._adapter.isConnected()) {
|
||||
await this._adapter.connect();
|
||||
}
|
||||
|
||||
// 注册服务器 | Register server
|
||||
await this._registerServer();
|
||||
|
||||
// 订阅事件 | Subscribe to events
|
||||
await this._subscribeToEvents();
|
||||
|
||||
// 启动心跳 | Start heartbeat
|
||||
this._startHeartbeat();
|
||||
|
||||
// 启动快照(如果启用)| Start snapshots (if enabled)
|
||||
if (this._config.snapshotInterval > 0) {
|
||||
this._startSnapshotTimer();
|
||||
}
|
||||
|
||||
logger.info(`Distributed room manager started: ${this._serverId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 停止分布式房间管理器
|
||||
* @en Stop distributed room manager
|
||||
*
|
||||
* @param graceful - 是否优雅关闭(等待玩家退出)| Whether to gracefully shutdown (wait for players)
|
||||
*/
|
||||
async stop(graceful = true): Promise<void> {
|
||||
this._isShuttingDown = true;
|
||||
|
||||
// 停止定时器 | Stop timers
|
||||
if (this._heartbeatTimer) {
|
||||
clearInterval(this._heartbeatTimer);
|
||||
this._heartbeatTimer = null;
|
||||
}
|
||||
|
||||
if (this._snapshotTimer) {
|
||||
clearInterval(this._snapshotTimer);
|
||||
this._snapshotTimer = null;
|
||||
}
|
||||
|
||||
// 取消订阅 | Unsubscribe
|
||||
for (const unsub of this._subscriptions) {
|
||||
unsub();
|
||||
}
|
||||
this._subscriptions = [];
|
||||
|
||||
if (graceful) {
|
||||
// 标记为 draining,停止接收新玩家 | Mark as draining, stop accepting new players
|
||||
await this._adapter.updateServer(this._serverId, { status: 'draining' });
|
||||
|
||||
// 保存所有房间状态快照 | Save all room state snapshots
|
||||
await this._saveAllSnapshots();
|
||||
}
|
||||
|
||||
// 注销服务器 | Unregister server
|
||||
await this._adapter.unregisterServer(this._serverId);
|
||||
|
||||
logger.info(`Distributed room manager stopped: ${this._serverId}`);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 房间操作覆盖 | Room Operation Overrides
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 房间创建后注册到分布式系统
|
||||
* @en Register room to distributed system after creation
|
||||
*/
|
||||
protected override async _onRoomCreated(name: string, room: Room): Promise<void> {
|
||||
const registration: RoomRegistration = {
|
||||
roomId: room.id,
|
||||
roomType: name,
|
||||
serverId: this._serverId,
|
||||
serverAddress: `${this._config.serverAddress}:${this._config.serverPort}`,
|
||||
playerCount: room.players.length,
|
||||
maxPlayers: room.maxPlayers,
|
||||
isLocked: room.isLocked,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
|
||||
await this._adapter.registerRoom(registration);
|
||||
logger.debug(`Registered room: ${room.id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 房间销毁时从分布式系统注销
|
||||
* @en Unregister room from distributed system when disposed
|
||||
*/
|
||||
protected override _onRoomDisposed(roomId: string): void {
|
||||
super._onRoomDisposed(roomId);
|
||||
|
||||
// 异步注销房间 | Async unregister room
|
||||
this._adapter.unregisterRoom(roomId).catch(err => {
|
||||
logger.error(`Failed to unregister room ${roomId}:`, err);
|
||||
});
|
||||
|
||||
// 删除快照 | Delete snapshot
|
||||
this._adapter.deleteSnapshot(roomId).catch(err => {
|
||||
logger.error(`Failed to delete snapshot for ${roomId}:`, err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 玩家加入后更新分布式房间信息
|
||||
* @en Update distributed room info after player joins
|
||||
*/
|
||||
protected override _onPlayerJoined(playerId: string, roomId: string, player: Player): void {
|
||||
super._onPlayerJoined(playerId, roomId, player);
|
||||
|
||||
const room = this._rooms.get(roomId);
|
||||
if (room) {
|
||||
this._adapter.updateRoom(roomId, {
|
||||
playerCount: room.players.length,
|
||||
updatedAt: Date.now()
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to update room ${roomId}:`, err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 玩家离开后更新分布式房间信息
|
||||
* @en Update distributed room info after player leaves
|
||||
*/
|
||||
protected override _onPlayerLeft(playerId: string, roomId: string): void {
|
||||
super._onPlayerLeft(playerId, roomId);
|
||||
|
||||
const room = this._rooms.get(roomId);
|
||||
if (room) {
|
||||
this._adapter.updateRoom(roomId, {
|
||||
playerCount: room.players.length,
|
||||
updatedAt: Date.now()
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to update room ${roomId}:`, err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 分布式路由 | Distributed Routing
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 路由玩家到合适的房间/服务器
|
||||
* @en Route player to appropriate room/server
|
||||
*
|
||||
* @param request - 路由请求 | Routing request
|
||||
* @returns 路由结果 | Routing result
|
||||
*/
|
||||
async route(request: RoutingRequest): Promise<RoutingResult> {
|
||||
// 如果指定了房间 ID,直接查找 | If room ID specified, look it up directly
|
||||
if (request.roomId) {
|
||||
return this._routeToRoom(request.roomId);
|
||||
}
|
||||
|
||||
// 按类型查找可用房间 | Find available room by type
|
||||
if (request.roomType) {
|
||||
return this._routeByType(request.roomType, request.query);
|
||||
}
|
||||
|
||||
return { type: 'unavailable', reason: 'No room type or room ID specified' };
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 加入或创建房间(分布式版本)
|
||||
* @en Join or create room (distributed version)
|
||||
*
|
||||
* @zh 此方法会:
|
||||
* 1. 先在分布式注册表中查找可用房间
|
||||
* 2. 如果找到其他服务器的房间,返回重定向
|
||||
* 3. 如果找到本地房间或需要创建,在本地处理
|
||||
* @en This method will:
|
||||
* 1. First search for available room in distributed registry
|
||||
* 2. If room found on another server, return redirect
|
||||
* 3. If local room found or creation needed, handle locally
|
||||
*/
|
||||
async joinOrCreateDistributed(
|
||||
name: string,
|
||||
playerId: string,
|
||||
conn: any,
|
||||
options?: RoomOptions
|
||||
): Promise<{ room: Room; player: Player } | { redirect: string } | null> {
|
||||
// 使用分布式锁防止竞态条件 | Use distributed lock to prevent race conditions
|
||||
const lockKey = `joinOrCreate:${name}`;
|
||||
const locked = await this._adapter.acquireLock(lockKey, 5000);
|
||||
|
||||
if (!locked) {
|
||||
// 等待一小段时间后重试 | Wait and retry
|
||||
await this._sleep(100);
|
||||
return this.joinOrCreateDistributed(name, playerId, conn, options);
|
||||
}
|
||||
|
||||
try {
|
||||
// 先在分布式注册表中查找 | First search in distributed registry
|
||||
const availableRoom = await this._adapter.findAvailableRoom(name);
|
||||
|
||||
if (availableRoom) {
|
||||
// 检查是否在本地服务器 | Check if on local server
|
||||
if (availableRoom.serverId === this._serverId) {
|
||||
// 本地房间 | Local room
|
||||
return super.joinOrCreate(name, playerId, conn, options);
|
||||
} else {
|
||||
// 其他服务器,返回重定向 | Other server, return redirect
|
||||
return { redirect: availableRoom.serverAddress };
|
||||
}
|
||||
}
|
||||
|
||||
// 没有可用房间,在本地创建 | No available room, create locally
|
||||
return super.joinOrCreate(name, playerId, conn, options);
|
||||
} finally {
|
||||
await this._adapter.releaseLock(lockKey);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 状态管理 | State Management
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 保存房间状态快照
|
||||
* @en Save room state snapshot
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
*/
|
||||
async saveSnapshot(roomId: string): Promise<void> {
|
||||
const room = this._rooms.get(roomId);
|
||||
if (!room) return;
|
||||
|
||||
const def = this._getDefinitionByRoom(room);
|
||||
if (!def) return;
|
||||
|
||||
const snapshot = {
|
||||
roomId: room.id,
|
||||
roomType: def.name,
|
||||
state: room.state ?? {},
|
||||
players: room.players.map(p => ({
|
||||
id: p.id,
|
||||
data: p.data ?? {}
|
||||
})),
|
||||
version: Date.now(),
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
await this._adapter.saveSnapshot(snapshot);
|
||||
logger.debug(`Saved snapshot for room: ${roomId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 从快照恢复房间
|
||||
* @en Restore room from snapshot
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
* @returns 是否成功恢复 | Whether restore was successful
|
||||
*/
|
||||
async restoreFromSnapshot(roomId: string): Promise<boolean> {
|
||||
const snapshot = await this._adapter.loadSnapshot(roomId);
|
||||
if (!snapshot) return false;
|
||||
|
||||
// 创建房间实例 | Create room instance
|
||||
const room = await this._createRoomInstance(
|
||||
snapshot.roomType,
|
||||
{ state: snapshot.state },
|
||||
snapshot.roomId
|
||||
);
|
||||
|
||||
if (!room) return false;
|
||||
|
||||
// 注册到分布式系统 | Register to distributed system
|
||||
await this._onRoomCreated(snapshot.roomType, room);
|
||||
|
||||
logger.info(`Restored room from snapshot: ${roomId}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 私有方法 | Private Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 注册服务器到分布式系统
|
||||
* @en Register server to distributed system
|
||||
*/
|
||||
private async _registerServer(): Promise<void> {
|
||||
const registration: ServerRegistration = {
|
||||
serverId: this._serverId,
|
||||
address: this._config.serverAddress,
|
||||
port: this._config.serverPort,
|
||||
roomCount: this._rooms.size,
|
||||
playerCount: this._countTotalPlayers(),
|
||||
capacity: this._config.capacity,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now(),
|
||||
metadata: this._config.metadata
|
||||
};
|
||||
|
||||
await this._adapter.registerServer(registration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 订阅分布式事件
|
||||
* @en Subscribe to distributed events
|
||||
*/
|
||||
private async _subscribeToEvents(): Promise<void> {
|
||||
// 订阅服务器离线事件以触发故障转移 | Subscribe to server offline for failover
|
||||
if (this._config.enableFailover) {
|
||||
const unsub = await this._adapter.subscribe('server:offline', (event) => {
|
||||
this._handleServerOffline(event);
|
||||
});
|
||||
this._subscriptions.push(unsub);
|
||||
}
|
||||
|
||||
// 订阅房间消息事件 | Subscribe to room message events
|
||||
const roomMsgUnsub = await this._adapter.subscribe('room:message', (event) => {
|
||||
this._handleRoomMessage(event);
|
||||
});
|
||||
this._subscriptions.push(roomMsgUnsub);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 启动心跳定时器
|
||||
* @en Start heartbeat timer
|
||||
*/
|
||||
private _startHeartbeat(): void {
|
||||
this._heartbeatTimer = setInterval(async () => {
|
||||
try {
|
||||
await this._adapter.heartbeat(this._serverId);
|
||||
await this._adapter.updateServer(this._serverId, {
|
||||
roomCount: this._rooms.size,
|
||||
playerCount: this._countTotalPlayers()
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Heartbeat failed:', err);
|
||||
}
|
||||
}, this._config.heartbeatInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 启动快照定时器
|
||||
* @en Start snapshot timer
|
||||
*/
|
||||
private _startSnapshotTimer(): void {
|
||||
this._snapshotTimer = setInterval(async () => {
|
||||
await this._saveAllSnapshots();
|
||||
}, this._config.snapshotInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 保存所有房间快照
|
||||
* @en Save all room snapshots
|
||||
*/
|
||||
private async _saveAllSnapshots(): Promise<void> {
|
||||
const promises: Promise<void>[] = [];
|
||||
for (const roomId of this._rooms.keys()) {
|
||||
promises.push(this.saveSnapshot(roomId));
|
||||
}
|
||||
await Promise.allSettled(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 路由到指定房间
|
||||
* @en Route to specific room
|
||||
*/
|
||||
private async _routeToRoom(roomId: string): Promise<RoutingResult> {
|
||||
// 先检查本地 | Check local first
|
||||
if (this._rooms.has(roomId)) {
|
||||
return { type: 'local', roomId };
|
||||
}
|
||||
|
||||
// 从分布式注册表查询 | Query from distributed registry
|
||||
const registration = await this._adapter.getRoom(roomId);
|
||||
if (!registration) {
|
||||
return { type: 'unavailable', reason: 'Room not found' };
|
||||
}
|
||||
|
||||
if (registration.serverId === this._serverId) {
|
||||
return { type: 'local', roomId };
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'redirect',
|
||||
serverAddress: registration.serverAddress,
|
||||
roomId
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 按类型路由
|
||||
* @en Route by type
|
||||
*/
|
||||
private async _routeByType(
|
||||
roomType: string,
|
||||
_query?: RoutingRequest['query']
|
||||
): Promise<RoutingResult> {
|
||||
const availableRoom = await this._adapter.findAvailableRoom(roomType);
|
||||
|
||||
if (!availableRoom) {
|
||||
// 没有可用房间,需要创建 | No available room, need to create
|
||||
return { type: 'create', roomId: undefined };
|
||||
}
|
||||
|
||||
if (availableRoom.serverId === this._serverId) {
|
||||
return { type: 'local', roomId: availableRoom.roomId };
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'redirect',
|
||||
serverAddress: availableRoom.serverAddress,
|
||||
roomId: availableRoom.roomId
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 处理服务器离线事件
|
||||
* @en Handle server offline event
|
||||
*/
|
||||
private _handleServerOffline(event: DistributedEvent): void {
|
||||
if (this._isShuttingDown) return;
|
||||
if (!this._config.enableFailover) return;
|
||||
|
||||
const offlineServerId = event.serverId;
|
||||
if (offlineServerId === this._serverId) return;
|
||||
|
||||
logger.info(`Server offline detected: ${offlineServerId}`);
|
||||
|
||||
this._tryRecoverRoomsFromServer(offlineServerId).catch(err => {
|
||||
logger.error(`Failed to recover rooms from ${offlineServerId}:`, err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 尝试从离线服务器恢复房间
|
||||
* @en Try to recover rooms from offline server
|
||||
*/
|
||||
private async _tryRecoverRoomsFromServer(offlineServerId: string): Promise<void> {
|
||||
// 检查是否有容量接收更多房间
|
||||
if (this._rooms.size >= this._config.capacity) {
|
||||
logger.warn(`Cannot recover rooms: server at capacity (${this._rooms.size}/${this._config.capacity})`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 查询该服务器上的所有房间
|
||||
const rooms = await this._adapter.queryRooms({ serverId: offlineServerId });
|
||||
if (rooms.length === 0) {
|
||||
logger.info(`No rooms to recover from ${offlineServerId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`Attempting to recover ${rooms.length} rooms from ${offlineServerId}`);
|
||||
|
||||
for (const roomReg of rooms) {
|
||||
// 检查容量
|
||||
if (this._rooms.size >= this._config.capacity) {
|
||||
logger.warn('Reached capacity during recovery, stopping');
|
||||
break;
|
||||
}
|
||||
|
||||
// 尝试获取恢复锁,防止多个服务器同时恢复同一房间
|
||||
const lockKey = `failover:${roomReg.roomId}`;
|
||||
const acquired = await this._adapter.acquireLock(lockKey, this._config.migrationTimeout);
|
||||
|
||||
if (!acquired) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
// 从快照恢复房间
|
||||
const success = await this.restoreFromSnapshot(roomReg.roomId);
|
||||
if (success) {
|
||||
logger.info(`Successfully recovered room ${roomReg.roomId}`);
|
||||
// 发布恢复事件
|
||||
await this._adapter.publish({
|
||||
type: 'room:migrated',
|
||||
serverId: this._serverId,
|
||||
roomId: roomReg.roomId,
|
||||
payload: {
|
||||
fromServer: offlineServerId,
|
||||
toServer: this._serverId
|
||||
},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await this._adapter.releaseLock(lockKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 处理跨服务器房间消息
|
||||
* @en Handle cross-server room message
|
||||
*/
|
||||
private _handleRoomMessage(event: DistributedEvent): void {
|
||||
if (!event.roomId) return;
|
||||
|
||||
const room = this._rooms.get(event.roomId);
|
||||
if (!room) return;
|
||||
|
||||
const payload = event.payload as { messageType: string; data: unknown; playerId?: string };
|
||||
if (payload.playerId) {
|
||||
room._handleMessage(payload.messageType, payload.data, payload.playerId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 统计总玩家数
|
||||
* @en Count total players
|
||||
*/
|
||||
private _countTotalPlayers(): number {
|
||||
let count = 0;
|
||||
for (const room of this._rooms.values()) {
|
||||
count += room.players.length;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 根据房间实例获取定义
|
||||
* @en Get definition by room instance
|
||||
*/
|
||||
private _getDefinitionByRoom(room: Room): { name: string } | null {
|
||||
for (const [name, def] of this._definitions) {
|
||||
if (room instanceof def.roomClass) {
|
||||
return { name };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 休眠指定时间
|
||||
* @en Sleep for specified time
|
||||
*/
|
||||
private _sleep(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 向其他服务器的房间发送消息
|
||||
* @en Send message to room on another server
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
* @param messageType - 消息类型 | Message type
|
||||
* @param data - 消息数据 | Message data
|
||||
* @param playerId - 发送者玩家 ID(可选)| Sender player ID (optional)
|
||||
*/
|
||||
async sendToRemoteRoom(
|
||||
roomId: string,
|
||||
messageType: string,
|
||||
data: unknown,
|
||||
playerId?: string
|
||||
): Promise<void> {
|
||||
await this._adapter.sendToRoom(roomId, messageType, data, playerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取所有在线服务器
|
||||
* @en Get all online servers
|
||||
*/
|
||||
async getServers(): Promise<ServerRegistration[]> {
|
||||
return this._adapter.getServers();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 查询分布式房间
|
||||
* @en Query distributed rooms
|
||||
*/
|
||||
async queryDistributedRooms(query: {
|
||||
roomType?: string;
|
||||
hasSpace?: boolean;
|
||||
notLocked?: boolean;
|
||||
metadata?: Record<string, unknown>;
|
||||
limit?: number;
|
||||
}): Promise<RoomRegistration[]> {
|
||||
return this._adapter.queryRooms(query);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,453 @@
|
||||
/**
|
||||
* @zh DistributedRoomManager 单元测试
|
||||
* @en DistributedRoomManager unit tests
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { DistributedRoomManager } from '../DistributedRoomManager.js';
|
||||
import { MemoryAdapter } from '../adapters/MemoryAdapter.js';
|
||||
import { Room } from '../../room/Room.js';
|
||||
|
||||
class TestRoom extends Room {
|
||||
maxPlayers = 4;
|
||||
}
|
||||
|
||||
describe('DistributedRoomManager', () => {
|
||||
let adapter: MemoryAdapter;
|
||||
let manager: DistributedRoomManager;
|
||||
const mockSendFn = vi.fn();
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
adapter = new MemoryAdapter({ enableTtlCheck: false });
|
||||
|
||||
manager = new DistributedRoomManager(adapter, {
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3000,
|
||||
heartbeatInterval: 60000, // 长间隔避免测试中触发
|
||||
snapshotInterval: 0 // 禁用自动快照
|
||||
}, mockSendFn);
|
||||
|
||||
manager.define('test', TestRoom);
|
||||
await manager.start();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await manager.stop(false);
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
describe('lifecycle', () => {
|
||||
it('should start and register server', async () => {
|
||||
const servers = await adapter.getServers();
|
||||
expect(servers).toHaveLength(1);
|
||||
expect(servers[0].serverId).toBe('server-1');
|
||||
expect(servers[0].status).toBe('online');
|
||||
});
|
||||
|
||||
it('should stop and unregister server', async () => {
|
||||
await manager.stop(false);
|
||||
|
||||
const servers = await adapter.getServers();
|
||||
expect(servers).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should expose serverId and config', () => {
|
||||
expect(manager.serverId).toBe('server-1');
|
||||
expect(manager.config.serverAddress).toBe('localhost');
|
||||
expect(manager.config.serverPort).toBe(3000);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 房间操作 | Room Operations
|
||||
// =========================================================================
|
||||
|
||||
describe('room operations', () => {
|
||||
it('should create room and register to distributed system', async () => {
|
||||
const room = await manager.create('test');
|
||||
|
||||
expect(room).toBeDefined();
|
||||
expect(room?.id).toBeDefined();
|
||||
|
||||
const registration = await adapter.getRoom(room!.id);
|
||||
expect(registration).toBeDefined();
|
||||
expect(registration?.roomType).toBe('test');
|
||||
expect(registration?.serverId).toBe('server-1');
|
||||
});
|
||||
|
||||
it('should update room count on server after creating room', async () => {
|
||||
await manager.create('test');
|
||||
await manager.create('test');
|
||||
|
||||
const server = await adapter.getServer('server-1');
|
||||
expect(server?.roomCount).toBe(2);
|
||||
});
|
||||
|
||||
it('should unregister room from distributed system on dispose', async () => {
|
||||
const room = await manager.create('test');
|
||||
const roomId = room!.id;
|
||||
|
||||
room!.dispose();
|
||||
|
||||
// 等待异步注销完成 | Wait for async unregister
|
||||
await new Promise(r => setTimeout(r, 50));
|
||||
|
||||
const registration = await adapter.getRoom(roomId);
|
||||
expect(registration).toBeNull();
|
||||
});
|
||||
|
||||
it('should update player count in distributed registration', async () => {
|
||||
const mockConn = { send: vi.fn() };
|
||||
const result = await manager.joinOrCreate('test', 'player-1', mockConn);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
|
||||
const registration = await adapter.getRoom(result!.room.id);
|
||||
expect(registration?.playerCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 分布式路由 | Distributed Routing
|
||||
// =========================================================================
|
||||
|
||||
describe('distributed routing', () => {
|
||||
it('should route to local room', async () => {
|
||||
const room = await manager.create('test');
|
||||
|
||||
const result = await manager.route({ roomId: room!.id, playerId: 'p1' });
|
||||
|
||||
expect(result.type).toBe('local');
|
||||
expect(result.roomId).toBe(room!.id);
|
||||
});
|
||||
|
||||
it('should return unavailable for non-existent room', async () => {
|
||||
const result = await manager.route({ roomId: 'non-existent', playerId: 'p1' });
|
||||
|
||||
expect(result.type).toBe('unavailable');
|
||||
});
|
||||
|
||||
it('should return create when no available room exists', async () => {
|
||||
const result = await manager.route({ roomType: 'test', playerId: 'p1' });
|
||||
|
||||
expect(result.type).toBe('create');
|
||||
});
|
||||
|
||||
it('should return local for available local room', async () => {
|
||||
const room = await manager.create('test');
|
||||
|
||||
const result = await manager.route({ roomType: 'test', playerId: 'p1' });
|
||||
|
||||
expect(result.type).toBe('local');
|
||||
expect(result.roomId).toBe(room!.id);
|
||||
});
|
||||
|
||||
it('should return redirect for room on another server', async () => {
|
||||
// 直接在适配器中注册另一个服务器的房间 | Register room from another server directly
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-2',
|
||||
address: 'other-host',
|
||||
port: 3001,
|
||||
roomCount: 1,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await adapter.registerRoom({
|
||||
roomId: 'remote-room-1',
|
||||
roomType: 'test',
|
||||
serverId: 'server-2',
|
||||
serverAddress: 'other-host:3001',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
const result = await manager.route({ roomType: 'test', playerId: 'p1' });
|
||||
|
||||
expect(result.type).toBe('redirect');
|
||||
expect(result.serverAddress).toBe('other-host:3001');
|
||||
expect(result.roomId).toBe('remote-room-1');
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 分布式加入创建 | Distributed Join/Create
|
||||
// =========================================================================
|
||||
|
||||
describe('joinOrCreateDistributed', () => {
|
||||
it('should create room locally when none exists', async () => {
|
||||
const mockConn = { send: vi.fn() };
|
||||
const result = await manager.joinOrCreateDistributed('test', 'player-1', mockConn);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect('room' in result!).toBe(true);
|
||||
if ('room' in result!) {
|
||||
expect(result.room).toBeDefined();
|
||||
expect(result.player.id).toBe('player-1');
|
||||
}
|
||||
});
|
||||
|
||||
it('should join existing local room', async () => {
|
||||
const room = await manager.create('test');
|
||||
const mockConn = { send: vi.fn() };
|
||||
|
||||
const result = await manager.joinOrCreateDistributed('test', 'player-1', mockConn);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect('room' in result!).toBe(true);
|
||||
if ('room' in result!) {
|
||||
expect(result.room.id).toBe(room!.id);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return redirect for remote room', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-2',
|
||||
address: 'remote',
|
||||
port: 3001,
|
||||
roomCount: 1,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await adapter.registerRoom({
|
||||
roomId: 'remote-room',
|
||||
roomType: 'test',
|
||||
serverId: 'server-2',
|
||||
serverAddress: 'remote:3001',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
const mockConn = { send: vi.fn() };
|
||||
const result = await manager.joinOrCreateDistributed('test', 'player-1', mockConn);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect('redirect' in result!).toBe(true);
|
||||
if ('redirect' in result!) {
|
||||
expect(result.redirect).toBe('remote:3001');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 状态快照 | State Snapshots
|
||||
// =========================================================================
|
||||
|
||||
describe('snapshots', () => {
|
||||
it('should save room snapshot', async () => {
|
||||
const room = await manager.create('test', { state: { score: 100 } });
|
||||
|
||||
await manager.saveSnapshot(room!.id);
|
||||
|
||||
const snapshot = await adapter.loadSnapshot(room!.id);
|
||||
expect(snapshot).toBeDefined();
|
||||
expect(snapshot?.roomId).toBe(room!.id);
|
||||
expect(snapshot?.roomType).toBe('test');
|
||||
});
|
||||
|
||||
it('should restore room from snapshot', async () => {
|
||||
// 手动创建快照 | Manually create snapshot
|
||||
await adapter.saveSnapshot({
|
||||
roomId: 'restored-room',
|
||||
roomType: 'test',
|
||||
state: { score: 500 },
|
||||
players: [],
|
||||
version: 1,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
const restored = await manager.restoreFromSnapshot('restored-room');
|
||||
|
||||
expect(restored).toBe(true);
|
||||
|
||||
const room = manager.getRoom('restored-room');
|
||||
expect(room).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return false when snapshot not found', async () => {
|
||||
const restored = await manager.restoreFromSnapshot('non-existent');
|
||||
expect(restored).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 跨服务器通信 | Cross-Server Communication
|
||||
// =========================================================================
|
||||
|
||||
describe('cross-server communication', () => {
|
||||
it('should send message to remote room', async () => {
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('room:message', handler);
|
||||
|
||||
await adapter.registerRoom({
|
||||
roomId: 'remote-room',
|
||||
roomType: 'test',
|
||||
serverId: 'server-2',
|
||||
serverAddress: 'remote:3001',
|
||||
playerCount: 1,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
await manager.sendToRemoteRoom('remote-room', 'chat', { text: 'hello' }, 'player-1');
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'room:message',
|
||||
roomId: 'remote-room',
|
||||
payload: {
|
||||
messageType: 'chat',
|
||||
data: { text: 'hello' },
|
||||
playerId: 'player-1'
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 查询方法 | Query Methods
|
||||
// =========================================================================
|
||||
|
||||
describe('query methods', () => {
|
||||
it('should get all servers', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-2',
|
||||
address: 'other',
|
||||
port: 3001,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
const servers = await manager.getServers();
|
||||
expect(servers).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should query distributed rooms', async () => {
|
||||
await manager.create('test');
|
||||
|
||||
await adapter.registerRoom({
|
||||
roomId: 'remote-room',
|
||||
roomType: 'test',
|
||||
serverId: 'server-2',
|
||||
serverAddress: 'remote:3001',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
const rooms = await manager.queryDistributedRooms({ roomType: 'test' });
|
||||
expect(rooms).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 事件订阅 | Event Subscription
|
||||
// =========================================================================
|
||||
|
||||
describe('event subscription', () => {
|
||||
it('should handle room messages for local rooms', async () => {
|
||||
const room = await manager.create('test');
|
||||
const handleSpy = vi.spyOn(room!, '_handleMessage');
|
||||
|
||||
await adapter.publish({
|
||||
type: 'room:message',
|
||||
serverId: 'server-2',
|
||||
roomId: room!.id,
|
||||
payload: {
|
||||
messageType: 'test',
|
||||
data: { foo: 'bar' },
|
||||
playerId: 'player-1'
|
||||
},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
expect(handleSpy).toHaveBeenCalledWith('test', { foo: 'bar' }, 'player-1');
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 优雅关闭 | Graceful Shutdown
|
||||
// =========================================================================
|
||||
|
||||
describe('graceful shutdown', () => {
|
||||
it('should mark server as draining on graceful stop', async () => {
|
||||
const statusHandler = vi.fn();
|
||||
|
||||
// 创建新的管理器用于此测试 | Create new manager for this test
|
||||
const newAdapter = new MemoryAdapter({ enableTtlCheck: false });
|
||||
const newManager = new DistributedRoomManager(newAdapter, {
|
||||
serverId: 'graceful-server',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3002,
|
||||
heartbeatInterval: 60000,
|
||||
snapshotInterval: 0
|
||||
}, mockSendFn);
|
||||
|
||||
newManager.define('test', TestRoom);
|
||||
await newManager.start();
|
||||
|
||||
// 监听状态变化 | Watch for status changes
|
||||
// 由于我们在 stop(true) 中调用 updateServer,可以检查最终状态
|
||||
await newManager.stop(true);
|
||||
|
||||
// 验证服务器已注销 | Verify server is unregistered
|
||||
const server = await newAdapter.getServer('graceful-server');
|
||||
expect(server).toBeNull();
|
||||
});
|
||||
|
||||
it('should save all snapshots on graceful stop', async () => {
|
||||
const newAdapter = new MemoryAdapter({ enableTtlCheck: false });
|
||||
const newManager = new DistributedRoomManager(newAdapter, {
|
||||
serverId: 'snapshot-server',
|
||||
serverAddress: 'localhost',
|
||||
serverPort: 3003,
|
||||
heartbeatInterval: 60000,
|
||||
snapshotInterval: 0
|
||||
}, mockSendFn);
|
||||
|
||||
newManager.define('test', TestRoom);
|
||||
await newManager.start();
|
||||
|
||||
// 创建房间 | Create rooms
|
||||
const room1 = await newManager.create('test');
|
||||
const room2 = await newManager.create('test');
|
||||
|
||||
await newManager.stop(true);
|
||||
|
||||
// 验证快照已保存 | Verify snapshots are saved
|
||||
const snapshot1 = await newAdapter.loadSnapshot(room1!.id);
|
||||
const snapshot2 = await newAdapter.loadSnapshot(room2!.id);
|
||||
|
||||
expect(snapshot1).toBeDefined();
|
||||
expect(snapshot2).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,582 @@
|
||||
/**
|
||||
* @zh MemoryAdapter 单元测试
|
||||
* @en MemoryAdapter unit tests
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { MemoryAdapter } from '../adapters/MemoryAdapter.js';
|
||||
import type { ServerRegistration, RoomRegistration, DistributedEvent } from '../types.js';
|
||||
|
||||
describe('MemoryAdapter', () => {
|
||||
let adapter: MemoryAdapter;
|
||||
|
||||
beforeEach(async () => {
|
||||
adapter = new MemoryAdapter({ enableTtlCheck: false });
|
||||
await adapter.connect();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await adapter.disconnect();
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
describe('lifecycle', () => {
|
||||
it('should connect and disconnect', async () => {
|
||||
const newAdapter = new MemoryAdapter();
|
||||
expect(newAdapter.isConnected()).toBe(false);
|
||||
|
||||
await newAdapter.connect();
|
||||
expect(newAdapter.isConnected()).toBe(true);
|
||||
|
||||
await newAdapter.disconnect();
|
||||
expect(newAdapter.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
it('should not throw on double connect', async () => {
|
||||
await adapter.connect();
|
||||
expect(adapter.isConnected()).toBe(true);
|
||||
});
|
||||
|
||||
it('should not throw on double disconnect', async () => {
|
||||
await adapter.disconnect();
|
||||
await adapter.disconnect();
|
||||
expect(adapter.isConnected()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 服务器注册 | Server Registry
|
||||
// =========================================================================
|
||||
|
||||
describe('server registry', () => {
|
||||
const createServer = (id: string): ServerRegistration => ({
|
||||
serverId: id,
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
it('should register and get server', async () => {
|
||||
const server = createServer('server-1');
|
||||
await adapter.registerServer(server);
|
||||
|
||||
const result = await adapter.getServer('server-1');
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.serverId).toBe('server-1');
|
||||
});
|
||||
|
||||
it('should get all online servers', async () => {
|
||||
await adapter.registerServer(createServer('server-1'));
|
||||
await adapter.registerServer(createServer('server-2'));
|
||||
|
||||
const servers = await adapter.getServers();
|
||||
expect(servers).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should filter out offline servers', async () => {
|
||||
const server1 = createServer('server-1');
|
||||
const server2 = { ...createServer('server-2'), status: 'offline' as const };
|
||||
|
||||
await adapter.registerServer(server1);
|
||||
await adapter.registerServer(server2);
|
||||
|
||||
const servers = await adapter.getServers();
|
||||
expect(servers).toHaveLength(1);
|
||||
expect(servers[0].serverId).toBe('server-1');
|
||||
});
|
||||
|
||||
it('should unregister server and cleanup rooms', async () => {
|
||||
const server = createServer('server-1');
|
||||
await adapter.registerServer(server);
|
||||
|
||||
const room: RoomRegistration = {
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost:3000',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
await adapter.registerRoom(room);
|
||||
|
||||
await adapter.unregisterServer('server-1');
|
||||
|
||||
const serverResult = await adapter.getServer('server-1');
|
||||
expect(serverResult).toBeNull();
|
||||
|
||||
const roomResult = await adapter.getRoom('room-1');
|
||||
expect(roomResult).toBeNull();
|
||||
});
|
||||
|
||||
it('should update server heartbeat', async () => {
|
||||
const server = createServer('server-1');
|
||||
await adapter.registerServer(server);
|
||||
|
||||
const before = (await adapter.getServer('server-1'))?.lastHeartbeat;
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
await adapter.heartbeat('server-1');
|
||||
const after = (await adapter.getServer('server-1'))?.lastHeartbeat;
|
||||
|
||||
expect(after).toBeGreaterThan(before!);
|
||||
});
|
||||
|
||||
it('should update server info', async () => {
|
||||
const server = createServer('server-1');
|
||||
await adapter.registerServer(server);
|
||||
|
||||
await adapter.updateServer('server-1', { roomCount: 5, playerCount: 10 });
|
||||
|
||||
const result = await adapter.getServer('server-1');
|
||||
expect(result?.roomCount).toBe(5);
|
||||
expect(result?.playerCount).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 房间注册 | Room Registry
|
||||
// =========================================================================
|
||||
|
||||
describe('room registry', () => {
|
||||
const createRoom = (id: string, serverId = 'server-1'): RoomRegistration => ({
|
||||
roomId: id,
|
||||
roomType: 'game',
|
||||
serverId,
|
||||
serverAddress: 'localhost:3000',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
});
|
||||
|
||||
it('should register and get room', async () => {
|
||||
const room = createRoom('room-1');
|
||||
await adapter.registerRoom(room);
|
||||
|
||||
const result = await adapter.getRoom('room-1');
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should update server room count on register', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom(createRoom('room-2'));
|
||||
|
||||
const server = await adapter.getServer('server-1');
|
||||
expect(server?.roomCount).toBe(2);
|
||||
});
|
||||
|
||||
it('should unregister room', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.unregisterRoom('room-1');
|
||||
|
||||
const result = await adapter.getRoom('room-1');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should update room info', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.updateRoom('room-1', { playerCount: 2, isLocked: true });
|
||||
|
||||
const result = await adapter.getRoom('room-1');
|
||||
expect(result?.playerCount).toBe(2);
|
||||
expect(result?.isLocked).toBe(true);
|
||||
});
|
||||
|
||||
it('should query rooms by type', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), roomType: 'lobby' });
|
||||
|
||||
const games = await adapter.queryRooms({ roomType: 'game' });
|
||||
expect(games).toHaveLength(1);
|
||||
expect(games[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should query rooms with space', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), playerCount: 4 });
|
||||
|
||||
const available = await adapter.queryRooms({ hasSpace: true });
|
||||
expect(available).toHaveLength(1);
|
||||
expect(available[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should query unlocked rooms', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), isLocked: true });
|
||||
|
||||
const unlocked = await adapter.queryRooms({ notLocked: true });
|
||||
expect(unlocked).toHaveLength(1);
|
||||
expect(unlocked[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should query rooms by metadata', async () => {
|
||||
await adapter.registerRoom({ ...createRoom('room-1'), metadata: { map: 'forest' } });
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), metadata: { map: 'desert' } });
|
||||
|
||||
const forest = await adapter.queryRooms({ metadata: { map: 'forest' } });
|
||||
expect(forest).toHaveLength(1);
|
||||
expect(forest[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should support pagination', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom(createRoom('room-2'));
|
||||
await adapter.registerRoom(createRoom('room-3'));
|
||||
|
||||
const page1 = await adapter.queryRooms({ limit: 2 });
|
||||
expect(page1).toHaveLength(2);
|
||||
|
||||
const page2 = await adapter.queryRooms({ offset: 2, limit: 2 });
|
||||
expect(page2).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should find available room', async () => {
|
||||
await adapter.registerRoom({ ...createRoom('room-1'), playerCount: 4 }); // full
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), isLocked: true }); // locked
|
||||
await adapter.registerRoom(createRoom('room-3')); // available
|
||||
|
||||
const available = await adapter.findAvailableRoom('game');
|
||||
expect(available?.roomId).toBe('room-3');
|
||||
});
|
||||
|
||||
it('should return null when no available room', async () => {
|
||||
await adapter.registerRoom({ ...createRoom('room-1'), playerCount: 4 });
|
||||
|
||||
const available = await adapter.findAvailableRoom('game');
|
||||
expect(available).toBeNull();
|
||||
});
|
||||
|
||||
it('should get rooms by server', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-2',
|
||||
address: 'localhost',
|
||||
port: 3001,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await adapter.registerRoom(createRoom('room-1', 'server-1'));
|
||||
await adapter.registerRoom(createRoom('room-2', 'server-2'));
|
||||
|
||||
const server1Rooms = await adapter.getRoomsByServer('server-1');
|
||||
expect(server1Rooms).toHaveLength(1);
|
||||
expect(server1Rooms[0].roomId).toBe('room-1');
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 快照 | Snapshots
|
||||
// =========================================================================
|
||||
|
||||
describe('snapshots', () => {
|
||||
it('should save and load snapshot', async () => {
|
||||
const snapshot = {
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
state: { score: 100 },
|
||||
players: [{ id: 'player-1', data: { name: 'Alice' } }],
|
||||
version: 1,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
await adapter.saveSnapshot(snapshot);
|
||||
const result = await adapter.loadSnapshot('room-1');
|
||||
|
||||
expect(result).toEqual(snapshot);
|
||||
});
|
||||
|
||||
it('should return null for non-existent snapshot', async () => {
|
||||
const result = await adapter.loadSnapshot('non-existent');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should delete snapshot', async () => {
|
||||
await adapter.saveSnapshot({
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
state: {},
|
||||
players: [],
|
||||
version: 1,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
await adapter.deleteSnapshot('room-1');
|
||||
const result = await adapter.loadSnapshot('room-1');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 发布/订阅 | Pub/Sub
|
||||
// =========================================================================
|
||||
|
||||
describe('pub/sub', () => {
|
||||
it('should publish and subscribe to events', async () => {
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('room:created', handler);
|
||||
|
||||
const event: DistributedEvent = {
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
roomId: 'room-1',
|
||||
payload: { roomType: 'game' },
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
await adapter.publish(event);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(event);
|
||||
});
|
||||
|
||||
it('should support wildcard subscription', async () => {
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('*', handler);
|
||||
|
||||
await adapter.publish({
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
await adapter.publish({
|
||||
type: 'server:online',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
expect(handler).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should unsubscribe correctly', async () => {
|
||||
const handler = vi.fn();
|
||||
const unsub = await adapter.subscribe('room:created', handler);
|
||||
|
||||
unsub();
|
||||
|
||||
await adapter.publish({
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle errors in handlers gracefully', async () => {
|
||||
const errorHandler = vi.fn(() => { throw new Error('Test error'); });
|
||||
const normalHandler = vi.fn();
|
||||
|
||||
await adapter.subscribe('room:created', errorHandler);
|
||||
await adapter.subscribe('room:created', normalHandler);
|
||||
|
||||
await adapter.publish({
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
expect(errorHandler).toHaveBeenCalled();
|
||||
expect(normalHandler).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should send to room', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await adapter.registerRoom({
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost:3000',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('room:message', handler);
|
||||
|
||||
await adapter.sendToRoom('room-1', 'chat', { text: 'hello' }, 'player-1');
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'room:message',
|
||||
roomId: 'room-1',
|
||||
payload: {
|
||||
messageType: 'chat',
|
||||
data: { text: 'hello' },
|
||||
playerId: 'player-1'
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 分布式锁 | Distributed Locks
|
||||
// =========================================================================
|
||||
|
||||
describe('distributed locks', () => {
|
||||
it('should acquire and release lock', async () => {
|
||||
const acquired = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquired).toBe(true);
|
||||
|
||||
await adapter.releaseLock('test-lock');
|
||||
|
||||
const acquiredAgain = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquiredAgain).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail to acquire held lock', async () => {
|
||||
await adapter.acquireLock('test-lock', 5000);
|
||||
|
||||
const acquiredAgain = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquiredAgain).toBe(false);
|
||||
});
|
||||
|
||||
it('should acquire expired lock', async () => {
|
||||
await adapter.acquireLock('test-lock', 1);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
const acquired = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquired).toBe(true);
|
||||
});
|
||||
|
||||
it('should extend lock', async () => {
|
||||
await adapter.acquireLock('test-lock', 100);
|
||||
|
||||
const extended = await adapter.extendLock('test-lock', 5000);
|
||||
expect(extended).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail to extend non-existent lock', async () => {
|
||||
const extended = await adapter.extendLock('non-existent', 5000);
|
||||
expect(extended).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// TTL 检查 | TTL Check
|
||||
// =========================================================================
|
||||
|
||||
describe('TTL check', () => {
|
||||
it('should mark server offline after TTL expires', async () => {
|
||||
const ttlAdapter = new MemoryAdapter({
|
||||
serverTtl: 50,
|
||||
ttlCheckInterval: 20,
|
||||
enableTtlCheck: true
|
||||
});
|
||||
await ttlAdapter.connect();
|
||||
|
||||
const handler = vi.fn();
|
||||
await ttlAdapter.subscribe('server:offline', handler);
|
||||
|
||||
await ttlAdapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
|
||||
expect(handler).toHaveBeenCalled();
|
||||
const server = await ttlAdapter.getServer('server-1');
|
||||
expect(server?.status).toBe('offline');
|
||||
|
||||
await ttlAdapter.disconnect();
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 测试辅助方法 | Test Helper Methods
|
||||
// =========================================================================
|
||||
|
||||
describe('test helpers', () => {
|
||||
it('should clear all data', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
adapter._clear();
|
||||
|
||||
const servers = await adapter.getServers();
|
||||
expect(servers).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should expose internal state for testing', () => {
|
||||
const state = adapter._getState();
|
||||
expect(state.servers).toBeDefined();
|
||||
expect(state.rooms).toBeDefined();
|
||||
expect(state.snapshots).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 错误处理 | Error Handling
|
||||
// =========================================================================
|
||||
|
||||
describe('error handling', () => {
|
||||
it('should throw when not connected', async () => {
|
||||
const disconnected = new MemoryAdapter();
|
||||
|
||||
await expect(disconnected.registerServer({} as ServerRegistration))
|
||||
.rejects.toThrow('MemoryAdapter is not connected');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,750 @@
|
||||
/**
|
||||
* @zh RedisAdapter 单元测试
|
||||
* @en RedisAdapter unit tests
|
||||
*
|
||||
* @zh 使用 Mock Redis 客户端进行测试
|
||||
* @en Uses Mock Redis client for testing
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { RedisAdapter } from '../adapters/RedisAdapter.js';
|
||||
import type { RedisClient } from '../adapters/RedisAdapter.js';
|
||||
import type { ServerRegistration, RoomRegistration, DistributedEvent } from '../types.js';
|
||||
|
||||
// 共享状态,用于模拟 Redis Pub/Sub
|
||||
const sharedStore = new Map<string, string>();
|
||||
const sharedSets = new Map<string, Set<string>>();
|
||||
const sharedHashes = new Map<string, Map<string, string>>();
|
||||
const sharedExpireTimes = new Map<string, number>();
|
||||
const sharedPubSubHandlers = new Map<string, Set<(channel: string, message: string) => void>>();
|
||||
|
||||
function clearSharedState(): void {
|
||||
sharedStore.clear();
|
||||
sharedSets.clear();
|
||||
sharedHashes.clear();
|
||||
sharedExpireTimes.clear();
|
||||
sharedPubSubHandlers.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建 Mock Redis 客户端
|
||||
* @en Create Mock Redis client
|
||||
*/
|
||||
function createMockRedisClient(): RedisClient {
|
||||
const eventHandlers = new Map<string, Set<(...args: unknown[]) => void>>();
|
||||
|
||||
const mockClient: RedisClient = {
|
||||
// 基础操作
|
||||
get: vi.fn(async (key: string) => sharedStore.get(key) ?? null),
|
||||
set: vi.fn(async (key: string, value: string, ...args: (string | number)[]) => {
|
||||
// 处理 NX 选项
|
||||
if (args.includes('NX') && sharedStore.has(key)) {
|
||||
return null;
|
||||
}
|
||||
sharedStore.set(key, value);
|
||||
// 处理 EX 选项
|
||||
const exIndex = args.indexOf('EX');
|
||||
if (exIndex !== -1 && typeof args[exIndex + 1] === 'number') {
|
||||
sharedExpireTimes.set(key, Date.now() + (args[exIndex + 1] as number) * 1000);
|
||||
}
|
||||
return 'OK';
|
||||
}),
|
||||
del: vi.fn(async (...keys: string[]) => {
|
||||
let count = 0;
|
||||
for (const key of keys) {
|
||||
if (sharedStore.delete(key) || sharedHashes.delete(key) || sharedSets.delete(key)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}),
|
||||
expire: vi.fn(async (key: string, seconds: number) => {
|
||||
if (sharedStore.has(key) || sharedHashes.has(key)) {
|
||||
sharedExpireTimes.set(key, Date.now() + seconds * 1000);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}),
|
||||
ttl: vi.fn(async (key: string) => {
|
||||
const expire = sharedExpireTimes.get(key);
|
||||
if (!expire) return -1;
|
||||
const remaining = Math.ceil((expire - Date.now()) / 1000);
|
||||
return remaining > 0 ? remaining : -2;
|
||||
}),
|
||||
|
||||
// Hash 操作
|
||||
hget: vi.fn(async (key: string, field: string) => {
|
||||
return sharedHashes.get(key)?.get(field) ?? null;
|
||||
}),
|
||||
hset: vi.fn(async (key: string, ...args: (string | number | Buffer)[]) => {
|
||||
if (!sharedHashes.has(key)) {
|
||||
sharedHashes.set(key, new Map());
|
||||
}
|
||||
const hash = sharedHashes.get(key)!;
|
||||
let added = 0;
|
||||
for (let i = 0; i < args.length; i += 2) {
|
||||
const field = String(args[i]);
|
||||
const value = String(args[i + 1]);
|
||||
if (!hash.has(field)) added++;
|
||||
hash.set(field, value);
|
||||
}
|
||||
return added;
|
||||
}),
|
||||
hdel: vi.fn(async (key: string, ...fields: string[]) => {
|
||||
const hash = sharedHashes.get(key);
|
||||
if (!hash) return 0;
|
||||
let count = 0;
|
||||
for (const field of fields) {
|
||||
if (hash.delete(field)) count++;
|
||||
}
|
||||
return count;
|
||||
}),
|
||||
hgetall: vi.fn(async (key: string) => {
|
||||
const hash = sharedHashes.get(key);
|
||||
if (!hash) return {};
|
||||
const result: Record<string, string> = {};
|
||||
for (const [k, v] of hash) {
|
||||
result[k] = v;
|
||||
}
|
||||
return result;
|
||||
}),
|
||||
hmset: vi.fn(async (key: string, ...args: (string | number | Buffer)[]) => {
|
||||
if (!sharedHashes.has(key)) {
|
||||
sharedHashes.set(key, new Map());
|
||||
}
|
||||
const hash = sharedHashes.get(key)!;
|
||||
for (let i = 0; i < args.length; i += 2) {
|
||||
hash.set(String(args[i]), String(args[i + 1]));
|
||||
}
|
||||
return 'OK';
|
||||
}),
|
||||
|
||||
// Set 操作
|
||||
sadd: vi.fn(async (key: string, ...members: string[]) => {
|
||||
if (!sharedSets.has(key)) {
|
||||
sharedSets.set(key, new Set());
|
||||
}
|
||||
const set = sharedSets.get(key)!;
|
||||
let added = 0;
|
||||
for (const member of members) {
|
||||
if (!set.has(member)) {
|
||||
set.add(member);
|
||||
added++;
|
||||
}
|
||||
}
|
||||
return added;
|
||||
}),
|
||||
srem: vi.fn(async (key: string, ...members: string[]) => {
|
||||
const set = sharedSets.get(key);
|
||||
if (!set) return 0;
|
||||
let removed = 0;
|
||||
for (const member of members) {
|
||||
if (set.delete(member)) removed++;
|
||||
}
|
||||
return removed;
|
||||
}),
|
||||
smembers: vi.fn(async (key: string) => {
|
||||
return Array.from(sharedSets.get(key) ?? []);
|
||||
}),
|
||||
|
||||
// Pub/Sub - 使用共享的处理器集合
|
||||
publish: vi.fn(async (channel: string, message: string) => {
|
||||
const handlers = sharedPubSubHandlers.get(channel);
|
||||
if (handlers) {
|
||||
for (const handler of handlers) {
|
||||
handler(channel, message);
|
||||
}
|
||||
}
|
||||
return handlers?.size ?? 0;
|
||||
}),
|
||||
subscribe: vi.fn(async (channel: string) => {
|
||||
// 注册 message 事件处理器到共享的 pub/sub 处理器
|
||||
const messageHandlers = eventHandlers.get('message');
|
||||
if (messageHandlers) {
|
||||
if (!sharedPubSubHandlers.has(channel)) {
|
||||
sharedPubSubHandlers.set(channel, new Set());
|
||||
}
|
||||
for (const handler of messageHandlers) {
|
||||
sharedPubSubHandlers.get(channel)!.add(handler as (channel: string, message: string) => void);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}),
|
||||
psubscribe: vi.fn(async () => 1),
|
||||
unsubscribe: vi.fn(async (channel: string) => {
|
||||
sharedPubSubHandlers.delete(channel);
|
||||
return 1;
|
||||
}),
|
||||
punsubscribe: vi.fn(async () => 1),
|
||||
|
||||
// 事件
|
||||
on: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
|
||||
if (!eventHandlers.has(event)) {
|
||||
eventHandlers.set(event, new Set());
|
||||
}
|
||||
eventHandlers.get(event)!.add(callback);
|
||||
}),
|
||||
off: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
|
||||
eventHandlers.get(event)?.delete(callback);
|
||||
}),
|
||||
|
||||
// Lua 脚本
|
||||
eval: vi.fn(async (script: string, numkeys: number, ...args: (string | number)[]) => {
|
||||
const key = String(args[0]);
|
||||
const token = String(args[1]);
|
||||
|
||||
// 释放锁脚本
|
||||
if (script.includes('redis.call("del"')) {
|
||||
if (sharedStore.get(key) === token) {
|
||||
sharedStore.delete(key);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 扩展锁脚本
|
||||
if (script.includes('redis.call("pexpire"')) {
|
||||
if (sharedStore.get(key) === token) {
|
||||
const ttlMs = Number(args[2]);
|
||||
sharedExpireTimes.set(key, Date.now() + ttlMs);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}),
|
||||
|
||||
// 连接
|
||||
duplicate: vi.fn(() => createMockRedisClient()),
|
||||
quit: vi.fn(async () => 'OK'),
|
||||
disconnect: vi.fn()
|
||||
};
|
||||
|
||||
return mockClient;
|
||||
}
|
||||
|
||||
describe('RedisAdapter', () => {
|
||||
let adapter: RedisAdapter;
|
||||
let mockClient: RedisClient;
|
||||
|
||||
beforeEach(async () => {
|
||||
clearSharedState();
|
||||
mockClient = createMockRedisClient();
|
||||
adapter = new RedisAdapter({
|
||||
factory: () => mockClient,
|
||||
prefix: 'test:'
|
||||
});
|
||||
await adapter.connect();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await adapter.disconnect();
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
describe('lifecycle', () => {
|
||||
it('should connect and disconnect', async () => {
|
||||
const newAdapter = new RedisAdapter({
|
||||
factory: () => createMockRedisClient()
|
||||
});
|
||||
expect(newAdapter.isConnected()).toBe(false);
|
||||
|
||||
await newAdapter.connect();
|
||||
expect(newAdapter.isConnected()).toBe(true);
|
||||
|
||||
await newAdapter.disconnect();
|
||||
expect(newAdapter.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
it('should not throw on double connect', async () => {
|
||||
await adapter.connect();
|
||||
expect(adapter.isConnected()).toBe(true);
|
||||
});
|
||||
|
||||
it('should not throw on double disconnect', async () => {
|
||||
await adapter.disconnect();
|
||||
await adapter.disconnect();
|
||||
expect(adapter.isConnected()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 服务器注册 | Server Registry
|
||||
// =========================================================================
|
||||
|
||||
describe('server registry', () => {
|
||||
const createServer = (id: string): ServerRegistration => ({
|
||||
serverId: id,
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
it('should register and get server', async () => {
|
||||
const server = createServer('server-1');
|
||||
await adapter.registerServer(server);
|
||||
|
||||
const result = await adapter.getServer('server-1');
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.serverId).toBe('server-1');
|
||||
expect(result?.address).toBe('localhost');
|
||||
expect(result?.port).toBe(3000);
|
||||
});
|
||||
|
||||
it('should get all online servers', async () => {
|
||||
await adapter.registerServer(createServer('server-1'));
|
||||
await adapter.registerServer(createServer('server-2'));
|
||||
|
||||
const servers = await adapter.getServers();
|
||||
expect(servers).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should unregister server', async () => {
|
||||
await adapter.registerServer(createServer('server-1'));
|
||||
await adapter.unregisterServer('server-1');
|
||||
|
||||
const result = await adapter.getServer('server-1');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should update server heartbeat', async () => {
|
||||
const server = createServer('server-1');
|
||||
await adapter.registerServer(server);
|
||||
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
await adapter.heartbeat('server-1');
|
||||
|
||||
const result = await adapter.getServer('server-1');
|
||||
expect(result?.lastHeartbeat).toBeGreaterThan(server.lastHeartbeat);
|
||||
});
|
||||
|
||||
it('should update server info', async () => {
|
||||
await adapter.registerServer(createServer('server-1'));
|
||||
await adapter.updateServer('server-1', { roomCount: 5, playerCount: 10 });
|
||||
|
||||
const result = await adapter.getServer('server-1');
|
||||
expect(result?.roomCount).toBe(5);
|
||||
expect(result?.playerCount).toBe(10);
|
||||
});
|
||||
|
||||
it('should publish draining event when status changes', async () => {
|
||||
await adapter.registerServer(createServer('server-1'));
|
||||
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('server:draining', handler);
|
||||
|
||||
await adapter.updateServer('server-1', { status: 'draining' });
|
||||
|
||||
expect(handler).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 房间注册 | Room Registry
|
||||
// =========================================================================
|
||||
|
||||
describe('room registry', () => {
|
||||
const createRoom = (id: string, serverId = 'server-1'): RoomRegistration => ({
|
||||
roomId: id,
|
||||
roomType: 'game',
|
||||
serverId,
|
||||
serverAddress: 'localhost:3000',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
});
|
||||
|
||||
it('should register and get room', async () => {
|
||||
const room = createRoom('room-1');
|
||||
await adapter.registerRoom(room);
|
||||
|
||||
const result = await adapter.getRoom('room-1');
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.roomId).toBe('room-1');
|
||||
expect(result?.roomType).toBe('game');
|
||||
});
|
||||
|
||||
it('should unregister room', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.unregisterRoom('room-1');
|
||||
|
||||
const result = await adapter.getRoom('room-1');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should update room info', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.updateRoom('room-1', { playerCount: 2, isLocked: true });
|
||||
|
||||
const result = await adapter.getRoom('room-1');
|
||||
expect(result?.playerCount).toBe(2);
|
||||
expect(result?.isLocked).toBe(true);
|
||||
});
|
||||
|
||||
it('should query rooms by type', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), roomType: 'lobby' });
|
||||
|
||||
const games = await adapter.queryRooms({ roomType: 'game' });
|
||||
expect(games).toHaveLength(1);
|
||||
expect(games[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should query rooms with space', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), playerCount: 4 });
|
||||
|
||||
const available = await adapter.queryRooms({ hasSpace: true });
|
||||
expect(available).toHaveLength(1);
|
||||
expect(available[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should query unlocked rooms', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), isLocked: true });
|
||||
|
||||
const unlocked = await adapter.queryRooms({ notLocked: true });
|
||||
expect(unlocked).toHaveLength(1);
|
||||
expect(unlocked[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should support pagination', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
await adapter.registerRoom(createRoom('room-2'));
|
||||
await adapter.registerRoom(createRoom('room-3'));
|
||||
|
||||
const page1 = await adapter.queryRooms({ limit: 2 });
|
||||
expect(page1).toHaveLength(2);
|
||||
|
||||
const page2 = await adapter.queryRooms({ offset: 2, limit: 2 });
|
||||
expect(page2).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should find available room', async () => {
|
||||
await adapter.registerRoom({ ...createRoom('room-1'), playerCount: 4 }); // full
|
||||
await adapter.registerRoom({ ...createRoom('room-2'), isLocked: true }); // locked
|
||||
await adapter.registerRoom(createRoom('room-3')); // available
|
||||
|
||||
const available = await adapter.findAvailableRoom('game');
|
||||
expect(available?.roomId).toBe('room-3');
|
||||
});
|
||||
|
||||
it('should get rooms by server', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-2',
|
||||
address: 'localhost',
|
||||
port: 3001,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await adapter.registerRoom(createRoom('room-1', 'server-1'));
|
||||
await adapter.registerRoom(createRoom('room-2', 'server-2'));
|
||||
|
||||
const server1Rooms = await adapter.getRoomsByServer('server-1');
|
||||
expect(server1Rooms).toHaveLength(1);
|
||||
expect(server1Rooms[0].roomId).toBe('room-1');
|
||||
});
|
||||
|
||||
it('should publish lock/unlock events', async () => {
|
||||
await adapter.registerRoom(createRoom('room-1'));
|
||||
|
||||
const lockHandler = vi.fn();
|
||||
const unlockHandler = vi.fn();
|
||||
await adapter.subscribe('room:locked', lockHandler);
|
||||
await adapter.subscribe('room:unlocked', unlockHandler);
|
||||
|
||||
await adapter.updateRoom('room-1', { isLocked: true });
|
||||
expect(lockHandler).toHaveBeenCalled();
|
||||
|
||||
await adapter.updateRoom('room-1', { isLocked: false });
|
||||
expect(unlockHandler).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 快照 | Snapshots
|
||||
// =========================================================================
|
||||
|
||||
describe('snapshots', () => {
|
||||
it('should save and load snapshot', async () => {
|
||||
const snapshot = {
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
state: { score: 100 },
|
||||
players: [{ id: 'player-1', data: { name: 'Alice' } }],
|
||||
version: 1,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
await adapter.saveSnapshot(snapshot);
|
||||
const result = await adapter.loadSnapshot('room-1');
|
||||
|
||||
expect(result).toEqual(snapshot);
|
||||
});
|
||||
|
||||
it('should return null for non-existent snapshot', async () => {
|
||||
const result = await adapter.loadSnapshot('non-existent');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should delete snapshot', async () => {
|
||||
await adapter.saveSnapshot({
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
state: {},
|
||||
players: [],
|
||||
version: 1,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
await adapter.deleteSnapshot('room-1');
|
||||
const result = await adapter.loadSnapshot('room-1');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 发布/订阅 | Pub/Sub
|
||||
// =========================================================================
|
||||
|
||||
describe('pub/sub', () => {
|
||||
it('should publish and subscribe to events', async () => {
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('room:created', handler);
|
||||
|
||||
const event: DistributedEvent = {
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
roomId: 'room-1',
|
||||
payload: { roomType: 'game' },
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
await adapter.publish(event);
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(event);
|
||||
});
|
||||
|
||||
it('should support wildcard subscription', async () => {
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('*', handler);
|
||||
|
||||
await adapter.publish({
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
await adapter.publish({
|
||||
type: 'server:online',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
expect(handler).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should unsubscribe correctly', async () => {
|
||||
const handler = vi.fn();
|
||||
const unsub = await adapter.subscribe('room:created', handler);
|
||||
|
||||
unsub();
|
||||
|
||||
await adapter.publish({
|
||||
type: 'room:created',
|
||||
serverId: 'server-1',
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should send to room', async () => {
|
||||
await adapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
await adapter.registerRoom({
|
||||
roomId: 'room-1',
|
||||
roomType: 'game',
|
||||
serverId: 'server-1',
|
||||
serverAddress: 'localhost:3000',
|
||||
playerCount: 0,
|
||||
maxPlayers: 4,
|
||||
isLocked: false,
|
||||
metadata: {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
|
||||
const handler = vi.fn();
|
||||
await adapter.subscribe('room:message', handler);
|
||||
|
||||
await adapter.sendToRoom('room-1', 'chat', { text: 'hello' }, 'player-1');
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'room:message',
|
||||
roomId: 'room-1',
|
||||
payload: {
|
||||
messageType: 'chat',
|
||||
data: { text: 'hello' },
|
||||
playerId: 'player-1'
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 分布式锁 | Distributed Locks
|
||||
// =========================================================================
|
||||
|
||||
describe('distributed locks', () => {
|
||||
it('should acquire and release lock', async () => {
|
||||
const acquired = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquired).toBe(true);
|
||||
|
||||
await adapter.releaseLock('test-lock');
|
||||
|
||||
const acquiredAgain = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquiredAgain).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail to acquire held lock', async () => {
|
||||
await adapter.acquireLock('test-lock', 5000);
|
||||
|
||||
const acquiredAgain = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquiredAgain).toBe(false);
|
||||
});
|
||||
|
||||
it('should extend lock', async () => {
|
||||
await adapter.acquireLock('test-lock', 100);
|
||||
|
||||
const extended = await adapter.extendLock('test-lock', 5000);
|
||||
expect(extended).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail to extend non-existent lock', async () => {
|
||||
const extended = await adapter.extendLock('non-existent', 5000);
|
||||
expect(extended).toBe(false);
|
||||
});
|
||||
|
||||
it('should fail to release lock without token', async () => {
|
||||
// 没有获取锁就释放,应该什么都不做
|
||||
await adapter.releaseLock('test-lock');
|
||||
|
||||
// 仍然可以获取锁
|
||||
const acquired = await adapter.acquireLock('test-lock', 5000);
|
||||
expect(acquired).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 错误处理 | Error Handling
|
||||
// =========================================================================
|
||||
|
||||
describe('error handling', () => {
|
||||
it('should throw when not connected', async () => {
|
||||
const disconnected = new RedisAdapter({
|
||||
factory: () => createMockRedisClient()
|
||||
});
|
||||
|
||||
await expect(disconnected.registerServer({} as ServerRegistration))
|
||||
.rejects.toThrow('RedisAdapter is not connected');
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
// 配置 | Configuration
|
||||
// =========================================================================
|
||||
|
||||
describe('configuration', () => {
|
||||
it('should use default prefix', async () => {
|
||||
const testMockClient = createMockRedisClient();
|
||||
const defaultAdapter = new RedisAdapter({
|
||||
factory: () => testMockClient
|
||||
});
|
||||
await defaultAdapter.connect();
|
||||
|
||||
await defaultAdapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
// hmset 应该被调用,key 应该包含默认前缀 'dist:'
|
||||
expect(testMockClient.hmset).toHaveBeenCalled();
|
||||
|
||||
await defaultAdapter.disconnect();
|
||||
});
|
||||
|
||||
it('should use custom prefix', async () => {
|
||||
const testMockClient = createMockRedisClient();
|
||||
const customAdapter = new RedisAdapter({
|
||||
factory: () => testMockClient,
|
||||
prefix: 'game:'
|
||||
});
|
||||
await customAdapter.connect();
|
||||
|
||||
await customAdapter.registerServer({
|
||||
serverId: 'server-1',
|
||||
address: 'localhost',
|
||||
port: 3000,
|
||||
roomCount: 0,
|
||||
playerCount: 0,
|
||||
capacity: 100,
|
||||
status: 'online',
|
||||
lastHeartbeat: Date.now()
|
||||
});
|
||||
|
||||
// hmset 应该被调用
|
||||
expect(testMockClient.hmset).toHaveBeenCalled();
|
||||
|
||||
await customAdapter.disconnect();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,257 @@
|
||||
/**
|
||||
* @zh 分布式适配器接口
|
||||
* @en Distributed adapter interface
|
||||
*
|
||||
* @zh 定义分布式房间系统的存储和通信层抽象
|
||||
* @en Defines the storage and communication layer abstraction for distributed room system
|
||||
*/
|
||||
|
||||
import type {
|
||||
ServerRegistration,
|
||||
RoomRegistration,
|
||||
RoomQuery,
|
||||
RoomSnapshot,
|
||||
DistributedEvent,
|
||||
DistributedEventType,
|
||||
DistributedEventHandler,
|
||||
Unsubscribe
|
||||
} from '../types.js';
|
||||
|
||||
/**
|
||||
* @zh 分布式适配器接口
|
||||
* @en Distributed adapter interface
|
||||
*
|
||||
* @zh 所有分布式后端(Redis、消息队列等)都需要实现此接口
|
||||
* @en All distributed backends (Redis, message queue, etc.) must implement this interface
|
||||
*/
|
||||
export interface IDistributedAdapter {
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 连接到分布式后端
|
||||
* @en Connect to distributed backend
|
||||
*/
|
||||
connect(): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 断开连接
|
||||
* @en Disconnect from backend
|
||||
*/
|
||||
disconnect(): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 检查是否已连接
|
||||
* @en Check if connected
|
||||
*/
|
||||
isConnected(): boolean;
|
||||
|
||||
// =========================================================================
|
||||
// 服务器注册 | Server Registry
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 注册服务器
|
||||
* @en Register server
|
||||
*
|
||||
* @param server - 服务器注册信息 | Server registration info
|
||||
*/
|
||||
registerServer(server: ServerRegistration): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 注销服务器
|
||||
* @en Unregister server
|
||||
*
|
||||
* @param serverId - 服务器 ID | Server ID
|
||||
*/
|
||||
unregisterServer(serverId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 更新服务器心跳
|
||||
* @en Update server heartbeat
|
||||
*
|
||||
* @param serverId - 服务器 ID | Server ID
|
||||
*/
|
||||
heartbeat(serverId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 获取所有在线服务器
|
||||
* @en Get all online servers
|
||||
*/
|
||||
getServers(): Promise<ServerRegistration[]>;
|
||||
|
||||
/**
|
||||
* @zh 获取指定服务器
|
||||
* @en Get specific server
|
||||
*
|
||||
* @param serverId - 服务器 ID | Server ID
|
||||
*/
|
||||
getServer(serverId: string): Promise<ServerRegistration | null>;
|
||||
|
||||
/**
|
||||
* @zh 更新服务器信息
|
||||
* @en Update server info
|
||||
*
|
||||
* @param serverId - 服务器 ID | Server ID
|
||||
* @param updates - 更新内容 | Updates
|
||||
*/
|
||||
updateServer(serverId: string, updates: Partial<ServerRegistration>): Promise<void>;
|
||||
|
||||
// =========================================================================
|
||||
// 房间注册 | Room Registry
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 注册房间
|
||||
* @en Register room
|
||||
*
|
||||
* @param room - 房间注册信息 | Room registration info
|
||||
*/
|
||||
registerRoom(room: RoomRegistration): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 注销房间
|
||||
* @en Unregister room
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
*/
|
||||
unregisterRoom(roomId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 更新房间信息
|
||||
* @en Update room info
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
* @param updates - 更新内容 | Updates
|
||||
*/
|
||||
updateRoom(roomId: string, updates: Partial<RoomRegistration>): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 获取房间信息
|
||||
* @en Get room info
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
*/
|
||||
getRoom(roomId: string): Promise<RoomRegistration | null>;
|
||||
|
||||
/**
|
||||
* @zh 查询房间列表
|
||||
* @en Query room list
|
||||
*
|
||||
* @param query - 查询条件 | Query criteria
|
||||
*/
|
||||
queryRooms(query: RoomQuery): Promise<RoomRegistration[]>;
|
||||
|
||||
/**
|
||||
* @zh 获取指定类型的可用房间(用于 joinOrCreate)
|
||||
* @en Get available room of type (for joinOrCreate)
|
||||
*
|
||||
* @param roomType - 房间类型 | Room type
|
||||
*/
|
||||
findAvailableRoom(roomType: string): Promise<RoomRegistration | null>;
|
||||
|
||||
/**
|
||||
* @zh 获取服务器的所有房间
|
||||
* @en Get all rooms of a server
|
||||
*
|
||||
* @param serverId - 服务器 ID | Server ID
|
||||
*/
|
||||
getRoomsByServer(serverId: string): Promise<RoomRegistration[]>;
|
||||
|
||||
// =========================================================================
|
||||
// 房间状态 | Room State
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 保存房间状态快照
|
||||
* @en Save room state snapshot
|
||||
*
|
||||
* @param snapshot - 状态快照 | State snapshot
|
||||
*/
|
||||
saveSnapshot(snapshot: RoomSnapshot): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 加载房间状态快照
|
||||
* @en Load room state snapshot
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
*/
|
||||
loadSnapshot(roomId: string): Promise<RoomSnapshot | null>;
|
||||
|
||||
/**
|
||||
* @zh 删除房间状态
|
||||
* @en Delete room state
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
*/
|
||||
deleteSnapshot(roomId: string): Promise<void>;
|
||||
|
||||
// =========================================================================
|
||||
// 发布/订阅 | Pub/Sub
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 发布事件
|
||||
* @en Publish event
|
||||
*
|
||||
* @param event - 分布式事件 | Distributed event
|
||||
*/
|
||||
publish(event: DistributedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 订阅事件
|
||||
* @en Subscribe to events
|
||||
*
|
||||
* @param pattern - 事件类型模式(支持 '*' 通配符) | Event type pattern (supports '*' wildcard)
|
||||
* @param handler - 事件处理器 | Event handler
|
||||
* @returns 取消订阅函数 | Unsubscribe function
|
||||
*/
|
||||
subscribe(
|
||||
pattern: DistributedEventType | '*',
|
||||
handler: DistributedEventHandler
|
||||
): Promise<Unsubscribe>;
|
||||
|
||||
/**
|
||||
* @zh 向特定房间发送消息(跨服务器)
|
||||
* @en Send message to specific room (cross-server)
|
||||
*
|
||||
* @param roomId - 房间 ID | Room ID
|
||||
* @param messageType - 消息类型 | Message type
|
||||
* @param data - 消息数据 | Message data
|
||||
* @param playerId - 发送者玩家 ID(可选) | Sender player ID (optional)
|
||||
*/
|
||||
sendToRoom(roomId: string, messageType: string, data: unknown, playerId?: string): Promise<void>;
|
||||
|
||||
// =========================================================================
|
||||
// 分布式锁 | Distributed Lock
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 获取分布式锁
|
||||
* @en Acquire distributed lock
|
||||
*
|
||||
* @param key - 锁的键名 | Lock key
|
||||
* @param ttlMs - 锁的生存时间(毫秒) | Lock TTL (ms)
|
||||
* @returns 是否成功获取锁 | Whether lock was acquired
|
||||
*/
|
||||
acquireLock(key: string, ttlMs: number): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* @zh 释放分布式锁
|
||||
* @en Release distributed lock
|
||||
*
|
||||
* @param key - 锁的键名 | Lock key
|
||||
*/
|
||||
releaseLock(key: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* @zh 扩展锁的生存时间
|
||||
* @en Extend lock TTL
|
||||
*
|
||||
* @param key - 锁的键名 | Lock key
|
||||
* @param ttlMs - 新的生存时间(毫秒) | New TTL (ms)
|
||||
* @returns 是否成功扩展 | Whether extension was successful
|
||||
*/
|
||||
extendLock(key: string, ttlMs: number): Promise<boolean>;
|
||||
}
|
||||
@@ -0,0 +1,503 @@
|
||||
/**
|
||||
* @zh 内存分布式适配器
|
||||
* @en Memory distributed adapter
|
||||
*
|
||||
* @zh 用于单机模式和测试的内存实现。所有数据存储在进程内存中。
|
||||
* @en In-memory implementation for single-server mode and testing. All data stored in process memory.
|
||||
*/
|
||||
|
||||
import type { IDistributedAdapter } from './IDistributedAdapter.js';
|
||||
import type {
|
||||
ServerRegistration,
|
||||
RoomRegistration,
|
||||
RoomQuery,
|
||||
RoomSnapshot,
|
||||
DistributedEvent,
|
||||
DistributedEventType,
|
||||
DistributedEventHandler,
|
||||
Unsubscribe
|
||||
} from '../types.js';
|
||||
|
||||
/**
|
||||
* @zh 内存适配器配置
|
||||
* @en Memory adapter configuration
|
||||
*/
|
||||
export interface MemoryAdapterConfig {
|
||||
/**
|
||||
* @zh 服务器 TTL(毫秒),超时后视为离线
|
||||
* @en Server TTL (ms), considered offline after timeout
|
||||
* @default 15000
|
||||
*/
|
||||
serverTtl?: number;
|
||||
|
||||
/**
|
||||
* @zh 是否启用 TTL 检查
|
||||
* @en Whether to enable TTL check
|
||||
* @default true
|
||||
*/
|
||||
enableTtlCheck?: boolean;
|
||||
|
||||
/**
|
||||
* @zh TTL 检查间隔(毫秒)
|
||||
* @en TTL check interval (ms)
|
||||
* @default 5000
|
||||
*/
|
||||
ttlCheckInterval?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 内存分布式适配器
|
||||
* @en Memory distributed adapter
|
||||
*/
|
||||
export class MemoryAdapter implements IDistributedAdapter {
|
||||
private readonly _config: Required<MemoryAdapterConfig>;
|
||||
private _connected = false;
|
||||
|
||||
// 存储
|
||||
private readonly _servers = new Map<string, ServerRegistration>();
|
||||
private readonly _rooms = new Map<string, RoomRegistration>();
|
||||
private readonly _snapshots = new Map<string, RoomSnapshot>();
|
||||
private readonly _locks = new Map<string, { owner: string; expireAt: number }>();
|
||||
|
||||
// 事件订阅
|
||||
private readonly _subscribers = new Map<string, Set<DistributedEventHandler>>();
|
||||
private _subscriberId = 0;
|
||||
|
||||
// TTL 检查定时器
|
||||
private _ttlCheckTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
constructor(config: MemoryAdapterConfig = {}) {
|
||||
this._config = {
|
||||
serverTtl: 15000,
|
||||
enableTtlCheck: true,
|
||||
ttlCheckInterval: 5000,
|
||||
...config
|
||||
};
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this._connected) return;
|
||||
|
||||
this._connected = true;
|
||||
|
||||
if (this._config.enableTtlCheck) {
|
||||
this._ttlCheckTimer = setInterval(
|
||||
() => this._checkServerTtl(),
|
||||
this._config.ttlCheckInterval
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (!this._connected) return;
|
||||
|
||||
if (this._ttlCheckTimer) {
|
||||
clearInterval(this._ttlCheckTimer);
|
||||
this._ttlCheckTimer = null;
|
||||
}
|
||||
|
||||
this._connected = false;
|
||||
this._servers.clear();
|
||||
this._rooms.clear();
|
||||
this._snapshots.clear();
|
||||
this._locks.clear();
|
||||
this._subscribers.clear();
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this._connected;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 服务器注册 | Server Registry
|
||||
// =========================================================================
|
||||
|
||||
async registerServer(server: ServerRegistration): Promise<void> {
|
||||
this._ensureConnected();
|
||||
this._servers.set(server.serverId, { ...server, lastHeartbeat: Date.now() });
|
||||
|
||||
await this.publish({
|
||||
type: 'server:online',
|
||||
serverId: server.serverId,
|
||||
payload: server,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async unregisterServer(serverId: string): Promise<void> {
|
||||
this._ensureConnected();
|
||||
const server = this._servers.get(serverId);
|
||||
if (!server) return;
|
||||
|
||||
this._servers.delete(serverId);
|
||||
|
||||
// 清理该服务器的所有房间
|
||||
for (const [roomId, room] of this._rooms) {
|
||||
if (room.serverId === serverId) {
|
||||
this._rooms.delete(roomId);
|
||||
}
|
||||
}
|
||||
|
||||
await this.publish({
|
||||
type: 'server:offline',
|
||||
serverId,
|
||||
payload: { serverId },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async heartbeat(serverId: string): Promise<void> {
|
||||
this._ensureConnected();
|
||||
const server = this._servers.get(serverId);
|
||||
if (server) {
|
||||
server.lastHeartbeat = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
async getServers(): Promise<ServerRegistration[]> {
|
||||
this._ensureConnected();
|
||||
return Array.from(this._servers.values()).filter(s => s.status === 'online');
|
||||
}
|
||||
|
||||
async getServer(serverId: string): Promise<ServerRegistration | null> {
|
||||
this._ensureConnected();
|
||||
return this._servers.get(serverId) ?? null;
|
||||
}
|
||||
|
||||
async updateServer(serverId: string, updates: Partial<ServerRegistration>): Promise<void> {
|
||||
this._ensureConnected();
|
||||
const server = this._servers.get(serverId);
|
||||
if (server) {
|
||||
Object.assign(server, updates);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 房间注册 | Room Registry
|
||||
// =========================================================================
|
||||
|
||||
async registerRoom(room: RoomRegistration): Promise<void> {
|
||||
this._ensureConnected();
|
||||
this._rooms.set(room.roomId, { ...room });
|
||||
|
||||
// 更新服务器的房间计数
|
||||
const server = this._servers.get(room.serverId);
|
||||
if (server) {
|
||||
server.roomCount = this._countRoomsByServer(room.serverId);
|
||||
}
|
||||
|
||||
await this.publish({
|
||||
type: 'room:created',
|
||||
serverId: room.serverId,
|
||||
roomId: room.roomId,
|
||||
payload: { roomType: room.roomType },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async unregisterRoom(roomId: string): Promise<void> {
|
||||
this._ensureConnected();
|
||||
const room = this._rooms.get(roomId);
|
||||
if (!room) return;
|
||||
|
||||
this._rooms.delete(roomId);
|
||||
this._snapshots.delete(roomId);
|
||||
|
||||
// 更新服务器的房间计数
|
||||
const server = this._servers.get(room.serverId);
|
||||
if (server) {
|
||||
server.roomCount = this._countRoomsByServer(room.serverId);
|
||||
}
|
||||
|
||||
await this.publish({
|
||||
type: 'room:disposed',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async updateRoom(roomId: string, updates: Partial<RoomRegistration>): Promise<void> {
|
||||
this._ensureConnected();
|
||||
const room = this._rooms.get(roomId);
|
||||
if (!room) return;
|
||||
|
||||
Object.assign(room, updates, { updatedAt: Date.now() });
|
||||
|
||||
await this.publish({
|
||||
type: 'room:updated',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: updates,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async getRoom(roomId: string): Promise<RoomRegistration | null> {
|
||||
this._ensureConnected();
|
||||
return this._rooms.get(roomId) ?? null;
|
||||
}
|
||||
|
||||
async queryRooms(query: RoomQuery): Promise<RoomRegistration[]> {
|
||||
this._ensureConnected();
|
||||
|
||||
let results = Array.from(this._rooms.values());
|
||||
|
||||
// 按类型过滤
|
||||
if (query.roomType) {
|
||||
results = results.filter(r => r.roomType === query.roomType);
|
||||
}
|
||||
|
||||
// 按空位过滤
|
||||
if (query.hasSpace) {
|
||||
results = results.filter(r => r.playerCount < r.maxPlayers);
|
||||
}
|
||||
|
||||
// 按锁定状态过滤
|
||||
if (query.notLocked) {
|
||||
results = results.filter(r => !r.isLocked);
|
||||
}
|
||||
|
||||
// 按元数据过滤
|
||||
if (query.metadata) {
|
||||
results = results.filter(r => {
|
||||
for (const [key, value] of Object.entries(query.metadata!)) {
|
||||
if (r.metadata[key] !== value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// 分页
|
||||
if (query.offset) {
|
||||
results = results.slice(query.offset);
|
||||
}
|
||||
if (query.limit) {
|
||||
results = results.slice(0, query.limit);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
async findAvailableRoom(roomType: string): Promise<RoomRegistration | null> {
|
||||
const rooms = await this.queryRooms({
|
||||
roomType,
|
||||
hasSpace: true,
|
||||
notLocked: true,
|
||||
limit: 1
|
||||
});
|
||||
return rooms[0] ?? null;
|
||||
}
|
||||
|
||||
async getRoomsByServer(serverId: string): Promise<RoomRegistration[]> {
|
||||
this._ensureConnected();
|
||||
return Array.from(this._rooms.values()).filter(r => r.serverId === serverId);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 房间状态 | Room State
|
||||
// =========================================================================
|
||||
|
||||
async saveSnapshot(snapshot: RoomSnapshot): Promise<void> {
|
||||
this._ensureConnected();
|
||||
this._snapshots.set(snapshot.roomId, { ...snapshot });
|
||||
}
|
||||
|
||||
async loadSnapshot(roomId: string): Promise<RoomSnapshot | null> {
|
||||
this._ensureConnected();
|
||||
return this._snapshots.get(roomId) ?? null;
|
||||
}
|
||||
|
||||
async deleteSnapshot(roomId: string): Promise<void> {
|
||||
this._ensureConnected();
|
||||
this._snapshots.delete(roomId);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 发布/订阅 | Pub/Sub
|
||||
// =========================================================================
|
||||
|
||||
async publish(event: DistributedEvent): Promise<void> {
|
||||
this._ensureConnected();
|
||||
|
||||
// 通知所有匹配的订阅者
|
||||
const wildcardHandlers = this._subscribers.get('*') ?? new Set();
|
||||
const typeHandlers = this._subscribers.get(event.type) ?? new Set();
|
||||
|
||||
for (const handler of wildcardHandlers) {
|
||||
try {
|
||||
handler(event);
|
||||
} catch (error) {
|
||||
console.error('Event handler error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
for (const handler of typeHandlers) {
|
||||
try {
|
||||
handler(event);
|
||||
} catch (error) {
|
||||
console.error('Event handler error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async subscribe(
|
||||
pattern: DistributedEventType | '*',
|
||||
handler: DistributedEventHandler
|
||||
): Promise<Unsubscribe> {
|
||||
this._ensureConnected();
|
||||
|
||||
if (!this._subscribers.has(pattern)) {
|
||||
this._subscribers.set(pattern, new Set());
|
||||
}
|
||||
|
||||
this._subscribers.get(pattern)!.add(handler);
|
||||
|
||||
return () => {
|
||||
const handlers = this._subscribers.get(pattern);
|
||||
if (handlers) {
|
||||
handlers.delete(handler);
|
||||
if (handlers.size === 0) {
|
||||
this._subscribers.delete(pattern);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async sendToRoom(
|
||||
roomId: string,
|
||||
messageType: string,
|
||||
data: unknown,
|
||||
playerId?: string
|
||||
): Promise<void> {
|
||||
this._ensureConnected();
|
||||
|
||||
const room = this._rooms.get(roomId);
|
||||
if (!room) return;
|
||||
|
||||
await this.publish({
|
||||
type: 'room:message',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: { messageType, data, playerId },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 分布式锁 | Distributed Lock
|
||||
// =========================================================================
|
||||
|
||||
async acquireLock(key: string, ttlMs: number): Promise<boolean> {
|
||||
this._ensureConnected();
|
||||
|
||||
const now = Date.now();
|
||||
const existing = this._locks.get(key);
|
||||
|
||||
// 检查锁是否已过期
|
||||
if (existing && existing.expireAt > now) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取锁
|
||||
const owner = `lock_${++this._subscriberId}`;
|
||||
this._locks.set(key, { owner, expireAt: now + ttlMs });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async releaseLock(key: string): Promise<void> {
|
||||
this._ensureConnected();
|
||||
this._locks.delete(key);
|
||||
}
|
||||
|
||||
async extendLock(key: string, ttlMs: number): Promise<boolean> {
|
||||
this._ensureConnected();
|
||||
|
||||
const lock = this._locks.get(key);
|
||||
if (!lock) return false;
|
||||
|
||||
lock.expireAt = Date.now() + ttlMs;
|
||||
return true;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 私有方法 | Private Methods
|
||||
// =========================================================================
|
||||
|
||||
private _ensureConnected(): void {
|
||||
if (!this._connected) {
|
||||
throw new Error('MemoryAdapter is not connected');
|
||||
}
|
||||
}
|
||||
|
||||
private _countRoomsByServer(serverId: string): number {
|
||||
let count = 0;
|
||||
for (const room of this._rooms.values()) {
|
||||
if (room.serverId === serverId) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private async _checkServerTtl(): Promise<void> {
|
||||
const now = Date.now();
|
||||
const expiredServers: string[] = [];
|
||||
|
||||
for (const [serverId, server] of this._servers) {
|
||||
if (server.status === 'online' && now - server.lastHeartbeat > this._config.serverTtl) {
|
||||
server.status = 'offline';
|
||||
expiredServers.push(serverId);
|
||||
}
|
||||
}
|
||||
|
||||
// 发布服务器离线事件
|
||||
for (const serverId of expiredServers) {
|
||||
await this.publish({
|
||||
type: 'server:offline',
|
||||
serverId,
|
||||
payload: { serverId, reason: 'heartbeat_timeout' },
|
||||
timestamp: now
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 测试辅助方法 | Test Helper Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 清除所有数据(仅用于测试)
|
||||
* @en Clear all data (for testing only)
|
||||
*/
|
||||
_clear(): void {
|
||||
this._servers.clear();
|
||||
this._rooms.clear();
|
||||
this._snapshots.clear();
|
||||
this._locks.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取内部状态(仅用于测试)
|
||||
* @en Get internal state (for testing only)
|
||||
*/
|
||||
_getState(): {
|
||||
servers: Map<string, ServerRegistration>;
|
||||
rooms: Map<string, RoomRegistration>;
|
||||
snapshots: Map<string, RoomSnapshot>;
|
||||
} {
|
||||
return {
|
||||
servers: this._servers,
|
||||
rooms: this._rooms,
|
||||
snapshots: this._snapshots
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,789 @@
|
||||
/**
|
||||
* @zh Redis 分布式适配器
|
||||
* @en Redis distributed adapter
|
||||
*
|
||||
* @zh 基于 Redis 的分布式房间适配器,支持 Pub/Sub、分布式锁和状态持久化
|
||||
* @en Redis-based distributed room adapter with Pub/Sub, distributed lock and state persistence
|
||||
*/
|
||||
|
||||
import type { IDistributedAdapter } from './IDistributedAdapter.js';
|
||||
import type {
|
||||
ServerRegistration,
|
||||
RoomRegistration,
|
||||
RoomQuery,
|
||||
RoomSnapshot,
|
||||
DistributedEvent,
|
||||
DistributedEventType,
|
||||
DistributedEventHandler,
|
||||
Unsubscribe
|
||||
} from '../types.js';
|
||||
|
||||
/**
|
||||
* @zh Redis 客户端接口(兼容 ioredis)
|
||||
* @en Redis client interface (compatible with ioredis)
|
||||
*/
|
||||
export interface RedisClient {
|
||||
// 基础操作
|
||||
get(key: string): Promise<string | null>;
|
||||
set(key: string, value: string, ...args: (string | number)[]): Promise<string | null>;
|
||||
del(...keys: string[]): Promise<number>;
|
||||
expire(key: string, seconds: number): Promise<number>;
|
||||
ttl(key: string): Promise<number>;
|
||||
|
||||
// Hash 操作
|
||||
hget(key: string, field: string): Promise<string | null>;
|
||||
hset(key: string, ...args: (string | number | Buffer)[]): Promise<number>;
|
||||
hdel(key: string, ...fields: string[]): Promise<number>;
|
||||
hgetall(key: string): Promise<Record<string, string>>;
|
||||
hmset(key: string, ...args: (string | number | Buffer)[]): Promise<'OK'>;
|
||||
|
||||
// Set 操作
|
||||
sadd(key: string, ...members: string[]): Promise<number>;
|
||||
srem(key: string, ...members: string[]): Promise<number>;
|
||||
smembers(key: string): Promise<string[]>;
|
||||
|
||||
// Pub/Sub
|
||||
publish(channel: string, message: string): Promise<number>;
|
||||
subscribe(channel: string): Promise<number>;
|
||||
psubscribe(pattern: string): Promise<number>;
|
||||
unsubscribe(...channels: string[]): Promise<number>;
|
||||
punsubscribe(...patterns: string[]): Promise<number>;
|
||||
|
||||
// 事件(重载支持 message 事件的类型安全)
|
||||
on(event: 'message', callback: (channel: string, message: string) => void): void;
|
||||
on(event: 'pmessage', callback: (pattern: string, channel: string, message: string) => void): void;
|
||||
on(event: string, callback: (...args: unknown[]) => void): void;
|
||||
off(event: 'message', callback: (channel: string, message: string) => void): void;
|
||||
off(event: 'pmessage', callback: (pattern: string, channel: string, message: string) => void): void;
|
||||
off(event: string, callback: (...args: unknown[]) => void): void;
|
||||
|
||||
// Lua 脚本
|
||||
eval(script: string, numkeys: number, ...args: (string | number)[]): Promise<unknown>;
|
||||
|
||||
// 连接
|
||||
duplicate(): RedisClient;
|
||||
quit(): Promise<'OK'>;
|
||||
disconnect(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh Redis 连接工厂
|
||||
* @en Redis connection factory
|
||||
*/
|
||||
export type RedisClientFactory = () => RedisClient | Promise<RedisClient>;
|
||||
|
||||
/**
|
||||
* @zh Redis 适配器配置
|
||||
* @en Redis adapter configuration
|
||||
*/
|
||||
export interface RedisAdapterConfig {
|
||||
/**
|
||||
* @zh Redis 客户端工厂(惰性连接)
|
||||
* @en Redis client factory (lazy connection)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import Redis from 'ioredis'
|
||||
* const adapter = new RedisAdapter({
|
||||
* factory: () => new Redis('redis://localhost:6379')
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
factory: RedisClientFactory;
|
||||
|
||||
/**
|
||||
* @zh 键前缀
|
||||
* @en Key prefix
|
||||
* @default 'dist:'
|
||||
*/
|
||||
prefix?: string;
|
||||
|
||||
/**
|
||||
* @zh 服务器 TTL(秒)
|
||||
* @en Server TTL (seconds)
|
||||
* @default 30
|
||||
*/
|
||||
serverTtl?: number;
|
||||
|
||||
/**
|
||||
* @zh 房间 TTL(秒),0 = 永不过期
|
||||
* @en Room TTL (seconds), 0 = never expire
|
||||
* @default 0
|
||||
*/
|
||||
roomTtl?: number;
|
||||
|
||||
/**
|
||||
* @zh 快照 TTL(秒)
|
||||
* @en Snapshot TTL (seconds)
|
||||
* @default 86400 (24 hours)
|
||||
*/
|
||||
snapshotTtl?: number;
|
||||
|
||||
/**
|
||||
* @zh Pub/Sub 频道名
|
||||
* @en Pub/Sub channel name
|
||||
* @default 'distributed:events'
|
||||
*/
|
||||
channel?: string;
|
||||
}
|
||||
|
||||
// Lua 脚本:安全释放锁
|
||||
const RELEASE_LOCK_SCRIPT = `
|
||||
if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("del", KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end
|
||||
`;
|
||||
|
||||
// Lua 脚本:扩展锁 TTL
|
||||
const EXTEND_LOCK_SCRIPT = `
|
||||
if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("pexpire", KEYS[1], ARGV[2])
|
||||
else
|
||||
return 0
|
||||
end
|
||||
`;
|
||||
|
||||
/**
|
||||
* @zh Redis 分布式适配器
|
||||
* @en Redis distributed adapter
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import Redis from 'ioredis'
|
||||
* import { RedisAdapter, DistributedRoomManager } from '@esengine/server'
|
||||
*
|
||||
* const adapter = new RedisAdapter({
|
||||
* factory: () => new Redis('redis://localhost:6379'),
|
||||
* prefix: 'game:'
|
||||
* })
|
||||
*
|
||||
* const manager = new DistributedRoomManager(adapter, {
|
||||
* serverId: 'server-1',
|
||||
* serverAddress: 'localhost',
|
||||
* serverPort: 3000
|
||||
* }, sendFn)
|
||||
*
|
||||
* await manager.start()
|
||||
* ```
|
||||
*/
|
||||
export class RedisAdapter implements IDistributedAdapter {
|
||||
private readonly _config: Required<RedisAdapterConfig>;
|
||||
private _client: RedisClient | null = null;
|
||||
private _subscriber: RedisClient | null = null;
|
||||
private _connected = false;
|
||||
|
||||
// 锁的 owner token(用于安全释放)
|
||||
private readonly _lockTokens = new Map<string, string>();
|
||||
|
||||
// 事件处理器
|
||||
private readonly _handlers = new Map<string, Set<DistributedEventHandler>>();
|
||||
private _messageHandler: ((channel: string, message: string) => void) | null = null;
|
||||
|
||||
constructor(config: RedisAdapterConfig) {
|
||||
this._config = {
|
||||
prefix: 'dist:',
|
||||
serverTtl: 30,
|
||||
roomTtl: 0,
|
||||
snapshotTtl: 86400,
|
||||
channel: 'distributed:events',
|
||||
...config,
|
||||
factory: config.factory
|
||||
};
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Key 生成器 | Key Generators
|
||||
// =========================================================================
|
||||
|
||||
private _key(type: string, id?: string): string {
|
||||
return id
|
||||
? `${this._config.prefix}${type}:${id}`
|
||||
: `${this._config.prefix}${type}`;
|
||||
}
|
||||
|
||||
private _serverKey(serverId: string): string {
|
||||
return this._key('server', serverId);
|
||||
}
|
||||
|
||||
private _roomKey(roomId: string): string {
|
||||
return this._key('room', roomId);
|
||||
}
|
||||
|
||||
private _snapshotKey(roomId: string): string {
|
||||
return this._key('snapshot', roomId);
|
||||
}
|
||||
|
||||
private _lockKey(key: string): string {
|
||||
return this._key('lock', key);
|
||||
}
|
||||
|
||||
private _serversSetKey(): string {
|
||||
return this._key('servers');
|
||||
}
|
||||
|
||||
private _roomsSetKey(): string {
|
||||
return this._key('rooms');
|
||||
}
|
||||
|
||||
private _serverRoomsKey(serverId: string): string {
|
||||
return this._key('server-rooms', serverId);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 生命周期 | Lifecycle
|
||||
// =========================================================================
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this._connected) return;
|
||||
|
||||
// 创建主客户端
|
||||
this._client = await this._config.factory();
|
||||
|
||||
// 创建订阅专用客户端
|
||||
this._subscriber = this._client.duplicate();
|
||||
|
||||
// 设置消息处理器
|
||||
this._messageHandler = (channel: string, message: string) => {
|
||||
if (channel !== this._config.channel) return;
|
||||
|
||||
try {
|
||||
const event: DistributedEvent = JSON.parse(message);
|
||||
this._dispatchEvent(event);
|
||||
} catch (error) {
|
||||
console.error('[RedisAdapter] Failed to parse event:', error);
|
||||
}
|
||||
};
|
||||
|
||||
this._subscriber.on('message', this._messageHandler);
|
||||
await this._subscriber.subscribe(this._config.channel);
|
||||
|
||||
this._connected = true;
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (!this._connected) return;
|
||||
|
||||
// 清理订阅
|
||||
if (this._subscriber) {
|
||||
if (this._messageHandler) {
|
||||
this._subscriber.off('message', this._messageHandler);
|
||||
}
|
||||
await this._subscriber.unsubscribe(this._config.channel);
|
||||
this._subscriber.disconnect();
|
||||
this._subscriber = null;
|
||||
}
|
||||
|
||||
// 关闭主客户端
|
||||
if (this._client) {
|
||||
await this._client.quit();
|
||||
this._client = null;
|
||||
}
|
||||
|
||||
this._handlers.clear();
|
||||
this._lockTokens.clear();
|
||||
this._connected = false;
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this._connected;
|
||||
}
|
||||
|
||||
private _ensureConnected(): RedisClient {
|
||||
if (!this._connected || !this._client) {
|
||||
throw new Error('RedisAdapter is not connected');
|
||||
}
|
||||
return this._client;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 服务器注册 | Server Registry
|
||||
// =========================================================================
|
||||
|
||||
async registerServer(server: ServerRegistration): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._serverKey(server.serverId);
|
||||
|
||||
// 存储服务器信息
|
||||
await client.hmset(
|
||||
key,
|
||||
'serverId', server.serverId,
|
||||
'address', server.address,
|
||||
'port', String(server.port),
|
||||
'roomCount', String(server.roomCount),
|
||||
'playerCount', String(server.playerCount),
|
||||
'capacity', String(server.capacity),
|
||||
'status', server.status,
|
||||
'lastHeartbeat', String(Date.now()),
|
||||
'metadata', JSON.stringify(server.metadata ?? {})
|
||||
);
|
||||
|
||||
// 设置 TTL
|
||||
await client.expire(key, this._config.serverTtl);
|
||||
|
||||
// 添加到服务器集合
|
||||
await client.sadd(this._serversSetKey(), server.serverId);
|
||||
|
||||
// 发布事件
|
||||
await this.publish({
|
||||
type: 'server:online',
|
||||
serverId: server.serverId,
|
||||
payload: server,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async unregisterServer(serverId: string): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._serverKey(serverId);
|
||||
|
||||
// 删除服务器信息
|
||||
await client.del(key);
|
||||
|
||||
// 从服务器集合移除
|
||||
await client.srem(this._serversSetKey(), serverId);
|
||||
|
||||
// 删除该服务器的所有房间
|
||||
const roomIds = await client.smembers(this._serverRoomsKey(serverId));
|
||||
for (const roomId of roomIds) {
|
||||
await this.unregisterRoom(roomId);
|
||||
}
|
||||
await client.del(this._serverRoomsKey(serverId));
|
||||
|
||||
// 发布事件
|
||||
await this.publish({
|
||||
type: 'server:offline',
|
||||
serverId,
|
||||
payload: { serverId },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async heartbeat(serverId: string): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._serverKey(serverId);
|
||||
|
||||
// 更新心跳时间并刷新 TTL
|
||||
await client.hset(key, 'lastHeartbeat', String(Date.now()));
|
||||
await client.expire(key, this._config.serverTtl);
|
||||
}
|
||||
|
||||
async getServers(): Promise<ServerRegistration[]> {
|
||||
const client = this._ensureConnected();
|
||||
const serverIds = await client.smembers(this._serversSetKey());
|
||||
|
||||
const servers: ServerRegistration[] = [];
|
||||
for (const serverId of serverIds) {
|
||||
const server = await this.getServer(serverId);
|
||||
if (server && server.status === 'online') {
|
||||
servers.push(server);
|
||||
}
|
||||
}
|
||||
|
||||
return servers;
|
||||
}
|
||||
|
||||
async getServer(serverId: string): Promise<ServerRegistration | null> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._serverKey(serverId);
|
||||
const data = await client.hgetall(key);
|
||||
|
||||
if (!data || !data.serverId) return null;
|
||||
|
||||
return {
|
||||
serverId: data.serverId,
|
||||
address: data.address,
|
||||
port: parseInt(data.port, 10),
|
||||
roomCount: parseInt(data.roomCount, 10),
|
||||
playerCount: parseInt(data.playerCount, 10),
|
||||
capacity: parseInt(data.capacity, 10),
|
||||
status: data.status as ServerRegistration['status'],
|
||||
lastHeartbeat: parseInt(data.lastHeartbeat, 10),
|
||||
metadata: data.metadata ? JSON.parse(data.metadata) : {}
|
||||
};
|
||||
}
|
||||
|
||||
async updateServer(serverId: string, updates: Partial<ServerRegistration>): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._serverKey(serverId);
|
||||
|
||||
const args: (string | number)[] = [];
|
||||
if (updates.address !== undefined) args.push('address', updates.address);
|
||||
if (updates.port !== undefined) args.push('port', String(updates.port));
|
||||
if (updates.roomCount !== undefined) args.push('roomCount', String(updates.roomCount));
|
||||
if (updates.playerCount !== undefined) args.push('playerCount', String(updates.playerCount));
|
||||
if (updates.capacity !== undefined) args.push('capacity', String(updates.capacity));
|
||||
if (updates.status !== undefined) args.push('status', updates.status);
|
||||
if (updates.metadata !== undefined) args.push('metadata', JSON.stringify(updates.metadata));
|
||||
|
||||
if (args.length > 0) {
|
||||
await client.hmset(key, ...args);
|
||||
}
|
||||
|
||||
// 如果是 draining 状态,发布事件
|
||||
if (updates.status === 'draining') {
|
||||
await this.publish({
|
||||
type: 'server:draining',
|
||||
serverId,
|
||||
payload: { serverId },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 房间注册 | Room Registry
|
||||
// =========================================================================
|
||||
|
||||
async registerRoom(room: RoomRegistration): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._roomKey(room.roomId);
|
||||
|
||||
// 存储房间信息
|
||||
await client.hmset(
|
||||
key,
|
||||
'roomId', room.roomId,
|
||||
'roomType', room.roomType,
|
||||
'serverId', room.serverId,
|
||||
'serverAddress', room.serverAddress,
|
||||
'playerCount', String(room.playerCount),
|
||||
'maxPlayers', String(room.maxPlayers),
|
||||
'isLocked', room.isLocked ? '1' : '0',
|
||||
'metadata', JSON.stringify(room.metadata),
|
||||
'createdAt', String(room.createdAt),
|
||||
'updatedAt', String(room.updatedAt)
|
||||
);
|
||||
|
||||
// 设置 TTL(如果配置了)
|
||||
if (this._config.roomTtl > 0) {
|
||||
await client.expire(key, this._config.roomTtl);
|
||||
}
|
||||
|
||||
// 添加到房间集合
|
||||
await client.sadd(this._roomsSetKey(), room.roomId);
|
||||
|
||||
// 添加到服务器的房间列表
|
||||
await client.sadd(this._serverRoomsKey(room.serverId), room.roomId);
|
||||
|
||||
// 更新服务器房间计数
|
||||
const roomCount = (await client.smembers(this._serverRoomsKey(room.serverId))).length;
|
||||
await client.hset(this._serverKey(room.serverId), 'roomCount', String(roomCount));
|
||||
|
||||
// 发布事件
|
||||
await this.publish({
|
||||
type: 'room:created',
|
||||
serverId: room.serverId,
|
||||
roomId: room.roomId,
|
||||
payload: { roomType: room.roomType },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async unregisterRoom(roomId: string): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const room = await this.getRoom(roomId);
|
||||
if (!room) return;
|
||||
|
||||
const key = this._roomKey(roomId);
|
||||
|
||||
// 删除房间信息
|
||||
await client.del(key);
|
||||
|
||||
// 从房间集合移除
|
||||
await client.srem(this._roomsSetKey(), roomId);
|
||||
|
||||
// 从服务器的房间列表移除
|
||||
await client.srem(this._serverRoomsKey(room.serverId), roomId);
|
||||
|
||||
// 更新服务器房间计数
|
||||
const roomCount = (await client.smembers(this._serverRoomsKey(room.serverId))).length;
|
||||
await client.hset(this._serverKey(room.serverId), 'roomCount', String(roomCount));
|
||||
|
||||
// 删除快照
|
||||
await this.deleteSnapshot(roomId);
|
||||
|
||||
// 发布事件
|
||||
await this.publish({
|
||||
type: 'room:disposed',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
async updateRoom(roomId: string, updates: Partial<RoomRegistration>): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const room = await this.getRoom(roomId);
|
||||
if (!room) return;
|
||||
|
||||
const key = this._roomKey(roomId);
|
||||
const args: (string | number)[] = [];
|
||||
|
||||
if (updates.playerCount !== undefined) args.push('playerCount', String(updates.playerCount));
|
||||
if (updates.maxPlayers !== undefined) args.push('maxPlayers', String(updates.maxPlayers));
|
||||
if (updates.isLocked !== undefined) args.push('isLocked', updates.isLocked ? '1' : '0');
|
||||
if (updates.metadata !== undefined) args.push('metadata', JSON.stringify(updates.metadata));
|
||||
args.push('updatedAt', String(Date.now()));
|
||||
|
||||
if (args.length > 0) {
|
||||
await client.hmset(key, ...args);
|
||||
}
|
||||
|
||||
// 发布更新事件
|
||||
await this.publish({
|
||||
type: 'room:updated',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: updates,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
// 如果锁定状态变化,发布专门事件
|
||||
if (updates.isLocked !== undefined) {
|
||||
await this.publish({
|
||||
type: updates.isLocked ? 'room:locked' : 'room:unlocked',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: {},
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getRoom(roomId: string): Promise<RoomRegistration | null> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._roomKey(roomId);
|
||||
const data = await client.hgetall(key);
|
||||
|
||||
if (!data || !data.roomId) return null;
|
||||
|
||||
return {
|
||||
roomId: data.roomId,
|
||||
roomType: data.roomType,
|
||||
serverId: data.serverId,
|
||||
serverAddress: data.serverAddress,
|
||||
playerCount: parseInt(data.playerCount, 10),
|
||||
maxPlayers: parseInt(data.maxPlayers, 10),
|
||||
isLocked: data.isLocked === '1',
|
||||
metadata: data.metadata ? JSON.parse(data.metadata) : {},
|
||||
createdAt: parseInt(data.createdAt, 10),
|
||||
updatedAt: parseInt(data.updatedAt, 10)
|
||||
};
|
||||
}
|
||||
|
||||
async queryRooms(query: RoomQuery): Promise<RoomRegistration[]> {
|
||||
const client = this._ensureConnected();
|
||||
const roomIds = await client.smembers(this._roomsSetKey());
|
||||
|
||||
let results: RoomRegistration[] = [];
|
||||
|
||||
// 获取所有房间
|
||||
for (const roomId of roomIds) {
|
||||
const room = await this.getRoom(roomId);
|
||||
if (room) results.push(room);
|
||||
}
|
||||
|
||||
// 过滤
|
||||
if (query.roomType) {
|
||||
results = results.filter(r => r.roomType === query.roomType);
|
||||
}
|
||||
if (query.hasSpace) {
|
||||
results = results.filter(r => r.playerCount < r.maxPlayers);
|
||||
}
|
||||
if (query.notLocked) {
|
||||
results = results.filter(r => !r.isLocked);
|
||||
}
|
||||
if (query.metadata) {
|
||||
results = results.filter(r => {
|
||||
for (const [key, value] of Object.entries(query.metadata!)) {
|
||||
if (r.metadata[key] !== value) return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// 分页
|
||||
if (query.offset) {
|
||||
results = results.slice(query.offset);
|
||||
}
|
||||
if (query.limit) {
|
||||
results = results.slice(0, query.limit);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
async findAvailableRoom(roomType: string): Promise<RoomRegistration | null> {
|
||||
const rooms = await this.queryRooms({
|
||||
roomType,
|
||||
hasSpace: true,
|
||||
notLocked: true,
|
||||
limit: 1
|
||||
});
|
||||
return rooms[0] ?? null;
|
||||
}
|
||||
|
||||
async getRoomsByServer(serverId: string): Promise<RoomRegistration[]> {
|
||||
const client = this._ensureConnected();
|
||||
const roomIds = await client.smembers(this._serverRoomsKey(serverId));
|
||||
|
||||
const rooms: RoomRegistration[] = [];
|
||||
for (const roomId of roomIds) {
|
||||
const room = await this.getRoom(roomId);
|
||||
if (room) rooms.push(room);
|
||||
}
|
||||
|
||||
return rooms;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 房间状态 | Room State
|
||||
// =========================================================================
|
||||
|
||||
async saveSnapshot(snapshot: RoomSnapshot): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._snapshotKey(snapshot.roomId);
|
||||
|
||||
await client.set(key, JSON.stringify(snapshot));
|
||||
await client.expire(key, this._config.snapshotTtl);
|
||||
}
|
||||
|
||||
async loadSnapshot(roomId: string): Promise<RoomSnapshot | null> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._snapshotKey(roomId);
|
||||
const data = await client.get(key);
|
||||
|
||||
return data ? JSON.parse(data) : null;
|
||||
}
|
||||
|
||||
async deleteSnapshot(roomId: string): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const key = this._snapshotKey(roomId);
|
||||
await client.del(key);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 发布/订阅 | Pub/Sub
|
||||
// =========================================================================
|
||||
|
||||
async publish(event: DistributedEvent): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
await client.publish(this._config.channel, JSON.stringify(event));
|
||||
}
|
||||
|
||||
async subscribe(
|
||||
pattern: DistributedEventType | '*',
|
||||
handler: DistributedEventHandler
|
||||
): Promise<Unsubscribe> {
|
||||
if (!this._handlers.has(pattern)) {
|
||||
this._handlers.set(pattern, new Set());
|
||||
}
|
||||
this._handlers.get(pattern)!.add(handler);
|
||||
|
||||
return () => {
|
||||
const handlers = this._handlers.get(pattern);
|
||||
if (handlers) {
|
||||
handlers.delete(handler);
|
||||
if (handlers.size === 0) {
|
||||
this._handlers.delete(pattern);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async sendToRoom(
|
||||
roomId: string,
|
||||
messageType: string,
|
||||
data: unknown,
|
||||
playerId?: string
|
||||
): Promise<void> {
|
||||
const room = await this.getRoom(roomId);
|
||||
if (!room) return;
|
||||
|
||||
await this.publish({
|
||||
type: 'room:message',
|
||||
serverId: room.serverId,
|
||||
roomId,
|
||||
payload: { messageType, data, playerId },
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
private _dispatchEvent(event: DistributedEvent): void {
|
||||
// 通知通配符订阅者
|
||||
const wildcardHandlers = this._handlers.get('*');
|
||||
if (wildcardHandlers) {
|
||||
for (const handler of wildcardHandlers) {
|
||||
try {
|
||||
handler(event);
|
||||
} catch (error) {
|
||||
console.error('[RedisAdapter] Event handler error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 通知类型匹配的订阅者
|
||||
const typeHandlers = this._handlers.get(event.type);
|
||||
if (typeHandlers) {
|
||||
for (const handler of typeHandlers) {
|
||||
try {
|
||||
handler(event);
|
||||
} catch (error) {
|
||||
console.error('[RedisAdapter] Event handler error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 分布式锁 | Distributed Lock
|
||||
// =========================================================================
|
||||
|
||||
async acquireLock(key: string, ttlMs: number): Promise<boolean> {
|
||||
const client = this._ensureConnected();
|
||||
const lockKey = this._lockKey(key);
|
||||
const token = `${Date.now()}_${Math.random().toString(36).substring(2)}`;
|
||||
const ttlSeconds = Math.ceil(ttlMs / 1000);
|
||||
|
||||
const result = await client.set(lockKey, token, 'NX', 'EX', ttlSeconds);
|
||||
|
||||
if (result === 'OK') {
|
||||
this._lockTokens.set(key, token);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async releaseLock(key: string): Promise<void> {
|
||||
const client = this._ensureConnected();
|
||||
const lockKey = this._lockKey(key);
|
||||
const token = this._lockTokens.get(key);
|
||||
|
||||
if (!token) return;
|
||||
|
||||
await client.eval(RELEASE_LOCK_SCRIPT, 1, lockKey, token);
|
||||
this._lockTokens.delete(key);
|
||||
}
|
||||
|
||||
async extendLock(key: string, ttlMs: number): Promise<boolean> {
|
||||
const client = this._ensureConnected();
|
||||
const lockKey = this._lockKey(key);
|
||||
const token = this._lockTokens.get(key);
|
||||
|
||||
if (!token) return false;
|
||||
|
||||
const result = await client.eval(EXTEND_LOCK_SCRIPT, 1, lockKey, token, String(ttlMs));
|
||||
return result === 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建 Redis 适配器
|
||||
* @en Create Redis adapter
|
||||
*/
|
||||
export function createRedisAdapter(config: RedisAdapterConfig): RedisAdapter {
|
||||
return new RedisAdapter(config);
|
||||
}
|
||||
14
packages/framework/server/src/distributed/adapters/index.ts
Normal file
14
packages/framework/server/src/distributed/adapters/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @zh 分布式适配器模块导出
|
||||
* @en Distributed adapters module exports
|
||||
*/
|
||||
|
||||
export type { IDistributedAdapter } from './IDistributedAdapter.js';
|
||||
export { MemoryAdapter, type MemoryAdapterConfig } from './MemoryAdapter.js';
|
||||
export {
|
||||
RedisAdapter,
|
||||
createRedisAdapter,
|
||||
type RedisAdapterConfig,
|
||||
type RedisClient,
|
||||
type RedisClientFactory
|
||||
} from './RedisAdapter.js';
|
||||
67
packages/framework/server/src/distributed/index.ts
Normal file
67
packages/framework/server/src/distributed/index.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* @zh 分布式房间支持模块
|
||||
* @en Distributed room support module
|
||||
*
|
||||
* @zh 提供多服务器房间管理、跨服务器路由和故障转移功能。
|
||||
* @en Provides multi-server room management, cross-server routing, and failover features.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import {
|
||||
* DistributedRoomManager,
|
||||
* MemoryAdapter,
|
||||
* type IDistributedAdapter
|
||||
* } from '@esengine/server/distributed';
|
||||
*
|
||||
* // 单机模式(使用内存适配器)
|
||||
* const adapter = new MemoryAdapter();
|
||||
* const manager = new DistributedRoomManager(adapter, {
|
||||
* serverId: 'server-1',
|
||||
* serverAddress: 'localhost',
|
||||
* serverPort: 3000
|
||||
* }, sendFn);
|
||||
*
|
||||
* await manager.start();
|
||||
* ```
|
||||
*/
|
||||
|
||||
// 类型导出 | Type exports
|
||||
export type {
|
||||
ServerStatus,
|
||||
ServerRegistration,
|
||||
RoomRegistration,
|
||||
RoomQuery,
|
||||
PlayerSnapshot,
|
||||
RoomSnapshot,
|
||||
DistributedEventType,
|
||||
DistributedEvent,
|
||||
DistributedEventHandler,
|
||||
Unsubscribe,
|
||||
DistributedRoomManagerConfig,
|
||||
DistributedConfig,
|
||||
RoutingResultType,
|
||||
RoutingResult,
|
||||
RoutingRequest
|
||||
} from './types.js';
|
||||
|
||||
// 适配器导出 | Adapter exports
|
||||
export type { IDistributedAdapter } from './adapters/index.js';
|
||||
export { MemoryAdapter, type MemoryAdapterConfig } from './adapters/index.js';
|
||||
export {
|
||||
RedisAdapter,
|
||||
createRedisAdapter,
|
||||
type RedisAdapterConfig,
|
||||
type RedisClient,
|
||||
type RedisClientFactory
|
||||
} from './adapters/index.js';
|
||||
|
||||
// 路由模块 | Routing module
|
||||
export {
|
||||
LoadBalancedRouter,
|
||||
createLoadBalancedRouter,
|
||||
type LoadBalanceStrategy,
|
||||
type LoadBalancedRouterConfig
|
||||
} from './routing/index.js';
|
||||
|
||||
// 分布式房间管理器 | Distributed room manager
|
||||
export { DistributedRoomManager } from './DistributedRoomManager.js';
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user