2

I am receiving data reading from an S3 bucket, which will contain .html files. they are being received by Node like so:

{"type":"Buffer","data":[60,104,116,109,108,32,120,109,108,110,115,58,118,61,34,117,114 ....]}

Is there any way you can take this buffer and use it somewhere to insert directly into like an iFrame src? Or what would be the best way to go about taking this buffer and being able to show it as an HTML page?

Was also thinking of taking the buffer and writing the file with fs.writeFile(...) and using the local path as the src.

Any suggestions / advice would be appreciated thank you in advance.

kt-workflow
  • 359
  • 1
  • 3
  • 14

2 Answers2

2

You can convert the ArrayBuffer to a String easily. Given your partial Buffer, it would look like this:

const dataFromS3 = {"type":"Buffer","data":[60,104,116,109,108,32,120,109,108,110,115,58,118,61,34,117,114]};
const output = String.fromCharCode.apply(null, dataFromS3.data);

console.log(output);
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
2

That would work as well

const d = {"type":"Buffer","data":[60,104,116,109,108,32,120,109,108,110,115,58,118,61,34,117,114]}

const r =  Buffer.from(d.data).toString()
console.log(r) // <html xmlns:v="ur
Jerome
  • 294
  • 2
  • 14
  • Thanks, I had used the below answer but this would work as well – kt-workflow Feb 23 '21 at 19:39
  • 1
    Actually, I will say this method is better than the other one listed. Because, when I was receiving quotation marks it wouldn't be able to translate it properly. Example: ("Test") would translate to (âTestâ) Marking this as answer! – kt-workflow Feb 24 '21 at 23:07
  • @kt-workflow - Thanks for pointing that out about the `fromCharCode` vs. `Buffer.toString()` - can you please provide sample data that produces that effect so I can trouble shoot my answer? Thanks. – Randy Casburn Feb 25 '21 at 22:37