MCPcopy Create free account
hub / github.com/EvoMap/evolver / forEachJsonLine

Function forEachJsonLine

src/proxy/mailbox/store.js:128–213  ·  view source on GitHub ↗
(filePath, onRow, opts = {})

Source from the content-addressed store, hash-verified

126}
127
128function forEachJsonLine(filePath, onRow, opts = {}) {
129 const stats = { parsed: 0, corrupt: 0, overlong: 0 };
130 if (!fs.existsSync(filePath)) return stats;
131
132 const maxLineBytes = Math.max(1, Math.floor(Number(opts.maxLineBytes) || resolveMaxJsonlLineBytes()));
133 const chunk = Buffer.allocUnsafe(JSONL_READ_CHUNK_BYTES);
134 let fd = null;
135 let parts = [];
136 let lineBytes = 0;
137 let dropping = false;
138
139 const resetLine = () => {
140 parts = [];
141 lineBytes = 0;
142 dropping = false;
143 };
144
145 const appendSegment = (segment) => {
146 if (!segment || segment.length === 0 || dropping) return;
147 if (lineBytes + segment.length > maxLineBytes) {
148 parts = [];
149 lineBytes = 0;
150 dropping = true;
151 return;
152 }
153 parts.push(Buffer.from(segment));
154 lineBytes += segment.length;
155 };
156
157 const finishLine = (hasNewline = false) => {
158 const totalLineBytes = lineBytes + (hasNewline ? 1 : 0);
159 if (dropping) {
160 stats.overlong += 1;
161 resetLine();
162 return;
163 }
164 if (totalLineBytes > maxLineBytes) {
165 stats.overlong += 1;
166 resetLine();
167 return;
168 }
169 if (lineBytes === 0) {
170 resetLine();
171 return;
172 }
173 const trimmed = Buffer.concat(parts, lineBytes).toString('utf8').trim();
174 resetLine();
175 if (!trimmed) return;
176 try {
177 onRow(JSON.parse(trimmed));
178 stats.parsed += 1;
179 } catch {
180 stats.corrupt += 1;
181 }
182 };
183
184 try {
185 fd = fs.openSync(filePath, 'r');

Callers 1

_rebuildIndexMethod · 0.85

Calls 3

resolveMaxJsonlLineBytesFunction · 0.85
appendSegmentFunction · 0.85
finishLineFunction · 0.85

Tested by

no test coverage detected