From 3827d9692a169d9b315ce2a7640f37cc65ce0237 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 2 Jul 2021 09:04:16 -0700 Subject: [PATCH] [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 --- .../auth-exp/src/core/util/event_id.test.ts | 33 +++++++++++++++++++ .../auth-exp/src/core/util/event_id.ts | 8 +++-- .../platform_browser/messagechannel/sender.ts | 7 ++-- 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 packages-exp/auth-exp/src/core/util/event_id.test.ts diff --git a/packages-exp/auth-exp/src/core/util/event_id.test.ts b/packages-exp/auth-exp/src/core/util/event_id.test.ts new file mode 100644 index 00000000000..510b66b5c25 --- /dev/null +++ b/packages-exp/auth-exp/src/core/util/event_id.test.ts @@ -0,0 +1,33 @@ +/** + * @license + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import { _generateEventId } from './event_id'; + +describe('core/util/event_id', () => { + it('sub-15 digit id', () => { + expect(_generateEventId('', 10)).to.have.length(10); + }); + + it('15 digit id', () => { + expect(_generateEventId('', 15)).to.have.length(15); + }); + + it('above-15 digit id', () => { + expect(_generateEventId('', 20)).to.have.length(20); + }); +}); diff --git a/packages-exp/auth-exp/src/core/util/event_id.ts b/packages-exp/auth-exp/src/core/util/event_id.ts index ef5abe1839c..6e2e02fca63 100644 --- a/packages-exp/auth-exp/src/core/util/event_id.ts +++ b/packages-exp/auth-exp/src/core/util/event_id.ts @@ -15,6 +15,10 @@ * limitations under the License. */ -export function _generateEventId(prefix?: string): string { - return `${prefix ? prefix : ''}${Math.floor(Math.random() * 1000000000)}`; +export function _generateEventId(prefix = '', digits = 10): string { + let random = ''; + for (let i = 0; i < digits; i++) { + random += Math.floor(Math.random() * 10); + } + return prefix + random; } diff --git a/packages-exp/auth-exp/src/platform_browser/messagechannel/sender.ts b/packages-exp/auth-exp/src/platform_browser/messagechannel/sender.ts index f8f4147c52a..1452c9857f7 100644 --- a/packages-exp/auth-exp/src/platform_browser/messagechannel/sender.ts +++ b/packages-exp/auth-exp/src/platform_browser/messagechannel/sender.ts @@ -15,6 +15,7 @@ * limitations under the License. */ +import { _generateEventId } from '../../core/util/event_id'; import { _SenderRequest, _EventType, @@ -32,10 +33,6 @@ interface MessageHandler { onMessage: EventListenerOrEventListenerObject; } -function generateEventId(prefix = '', digits = 20): string { - return `${prefix}${Math.floor(Math.random() * Math.pow(10, digits))}`; -} - /** * Interface for sending messages and waiting for a completion response. * @@ -91,7 +88,7 @@ export class Sender { let completionTimer: any; let handler: MessageHandler; return new Promise<_ReceiverMessageResponse>((resolve, reject) => { - const eventId = generateEventId(); + const eventId = _generateEventId('', 20); messageChannel.port1.start(); const ackTimer = setTimeout(() => { reject(new Error(_MessageError.UNSUPPORTED_EVENT));