@flyingrobots/parley-read 0.0.0-stage → 0.1.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/CHANGELOG.md +272 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +560 -2
- package/package.json +39 -4
- package/src/digest.ts +438 -0
- package/src/fold.ts +166 -0
- package/src/index.ts +10 -0
- package/src/lines.ts +77 -0
- package/src/load.ts +711 -0
- package/src/locate.ts +70 -0
- package/src/open.ts +435 -0
- package/src/query.ts +760 -0
- package/src/rank.ts +309 -0
- package/src/schemas.ts +2042 -0
- package/src/validate.ts +94 -0
package/src/lines.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Lines, as spec §1.4 defines them for a reader: "A reader MUST buffer until a newline before
|
|
2
|
+
// parsing. A read chunk or a flush is not a line boundary; valid JSON that arrived across two
|
|
3
|
+
// reads is not garbage." So the buffer holds bytes, not characters — a multi-byte character split
|
|
4
|
+
// across a chunk boundary is not two broken characters either — and only `\n` ends a line. What is
|
|
5
|
+
// left over at EOF is not a line (§5.4) and is reported as such by the caller.
|
|
6
|
+
import { closeSync, openSync, readSync } from "node:fs";
|
|
7
|
+
|
|
8
|
+
const NEWLINE = 0x0a;
|
|
9
|
+
/** 64 KiB: large enough that a receipt of ordinary lines takes few reads, small enough to prove the buffering. */
|
|
10
|
+
export const CHUNK_BYTES = 64 * 1024;
|
|
11
|
+
|
|
12
|
+
export interface Line {
|
|
13
|
+
/** 1-based position in the stream, counting complete lines; the unterminated tail takes the next number. */
|
|
14
|
+
readonly n: number;
|
|
15
|
+
readonly raw: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Feed bytes in, take complete lines out. The remainder is held until the newline that ends it
|
|
20
|
+
* arrives, and `rest()` reports whatever never got one.
|
|
21
|
+
*/
|
|
22
|
+
export class LineBuffer {
|
|
23
|
+
private held: Buffer = Buffer.alloc(0);
|
|
24
|
+
private n = 0;
|
|
25
|
+
|
|
26
|
+
/** The complete lines this chunk finished, in order. A chunk that finishes none returns `[]`. */
|
|
27
|
+
push(chunk: Uint8Array | string): Line[] {
|
|
28
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk, "utf8") : Buffer.from(chunk);
|
|
29
|
+
this.held = this.held.length === 0 ? bytes : Buffer.concat([this.held, bytes]);
|
|
30
|
+
const out: Line[] = [];
|
|
31
|
+
let start = 0;
|
|
32
|
+
for (;;) {
|
|
33
|
+
const at = this.held.indexOf(NEWLINE, start);
|
|
34
|
+
if (at < 0) break;
|
|
35
|
+
this.n += 1;
|
|
36
|
+
out.push({ n: this.n, raw: this.held.subarray(start, at).toString("utf8") });
|
|
37
|
+
start = at + 1;
|
|
38
|
+
}
|
|
39
|
+
if (start > 0) this.held = this.held.subarray(start);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The unterminated fragment at EOF, or null when the last byte was a newline. */
|
|
44
|
+
rest(): Line | null {
|
|
45
|
+
return this.held.length === 0 ? null : { n: this.n + 1, raw: this.held.toString("utf8") };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** How many complete lines have been taken out so far. */
|
|
49
|
+
get count(): number { return this.n; }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface Split { readonly lines: Line[]; readonly unterminated: Line | null }
|
|
53
|
+
|
|
54
|
+
/** Split text, or bytes, the way a reader must: on newlines only, with the tail kept apart. */
|
|
55
|
+
export function splitLines(text: string | Uint8Array): Split {
|
|
56
|
+
const buffer = new LineBuffer();
|
|
57
|
+
const lines = buffer.push(text);
|
|
58
|
+
return { lines, unterminated: buffer.rest() };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Read a file in chunks through a `LineBuffer`, so the buffering is the real thing and not a pretence. */
|
|
62
|
+
export function readFileLines(file: string, chunkBytes = CHUNK_BYTES): Split {
|
|
63
|
+
const buffer = new LineBuffer();
|
|
64
|
+
const lines: Line[] = [];
|
|
65
|
+
const chunk = Buffer.allocUnsafe(chunkBytes);
|
|
66
|
+
const fd = openSync(file, "r");
|
|
67
|
+
try {
|
|
68
|
+
for (;;) {
|
|
69
|
+
const read = readSync(fd, chunk, 0, chunkBytes, null);
|
|
70
|
+
if (read === 0) break;
|
|
71
|
+
for (const line of buffer.push(chunk.subarray(0, read))) lines.push(line);
|
|
72
|
+
}
|
|
73
|
+
} finally {
|
|
74
|
+
closeSync(fd);
|
|
75
|
+
}
|
|
76
|
+
return { lines, unterminated: buffer.rest() };
|
|
77
|
+
}
|