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,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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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.', () => {