plc-replica/scan-from-raw.ts

37 lines
1 KiB
TypeScript
Raw Permalink Normal View History

2024-11-27 16:30:50 +00:00
import { TextLineStream } from "jsr:@std/streams@1/text-line-stream";
import { ExportEntry } from "./directory-tailer.ts";
import { tailer } from "./main.ts";
export const fullScan = async (fromByteOffset?: number) => {
2024-11-27 16:30:50 +00:00
using exports = await Deno.open("./data/exports.jsonl", { read: true });
2024-11-28 03:08:47 +00:00
if (fromByteOffset) {
await exports.seek(fromByteOffset, Deno.SeekMode.Start);
}
2024-11-28 03:08:47 +00:00
2024-11-27 16:30:50 +00:00
const lineStream = exports.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
2024-11-28 03:08:47 +00:00
if (fromByteOffset) {
2024-11-28 03:08:47 +00:00
const reader = lineStream.getReader();
const line = await reader.read();
console.log("dropping: " + line.value);
reader.releaseLock();
}
2024-11-27 16:30:50 +00:00
for await (const line of lineStream.values()) {
const entry = JSON.parse(line) as unknown as ExportEntry;
2024-11-28 03:08:47 +00:00
await tailer.processRecord(entry, line);
2024-11-27 16:30:50 +00:00
}
};
if (import.meta.main) {
const fromByteOffsetStr = Deno.args.at(0);
const fromByteOffset = fromByteOffsetStr
? parseInt(fromByteOffsetStr)
: undefined;
await fullScan(fromByteOffset);
}