From 5c17b880cfd59c0d3bf6bc3fcc1e6de6e65728c1 Mon Sep 17 00:00:00 2001 From: Jack Hadrill Date: Tue, 22 Feb 2022 22:35:55 +0000 Subject: [PATCH] Remove buffer dependency --- src/common.ts | 2 +- src/messages/basic.ts | 11 +++++---- src/messages/keepalive.ts | 4 +-- src/messages/packet.ts | 19 +++++++------- src/messages/subscribe.ts | 2 +- src/messages/unsubscribe.ts | 2 +- src/messages/userDataRequest.ts | 33 ++++++++++++++----------- src/messages/userDataResponse.ts | 33 ++++++++++++++----------- src/utilities/number.ts | 16 +++++++----- src/utilities/smart-buffer.ts | 24 +++++++++--------- tests/messages/basic.test.ts | 6 ++--- tests/messages/keepalive.test.ts | 2 +- tests/messages/packet.test.ts | 10 ++++---- tests/messages/subscribe.test.ts | 2 +- tests/messages/unsubscribe.test.ts | 2 +- tests/messages/userDataRequest.test.ts | 6 ++--- tests/messages/userDataResponse.test.ts | 8 +++--- tests/utilities/number.test.ts | 4 +-- tests/utilities/smart-buffer.test.ts | 16 ++++++------ 19 files changed, 106 insertions(+), 96 deletions(-) diff --git a/src/common.ts b/src/common.ts index 8206c19..0ae31ed 100644 --- a/src/common.ts +++ b/src/common.ts @@ -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, diff --git a/src/messages/basic.ts b/src/messages/basic.ts index cd724fd..197efc1 100644 --- a/src/messages/basic.ts +++ b/src/messages/basic.ts @@ -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 } } diff --git a/src/messages/keepalive.ts b/src/messages/keepalive.ts index c5d50dc..f30b8c1 100644 --- a/src/messages/keepalive.ts +++ b/src/messages/keepalive.ts @@ -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) }) } diff --git a/src/messages/packet.ts b/src/messages/packet.ts index 289ec69..2a16043 100644 --- a/src/messages/packet.ts +++ b/src/messages/packet.ts @@ -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 } /** diff --git a/src/messages/subscribe.ts b/src/messages/subscribe.ts index 5f24d68..37e64d2 100644 --- a/src/messages/subscribe.ts +++ b/src/messages/subscribe.ts @@ -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, diff --git a/src/messages/unsubscribe.ts b/src/messages/unsubscribe.ts index 2e6a57c..418da76 100644 --- a/src/messages/unsubscribe.ts +++ b/src/messages/unsubscribe.ts @@ -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, diff --git a/src/messages/userDataRequest.ts b/src/messages/userDataRequest.ts index daa50d1..edc5001 100644 --- a/src/messages/userDataRequest.ts +++ b/src/messages/userDataRequest.ts @@ -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 } } diff --git a/src/messages/userDataResponse.ts b/src/messages/userDataResponse.ts index f3099a3..1f9fc8c 100644 --- a/src/messages/userDataResponse.ts +++ b/src/messages/userDataResponse.ts @@ -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 } } diff --git a/src/utilities/number.ts b/src/utilities/number.ts index 7e43025..978c806 100644 --- a/src/utilities/number.ts +++ b/src/utilities/number.ts @@ -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 } diff --git a/src/utilities/smart-buffer.ts b/src/utilities/smart-buffer.ts index 7f25e60..c7dbea1 100644 --- a/src/utilities/smart-buffer.ts +++ b/src/utilities/smart-buffer.ts @@ -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 } diff --git a/tests/messages/basic.test.ts b/tests/messages/basic.test.ts index 31920f6..f7049fe 100644 --- a/tests/messages/basic.test.ts +++ b/tests/messages/basic.test.ts @@ -1,7 +1,7 @@ import { MessageTypes } from '../../src/common' import { packers, unpackers } from '../../src/mapping' -const KEY = Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) +const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) test('Create a basic message (0x0001) packet.', () => { // Given @@ -16,7 +16,7 @@ test('Create a basic message (0x0001) packet.', () => { // Then // We can't check the contents of the data as it's encrypted with a random nonce. // Check the message type and length. - expect(packedPacket.slice(0, 4)).toMatchObject(Buffer.from([0x00, 0x01, 0x00, 0x2D])) + expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x01, 0x00, 0x2D])) // Check the total length is as expected. expect(packedPacket.length).toBe(49) @@ -24,7 +24,7 @@ test('Create a basic message (0x0001) packet.', () => { test('Parse a basic message (0x0001).', () => { // Given - const ciphertext = Buffer.from([ + const ciphertext = new Uint8Array([ 0x84, 0xa2, 0x3a, 0xe2, 0xa8, 0xff, 0x43, 0x56, 0x96, 0x94, 0xf6, 0xe5, 0x1a, 0x30, 0x53, 0x39, 0xb9, 0xc4, 0x2c, 0xab, 0x21, 0x52, 0x8f, 0xc2, 0xba, 0x6c, 0x8b, 0x96, 0x82, 0x9d, 0x51, 0x27, 0x0c, 0xb6, 0x3b, 0x7f, 0x3e, 0x2a, 0x7b, 0x70, 0xbe, 0x01, 0x7b, 0x71, 0x9d diff --git a/tests/messages/keepalive.test.ts b/tests/messages/keepalive.test.ts index f411b30..6f1f0c0 100644 --- a/tests/messages/keepalive.test.ts +++ b/tests/messages/keepalive.test.ts @@ -6,6 +6,6 @@ test('Create a keepalive (0x0005) packet.', () => { const packedPacket = packers[MessageTypes.Keepalive]() // Then - const expectedResult = Buffer.from([0x00, 0x05, 0x00, 0x00]) + const expectedResult = new Uint8Array([0x00, 0x05, 0x00, 0x00]) expect(packedPacket).toMatchObject(expectedResult) }) diff --git a/tests/messages/packet.test.ts b/tests/messages/packet.test.ts index 5c73441..fab93e5 100644 --- a/tests/messages/packet.test.ts +++ b/tests/messages/packet.test.ts @@ -2,8 +2,8 @@ import { packOutgoingPacket, unpackIncomingPacket } from '../../src/messages/pac test('Pack an outgoing packet.', () => { // Given - const messageType = Buffer.from([0x12, 0x34]) - const data = Buffer.from([0x12, 0x34, 0x56, 0x78]) + const messageType = new Uint8Array([0x12, 0x34]) + const data = new Uint8Array([0x12, 0x34, 0x56, 0x78]) // When const packedPacket = packOutgoingPacket({ @@ -12,7 +12,7 @@ test('Pack an outgoing packet.', () => { }) // Then - const expectedResult = Buffer.from([ + const expectedResult = new Uint8Array([ // Message type 0x12, 0x34, // Data length @@ -25,7 +25,7 @@ test('Pack an outgoing packet.', () => { test('Unpack an incoming packet.', () => { // Given - const incomingPacket = Buffer.from([ + const incomingPacket = new Uint8Array([ // Message type 0x12, 0x34, // Sender ID @@ -42,5 +42,5 @@ test('Unpack an incoming packet.', () => { // Then expect(unpackedResult.messageType).toBe(0x1234) expect(unpackedResult.senderId).toBe(0xaabbccdd) - expect(unpackedResult.data).toMatchObject(Buffer.from([0x12, 0x34, 0x56, 0x78])) + expect(unpackedResult.data).toMatchObject(new Uint8Array([0x12, 0x34, 0x56, 0x78])) }) diff --git a/tests/messages/subscribe.test.ts b/tests/messages/subscribe.test.ts index 8e4a2c0..49ca087 100644 --- a/tests/messages/subscribe.test.ts +++ b/tests/messages/subscribe.test.ts @@ -9,6 +9,6 @@ test('Create a subscribe (0x0000) packet.', () => { const packedPacket = packers[MessageTypes.Subscribe]({ messageType: messageType }) // Then - const expectedResult = Buffer.from([0x00, 0x00, 0x00, 0x02, 0xab, 0xcd]) + const expectedResult = new Uint8Array([0x00, 0x00, 0x00, 0x02, 0xab, 0xcd]) expect(packedPacket).toMatchObject(expectedResult) }) diff --git a/tests/messages/unsubscribe.test.ts b/tests/messages/unsubscribe.test.ts index 5c28df1..46df045 100644 --- a/tests/messages/unsubscribe.test.ts +++ b/tests/messages/unsubscribe.test.ts @@ -9,6 +9,6 @@ test('Create an unsubscribe (0xffff) packet.', () => { const packedPacket = packers[MessageTypes.Unsubscribe]({ messageType: messageType }) // Then - const expectedResult = Buffer.from([0xff, 0xff, 0x00, 0x02, 0xab, 0xcd]) + const expectedResult = new Uint8Array([0xff, 0xff, 0x00, 0x02, 0xab, 0xcd]) expect(packedPacket).toMatchObject(expectedResult) }) diff --git a/tests/messages/userDataRequest.test.ts b/tests/messages/userDataRequest.test.ts index 82186cc..b43d8e3 100644 --- a/tests/messages/userDataRequest.test.ts +++ b/tests/messages/userDataRequest.test.ts @@ -2,7 +2,7 @@ import Color from 'color' import { MessageTypes } from '../../src/common' import { packers, unpackers } from '../../src/mapping' -const KEY = Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) +const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) test('Create a user data request (0x0002) packet.', () => { // Given @@ -23,7 +23,7 @@ test('Create a user data request (0x0002) packet.', () => { // Then // We can't check the contents of the data as it's encrypted with a random nonce. // Check the message type and length. - expect(packedPacket.slice(0, 4)).toMatchObject(Buffer.from([0x00, 0x02, 0x00, 0x3A])) + expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x3A])) // Check the total length is as expected. expect(packedPacket.length).toBe(62) @@ -31,7 +31,7 @@ test('Create a user data request (0x0002) packet.', () => { test('Parse a user data request (0x0002).', () => { // Given - const ciphertext = Buffer.from([ + const ciphertext = new Uint8Array([ 0x6e, 0x3f, 0xe8, 0x45, 0x65, 0x59, 0x45, 0x85, 0xb4, 0xb2, 0xbc, 0x7a, 0x03, 0xc5, 0x6d, 0xf4, 0x23, 0x3e, 0xe9, 0x3b, 0x08, 0x6d, 0x67, 0x85, 0xc1, 0xda, 0xc6, 0x3f, 0xef, 0xaf, 0x4f, 0xd8, 0x63, 0xe6, 0xc1, 0x6c, 0x98, 0x45, 0x46, 0x4a, 0x3b, 0x61, diff --git a/tests/messages/userDataResponse.test.ts b/tests/messages/userDataResponse.test.ts index bda8319..a3556be 100644 --- a/tests/messages/userDataResponse.test.ts +++ b/tests/messages/userDataResponse.test.ts @@ -2,7 +2,7 @@ import Color from 'color' import { MessageTypes } from '../../src/common' import { packers, unpackers } from '../../src/mapping' -const KEY = Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) +const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) test('Create a user data response (0x0003) packet.', () => { // Given @@ -20,12 +20,10 @@ test('Create a user data response (0x0003) packet.', () => { KEY ) - console.log(packedPacket.slice(4).toString('hex')) - // Then // We can't check the contents of the data as it's encrypted with a random nonce. // Check the message type and length. - expect(packedPacket.slice(0, 4)).toMatchObject(Buffer.from([0x00, 0x03, 0x00, 0x3A])) + expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x03, 0x00, 0x3A])) // Check the total length is as expected. expect(packedPacket.length).toBe(62) @@ -33,7 +31,7 @@ test('Create a user data response (0x0003) packet.', () => { test('Parse a user data response (0x0003).', () => { // Given - const ciphertext = Buffer.from([ + const ciphertext = new Uint8Array([ 0x56, 0x71, 0x08, 0xf9, 0x2c, 0x4e, 0x41, 0x13, 0xbe, 0x44, 0xba, 0xd9, 0xe4, 0x25, 0x14, 0x60, 0x3b, 0x96, 0x7e, 0x0b, 0xbd, 0xac, 0xf0, 0xaf, 0xac, 0xd7, 0x80, 0xe5, 0x62, 0xd4, 0x33, 0x10, 0x23, 0x6d, 0x00, 0x3c, 0xae, 0x40, 0x6c, 0xe9, 0x40, 0xfc, diff --git a/tests/utilities/number.test.ts b/tests/utilities/number.test.ts index 8eea54c..55d9c79 100644 --- a/tests/utilities/number.test.ts +++ b/tests/utilities/number.test.ts @@ -5,7 +5,7 @@ test('Test number conversion to Uint16 big endian buffer.', () => { const result = numberToUint16BE(1234) // Then - const expectedResult = Buffer.from([0x04, 0xd2]) + const expectedResult = new Uint8Array([0x04, 0xd2]) expect(result).toMatchObject(expectedResult) }) @@ -14,6 +14,6 @@ test('Test number conversion to Uint32 big endian buffer.', () => { const result = numberToUint32BE(123456) // Then - const expectedResult = Buffer.from([0x00, 0x01, 0xE2, 0x40]) + const expectedResult = new Uint8Array([0x00, 0x01, 0xE2, 0x40]) expect(result).toMatchObject(expectedResult) }) diff --git a/tests/utilities/smart-buffer.test.ts b/tests/utilities/smart-buffer.test.ts index 13f75c3..ea629ad 100644 --- a/tests/utilities/smart-buffer.test.ts +++ b/tests/utilities/smart-buffer.test.ts @@ -31,7 +31,7 @@ test('Read a buffer.', () => { // Then const result = smartBuffer.readBytes(4) - expect(result).toMatchObject(Buffer.from([0, 1, 2, 3])) + expect(result).toMatchObject(new Uint8Array([0, 1, 2, 3])) }) test('Read a UInt16 from an offset.', () => { @@ -67,7 +67,7 @@ test('Read a buffer from an offset.', () => { smartBuffer.cursor = 2 // Then - expect(smartBuffer.readBytes(4)).toMatchObject(Buffer.from([2, 3, 4, 5])) + expect(smartBuffer.readBytes(4)).toMatchObject(new Uint8Array([2, 3, 4, 5])) }) test('Write a UInt16.', () => { @@ -78,7 +78,7 @@ test('Write a UInt16.', () => { smartBuffer.writeUInt16(12345) // Then - expect(smartBuffer.data).toMatchObject(Buffer.from([0x30, 0x39])) + expect(smartBuffer.data).toMatchObject(new Uint8Array([0x30, 0x39])) }) test('Write a UInt32.', () => { @@ -89,7 +89,7 @@ test('Write a UInt32.', () => { smartBuffer.writeUInt32(1234567890) // Then - expect(smartBuffer.data).toMatchObject(Buffer.from([0x49, 0x96, 0x02, 0xD2])) + expect(smartBuffer.data).toMatchObject(new Uint8Array([0x49, 0x96, 0x02, 0xD2])) }) test('Write a buffer.', () => { @@ -100,7 +100,7 @@ test('Write a buffer.', () => { smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) // Then - expect(smartBuffer.data).toMatchObject(Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) + expect(smartBuffer.data).toMatchObject(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) }) test('Write a UInt16 at an offset.', () => { @@ -112,7 +112,7 @@ test('Write a UInt16 at an offset.', () => { smartBuffer.writeUInt16(12345) // Then - expect(smartBuffer.data).toMatchObject(Buffer.from([0x00, 0x00, 0x30, 0x39])) + expect(smartBuffer.data).toMatchObject(new Uint8Array([0x00, 0x00, 0x30, 0x39])) }) test('Write a UInt32 at an offset.', () => { @@ -124,7 +124,7 @@ test('Write a UInt32 at an offset.', () => { smartBuffer.writeUInt32(1234567890) // Then - expect(smartBuffer.data).toMatchObject(Buffer.from([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2])) + expect(smartBuffer.data).toMatchObject(new Uint8Array([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2])) }) test('Write a buffer at an offset.', () => { @@ -136,7 +136,7 @@ test('Write a buffer at an offset.', () => { smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) // Then - expect(smartBuffer.data).toMatchObject(Buffer.from([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) + expect(smartBuffer.data).toMatchObject(new Uint8Array([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) }) test('Cursor is correctly incremented after reading a UInt16.', () => {