microsoftgraph / msgraph-sdk-javascript

Microsoft Graph client library for JavaScript
https://graph.microsoft.com
MIT License
730 stars 220 forks source link

Configuring HTTP proxy as described in our docs no longer works in Node@18 and @20 #1646

Open waldekmastykarz opened 3 months ago

waldekmastykarz commented 3 months ago

Bug Report

Description

Since v18, Node ships with a native fetch client. This client requires a different way to specify an HTTP proxy than what we've been supporting in our SDKs so far.

To specify an HTTP proxy for Node's fetch, you need to use the following code (tested on Node v18.19 and v20.11):

import { ProxyAgent } from 'undici';
import { Client } from '@microsoft/microsoft-graph-client';

const dispatcher = new ProxyAgent(process.env.https_proxy);
const fetchOptions = {
  dispatcher
};

Client.initWithMiddleware({
  middleware,
  fetchOptions
});

What's noteworthy:

We should update our FetchOptions interface to support specifying dispatcher.

Console Errors: no

Steps to Reproduce

  1. Configure an http proxy as specified in our docs

Expected behavior: web requests intercepted by the specified proxy

Actual behavior: web requests passed through without being intercepted

Additional Context

Usage Information

SDK Version - 3.06

Node Version - v18.19 and v20.11

sebastienlevert commented 3 months ago

Thanks for the report! At this point, core enhancements won't be added to this library by the team. A PR would be reviewed though.

Also, could this be fixed with just docs? Very suboptimal, but could be an option.

Thoughts?

waldekmastykarz commented 3 months ago

As far as I can tell, the only thing we need to change in the code is the type that defines fetch options, so that it includes the new dispatcher property. Other than that, we need to update docs and explain how to use it. The code itself works just fine.

feedmypixel commented 2 days ago

Came across this problem on Node v20.11.1 we were using something similar to - https://learn.microsoft.com/en-us/graph/sdks/customize-client?tabs=typescript#configuring-the-http-proxy-for-the-client:

// Create a client with the proxy
const graphClient = Client.initWithMiddleware({
  authProvider: authProvider,
  fetchOptions: {
    agent: proxyAgent,
  },
});

This was giving us a fetch failed error.

After reverse engineering things and reading through various posts we arrived at https://github.com/nodejs/undici/issues/1350#issuecomment-1137916129

As we are Js and not Ts, we have ended up with:

const graphClient = Client.initWithMiddleware({
  debugLogging: true,
  authProvider,
  fetchOptions: {
    dispatcher: new ProxyAgent({
      uri: proxyUrl,
      keepAliveTimeout: 10,
      keepAliveMaxTimeout: 10
    })
  }
})

Thought this might help someone in the future.