Remove buffer dependency
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user