Remove buffer dependency

This commit is contained in:
Jack Hadrill
2022-02-22 22:35:55 +00:00
parent 4d7cf1347f
commit 5c17b880cf
19 changed files with 106 additions and 96 deletions

View File

@@ -5,12 +5,12 @@ import { SmartBuffer } from '../utilities/smart-buffer'
export interface IncomingPacket {
messageType: number
senderId: number
data: Buffer
data: Uint8Array
}
export interface OutgoingPacket {
messageType: Buffer
data: Buffer
messageType: Uint8Array
data: Uint8Array
}
/**
@@ -18,20 +18,19 @@ export interface OutgoingPacket {
* @param outgoingPacket The message type and data to send.
* @returns A buffer containing the ready-to-send packet.
*/
export function packOutgoingPacket (outgoingPacket: OutgoingPacket): Buffer {
export function packOutgoingPacket (outgoingPacket: OutgoingPacket): Uint8Array {
// Verify that the data does not exceed the maximum data length.
if (outgoingPacket.data.length > MAX_DATA_LENGTH) {
throw RangeError(`Specified data of length ${outgoingPacket.data.length} exceeds max data length ${MAX_DATA_LENGTH}.`)
}
// Prepare the outgoing packet.
const buffer = Buffer.concat([
outgoingPacket.messageType,
numberToUint16BE(outgoingPacket.data.length),
outgoingPacket.data
])
const buffer = new SmartBuffer()
buffer.writeBytes(outgoingPacket.messageType)
buffer.writeBytes(numberToUint16BE(outgoingPacket.data.length))
buffer.writeBytes(outgoingPacket.data)
return buffer
return buffer.data
}
/**