Skip to content

Commit

Permalink
[Identity] InteractiveBrowserCredential for Node now waits for the li…
Browse files Browse the repository at this point in the history
…stener to start listening (#15497)

* [Identity] Attempting to fix odd bug on Windows tests

* listening before allowing any throw

* one less change

* just throwing the error we receive
  • Loading branch information
sadasant authored Jun 2, 2021
1 parent cc05f39 commit a6fbc39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
3 changes: 3 additions & 0 deletions sdk/identity/identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 2.0.0-beta.4 (Unreleased)

## Bug fixes

- Fixed an issue in which `InteractiveBrowserCredential` on Node would sometimes cause the process to hang if there was no browser available.
### Breaking changes

- Removed the protected method `getAzureCliAccessToken` from the public API of the `AzureCliCredential`. While it will continue to be available as part of v1, we won't be supporting this method as part of v2's public API.
Expand Down
30 changes: 18 additions & 12 deletions sdk/identity/identity/src/msal/nodeFlows/msalOpenBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,13 @@ export class MsalOpenBrowser extends MsalNode {
cleanup();
});
};

const app = http.createServer(requestListener);
const server = stoppable(app);

const listen = app.listen(this.port, this.hostname, () =>
this.logger.info(`InteractiveBrowserCredential listening on port ${this.port}!`)
);
app.on("connection", (socket) => socketToDestroy.push(socket));
const server = stoppable(app);

this.openAuthCodeUrl(scopes).catch((e) => {
cleanup();
reject(e);
});

function cleanup(): void {
if (listen) {
Expand All @@ -166,13 +161,24 @@ export class MsalOpenBrowser extends MsalNode {
}
}

const abortSignal = options?.abortSignal;
if (abortSignal) {
abortSignal.addEventListener("abort", () => {
app.on("connection", (socket) => socketToDestroy.push(socket));

app.on("listening", () => {
const openPromise = this.openAuthCodeUrl(scopes);

const abortSignal = options?.abortSignal;
if (abortSignal) {
abortSignal.addEventListener("abort", () => {
cleanup();
reject(new Error("Aborted"));
});
}

openPromise.then().catch((e) => {
cleanup();
reject(new Error("Aborted"));
reject(e);
});
}
});
});
}

Expand Down

0 comments on commit a6fbc39

Please sign in to comment.