commit db1ccadf9bde3c4326cc21966b4a8be689ccfa71 Author: worm Date: Thu Oct 5 17:29:11 2023 -0700 Initial commit but with files this time diff --git a/main.mjs b/main.mjs new file mode 100644 index 0000000..bfb5b9c --- /dev/null +++ b/main.mjs @@ -0,0 +1,32 @@ +import { createReadStream } from 'fs'; +import { createInterface } from 'node:readline'; + +const fileToRead = process.argv[2]; + +// https://nodejs.org/api/readline.html#readline_example_read_file_stream_line_by_line + +// TEST LOG LINE +// The logs are read line by line, and aren't put through the JS parser (aren't parsed as JS source). +// As such, for testing String.raw is indicated +const testLine = String.raw `2023-09-28T03:16:24.580872000Z {"log":"52.88.91.78 - - [28/Sep/2023:03:16:23 +0000] \"GET /api/v2/account/details HTTP/1.1\" 200 1006 \"-\" \"go-tfe\"","component":"nginx"}'` + +async function processLineByLine(filename) { + const fileStream = createReadStream(filename); + + const rl = createInterface({ + input: fileStream, + crlfDelay: Infinity, + }); + + for await (const line of rl) { + console.log(JSON.parse(getLineJSON(line).log)); + } +} + +async function getLineJSON(line) { + return line.split(/ (?=\{)/)[1]; +} + +// processLineByLine(fileToRead); + +console.log(await getLineJSON(testLine)); \ No newline at end of file