{"version":3,"file":"notification.mjs","sources":["../../src/services/notification.ts"],"sourcesContent":["import EventSource from 'eventsource';\nimport EventEmitter from 'node:events';\nimport type { CLIContext, CloudCliConfig } from '../types';\n\ntype Event = {\n  type: string;\n  data: string;\n  lastEventId: string;\n  origin: string;\n};\n\ntype DeploymentNotificationData = {\n  event:\n    | 'deploymentFailed'\n    | 'deploymentCompleted'\n    | 'environmentCreationFailed'\n    | 'environmentCreationCompleted';\n  userId: string;\n  projectName: string;\n  environmentName: string;\n  createdAt: string;\n  message?: string;\n};\n\nexport function notificationServiceFactory({ logger }: CLIContext) {\n  return (url: string, token: string, cliConfig: CloudCliConfig) => {\n    const CONN_TIMEOUT = Number(cliConfig.notificationsConnectionTimeout);\n    const eventEmitter = new EventEmitter();\n\n    const es = new EventSource(url, {\n      headers: {\n        Authorization: `Bearer ${token}`,\n      },\n    });\n    let timeoutId: NodeJS.Timeout;\n\n    const resetTimeout = () => {\n      clearTimeout(timeoutId);\n      timeoutId = setTimeout(() => {\n        logger.log(\n          'We were unable to connect to the server at this time. This could be due to a temporary issue. Please try again in a moment.'\n        );\n        es.close();\n        eventEmitter.emit('connectionTimedOut');\n      }, CONN_TIMEOUT); // 5 minutes\n    };\n\n    es.onopen = () => {\n      resetTimeout();\n    };\n    es.onmessage = (event: Event) => {\n      resetTimeout();\n      if (!event.data) {\n        return;\n      }\n      const data: DeploymentNotificationData = JSON.parse(event.data);\n\n      if (data.message) {\n        logger.log(data.message);\n      }\n\n      // Close connection when a specific event is received\n      if (\n        data.event === 'deploymentCompleted' ||\n        data.event === 'deploymentFailed' ||\n        data.event === 'environmentCreationFailed' ||\n        data.event === 'environmentCreationCompleted'\n      ) {\n        clearTimeout(timeoutId);\n        es.close();\n      }\n      eventEmitter.emit(data.event, data);\n    };\n\n    const waitForEnvironmentCreation = (environmentName: string) => {\n      return new Promise((resolve, reject) => {\n        eventEmitter.on('environmentCreationCompleted', (data: DeploymentNotificationData) => {\n          if (data.environmentName !== environmentName) {\n            return;\n          }\n          resolve('Environment created successfully');\n          eventEmitter.removeAllListeners('environmentCreationCompleted');\n        });\n        eventEmitter.on('environmentCreationFailed', (data: DeploymentNotificationData) => {\n          if (data.environmentName !== environmentName) {\n            return;\n          }\n          reject(new Error(`Environment creation failed`, { cause: 'EnvironmentCreationFailed' }));\n          eventEmitter.removeAllListeners('environmentCreationFailed');\n        });\n\n        eventEmitter.on('connectionTimedOut', () => {\n          reject(new Error('Connection timed out'));\n        });\n      });\n    };\n    const close = () => {\n      clearTimeout(timeoutId);\n      es.close();\n      eventEmitter.removeAllListeners();\n    };\n    return { waitForEnvironmentCreation, close };\n  };\n}\n"],"names":["notificationServiceFactory","logger","url","token","cliConfig","CONN_TIMEOUT","Number","notificationsConnectionTimeout","eventEmitter","EventEmitter","es","EventSource","headers","Authorization","timeoutId","resetTimeout","clearTimeout","setTimeout","log","close","emit","onopen","onmessage","event","data","JSON","parse","message","waitForEnvironmentCreation","environmentName","Promise","resolve","reject","on","removeAllListeners","Error","cause"],"mappings":";;;AAwBO,SAASA,0BAAAA,CAA2B,EAAEC,MAAM,EAAc,EAAA;IAC/D,OAAO,CAACC,KAAaC,KAAeC,EAAAA,SAAAA,GAAAA;QAClC,MAAMC,YAAAA,GAAeC,MAAOF,CAAAA,SAAAA,CAAUG,8BAA8B,CAAA;AACpE,QAAA,MAAMC,eAAe,IAAIC,YAAAA,EAAAA;QAEzB,MAAMC,EAAAA,GAAK,IAAIC,WAAAA,CAAYT,GAAK,EAAA;YAC9BU,OAAS,EAAA;gBACPC,aAAe,EAAA,CAAC,OAAO,EAAEV,KAAO,CAAA;AAClC;AACF,SAAA,CAAA;QACA,IAAIW,SAAAA;AAEJ,QAAA,MAAMC,YAAe,GAAA,IAAA;YACnBC,YAAaF,CAAAA,SAAAA,CAAAA;AACbA,YAAAA,SAAAA,GAAYG,UAAW,CAAA,IAAA;AACrBhB,gBAAAA,MAAAA,CAAOiB,GAAG,CACR,6HAAA,CAAA;AAEFR,gBAAAA,EAAA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