Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/log-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,29 @@ export type Events =
| ValidatedEvent
| WarmodEvent;

const { length: LENGTH_OF_DATE } = "10/20/2020 - 10:30:50: ";
const { length: LENGTH_OF_DATE_FILE } = "10/20/2020 - 10:30:50: "; // File format
const { length: LENGTH_OF_DATE_HTTP } = "01/28/2024 - 13:11:03.628 - "; // Http format

export interface IParseOptions {
parsers?: IBaseParser<Events>[];
format?: "file" | "http";
}

export function parse(rawLog: string, { parsers = defaultParsers }: IParseOptions = {}): Events | undefined {
const receivedAt = new Date(rawLog.substring(0, LENGTH_OF_DATE - 2).replace(" - ", " "));
export function parse(rawLog: string, { parsers = defaultParsers, format="file" }: IParseOptions = {}): Events | undefined {

const log = rawLog.substring(LENGTH_OF_DATE);
let receivedAt: Date;
let log: string;

if (format==="file"){
receivedAt = new Date(rawLog.substring(0, LENGTH_OF_DATE_FILE - 2).replace(" - ", " "));
log = rawLog.substring(LENGTH_OF_DATE_FILE);
} else if (format==="http"){
receivedAt = new Date(rawLog.substring(0, LENGTH_OF_DATE_HTTP - 3).replace(" - ", " "));
log = rawLog.substring(LENGTH_OF_DATE_HTTP);
} else {
throw new Error("Invalid format specified.");
}

for (const parser of parsers) {
const pattern = parser.patterns.find((item) => item.test(log));

Expand Down
45 changes: 45 additions & 0 deletions tests/received_at.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ok } from "assert";
import { parse, IParseOptions } from "../src";

const DATE_FILE = "01/28/2024 - 16:17:18: "
const DATE_HTTP = "01/28/2024 - 16:17:18.000 - "
const DATE = "01/28/2024 16:17:18"

describe("parse function", (): void => {
it("should correctly parse the receivedAt date for 'file' format", () => {
const log = `${DATE_FILE}"PlayerName<1><[U:1:230970467]><>" connected, address ""`;
const options: IParseOptions = {
format: 'file'
}
const result = parse(log, options);
const expectedDate = new Date(DATE)

ok(result !== undefined, `Failed parse log: ${log}`);
expect(result).toBeDefined();
expect(result.receivedAt).toEqual(expectedDate);
});

it("should correctly parse the receivedAt date for 'http' format", () => {
const log = `${DATE_HTTP}"PlayerName<1><[U:1:230970467]><>" connected, address ""`;
const options: IParseOptions = {
format: 'http'
}
const result = parse(log, options);
const expectedDate = new Date(DATE)

ok(result !== undefined, `Failed parse log: ${log}`);
expect(result).toBeDefined();
expect(result.receivedAt).toEqual(expectedDate);
});

it('should use "file" when not specified for backwards compatibility.', () => {
const log = `${DATE_FILE}"PlayerName<1><[U:1:230970467]><>" connected, address ""`;
const result = parse(log);
const expectedDate = new Date(DATE)

ok(result !== undefined, `Failed parse log: ${log}`);
expect(result).toBeDefined();
expect(result.receivedAt).toBeInstanceOf(Date);
expect(result.receivedAt).toEqual(expectedDate);
});
});