Remove buffer dependency
continuous-integration/drone/push Build is passing Details

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 MAX_DATA_LENGTH = 1000
export const DEFAULT_KEY = Buffer.alloc(16) export const DEFAULT_KEY = new Uint8Array(16)
export enum MessageTypes { export enum MessageTypes {
Subscribe = 0x0000, Subscribe = 0x0000,

View File

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

View File

@ -8,9 +8,9 @@ const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive)
* Create an outgoing keepalive (0x0005) packet. * Create an outgoing keepalive (0x0005) packet.
* @returns An outgoing keepalive (0x0005) packet. * @returns An outgoing keepalive (0x0005) packet.
*/ */
export function packKeepaliveMessage (): Buffer { export function packKeepaliveMessage (): Uint8Array {
return packOutgoingPacket({ return packOutgoingPacket({
messageType: MESSAGE_TYPE, 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 { export interface IncomingPacket {
messageType: number messageType: number
senderId: number senderId: number
data: Buffer data: Uint8Array
} }
export interface OutgoingPacket { export interface OutgoingPacket {
messageType: Buffer messageType: Uint8Array
data: Buffer data: Uint8Array
} }
/** /**
@ -18,20 +18,19 @@ export interface OutgoingPacket {
* @param outgoingPacket The message type and data to send. * @param outgoingPacket The message type and data to send.
* @returns A buffer containing the ready-to-send packet. * @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. // Verify that the data does not exceed the maximum data length.
if (outgoingPacket.data.length > MAX_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}.`) throw RangeError(`Specified data of length ${outgoingPacket.data.length} exceeds max data length ${MAX_DATA_LENGTH}.`)
} }
// Prepare the outgoing packet. // Prepare the outgoing packet.
const buffer = Buffer.concat([ const buffer = new SmartBuffer()
outgoingPacket.messageType, buffer.writeBytes(outgoingPacket.messageType)
numberToUint16BE(outgoingPacket.data.length), buffer.writeBytes(numberToUint16BE(outgoingPacket.data.length))
outgoingPacket.data 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. * @param properties The properties for the message.
* @returns An outgoing subscribe (0x0000) packet. * @returns An outgoing subscribe (0x0000) packet.
*/ */
export function packSubscribeMessage (properties: SubscribeMessage): Buffer { export function packSubscribeMessage (properties: SubscribeMessage): Uint8Array {
const data = numberToUint16BE(properties.messageType) const data = numberToUint16BE(properties.messageType)
return packOutgoingPacket({ return packOutgoingPacket({
messageType: MESSAGE_TYPE, messageType: MESSAGE_TYPE,

View File

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

View File

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

View File

@ -20,25 +20,26 @@ export interface UserDataResponseMessage {
* @param key The key to encrypt the data with. * @param key The key to encrypt the data with.
* @returns The data section of an outgoing user data response (0x0003) message. * @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. // 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 usernameLength = numberToUint16BE(username.length)
const colour = Buffer.from(properties.colour.array()) const colour = new Uint8Array(properties.colour.array())
const clientId = Buffer.from(properties.clientId, 'utf-8') const clientId = encoder.encode(properties.clientId)
const clientIdLength = numberToUint16BE(clientId.length) const clientIdLength = numberToUint16BE(clientId.length)
// Pack data. // Pack data.
const packedData = Buffer.concat([ const packedData = new SmartBuffer()
usernameLength, packedData.writeBytes(usernameLength)
username, packedData.writeBytes(username)
colour, packedData.writeBytes(colour)
clientIdLength, packedData.writeBytes(clientIdLength)
clientId packedData.writeBytes(clientId)
])
// Return encrypted data. // Return encrypted data.
const data = encrypt(packedData, MESSAGE_TYPE, key) const data = encrypt(packedData.data, MESSAGE_TYPE, key)
return packOutgoingPacket({ return packOutgoingPacket({
messageType: MESSAGE_TYPE, messageType: MESSAGE_TYPE,
data: data data: data
@ -51,7 +52,7 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
* @param key The key to decrypt the data with. * @param key The key to decrypt the data with.
* @returns An unpacked user data response (0x0003) message * @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. // Decrypt the incoming data.
const message = decrypt(data, MESSAGE_TYPE, key) const message = decrypt(data, MESSAGE_TYPE, key)
@ -74,11 +75,13 @@ export function unpackUserDataResponseMessage (data: Buffer, key: Buffer = DEFAU
const clientIdLength = packedData.readUInt16() const clientIdLength = packedData.readUInt16()
const clientId = packedData.readBytes(clientIdLength) const clientId = packedData.readBytes(clientIdLength)
const decoder = new TextDecoder()
// Return data in correct format. // Return data in correct format.
return { return {
username: username.toString(), username: decoder.decode(username),
colour: Color.rgb(colour), colour: Color.rgb(colour),
clientId: clientId.toString(), clientId: decoder.decode(clientId),
success: message.success success: message.success
} }
} }

View File

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

View File

@ -16,14 +16,14 @@ export class SmartBuffer {
/** /**
* Return a regular buffer. * Return a regular buffer.
*/ */
get data (): Buffer { get data (): Uint8Array {
return Buffer.from(this._data) return new Uint8Array(this._data)
} }
/** /**
* Update the smart buffer to wrap new data. * Update the smart buffer to wrap new data.
*/ */
set data (data: Buffer) { set data (data: Uint8Array) {
this._data = Array.from(data) this._data = Array.from(data)
this.cursor = 0 this.cursor = 0
} }
@ -64,10 +64,10 @@ export class SmartBuffer {
* @param data The object to convert to a new SmartBuffer. * @param data The object to convert to a new SmartBuffer.
* @returns A new SmartBuffer. * @returns A new SmartBuffer.
*/ */
static from (data: number[] | ArrayBuffer | Buffer): SmartBuffer { static from (data: number[] | ArrayBuffer): SmartBuffer {
const smartBuffer = new SmartBuffer() const smartBuffer = new SmartBuffer()
if (data instanceof ArrayBuffer) { if (data instanceof ArrayBuffer) {
smartBuffer._data = Array.from(Buffer.from(data)) smartBuffer._data = Array.from(new Uint8Array(data))
} else { } else {
smartBuffer._data = Array.from(data) smartBuffer._data = Array.from(data)
} }
@ -88,8 +88,8 @@ export class SmartBuffer {
* @param end The end position. * @param end The end position.
* @returns A new buffer containing data from the specified range. * @returns A new buffer containing data from the specified range.
*/ */
slice (start: number, end: number): Buffer { slice (start: number, end: number): Uint8Array {
return Buffer.from(this._data.slice(start, end)) 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. * @returns A number represented by the bytes at the current cursor position.
*/ */
readUInt16 (): number { readUInt16 (): number {
const num = this.slice(this.cursor, this.cursor + 2)
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. * @returns A number represented by the bytes at the current cursor position.
*/ */
readUInt32 (): number { readUInt32 (): number {
const num = this.slice(this.cursor, this.cursor + 4)
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. * 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. * @returns A buffer containing the bytes read from the current cursor position.
*/ */
readBytes (length: number): Buffer { readBytes (length: number): Uint8Array {
this.cursor += length this.cursor += length
return this.slice(this.cursor - length, this.cursor) 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. * 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. * @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.splice(this.cursor, buffer.length, ...buffer)
this.cursor += buffer.length this.cursor += buffer.length
} }

View File

@ -1,7 +1,7 @@
import { MessageTypes } from '../../src/common' import { MessageTypes } from '../../src/common'
import { packers, unpackers } from '../../src/mapping' 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.', () => { test('Create a basic message (0x0001) packet.', () => {
// Given // Given
@ -16,7 +16,7 @@ test('Create a basic message (0x0001) packet.', () => {
// Then // Then
// We can't check the contents of the data as it's encrypted with a random nonce. // We can't check the contents of the data as it's encrypted with a random nonce.
// Check the message type and length. // 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. // Check the total length is as expected.
expect(packedPacket.length).toBe(49) expect(packedPacket.length).toBe(49)
@ -24,7 +24,7 @@ test('Create a basic message (0x0001) packet.', () => {
test('Parse a basic message (0x0001).', () => { test('Parse a basic message (0x0001).', () => {
// Given // Given
const ciphertext = Buffer.from([ const ciphertext = new Uint8Array([
0x84, 0xa2, 0x3a, 0xe2, 0xa8, 0xff, 0x43, 0x56, 0x96, 0x94, 0xf6, 0xe5, 0x1a, 0x30, 0x53, 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, 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 0x51, 0x27, 0x0c, 0xb6, 0x3b, 0x7f, 0x3e, 0x2a, 0x7b, 0x70, 0xbe, 0x01, 0x7b, 0x71, 0x9d

View File

@ -6,6 +6,6 @@ test('Create a keepalive (0x0005) packet.', () => {
const packedPacket = packers[MessageTypes.Keepalive]() const packedPacket = packers[MessageTypes.Keepalive]()
// Then // Then
const expectedResult = Buffer.from([0x00, 0x05, 0x00, 0x00]) const expectedResult = new Uint8Array([0x00, 0x05, 0x00, 0x00])
expect(packedPacket).toMatchObject(expectedResult) expect(packedPacket).toMatchObject(expectedResult)
}) })

View File

@ -2,8 +2,8 @@ import { packOutgoingPacket, unpackIncomingPacket } from '../../src/messages/pac
test('Pack an outgoing packet.', () => { test('Pack an outgoing packet.', () => {
// Given // Given
const messageType = Buffer.from([0x12, 0x34]) const messageType = new Uint8Array([0x12, 0x34])
const data = Buffer.from([0x12, 0x34, 0x56, 0x78]) const data = new Uint8Array([0x12, 0x34, 0x56, 0x78])
// When // When
const packedPacket = packOutgoingPacket({ const packedPacket = packOutgoingPacket({
@ -12,7 +12,7 @@ test('Pack an outgoing packet.', () => {
}) })
// Then // Then
const expectedResult = Buffer.from([ const expectedResult = new Uint8Array([
// Message type // Message type
0x12, 0x34, 0x12, 0x34,
// Data length // Data length
@ -25,7 +25,7 @@ test('Pack an outgoing packet.', () => {
test('Unpack an incoming packet.', () => { test('Unpack an incoming packet.', () => {
// Given // Given
const incomingPacket = Buffer.from([ const incomingPacket = new Uint8Array([
// Message type // Message type
0x12, 0x34, 0x12, 0x34,
// Sender ID // Sender ID
@ -42,5 +42,5 @@ test('Unpack an incoming packet.', () => {
// Then // Then
expect(unpackedResult.messageType).toBe(0x1234) expect(unpackedResult.messageType).toBe(0x1234)
expect(unpackedResult.senderId).toBe(0xaabbccdd) 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]))
}) })

View File

@ -9,6 +9,6 @@ test('Create a subscribe (0x0000) packet.', () => {
const packedPacket = packers[MessageTypes.Subscribe]({ messageType: messageType }) const packedPacket = packers[MessageTypes.Subscribe]({ messageType: messageType })
// Then // 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) expect(packedPacket).toMatchObject(expectedResult)
}) })

View File

@ -9,6 +9,6 @@ test('Create an unsubscribe (0xffff) packet.', () => {
const packedPacket = packers[MessageTypes.Unsubscribe]({ messageType: messageType }) const packedPacket = packers[MessageTypes.Unsubscribe]({ messageType: messageType })
// Then // 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) expect(packedPacket).toMatchObject(expectedResult)
}) })

View File

@ -2,7 +2,7 @@ import Color from 'color'
import { MessageTypes } from '../../src/common' import { MessageTypes } from '../../src/common'
import { packers, unpackers } from '../../src/mapping' 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.', () => { test('Create a user data request (0x0002) packet.', () => {
// Given // Given
@ -23,7 +23,7 @@ test('Create a user data request (0x0002) packet.', () => {
// Then // Then
// We can't check the contents of the data as it's encrypted with a random nonce. // We can't check the contents of the data as it's encrypted with a random nonce.
// Check the message type and length. // 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. // Check the total length is as expected.
expect(packedPacket.length).toBe(62) expect(packedPacket.length).toBe(62)
@ -31,7 +31,7 @@ test('Create a user data request (0x0002) packet.', () => {
test('Parse a user data request (0x0002).', () => { test('Parse a user data request (0x0002).', () => {
// Given // Given
const ciphertext = Buffer.from([ const ciphertext = new Uint8Array([
0x6e, 0x3f, 0xe8, 0x45, 0x65, 0x59, 0x45, 0x85, 0xb4, 0xb2, 0xbc, 0x7a, 0x03, 0xc5, 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, 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, 0xef, 0xaf, 0x4f, 0xd8, 0x63, 0xe6, 0xc1, 0x6c, 0x98, 0x45, 0x46, 0x4a, 0x3b, 0x61,

View File

@ -2,7 +2,7 @@ import Color from 'color'
import { MessageTypes } from '../../src/common' import { MessageTypes } from '../../src/common'
import { packers, unpackers } from '../../src/mapping' 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.', () => { test('Create a user data response (0x0003) packet.', () => {
// Given // Given
@ -20,12 +20,10 @@ test('Create a user data response (0x0003) packet.', () => {
KEY KEY
) )
console.log(packedPacket.slice(4).toString('hex'))
// Then // Then
// We can't check the contents of the data as it's encrypted with a random nonce. // We can't check the contents of the data as it's encrypted with a random nonce.
// Check the message type and length. // 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. // Check the total length is as expected.
expect(packedPacket.length).toBe(62) expect(packedPacket.length).toBe(62)
@ -33,7 +31,7 @@ test('Create a user data response (0x0003) packet.', () => {
test('Parse a user data response (0x0003).', () => { test('Parse a user data response (0x0003).', () => {
// Given // Given
const ciphertext = Buffer.from([ const ciphertext = new Uint8Array([
0x56, 0x71, 0x08, 0xf9, 0x2c, 0x4e, 0x41, 0x13, 0xbe, 0x44, 0xba, 0xd9, 0xe4, 0x25, 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, 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, 0x62, 0xd4, 0x33, 0x10, 0x23, 0x6d, 0x00, 0x3c, 0xae, 0x40, 0x6c, 0xe9, 0x40, 0xfc,

View File

@ -5,7 +5,7 @@ test('Test number conversion to Uint16 big endian buffer.', () => {
const result = numberToUint16BE(1234) const result = numberToUint16BE(1234)
// Then // Then
const expectedResult = Buffer.from([0x04, 0xd2]) const expectedResult = new Uint8Array([0x04, 0xd2])
expect(result).toMatchObject(expectedResult) expect(result).toMatchObject(expectedResult)
}) })
@ -14,6 +14,6 @@ test('Test number conversion to Uint32 big endian buffer.', () => {
const result = numberToUint32BE(123456) const result = numberToUint32BE(123456)
// Then // Then
const expectedResult = Buffer.from([0x00, 0x01, 0xE2, 0x40]) const expectedResult = new Uint8Array([0x00, 0x01, 0xE2, 0x40])
expect(result).toMatchObject(expectedResult) expect(result).toMatchObject(expectedResult)
}) })

View File

@ -31,7 +31,7 @@ test('Read a buffer.', () => {
// Then // Then
const result = smartBuffer.readBytes(4) 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.', () => { test('Read a UInt16 from an offset.', () => {
@ -67,7 +67,7 @@ test('Read a buffer from an offset.', () => {
smartBuffer.cursor = 2 smartBuffer.cursor = 2
// Then // 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.', () => { test('Write a UInt16.', () => {
@ -78,7 +78,7 @@ test('Write a UInt16.', () => {
smartBuffer.writeUInt16(12345) smartBuffer.writeUInt16(12345)
// Then // Then
expect(smartBuffer.data).toMatchObject(Buffer.from([0x30, 0x39])) expect(smartBuffer.data).toMatchObject(new Uint8Array([0x30, 0x39]))
}) })
test('Write a UInt32.', () => { test('Write a UInt32.', () => {
@ -89,7 +89,7 @@ test('Write a UInt32.', () => {
smartBuffer.writeUInt32(1234567890) smartBuffer.writeUInt32(1234567890)
// Then // 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.', () => { test('Write a buffer.', () => {
@ -100,7 +100,7 @@ test('Write a buffer.', () => {
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
// Then // 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.', () => { test('Write a UInt16 at an offset.', () => {
@ -112,7 +112,7 @@ test('Write a UInt16 at an offset.', () => {
smartBuffer.writeUInt16(12345) smartBuffer.writeUInt16(12345)
// Then // 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.', () => { test('Write a UInt32 at an offset.', () => {
@ -124,7 +124,7 @@ test('Write a UInt32 at an offset.', () => {
smartBuffer.writeUInt32(1234567890) smartBuffer.writeUInt32(1234567890)
// Then // 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.', () => { 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]) smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
// Then // 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.', () => { test('Cursor is correctly incremented after reading a UInt16.', () => {