Skip to content

Commit

Permalink
Adding getBytes method, improving putBytes, updating logger, and publ…
Browse files Browse the repository at this point in the history
…ishing v1.0.0 final
  • Loading branch information
Tynarus committed Dec 24, 2020
1 parent 672aa14 commit 5048ea0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 7 additions & 2 deletions src/buffer/byte-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
20 changes: 14 additions & 6 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
};
3 changes: 2 additions & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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));
}

Expand Down

0 comments on commit 5048ea0

Please sign in to comment.