From 5048ea07ec57201f257ea7bca743154e0f7820a0 Mon Sep 17 00:00:00 2001 From: Tynarus Date: Thu, 24 Dec 2020 16:14:05 -0600 Subject: [PATCH] Adding getBytes method, improving putBytes, updating logger, and publishing v1.0.0 final --- package-lock.json | 2 +- package.json | 2 +- src/buffer/byte-buffer.ts | 9 +++++++-- src/index.ts | 2 +- src/logger/logger.ts | 20 ++++++++++++++------ src/test.ts | 3 ++- 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index f6625d7..b6954e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@runejs/core", - "version": "1.0.0-beta.0", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ffe9928..02ae971 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@runejs/core", - "version": "1.0.0-beta.0", + "version": "1.0.0", "description": "Core functionality for RuneJS applications.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/buffer/byte-buffer.ts b/src/buffer/byte-buffer.ts index 6f02a3e..b5f6312 100644 --- a/src/buffer/byte-buffer.ts +++ b/src/buffer/byte-buffer.ts @@ -160,11 +160,16 @@ export class ByteBuffer extends Uint8Array { return new ByteBuffer(this.slice(position, position + length)); } - public putBytes(from: ByteBuffer | Buffer): void { - from.copy(this, this.writerIndex, 0); + public putBytes(from: ByteBuffer | Buffer, fromStart?: number, fromEnd?: number): void { + from.copy(this, this.writerIndex, fromStart || 0, fromEnd || from.length); this.writerIndex = (this.writerIndex + from.length); } + public getBytes(to: ByteBuffer | Buffer, length?: number): void { + this.copy(to, 0, this.readerIndex, this.readerIndex + length); + this.readerIndex += length; + } + public readInt8: (offset: number) => number; public readUInt8: (offset: number) => number; public copy: (targetBuffer: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number) => number; diff --git a/src/index.ts b/src/index.ts index ba4592a..1e2c546 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ export { ByteBuffer } from './buffer/byte-buffer'; -export { logger, setLoggerDateFormat } from './logger/logger'; +export * from './logger/logger'; export * from './net/socket-server'; export { parseServerConfig } from './net/server-config'; diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 110cc19..f0e2e04 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -7,6 +7,7 @@ export const setLoggerDateFormat = (format: string) => runeJsLoggerDateFormat = const log = (consoleType: string, ...args: any[]): void => { const date = moment().format(runeJsLoggerDateFormat); + args.forEach(msg => { if(consoleType === 'debug') { msg = cyan(msg); @@ -15,14 +16,21 @@ const log = (consoleType: string, ...args: any[]): void => { } else if(consoleType === 'error') { msg = red(msg); } - const str = gray(`[${date}]: `) + msg; - console[consoleType](str); + + if(typeof msg === 'string') { + const str = gray(`[${date}] `) + msg; + console[consoleType](str); + } else { + console[consoleType](gray(`[${date}]`), msg); + } }); }; export const logger = { - info: (...message: any[]) => log('info', ...message), - debug: (...message: any[]) => log('info', ...message), - warn: (...message: any[]) => log('warn', ...message), - error: (...message: any[]) => log('error', ...message) + info: (...messages: any[]) => log('info', ...messages), + debug: (...messages: any[]) => log('info', ...messages), + warn: (...messages: any[]) => log('warn', ...messages), + error: (...messages: any[]) => log('error', ...messages), + trace: (...messages: any[]) => log('trace', ...messages), + fatal: (...messages: any[]) => log('fatal', ...messages) }; diff --git a/src/test.ts b/src/test.ts index 0bcf400..933a9e7 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,4 +1,4 @@ -import { openServer, SocketConnectionHandler, logger, setLoggerDateFormat, ByteBuffer } from './index'; +import { openServer, SocketConnectionHandler, logger, ByteBuffer, setLoggerDateFormat } from './index'; import { Socket } from 'net'; class TestConnectionHandler extends SocketConnectionHandler { @@ -19,6 +19,7 @@ class TestConnectionHandler extends SocketConnectionHandler { function launchTestServer() { setLoggerDateFormat('HH:mm:ss'); + logger.info('Starting server...', { hello: 'world' }); openServer('Test Server', '0.0.0.0', 43586, socket => new TestConnectionHandler(socket)); }