question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to parse file and create a readable stream to send in a http request.

See original GitHub issue

I want to parse a file and create a readable stream that can be sent to another service in a http request.

Uploadmiddleware

export const UploadMiddleware = (req: Request, res: Response, next: NextFunction) => {

    const busboy = new Busboy({ headers: req.headers });
    busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
        req.body.image = file;
    });
    busboy.on("field", (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
        req.body[fieldname] = val;
    });
    busboy.on("finish", () => {
        next();
    });
    req.pipe(busboy);
}

This upload middleware is adding the file to the req.body.image field. This field now contains this stream.

File Stream

  image: FileStream {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: null,
      pipesCount: 0,
      flowing: true,
      ended: true,
      endEmitted: true,
      reading: false,
      sync: false,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      paused: false,
      emitClose: true,
      autoDestroy: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrain: 0,
      readingMore: false,
      decoder: null,
      encoding: null
    },
    readable: false,
    _events: [Object: null prototype] { end: [Array], data: [Function] },
    _eventsCount: 2,
    _maxListeners: undefined,
    truncated: false,
    _read: [Function]
  },

However when i try to post this stream to another service using axios. It does not like it and the other service times out. I believe this stream is not something the other service can read.

Http request to the other service

      const formData = new FormData();
      formData.append("image", request.image); //req.image is the above stream

      const options: any = {
            url,
            method: "post",
            headers: formData.getHeaders(),
            data: formData,
       };

        await axios(options)

Side Note:

The stream obtained from fs.createReadStream is something that the other service can read when sent in the same http post request using axios. That stream looks like the following.


  image: ReadStream {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 65536,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: null,
      pipesCount: 0,
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      paused: true,
      emitClose: false,
      autoDestroy: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrain: 0,
      readingMore: false,
      decoder: null,
      encoding: null
    },
    readable: true,
    _events: [Object: null prototype] { end: [Function] },
    _eventsCount: 1,
    _maxListeners: undefined,
    path: 'front.jpg',
    fd: null,
    flags: 'r',
    mode: 438,
    start: undefined,
    end: Infinity,
    autoClose: true,
    pos: undefined,
    bytesRead: 0,
    closed: false
  },

Can i convert this Filestream obtained from busboy into a stream that can be sent in a http post request to be read.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
ghostcommented, Oct 24, 2019

Okay so if anyone else is interested this is how i solved the problem. In the end i stored the bytes in an array and then created a new Readable Stream because i couldint send original file stream via axios(http) to another service.

      const f = [];
      busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
            file.on("data", (data) => {
                f.push(data);
            });
            file.on("end", () => {
                const fileStream = createReadableStream(f);
                req.body[fieldname] = fileStream;
            });
        });
const createReadableStream = (buffer) => {
    const readableInstanceStream = new Readable({
        read() {
            for (const bytes of buffer) {
                this.push(bytes);
            }
            this.push(null);
        },
    });
    return readableInstanceStream;
};
1reaction
Asheboycommented, Jun 9, 2021

@mscdex Should this be closed? It’s definitely still an issue and I’m not sure there’s an explanation as to why this happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using readable streams - Web APIs | MDN
In our Simple stream pump example, we consume the custom readable stream by passing it into a Response constructor call, after which we...
Read more >
How To Work with Files Using Streams in Node.js - DigitalOcean
In this article, you will read from and write to a file using the fs.createReadStream and fs.createWriteStream functions. You will also use the ......
Read more >
How to append a readable stream to from data and post
I am a new nodejs stream. I want to create a stream in server A, that can store the file after parsing a...
Read more >
Implement HTTP Streaming with Node.js and Fetch API
When building web applications, we typically have a REST API with GET endpoints that do something like this: Parse the request (URL, query...
Read more >
Streaming requests with the fetch API - Chrome Developers
# Streaming request bodies ... method: 'POST', headers: {'Content-Type': 'text/plain'}, body: stream, duplex: 'half', });. The above will send " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found