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

@@ -1,6 +1,6 @@
export const MAX_DATA_LENGTH = 1000
export const DEFAULT_KEY = Buffer.alloc(16)
export const DEFAULT_KEY = new Uint8Array(16)
export enum MessageTypes {
Subscribe = 0x0000,

View File

@@ -16,8 +16,9 @@ export interface BasicMessage {
* @param key The key to encrypt the data with.
* @returns An outgoing basic message (0x0001) packet.
*/
export function packBasicMessage (properties: BasicMessage, key: Buffer = DEFAULT_KEY): Buffer {
const message = Buffer.from(properties.message, 'utf-8')
export function packBasicMessage (properties: BasicMessage, key: Uint8Array = DEFAULT_KEY): Uint8Array {
const encoder = new TextEncoder()
const message = encoder.encode(properties.message)
const data = encrypt(message, MESSAGE_TYPE, key)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
@@ -31,11 +32,11 @@ export function packBasicMessage (properties: BasicMessage, key: Buffer = DEFAUL
* @param key The key to decrypt the data with.
* @returns An unpacked basic message (0x0001) message.
*/
export function unpackBasicMessage (data: Buffer, key: Buffer = DEFAULT_KEY): BasicMessage {
export function unpackBasicMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): BasicMessage {
const decoder = new TextDecoder()
const message = decrypt(data, MESSAGE_TYPE, key)
return {
message: message.plaintext.toString(),
message: decoder.decode(message.plaintext),
success: message.success
}
}

View File

@@ -8,9 +8,9 @@ const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive)
* Create an outgoing keepalive (0x0005) packet.
* @returns An outgoing keepalive (0x0005) packet.
*/
export function packKeepaliveMessage (): Buffer {
export function packKeepaliveMessage (): Uint8Array {
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: Buffer.alloc(0)
data: new Uint8Array(0)
})
}

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
}
/**

View File

@@ -13,7 +13,7 @@ export interface SubscribeMessage {
* @param properties The properties for the message.
* @returns An outgoing subscribe (0x0000) packet.
*/
export function packSubscribeMessage (properties: SubscribeMessage): Buffer {
export function packSubscribeMessage (properties: SubscribeMessage): Uint8Array {
const data = numberToUint16BE(properties.messageType)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,

View File

@@ -13,7 +13,7 @@ export interface UnsubscribeMessage {
* @param properties The properties for the message.
* @returns An outgoing unsubscribe (0xFFFF) packet.
*/
export function packUnsubscribeMessage (properties: UnsubscribeMessage): Buffer {
export function packUnsubscribeMessage (properties: UnsubscribeMessage): Uint8Array {
const data = numberToUint16BE(properties.messageType)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,

View File

@@ -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
}
}

View File

@@ -20,25 +20,26 @@ export interface UserDataResponseMessage {
* @param key The key to encrypt the data with.
* @returns The data section of an outgoing user data response (0x0003) message.
*/
export function packUserDataResponseMessage (properties: UserDataResponseMessage, key: Buffer = DEFAULT_KEY): Buffer {
export function packUserDataResponseMessage (properties: UserDataResponseMessage, 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)
// Return encrypted data.
const data = encrypt(packedData, MESSAGE_TYPE, key)
const data = encrypt(packedData.data, MESSAGE_TYPE, key)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: data
@@ -51,7 +52,7 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
* @param key The key to decrypt the data with.
* @returns An unpacked user data response (0x0003) message
*/
export function unpackUserDataResponseMessage (data: Buffer, key: Buffer = DEFAULT_KEY): UserDataResponseMessage {
export function unpackUserDataResponseMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): UserDataResponseMessage {
// Decrypt the incoming data.
const message = decrypt(data, MESSAGE_TYPE, key)
@@ -74,11 +75,13 @@ export function unpackUserDataResponseMessage (data: Buffer, key: Buffer = DEFAU
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
}
}

View File

@@ -3,9 +3,10 @@
* @param number The number to pack.
* @returns The packed buffer.
*/
export function numberToUint16BE (number: number): Buffer {
const ret = Buffer.alloc(2)
ret.writeUInt16BE(number)
export function numberToUint16BE (number: number): Uint8Array {
const ret = new Uint8Array(2)
ret[0] = (number & 0xFF00) >> 8
ret[1] = (number & 0x00FF) >> 0
return ret
}
@@ -14,8 +15,11 @@ export function numberToUint16BE (number: number): Buffer {
* @param number The number to pack.
* @returns The packed buffer.
*/
export function numberToUint32BE (number: number): Buffer {
const ret = Buffer.alloc(4)
ret.writeUInt32BE(number)
export function numberToUint32BE (number: number): Uint8Array {
const ret = new Uint8Array(4)
ret[0] = (number & 0xFF000000) >> 24
ret[1] = (number & 0x00FF0000) >> 16
ret[2] = (number & 0x0000FF00) >> 8
ret[3] = (number & 0x000000FF) >> 0
return ret
}

View File

@@ -16,14 +16,14 @@ export class SmartBuffer {
/**
* Return a regular buffer.
*/
get data (): Buffer {
return Buffer.from(this._data)
get data (): Uint8Array {
return new Uint8Array(this._data)
}
/**
* Update the smart buffer to wrap new data.
*/
set data (data: Buffer) {
set data (data: Uint8Array) {
this._data = Array.from(data)
this.cursor = 0
}
@@ -64,10 +64,10 @@ export class SmartBuffer {
* @param data The object to convert to a new SmartBuffer.
* @returns A new SmartBuffer.
*/
static from (data: number[] | ArrayBuffer | Buffer): SmartBuffer {
static from (data: number[] | ArrayBuffer): SmartBuffer {
const smartBuffer = new SmartBuffer()
if (data instanceof ArrayBuffer) {
smartBuffer._data = Array.from(Buffer.from(data))
smartBuffer._data = Array.from(new Uint8Array(data))
} else {
smartBuffer._data = Array.from(data)
}
@@ -88,8 +88,8 @@ export class SmartBuffer {
* @param end The end position.
* @returns A new buffer containing data from the specified range.
*/
slice (start: number, end: number): Buffer {
return Buffer.from(this._data.slice(start, end))
slice (start: number, end: number): Uint8Array {
return new Uint8Array(this._data.slice(start, end))
}
/**
@@ -110,8 +110,9 @@ export class SmartBuffer {
* @returns A number represented by the bytes at the current cursor position.
*/
readUInt16 (): number {
const num = this.slice(this.cursor, this.cursor + 2)
this.cursor += 2
return Buffer.from(this._data).readUInt16BE(this.cursor - 2)
return (num[0] << 8 | num[1]) >>> 0
}
/**
@@ -119,15 +120,16 @@ export class SmartBuffer {
* @returns A number represented by the bytes at the current cursor position.
*/
readUInt32 (): number {
const num = this.slice(this.cursor, this.cursor + 4)
this.cursor += 4
return Buffer.from(this._data).readUInt32BE(this.cursor - 4)
return (num[0] << 24 | num[1] << 16 | num[2] << 8 | num[3]) >>> 0
}
/**
* Read the specified number of bytes from the smart buffer at the current cursor position, and increment the cursor.
* @returns A buffer containing the bytes read from the current cursor position.
*/
readBytes (length: number): Buffer {
readBytes (length: number): Uint8Array {
this.cursor += length
return this.slice(this.cursor - length, this.cursor)
}
@@ -154,7 +156,7 @@ export class SmartBuffer {
* Write the specified bytes to the smart buffer at the current cursor position, and increment the cursor.
* @param buffer The bytes to write at the current cursor position.
*/
writeBytes (buffer: number[] | Buffer): void {
writeBytes (buffer: number[] | Uint8Array): void {
this.splice(this.cursor, buffer.length, ...buffer)
this.cursor += buffer.length
}