Skip to content

Commit

Permalink
chore: refactor test.utils.ts and PacketUtils into exported consts
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Jan 30, 2025
1 parent d6f3b5f commit cf8feed
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
import { connect, Socket } from 'net';
import { promises as dns, SrvRecord } from 'dns';

import { padData } from './test.utils';
import { padData } from './packetUtils';
import { queryBytes } from './legacyProtocol';
import { validData } from './legacyProtocol.test';
import { QueryProtocols, fetchServerInfo } from './index';
Expand Down
2 changes: 1 addition & 1 deletion src/modernProtocol.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';

import { padData } from './test.utils';
import { padData } from './packetUtils';
import { ModernServerResponse } from './types';
import { buildMotd, ModernQueryProtocol } from './modernProtocol';

Expand Down
18 changes: 8 additions & 10 deletions src/modernProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { QueryProtocol } from './protocol';
import { PacketUtils } from './packetUtils';
import { createPacket, encode, encodingLength } from './packetUtils';
import { Description, ModernServerResponse, ServerInfo } from './types';

const protocolVersion = 757;
Expand All @@ -25,17 +25,17 @@ export class ModernQueryProtocol implements QueryProtocol {

portBuffer.writeUInt16BE(port);

const handshake = PacketUtils.createPacket(
const handshake = createPacket(
0,
Buffer.concat([
Buffer.from(PacketUtils.encode(protocolVersion)),
Buffer.from(PacketUtils.encode(address.length)),
Buffer.from(encode(protocolVersion)),
Buffer.from(encode(address.length)),
Buffer.from(address),
portBuffer,
Buffer.from(PacketUtils.encode(1))
Buffer.from(encode(1))
])
);
const request = PacketUtils.createPacket(0, Buffer.alloc(0));
const request = createPacket(0, Buffer.alloc(0));

return Buffer.concat([handshake, request]);
}
Expand All @@ -46,7 +46,7 @@ export class ModernQueryProtocol implements QueryProtocol {
// value supplied to server does not matter
pingBuffer.writeUint32BE(0);

return PacketUtils.createPacket(1, pingBuffer);
return createPacket(1, pingBuffer);
}

parse(response?: Buffer): ServerInfo {
Expand All @@ -57,9 +57,7 @@ export class ModernQueryProtocol implements QueryProtocol {
};
}

const payload = response.subarray(
PacketUtils.encodingLength(response.length) * 2 + 1
);
const payload = response.subarray(encodingLength(response.length) * 2 + 1);

try {
const result = JSON.parse(
Expand Down
57 changes: 17 additions & 40 deletions src/packetUtils.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,23 @@
import varint from 'varint';

// eslint-disable-next-line import-x/no-named-as-default-member
const { encode, encodingLength } = varint;
const { encode: varintEncode, encodingLength: varintEncodingLength } = varint;

/**
* Static utility methods for creating Minecraft packets
*/
export class PacketUtils {
/**
* Creates a packet with the specified Packet ID and payload.
*
* @param id An integer Packet ID as defined by the MC spec
* @param payload A Buffer containing the packet data
* @returns A Buffer containing the packet ID and payload wrapped as a Minecraft packet
*/
public static createPacket(id: number, payload: Buffer): Buffer {
return Buffer.concat([
Buffer.from(encode(encodingLength(id) + payload.length)),
Buffer.from(encode(id)),
payload
]);
}
export const createPacket = (id: number, payload: Buffer): Buffer =>
Buffer.concat([
Buffer.from(varintEncode(encodingLength(id) + payload.length)),
Buffer.from(varintEncode(id)),
payload
]);

/**
* Encodes a number into a VarInt byte array.
*
* @param num Number to encode
* @param buffer Buffer to use, if specified
* @param offset Offset in buffer, if specified
* @returns Byte array representing "num" as a VarInt
*/
public static encode(num: number, buffer?: number[], offset?: number) {
return encode(num, buffer, offset);
}
export const encode = varintEncode;

/**
* Get the byte length of a given number stored as a VarInt.
*
* @param num Number to check length of
* @returns Length in bytes
*/
public static encodingLength(num: number) {
return encodingLength(num);
}
}
export const encodingLength = varintEncodingLength;

export const padData = (data: Buffer): Buffer => {
const skipBytes = encodingLength(data.length) * 2 + 1;
const padded = Buffer.alloc(data.byteLength + skipBytes);
data.copy(padded, skipBytes);

return padded;
};
12 changes: 0 additions & 12 deletions src/test.utils.ts

This file was deleted.

0 comments on commit cf8feed

Please sign in to comment.