Prettier
This commit is contained in:
18
src/index.ts
18
src/index.ts
@@ -1,9 +1,9 @@
|
||||
export { numberToUint16BE, numberToUint32BE } from './utilities/number'
|
||||
export { unpackIncomingPacket } from './messages/packet'
|
||||
export { packers, unpackers } from './mapping'
|
||||
export { MessageTypes } from './common'
|
||||
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'
|
||||
export { numberToUint16BE, numberToUint32BE } from "./utilities/number";
|
||||
export { unpackIncomingPacket } from "./messages/packet";
|
||||
export { packers, unpackers } from "./mapping";
|
||||
export { MessageTypes } from "./common";
|
||||
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";
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
import { 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)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Basic);
|
||||
|
||||
export interface BasicMessage {
|
||||
data: Uint8Array
|
||||
data: Uint8Array;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -15,12 +15,15 @@ export interface BasicMessage {
|
||||
* @param key The key to encrypt the data with.
|
||||
* @returns An encrypted outgoing basic message (0x0001) packet.
|
||||
*/
|
||||
export function packBasicMessage (message: Uint8Array, key: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const data = encrypt(message, MESSAGE_TYPE, key)
|
||||
export function packBasicMessage(
|
||||
message: Uint8Array,
|
||||
key: Uint8Array = DEFAULT_KEY,
|
||||
): Uint8Array {
|
||||
const data = encrypt(message, MESSAGE_TYPE, key);
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,6 +31,6 @@ export function packBasicMessage (message: Uint8Array, key: Uint8Array = DEFAULT
|
||||
* @param data The data section of an incoming basic message (0x0001) message.
|
||||
* @returns An encrypted unpacked basic message (0x0001) message.
|
||||
*/
|
||||
export function unpackBasicMessage (data: Uint8Array): Uint8Array {
|
||||
return data
|
||||
export function unpackBasicMessage(data: Uint8Array): Uint8Array {
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
import { MessageTypes } from "../common";
|
||||
import { numberToUint16BE } from "../utilities/number";
|
||||
import { packOutgoingPacket } from "./packet";
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive);
|
||||
|
||||
/**
|
||||
* Create an outgoing keepalive (0x0005) packet.
|
||||
* @returns An outgoing keepalive (0x0005) packet.
|
||||
*/
|
||||
export function packKeepaliveMessage (): Uint8Array {
|
||||
export function packKeepaliveMessage(): Uint8Array {
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: new Uint8Array(0)
|
||||
})
|
||||
data: new Uint8Array(0),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { MAX_DATA_LENGTH } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { SmartBuffer } from '../utilities/smart-buffer'
|
||||
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: Uint8Array
|
||||
messageType: number;
|
||||
senderId: number;
|
||||
data: Uint8Array;
|
||||
}
|
||||
|
||||
export interface OutgoingPacket {
|
||||
messageType: Uint8Array
|
||||
data: Uint8Array
|
||||
messageType: Uint8Array;
|
||||
data: Uint8Array;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -18,19 +18,21 @@ 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): Uint8Array {
|
||||
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}.`)
|
||||
throw RangeError(
|
||||
`Specified data of length ${outgoingPacket.data.length} exceeds max data length ${MAX_DATA_LENGTH}.`,
|
||||
);
|
||||
}
|
||||
|
||||
// Prepare the outgoing packet.
|
||||
const buffer = new SmartBuffer()
|
||||
buffer.writeBytes(outgoingPacket.messageType)
|
||||
buffer.writeBytes(numberToUint16BE(outgoingPacket.data.length))
|
||||
buffer.writeBytes(outgoingPacket.data)
|
||||
const buffer = new SmartBuffer();
|
||||
buffer.writeBytes(outgoingPacket.messageType);
|
||||
buffer.writeBytes(numberToUint16BE(outgoingPacket.data.length));
|
||||
buffer.writeBytes(outgoingPacket.data);
|
||||
|
||||
return buffer.data
|
||||
return buffer.data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,17 +40,19 @@ export function packOutgoingPacket (outgoingPacket: OutgoingPacket): Uint8Array
|
||||
* @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)
|
||||
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)
|
||||
const messageType = buffer.readUInt16();
|
||||
const senderId = buffer.readUInt32();
|
||||
const length = buffer.readUInt16();
|
||||
const data = buffer.readBytes(length);
|
||||
|
||||
return {
|
||||
messageType: messageType,
|
||||
senderId: senderId,
|
||||
data: data
|
||||
}
|
||||
data: data,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
import { MessageTypes } from "../common";
|
||||
import { numberToUint16BE } from "../utilities/number";
|
||||
import { packOutgoingPacket } from "./packet";
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Subscribe)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Subscribe);
|
||||
|
||||
export interface SubscribeMessage {
|
||||
messageType: number
|
||||
messageType: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,10 +13,10 @@ export interface SubscribeMessage {
|
||||
* @param properties The properties for the message.
|
||||
* @returns An outgoing subscribe (0x0000) packet.
|
||||
*/
|
||||
export function packSubscribeMessage (properties: SubscribeMessage): Uint8Array {
|
||||
const data = numberToUint16BE(properties.messageType)
|
||||
export function packSubscribeMessage(properties: SubscribeMessage): Uint8Array {
|
||||
const data = numberToUint16BE(properties.messageType);
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
import { MessageTypes } from "../common";
|
||||
import { numberToUint16BE } from "../utilities/number";
|
||||
import { packOutgoingPacket } from "./packet";
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Unsubscribe)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Unsubscribe);
|
||||
|
||||
export interface UnsubscribeMessage {
|
||||
messageType: number
|
||||
messageType: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,10 +13,12 @@ export interface UnsubscribeMessage {
|
||||
* @param properties The properties for the message.
|
||||
* @returns An outgoing unsubscribe (0xFFFF) packet.
|
||||
*/
|
||||
export function packUnsubscribeMessage (properties: UnsubscribeMessage): Uint8Array {
|
||||
const data = numberToUint16BE(properties.messageType)
|
||||
export function packUnsubscribeMessage(
|
||||
properties: UnsubscribeMessage,
|
||||
): Uint8Array {
|
||||
const data = numberToUint16BE(properties.messageType);
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import Color from 'color'
|
||||
import { 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'
|
||||
import Color from "color";
|
||||
import { 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)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataRequest);
|
||||
|
||||
export interface UserDataRequestMessage {
|
||||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
username: string;
|
||||
colour: Color;
|
||||
clientId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,33 +19,36 @@ 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: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const encoder = new TextEncoder()
|
||||
export function packUserDataRequestMessage(
|
||||
properties: UserDataRequestMessage,
|
||||
key: Uint8Array = DEFAULT_KEY,
|
||||
): Uint8Array {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
// Prepare data in correct format.
|
||||
const username = encoder.encode(properties.username)
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = new Uint8Array(properties.colour.array())
|
||||
const clientId = encoder.encode(properties.clientId)
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
const username = encoder.encode(properties.username);
|
||||
const usernameLength = numberToUint16BE(username.length);
|
||||
const colour = new Uint8Array(properties.colour.array());
|
||||
const clientId = encoder.encode(properties.clientId);
|
||||
const clientIdLength = numberToUint16BE(clientId.length);
|
||||
|
||||
// Pack data.
|
||||
const packedData = new SmartBuffer()
|
||||
packedData.writeBytes(usernameLength)
|
||||
packedData.writeBytes(username)
|
||||
packedData.pad(32 - username.length)
|
||||
packedData.writeBytes(colour)
|
||||
packedData.writeBytes(clientIdLength)
|
||||
packedData.writeBytes(clientId)
|
||||
packedData.pad(32 - clientId.length)
|
||||
const packedData = new SmartBuffer();
|
||||
packedData.writeBytes(usernameLength);
|
||||
packedData.writeBytes(username);
|
||||
packedData.pad(32 - username.length);
|
||||
packedData.writeBytes(colour);
|
||||
packedData.writeBytes(clientIdLength);
|
||||
packedData.writeBytes(clientId);
|
||||
packedData.pad(32 - clientId.length);
|
||||
|
||||
// Encrypt the data.
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key)
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key);
|
||||
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,23 +56,25 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
|
||||
* @param data The decrypted data section of an incoming user data request (0x0002) message.
|
||||
* @returns An unpacked user data request (0x0002) message.
|
||||
*/
|
||||
export function unpackUserDataRequestMessage (data: Uint8Array): UserDataRequestMessage {
|
||||
export function unpackUserDataRequestMessage(
|
||||
data: Uint8Array,
|
||||
): UserDataRequestMessage {
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(data)
|
||||
const packedData = SmartBuffer.from(data);
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
packedData.cursor = 34
|
||||
const colour = packedData.readBytes(3)
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
const usernameLength = packedData.readUInt16();
|
||||
const username = packedData.readBytes(usernameLength);
|
||||
packedData.cursor = 34;
|
||||
const colour = packedData.readBytes(3);
|
||||
const clientIdLength = packedData.readUInt16();
|
||||
const clientId = packedData.readBytes(clientIdLength);
|
||||
|
||||
const decoder = new TextDecoder()
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: decoder.decode(username),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: decoder.decode(clientId)
|
||||
}
|
||||
clientId: decoder.decode(clientId),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import Color from 'color'
|
||||
import { 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'
|
||||
import Color from "color";
|
||||
import { 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)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataResponse);
|
||||
|
||||
export interface UserDataResponseMessage {
|
||||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
username: string;
|
||||
colour: Color;
|
||||
clientId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,32 +19,35 @@ 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: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const encoder = new TextEncoder()
|
||||
export function packUserDataResponseMessage(
|
||||
properties: UserDataResponseMessage,
|
||||
key: Uint8Array = DEFAULT_KEY,
|
||||
): Uint8Array {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
// Prepare data in correct format.
|
||||
const username = encoder.encode(properties.username)
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = new Uint8Array(properties.colour.array())
|
||||
const clientId = encoder.encode(properties.clientId)
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
const username = encoder.encode(properties.username);
|
||||
const usernameLength = numberToUint16BE(username.length);
|
||||
const colour = new Uint8Array(properties.colour.array());
|
||||
const clientId = encoder.encode(properties.clientId);
|
||||
const clientIdLength = numberToUint16BE(clientId.length);
|
||||
|
||||
// Pack data.
|
||||
const packedData = new SmartBuffer()
|
||||
packedData.writeBytes(usernameLength)
|
||||
packedData.writeBytes(username)
|
||||
packedData.pad(32 - username.length)
|
||||
packedData.writeBytes(colour)
|
||||
packedData.writeBytes(clientIdLength)
|
||||
packedData.writeBytes(clientId)
|
||||
packedData.pad(32 - clientId.length)
|
||||
const packedData = new SmartBuffer();
|
||||
packedData.writeBytes(usernameLength);
|
||||
packedData.writeBytes(username);
|
||||
packedData.pad(32 - username.length);
|
||||
packedData.writeBytes(colour);
|
||||
packedData.writeBytes(clientIdLength);
|
||||
packedData.writeBytes(clientId);
|
||||
packedData.pad(32 - clientId.length);
|
||||
|
||||
// Return encrypted data.
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key)
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key);
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,23 +55,25 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
|
||||
* @param data The decrypted data section of an incoming user data response (0x0003) message.
|
||||
* @returns A unpacked user data response (0x0003) message.
|
||||
*/
|
||||
export function unpackUserDataResponseMessage (data: Uint8Array): UserDataResponseMessage {
|
||||
export function unpackUserDataResponseMessage(
|
||||
data: Uint8Array,
|
||||
): UserDataResponseMessage {
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(data)
|
||||
const packedData = SmartBuffer.from(data);
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
packedData.cursor = 34
|
||||
const colour = packedData.readBytes(3)
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
const usernameLength = packedData.readUInt16();
|
||||
const username = packedData.readBytes(usernameLength);
|
||||
packedData.cursor = 34;
|
||||
const colour = packedData.readBytes(3);
|
||||
const clientIdLength = packedData.readUInt16();
|
||||
const clientId = packedData.readBytes(clientIdLength);
|
||||
|
||||
const decoder = new TextDecoder()
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: decoder.decode(username),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: decoder.decode(clientId)
|
||||
}
|
||||
clientId: decoder.decode(clientId),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
* @param number The number to pack.
|
||||
* @returns The packed buffer.
|
||||
*/
|
||||
export function numberToUint16BE (number: number): Uint8Array {
|
||||
const ret = new Uint8Array(2)
|
||||
ret[0] = (number & 0xFF00) >> 8
|
||||
ret[1] = (number & 0x00FF) >> 0
|
||||
return ret
|
||||
export function numberToUint16BE(number: number): Uint8Array {
|
||||
const ret = new Uint8Array(2);
|
||||
ret[0] = (number & 0xff00) >> 8;
|
||||
ret[1] = (number & 0x00ff) >> 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -15,11 +15,11 @@ export function numberToUint16BE (number: number): Uint8Array {
|
||||
* @param number The number to pack.
|
||||
* @returns The packed buffer.
|
||||
*/
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,62 +1,64 @@
|
||||
import { numberToUint16BE, numberToUint32BE } from './number'
|
||||
import { numberToUint16BE, numberToUint32BE } from "./number";
|
||||
|
||||
export class SmartBuffer {
|
||||
private _data: number[]
|
||||
private _cursor: number
|
||||
private _data: number[];
|
||||
private _cursor: number;
|
||||
|
||||
/**
|
||||
* Wrap a buffer to track position and provide useful read / write functionality.
|
||||
* @param data Buffer to wrap (optional).
|
||||
*/
|
||||
constructor (length: number = 0) {
|
||||
this._data = new Array<number>(length)
|
||||
this._cursor = 0
|
||||
* Wrap a buffer to track position and provide useful read / write functionality.
|
||||
* @param data Buffer to wrap (optional).
|
||||
*/
|
||||
constructor(length: number = 0) {
|
||||
this._data = new Array<number>(length);
|
||||
this._cursor = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a regular buffer.
|
||||
*/
|
||||
get data (): Uint8Array {
|
||||
return new Uint8Array(this._data)
|
||||
* Return a regular buffer.
|
||||
*/
|
||||
get data(): Uint8Array {
|
||||
return new Uint8Array(this._data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the smart buffer to wrap new data.
|
||||
*/
|
||||
set data (data: Uint8Array) {
|
||||
this._data = Array.from(data)
|
||||
this.cursor = 0
|
||||
* Update the smart buffer to wrap new data.
|
||||
*/
|
||||
set data(data: Uint8Array) {
|
||||
this._data = Array.from(data);
|
||||
this.cursor = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the length of the smart buffer data.
|
||||
*/
|
||||
get length (): number {
|
||||
return this._data.length
|
||||
* Get the length of the smart buffer data.
|
||||
*/
|
||||
get length(): number {
|
||||
return this._data.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the length of the smart buffer data.
|
||||
*/
|
||||
set length (length: number) {
|
||||
this._data.length = length
|
||||
* Set the length of the smart buffer data.
|
||||
*/
|
||||
set length(length: number) {
|
||||
this._data.length = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current cursor position of the smart buffer.
|
||||
*/
|
||||
get cursor (): number {
|
||||
return this._cursor
|
||||
* Get the current cursor position of the smart buffer.
|
||||
*/
|
||||
get cursor(): number {
|
||||
return this._cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cursor position of the smart buffer.
|
||||
*/
|
||||
set cursor (position: number) {
|
||||
* Set the cursor position of the smart buffer.
|
||||
*/
|
||||
set cursor(position: number) {
|
||||
if (position < 0) {
|
||||
throw RangeError(`Cannot seek to ${this.cursor} of ${this.length} bytes.`)
|
||||
throw RangeError(
|
||||
`Cannot seek to ${this.cursor} of ${this.length} bytes.`,
|
||||
);
|
||||
}
|
||||
this._cursor = position
|
||||
this._cursor = position;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,33 +66,33 @@ export class SmartBuffer {
|
||||
* @param data The object to convert to a new SmartBuffer.
|
||||
* @returns A new SmartBuffer.
|
||||
*/
|
||||
static from (data: number[] | ArrayBuffer): SmartBuffer {
|
||||
const smartBuffer = new SmartBuffer()
|
||||
static from(data: number[] | ArrayBuffer): SmartBuffer {
|
||||
const smartBuffer = new SmartBuffer();
|
||||
if (data instanceof ArrayBuffer) {
|
||||
smartBuffer._data = Array.from(new Uint8Array(data))
|
||||
smartBuffer._data = Array.from(new Uint8Array(data));
|
||||
} else {
|
||||
smartBuffer._data = Array.from(data)
|
||||
smartBuffer._data = Array.from(data);
|
||||
}
|
||||
return smartBuffer
|
||||
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(...Array<number>(length).fill(0))
|
||||
this.cursor += length
|
||||
* Pads bytes to the end of the smart buffer.
|
||||
* @param length The number of bytes to pad.
|
||||
*/
|
||||
pad(length: number): void {
|
||||
this._data.push(...Array<number>(length).fill(0));
|
||||
this.cursor += length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data from the specified range.
|
||||
* @param start The start position.
|
||||
* @param end The end position.
|
||||
* @returns A new buffer containing data from the specified range.
|
||||
*/
|
||||
slice (start: number, end: number): Uint8Array {
|
||||
return new Uint8Array(this._data.slice(start, end))
|
||||
* Return the data from the specified range.
|
||||
* @param start The start position.
|
||||
* @param end The end position.
|
||||
* @returns A new buffer containing data from the specified range.
|
||||
*/
|
||||
slice(start: number, end: number): Uint8Array {
|
||||
return new Uint8Array(this._data.slice(start, end));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,66 +101,66 @@ export class SmartBuffer {
|
||||
* @param deleteCount The number of items to remove before inserting new data.
|
||||
* @param items The items to insert at the specified position.
|
||||
*/
|
||||
splice (start: number, deleteCount: number, ...items: number[]): void {
|
||||
splice(start: number, deleteCount: number, ...items: number[]): void {
|
||||
if (this.length < start) {
|
||||
this._data.push(...Array<number>(start))
|
||||
this._data.push(...Array<number>(start));
|
||||
}
|
||||
this._data.splice(this.cursor, deleteCount, ...items)
|
||||
this._data.splice(this.cursor, deleteCount, ...items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a UInt16 number from the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @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 (num[0] << 8 | num[1]) >>> 0
|
||||
* Read a UInt16 number from the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @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 ((num[0] << 8) | num[1]) >>> 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a UInt32 number from the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @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 (num[0] << 24 | num[1] << 16 | num[2] << 8 | num[3]) >>> 0
|
||||
* Read a UInt32 number from the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @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 ((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): Uint8Array {
|
||||
this.cursor += length
|
||||
return this.slice(this.cursor - length, this.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.
|
||||
*/
|
||||
readBytes(length: number): Uint8Array {
|
||||
this.cursor += length;
|
||||
return this.slice(this.cursor - length, this.cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a UInt16 number to the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @param number The number to write in UInt16 format at the current cursor position.
|
||||
*/
|
||||
writeUInt16 (number: number): void {
|
||||
this.splice(this.cursor, 2, ...numberToUint16BE(number))
|
||||
this.cursor += 2
|
||||
* Write a UInt16 number to the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @param number The number to write in UInt16 format at the current cursor position.
|
||||
*/
|
||||
writeUInt16(number: number): void {
|
||||
this.splice(this.cursor, 2, ...numberToUint16BE(number));
|
||||
this.cursor += 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a UInt32 number to the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @param number The number to write in UInt32 format at the current cursor position.
|
||||
*/
|
||||
writeUInt32 (number: number): void {
|
||||
this.splice(this.cursor, 4, ...numberToUint32BE(number))
|
||||
this.cursor += 4
|
||||
* Write a UInt32 number to the smart buffer at the current cursor position, and increment the cursor.
|
||||
* @param number The number to write in UInt32 format at the current cursor position.
|
||||
*/
|
||||
writeUInt32(number: number): void {
|
||||
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: number[] | Uint8Array): void {
|
||||
this.splice(this.cursor, buffer.length, ...buffer)
|
||||
this.cursor += buffer.length
|
||||
* 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[] | Uint8Array): void {
|
||||
this.splice(this.cursor, buffer.length, ...buffer);
|
||||
this.cursor += buffer.length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user