Skip to content

Commit ae09bf1

Browse files
committed
addressed comments
1 parent 42bc3c2 commit ae09bf1

File tree

7 files changed

+33
-34
lines changed

7 files changed

+33
-34
lines changed

lib/msal-browser/apiReview/msal-browser.api.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ export interface IController {
938938
// (undocumented)
939939
initialize(request?: InitializeApplicationRequest): Promise<void>;
940940
// (undocumented)
941+
initialize(request?: InitializeApplicationRequest, isBroker?: boolean): Promise<void>;
942+
// (undocumented)
941943
initializeWrapperLibrary(sku: WrapperSKU, version: string): void;
942944
// @internal (undocumented)
943945
isBrowserEnv(): boolean;
@@ -993,7 +995,6 @@ export { INetworkModule }
993995
// @public
994996
export type InitializeApplicationRequest = {
995997
correlationId?: string;
996-
isBroker?: boolean;
997998
};
998999

9991000
// Warning: (ae-missing-release-tag) "inMemRedirectUnavailable" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)

lib/msal-browser/src/app/PublicClientApplication.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ export class PublicClientApplication implements IPublicClientApplication {
9393
* @param request {?InitializeApplicationRequest}
9494
*/
9595
async initialize(request?: InitializeApplicationRequest): Promise<void> {
96-
return this.controller.initialize({
97-
...request,
98-
isBroker: this.isBroker,
99-
});
96+
return this.controller.initialize(request, this.isBroker);
10097
}
10198

10299
/**

lib/msal-browser/src/controllers/IController.ts

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export interface IController {
3131
// TODO: Make request mandatory in the next major version?
3232
initialize(request?: InitializeApplicationRequest): Promise<void>;
3333

34+
initialize(
35+
request?: InitializeApplicationRequest,
36+
isBroker?: boolean
37+
): Promise<void>;
38+
3439
acquireTokenPopup(request: PopupRequest): Promise<AuthenticationResult>;
3540

3641
acquireTokenRedirect(request: RedirectRequest): Promise<void>;

lib/msal-browser/src/controllers/StandardController.ts

+12-17
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ export class StandardController implements IController {
314314
* Initializer function to perform async startup tasks such as connecting to WAM extension
315315
* @param request {?InitializeApplicationRequest} correlation id
316316
*/
317-
async initialize(request?: InitializeApplicationRequest): Promise<void> {
317+
async initialize(
318+
request?: InitializeApplicationRequest,
319+
isBroker?: boolean
320+
): Promise<void> {
318321
this.logger.trace("initialize called");
319322
if (this.initialized) {
320323
this.logger.info(
@@ -339,8 +342,11 @@ export class StandardController implements IController {
339342
);
340343
this.eventHandler.emitEvent(EventType.INITIALIZE_START);
341344

342-
if (!request?.isBroker) {
343-
this.logMultipleInstances(initMeasurement);
345+
// Broker applications are initialized twice, so we avoid double-counting it
346+
if (!isBroker) {
347+
try {
348+
this.logMultipleInstances(initMeasurement);
349+
} catch {}
344350
}
345351

346352
await invokeAsync(
@@ -2397,6 +2403,7 @@ export class StandardController implements IController {
23972403
): void {
23982404
const clientId = this.config.auth.clientId;
23992405

2406+
if (!window) return;
24002407
// @ts-ignore
24012408
window.msal = window.msal || {};
24022409
// @ts-ignore
@@ -2405,26 +2412,14 @@ export class StandardController implements IController {
24052412
// @ts-ignore
24062413
const clientIds: string[] = window.msal.clientIds;
24072414

2408-
for (const currentId of clientIds) {
2409-
if (currentId === clientId) {
2410-
this.logger.warning(
2411-
"There is already an instance of MSAL.js in the window with the same client id."
2412-
);
2413-
// @ts-ignore
2414-
window.msal.clientIds.push(clientId);
2415-
collectInstanceStats(clientId, performanceEvent);
2416-
return;
2417-
}
2418-
}
2419-
24202415
if (clientIds.length > 0) {
2421-
this.logger.warning(
2416+
this.logger.verbose(
24222417
"There is already an instance of MSAL.js in the window."
24232418
);
24242419
}
24252420
// @ts-ignore
24262421
window.msal.clientIds.push(clientId);
2427-
collectInstanceStats(clientId, performanceEvent);
2422+
collectInstanceStats(clientId, performanceEvent, this.logger);
24282423
}
24292424
}
24302425

lib/msal-browser/src/request/InitializeApplicationRequest.ts

-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
* InitializeApplicationRequest: Request object passed by user to initialize application
88
*
99
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
10-
* - isBroker - Boolean flag indicating whether the application is acting as a broker.
1110
*/
1211
export type InitializeApplicationRequest = {
1312
correlationId?: string;
14-
isBroker?: boolean;
1513
};

lib/msal-browser/src/utils/MsalFrameStatsUtils.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
* Licensed under the MIT License.
44
*/
55

6-
import { InProgressPerformanceEvent } from "@azure/msal-common/browser";
6+
import { InProgressPerformanceEvent, Logger } from "@azure/msal-common/browser";
77

88
export function collectInstanceStats(
99
currentClientId: string,
10-
performanceEvent: InProgressPerformanceEvent
10+
performanceEvent: InProgressPerformanceEvent,
11+
logger: Logger
1112
): void {
1213
const frameInstances: string[] =
1314
// @ts-ignore
1415
window.msal?.clientIds || [];
1516

1617
const msalInstanceCount = frameInstances.length;
1718

18-
let sameClientIdInstanceCount = 0;
19+
const sameClientIdInstanceCount = frameInstances.filter(
20+
(i) => i === currentClientId
21+
).length;
1922

20-
for (const i of frameInstances) {
21-
if (i === currentClientId) sameClientIdInstanceCount++;
23+
if (sameClientIdInstanceCount > 1) {
24+
logger.warning(
25+
"There is already an instance of MSAL.js in the window with the same client id."
26+
);
2227
}
2328
performanceEvent.add({
2429
msalInstanceCount: msalInstanceCount,

lib/msal-browser/test/app/PublicClientApplication.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
608608
"initialize"
609609
);
610610
await pca.initialize();
611-
expect(initializeControllerSpy).toHaveBeenCalledWith({
612-
isBroker: false,
613-
});
611+
expect(initializeControllerSpy).toHaveBeenCalledWith(undefined, false);
614612
});
615613
});
616614

@@ -7794,7 +7792,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
77947792
);
77957793
});
77967794

7797-
it("Logs warning if there are two applications with different client ids in the same frame", async () => {
7795+
it("Logs verbose if there are two applications with different client ids in the same frame", async () => {
77987796
const msalConfig: Configuration = {
77997797
auth: {
78007798
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
@@ -7819,7 +7817,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
78197817
.mockImplementation();
78207818
await pca2.initialize();
78217819
expect(loggerCallbackStub).toHaveBeenCalledWith(
7822-
LogLevel.Warning,
7820+
LogLevel.Verbose,
78237821
expect.stringContaining(
78247822
"There is already an instance of MSAL.js in the window."
78257823
),

0 commit comments

Comments
 (0)