Remove buffer dependency
This commit is contained in:
@@ -20,25 +20,26 @@ export interface UserDataRequestMessage {
|
||||
* @param key The key to encrypt the data with.
|
||||
* @returns An outgoing user data request (0x0002) packet.
|
||||
*/
|
||||
export function packUserDataRequestMessage (properties: UserDataRequestMessage, key: Buffer = DEFAULT_KEY): Buffer {
|
||||
export function packUserDataRequestMessage (properties: UserDataRequestMessage, key: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const encoder = new TextEncoder()
|
||||
|
||||
// Prepare data in correct format.
|
||||
const username = Buffer.from(properties.username, 'utf-8')
|
||||
const username = encoder.encode(properties.username)
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = Buffer.from(properties.colour.array())
|
||||
const clientId = Buffer.from(properties.clientId, 'utf-8')
|
||||
const colour = new Uint8Array(properties.colour.array())
|
||||
const clientId = encoder.encode(properties.clientId)
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
|
||||
// Pack data.
|
||||
const packedData = Buffer.concat([
|
||||
usernameLength,
|
||||
username,
|
||||
colour,
|
||||
clientIdLength,
|
||||
clientId
|
||||
])
|
||||
const packedData = new SmartBuffer()
|
||||
packedData.writeBytes(usernameLength)
|
||||
packedData.writeBytes(username)
|
||||
packedData.writeBytes(colour)
|
||||
packedData.writeBytes(clientIdLength)
|
||||
packedData.writeBytes(clientId)
|
||||
|
||||
// Encrypt the data.
|
||||
const data = encrypt(packedData, MESSAGE_TYPE, key)
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key)
|
||||
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
@@ -52,7 +53,7 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
|
||||
* @param key The key to decrypt the data with.
|
||||
* @returns An unpacked user data request (0x0002) message
|
||||
*/
|
||||
export function unpackUserDataRequestMessage (data: Buffer, key: Buffer = DEFAULT_KEY): UserDataRequestMessage {
|
||||
export function unpackUserDataRequestMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): UserDataRequestMessage {
|
||||
// Decrypt the incoming data.
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
@@ -75,11 +76,13 @@ export function unpackUserDataRequestMessage (data: Buffer, key: Buffer = DEFAUL
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
|
||||
const decoder = new TextDecoder()
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: username.toString(),
|
||||
username: decoder.decode(username),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: clientId.toString(),
|
||||
clientId: decoder.decode(clientId),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user