I want to replace readFileSync with fetch – I want to fetch a certificate file from a remote server and turn it into a buffer identical to what readFileSync would return reading the same certificate file stored locally. However, the buffers differ.
This is what I’ve got so far:
const fs = require('fs');
const fsBuffer = fs.readFileSync('./FPTestcert4_20220818.p12');
const res = await fetch('https://www.bankid.com/assets/bankid/rp/FPTestcert4_20220818.p12');
const wBuffer = await res.arrayBuffer();
const wBuffer2 = Buffer.from(wBuffer, 'utf8');
console.log(fsBuffer);
console.log(wBuffer2);
Output:
<Buffer 30 82 0b 09 02 01 03 30 82 0a cf 06 09 2a 86 48 86 f7 0d 01 07 01 a0 82 0a c0 04 82 0a bc 30 82 0a b8 30 82 05 6f 06 09 2a 86 48 86 f7 0d 01 07 06 a0 ... 2779 more bytes>
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 65 6e 22 3e 0a 3c 68 65 61 64 3e 0a 20 20 20 20 3c 74 69 74 6c 65 ... 2840 more bytes>
I have verified that the two files are identical.
Any ideas? Using axios or other fetching mechanisms is acceptable.
2
Answers
For completeness - here are the two discussed solutions (replace
example.com
with your own server):Output:
After you fetched, it seems like you didn’t do anything to the response and just straight up converted it into a BufferArr, which means the Buffer isn’t really your file, it’s just the response body returned from your fetch.
wBuffer2
is just a buffer representation of the response body.More about that here: https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods
So you probably want to get the body of the response first, then convert that body into the buffer array
Note: I’m not sure if you’re using the nodejs experimental fetch api, or the node-fetch package, but they are identical
Since you are fetching a file, you will always get a ReadableStream your body, and you would have to use this to convert it into a Buffer:
Here’s a useful resource https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams and you can always search up how to do so.