claude-code-jsonl-parser 0.1.4-beta.24 → 0.1.4-beta.25
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/README.md +44 -43
- package/dist/index.cjs +64 -66
- package/dist/index.d.cts +95 -95
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +95 -95
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +64 -66
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
# claude-code-jsonl-parser
|
|
2
2
|
|
|
3
|
-
Claude Code
|
|
3
|
+
A zero-dependency TypeScript library that parses Claude Code JSONL conversation logs (such as `~/.claude/projects/**/*.jsonl`) in a type-safe way.
|
|
4
4
|
|
|
5
5
|
## ✨ Features
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
- **
|
|
10
|
-
-
|
|
11
|
-
-
|
|
7
|
+
- **Zero dependencies**: no runtime dependencies
|
|
8
|
+
- **No exceptions on bad input**: each line is classified as `known` / `unknown` / `error`
|
|
9
|
+
- **Diagnosable `unknown`**: a line of a known type with a mismatched shape is represented with `typeHint` and `reason`, so no data is lost
|
|
10
|
+
- **Whole-file and streaming APIs**: read a whole file at once, or process it line by line in constant memory
|
|
11
|
+
- **ESM and CJS**: usable from both Node.js module systems
|
|
12
12
|
|
|
13
|
-
## 📦
|
|
13
|
+
## 📦 Installation
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
16
|
npm install claude-code-jsonl-parser
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
## 🚀
|
|
19
|
+
## 🚀 Quick start
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
import { parseJsonlFile } from 'claude-code-jsonl-parser'
|
|
@@ -31,23 +31,22 @@ if (result.isOk) {
|
|
|
31
31
|
}
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
`parseJsonlFile`
|
|
35
|
-
JSON
|
|
36
|
-
`_kind: 'error'` / `_kind: 'unknown'` として表現される (後述)。
|
|
34
|
+
`parseJsonlFile` returns `Err` only for file I/O errors (the file does not exist, no permission, and so on).
|
|
35
|
+
A line with a JSON syntax error or an unknown shape does not cause an exception: the line itself is represented as a `ParsedLine` with `_kind: 'error'` or `_kind: 'unknown'` (see below).
|
|
37
36
|
|
|
38
37
|
## 📖 API
|
|
39
38
|
|
|
40
|
-
###
|
|
39
|
+
### Whole-text, whole-file and streaming APIs
|
|
41
40
|
|
|
42
|
-
|
|
|
41
|
+
| Function | Purpose |
|
|
43
42
|
| --- | --- |
|
|
44
|
-
| `parseJsonl(text)` |
|
|
45
|
-
| `parseJsonlLines(text)` |
|
|
46
|
-
| `parseJsonlFile(path)` |
|
|
47
|
-
| `parseJsonlFileStream(path)` |
|
|
43
|
+
| `parseJsonl(text)` | Parses a whole string at once (`Result<ParsedLine[], never>`) |
|
|
44
|
+
| `parseJsonlLines(text)` | Parses a whole string with a synchronous line-by-line iterator |
|
|
45
|
+
| `parseJsonlFile(path)` | Reads and parses a whole file at once (`ResultAsync<ParsedLine[], FileReadError>`) |
|
|
46
|
+
| `parseJsonlFileStream(path)` | Parses a file line by line with an asynchronous iterator (constant memory); a read failure is thrown as a `FileReadError` during iteration |
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
Blank lines are skipped and do not count towards line numbers.
|
|
49
|
+
For large log files (tens of MB or more), `parseJsonlFileStream` keeps memory usage down because it does not hold every line in an array.
|
|
51
50
|
|
|
52
51
|
```ts
|
|
53
52
|
import { parseJsonlFileStream } from 'claude-code-jsonl-parser'
|
|
@@ -59,9 +58,9 @@ for await (const line of parseJsonlFileStream('/path/to/large-session.jsonl')) {
|
|
|
59
58
|
}
|
|
60
59
|
```
|
|
61
60
|
|
|
62
|
-
###
|
|
61
|
+
### Discriminating a ParsedLine
|
|
63
62
|
|
|
64
|
-
|
|
63
|
+
Discriminate the result of each line by `_kind`.
|
|
65
64
|
|
|
66
65
|
```ts
|
|
67
66
|
import type { ParsedLine } from 'claude-code-jsonl-parser'
|
|
@@ -69,59 +68,61 @@ import type { ParsedLine } from 'claude-code-jsonl-parser'
|
|
|
69
68
|
function handle(line: ParsedLine) {
|
|
70
69
|
switch (line._kind) {
|
|
71
70
|
case 'known': {
|
|
72
|
-
// line.type
|
|
71
|
+
// line.type narrows further to assistant / user / system / ...
|
|
73
72
|
break
|
|
74
73
|
}
|
|
75
74
|
case 'unknown': {
|
|
76
|
-
//
|
|
77
|
-
// line.
|
|
78
|
-
// line.
|
|
75
|
+
// An unknown type, or a known type with a mismatched shape.
|
|
76
|
+
// line.raw: the parsed JSON value as is
|
|
77
|
+
// line.typeHint: the original type value for a shape mismatch (undefined for an unknown type)
|
|
78
|
+
// line.reason: for a shape mismatch, a diagnostic message naming the offending fields
|
|
79
79
|
break
|
|
80
80
|
}
|
|
81
81
|
case 'error': {
|
|
82
|
-
// JSON
|
|
82
|
+
// A line with a JSON syntax error. line.rawLine is the text and line.message the syntax error message
|
|
83
83
|
break
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
`unknown` (`typeHint` に元の type、`reason` に不一致理由) として表現される。
|
|
91
|
-
例外は投げない。
|
|
89
|
+
A line of a known type that does not satisfy the guard's required fields is not dropped: it is represented as `unknown` (`typeHint` holds the original type and `reason` the cause), and no exception is thrown.
|
|
92
90
|
|
|
93
|
-
### content
|
|
91
|
+
### Narrowing content blocks
|
|
94
92
|
|
|
95
|
-
|
|
96
|
-
`
|
|
97
|
-
先に `_kind` で `'unknown'` かどうかを判定してから `.type` で絞り込む。
|
|
93
|
+
The elements of `message.content` in `assistant` / `user` entries (`AssistantContentBlock` / `UserContentBlock`) are also discriminated unions of known blocks and `UnknownContentBlock`.
|
|
94
|
+
Check `_kind` for `'unknown'` first, then narrow by `.type`.
|
|
98
95
|
|
|
99
96
|
```ts
|
|
100
97
|
import type { AssistantContentBlock } from 'claude-code-jsonl-parser'
|
|
101
98
|
|
|
102
99
|
function describe(block: AssistantContentBlock): string {
|
|
103
|
-
if (block._kind === 'unknown') return '(
|
|
100
|
+
if (block._kind === 'unknown') return '(unknown block)'
|
|
104
101
|
if (block.type === 'text') return block.text
|
|
105
102
|
if (block.type === 'thinking') return block.thinking
|
|
106
103
|
return `tool_use: ${block.name}`
|
|
107
104
|
}
|
|
108
105
|
```
|
|
109
106
|
|
|
110
|
-
###
|
|
107
|
+
### Result
|
|
111
108
|
|
|
112
|
-
`
|
|
113
|
-
`
|
|
114
|
-
|
|
109
|
+
`parseJsonl` and `parseJsonlFile` return a dependency-free `Result` / `ResultAsync`. Both expose `map`, `mapErr`, `andThen`, `match` and `unwrapOr`; `ok`, `err` and `ResultAsync` are exported.
|
|
110
|
+
A `Result` has `isOk` / `isErr` and either `value` or `error`; a `ResultAsync` can be awaited to obtain the `Result`.
|
|
111
|
+
|
|
112
|
+
### Exported type guards (advanced)
|
|
113
|
+
|
|
114
|
+
`guards`, `guardByType`, `KNOWN_TYPES`, `normalizeAssistantContent` and `normalizeUserContent` are exported for building your own parsing pipeline.
|
|
115
|
+
For normal use, the `parseJsonlFile` family of APIs is enough.
|
|
115
116
|
|
|
116
117
|
```ts
|
|
117
118
|
import { guardByType } from 'claude-code-jsonl-parser'
|
|
118
119
|
|
|
119
120
|
const result = guardByType('assistant', someRawObject)
|
|
120
|
-
// result === undefined
|
|
121
|
-
// result.ok === true
|
|
122
|
-
// result.ok === false, result.reason
|
|
121
|
+
// result === undefined -> there is no guard for 'assistant' (unknown type)
|
|
122
|
+
// result.ok === true -> the shape matches
|
|
123
|
+
// result.ok === false, result.reason -> the shape does not match, and why
|
|
123
124
|
```
|
|
124
125
|
|
|
125
|
-
## 📑
|
|
126
|
+
## 📑 License
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
This project is released under the [MIT License](./LICENSE).
|
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let node_fs = require("node:fs");
|
|
3
3
|
let node_readline = require("node:readline");
|
|
4
4
|
// src/result.ts
|
|
5
|
-
/** `OkResult<T
|
|
5
|
+
/** Implementation of `OkResult<T>`. */
|
|
6
6
|
var OkResultImpl = class OkResultImpl {
|
|
7
7
|
value;
|
|
8
8
|
isOk = true;
|
|
@@ -26,7 +26,7 @@ var OkResultImpl = class OkResultImpl {
|
|
|
26
26
|
return this.value;
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
|
-
/** `ErrResult<E
|
|
29
|
+
/** Implementation of `ErrResult<E>`. */
|
|
30
30
|
var ErrResultImpl = class ErrResultImpl {
|
|
31
31
|
error;
|
|
32
32
|
isOk = false;
|
|
@@ -51,23 +51,23 @@ var ErrResultImpl = class ErrResultImpl {
|
|
|
51
51
|
}
|
|
52
52
|
};
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
54
|
+
* Creates a success `Result<T, never>`.
|
|
55
55
|
*
|
|
56
|
-
* @param value -
|
|
56
|
+
* @param value - Success value
|
|
57
57
|
*/
|
|
58
58
|
function ok(value) {
|
|
59
59
|
return new OkResultImpl(value);
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* Creates a failure `Result<never, E>`.
|
|
63
63
|
*
|
|
64
|
-
* @param error -
|
|
64
|
+
* @param error - Error value
|
|
65
65
|
*/
|
|
66
66
|
function err(error) {
|
|
67
67
|
return new ErrResultImpl(error);
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
|
-
* `
|
|
70
|
+
* A `PromiseLike<Result<T, E>>` that can be awaited and chained with `map` / `mapErr` / `andThen`.
|
|
71
71
|
*/
|
|
72
72
|
var ResultAsync = class ResultAsync {
|
|
73
73
|
_promise;
|
|
@@ -78,42 +78,42 @@ var ResultAsync = class ResultAsync {
|
|
|
78
78
|
return this._promise.then(onfulfilled, onrejected);
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
|
-
* `Promise<T>`
|
|
81
|
+
* Wraps a `Promise<T>` in a `ResultAsync<T, E>`. A rejection is mapped to `E` with `onError`.
|
|
82
82
|
*
|
|
83
|
-
* @param promise -
|
|
84
|
-
* @param onError -
|
|
83
|
+
* @param promise - Target promise
|
|
84
|
+
* @param onError - Error mapper
|
|
85
85
|
*/
|
|
86
86
|
static fromPromise(promise, onError) {
|
|
87
87
|
return new ResultAsync(promise.then((v) => ok(v), (error) => err(onError(error))));
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
|
-
*
|
|
90
|
+
* Wraps a resolved `Result<T, E>` in a `ResultAsync<T, E>`.
|
|
91
91
|
*
|
|
92
|
-
* @param result -
|
|
92
|
+
* @param result - Target result
|
|
93
93
|
*/
|
|
94
94
|
static fromResult(result) {
|
|
95
95
|
return new ResultAsync(Promise.resolve(result));
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
|
-
*
|
|
98
|
+
* Transforms the success value.
|
|
99
99
|
*
|
|
100
|
-
* @param fn -
|
|
100
|
+
* @param fn - Synchronous mapper
|
|
101
101
|
*/
|
|
102
102
|
map(fn) {
|
|
103
103
|
return new ResultAsync(this._promise.then((r) => r.map(fn)));
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
|
-
*
|
|
106
|
+
* Transforms the error value.
|
|
107
107
|
*
|
|
108
|
-
* @param fn -
|
|
108
|
+
* @param fn - Synchronous error mapper
|
|
109
109
|
*/
|
|
110
110
|
mapErr(fn) {
|
|
111
111
|
return new ResultAsync(this._promise.then((r) => r.mapErr(fn)));
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
|
-
*
|
|
114
|
+
* Chains a step that may fail.
|
|
115
115
|
*
|
|
116
|
-
* @param fn - `ResultAsync<U, F>`
|
|
116
|
+
* @param fn - Mapper returning a `ResultAsync<U, F>` or `Result<U, F>`
|
|
117
117
|
*/
|
|
118
118
|
andThen(fn) {
|
|
119
119
|
return new ResultAsync(this._promise.then(async (r) => {
|
|
@@ -123,19 +123,19 @@ var ResultAsync = class ResultAsync {
|
|
|
123
123
|
}));
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
|
-
*
|
|
126
|
+
* Pattern-matches on success / failure.
|
|
127
127
|
*
|
|
128
|
-
* @param onOk -
|
|
129
|
-
* @param onErr -
|
|
128
|
+
* @param onOk - Success handler
|
|
129
|
+
* @param onErr - Error handler
|
|
130
130
|
*/
|
|
131
131
|
async match(onOk, onErr) {
|
|
132
132
|
const r = await this._promise;
|
|
133
133
|
return r.isOk ? onOk(r.value) : onErr(r.error);
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
136
|
+
* Returns the value on success, or `fallback` on failure.
|
|
137
137
|
*
|
|
138
|
-
* @param fallback -
|
|
138
|
+
* @param fallback - Fallback value
|
|
139
139
|
*/
|
|
140
140
|
async unwrapOr(fallback) {
|
|
141
141
|
const r = await this._promise;
|
|
@@ -143,28 +143,25 @@ var ResultAsync = class ResultAsync {
|
|
|
143
143
|
}
|
|
144
144
|
};
|
|
145
145
|
// src/generated/guards.ts
|
|
146
|
-
/**
|
|
146
|
+
/** Whether the value is a plain object (neither an array nor null). */
|
|
147
147
|
function isObject(v) {
|
|
148
148
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
149
149
|
}
|
|
150
|
-
/** `string` かどうかを判定する。 */
|
|
151
150
|
function isString(v) {
|
|
152
151
|
return typeof v === "string";
|
|
153
152
|
}
|
|
154
|
-
/** `number` かどうかを判定する。 */
|
|
155
153
|
function isNumber(v) {
|
|
156
154
|
return typeof v === "number";
|
|
157
155
|
}
|
|
158
|
-
/** `boolean` かどうかを判定する。 */
|
|
159
156
|
function isBoolean(v) {
|
|
160
157
|
return typeof v === "boolean";
|
|
161
158
|
}
|
|
162
|
-
/** `[
|
|
159
|
+
/** Builds a diagnostic message listing the failing field names from `[field name, check result]` pairs. Returns undefined when all pass. */
|
|
163
160
|
function fieldCheck(fields) {
|
|
164
161
|
const failing = fields.filter(([, ok]) => !ok).map(([name]) => name);
|
|
165
162
|
return failing.length > 0 ? `missing/invalid field(s): ${failing.join(", ")}` : void 0;
|
|
166
163
|
}
|
|
167
|
-
/**
|
|
164
|
+
/** Checks the required fields common to the full-metadata entries and returns the reason on mismatch (undefined on match). */
|
|
168
165
|
function entryBaseReason(o) {
|
|
169
166
|
return fieldCheck([
|
|
170
167
|
["cwd", isString(o.cwd)],
|
|
@@ -523,7 +520,7 @@ function isResult(v) {
|
|
|
523
520
|
reason
|
|
524
521
|
};
|
|
525
522
|
}
|
|
526
|
-
/** type
|
|
523
|
+
/** Map from type string to guard function. */
|
|
527
524
|
const guards = {
|
|
528
525
|
assistant: isAssistant,
|
|
529
526
|
user: isUser,
|
|
@@ -545,15 +542,15 @@ const guards = {
|
|
|
545
542
|
started: isStarted,
|
|
546
543
|
result: isResult
|
|
547
544
|
};
|
|
548
|
-
/**
|
|
545
|
+
/** Names of the known types. */
|
|
549
546
|
const KNOWN_TYPES = Object.keys(guards);
|
|
550
547
|
/**
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
548
|
+
* Applies the guard of the given type, or returns undefined if no guard exists for it.
|
|
549
|
+
* Returns `{ ok: true }` on a match and `{ ok: false, reason }` on a mismatch, where
|
|
550
|
+
* `reason` names the fields that caused it.
|
|
554
551
|
*
|
|
555
|
-
* @param type -
|
|
556
|
-
* @param v -
|
|
552
|
+
* @param type - Discriminator
|
|
553
|
+
* @param v - Raw JSON to check
|
|
557
554
|
*/
|
|
558
555
|
function guardByType(type, v) {
|
|
559
556
|
const g = guards[type];
|
|
@@ -581,7 +578,7 @@ function isImageBlock(v) {
|
|
|
581
578
|
function isToolReferenceBlock(v) {
|
|
582
579
|
return v.type === "tool_reference" && isString(v.tool_name);
|
|
583
580
|
}
|
|
584
|
-
/**
|
|
581
|
+
/** Normalizes the inner content of a tool_result. */
|
|
585
582
|
function normalizeToolResultContent(content) {
|
|
586
583
|
if (isString(content)) return content;
|
|
587
584
|
if (!Array.isArray(content)) return [unknownBlock(content)];
|
|
@@ -594,7 +591,7 @@ function normalizeToolResultContent(content) {
|
|
|
594
591
|
return unknownBlock(el);
|
|
595
592
|
});
|
|
596
593
|
}
|
|
597
|
-
/** tool_result
|
|
594
|
+
/** Checks and normalizes a whole tool_result block. */
|
|
598
595
|
function normalizeToolResultBlock(v) {
|
|
599
596
|
if (v.type !== "tool_result" || !isString(v.tool_use_id)) return unknownBlock(v);
|
|
600
597
|
const block = {
|
|
@@ -607,10 +604,11 @@ function normalizeToolResultBlock(v) {
|
|
|
607
604
|
return block;
|
|
608
605
|
}
|
|
609
606
|
/**
|
|
610
|
-
*
|
|
611
|
-
*
|
|
607
|
+
* Normalizes the message.content of an assistant entry.
|
|
608
|
+
* Known blocks are kept as is; unknown ones are demoted to `UnknownContentBlock`.
|
|
609
|
+
* A non-array value becomes a single `UnknownContentBlock`.
|
|
612
610
|
*
|
|
613
|
-
* @param content -
|
|
611
|
+
* @param content - Raw content
|
|
614
612
|
*/
|
|
615
613
|
function normalizeAssistantContent(content) {
|
|
616
614
|
if (!Array.isArray(content)) return [unknownBlock(content)];
|
|
@@ -624,9 +622,10 @@ function normalizeAssistantContent(content) {
|
|
|
624
622
|
});
|
|
625
623
|
}
|
|
626
624
|
/**
|
|
627
|
-
*
|
|
625
|
+
* Normalizes the message.content of a user entry. A string is returned as is and an array is normalized per element.
|
|
626
|
+
* Any other value becomes a single `UnknownContentBlock`.
|
|
628
627
|
*
|
|
629
|
-
* @param content -
|
|
628
|
+
* @param content - Raw content (string or array)
|
|
630
629
|
*/
|
|
631
630
|
function normalizeUserContent(content) {
|
|
632
631
|
if (isString(content)) return content;
|
|
@@ -643,15 +642,15 @@ function normalizeUserContent(content) {
|
|
|
643
642
|
}
|
|
644
643
|
// src/parse-line.ts
|
|
645
644
|
/**
|
|
646
|
-
* JSONL
|
|
645
|
+
* Converts one JSONL line into a `ParsedLine`.
|
|
647
646
|
*
|
|
648
|
-
* - JSON
|
|
649
|
-
* -
|
|
650
|
-
* -
|
|
651
|
-
* -
|
|
647
|
+
* - JSON syntax error: `LineParseError`
|
|
648
|
+
* - Known type with a matching shape: `KnownEntry` (`_kind: 'known'` is added and content is normalized)
|
|
649
|
+
* - Known type with a mismatched shape: `UnknownEntry` (`typeHint` holds the type)
|
|
650
|
+
* - Unknown type or no type: `UnknownEntry` (no `typeHint`)
|
|
652
651
|
*
|
|
653
|
-
* @param rawLine -
|
|
654
|
-
* @param lineNumber - 1
|
|
652
|
+
* @param rawLine - One line of text (without the line break)
|
|
653
|
+
* @param lineNumber - 1-based line number
|
|
655
654
|
*/
|
|
656
655
|
function parseLine(rawLine, lineNumber) {
|
|
657
656
|
let parsed;
|
|
@@ -701,10 +700,10 @@ function parseLine(rawLine, lineNumber) {
|
|
|
701
700
|
}
|
|
702
701
|
// src/errors.ts
|
|
703
702
|
/**
|
|
704
|
-
* FileReadError
|
|
703
|
+
* Creates a `FileReadError`.
|
|
705
704
|
*
|
|
706
|
-
* @param path -
|
|
707
|
-
* @param error -
|
|
705
|
+
* @param path - Target path
|
|
706
|
+
* @param error - Caught exception
|
|
708
707
|
*/
|
|
709
708
|
function fileReadError(path, error) {
|
|
710
709
|
const rawCode = typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
|
|
@@ -717,13 +716,12 @@ function fileReadError(path, error) {
|
|
|
717
716
|
}
|
|
718
717
|
// src/parse.ts
|
|
719
718
|
/**
|
|
720
|
-
* JSONL
|
|
721
|
-
* `text.split('\n')
|
|
722
|
-
* 追加メモリは 1 行分にとどまる (全行配列分の追加確保が発生しない)。
|
|
719
|
+
* Synchronous iterator that parses a JSONL string line by line.
|
|
720
|
+
* It slices one line at a time with `indexOf` instead of building a full line array with `text.split('\n')`, so the extra memory stays at one line.
|
|
723
721
|
*
|
|
724
|
-
*
|
|
722
|
+
* Blank lines (empty after trimming) are skipped and not counted in line numbers.
|
|
725
723
|
*
|
|
726
|
-
* @param text - JSONL
|
|
724
|
+
* @param text - Whole JSONL text
|
|
727
725
|
*/
|
|
728
726
|
function* parseJsonlLines(text) {
|
|
729
727
|
let lineNumber = 0;
|
|
@@ -742,19 +740,19 @@ function* parseJsonlLines(text) {
|
|
|
742
740
|
}
|
|
743
741
|
}
|
|
744
742
|
/**
|
|
745
|
-
* JSONL
|
|
746
|
-
*
|
|
743
|
+
* Parses a whole JSONL string and returns the lines as an array.
|
|
744
|
+
* Per-line errors are represented as array elements, so this function never returns `Err`.
|
|
747
745
|
*
|
|
748
|
-
* @param text - JSONL
|
|
746
|
+
* @param text - Whole JSONL text
|
|
749
747
|
*/
|
|
750
748
|
function parseJsonl(text) {
|
|
751
749
|
return ok([...parseJsonlLines(text)]);
|
|
752
750
|
}
|
|
753
751
|
/**
|
|
754
|
-
*
|
|
755
|
-
* I/O
|
|
752
|
+
* Asynchronous iterator that reads and parses a file line by line (constant memory).
|
|
753
|
+
* An I/O error is thrown as a `FileReadError` during iteration.
|
|
756
754
|
*
|
|
757
|
-
* @param path - JSONL
|
|
755
|
+
* @param path - JSONL file path
|
|
758
756
|
*/
|
|
759
757
|
async function* parseJsonlFileStream(path) {
|
|
760
758
|
const stream = (0, node_fs.createReadStream)(path, { encoding: "utf8" });
|
|
@@ -777,9 +775,9 @@ async function* parseJsonlFileStream(path) {
|
|
|
777
775
|
}
|
|
778
776
|
}
|
|
779
777
|
/**
|
|
780
|
-
*
|
|
778
|
+
* Reads a whole file and parses it at once. Only I/O errors become `Err`.
|
|
781
779
|
*
|
|
782
|
-
* @param path - JSONL
|
|
780
|
+
* @param path - JSONL file path
|
|
783
781
|
*/
|
|
784
782
|
function parseJsonlFile(path) {
|
|
785
783
|
return ResultAsync.fromPromise((async () => {
|