|
| 1 | +/** |
| 2 | + * Copyright 2022, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { uuid } from '../../utils/fns'; |
| 18 | +import PersistentKeyValueCache from '../key_value_cache/persistentKeyValueCache'; |
| 19 | + |
| 20 | +export interface IVuidManager { |
| 21 | + readonly vuid: string; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Manager for creating, persisting, and retrieving a Visitor Unique Identifier |
| 26 | + */ |
| 27 | +export class VuidManager implements IVuidManager { |
| 28 | + /** |
| 29 | + * Unique key used within the persistent value cache against which to |
| 30 | + * store the VUID |
| 31 | + * @private |
| 32 | + */ |
| 33 | + private _keyForVuid = 'optimizely-vuid'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Prefix used as part of the VUID format |
| 37 | + * @private |
| 38 | + */ |
| 39 | + private readonly _prefix: string = `vuid_`; |
| 40 | + |
| 41 | + /** |
| 42 | + * Current VUID value being used |
| 43 | + * @private |
| 44 | + */ |
| 45 | + private _vuid: string; |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the current VUID value being used |
| 49 | + */ |
| 50 | + public get vuid(): string { |
| 51 | + return this._vuid; |
| 52 | + } |
| 53 | + |
| 54 | + private constructor() { |
| 55 | + this._vuid = ''; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Instance of the VUID Manager |
| 60 | + * @private |
| 61 | + */ |
| 62 | + private static _instance: VuidManager; |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets the current instance of the VUID Manager, initializing if needed |
| 66 | + * @param cache Caching mechanism to use for persisting the VUID outside working memory * |
| 67 | + * @returns An instance of VuidManager |
| 68 | + */ |
| 69 | + public static async instance(cache: PersistentKeyValueCache): Promise<VuidManager> { |
| 70 | + if (!this._instance) { |
| 71 | + this._instance = new VuidManager(); |
| 72 | + } |
| 73 | + |
| 74 | + if (!this._instance._vuid) { |
| 75 | + await this._instance.load(cache); |
| 76 | + } |
| 77 | + |
| 78 | + return this._instance; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Attempts to load a VUID from persistent cache or generates a new VUID |
| 83 | + * @param cache Caching mechanism to use for persisting the VUID outside working memory |
| 84 | + * @returns Current VUID stored in the VuidManager |
| 85 | + */ |
| 86 | + private async load(cache: PersistentKeyValueCache): Promise<string> { |
| 87 | + const cachedValue = await cache.get(this._keyForVuid); |
| 88 | + if (cachedValue && this.isVuid(cachedValue)) { |
| 89 | + this._vuid = cachedValue; |
| 90 | + } else { |
| 91 | + this._vuid = this.makeVuid(); |
| 92 | + await this.save(this._vuid, cache); |
| 93 | + } |
| 94 | + |
| 95 | + return this._vuid; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a new VUID |
| 100 | + * @returns A new visitor unique identifier |
| 101 | + */ |
| 102 | + private makeVuid(): string { |
| 103 | + const maxLength = 32; // required by ODP server |
| 104 | + |
| 105 | + // make sure UUIDv4 is used (not UUIDv1 or UUIDv6) since the trailing 5 chars will be truncated. See TDD for details. |
| 106 | + const uuidV4 = uuid(); |
| 107 | + const formatted = uuidV4.replace(/-/g, '').toLowerCase(); |
| 108 | + const vuidFull = `${(this._prefix)}${formatted}`; |
| 109 | + |
| 110 | + return (vuidFull.length <= maxLength) ? vuidFull : vuidFull.substring(0, maxLength); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Saves a VUID to a persistent cache |
| 115 | + * @param vuid VUID to be stored |
| 116 | + * @param cache Caching mechanism to use for persisting the VUID outside working memory |
| 117 | + */ |
| 118 | + private async save(vuid: string, cache: PersistentKeyValueCache): Promise<void> { |
| 119 | + await cache.set(this._keyForVuid, vuid); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Validates the format of a Visitor Unique Identifier |
| 124 | + * @param vuid VistorId to check |
| 125 | + * @returns *true* if the VisitorId is valid otherwise *false* for invalid |
| 126 | + */ |
| 127 | + private isVuid = (vuid: string): boolean => vuid.startsWith(this._prefix); |
| 128 | + |
| 129 | + /** |
| 130 | + * Function used in unit testing to reset the VuidManager |
| 131 | + * **Important**: This should not to be used in production code |
| 132 | + */ |
| 133 | + private static _reset(): void { |
| 134 | + this._instance._vuid = ''; |
| 135 | + } |
| 136 | +} |
0 commit comments