@oh-my-pi/pi-utils 17.4.2 → 18.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/logger.ts +19 -15
- package/src/stream.ts +11 -23
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@oh-my-pi/pi-utils",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "18.0.0",
|
|
5
5
|
"description": "Shared utilities for pi packages",
|
|
6
6
|
"homepage": "https://omp.sh",
|
|
7
7
|
"author": "Stencil Labs, Inc.",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"fmt": "biome format --write ."
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@oh-my-pi/pi-natives": "
|
|
34
|
+
"@oh-my-pi/pi-natives": "18.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/bun": "^1.3.14"
|
package/src/logger.ts
CHANGED
|
@@ -91,7 +91,7 @@ function pruneStaleProcessLogs(dir: string): void {
|
|
|
91
91
|
`${cutoff.getFullYear()}-${String(cutoff.getMonth() + 1).padStart(2, "0")}-` +
|
|
92
92
|
String(cutoff.getDate()).padStart(2, "0");
|
|
93
93
|
|
|
94
|
-
const staleLogsByProcessDay = new Map<string, Array<{ path: string;
|
|
94
|
+
const staleLogsByProcessDay = new Map<string, Array<{ path: string; rollover: number }>>();
|
|
95
95
|
for (const entry of entries) {
|
|
96
96
|
if (!entry.isFile()) continue;
|
|
97
97
|
const logMatch = PROCESS_LOG_PATTERN.exec(entry.name);
|
|
@@ -120,25 +120,29 @@ function pruneStaleProcessLogs(dir: string): void {
|
|
|
120
120
|
continue;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
});
|
|
131
|
-
staleLogsByProcessDay.set(key, staleLogs);
|
|
132
|
-
} catch {
|
|
133
|
-
// Another process may have pruned the same stale namespace.
|
|
134
|
-
}
|
|
123
|
+
const key = `${pidText}:${logMatch[1]}`;
|
|
124
|
+
const staleLogs = staleLogsByProcessDay.get(key) ?? [];
|
|
125
|
+
staleLogs.push({
|
|
126
|
+
path: entryPath,
|
|
127
|
+
rollover: Number(logMatch[3] ?? 0),
|
|
128
|
+
});
|
|
129
|
+
staleLogsByProcessDay.set(key, staleLogs);
|
|
135
130
|
}
|
|
136
131
|
|
|
137
132
|
for (const staleLogs of staleLogsByProcessDay.values()) {
|
|
138
|
-
staleLogs.
|
|
133
|
+
if (staleLogs.length <= RETAINED_STALE_LOGS_PER_PROCESS_DAY) continue;
|
|
134
|
+
const ranked: Array<{ path: string; mtimeMs: number; rollover: number }> = [];
|
|
135
|
+
for (const stale of staleLogs) {
|
|
136
|
+
try {
|
|
137
|
+
ranked.push({ ...stale, mtimeMs: fs.statSync(stale.path).mtimeMs });
|
|
138
|
+
} catch {
|
|
139
|
+
// Another process may have pruned the same stale namespace.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
ranked.sort(
|
|
139
143
|
(a, b) => b.mtimeMs - a.mtimeMs || b.rollover - a.rollover || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0),
|
|
140
144
|
);
|
|
141
|
-
for (const stale of
|
|
145
|
+
for (const stale of ranked.slice(RETAINED_STALE_LOGS_PER_PROCESS_DAY)) {
|
|
142
146
|
try {
|
|
143
147
|
fs.rmSync(stale.path, { force: true });
|
|
144
148
|
} catch {
|
package/src/stream.ts
CHANGED
|
@@ -4,25 +4,6 @@ import { abortableSource } from "./abortable";
|
|
|
4
4
|
import { parseStreamingJson } from "./json-parse";
|
|
5
5
|
|
|
6
6
|
const LF = 0x0a;
|
|
7
|
-
type JsonlChunkResult = {
|
|
8
|
-
values: unknown[];
|
|
9
|
-
error: unknown;
|
|
10
|
-
read: number;
|
|
11
|
-
done: boolean;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
function parseJsonlChunkCompat(input: Uint8Array, beg?: number, end?: number): JsonlChunkResult;
|
|
15
|
-
function parseJsonlChunkCompat(input: string): JsonlChunkResult;
|
|
16
|
-
function parseJsonlChunkCompat(input: Uint8Array | string, beg?: number, end?: number): JsonlChunkResult {
|
|
17
|
-
if (typeof input === "string") {
|
|
18
|
-
const { values, error, read, done } = Bun.JSONL.parseChunk(input);
|
|
19
|
-
return { values, error, read, done };
|
|
20
|
-
}
|
|
21
|
-
const start = beg ?? 0;
|
|
22
|
-
const stop = end ?? input.length;
|
|
23
|
-
const { values, error, read, done } = Bun.JSONL.parseChunk(input, start, stop);
|
|
24
|
-
return { values, error, read, done };
|
|
25
|
-
}
|
|
26
7
|
|
|
27
8
|
export async function* readLines(stream: ReadableStream<Uint8Array>, signal?: AbortSignal): AsyncGenerator<Uint8Array> {
|
|
28
9
|
const buffer = new ConcatSink();
|
|
@@ -58,7 +39,7 @@ export async function* readJsonl<T>(stream: ReadableStream<Uint8Array>, signal?:
|
|
|
58
39
|
const tail = buffer.flush();
|
|
59
40
|
if (tail) {
|
|
60
41
|
buffer.clear();
|
|
61
|
-
const { values, error, done } =
|
|
42
|
+
const { values, error, done } = Bun.JSONL.parseChunk(tail, 0, tail.length);
|
|
62
43
|
if (values.length > 0) {
|
|
63
44
|
yield* values as T[];
|
|
64
45
|
}
|
|
@@ -174,8 +155,15 @@ class ConcatSink {
|
|
|
174
155
|
return text;
|
|
175
156
|
}
|
|
176
157
|
*pullJSONL<T>(chunk: Uint8Array, beg: number, end: number) {
|
|
158
|
+
const newline = chunk.indexOf(LF, beg);
|
|
159
|
+
if (newline === -1 || newline >= end) {
|
|
160
|
+
if (this.isEmpty) this.reset(chunk.subarray(beg, end));
|
|
161
|
+
else this.append(chunk.subarray(beg, end));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
177
165
|
if (this.isEmpty) {
|
|
178
|
-
const { values, error, read, done } =
|
|
166
|
+
const { values, error, read, done } = Bun.JSONL.parseChunk(chunk, beg, end);
|
|
179
167
|
if (values.length > 0) {
|
|
180
168
|
yield* values as T[];
|
|
181
169
|
}
|
|
@@ -192,7 +180,7 @@ class ConcatSink {
|
|
|
192
180
|
space.set(chunk.subarray(beg, end), offset);
|
|
193
181
|
this.#length = total;
|
|
194
182
|
|
|
195
|
-
const { values, error, read, done } =
|
|
183
|
+
const { values, error, read, done } = Bun.JSONL.parseChunk(space, 0, total);
|
|
196
184
|
if (values.length > 0) {
|
|
197
185
|
yield* values as T[];
|
|
198
186
|
}
|
|
@@ -456,7 +444,7 @@ export function parseJsonlLenient<T>(buffer: string, options: { onMalformedRecor
|
|
|
456
444
|
let entries: T[] | undefined;
|
|
457
445
|
|
|
458
446
|
while (buffer.length > 0) {
|
|
459
|
-
const { values, error, read, done } =
|
|
447
|
+
const { values, error, read, done } = Bun.JSONL.parseChunk(buffer);
|
|
460
448
|
if (values.length > 0) {
|
|
461
449
|
const ext = values as T[];
|
|
462
450
|
if (!entries) {
|