-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor test.utils.ts and PacketUtils into exported consts
- Loading branch information
Showing
5 changed files
with
27 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file was deleted.
Oops, something went wrong.