module import Config from './config' ; import { JWT } from './twitch' ; since export class Environment { public environment : string ; } ignore const ProductionEnvironment = { environment : 'production' } ; ignore const AdministrationEnvironment = { environment : 'production' } ; ignore const SandboxDevEnvironment = { environment : 'sandbox' } ; ignore const SandboxAdministrationEnvironment = { environment : 'sandbox' } ; ignore const SandboxTwitchEnvironment = { environment : 'sandbox' } ; ignore const ServerEnvironment = { environment : 'server' } ; ignore const TestingEnvironment = { environment : 'testing' } ; since deprecated Use  Util . Environments instead.
  ignore export const ENVIRONMENTS = { ADMIN : AdministrationEnvironment , PRODUCTION : ProductionEnvironment , SANDBOX_ADMIN : SandboxAdministrationEnvironment , SANDBOX_DEV : SandboxDevEnvironment , SANDBOX_TWITCH : SandboxTwitchEnvironment , SERVER : ServerEnvironment , TESTING : TestingEnvironment } ; export interface ConsolePrintOptions { type ? : string ; boxed ? : boolean ; style ? : string ; } /**
 * A collection of static utility functions, available at  Muxy . Util .
 *
 *  example export default class Util { since type { Object } static get Environments ( ) { return this . availableEnvironments ; } public static overrideEnvironment ? : Environment ; public static registerEnvironment ( key , env ) { this . availableEnvironments [ key ] = env ; } since param { string } err returns { Promise < string > } public static errorPromise ( err ) { return Promise . reject ( err ) ; } since ignore param { string [ ] } lines public static widestLine ( lines ) { return Math . max . apply ( null , lines . map ( x => x . length ) ) ; } since ignore param { string [ ] } lines returns { string } public static asciiBox ( lines ) { const contentWidth = Util . widestLine ( lines ) ; const intro = `${ ' ' . repeat ( contentWidth / 2 ) }🦊` ; const out = [ intro ] ; out . push ( `┌${ '─' . repeat ( contentWidth + 2 ) }┐` ) ; lines . forEach ( line => { const paddingRight = ' ' . repeat ( contentWidth - line . length ) ; out . push ( `| ${ line }${ paddingRight } |` ) ; } ) ; out . push ( `└${ '─' . repeat ( contentWidth + 2 ) }┘` ) ; return out ; } since ignore public static isWindowFramed ( overrideWindow ? ) { let vWindow ; if ( typeof window !== 'undefined' ) { vWindow = window ; } if ( overrideWindow ) { vWindow = overrideWindow ; } const isNotChildWindow = ! vWindow . opener ; const windowTop = vWindow . top && vWindow != vWindow . top ; const windowParent = vWindow . parent && vWindow != vWindow . parent ; const hasWindowAncestors = ! ! ( windowTop || windowParent ) ; return isNotChildWindow && hasWindowAncestors ; } /**
     * currentEnvironment uses the hostname and available info to determine in what
     * environment the SDK is running. Possible values are available in  Util . Environments .
     *  since returns { string } public static currentEnvironment ( overrideWindow ? ) { let vWindow ; if ( typeof window !== 'undefined' ) { vWindow = window ; } if ( overrideWindow ) { vWindow = overrideWindow ; } if ( Util . overrideEnvironment ) { return Util . overrideEnvironment ; } const otherEnv = Config . OtherEnvironmentCheck ( vWindow ) ; if ( otherEnv !== undefined ) { return otherEnv as Environment ; } try { if ( typeof module !== 'undefined' && module . exports && typeof vWindow === 'undefined' ) { return ENVIRONMENTS . SERVER ; } if ( ! Util . isWindowFramed ( vWindow ) ) { const urlParams = new URLSearchParams ( window . location . search ) ; if ( urlParams . get ( 'muxyAdminInterface' ) || vWindow . name === 'SandboxAdmin' ) { return ENVIRONMENTS . SANDBOX_ADMIN ; } return ENVIRONMENTS . SANDBOX_DEV ; } if ( vWindow . name === 'Admin' ) { return ENVIRONMENTS . ADMIN ; } const iFrameUrlParams = new URLSearchParams ( window . location . search ) ; if ( iFrameUrlParams . get ( 'muxyAdminInterface' ) || vWindow . name === 'SandboxAdmin' ) { return ENVIRONMENTS . SANDBOX_ADMIN ; } if ( vWindow . location . origin . indexOf ( '.ext-twitch.tv' ) !== - 1 ) { return ENVIRONMENTS . PRODUCTION ; } if ( vWindow . document . referrer && vWindow . document . referrer . indexOf ( 'twitch.tv' ) !== - 1 ) { return ENVIRONMENTS . SANDBOX_TWITCH ; } if ( ( vWindow as any ) . testing ) { return ENVIRONMENTS . TESTING ; } } catch ( err ) { Util . consolePrint ( err . toString ( ) , { type : 'error' } ) ; } return ENVIRONMENTS . SANDBOX_DEV ; } since public param { string | string [ ] } lines param { object ? } options param { boolean } options . boxed param { string } options . style param { string } options . type example public static consolePrint ( lines , options = { } ) { if ( ! lines || Util . currentEnvironment ( ) === Util . Environments . Production ) { return ; } let style = 'font-family: monospace;' ; let lineArr = Array . isArray ( lines ) ? lines : lines . split ( '\n' ) ; const type = options . type || 'log' ; if ( options . boxed ) { lineArr = Util . asciiBox ( lineArr ) ; } if ( options . style ) { style += options . style ; } if ( Util . currentEnvironment ( ) === Util . Environments . Server ) { ( console as any ) [ type ] . call ( this , lineArr . join ( '\n' ) ) ; } else { ( console as any ) [ type ] . call ( this , `%c${ lineArr . join ( '\n' ) }` , style ) ; } } since private param { string } input param { string } pattern return public static eventPatternMatch ( input , pattern ) { const inputParts = input . split ( ':' ) ; const patternParts = pattern . split ( ':' ) ; if ( inputParts . length !== patternParts . length ) { return false ; } for ( let i = 0 ; i < inputParts . length ; i += 1 ) { if ( inputParts [ i ] !== patternParts [ i ] && patternParts [ i ] !== '*' ) { return false ; } } return true ; } since public param { any } value param { string } type throws public static forceType ( value , type ) { const types = [ ] . concat ( type ) ; const typeString = typeof value ; if ( types . indexOf ( typeString ) === - 1 ) { throw new TypeError ( `expected '${ typeString }' to be one of [${ types }]` ) ; } } public return { TwitchEnvironment } public static getTwitchEnvironment ( ) { const url = new URL ( window . location . search ) ; const env = { anchor : null , language : null , mode : null , platform : null , state : null , version : null } ; const path = url . pathname ; const splitPath = path . split ( '/' ) ; if ( splitPath . length > 2 ) { env . version = splitPath [ 2 ] ; } const urlParams = url . searchParams ; env . anchor = urlParams . get ( 'anchor' ) ; env . language = urlParams . get ( 'language' ) ; env . mode = urlParams . get ( 'mode' ) ; env . platform = urlParams . get ( 'platform' ) ; env . state = urlParams . get ( 'state' ) ; return env ; } param { Object } jwt public static extractJWTInfo ( jwt ) { try { const splitToken = jwt . split ( '.' ) ; if ( splitToken . length === 3 ) { return JSON . parse ( window . atob ( splitToken [ 1 ] ) ) as JWT ; } } catch ( err ) { throw new Error ( 'Failed to parse JWT' ) ; } } private static availableEnvironments : { [ key ] : Environment ; } = { Admin : AdministrationEnvironment , Production : ProductionEnvironment , SandboxAdmin : SandboxAdministrationEnvironment , SandboxDev : SandboxDevEnvironment , SandboxTwitch : SandboxTwitchEnvironment , Server : ServerEnvironment , Testing : TestingEnvironment } ; } /**
 * The response from  getTwitchEnvironment .
 *
 *  typedef TwitchEnvironment property { string } anchor property { string } language property { string } mode property { string } platform property { string } state property { string } version export interface TwitchEnvironment { anchor : string ; language : string ; mode : string ; platform : string ; state : string ; version : string ; } ignore export const consolePrint = Util . consolePrint ; ignore export const forceType = Util . forceType ; ignore export const eventPatternMatch = Util . eventPatternMatch ; ignore export const CurrentEnvironment = Util . currentEnvironment ; ignore export const errorPromise = Util . errorPromise ;