I'm trying to download a series of csv files from a website and concatenate them into one big file. The output of each file starts with:
Number of records: x
Header row
record 1
record 2
....
I'm pretty new to the concepts of buffers and streams, but I decided to download the csvs as a stream and load into a buffer to manipulate. I have:
async function stream2buffer(stream) {
return new Promise((resolve, reject) => {
const _buf = [];
stream.on("data", (chunk) => _buf.push(chunk));
stream.on("end", () => resolve(Buffer.concat(_buf)));
stream.on("error", (err) => reject(err));
});
}
(async () => {
const finishedDownload = promisify(stream.finished);
const writer = fs.createWriteStream('./file1.csv');
const response = await axios({
method: 'GET',
url: URL,
responseType: 'stream',
});
const buf = await stream2buffer(response.data);
console.log('buffer?');
console.log(buf);
// response.data.pipe(writer);
// await finishedDownload(writer);
})();
There are no errors but the output is not text:
buffer?
<Buffer 54 6f 74 61 6c 20 52 65 73 75 6c 74 73 3a 20 31 34 0d 0a 41 50 4e 2c 4f 77 6e 65 72 2c 41 64 64 72 65 73 73 2c 43 69 74 79 2c 5a 69 70 2c 53 75 62 64 ... 1731 more bytes>
How can I convert the buffer info to text I can work with?
CodePudding user response:
I don't know if this answers your question:
console.log('buffer?');
// console.log(buf);
console.log(buf.toString());
CodePudding user response:
It looks like it is outputting it in hexadecimal, does this answer your question?:
function hex_txt(hex) {
var str = '';
hex = hex.replace(/ /g, "")//.replace(/<Buffer/g, "").replace(/>/g, ""); // uncomment this if it doesn't work
for (let i = 0; i < hex.length; i = 2) {
str = String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
console.log(hex_txt(buf.toString())); //instead of console.log(buf), put this
There wasn't a lot of detail, so I don't exactly know if this will be correct.