2

I've a stream of pdf file which I need to convert into a buffer and I am tying to read the stream using stream.on but its not working and I keep getting error

UnhandledPromiseRejectionWarning: TypeError: stream.on is not a function

My stream starts looks like this

%PDF-1.3
%����
12 0 obj
<<
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Filter [/FlateDecode /DCTDecode]
/Height 117
/Length 2703
/Mask [ 253 255 253 255 253 255 ]
/Name /Obj0
/Subtype /Image
/Type /XObject
/Width 73
>>
stream

and it ends with

%%EOF

Here is my code

// api call
 instance.post(GENERATE_LABEL, strigifiedBody, { headers: {
      'Content-Type': 'application/json; charset=utf-8'
    }, responseType: 'stream' } ).then(async (res) => {
      streamToString(res.data).then(async (strData) => {
          const stream = extractStreamFromResponse(res, strData);
        buffer = await getBuffer(stream);
        console.log(buffer);       
      });
    }).catch(err => {
        console.log(err);
    });

//function to extract stream from multipart/mixed data
  function extractStreamFromResponse(res, data) {
    let header = res.headers['content-type'];
    let boundary = header.split(' ')[1];
    boundary = boundary.split('=')[1];
    boundary = boundary.split('"')[1];
    const rawStream = data.split('--' + boundary)[2];
    const stream = rawStream.substring(100);
    return stream;
  }

  // function to get buffer from stream
  function getBuffer(stream) {
  const chunks = [];
  return new Promise((resolve, reject) => {
      stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
      stream.on('error', (err) => reject(err));
      stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
    })
  }

How can I create a buffer of this stream?

Ehsan Nissar
  • 643
  • 2
  • 13
  • 35
  • 1
    What does generate the stream? Thats the most interesting part, and its missing. – Marc Dec 01 '21 at 17:12
  • @Marc it will make a mess here I am afraid but actually I have a multipart/mixed format data which contains stream and json and so I wrote 2 functions one of which returns me json and the one returns me this stream which I posted above. Know that I had to remove content-header and other not required data from top of the stream using `data.substring(100)`. So now stream starts from %PDF – Ehsan Nissar Dec 01 '21 at 17:15
  • @Marc can you tell if this stream is valid or not because I checked from stream packakge and it says it not valid. Which explains why stream.on is not working but how can i make this valid then? – Ehsan Nissar Dec 01 '21 at 17:27
  • I cant answer your question. You need to show the full code. – Marc Dec 01 '21 at 18:00
  • @Marc Please check now, I think I've added everything – Ehsan Nissar Dec 01 '21 at 18:19
  • `extractStreamFromResponse()` does not create/return a stream – Marc Dec 01 '21 at 18:36
  • but it is returning me the stream which I shoed in question @Marc – Ehsan Nissar Dec 01 '21 at 18:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239752/discussion-between-ahsan-nissar-and-marc). – Ehsan Nissar Dec 01 '21 at 18:54
  • You dont retrun a stream, its just something from the input string. https://nodejs.org/dist/latest-v16.x/docs/api/stream.html – Marc Dec 01 '21 at 18:59
  • @KJ Then how do you propose to separate json and stream from multipart/mixed data? – Ehsan Nissar Dec 02 '21 at 05:35

0 Answers0