This commit is contained in:
Will Greenberg
2024-11-18 16:00:04 -08:00
parent fa96520fe5
commit 57b0455363
22 changed files with 5071 additions and 509 deletions

24
bin/web/src/lib/ndjson.ts Normal file
View File

@@ -0,0 +1,24 @@
export type NewlineDeliminatedJson = any[];
export function parse_ndjson(input: string): NewlineDeliminatedJson {
const lines = input.split('\n');
const result = [];
let current_line = '';
while (lines.length > 0) {
current_line += lines.shift();
try {
const entry = JSON.parse(current_line);
result.push(entry);
current_line = '';
} catch (e) {
// if this chunk wasn't valid JSON, assume there was an escaped
// newline in the JSON line, so simply continue to the next one.
// however, if we've reached the end of the input, that means we
// were given invalid nd-json
if (lines.length === 0) {
throw new Error(`unable to parse invalid nd-json: ${e}`);
}
}
}
return result;
}