import * as crypto from 'crypto' ; export class RolloutTracker { private forcedRollouts = new Map < string , boolean > ( ) ; constructor ( private instanceId ) { } forceRollout ( rolloutId , enabled ) { this . forcedRollouts . set ( rolloutId , enabled ) ; } isRolloutEnabled ( rolloutId , percentage ) { if ( this . forcedRollouts . has ( rolloutId ) ) { return this . forcedRollouts . get ( rolloutId ) ; } if ( percentage < 0 || percentage > 100 ) { throw new Error ( `Expected 0 <= percentage <= 100. Found ${ percentage }` ) ; } if ( Math . floor ( percentage ) !== percentage ) { throw new Error ( `Expected percentage to be an integer. Found ${ percentage }` ) ; } const hash = crypto . createHash ( 'md5' ) ; hash . update ( this . instanceId ) ; hash . update ( rolloutId ) ; const buffer = hash . digest ( ) ; return 100 * buffer [ 0 ] < percentage * 256 ; } }