msgraph-sdk-javascript icon indicating copy to clipboard operation
msgraph-sdk-javascript copied to clipboard

getStream doesn't work on node 18

Open avdovi opened this issue 3 years ago • 6 comments

Bug Report

Prerequisites

  • [x] Can you reproduce the problem?
  • [x] Are you running the latest version?
  • [x] Are you reporting to the correct repository?
  • [x] Did you perform a cursory search?

For more information, see the CONTRIBUTING guide.

Description

The .getStream() method doesn't work when running on node 18.

Console Errors: [Is there any console error]

TypeError: stream.on is not a function

Screenshots: [If applicable, add screenshots to help explain your problem]

image

Steps to Reproduce

Here is a small application to reproduce the issue(just include the IDs):

import 'isomorphic-fetch'; // unnecessary and can be removed
import { Client } from '@microsoft/microsoft-graph-client';

const tenantId = '';
const clientId = '';
const clientSecret = '';
const userID = '';

const createAccessToken = async ({ tenantId, clientId, clientSecret }) => {
  const params = new URLSearchParams();
  params.append('grant_type', 'client_credentials');
  params.append('client_id', clientId);
  params.append('client_secret', clientSecret);
  params.append('scope', 'https://graph.microsoft.com/.default');

  const response = await fetch(
    `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
    {
      method: 'POST',
      body: params,
    }
  );

  const { access_token } = await response.json();

  return access_token;
};

const createClient = async connectionConfig => {
  const accessToken = await createAccessToken(connectionConfig);

  return Client.init({
    authProvider: done => {
      done(null, accessToken);
    },
  });
};

const run = async () => {
  const imageUrl = `/users/${userID}/photo/$value`;
  const client = await createClient({ tenantId, clientId, clientSecret });

  const photo = await client.api(imageUrl).get();
  console.log({ photo }); // <-- photo is loaded

  const stream = await client.api(imageUrl).getStream();
  // fails with: stream.on is not a function
  stream.on('data', data => {
    console.log(data);
  });
};

run();

Expected behavior: [What you expected to happen]

a stream to be returned by .getStream() function

Actual behavior: [What actually happened]

.getStream() function is not returning a stream

Additional Context

Node 18 is used

Usage Information

Request ID - Value of the requestId field if you are receiving a Graph API error response

SDK Version - [SDK version you are using]

3.0.2

  • [x] Node (Check, if using Node version of SDK)

Node Version - [The version of Node you are using]

18.8.0

avdovi avatar Sep 08 '22 15:09 avdovi

getStream returns Web Streams ReadableStream, so you can either use its API or convert it to the Node stream with stream.Readable.fromWeb(yourStream)

byF avatar Sep 10 '22 16:09 byF

@avdovi is this still an issue with node streams?

nikithauc avatar Sep 14 '22 02:09 nikithauc

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Sep 18 '22 20:09 ghost

@avdovi is this still an issue with node streams?

I was able to implement a workaround using the help from @byF, but this is a breaking change.

rebelok avatar Sep 18 '22 21:09 rebelok

@rebelok I would like to understand if you prefer using the global fetch introduced in node18 or do you think you can disable the feature using the flag --no-experimental-fetch?

nikithauc avatar Oct 10 '22 22:10 nikithauc

Using @byF suggested workaround Readable.fromWeb() has stopped working for us on node 20. The \"readableStream\" argument must be an instance of ReadableStream. Received an instance of PassThrough". Still looking for a solution. Any reason why getStreams returns Promise<any>? Having an actual return type would help with clarify some confusion between node readablestreams and web readablestreams.

rafagsiqueira avatar May 13 '24 21:05 rafagsiqueira