Skip to content

Commit

Permalink
Update package and dependencies versions
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Dec 4, 2023
1 parent f971d22 commit aae958e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 59 deletions.
47 changes: 2 additions & 45 deletions sync-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions sync-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-room",
"version": "3.0.5",
"version": "3.0.6",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand All @@ -21,7 +21,6 @@
"license": "MIT",
"dependencies": {
"@google-cloud/agones-sdk": "^1.35.0",
"buffer": "^6.0.3",
"msgpack-lite": "0.1.26",
"rxjs": "7.8.1"
},
Expand Down
5 changes: 2 additions & 3 deletions sync-server/src/transports/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TransportCommon } from './common';
import { BehaviorSubject } from 'rxjs';
import { Utils } from '../utils';
import { NotAuthorized } from '../errors/not-authorized';
import { Buffer } from 'buffer/';

export type BandwidthSocket = { incoming: { size: number, timestamp: number }[], outgoing: { size: number, timestamp: number }[] }
export type BandwidthData = Record<string, BehaviorSubject<BandwidthSocket>>
Expand Down Expand Up @@ -70,7 +69,7 @@ export class Transport extends TransportCommon {
// Intercept incoming messages
socket.use((packet, nextMiddleware) => {
if (packet && packet[1]) {
const packetSize = Buffer.from(JSON.stringify(packet)).length - 2;
const packetSize = Utils.bufferFrom(JSON.stringify(packet)).length - 2;
const data = { size: packetSize, timestamp: Date.now() };

this.updateBandwidthData(playerId, { incoming: data });
Expand All @@ -88,7 +87,7 @@ export class Transport extends TransportCommon {
// Intercept outgoing messages
const originalEmit = socket.emit;
socket.emit = (...args) => {
const packetSize = Buffer.from(JSON.stringify(args)).length - 2;
const packetSize = Utils.bufferFrom(JSON.stringify(args)).length - 2;
const data = { size: packetSize, timestamp: Date.now() };

this.updateBandwidthData(playerId, { outgoing: data });
Expand Down
32 changes: 23 additions & 9 deletions sync-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,57 @@ export class Utils {

static set(obj, path, value, onlyPlainObject = false) {
if (Object(obj) !== obj) return obj;

if (typeof path === 'string') {
path = path.split('.');
}

let len = path.length;
if (!len) return obj;

let current = obj;
for (let i = 0; i < len - 1; i++) {
let segment = path[i];
let nextSegment = path[i + 1];
let isNextNumeric = !isNaN(nextSegment) && isFinite(nextSegment);

if (!current[segment] || typeof current[segment] !== 'object') {
current[segment] = (isNextNumeric && !onlyPlainObject) ? [] : {};
}

current = current[segment];
}

current[path[len - 1]] = value;

return obj;
}

static get(obj, path) {
const keys = path.split('.');
let current = obj;

for (let key of keys) {
if (current[key] === undefined) {
return undefined;
}
current = current[key];
}

return current;
}

static bufferFrom(input: string | ArrayBuffer | ArrayBufferView): Uint8Array {
if (typeof input === 'string') {
// If the input is a string, convert it to an ArrayBuffer
let encoder = new TextEncoder();
return encoder.encode(input);
} else if (input instanceof ArrayBuffer || ArrayBuffer.isView(input)) {
// If the input is an ArrayBuffer or a view of it, return it as Uint8Array
return new Uint8Array(input as ArrayBuffer);
} else {
// Extend this function for other types of inputs as needed
throw new Error('Input type not supported');
}
}
}
34 changes: 34 additions & 0 deletions sync-server/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Utils } from '../src'
import { describe, test, expect } from 'vitest'

describe('Utils.bufferFrom', () => {
// Example test for string input
test('should convert string to Uint8Array', () => {
const input = 'Hello World';
const result = Utils.bufferFrom(input);
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toEqual(new TextEncoder().encode(input));
});

// Example test for ArrayBuffer input
test('should handle ArrayBuffer input', () => {
const buffer = new ArrayBuffer(10);
const result = Utils.bufferFrom(buffer);
expect(result).toBeInstanceOf(Uint8Array);
expect(result.byteLength).toBe(buffer.byteLength);
});

// Example test for input that is a view of ArrayBuffer
test('should handle ArrayBufferView input', () => {
const buffer = new Uint8Array([1, 2, 3]);
const result = Utils.bufferFrom(buffer);
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toEqual(buffer);
});

// Example test for unsupported input types
test('should throw error for unsupported input types', () => {
const input = { not: 'supported' };
expect(() => Utils.bufferFrom(input as any)).toThrow('Input type not supported');
});
});

0 comments on commit aae958e

Please sign in to comment.