fix(api): resolve lint errors
This commit is contained in:
1 parent
96fede9fe5
commit
fd1fa2366c
42 files changed
+307
-199
No files matched your search
@@ -13,7 +13,7 @@ import {
|
||||
} from '@nestjs/websockets';
|
||||
import mongoose from 'mongoose';
|
||||
import type { JWTPayload } from 'jose';
|
||||
import { Namespace, Socket } from 'socket.io';
|
||||
import type { DefaultEventsMap, Namespace, Socket } from 'socket.io';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { IdentityService } from '../auth/identity.service';
|
||||
import { JwtService } from '../auth/jwt.service';
|
||||
@@ -78,6 +78,20 @@ interface BidRateLimitState {
|
||||
windowStartedAtTimestampMS: number;
|
||||
}
|
||||
|
||||
interface AuctionSocketData {
|
||||
auctionBidRateLimit?: BidRateLimitState;
|
||||
identity?: IdentityDocument;
|
||||
matchId?: string;
|
||||
user?: UserDocument;
|
||||
}
|
||||
|
||||
type AuctionSocket = Socket<
|
||||
DefaultEventsMap,
|
||||
DefaultEventsMap,
|
||||
DefaultEventsMap,
|
||||
AuctionSocketData
|
||||
>;
|
||||
|
||||
const BID_RATE_LIMIT_WINDOW_MS = 1000;
|
||||
const BID_RATE_LIMIT_MAX_REQUESTS_PER_WINDOW = 8;
|
||||
const BID_RATE_LIMIT_REJECTION_CODE = 'BID_RATE_LIMITED';
|
||||
@@ -112,14 +126,14 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
this.auctionEventBusService.attachAuctionNamespace(server);
|
||||
}
|
||||
|
||||
public async handleConnection(socket: Socket) {
|
||||
public async handleConnection(socket: AuctionSocket) {
|
||||
try {
|
||||
await this.authenticateSocket(socket);
|
||||
this.logger.debug(`Auction client connected: ${socket.id}`);
|
||||
} catch (err) {
|
||||
this.logger.warn(
|
||||
`Auction client authentication failed: ${
|
||||
err instanceof Error ? err.message : err
|
||||
err instanceof Error ? err.message : String(err)
|
||||
}`,
|
||||
);
|
||||
socket.disconnect(true);
|
||||
@@ -128,7 +142,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('join')
|
||||
public async handleJoin(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: JoinAuctionPayload,
|
||||
) {
|
||||
if (!payload?.matchId || !mongoose.isValidObjectId(payload.matchId)) {
|
||||
@@ -156,7 +170,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('bid')
|
||||
public async handleBid(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: BidPayload,
|
||||
@Ack() ack?: (response: Record<string, unknown>) => void,
|
||||
) {
|
||||
@@ -220,7 +234,12 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
error,
|
||||
);
|
||||
const bidServerTiming = this.extractBidServerTiming(rejectionAckFields);
|
||||
this.emitError(socket, String(rejectionAckFields.message ?? ''));
|
||||
this.emitError(
|
||||
socket,
|
||||
typeof rejectionAckFields.message === 'string'
|
||||
? rejectionAckFields.message
|
||||
: '',
|
||||
);
|
||||
ack?.({
|
||||
...rejectionAckFields,
|
||||
bidRequestId,
|
||||
@@ -263,7 +282,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('open-bidding')
|
||||
public async handleOpenBidding(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: AuctionActionPayload,
|
||||
@Ack() ack?: (response: Record<string, unknown>) => void,
|
||||
) {
|
||||
@@ -292,7 +311,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('sold')
|
||||
public async handleSold(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: SoldPayload,
|
||||
@Ack() ack?: (response: Record<string, unknown>) => void,
|
||||
) {
|
||||
@@ -322,7 +341,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('next')
|
||||
public async handleNext(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: AuctionActionPayload,
|
||||
@Ack() ack?: (response: Record<string, unknown>) => void,
|
||||
) {
|
||||
@@ -351,7 +370,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('passed')
|
||||
public async handlePassed(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: AuctionActionPayload,
|
||||
@Ack() ack?: (response: Record<string, unknown>) => void,
|
||||
) {
|
||||
@@ -380,7 +399,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
|
||||
@SubscribeMessage('chat-message')
|
||||
public async handleChatMessage(
|
||||
@ConnectedSocket() socket: Socket,
|
||||
@ConnectedSocket() socket: AuctionSocket,
|
||||
@MessageBody() payload: ChatMessagePayload,
|
||||
) {
|
||||
const matchId = this.requireJoinedMatchId(socket);
|
||||
@@ -406,7 +425,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
}
|
||||
}
|
||||
|
||||
private requireJoinedMatchId(socket: Socket): string | null {
|
||||
private requireJoinedMatchId(socket: AuctionSocket): string | null {
|
||||
const matchId = this.getSocketMatchId(socket);
|
||||
if (!matchId) {
|
||||
this.emitError(socket, 'Join a match room before sending events.');
|
||||
@@ -415,11 +434,9 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
return matchId;
|
||||
}
|
||||
|
||||
private consumeBidRateLimit(socket: Socket): boolean {
|
||||
private consumeBidRateLimit(socket: AuctionSocket): boolean {
|
||||
const currentTimestampMS = Date.now();
|
||||
const previousState = socket.data.auctionBidRateLimit as
|
||||
| BidRateLimitState
|
||||
| undefined;
|
||||
const previousState = socket.data.auctionBidRateLimit;
|
||||
if (
|
||||
!previousState ||
|
||||
currentTimestampMS - previousState.windowStartedAtTimestampMS >=
|
||||
@@ -437,10 +454,8 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
return previousState.count <= BID_RATE_LIMIT_MAX_REQUESTS_PER_WINDOW;
|
||||
}
|
||||
|
||||
private getSocketMatchId(socket: Socket): string | undefined {
|
||||
return typeof socket.data.matchId === 'string'
|
||||
? socket.data.matchId
|
||||
: undefined;
|
||||
private getSocketMatchId(socket: AuctionSocket): string | undefined {
|
||||
return socket.data.matchId;
|
||||
}
|
||||
|
||||
private toAuctionActionRequest(payload?: AuctionActionPayload) {
|
||||
@@ -464,8 +479,9 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
};
|
||||
}
|
||||
|
||||
private readHandshakeUserId(socket: Socket): string | undefined {
|
||||
const authUserId = socket.handshake.auth['x-auth-user-id'];
|
||||
private readHandshakeUserId(socket: AuctionSocket): string | undefined {
|
||||
const handshakeAuth = socket.handshake.auth as Record<string, unknown>;
|
||||
const authUserId = handshakeAuth['x-auth-user-id'];
|
||||
if (typeof authUserId === 'string') return authUserId;
|
||||
|
||||
const header = socket.handshake.headers['x-auth-user-id'];
|
||||
@@ -473,16 +489,16 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
}
|
||||
|
||||
private resolveSocketUserId(
|
||||
socket: Socket,
|
||||
socket: AuctionSocket,
|
||||
): mongoose.Types.ObjectId | undefined {
|
||||
return (socket.data.user as UserDocument | undefined)?._id;
|
||||
return socket.data.user?._id;
|
||||
}
|
||||
|
||||
private resolveChatDisplayName(
|
||||
socket: Socket,
|
||||
socket: AuctionSocket,
|
||||
fallbackDisplayName?: string,
|
||||
): string {
|
||||
const user = socket.data.user as UserDocument | undefined;
|
||||
const user = socket.data.user;
|
||||
const primaryRiotAccount =
|
||||
user?.riotAccounts.find((entry) => entry.isPrimary) ??
|
||||
user?.riotAccounts[0];
|
||||
@@ -495,14 +511,14 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
if (user?.kakaoTalkNickname) return user.kakaoTalkNickname;
|
||||
if (user?.discordNickname) return user.discordNickname;
|
||||
|
||||
const identity = socket.data.identity as IdentityDocument | undefined;
|
||||
const identity = socket.data.identity;
|
||||
if (identity?.displayName) return identity.displayName;
|
||||
if (identity?.email) return identity.email;
|
||||
|
||||
return fallbackDisplayName ?? '관전자';
|
||||
}
|
||||
|
||||
private async authenticateSocket(socket: Socket): Promise<void> {
|
||||
private async authenticateSocket(socket: AuctionSocket): Promise<void> {
|
||||
const fallback = await this.authService.resolveDevUserById(
|
||||
this.readHandshakeUserId(socket),
|
||||
);
|
||||
@@ -532,14 +548,20 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
});
|
||||
}
|
||||
|
||||
private readHandshakeToken(socket: Socket): string | undefined {
|
||||
const authToken = socket.handshake.auth.token;
|
||||
private readHandshakeToken(socket: AuctionSocket): string | undefined {
|
||||
const handshakeAuth = socket.handshake.auth as Record<string, unknown>;
|
||||
const authToken = handshakeAuth.token;
|
||||
if (typeof authToken === 'string' && authToken.trim()) {
|
||||
return authToken.trim().replace(/^Bearer\s+/i, '');
|
||||
}
|
||||
|
||||
const header = socket.handshake.headers.authorization;
|
||||
const authorization = Array.isArray(header) ? header[0] : header;
|
||||
const header: unknown = socket.handshake.headers.authorization;
|
||||
const authorization =
|
||||
typeof header === 'string'
|
||||
? header
|
||||
: Array.isArray(header) && typeof header[0] === 'string'
|
||||
? header[0]
|
||||
: undefined;
|
||||
if (authorization?.startsWith('Bearer ')) {
|
||||
return authorization.slice('Bearer '.length).trim();
|
||||
}
|
||||
@@ -547,10 +569,11 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
}
|
||||
|
||||
private async resolveSocketUserForMatch(
|
||||
socket: Socket,
|
||||
socket: AuctionSocket,
|
||||
matchId: string,
|
||||
): Promise<void> {
|
||||
if (socket.data.user || !socket.data.identity) return;
|
||||
const identity = socket.data.identity;
|
||||
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const match = await MatchModel.findById(matchId).select({
|
||||
@@ -559,10 +582,11 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
if (!match) return;
|
||||
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
socket.data.user = await UserModel.findOne({
|
||||
const user = await UserModel.findOne({
|
||||
communityId: match.communityId,
|
||||
identityId: (socket.data.identity as IdentityDocument)._id,
|
||||
identityId: identity._id,
|
||||
}).exec();
|
||||
socket.data.user = user ?? undefined;
|
||||
}
|
||||
|
||||
private requireStringClaim(payload: JWTPayload, key: string): string {
|
||||
@@ -596,7 +620,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
this.server.to(matchId).emit('match-side-effect', delta);
|
||||
}
|
||||
|
||||
private emitChatHistory(socket: Socket, auction: AuctionDocument) {
|
||||
private emitChatHistory(socket: AuctionSocket, auction: AuctionDocument) {
|
||||
socket.emit('chat-history', {
|
||||
messages: (auction.chatMessages ?? []).map((message) =>
|
||||
this.serializeChatMessage(message),
|
||||
@@ -604,16 +628,21 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
});
|
||||
}
|
||||
|
||||
private emitKnownError(socket: Socket, error: unknown) {
|
||||
private emitKnownError(socket: AuctionSocket, error: unknown) {
|
||||
this.emitError(socket, this.resolveErrorPayload(error).message);
|
||||
}
|
||||
|
||||
private emitError(socket: Socket, message: string) {
|
||||
private emitError(socket: AuctionSocket, message: string) {
|
||||
socket.emit('error', { message });
|
||||
}
|
||||
|
||||
private serializeAuction(auction: AuctionStateResponse) {
|
||||
return JSON.parse(JSON.stringify(auction));
|
||||
private serializeAuction(
|
||||
auction: AuctionStateResponse,
|
||||
): Record<string, unknown> {
|
||||
const serialized: unknown = JSON.parse(JSON.stringify(auction));
|
||||
return typeof serialized === 'object' && serialized !== null
|
||||
? (serialized as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -721,7 +750,8 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
bidDelta: Record<string, unknown> | null,
|
||||
): Record<string, unknown> | null {
|
||||
if (!bidDelta || !('bidServerTiming' in bidDelta)) return bidDelta;
|
||||
const { bidServerTiming: _bidServerTiming, ...publicBidDelta } = bidDelta;
|
||||
const publicBidDelta = { ...bidDelta };
|
||||
delete publicBidDelta.bidServerTiming;
|
||||
return publicBidDelta;
|
||||
}
|
||||
|
||||
@@ -729,10 +759,7 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
auction: AuctionStateResponse,
|
||||
bidRequestId?: string,
|
||||
): Record<string, unknown> | null {
|
||||
const serialized = this.serializeAuction(auction) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
const serialized = this.serializeAuction(auction);
|
||||
const revision = Number(serialized.revision);
|
||||
const currentHighestBidPointAmount = Number(
|
||||
serialized.currentHighestBidPointAmount ?? serialized.currentBid,
|
||||
@@ -747,11 +774,13 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
? Date.parse(serialized.currentDeadline)
|
||||
: 0),
|
||||
);
|
||||
const redisBidHistory = Array.isArray(serialized.auctionBidHistory)
|
||||
? serialized.auctionBidHistory
|
||||
const redisBidHistory: unknown[] = Array.isArray(
|
||||
serialized.auctionBidHistory,
|
||||
)
|
||||
? (serialized.auctionBidHistory as unknown[])
|
||||
: [];
|
||||
const legacyBidHistory = Array.isArray(serialized.bids)
|
||||
? serialized.bids
|
||||
const legacyBidHistory: unknown[] = Array.isArray(serialized.bids)
|
||||
? (serialized.bids as unknown[])
|
||||
: [];
|
||||
const auctionBidHistoryEntry =
|
||||
redisBidHistory[redisBidHistory.length - 1] ??
|
||||
@@ -810,7 +839,12 @@ export class AuctionGateway implements OnGatewayConnection, OnGatewayInit {
|
||||
};
|
||||
}
|
||||
|
||||
private serializeChatMessage(message: AuctionChatMessage) {
|
||||
return JSON.parse(JSON.stringify(message));
|
||||
private serializeChatMessage(
|
||||
message: AuctionChatMessage,
|
||||
): Record<string, unknown> {
|
||||
const serialized: unknown = JSON.parse(JSON.stringify(message));
|
||||
return typeof serialized === 'object' && serialized !== null
|
||||
? (serialized as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user