Skip to content

Conversation

@nikithauc
Copy link
Contributor

Fixes #265

* @param {string} url - The request url string
* @returns true if request url is a Graph url
*/
private isGraphURL(url: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. construct a Set() object for graphURLs where the values are only the host names.
  2. construct a typed URL object for url.
  3. Have this function return the result of graphURLs.has(urlObject.hostname)

Benefits:

  1. Efficiency: Lookup is amortized O(1).
  2. Correctness: We probably want to capture telemetry for urls like: https://graph.microsoft.com:443 and current implementation will not accept URLs with port numbers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also consider inlining the whole function as it will be a simple call to graphURLs.has(new URL(context.request).hostname)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for suggesting an optimal approach!

I have used the approach you suggested with some modifications. Implemented a set object with the host names. I haven't used the URL object as some browsers do not support of the URL object and I am extracting the host name as it is done in GraphRequest.ts during the url parsing process.


// using an IIFE to populate the set object with the graph host names as Set(iterable:Object) is not supported in Internet Explorer
(() => {
const urls = ["graph.microsoft.com", "graph.microsoft.us", "dod-graph.microsoft.us", "graph.microsoft.de", "microsoftgraph.chinacloudapi.cn"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could initialize the set directly from the array, see the edit in the response.
https://stackoverflow.com/a/30902786/3808675

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used an IIFE with set.add() instead as IE does not support Set(array). IE creates an empty set when I passed in new Set(object/array).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, and I'm guessing this is an edge case the transpilation is not catching?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additionally this is something that people (or we) can polyfill using core-js, see the first example. https://www.npmjs.com/package/core-js

const endOfHostStrPos = url.indexOf("/");
if (endOfHostStrPos !== -1) {
// Parse out the host
const hostName = url.substring(0, endOfHostStrPos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't cover the scenario of https://graph.microsoft.com:443/v1.0/me

@nikithauc nikithauc closed this Oct 28, 2020
@nikithauc
Copy link
Contributor Author

Will create another pull request dealing with custom headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LargeFileUploadTask cors error

3 participants