claude-code-jsonl-parser 0.1.4-beta.3 → 0.1.4-beta.31
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 +73 -69
- package/dist/index.d.cts +111 -94
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +111 -94
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +73 -69
- 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)],
|
|
@@ -200,7 +197,12 @@ function isAssistant(v) {
|
|
|
200
197
|
const reason = fieldCheck([
|
|
201
198
|
["message.role", isString(v.message.role)],
|
|
202
199
|
["message.id", isString(v.message.id)],
|
|
203
|
-
["message.model", isString(v.message.model)]
|
|
200
|
+
["message.model", isString(v.message.model)],
|
|
201
|
+
["message.type", isString(v.message.type)],
|
|
202
|
+
["message.stop_reason", v.message.stop_reason === null || isString(v.message.stop_reason)],
|
|
203
|
+
["message.stop_sequence", v.message.stop_sequence === null || isString(v.message.stop_sequence)],
|
|
204
|
+
["message.stop_details", "stop_details" in v.message],
|
|
205
|
+
["message.usage", "usage" in v.message]
|
|
204
206
|
]);
|
|
205
207
|
return reason === void 0 ? { ok: true } : {
|
|
206
208
|
ok: false,
|
|
@@ -523,7 +525,7 @@ function isResult(v) {
|
|
|
523
525
|
reason
|
|
524
526
|
};
|
|
525
527
|
}
|
|
526
|
-
/** type
|
|
528
|
+
/** Map from type string to guard function. */
|
|
527
529
|
const guards = {
|
|
528
530
|
assistant: isAssistant,
|
|
529
531
|
user: isUser,
|
|
@@ -545,17 +547,18 @@ const guards = {
|
|
|
545
547
|
started: isStarted,
|
|
546
548
|
result: isResult
|
|
547
549
|
};
|
|
548
|
-
/**
|
|
550
|
+
/** Names of the known types. */
|
|
549
551
|
const KNOWN_TYPES = Object.keys(guards);
|
|
550
552
|
/**
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
553
|
+
* Applies the guard of the given type, or returns undefined if no guard exists for it.
|
|
554
|
+
* Returns `{ ok: true }` on a match and `{ ok: false, reason }` on a mismatch, where
|
|
555
|
+
* `reason` names the fields that caused it.
|
|
554
556
|
*
|
|
555
|
-
* @param type -
|
|
556
|
-
* @param v -
|
|
557
|
+
* @param type - Discriminator
|
|
558
|
+
* @param v - Raw JSON to check
|
|
557
559
|
*/
|
|
558
560
|
function guardByType(type, v) {
|
|
561
|
+
if (!Object.hasOwn(guards, type)) return void 0;
|
|
559
562
|
const g = guards[type];
|
|
560
563
|
if (g === void 0) return void 0;
|
|
561
564
|
return g(v);
|
|
@@ -576,12 +579,12 @@ function isToolUseBlock(v) {
|
|
|
576
579
|
return v.type === "tool_use" && isString(v.id) && isString(v.name) && "input" in v;
|
|
577
580
|
}
|
|
578
581
|
function isImageBlock(v) {
|
|
579
|
-
return v.type === "image" && isObject(v.source) && isString(v.source.media_type) && isString(v.source.data);
|
|
582
|
+
return v.type === "image" && isObject(v.source) && isString(v.source.type) && isString(v.source.media_type) && isString(v.source.data);
|
|
580
583
|
}
|
|
581
584
|
function isToolReferenceBlock(v) {
|
|
582
585
|
return v.type === "tool_reference" && isString(v.tool_name);
|
|
583
586
|
}
|
|
584
|
-
/**
|
|
587
|
+
/** Normalizes the inner content of a tool_result. */
|
|
585
588
|
function normalizeToolResultContent(content) {
|
|
586
589
|
if (isString(content)) return content;
|
|
587
590
|
if (!Array.isArray(content)) return [unknownBlock(content)];
|
|
@@ -594,7 +597,7 @@ function normalizeToolResultContent(content) {
|
|
|
594
597
|
return unknownBlock(el);
|
|
595
598
|
});
|
|
596
599
|
}
|
|
597
|
-
/** tool_result
|
|
600
|
+
/** Checks and normalizes a whole tool_result block. */
|
|
598
601
|
function normalizeToolResultBlock(v) {
|
|
599
602
|
if (v.type !== "tool_result" || !isString(v.tool_use_id)) return unknownBlock(v);
|
|
600
603
|
const block = {
|
|
@@ -607,10 +610,11 @@ function normalizeToolResultBlock(v) {
|
|
|
607
610
|
return block;
|
|
608
611
|
}
|
|
609
612
|
/**
|
|
610
|
-
*
|
|
611
|
-
*
|
|
613
|
+
* Normalizes the message.content of an assistant entry.
|
|
614
|
+
* Known blocks are kept as is; unknown ones are demoted to `UnknownContentBlock`.
|
|
615
|
+
* A non-array value becomes a single `UnknownContentBlock`.
|
|
612
616
|
*
|
|
613
|
-
* @param content -
|
|
617
|
+
* @param content - Raw content
|
|
614
618
|
*/
|
|
615
619
|
function normalizeAssistantContent(content) {
|
|
616
620
|
if (!Array.isArray(content)) return [unknownBlock(content)];
|
|
@@ -624,9 +628,10 @@ function normalizeAssistantContent(content) {
|
|
|
624
628
|
});
|
|
625
629
|
}
|
|
626
630
|
/**
|
|
627
|
-
*
|
|
631
|
+
* Normalizes the message.content of a user entry. A string is returned as is and an array is normalized per element.
|
|
632
|
+
* Any other value becomes a single `UnknownContentBlock`.
|
|
628
633
|
*
|
|
629
|
-
* @param content -
|
|
634
|
+
* @param content - Raw content (string or array)
|
|
630
635
|
*/
|
|
631
636
|
function normalizeUserContent(content) {
|
|
632
637
|
if (isString(content)) return content;
|
|
@@ -643,15 +648,15 @@ function normalizeUserContent(content) {
|
|
|
643
648
|
}
|
|
644
649
|
// src/parse-line.ts
|
|
645
650
|
/**
|
|
646
|
-
* JSONL
|
|
651
|
+
* Converts one JSONL line into a `ParsedLine`.
|
|
647
652
|
*
|
|
648
|
-
* - JSON
|
|
649
|
-
* -
|
|
650
|
-
* -
|
|
651
|
-
* -
|
|
653
|
+
* - JSON syntax error: `LineParseError`
|
|
654
|
+
* - Known type with a matching shape: `KnownEntry` (`_kind: 'known'` is added and content is normalized)
|
|
655
|
+
* - Known type with a mismatched shape: `UnknownEntry` (`typeHint` holds the type)
|
|
656
|
+
* - Unknown type or no type: `UnknownEntry` (no `typeHint`)
|
|
652
657
|
*
|
|
653
|
-
* @param rawLine -
|
|
654
|
-
* @param lineNumber - 1
|
|
658
|
+
* @param rawLine - One line of text (without the line break)
|
|
659
|
+
* @param lineNumber - 1-based line number
|
|
655
660
|
*/
|
|
656
661
|
function parseLine(rawLine, lineNumber) {
|
|
657
662
|
let parsed;
|
|
@@ -701,10 +706,10 @@ function parseLine(rawLine, lineNumber) {
|
|
|
701
706
|
}
|
|
702
707
|
// src/errors.ts
|
|
703
708
|
/**
|
|
704
|
-
* FileReadError
|
|
709
|
+
* Creates a `FileReadError`.
|
|
705
710
|
*
|
|
706
|
-
* @param path -
|
|
707
|
-
* @param error -
|
|
711
|
+
* @param path - Target path
|
|
712
|
+
* @param error - Caught exception
|
|
708
713
|
*/
|
|
709
714
|
function fileReadError(path, error) {
|
|
710
715
|
const rawCode = typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
|
|
@@ -717,13 +722,12 @@ function fileReadError(path, error) {
|
|
|
717
722
|
}
|
|
718
723
|
// src/parse.ts
|
|
719
724
|
/**
|
|
720
|
-
* JSONL
|
|
721
|
-
* `text.split('\n')
|
|
722
|
-
* 追加メモリは 1 行分にとどまる (全行配列分の追加確保が発生しない)。
|
|
725
|
+
* Synchronous iterator that parses a JSONL string line by line.
|
|
726
|
+
* 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
727
|
*
|
|
724
|
-
*
|
|
728
|
+
* Blank lines (empty after trimming) are skipped and not counted in line numbers.
|
|
725
729
|
*
|
|
726
|
-
* @param text - JSONL
|
|
730
|
+
* @param text - Whole JSONL text
|
|
727
731
|
*/
|
|
728
732
|
function* parseJsonlLines(text) {
|
|
729
733
|
let lineNumber = 0;
|
|
@@ -742,25 +746,25 @@ function* parseJsonlLines(text) {
|
|
|
742
746
|
}
|
|
743
747
|
}
|
|
744
748
|
/**
|
|
745
|
-
* JSONL
|
|
746
|
-
*
|
|
749
|
+
* Parses a whole JSONL string and returns the lines as an array.
|
|
750
|
+
* Per-line errors are represented as array elements, so this function never returns `Err`.
|
|
747
751
|
*
|
|
748
|
-
* @param text - JSONL
|
|
752
|
+
* @param text - Whole JSONL text
|
|
749
753
|
*/
|
|
750
754
|
function parseJsonl(text) {
|
|
751
755
|
return ok([...parseJsonlLines(text)]);
|
|
752
756
|
}
|
|
753
757
|
/**
|
|
754
|
-
*
|
|
755
|
-
* I/O
|
|
758
|
+
* Asynchronous iterator that reads and parses a file line by line (constant memory).
|
|
759
|
+
* An I/O error is thrown as a `FileReadError` during iteration.
|
|
756
760
|
*
|
|
757
|
-
* @param path - JSONL
|
|
761
|
+
* @param path - JSONL file path
|
|
758
762
|
*/
|
|
759
763
|
async function* parseJsonlFileStream(path) {
|
|
760
764
|
const stream = (0, node_fs.createReadStream)(path, { encoding: "utf8" });
|
|
761
765
|
const rl = (0, node_readline.createInterface)({
|
|
762
766
|
input: stream,
|
|
763
|
-
crlfDelay:
|
|
767
|
+
crlfDelay: 1e400
|
|
764
768
|
});
|
|
765
769
|
let lineNumber = 0;
|
|
766
770
|
try {
|
|
@@ -777,9 +781,9 @@ async function* parseJsonlFileStream(path) {
|
|
|
777
781
|
}
|
|
778
782
|
}
|
|
779
783
|
/**
|
|
780
|
-
*
|
|
784
|
+
* Reads a whole file and parses it at once. Only I/O errors become `Err`.
|
|
781
785
|
*
|
|
782
|
-
* @param path - JSONL
|
|
786
|
+
* @param path - JSONL file path
|
|
783
787
|
*/
|
|
784
788
|
function parseJsonlFile(path) {
|
|
785
789
|
return ResultAsync.fromPromise((async () => {
|