Skip to content

Instantly share code, notes, and snippets.

@ntotten
Created December 31, 2024 22:37
Show Gist options
  • Save ntotten/4146becec72ed5109f4b24f30e3ebf69 to your computer and use it in GitHub Desktop.
Save ntotten/4146becec72ed5109f4b24f30e3ebf69 to your computer and use it in GitHub Desktop.
const fileStream = request.body; // Stream of the file content
const reader = fileStream.getReader();
const decoder = new TextDecoder();
let firstColumnValues = [];
let leftover = ''; // Store any partial lines
const processChunk = async ({ done, value }) => {
if (done) {
// Process any remaining line in leftover
if (leftover) {
const columns = leftover.split(',');
firstColumnValues.push(columns[0]);
}
return;
}
const chunk = decoder.decode(value, { stream: true });
const lines = (leftover + chunk).split('\n');
leftover = lines.pop(); // Save the last partial line
for (const line of lines) {
const columns = line.split(',');
if (columns.length > 0) {
firstColumnValues.push(columns[0]); // Extract the first column
}
}
return reader.read().then(processChunk);
};
await reader.read().then(processChunk);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment