Skip to content

Commit

Permalink
[Auth] Break down event ID digits to a max Math.random() digit count (#…
Browse files Browse the repository at this point in the history
…5098)

* [Auth] Break down event ID digits to a max Math.random() digit count

* License/formatting

* src/core/util/event_id.ts
  • Loading branch information
sam-gc authored Jul 2, 2021
1 parent 99526ac commit 3827d96
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
33 changes: 33 additions & 0 deletions packages-exp/auth-exp/src/core/util/event_id.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 6 additions & 2 deletions packages-exp/auth-exp/src/core/util/event_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { _generateEventId } from '../../core/util/event_id';
import {
_SenderRequest,
_EventType,
Expand All @@ -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.
*
Expand Down Expand Up @@ -91,7 +88,7 @@ export class Sender {
let completionTimer: any;
let handler: MessageHandler;
return new Promise<_ReceiverMessageResponse<T>>((resolve, reject) => {
const eventId = generateEventId();
const eventId = _generateEventId('', 20);
messageChannel.port1.start();
const ackTimer = setTimeout(() => {
reject(new Error(_MessageError.UNSUPPORTED_EVENT));
Expand Down

0 comments on commit 3827d96

Please sign in to comment.