Skip to content

Commit

Permalink
couple of bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
doomestee committed Dec 3, 2023
1 parent 596bec5 commit 01fd802
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default class Message<T=string> {

addT(check: boolean, value: any, type: EntryType, errorMessage: string) {
if (check) {
if (type === EntryType.ByteArray && Array.isArray(value)) value = new ByteArray(value);
if (type === EntryType.ByteArray) value = new ByteArray(value);

this.objects.push(value);
this.types.push(type);
Expand Down
19 changes: 15 additions & 4 deletions lib/structures/ByteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { decode, encode, encodingExists } from "iconv-lite";
import { deflateRawSync, inflateRawSync } from "zlib";
import type { InputType, ZlibOptions } from "zlib";

export enum Endian {
LITTLE_ENDIAN = "LE",
Expand All @@ -20,9 +21,9 @@ export default class ByteArray {
*/
protected hashendian:"LE"|"BE" = "BE";

constructor(buffer:Buffer|Array<number>|number) {
constructor(buffer:Buffer|Array<number>|number|Uint8Array) {
this.buffer = Buffer.isBuffer(buffer) ? buffer
: Array.isArray(buffer) ? Buffer.from(buffer)
: Array.isArray(buffer) || buffer instanceof Uint8Array ? Buffer.from(buffer)
: Number.isInteger(buffer) ? Buffer.alloc(buffer) : Buffer.alloc(0);
}

Expand Down Expand Up @@ -142,6 +143,10 @@ export default class ByteArray {
this.hashposition = this.length;
}

static compressor(buf: InputType, options?: ZlibOptions) {
return deflateRawSync(buf, options);
}

/**
* Reads a boolean
*/
Expand Down Expand Up @@ -302,6 +307,10 @@ export default class ByteArray {
this.hashposition = 0;
}

static uncompressor(buf: InputType, options?: ZlibOptions) {
return inflateRawSync(buf, options);
}

/**
* Writes a boolean (internally a 0 or 1)
*/
Expand All @@ -320,16 +329,18 @@ export default class ByteArray {
/**
* Writes multiple signed bytes to a ByteArray
*/
writeBytes(bytes:ByteArray|Buffer, offset:number = 0, length:number = 0):void {
writeBytes(bytes:ByteArray|Buffer|Array<any>, offset:number = 0, length:number = 0):void {
if (length === 0) {
length = bytes.length - offset;
}

this.hashexpand(length);

for (let i = 0; i < length; i++) {
this.buffer[i + this.hashposition] = Buffer.isBuffer(bytes) ? bytes[i + offset] : bytes.buffer[i + offset];
this.buffer[i + this.hashposition] = Buffer.isBuffer(bytes) || bytes instanceof Array ? bytes[i + offset] : bytes.buffer[i + offset];
}

this.hashposition += length;
}


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playerioclient",
"version": "1.0.3",
"version": "1.0.4",
"description": "A wrapper that sends requests and connects to PlayerIO via HTTP and WS respectively.",
"homepage": "https://github.com/doomestee/PlayerIOClient.js",
"repository": {
Expand Down

0 comments on commit 01fd802

Please sign in to comment.