MCPcopy Create free account
hub / github.com/Noumena-Network/code / readJSONLFile

Function readJSONLFile

src/utils/json.ts:231–256  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

229 * is ~2M tokens, which is well under 100 MB of JSONL.
230 */
231export async function readJSONLFile<T>(filePath: string): Promise<T[]> {
232 const { size } = await stat(filePath)
233 if (size <= MAX_JSONL_READ_BYTES) {
234 return parseJSONL<T>(await readFile(filePath))
235 }
236 await using fd = await open(filePath, 'r')
237 const buf = Buffer.allocUnsafe(MAX_JSONL_READ_BYTES)
238 let totalRead = 0
239 const fileOffset = size - MAX_JSONL_READ_BYTES
240 while (totalRead < MAX_JSONL_READ_BYTES) {
241 const { bytesRead } = await fd.read(
242 buf,
243 totalRead,
244 MAX_JSONL_READ_BYTES - totalRead,
245 fileOffset + totalRead,
246 )
247 if (bytesRead === 0) break
248 totalRead += bytesRead
249 }
250 // Skip the first partial line
251 const newlineIndex = buf.indexOf(0x0a)
252 if (newlineIndex !== -1 && newlineIndex < totalRead - 1) {
253 return parseJSONL<T>(buf.subarray(newlineIndex + 1, totalRead))
254 }
255 return parseJSONL<T>(buf.subarray(0, totalRead))
256}
257
258export function addItemToJSONCArray(content: string, newItem: unknown): string {
259 try {

Callers

nothing calls this directly

Calls 5

statFunction · 0.85
parseJSONLFunction · 0.85
readFileFunction · 0.85
openFunction · 0.85
readMethod · 0.45

Tested by

no test coverage detected