ycodium-acp-executor 0.2.5542 → 0.2.5751
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 +5 -4
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.d.ts +47 -0
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.js +172 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.d.ts +63 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.js +220 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.d.ts +41 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.js +197 -0
- package/dist/_private/ycodium-jsonl-stream/history.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/history.js +389 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.js +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.d.ts +19 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.js +117 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.d.ts +31 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.js +144 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.d.ts +8 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.js +142 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.js +18 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.d.ts +79 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.js +603 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.d.ts +69 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.js +106 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.js +32 -0
- package/dist/_private/ycodium-jsonl-stream/sessionTranscript.d.ts +3 -1
- package/dist/_private/ycodium-jsonl-stream/sessionTranscript.js +13 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
- package/dist/adapter-support.d.ts.map +1 -1
- package/dist/adapter-support.js.map +1 -1
- package/dist/apply-effort.d.ts.map +1 -1
- package/dist/apply-effort.js.map +1 -1
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js.map +1 -1
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js.map +1 -1
- package/dist/card-defaults.d.ts.map +1 -1
- package/dist/card-defaults.js.map +1 -1
- package/dist/config-option-wiring.d.ts.map +1 -1
- package/dist/config-option-wiring.js.map +1 -1
- package/dist/config-options.d.ts.map +1 -1
- package/dist/config-options.js.map +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/event-stream.d.ts.map +1 -1
- package/dist/event-stream.js.map +1 -1
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/executor/child-sessions.d.ts.map +1 -1
- package/dist/executor/child-sessions.js.map +1 -1
- package/dist/executor/lifecycle.d.ts.map +1 -1
- package/dist/executor/lifecycle.js.map +1 -1
- package/dist/executor/prompt-results.d.ts.map +1 -1
- package/dist/executor/prompt-results.js.map +1 -1
- package/dist/executor/prompt.d.ts.map +1 -1
- package/dist/executor/prompt.js.map +1 -1
- package/dist/executor/requests.d.ts.map +1 -1
- package/dist/executor/requests.js.map +1 -1
- package/dist/executor/runtime.d.ts.map +1 -1
- package/dist/executor/runtime.js.map +1 -1
- package/dist/executor/session.d.ts.map +1 -1
- package/dist/executor/session.js +17 -1
- package/dist/executor/session.js.map +1 -1
- package/dist/executor/settlement.d.ts.map +1 -1
- package/dist/executor/settlement.js.map +1 -1
- package/dist/executor/subagent-sessions.d.ts.map +1 -1
- package/dist/executor/subagent-sessions.js.map +1 -1
- package/dist/executor/support.d.ts.map +1 -1
- package/dist/executor/support.js.map +1 -1
- package/dist/executor/updates.d.ts.map +1 -1
- package/dist/executor/updates.js +11 -0
- package/dist/executor/updates.js.map +1 -1
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js.map +1 -1
- package/dist/failureCategory.d.ts.map +1 -1
- package/dist/failureCategory.js.map +1 -1
- package/dist/handshake.d.ts.map +1 -1
- package/dist/handshake.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/instance-kit.d.ts.map +1 -1
- package/dist/instance-kit.js.map +1 -1
- package/dist/jsonrpc.d.ts.map +1 -1
- package/dist/jsonrpc.js.map +1 -1
- package/dist/line-buffer.d.ts.map +1 -1
- package/dist/line-buffer.js.map +1 -1
- package/dist/mcp-servers.d.ts +26 -4
- package/dist/mcp-servers.d.ts.map +1 -1
- package/dist/mcp-servers.js +25 -4
- package/dist/mcp-servers.js.map +1 -1
- package/dist/model-selection.d.ts.map +1 -1
- package/dist/model-selection.js.map +1 -1
- package/dist/model-send.d.ts.map +1 -1
- package/dist/model-send.js.map +1 -1
- package/dist/mutex.d.ts.map +1 -1
- package/dist/mutex.js.map +1 -1
- package/dist/native-session-transcript.d.ts.map +1 -1
- package/dist/native-session-transcript.js.map +1 -1
- package/dist/native-sessions.d.ts.map +1 -1
- package/dist/native-sessions.js.map +1 -1
- package/dist/openCommands.d.ts.map +1 -1
- package/dist/openCommands.js.map +1 -1
- package/dist/permission-statement.d.ts.map +1 -1
- package/dist/permission-statement.js.map +1 -1
- package/dist/probe-process.d.ts +9 -5
- package/dist/probe-process.d.ts.map +1 -1
- package/dist/probe-process.js +4 -0
- package/dist/probe-process.js.map +1 -1
- package/dist/probe-session.d.ts.map +1 -1
- package/dist/probe-session.js.map +1 -1
- package/dist/probe.d.ts.map +1 -1
- package/dist/probe.js.map +1 -1
- package/dist/prompt-dispatch.d.ts.map +1 -1
- package/dist/prompt-dispatch.js.map +1 -1
- package/dist/protocol-log.d.ts.map +1 -1
- package/dist/protocol-log.js.map +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/refreshDeadline.d.ts.map +1 -1
- package/dist/refreshDeadline.js.map +1 -1
- package/dist/runtime-mode.d.ts.map +1 -1
- package/dist/runtime-mode.js.map +1 -1
- package/dist/runtime-model.d.ts +4 -0
- package/dist/runtime-model.d.ts.map +1 -1
- package/dist/runtime-model.js +13 -0
- package/dist/runtime-model.js.map +1 -1
- package/dist/session-cleanup.d.ts.map +1 -1
- package/dist/session-cleanup.js.map +1 -1
- package/dist/session-registry.d.ts.map +1 -1
- package/dist/session-registry.js.map +1 -1
- package/dist/session-update.d.ts.map +1 -1
- package/dist/session-update.js.map +1 -1
- package/dist/sessionProcess.d.ts.map +1 -1
- package/dist/sessionProcess.js.map +1 -1
- package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
- package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
- package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
- package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
- package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
- package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
- package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
- package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
- package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
- package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
- package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
- package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
- package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
- package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
- package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
- package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
- package/dist/source/adapter-support.ts +115 -0
- package/dist/source/apply-effort.ts +66 -0
- package/dist/source/approvals.ts +94 -0
- package/dist/source/artifacts.ts +88 -0
- package/dist/source/card-defaults.ts +25 -0
- package/dist/source/config-option-wiring.ts +119 -0
- package/dist/source/config-options.ts +270 -0
- package/dist/source/connection.ts +553 -0
- package/dist/source/errors.ts +207 -0
- package/dist/source/event-stream.ts +91 -0
- package/dist/source/events.ts +341 -0
- package/dist/source/executor/child-sessions.ts +87 -0
- package/dist/source/executor/lifecycle.ts +282 -0
- package/dist/source/executor/prompt-results.ts +123 -0
- package/dist/source/executor/prompt.ts +606 -0
- package/dist/source/executor/requests.ts +323 -0
- package/dist/source/executor/runtime.ts +137 -0
- package/dist/source/executor/session.ts +553 -0
- package/dist/source/executor/settlement.ts +92 -0
- package/dist/source/executor/subagent-sessions.ts +183 -0
- package/dist/source/executor/support.ts +129 -0
- package/dist/source/executor/updates.ts +427 -0
- package/dist/source/executor.ts +537 -0
- package/dist/source/failureCategory.ts +74 -0
- package/dist/source/handshake.ts +66 -0
- package/dist/source/index.ts +210 -0
- package/dist/source/instance-kit.ts +478 -0
- package/dist/source/jsonrpc.ts +59 -0
- package/dist/source/line-buffer.ts +54 -0
- package/dist/source/mcp-servers.ts +83 -0
- package/dist/source/model-selection.ts +138 -0
- package/dist/source/model-send.ts +80 -0
- package/dist/source/mutex.ts +47 -0
- package/dist/source/native-session-transcript.ts +167 -0
- package/dist/source/native-sessions.ts +271 -0
- package/dist/source/openCommands.ts +39 -0
- package/dist/source/permission-statement.ts +49 -0
- package/dist/source/probe-process.ts +95 -0
- package/dist/source/probe-session.ts +81 -0
- package/dist/source/probe.ts +261 -0
- package/dist/source/prompt-dispatch.ts +42 -0
- package/dist/source/protocol-log.ts +143 -0
- package/dist/source/protocol.ts +36 -0
- package/dist/source/refreshDeadline.ts +2 -0
- package/dist/source/runtime-mode.ts +125 -0
- package/dist/source/runtime-model.ts +598 -0
- package/dist/source/session-cleanup.ts +82 -0
- package/dist/source/session-registry.ts +62 -0
- package/dist/source/session-update.ts +88 -0
- package/dist/source/sessionProcess.ts +29 -0
- package/dist/source/start-abort.ts +19 -0
- package/dist/source/thread-operations.ts +145 -0
- package/dist/source/tool-call-state.ts +322 -0
- package/dist/source/tool-presentation.ts +20 -0
- package/dist/source/tool-subagents.ts +491 -0
- package/dist/source/turn-usage.ts +78 -0
- package/dist/source/types.ts +146 -0
- package/dist/source/user-input-pending.ts +31 -0
- package/dist/source/util.ts +38 -0
- package/dist/source/vendor-hooks.ts +394 -0
- package/dist/start-abort.d.ts.map +1 -1
- package/dist/start-abort.js.map +1 -1
- package/dist/thread-operations.d.ts.map +1 -1
- package/dist/thread-operations.js.map +1 -1
- package/dist/tool-call-state.d.ts.map +1 -1
- package/dist/tool-call-state.js.map +1 -1
- package/dist/tool-presentation.d.ts.map +1 -1
- package/dist/tool-presentation.js.map +1 -1
- package/dist/tool-subagents.d.ts +1 -0
- package/dist/tool-subagents.d.ts.map +1 -1
- package/dist/tool-subagents.js +2 -0
- package/dist/tool-subagents.js.map +1 -1
- package/dist/turn-usage.d.ts.map +1 -1
- package/dist/turn-usage.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/user-input-pending.d.ts.map +1 -1
- package/dist/user-input-pending.js.map +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js.map +1 -1
- package/dist/vendor-hooks.d.ts +11 -3
- package/dist/vendor-hooks.d.ts.map +1 -1
- package/dist/vendor-hooks.js.map +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
// @effect-diagnostics globalDate:off nodeBuiltinImport:off - 纯 TS/Promise 工具,被官方插件打进各自的 bundle,不碰 Effect 运行时。
|
|
2
|
+
/**
|
|
3
|
+
* 转录 jsonl 的逐行读取,外加超长行的流式字段筛选。
|
|
4
|
+
*
|
|
5
|
+
* 转录里偶尔有一行大到几十 MB(工具输出、整文件写入的 input)。`ctx.fs.readLines`
|
|
6
|
+
* 会把整行读成一个字符串,这样的行既占内存又只能被丢掉。这里改走字节流:
|
|
7
|
+
* - 不超过 `maxLineChars` 的行照旧交回完整字符串,由调用方 `JSON.parse`,行为不变;
|
|
8
|
+
* - 超过的行不再整行落成字符串,而是边读边按字段清单解析,只把清单里的字段装配
|
|
9
|
+
* 成对象,其余值读过即弃;清单内的长字符串只留头尾各 `maxChars` 个字符。
|
|
10
|
+
*
|
|
11
|
+
* 字段清单是调用方真正读取的字段,不是对记录结构的另一份描述;清单外的字段在
|
|
12
|
+
* 超长行里不存在。
|
|
13
|
+
*
|
|
14
|
+
* 官方插件(session-import、agent-claude)共用,各自用 esbuild 打进自己的 bundle:插件之间
|
|
15
|
+
* 不能互相 import,所以这份读取器放在独立的工作区包里。
|
|
16
|
+
*
|
|
17
|
+
* @module ycodium-jsonl-stream
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** 读字节流的来源:宿主的 `ctx.fs` 和 node:fs 都满足这个形状。 */
|
|
21
|
+
export interface JsonlSource {
|
|
22
|
+
readonly createReadStream: (path: string) => AsyncIterable<Uint8Array>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 超长行里要留下的一个字段。 */
|
|
26
|
+
export interface RetainedField {
|
|
27
|
+
/** 从记录根开始的键路径;`"*"` 匹配任意对象键或数组下标。 */
|
|
28
|
+
readonly path: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* 路径本身只在值是标量时才留;值是对象或数组时只往下走,由更长的清单项挑子字段。
|
|
31
|
+
* 用于 `message.content` 这种既可能是字符串、又可能是块数组的字段。
|
|
32
|
+
*/
|
|
33
|
+
readonly scalarOnly?: boolean;
|
|
34
|
+
/** 字符串留头尾各这么多字符,中间换成省略标记。不给就整串保留。 */
|
|
35
|
+
readonly maxChars?: number;
|
|
36
|
+
/** 解码后的字符串片段;提供时不保留该字符串,调用者自行限流或分窗。 */
|
|
37
|
+
readonly onChunk?: (text: string) => void;
|
|
38
|
+
/** 完整字符串的 UTF-16 长度;只在合法闭引号后调用。 */
|
|
39
|
+
readonly onEnd?: (total: number) => void;
|
|
40
|
+
/** 非字符串标量;提供时不保留该值。 */
|
|
41
|
+
readonly onScalar?: (value: boolean | number | null) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type JsonlEntry =
|
|
45
|
+
/** 未超长的一行,原文。 */
|
|
46
|
+
| { readonly line: string }
|
|
47
|
+
/** 超长的一行,按字段清单装配出的记录;写到一半、不是合法 JSON 时为 `undefined`。 */
|
|
48
|
+
| { readonly oversized: unknown };
|
|
49
|
+
|
|
50
|
+
export interface ReadJsonlOptions {
|
|
51
|
+
/** 超过这个字符数的行走流式筛选。 */
|
|
52
|
+
readonly maxLineChars: number;
|
|
53
|
+
readonly fields: readonly RetainedField[];
|
|
54
|
+
/** 单条超长记录里留下的字符总数上限;超了整条丢弃。默认 8M。 */
|
|
55
|
+
readonly maxRetainedChars?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const DEFAULT_MAX_RETAINED_CHARS = 8_000_000;
|
|
59
|
+
const MAX_DEPTH = 128;
|
|
60
|
+
/** 键长超过它就不再逐字符保留:清单里没有这么长的键。 */
|
|
61
|
+
const MAX_KEY_CHARS = 256;
|
|
62
|
+
const MAX_BARE_CHARS = 64;
|
|
63
|
+
|
|
64
|
+
const NUMBER_PATTERN = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u;
|
|
65
|
+
|
|
66
|
+
type Mode = "skip" | "descend" | "keep";
|
|
67
|
+
|
|
68
|
+
interface Container {
|
|
69
|
+
readonly isArray: boolean;
|
|
70
|
+
readonly mode: Mode;
|
|
71
|
+
/** `mode === "descend"` 时用于给子值定位;其余模式不用。 */
|
|
72
|
+
readonly path: readonly string[];
|
|
73
|
+
readonly cap: number | undefined;
|
|
74
|
+
readonly target: Record<string, unknown> | unknown[] | null;
|
|
75
|
+
key: string | null;
|
|
76
|
+
/** 当前键过长,对应的值整个跳过。 */
|
|
77
|
+
skipValue: boolean;
|
|
78
|
+
/** 已读完的成员数(跳过的也算)。 */
|
|
79
|
+
index: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 一条 JSON 记录的增量解析器:一次一个字符块,行尾 `finish` 取结果。
|
|
84
|
+
* 任何不合法的输入都只把这一条记录标成作废,不抛。
|
|
85
|
+
*/
|
|
86
|
+
export class OversizedRecordParser {
|
|
87
|
+
readonly #fields: readonly RetainedField[];
|
|
88
|
+
readonly #budget: number;
|
|
89
|
+
readonly #options: { readonly pruneEmpty?: boolean } | undefined;
|
|
90
|
+
#retained = 0;
|
|
91
|
+
#malformed = false;
|
|
92
|
+
#done = false;
|
|
93
|
+
#result: unknown = undefined;
|
|
94
|
+
readonly #stack: Container[] = [];
|
|
95
|
+
|
|
96
|
+
// 词法状态。
|
|
97
|
+
#inString = false;
|
|
98
|
+
#stringIsKey = false;
|
|
99
|
+
#escape = false;
|
|
100
|
+
#unicodeLeft = 0;
|
|
101
|
+
#unicodeValue = 0;
|
|
102
|
+
#bare = "";
|
|
103
|
+
// 当前字符串的保留规则与累积。
|
|
104
|
+
#strObserver: RetainedField | undefined;
|
|
105
|
+
#strRetain = false;
|
|
106
|
+
#strCap: number | undefined = undefined;
|
|
107
|
+
#strHead = "";
|
|
108
|
+
#strTail = "";
|
|
109
|
+
#strLength = 0;
|
|
110
|
+
#key = "";
|
|
111
|
+
#keyOverflow = false;
|
|
112
|
+
// 值之间的位置。
|
|
113
|
+
#expectKey = false;
|
|
114
|
+
#expectColon = false;
|
|
115
|
+
#expectComma = false;
|
|
116
|
+
#expectValue = true;
|
|
117
|
+
|
|
118
|
+
constructor(
|
|
119
|
+
fields: readonly RetainedField[],
|
|
120
|
+
maxRetainedChars = DEFAULT_MAX_RETAINED_CHARS,
|
|
121
|
+
options?: { readonly pruneEmpty?: boolean },
|
|
122
|
+
) {
|
|
123
|
+
this.#fields = fields;
|
|
124
|
+
this.#budget = maxRetainedChars;
|
|
125
|
+
this.#options = options;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
write(chunk: string): void {
|
|
129
|
+
if (this.#malformed) return;
|
|
130
|
+
try {
|
|
131
|
+
this.#consume(chunk);
|
|
132
|
+
} catch (cause) {
|
|
133
|
+
if (cause instanceof MalformedRecord) {
|
|
134
|
+
this.#malformed = true;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
throw cause;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 完整且合法才返回记录;其余(含空行)返回 `undefined`。 */
|
|
142
|
+
finish(): unknown {
|
|
143
|
+
if (this.#malformed) return undefined;
|
|
144
|
+
try {
|
|
145
|
+
if (this.#bare.length > 0) this.#finishBare();
|
|
146
|
+
} catch (cause) {
|
|
147
|
+
if (cause instanceof MalformedRecord) return undefined;
|
|
148
|
+
throw cause;
|
|
149
|
+
}
|
|
150
|
+
if (!this.#done || this.#inString || this.#result === SKIPPED) return undefined;
|
|
151
|
+
return this.#result;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#fail(): never {
|
|
155
|
+
throw new MalformedRecord();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#charge(chars: number): void {
|
|
159
|
+
this.#retained += chars;
|
|
160
|
+
if (this.#retained > this.#budget) this.#fail();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#consume(chunk: string): void {
|
|
164
|
+
const n = chunk.length;
|
|
165
|
+
let i = 0;
|
|
166
|
+
while (i < n) {
|
|
167
|
+
if (this.#inString) {
|
|
168
|
+
i = this.#consumeString(chunk, i);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const c = chunk.charCodeAt(i);
|
|
172
|
+
if (this.#bare.length > 0) {
|
|
173
|
+
// 数字与 true/false/null 的字符;遇到分隔符就结算。
|
|
174
|
+
if (isBareChar(c)) {
|
|
175
|
+
if (this.#bare.length >= MAX_BARE_CHARS) this.#fail();
|
|
176
|
+
this.#bare += chunk[i];
|
|
177
|
+
i += 1;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
this.#finishBare();
|
|
181
|
+
}
|
|
182
|
+
i += 1;
|
|
183
|
+
// 空白。
|
|
184
|
+
if (c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d) continue;
|
|
185
|
+
if (this.#done) this.#fail();
|
|
186
|
+
switch (c) {
|
|
187
|
+
case 0x7b: // {
|
|
188
|
+
this.#requireValue();
|
|
189
|
+
this.#openContainer(false);
|
|
190
|
+
break;
|
|
191
|
+
case 0x5b: // [
|
|
192
|
+
this.#requireValue();
|
|
193
|
+
this.#openContainer(true);
|
|
194
|
+
break;
|
|
195
|
+
case 0x7d: // }
|
|
196
|
+
this.#closeContainer(false);
|
|
197
|
+
break;
|
|
198
|
+
case 0x5d: // ]
|
|
199
|
+
this.#closeContainer(true);
|
|
200
|
+
break;
|
|
201
|
+
case 0x2c: // ,
|
|
202
|
+
this.#comma();
|
|
203
|
+
break;
|
|
204
|
+
case 0x3a: // :
|
|
205
|
+
if (!this.#expectColon) this.#fail();
|
|
206
|
+
this.#expectColon = false;
|
|
207
|
+
this.#expectValue = true;
|
|
208
|
+
break;
|
|
209
|
+
case 0x22: // "
|
|
210
|
+
this.#openString();
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
if (isBareChar(c)) {
|
|
214
|
+
this.#requireValue();
|
|
215
|
+
this.#bare = chunk[i - 1] ?? "";
|
|
216
|
+
} else this.#fail();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
#top(): Container | undefined {
|
|
222
|
+
return this.#stack[this.#stack.length - 1];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#requireValue(): void {
|
|
226
|
+
if (!this.#expectValue) this.#fail();
|
|
227
|
+
this.#expectValue = false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** 当前要读的值(刚要开始)落在什么模式;`isContainer` 决定 scalarOnly 清单项是否适用。 */
|
|
231
|
+
#childMode(isContainer: boolean): { mode: Mode; cap: number | undefined; path: string[] } {
|
|
232
|
+
const parent = this.#top();
|
|
233
|
+
if (parent === undefined) return this.#classify([], isContainer);
|
|
234
|
+
if (parent.mode === "skip") return { mode: "skip", cap: undefined, path: [] };
|
|
235
|
+
if (parent.mode === "keep") return { mode: "keep", cap: parent.cap, path: [] };
|
|
236
|
+
const segment = parent.isArray ? String(parent.index) : (parent.key ?? "");
|
|
237
|
+
if (parent.skipValue) return { mode: "skip", cap: undefined, path: [] };
|
|
238
|
+
return this.#classify([...parent.path, segment], isContainer);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#classify(
|
|
242
|
+
path: string[],
|
|
243
|
+
isContainer: boolean,
|
|
244
|
+
): { mode: Mode; cap: number | undefined; path: string[] } {
|
|
245
|
+
let descend = false;
|
|
246
|
+
for (const field of this.#fields) {
|
|
247
|
+
const fieldPath = field.path;
|
|
248
|
+
if (fieldPath.length <= path.length) {
|
|
249
|
+
if (field.scalarOnly === true && (isContainer || fieldPath.length !== path.length)) {
|
|
250
|
+
// scalarOnly 项只管它自己那个位置,且只管标量。
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (matchesPrefix(fieldPath, path, fieldPath.length)) {
|
|
254
|
+
return { mode: "keep", cap: field.maxChars, path };
|
|
255
|
+
}
|
|
256
|
+
} else if (matchesPrefix(fieldPath, path, path.length)) {
|
|
257
|
+
descend = true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return { mode: descend ? "descend" : "skip", cap: undefined, path };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#openContainer(isArray: boolean): void {
|
|
264
|
+
if (this.#stack.length >= MAX_DEPTH) this.#fail();
|
|
265
|
+
const { mode, cap, path } = this.#childMode(true);
|
|
266
|
+
const keeping = mode !== "skip";
|
|
267
|
+
let target: Record<string, unknown> | unknown[] | null = null;
|
|
268
|
+
if (keeping) {
|
|
269
|
+
this.#charge(16);
|
|
270
|
+
target = isArray ? [] : {};
|
|
271
|
+
}
|
|
272
|
+
this.#stack.push({ isArray, mode, path, cap, target, key: null, skipValue: false, index: 0 });
|
|
273
|
+
this.#expectValue = isArray;
|
|
274
|
+
this.#expectKey = !isArray;
|
|
275
|
+
this.#expectComma = false;
|
|
276
|
+
this.#expectColon = false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
#closeContainer(isArray: boolean): void {
|
|
280
|
+
const top = this.#top();
|
|
281
|
+
if (top === undefined || top.isArray !== isArray) this.#fail();
|
|
282
|
+
// 刚开就关的空容器合法;逗号之后紧接关括号、或键后没有值都不合法。
|
|
283
|
+
if (!this.#expectComma && top.index > 0) this.#fail();
|
|
284
|
+
if (this.#expectColon || (!isArray && this.#expectValue)) this.#fail();
|
|
285
|
+
this.#stack.pop();
|
|
286
|
+
this.#expectValue = false;
|
|
287
|
+
this.#expectKey = false;
|
|
288
|
+
this.#expectColon = false;
|
|
289
|
+
this.#expectComma = true;
|
|
290
|
+
const empty = top.target !== null && Object.keys(top.target).length === 0;
|
|
291
|
+
this.#store(
|
|
292
|
+
this.#options?.pruneEmpty && top.mode === "descend" && empty && this.#stack.length > 0
|
|
293
|
+
? SKIPPED
|
|
294
|
+
: (top.target ?? SKIPPED),
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
#comma(): void {
|
|
299
|
+
const top = this.#top();
|
|
300
|
+
if (top === undefined || !this.#expectComma) this.#fail();
|
|
301
|
+
this.#expectComma = false;
|
|
302
|
+
if (top.isArray) {
|
|
303
|
+
this.#expectValue = true;
|
|
304
|
+
} else {
|
|
305
|
+
this.#expectKey = true;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** 一个值读完:交给外层容器,或成为整条记录。 */
|
|
310
|
+
#store(value: unknown): void {
|
|
311
|
+
const parent = this.#top();
|
|
312
|
+
if (parent === undefined) {
|
|
313
|
+
this.#result = value;
|
|
314
|
+
this.#done = true;
|
|
315
|
+
this.#expectComma = false;
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
this.#expectComma = true;
|
|
319
|
+
if (parent.isArray) {
|
|
320
|
+
parent.index += 1;
|
|
321
|
+
if (parent.target !== null && value !== SKIPPED) {
|
|
322
|
+
(parent.target as unknown[]).push(value);
|
|
323
|
+
this.#charge(8);
|
|
324
|
+
}
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
parent.index += 1;
|
|
328
|
+
const key = parent.key;
|
|
329
|
+
parent.key = null;
|
|
330
|
+
parent.skipValue = false;
|
|
331
|
+
if (parent.target !== null && value !== SKIPPED && key !== null) {
|
|
332
|
+
// defineProperty 才能像 JSON.parse 一样把 "__proto__" 当普通自有键。
|
|
333
|
+
Object.defineProperty(parent.target, key, {
|
|
334
|
+
value,
|
|
335
|
+
enumerable: true,
|
|
336
|
+
writable: true,
|
|
337
|
+
configurable: true,
|
|
338
|
+
});
|
|
339
|
+
this.#charge(key.length + 8);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
#openString(): void {
|
|
344
|
+
const parent = this.#top();
|
|
345
|
+
const asKey = parent !== undefined && !parent.isArray && this.#expectKey;
|
|
346
|
+
if (!asKey) this.#requireValue();
|
|
347
|
+
this.#inString = true;
|
|
348
|
+
this.#stringIsKey = asKey;
|
|
349
|
+
this.#escape = false;
|
|
350
|
+
this.#unicodeLeft = 0;
|
|
351
|
+
this.#strLength = 0;
|
|
352
|
+
this.#strHead = "";
|
|
353
|
+
this.#strTail = "";
|
|
354
|
+
this.#key = "";
|
|
355
|
+
if (asKey) {
|
|
356
|
+
this.#keyOverflow = false;
|
|
357
|
+
this.#strRetain = true;
|
|
358
|
+
this.#strCap = undefined;
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const { mode, cap, path } = this.#childMode(false);
|
|
362
|
+
this.#strObserver = this.#fields.find(
|
|
363
|
+
(field) => field.path.length === path.length && matchesPrefix(field.path, path, path.length),
|
|
364
|
+
);
|
|
365
|
+
this.#strRetain = mode === "keep" && this.#strObserver?.onChunk === undefined;
|
|
366
|
+
this.#strCap = cap;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** 字符串里的一个解码后片段;按保留规则累积。 */
|
|
370
|
+
#append(text: string): void {
|
|
371
|
+
if (text.length === 0) return;
|
|
372
|
+
if (this.#stringIsKey) {
|
|
373
|
+
if (this.#keyOverflow) return;
|
|
374
|
+
this.#key += text;
|
|
375
|
+
if (this.#key.length > MAX_KEY_CHARS) {
|
|
376
|
+
this.#keyOverflow = true;
|
|
377
|
+
this.#key = "";
|
|
378
|
+
}
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this.#strLength += text.length;
|
|
382
|
+
this.#strObserver?.onChunk?.(text);
|
|
383
|
+
if (!this.#strRetain) return;
|
|
384
|
+
const cap = this.#strCap;
|
|
385
|
+
if (cap === undefined) {
|
|
386
|
+
this.#strHead += text;
|
|
387
|
+
this.#charge(text.length);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (this.#strHead.length < cap) {
|
|
391
|
+
const room = cap - this.#strHead.length;
|
|
392
|
+
this.#strHead += text.slice(0, room);
|
|
393
|
+
text = text.slice(room);
|
|
394
|
+
if (text.length === 0) return;
|
|
395
|
+
}
|
|
396
|
+
this.#strTail += text;
|
|
397
|
+
if (this.#strTail.length > cap * 2) this.#strTail = this.#strTail.slice(-cap);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#consumeString(chunk: string, start: number): number {
|
|
401
|
+
const n = chunk.length;
|
|
402
|
+
let i = start;
|
|
403
|
+
while (i < n) {
|
|
404
|
+
if (this.#unicodeLeft > 0) {
|
|
405
|
+
const digit = hexValue(chunk.charCodeAt(i));
|
|
406
|
+
if (digit < 0) this.#fail();
|
|
407
|
+
this.#unicodeValue = this.#unicodeValue * 16 + digit;
|
|
408
|
+
this.#unicodeLeft -= 1;
|
|
409
|
+
i += 1;
|
|
410
|
+
if (this.#unicodeLeft === 0) this.#append(String.fromCharCode(this.#unicodeValue));
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (this.#escape) {
|
|
414
|
+
this.#escape = false;
|
|
415
|
+
const c = chunk[i] ?? "";
|
|
416
|
+
i += 1;
|
|
417
|
+
switch (c) {
|
|
418
|
+
case '"':
|
|
419
|
+
case "\\":
|
|
420
|
+
case "/":
|
|
421
|
+
this.#append(c);
|
|
422
|
+
break;
|
|
423
|
+
case "b":
|
|
424
|
+
this.#append("\b");
|
|
425
|
+
break;
|
|
426
|
+
case "f":
|
|
427
|
+
this.#append("\f");
|
|
428
|
+
break;
|
|
429
|
+
case "n":
|
|
430
|
+
this.#append("\n");
|
|
431
|
+
break;
|
|
432
|
+
case "r":
|
|
433
|
+
this.#append("\r");
|
|
434
|
+
break;
|
|
435
|
+
case "t":
|
|
436
|
+
this.#append("\t");
|
|
437
|
+
break;
|
|
438
|
+
case "u":
|
|
439
|
+
this.#unicodeLeft = 4;
|
|
440
|
+
this.#unicodeValue = 0;
|
|
441
|
+
break;
|
|
442
|
+
default:
|
|
443
|
+
this.#fail();
|
|
444
|
+
}
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
// 普通字符:一路扫到下一个引号、反斜杠或控制字符。
|
|
448
|
+
const runStart = i;
|
|
449
|
+
while (i < n) {
|
|
450
|
+
const c = chunk.charCodeAt(i);
|
|
451
|
+
if (c === 0x22 || c === 0x5c || c < 0x20) break;
|
|
452
|
+
i += 1;
|
|
453
|
+
}
|
|
454
|
+
if (i > runStart) this.#append(chunk.slice(runStart, i));
|
|
455
|
+
if (i >= n) break;
|
|
456
|
+
const c = chunk.charCodeAt(i);
|
|
457
|
+
i += 1;
|
|
458
|
+
if (c === 0x5c) {
|
|
459
|
+
this.#escape = true;
|
|
460
|
+
} else if (c === 0x22) {
|
|
461
|
+
this.#inString = false;
|
|
462
|
+
this.#endString();
|
|
463
|
+
return i;
|
|
464
|
+
} else {
|
|
465
|
+
this.#fail(); // 字符串里不许有裸控制字符。
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return i;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
#endString(): void {
|
|
472
|
+
if (this.#stringIsKey) {
|
|
473
|
+
const parent = this.#top();
|
|
474
|
+
if (parent === undefined) this.#fail();
|
|
475
|
+
parent.key = this.#key;
|
|
476
|
+
parent.skipValue = this.#keyOverflow;
|
|
477
|
+
this.#key = "";
|
|
478
|
+
this.#expectKey = false;
|
|
479
|
+
this.#expectColon = true;
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
this.#strObserver?.onEnd?.(this.#strLength);
|
|
483
|
+
this.#strObserver = undefined;
|
|
484
|
+
if (!this.#strRetain) {
|
|
485
|
+
this.#store(SKIPPED);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const cap = this.#strCap;
|
|
489
|
+
let text = this.#strHead;
|
|
490
|
+
if (cap !== undefined) {
|
|
491
|
+
let tail = this.#strTail;
|
|
492
|
+
if (tail.length > cap) tail = tail.slice(-cap);
|
|
493
|
+
if (this.#strLength > text.length + tail.length) {
|
|
494
|
+
// 确有省略:别把代理对从边界中间劈开。
|
|
495
|
+
if (isHighSurrogate(text.charCodeAt(text.length - 1))) text = text.slice(0, -1);
|
|
496
|
+
if (isLowSurrogate(tail.charCodeAt(0))) tail = tail.slice(1);
|
|
497
|
+
const omitted = this.#strLength - text.length - tail.length;
|
|
498
|
+
text = `${text}\n[… ${omitted} chars omitted …]\n${tail}`;
|
|
499
|
+
} else {
|
|
500
|
+
text = `${text}${tail}`;
|
|
501
|
+
}
|
|
502
|
+
this.#charge(text.length);
|
|
503
|
+
}
|
|
504
|
+
this.#strHead = "";
|
|
505
|
+
this.#strTail = "";
|
|
506
|
+
this.#store(text);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
#finishBare(): void {
|
|
510
|
+
const text = this.#bare;
|
|
511
|
+
this.#bare = "";
|
|
512
|
+
const { mode, path } = this.#childMode(false);
|
|
513
|
+
let value: boolean | number | null;
|
|
514
|
+
if (text === "true") value = true;
|
|
515
|
+
else if (text === "false") value = false;
|
|
516
|
+
else if (text === "null") value = null;
|
|
517
|
+
else if (NUMBER_PATTERN.test(text)) value = Number(text);
|
|
518
|
+
else this.#fail();
|
|
519
|
+
const observer = this.#fields.find(
|
|
520
|
+
(field) => field.path.length === path.length && matchesPrefix(field.path, path, path.length),
|
|
521
|
+
);
|
|
522
|
+
if (observer?.onScalar !== undefined) {
|
|
523
|
+
observer.onScalar(value);
|
|
524
|
+
this.#store(SKIPPED);
|
|
525
|
+
} else this.#store(mode === "keep" ? value : SKIPPED);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const SKIPPED = Symbol("skipped");
|
|
530
|
+
|
|
531
|
+
class MalformedRecord extends Error {}
|
|
532
|
+
|
|
533
|
+
function isBareChar(c: number): boolean {
|
|
534
|
+
return (
|
|
535
|
+
(c >= 0x30 && c <= 0x39) || // 0-9
|
|
536
|
+
(c >= 0x61 && c <= 0x7a) || // a-z
|
|
537
|
+
(c >= 0x41 && c <= 0x5a) || // A-Z
|
|
538
|
+
c === 0x2b ||
|
|
539
|
+
c === 0x2d ||
|
|
540
|
+
c === 0x2e
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function hexValue(c: number): number {
|
|
545
|
+
if (c >= 0x30 && c <= 0x39) return c - 0x30;
|
|
546
|
+
if (c >= 0x61 && c <= 0x66) return c - 0x61 + 10;
|
|
547
|
+
if (c >= 0x41 && c <= 0x46) return c - 0x41 + 10;
|
|
548
|
+
return -1;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const isHighSurrogate = (c: number) => c >= 0xd800 && c <= 0xdbff;
|
|
552
|
+
const isLowSurrogate = (c: number) => c >= 0xdc00 && c <= 0xdfff;
|
|
553
|
+
|
|
554
|
+
function matchesPrefix(
|
|
555
|
+
pattern: readonly string[],
|
|
556
|
+
path: readonly string[],
|
|
557
|
+
length: number,
|
|
558
|
+
): boolean {
|
|
559
|
+
for (let index = 0; index < length; index += 1) {
|
|
560
|
+
const expected = pattern[index];
|
|
561
|
+
if (expected !== "*" && expected !== path[index]) return false;
|
|
562
|
+
}
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* 一行转成记录:没超长的先过 `accepts` 预筛再 `JSON.parse`,超长的已经是筛好的记录。
|
|
568
|
+
* 解析失败(写到一半的尾行是常态)返回 `undefined`。
|
|
569
|
+
*/
|
|
570
|
+
export function jsonlEntryRecord(entry: JsonlEntry, accepts: (line: string) => boolean): unknown {
|
|
571
|
+
if ("oversized" in entry) return entry.oversized;
|
|
572
|
+
if (!accepts(entry.line)) return undefined;
|
|
573
|
+
try {
|
|
574
|
+
return JSON.parse(entry.line);
|
|
575
|
+
} catch {
|
|
576
|
+
return undefined;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* 逐行读一份 jsonl。行以 `\n` 结尾(尾部 `\r` 去掉),空行不吐。
|
|
582
|
+
*
|
|
583
|
+
* 读失败(文件不存在、权限)原样抛出,由调用方决定是空还是错。
|
|
584
|
+
*/
|
|
585
|
+
export async function* readJsonlEntries(
|
|
586
|
+
fs: JsonlSource,
|
|
587
|
+
path: string,
|
|
588
|
+
options: ReadJsonlOptions,
|
|
589
|
+
): AsyncGenerator<JsonlEntry> {
|
|
590
|
+
const decoder = new TextDecoder("utf-8");
|
|
591
|
+
let pieces: string[] = [];
|
|
592
|
+
let pending = 0;
|
|
593
|
+
let parser: OversizedRecordParser | null = null;
|
|
594
|
+
|
|
595
|
+
const startOversized = (): OversizedRecordParser => {
|
|
596
|
+
const next = new OversizedRecordParser(options.fields, options.maxRetainedChars);
|
|
597
|
+
for (const piece of pieces) next.write(piece);
|
|
598
|
+
pieces = [];
|
|
599
|
+
pending = 0;
|
|
600
|
+
return next;
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
// 把一段不含换行的文本接到当前行上。
|
|
604
|
+
const append = (text: string): void => {
|
|
605
|
+
if (text.length === 0) return;
|
|
606
|
+
if (parser !== null) {
|
|
607
|
+
parser.write(text);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
pieces.push(text);
|
|
611
|
+
pending += text.length;
|
|
612
|
+
if (pending > options.maxLineChars) parser = startOversized();
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
const takeLine = (): JsonlEntry | null => {
|
|
616
|
+
if (parser !== null) {
|
|
617
|
+
const record = parser.finish();
|
|
618
|
+
parser = null;
|
|
619
|
+
return { oversized: record };
|
|
620
|
+
}
|
|
621
|
+
if (pending === 0) return null;
|
|
622
|
+
let line = pieces.length === 1 ? (pieces[0] ?? "") : pieces.join("");
|
|
623
|
+
pieces = [];
|
|
624
|
+
pending = 0;
|
|
625
|
+
if (line.endsWith("\r")) line = line.slice(0, -1);
|
|
626
|
+
return line.length === 0 ? null : { line };
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
const feed = function* (text: string): Generator<JsonlEntry> {
|
|
630
|
+
let from = 0;
|
|
631
|
+
while (from <= text.length) {
|
|
632
|
+
const newline = text.indexOf("\n", from);
|
|
633
|
+
if (newline < 0) {
|
|
634
|
+
append(text.slice(from));
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
append(text.slice(from, newline));
|
|
638
|
+
const entry = takeLine();
|
|
639
|
+
if (entry !== null) yield entry;
|
|
640
|
+
from = newline + 1;
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
for await (const chunk of fs.createReadStream(path)) {
|
|
645
|
+
yield* feed(decoder.decode(chunk, { stream: true }));
|
|
646
|
+
}
|
|
647
|
+
yield* feed(decoder.decode());
|
|
648
|
+
const last = takeLine();
|
|
649
|
+
if (last !== null) yield last;
|
|
650
|
+
}
|