Skip to content

Commit 3827d96

Browse files
authored
[Auth] Break down event ID digits to a max Math.random() digit count (#5098)
* [Auth] Break down event ID digits to a max Math.random() digit count * License/formatting * src/core/util/event_id.ts
1 parent 99526ac commit 3827d96

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
import { _generateEventId } from './event_id';
20+
21+
describe('core/util/event_id', () => {
22+
it('sub-15 digit id', () => {
23+
expect(_generateEventId('', 10)).to.have.length(10);
24+
});
25+
26+
it('15 digit id', () => {
27+
expect(_generateEventId('', 15)).to.have.length(15);
28+
});
29+
30+
it('above-15 digit id', () => {
31+
expect(_generateEventId('', 20)).to.have.length(20);
32+
});
33+
});

packages-exp/auth-exp/src/core/util/event_id.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
export function _generateEventId(prefix?: string): string {
19-
return `${prefix ? prefix : ''}${Math.floor(Math.random() * 1000000000)}`;
18+
export function _generateEventId(prefix = '', digits = 10): string {
19+
let random = '';
20+
for (let i = 0; i < digits; i++) {
21+
random += Math.floor(Math.random() * 10);
22+
}
23+
return prefix + random;
2024
}

packages-exp/auth-exp/src/platform_browser/messagechannel/sender.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { _generateEventId } from '../../core/util/event_id';
1819
import {
1920
_SenderRequest,
2021
_EventType,
@@ -32,10 +33,6 @@ interface MessageHandler {
3233
onMessage: EventListenerOrEventListenerObject;
3334
}
3435

35-
function generateEventId(prefix = '', digits = 20): string {
36-
return `${prefix}${Math.floor(Math.random() * Math.pow(10, digits))}`;
37-
}
38-
3936
/**
4037
* Interface for sending messages and waiting for a completion response.
4138
*
@@ -91,7 +88,7 @@ export class Sender {
9188
let completionTimer: any;
9289
let handler: MessageHandler;
9390
return new Promise<_ReceiverMessageResponse<T>>((resolve, reject) => {
94-
const eventId = generateEventId();
91+
const eventId = _generateEventId('', 20);
9592
messageChannel.port1.start();
9693
const ackTimer = setTimeout(() => {
9794
reject(new Error(_MessageError.UNSUPPORTED_EVENT));

0 commit comments

Comments
 (0)