C
C
crdrads2019-01-30 00:00:11
JavaScript
crdrads, 2019-01-30 00:00:11

Why is the buffer being transformed?

When uploading an image, I use Multer, which returns req.file. Next, req.fileI need to transfer this to another function (or rather, to a process). But it changes from

buffer:
   <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 04 ef 00 00 03 ce 08 02 00 00 00 d8 d3 ab 62 00 00 00 03 73 42 49 54 08 08 08 db e1 4f e0 00 00 ... 764163 more bytes>

on the
buffer:
   { type: 'Buffer',
     data:
      [ 137,
        80,
        78,
        71,
        13...
...

Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Somewhere Intech, 2019-01-30
@crdrads

It's simple, paste into the console

console.log(`<Buffer ${(137).toString(16)} ${(80).toString(16)} ${(78).toString(16)}`)

and get the start of the raw buffer.
Your data is transformed during transmission/parsing. But data, in fact, remains equivalent to the source and there is no difference in them (if the structure of the Buffer type is built this way). Why is it being transformed?
let buffer = new Buffer.from("example");
console.log(buffer);
// <Buffer 65 78 61 6d 70 6c 65>

let json = buffer.toJSON();
console.log(json); 
// {type: "Buffer", data: [ 101, 120, 97, 109, 112, 108, 101 ] }

let example = new Buffer.from(json);
console.log(example); 
// <Buffer 65 78 61 6d 70 6c 65>

This means that your data is converted to a json structure and then transferred. But not to another function:
function printBuffer(buffer){
    console.log(buffer);
}
printBuffer(example); 
//<Buffer 65 78 61 6d 70 6c 65>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question