Search code examples
node.jstypescriptmicrosoft-graph-api

How do I use the ReadableStream returned by the Graph Client?


While fetching user profile pictures, I'm using the microsoft graph client's getStream() in the hopes of streaming the image data.

Logging the return value claims the returned object is a ReadableStream, but it's not any readable stream I recognise - it has seemingly no pipe methods nor a getReader, and isn't accepted in anything that I would expect to take web streams, like Readable.fromWeb. How do I turn it into a conventional Node Readable?


Solution

  • It's also a mystery for me, what kind of "stream" is returned. Anyway, I am doing it like this (concatenates content in memory). If you need to pipe, you can do it as well without reading the whole file into memory (pass in the output stream and write into it in a loop)

    const getFileContent = async (path: string) => Promise<Buffer> {
    
        const stream = await graph.api(path).getStream();
    
        const chunks: any[] = [];
        for await (const chunk of stream) {
          chunks.push(Buffer.from(chunk));
        }
    
        return Buffer.concat(chunks);
    }