Skip to content

Commit

Permalink
chore: Purpose enum now is const
Browse files Browse the repository at this point in the history
  • Loading branch information
XeroAlpha committed Oct 24, 2023
1 parent fa5ccbb commit 94b9d1c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = {
},
rules: {
'prettier/prettier': 'error',
'@typescript-eslint/no-unsafe-declaration-merging': 'off'
'@typescript-eslint/no-unsafe-declaration-merging': 'off',
'@typescript-eslint/no-unsafe-enum-comparison': 'off'
},
overrides: [
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Session extends EventEmitter {
this.emit('message', frame);
const responser = this.responserMap.get(frame.requestId);
if (responser) {
let ret: boolean | undefined = true;
let ret: boolean | undefined;
try {
ret = responser.call(this, frame);
} catch (err) {
Expand All @@ -81,7 +81,7 @@ export class Session extends EventEmitter {
}
const handler = this.handlerMap.get(frame.purpose);
if (handler) {
let ret: boolean | undefined = false;
let ret: boolean | undefined;
try {
ret = handler.call(this, frame);
} catch (err) {
Expand Down
11 changes: 6 additions & 5 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
CommandRequestLegacyBody,
EncryptionMode,
EventSubscriptionBody,
RequestPurpose
RequestPurpose,
ResponsePurpose
} from './protocol.js';

export type CommandFrameBase = Frame<RequestPurpose.Command, CommandRequestBody>;
Expand Down Expand Up @@ -112,7 +113,7 @@ export class WSClient extends Session {

sendError(statusCode?: number, statusMessage?: string, requestId?: string) {
this.sendFrame(
'error',
ResponsePurpose.Error,
{
statusCode,
statusMessage
Expand All @@ -123,9 +124,9 @@ export class WSClient extends Session {

sendEvent(eventName: string, body: Record<string, unknown>) {
if (this.version >= Version.V1_1_0) {
this.sendFrame('event', body, undefined, { eventName });
this.sendFrame(ResponsePurpose.Event, body, undefined, { eventName });
} else {
this.sendFrame('event', {
this.sendFrame(ResponsePurpose.Event, {
...body,
eventName
});
Expand All @@ -140,7 +141,7 @@ export class WSClient extends Session {
}

respondCommand(requestId: string, body: unknown) {
this.sendFrame('commandResponse', body, requestId);
this.sendFrame(ResponsePurpose.Command, body, requestId);
}

disconnect() {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Header<Purpose extends string = string> {
[key: string]: unknown;
}

export enum RequestPurpose {
export const enum RequestPurpose {
Command = 'commandRequest',
Subscribe = 'subscribe',
Unsubscribe = 'unsubscribe',
Expand All @@ -26,7 +26,9 @@ export enum RequestPurpose {
EncryptConnection = 'ws:encrypt'
}

export enum ResponsePurpose {
export type DataRequestPurpose<T extends string> = `data:${T}`;

export const enum ResponsePurpose {
Command = 'commandResponse',
Error = 'error',
Event = 'event',
Expand Down
69 changes: 38 additions & 31 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
MinecraftDataType,
MinecraftItemData,
MinecraftMobData,
RequestPurpose,
ResponsePurpose
} from './protocol.js';

Expand Down Expand Up @@ -111,23 +112,25 @@ export class ServerSession extends Session {
this.chatResponsers = new Set();
this.exchangingKey = false;
const eventHandler = (frame: EventFrameBase) => {
const eventName = frame.header.eventName ?? frame.body.eventName ?? '';
const listeners = this.eventListeners.get(eventName);
const eventFrame = {
...frame,
eventName
} as EventFrame;
if (listeners) {
const listenersCopy = new Set(listeners);
listenersCopy.forEach((e) => {
try {
e.call(this, eventFrame);
} catch (err) {
this.emit('error', err as Error);
}
});
} else {
this.emit('event', eventFrame);
const eventName = frame.header.eventName ?? frame.body.eventName;
if (eventName !== undefined) {
const listeners = this.eventListeners.get(eventName);
const eventFrame = {
...frame,
eventName
} as EventFrame;
if (listeners) {
const listenersCopy = new Set(listeners);
listenersCopy.forEach((e) => {
try {
e.call(this, eventFrame);
} catch (err) {
this.emit('error', err as Error);
}
});
} else {
this.emit('event', eventFrame);
}
}
return false;
};
Expand Down Expand Up @@ -158,7 +161,7 @@ export class ServerSession extends Session {
const requestId = randomUUID();
this.sendEncryptionRequest(requestId, mode, keyExchangeParams.publicKey, keyExchangeParams.salt);
this.setResponser(requestId, (frame) => {
if (frame.purpose === 'ws:encrypt') {
if (frame.purpose === ResponsePurpose.EncryptConnection) {
const frameBase = frame as EncryptResponseFrameBase;
this.exchangingKey = false;
encryption.completeKeyExchange(mode, frameBase.body.publicKey);
Expand Down Expand Up @@ -188,7 +191,7 @@ export class ServerSession extends Session {
}

subscribeRaw(eventName: string) {
this.sendFrame('subscribe', { eventName } as EventSubscriptionBody);
this.sendFrame(RequestPurpose.Subscribe, { eventName } as EventSubscriptionBody);
}

subscribe<B extends EventBody = EventBody>(eventName: string, callback: (frame: EventFrame<B>) => void) {
Expand All @@ -202,7 +205,7 @@ export class ServerSession extends Session {
}

unsubscribeRaw(eventName: string) {
this.sendFrame('unsubscribe', { eventName } as EventSubscriptionBody);
this.sendFrame(RequestPurpose.Unsubscribe, { eventName } as EventSubscriptionBody);
}

unsubscribe<B extends EventBody = EventBody>(eventName: string, callback: (frame: EventFrame<B>) => void) {
Expand All @@ -219,7 +222,7 @@ export class ServerSession extends Session {

sendCommandRaw(requestId: string, command: string | string[], version?: CommandVersion) {
this.sendFrame(
'commandRequest',
RequestPurpose.Command,
{
version: version ?? MinecraftCommandVersion.Initial,
commandLine: Array.isArray(command) ? command.join(' ') : command,
Expand All @@ -238,7 +241,7 @@ export class ServerSession extends Session {
const requestId = randomUUID();
if (callback) {
this.setResponser(requestId, (frame) => {
if (frame.purpose === 'commandResponse') {
if (frame.purpose === ResponsePurpose.Command) {
callback.call(this, frame as CommandResponseFrame<B>);
return true;
}
Expand All @@ -250,7 +253,7 @@ export class ServerSession extends Session {

sendCommandLegacyRaw(requestId: string, commandName: string, overload: string, input: Record<string, unknown>) {
this.sendFrame(
'commandRequest',
RequestPurpose.Command,
{
version: MinecraftCommandVersion.Initial,
name: commandName,
Expand All @@ -271,7 +274,7 @@ export class ServerSession extends Session {
const requestId = randomUUID();
if (callback) {
this.setResponser(requestId, (frame) => {
if (frame.purpose === 'commandResponse') {
if (frame.purpose === ResponsePurpose.Command) {
callback.call(this, frame as CommandResponseFrame<B>);
return true;
}
Expand All @@ -283,7 +286,7 @@ export class ServerSession extends Session {

sendAgentCommandRaw(requestId: string, agentCommand: string | string[], version?: CommandVersion) {
this.sendFrame(
'action:agent',
RequestPurpose.AgentAction,
{
version: version ?? MinecraftCommandVersion.Initial,
commandLine: Array.isArray(agentCommand) ? agentCommand.join(' ') : agentCommand
Expand All @@ -299,7 +302,7 @@ export class ServerSession extends Session {
const requestId = randomUUID();
if (callback) {
this.setResponser(requestId, (frame) => {
if (frame.purpose === 'action:agent') {
if (frame.purpose === ResponsePurpose.AgentAction) {
const { action, actionName } = frame.header;
const agentActionFrame = {
...frame,
Expand All @@ -320,7 +323,11 @@ export class ServerSession extends Session {
}

subscribeChatRaw(requestId: string, sender?: string | null, receiver?: string | null, message?: string | null) {
this.sendFrame('chat:subscribe', { sender, receiver, message } as ChatSubscribeBody, requestId);
this.sendFrame(
RequestPurpose.ChatMessageSubscribe,
{ sender, receiver, message } as ChatSubscribeBody,
requestId
);
}

subscribeChat(
Expand All @@ -332,7 +339,7 @@ export class ServerSession extends Session {
const requestId = randomUUID();
if (callback) {
this.setResponser(requestId, (frame): undefined => {
if (frame.purpose === 'chat') {
if (frame.purpose === ResponsePurpose.ChatMessage) {
const frameBase = frame as ChatEventFrameBase;
const eventName = frameBase.header.eventName ?? frameBase.body.eventName ?? '';
const { sender, receiver, message: chatMessage, type: chatType } = frameBase.body;
Expand All @@ -354,7 +361,7 @@ export class ServerSession extends Session {
}

unsubscribeChatRaw(requestId?: string) {
this.sendFrame('chat:unsubscribe', { requestId } as ChatUnsubscribeBody);
this.sendFrame(RequestPurpose.ChatMessageUnsubscribe, { requestId } as ChatUnsubscribeBody);
}

unsubscribeChat(requestId: string) {
Expand Down Expand Up @@ -397,7 +404,7 @@ export class ServerSession extends Session {
const requestId = randomUUID();
if (callback) {
this.setResponser(requestId, (frame) => {
if (frame.purpose === 'data') {
if (frame.purpose === ResponsePurpose.Data) {
const frameBase = frame as DataFrameBase;
const dataFrame = {
...frameBase,
Expand All @@ -413,7 +420,7 @@ export class ServerSession extends Session {
}

sendEncryptionRequest(requestId: string, mode: EncryptionMode | number, publicKey: string, salt: string) {
this.sendFrame('ws:encrypt', { mode, publicKey, salt } as EncryptRequestBody, requestId);
this.sendFrame(RequestPurpose.EncryptConnection, { mode, publicKey, salt } as EncryptRequestBody, requestId);
}

disconnect(force?: boolean) {
Expand Down

0 comments on commit 94b9d1c

Please sign in to comment.