-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.ts
132 lines (116 loc) · 3.81 KB
/
handlers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { type FileHandle, open } from "node:fs/promises";
import { basename, join } from "node:path";
const BUF_SIZE = 8192;
const CHAR_LF = "\n".charCodeAt(0);
export const LOG_DIR = process.env.NODE_ENV === "test" ? import.meta.dirname : "/var/log";
export async function readLogBatch(
signal: AbortSignal,
file: string,
keyword: string,
cursor: number,
limit: number,
): Promise<{
hasMore: boolean;
lines: string[];
cursor?: number;
}> {
const handle = await open(join(LOG_DIR, basename(file)), "r");
let lines: string[] = [];
let hasMore = false;
try {
let prevLineStart = cursor;
for await (const { line, lineStart } of readLines(handle, keyword, cursor)) {
if (signal.aborted) {
hasMore = prevLineStart > 0;
cursor = prevLineStart;
break;
} else if (lines.push(line) >= limit) {
hasMore = lineStart > 0;
cursor = lineStart;
break;
}
prevLineStart = lineStart;
}
} finally {
handle.close().catch(console.error);
}
return { hasMore, lines, cursor: hasMore ? cursor : undefined };
}
export async function* readLogStream(
signal: AbortSignal,
file: string,
keyword: string,
cursor: number,
limit: number,
): AsyncGenerator<{
event: "log";
data: { line: string };
} | {
event: "pagination";
data: { hasMore: boolean; cursor?: number };
}> {
const handle = await open(join(LOG_DIR, basename(file)), "r");
let hasMore = false;
try {
let prevLineStart = cursor;
let count = 0;
for await (const { line, lineStart } of readLines(handle, keyword, cursor)) {
if (signal.aborted || count >= limit) {
hasMore = prevLineStart > 0;
cursor = prevLineStart;
break;
}
yield { event: "log", data: { line } };
count++;
prevLineStart = lineStart;
}
} finally {
handle.close().catch(console.error);
}
yield {
event: "pagination",
data: { hasMore, cursor: hasMore ? cursor : undefined },
};
}
async function* readLines(handle: FileHandle, keyword: string, cursor: number): AsyncGenerator<{ line: string; lineStart: number }> {
const stat = await handle.stat();
const pair = { line: null as unknown as string, lineStart: -1 };
let end = Math.min(cursor, stat.size);
let position = Math.max(0, end - BUF_SIZE); // where to start reading the file
let buf = Buffer.alloc(end - position);
let foundLF = false;
while (true) {
const { bytesRead } = await handle.read(buf, { position });
let i = bytesRead-1, j = bytesRead, line = null as unknown as Buffer;
while (i >= 0) {
if (buf[i] === CHAR_LF) {
if (foundLF) {
line = buf.subarray(i+1, j);
if (line.includes(keyword)) {
pair.line = line.toString();
pair.lineStart = position + i + 1;
yield pair;
}
} else {
foundLF = true;
}
j = i;
} else if (foundLF && position+i === 0) {
line = buf.subarray(i, j);
if (line.includes(keyword)) {
pair.line = line.toString();
pair.lineStart = position + i;
yield pair;
}
}
i--;
}
if (position <= 0 || j >= bytesRead) {
break;
}
end = position + j + 1;
buf = buf.subarray(0, Math.min(end, BUF_SIZE));
position = Math.max(0, end - buf.byteLength);
foundLF = false;
}
}