Add all BENNC message types with unit tests
This commit is contained in:
23
src/bennc.ts
23
src/bennc.ts
@@ -1,23 +0,0 @@
|
||||
import { IMessageEvent } from 'websocket'
|
||||
import { IncomingPacket } from './structures/server'
|
||||
// import { SubscribeMessage } from './structures/subscribe'
|
||||
|
||||
export function connect (url: string): void {
|
||||
const webSocket = new WebSocket(url)
|
||||
webSocket.binaryType = 'arraybuffer'
|
||||
webSocket.onmessage = onMessage
|
||||
|
||||
webSocket.onopen = () => {
|
||||
console.log('Connected!')
|
||||
// const subscribeMessage = new SubscribeMessage(0x0001)
|
||||
const tmp = Uint8Array.from([0x00, 0x00, 0x00, 0x02, 0x00, 0x01])
|
||||
console.log(tmp)
|
||||
webSocket.send(tmp)
|
||||
}
|
||||
}
|
||||
|
||||
function onMessage (event: IMessageEvent): void {
|
||||
console.log('New message.')
|
||||
console.log(event.data)
|
||||
console.log(new IncomingPacket(event.data as ArrayBuffer))
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
export const MAX_DATA_LENGTH = 1000
|
||||
|
||||
export enum ByteLength {
|
||||
UInt16 = 2,
|
||||
UInt32 = 4
|
||||
export const DEFAULT_KEY = Buffer.alloc(16)
|
||||
|
||||
export enum MessageTypes {
|
||||
Subscribe = 0x0000,
|
||||
Basic = 0x0001,
|
||||
UserDataRequest = 0x0002,
|
||||
UserDataResponse = 0x0003,
|
||||
Keepalive = 0x0005,
|
||||
Unsubscribe = 0xFFFF
|
||||
}
|
||||
|
||||
10
src/index.ts
10
src/index.ts
@@ -1,3 +1,7 @@
|
||||
import { connect } from './bennc'
|
||||
|
||||
connect('wss://chat.3t.network/BENNC')
|
||||
export { unpackIncomingPacket } from './messages/packet'
|
||||
export { packers, unpackers } from './mapping'
|
||||
export { IncomingPacket, OutgoingPacket } from './messages/packet'
|
||||
export { SubscribeMessage } from './messages/subscribe'
|
||||
export { BasicMessage } from './messages/basic'
|
||||
export { UserDataRequestMessage } from './messages/userDataRequest'
|
||||
export { UserDataResponseMessage } from './messages/userDataResponse'
|
||||
|
||||
21
src/mapping.ts
Normal file
21
src/mapping.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { packSubscribeMessage } from './messages/subscribe'
|
||||
import { packBasicMessage, unpackBasicMessage } from './messages/basic'
|
||||
import { packUserDataRequestMessage, unpackUserDataRequestMessage } from './messages/userDataRequest'
|
||||
import { packUserDataResponseMessage, unpackUserDataResponseMessage } from './messages/userDataResponse'
|
||||
import { packKeepaliveMessage } from './messages/keepalive'
|
||||
import { packUnsubscribeMessage } from './messages/unsubscribe'
|
||||
|
||||
export const packers = {
|
||||
0x0000: packSubscribeMessage,
|
||||
0x0001: packBasicMessage,
|
||||
0x0002: packUserDataRequestMessage,
|
||||
0x0003: packUserDataResponseMessage,
|
||||
0x0005: packKeepaliveMessage,
|
||||
0xffff: packUnsubscribeMessage
|
||||
}
|
||||
|
||||
export const unpackers = {
|
||||
0x0001: unpackBasicMessage,
|
||||
0x0002: unpackUserDataRequestMessage,
|
||||
0x0003: unpackUserDataResponseMessage
|
||||
}
|
||||
41
src/messages/basic.ts
Normal file
41
src/messages/basic.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { decrypt, encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Basic)
|
||||
|
||||
export interface BasicMessage {
|
||||
message: string
|
||||
success?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing basic message (0x0001) packet.
|
||||
* @param properties The properties for the message.
|
||||
* @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')
|
||||
const data = encrypt(message, MESSAGE_TYPE, key)
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack the data section of an incoming basic message (0x0001) message.
|
||||
* @param data The data section of an incoming basic message (0x0001) message.
|
||||
* @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 {
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
return {
|
||||
message: message.plaintext.toString(),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
16
src/messages/keepalive.ts
Normal file
16
src/messages/keepalive.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive)
|
||||
|
||||
/**
|
||||
* Create an outgoing keepalive (0x0005) packet.
|
||||
* @returns An outgoing keepalive (0x0005) packet.
|
||||
*/
|
||||
export function packKeepaliveMessage (): Buffer {
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: Buffer.alloc(0)
|
||||
})
|
||||
}
|
||||
55
src/messages/packet.ts
Normal file
55
src/messages/packet.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { MAX_DATA_LENGTH } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { SmartBuffer } from '../utilities/smart-buffer'
|
||||
|
||||
export interface IncomingPacket {
|
||||
messageType: number
|
||||
senderId: number
|
||||
data: Buffer
|
||||
}
|
||||
|
||||
export interface OutgoingPacket {
|
||||
messageType: Buffer
|
||||
data: Buffer
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing packet ready-to-send over a WebSocket in line with the BENNC basic message strucure specification.
|
||||
* @param outgoingPacket The message type and data to send.
|
||||
* @returns A buffer containing the ready-to-send packet.
|
||||
*/
|
||||
export function packOutgoingPacket (outgoingPacket: OutgoingPacket): Buffer {
|
||||
// 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
|
||||
])
|
||||
|
||||
return buffer
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack an incoming packet coming from a WebSocket in line with the BENNC basic message strucure specification.
|
||||
* @param incomingPacket The incoming buffer from a WebSocket to unpack.
|
||||
* @returns The unpacked data.
|
||||
*/
|
||||
export function unpackIncomingPacket (incomingPacket: ArrayBuffer): IncomingPacket {
|
||||
const buffer = SmartBuffer.from(incomingPacket)
|
||||
|
||||
const messageType = buffer.readUInt16()
|
||||
const senderId = buffer.readUInt32()
|
||||
const length = buffer.readUInt16()
|
||||
const data = buffer.readBytes(length)
|
||||
|
||||
return {
|
||||
messageType: messageType,
|
||||
senderId: senderId,
|
||||
data: data
|
||||
}
|
||||
}
|
||||
22
src/messages/subscribe.ts
Normal file
22
src/messages/subscribe.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Subscribe)
|
||||
|
||||
export interface SubscribeMessage {
|
||||
messageType: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing subscribe (0x0000) packet.
|
||||
* @param properties The properties for the message.
|
||||
* @returns An outgoing subscribe (0x0000) packet.
|
||||
*/
|
||||
export function packSubscribeMessage (properties: SubscribeMessage): Buffer {
|
||||
const data = numberToUint16BE(properties.messageType)
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
}
|
||||
22
src/messages/unsubscribe.ts
Normal file
22
src/messages/unsubscribe.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Unsubscribe)
|
||||
|
||||
export interface UnsubscribeMessage {
|
||||
messageType: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing unsubscribe (0xFFFF) packet.
|
||||
* @param properties The properties for the message.
|
||||
* @returns An outgoing unsubscribe (0xFFFF) packet.
|
||||
*/
|
||||
export function packUnsubscribeMessage (properties: UnsubscribeMessage): Buffer {
|
||||
const data = numberToUint16BE(properties.messageType)
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
}
|
||||
85
src/messages/userDataRequest.ts
Normal file
85
src/messages/userDataRequest.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import Color from 'color'
|
||||
import { decrypt, encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { SmartBuffer } from '../utilities/smart-buffer'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataRequest)
|
||||
|
||||
export interface UserDataRequestMessage {
|
||||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
success?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing user data request (0x0002) packet.
|
||||
* @param properties The properties for the message.
|
||||
* @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 {
|
||||
// Prepare data in correct format.
|
||||
const username = Buffer.from(properties.username, 'utf-8')
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = Buffer.from(properties.colour.array())
|
||||
const clientId = Buffer.from(properties.clientId, 'utf-8')
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
|
||||
// Pack data.
|
||||
const packedData = Buffer.concat([
|
||||
usernameLength,
|
||||
username,
|
||||
colour,
|
||||
clientIdLength,
|
||||
clientId
|
||||
])
|
||||
|
||||
// Encrypt the data.
|
||||
const data = encrypt(packedData, MESSAGE_TYPE, key)
|
||||
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack the data section of an incoming user data request (0x0002) message
|
||||
* @param data The data section of an incoming user data request (0x0002) message
|
||||
* @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 {
|
||||
// Decrypt the incoming data.
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
// Guard to check if decryption was successful.
|
||||
if (!message.success) {
|
||||
return {
|
||||
username: '',
|
||||
colour: Color('black'),
|
||||
clientId: '',
|
||||
success: false
|
||||
}
|
||||
}
|
||||
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(message.plaintext)
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
const colour = packedData.readBytes(3)
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: username.toString(),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: clientId.toString(),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
84
src/messages/userDataResponse.ts
Normal file
84
src/messages/userDataResponse.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import Color from 'color'
|
||||
import { decrypt, encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { SmartBuffer } from '../utilities/smart-buffer'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataResponse)
|
||||
|
||||
export interface UserDataResponseMessage {
|
||||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
success?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack the data section of an outgoing user data response (0x0003) message.
|
||||
* @param properties The properties for the message.
|
||||
* @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 {
|
||||
// Prepare data in correct format.
|
||||
const username = Buffer.from(properties.username, 'utf-8')
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = Buffer.from(properties.colour.array())
|
||||
const clientId = Buffer.from(properties.clientId, 'utf-8')
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
|
||||
// Pack data.
|
||||
const packedData = Buffer.concat([
|
||||
usernameLength,
|
||||
username,
|
||||
colour,
|
||||
clientIdLength,
|
||||
clientId
|
||||
])
|
||||
|
||||
// Return encrypted data.
|
||||
const data = encrypt(packedData, MESSAGE_TYPE, key)
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack the data section of an incoming user data response (0x0003) message
|
||||
* @param data The data section of an incoming user data response (0x0003) message
|
||||
* @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 {
|
||||
// Decrypt the incoming data.
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
// Guard to check if decryption was successful.
|
||||
if (!message.success) {
|
||||
return {
|
||||
username: '',
|
||||
colour: Color('black'),
|
||||
clientId: '',
|
||||
success: false
|
||||
}
|
||||
}
|
||||
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(message.plaintext)
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
const colour = packedData.readBytes(3)
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: username.toString(),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: clientId.toString(),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { MAX_DATA_LENGTH } from '../common'
|
||||
import { SmartBuffer } from '../smart-buffer'
|
||||
|
||||
export class IncomingPacket extends SmartBuffer {
|
||||
fields: {
|
||||
messageType: number
|
||||
senderId: number
|
||||
length: number
|
||||
data: Uint8Array
|
||||
}
|
||||
|
||||
constructor (data: ArrayBuffer) {
|
||||
super()
|
||||
this.data = new Uint8Array(data)
|
||||
this.fields = {
|
||||
messageType: this.readUInt16(),
|
||||
senderId: this.readUInt32(),
|
||||
length: this.readUInt16(),
|
||||
data: this.readBytes(this.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class OutgoingPacket extends SmartBuffer {
|
||||
constructor (messageType: number, data: Uint8Array = new Uint8Array(0)) {
|
||||
super()
|
||||
if (data.length > MAX_DATA_LENGTH) {
|
||||
throw RangeError(`Specified data of length ${data.length} exceeds max data length ${MAX_DATA_LENGTH}.`)
|
||||
}
|
||||
|
||||
this.writeUInt16(messageType)
|
||||
this.writeUInt16(data.length)
|
||||
this.writeBytes(data)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import { SmartBuffer } from '../smart-buffer'
|
||||
import { OutgoingPacket } from './server'
|
||||
|
||||
export class SubscribeMessage extends SmartBuffer {
|
||||
static readonly messageType = 0x0000
|
||||
|
||||
constructor (id: number) {
|
||||
super()
|
||||
const outgoingPacket = new OutgoingPacket(SubscribeMessage.messageType)
|
||||
this.data = outgoingPacket.data
|
||||
this.writeUInt16(id)
|
||||
}
|
||||
}
|
||||
|
||||
export class Unsubscribe extends OutgoingPacket {
|
||||
static readonly messageType = 0xffff
|
||||
|
||||
constructor (id: number) {
|
||||
super(Unsubscribe.messageType)
|
||||
this.writeUInt16(id)
|
||||
}
|
||||
}
|
||||
21
src/utilities/number.ts
Normal file
21
src/utilities/number.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Pack a number to a 2 byte Uint16 buffer (big endian).
|
||||
* @param number The number to pack.
|
||||
* @returns The packed buffer.
|
||||
*/
|
||||
export function numberToUint16BE (number: number): Buffer {
|
||||
const ret = Buffer.alloc(2)
|
||||
ret.writeUInt16BE(number)
|
||||
return ret
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a number to a 4 byte Uint32 buffer (big endian).
|
||||
* @param number The number to pack.
|
||||
* @returns The packed buffer.
|
||||
*/
|
||||
export function numberToUint32BE (number: number): Buffer {
|
||||
const ret = Buffer.alloc(4)
|
||||
ret.writeUInt32BE(number)
|
||||
return ret
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ByteLength } from './common'
|
||||
import { numberToUint16BE, numberToUint32BE } from './number'
|
||||
|
||||
export class SmartBuffer {
|
||||
private _data: number[]
|
||||
@@ -8,23 +8,23 @@ export class SmartBuffer {
|
||||
* Wrap a buffer to track position and provide useful read / write functionality.
|
||||
* @param data Buffer to wrap (optional).
|
||||
*/
|
||||
constructor (data?: Uint8Array) {
|
||||
this._data = data === undefined ? [] : [...data]
|
||||
constructor (length: number = 0) {
|
||||
this._data = new Array<number>(length)
|
||||
this._cursor = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a regular buffer.
|
||||
*/
|
||||
get data (): Uint8Array {
|
||||
return Uint8Array.from(this._data)
|
||||
get data (): Buffer {
|
||||
return Buffer.from(this._data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the smart buffer to wrap new data.
|
||||
*/
|
||||
set data (data: Uint8Array) {
|
||||
this._data = [...data]
|
||||
set data (data: Buffer) {
|
||||
this._data = Array.from(data)
|
||||
this.cursor = 0
|
||||
}
|
||||
|
||||
@@ -59,12 +59,27 @@ export class SmartBuffer {
|
||||
this._cursor = position
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new SmartBuffer from an existing object.
|
||||
* @param data The object to convert to a new SmartBuffer.
|
||||
* @returns A new SmartBuffer.
|
||||
*/
|
||||
static from (data: number[] | ArrayBuffer | Buffer): SmartBuffer {
|
||||
const smartBuffer = new SmartBuffer()
|
||||
if (data instanceof ArrayBuffer) {
|
||||
smartBuffer._data = Array.from(Buffer.from(data))
|
||||
} else {
|
||||
smartBuffer._data = Array.from(data)
|
||||
}
|
||||
return smartBuffer
|
||||
}
|
||||
|
||||
/**
|
||||
* Pads bytes to the end of the smart buffer.
|
||||
* @param length The number of bytes to pad.
|
||||
*/
|
||||
pad (length: number): void {
|
||||
this._data.push(...new Uint8Array(length))
|
||||
this._data.push(...Array<number>(length))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,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): Uint8Array {
|
||||
return Uint8Array.from(this._data.slice(start, end))
|
||||
slice (start: number, end: number): Buffer {
|
||||
return Buffer.from(this._data.slice(start, end))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,7 +102,7 @@ export class SmartBuffer {
|
||||
if (this.length < start) {
|
||||
this.pad(start)
|
||||
}
|
||||
this._data.splice(this.cursor, ByteLength.UInt16, ...items)
|
||||
this._data.splice(this.cursor, deleteCount, ...items)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,9 +110,8 @@ export class SmartBuffer {
|
||||
* @returns A number represented by the bytes at the current cursor position.
|
||||
*/
|
||||
readUInt16 (): number {
|
||||
this.cursor += ByteLength.UInt16
|
||||
const bytes = this.slice(this.cursor - ByteLength.UInt16, this.cursor)
|
||||
return (bytes[0] << 8) | bytes[1]
|
||||
this.cursor += 2
|
||||
return Buffer.from(this._data).readUInt16BE(this.cursor - 2)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,16 +119,15 @@ export class SmartBuffer {
|
||||
* @returns A number represented by the bytes at the current cursor position.
|
||||
*/
|
||||
readUInt32 (): number {
|
||||
this.cursor += ByteLength.UInt32
|
||||
const bytes = this.slice(this.cursor - ByteLength.UInt32, this.cursor)
|
||||
return (((bytes[0] << 24) | (bytes[1] << 16)) | (bytes[2] << 8) | bytes[3])
|
||||
this.cursor += 4
|
||||
return Buffer.from(this._data).readUInt32BE(this.cursor - 4)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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): Uint8Array {
|
||||
readBytes (length: number): Buffer {
|
||||
this.cursor += length
|
||||
return this.slice(this.cursor - length, this.cursor)
|
||||
}
|
||||
@@ -124,12 +137,8 @@ export class SmartBuffer {
|
||||
* @param number The number to write in UInt16 format at the current cursor position.
|
||||
*/
|
||||
writeUInt16 (number: number): void {
|
||||
const bytes = [
|
||||
(number & 0xff00) >> 8,
|
||||
(number & 0x00ff)
|
||||
]
|
||||
this.splice(this.cursor, ByteLength.UInt16, ...bytes)
|
||||
this.cursor += ByteLength.UInt16
|
||||
this.splice(this.cursor, 2, ...numberToUint16BE(number))
|
||||
this.cursor += 2
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,21 +146,15 @@ export class SmartBuffer {
|
||||
* @param number The number to write in UInt32 format at the current cursor position.
|
||||
*/
|
||||
writeUInt32 (number: number): void {
|
||||
const bytes = [
|
||||
(number & 0xff000000) >> 24,
|
||||
(number & 0x00ff0000) >> 16,
|
||||
(number & 0x0000ff00) >> 8,
|
||||
(number & 0x000000ff)
|
||||
]
|
||||
this.splice(this.cursor, ByteLength.UInt32, ...bytes)
|
||||
this.cursor += ByteLength.UInt32
|
||||
this.splice(this.cursor, 4, ...numberToUint32BE(number))
|
||||
this.cursor += 4
|
||||
}
|
||||
|
||||
/**
|
||||
* 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: Uint8Array): void {
|
||||
writeBytes (buffer: number[] | Buffer): void {
|
||||
this.splice(this.cursor, buffer.length, ...buffer)
|
||||
this.cursor += buffer.length
|
||||
}
|
||||
Reference in New Issue
Block a user