Improve library

This commit is contained in:
2025-09-06 18:50:08 +01:00
parent 02c9cfdabc
commit dd8e6ee49f
7 changed files with 31 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
export { numberToUint16BE, numberToUint32BE } from "./utilities/number"; export { numberToUint16BE, numberToUint32BE } from "./utilities/number";
export { unpackIncomingPacket } from "./messages/packet"; export { unpackIncomingPacket } from "./messages/packet";
export { packers, unpackers } from "./mapping"; export { packers, unpackers } from "./mapping";
export { MessageTypes } from "./common"; export type { MessageTypes } from "./common";
export { IncomingPacket, OutgoingPacket } from "./messages/packet"; export type { IncomingPacket, OutgoingPacket } from "./messages/packet";
export { SubscribeMessage } from "./messages/subscribe"; export type { SubscribeMessage } from "./messages/subscribe";
export { BasicMessage } from "./messages/basic"; export type { BasicMessage } from "./messages/basic";
export { UserDataRequestMessage } from "./messages/userDataRequest"; export type { UserDataRequestMessage } from "./messages/userDataRequest";
export { UserDataResponseMessage } from "./messages/userDataResponse"; export type { UserDataResponseMessage } from "./messages/userDataResponse";

View File

@@ -1,4 +1,4 @@
import { encrypt } from "romulus-js"; import { encrypt, decrypt } from "romulus-js";
import { DEFAULT_KEY, MessageTypes } from "../common"; import { DEFAULT_KEY, MessageTypes } from "../common";
import { numberToUint16BE } from "../utilities/number"; import { numberToUint16BE } from "../utilities/number";
import { packOutgoingPacket } from "./packet"; import { packOutgoingPacket } from "./packet";
@@ -28,9 +28,17 @@ export function packBasicMessage(
/** /**
* Unpack the data section of an incoming basic message (0x0001) message. * Unpack the data section of an incoming basic message (0x0001) message.
* @param data The data section of an incoming basic message (0x0001) message. * @param data The encrypted data section of an incoming basic message (0x0001) message.
* @returns An encrypted unpacked basic message (0x0001) message. * @param key The key to decrypt the data with.
* @returns The decrypted plaintext message.
*/ */
export function unpackBasicMessage(data: Uint8Array): Uint8Array { export function unpackBasicMessage(
return data; data: Uint8Array,
key: Uint8Array = DEFAULT_KEY,
): Uint8Array {
const result = decrypt(data, MESSAGE_TYPE, key);
if (result.success) {
return result.plaintext;
}
throw new Error("Failed to decrypt basic message");
} }

View File

@@ -5,12 +5,11 @@ import { packOutgoingPacket } from "./packet";
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.GetHistory); const MESSAGE_TYPE = numberToUint16BE(MessageTypes.GetHistory);
/** /**
* Create an outgoing keepalive (0x0005) packet. * Create an outgoing get history (0xfffe) packet.
* @returns An outgoing keepalive (0x0005) packet. * @returns An outgoing get history (0xfffe) packet.
*/ */
export function packGetHistoryMessage(): Uint8Array { export function packGetHistoryMessage(): Uint8Array {
return packOutgoingPacket({ return packOutgoingPacket({
messageType: MESSAGE_TYPE, messageType: MESSAGE_TYPE,
data: new Uint8Array(0),
}); });
} }

View File

@@ -11,6 +11,5 @@ const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive);
export function packKeepaliveMessage(): Uint8Array { export function packKeepaliveMessage(): Uint8Array {
return packOutgoingPacket({ return packOutgoingPacket({
messageType: MESSAGE_TYPE, messageType: MESSAGE_TYPE,
data: new Uint8Array(0),
}); });
} }

View File

@@ -10,7 +10,7 @@ export interface IncomingPacket {
export interface OutgoingPacket { export interface OutgoingPacket {
messageType: Uint8Array; messageType: Uint8Array;
data: Uint8Array; data?: Uint8Array;
} }
/** /**
@@ -19,18 +19,21 @@ export interface OutgoingPacket {
* @returns A buffer containing the ready-to-send packet. * @returns A buffer containing the ready-to-send packet.
*/ */
export function packOutgoingPacket(outgoingPacket: OutgoingPacket): Uint8Array { export function packOutgoingPacket(outgoingPacket: OutgoingPacket): Uint8Array {
// Default to empty data if not provided
const data = outgoingPacket.data ?? new Uint8Array(0);
// Verify that the data does not exceed the maximum data length. // Verify that the data does not exceed the maximum data length.
if (outgoingPacket.data.length > MAX_DATA_LENGTH) { if (data.length > MAX_DATA_LENGTH) {
throw RangeError( throw RangeError(
`Specified data of length ${outgoingPacket.data.length} exceeds max data length ${MAX_DATA_LENGTH}.`, `Specified data of length ${data.length} exceeds max data length ${MAX_DATA_LENGTH}.`,
); );
} }
// Prepare the outgoing packet. // Prepare the outgoing packet.
const buffer = new SmartBuffer(); const buffer = new SmartBuffer();
buffer.writeBytes(outgoingPacket.messageType); buffer.writeBytes(outgoingPacket.messageType);
buffer.writeBytes(numberToUint16BE(outgoingPacket.data.length)); buffer.writeBytes(numberToUint16BE(data.length));
buffer.writeBytes(outgoingPacket.data); buffer.writeBytes(data);
return buffer.data; return buffer.data;
} }

View File

@@ -60,7 +60,7 @@ export function unpackUserDataRequestMessage(
data: Uint8Array, data: Uint8Array,
): UserDataRequestMessage { ): UserDataRequestMessage {
// Unpack and read data in correct format. // Unpack and read data in correct format.
const packedData = SmartBuffer.from(data); const packedData = SmartBuffer.from(Array.from(data));
const usernameLength = packedData.readUInt16(); const usernameLength = packedData.readUInt16();
const username = packedData.readBytes(usernameLength); const username = packedData.readBytes(usernameLength);

View File

@@ -59,7 +59,7 @@ export function unpackUserDataResponseMessage(
data: Uint8Array, data: Uint8Array,
): UserDataResponseMessage { ): UserDataResponseMessage {
// Unpack and read data in correct format. // Unpack and read data in correct format.
const packedData = SmartBuffer.from(data); const packedData = SmartBuffer.from(Array.from(data));
const usernameLength = packedData.readUInt16(); const usernameLength = packedData.readUInt16();
const username = packedData.readBytes(usernameLength); const username = packedData.readBytes(usernameLength);