Created
December 31, 2024 22:37
-
-
Save ntotten/4146becec72ed5109f4b24f30e3ebf69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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