{"version":3,"file":"build-logs.mjs","sources":["../../src/services/build-logs.ts"],"sourcesContent":["import EventSource from 'eventsource';\nimport { CLIContext, type CloudCliConfig } from '../types';\n\nconst buildLogsServiceFactory = ({ logger }: CLIContext) => {\n  return async (url: string, token: string, cliConfig: CloudCliConfig) => {\n    const CONN_TIMEOUT = Number(cliConfig.buildLogsConnectionTimeout);\n    const MAX_RETRIES = Number(cliConfig.buildLogsMaxRetries);\n\n    return new Promise((resolve, reject) => {\n      let timeoutId: NodeJS.Timeout | null = null;\n      let retries = 0;\n\n      const connect = (url: string) => {\n        const logsSpinner = logger.spinner('Connecting to the server to get build logs\\n').start();\n        logsSpinner.indent = 1;\n        const es = new EventSource(`${url}`, {\n          headers: {\n            Authorization: `Bearer ${token}`,\n          },\n        });\n\n        const clearExistingTimeout = () => {\n          if (timeoutId) {\n            clearTimeout(timeoutId);\n          }\n        };\n\n        const resetTimeout = () => {\n          clearExistingTimeout();\n          timeoutId = setTimeout(() => {\n            if (logsSpinner.isSpinning) {\n              logsSpinner.fail(\n                'We were unable to connect to the server to get build logs at this time. This could be due to a temporary issue.'\n              );\n            }\n            es.close();\n            reject(new Error('Connection timed out'));\n          }, CONN_TIMEOUT);\n        };\n\n        es.onopen = resetTimeout;\n\n        es.addEventListener('finished', (event) => {\n          const data = JSON.parse(event.data);\n          logger.log(data.msg);\n          es.close();\n          clearExistingTimeout();\n          resolve(null);\n        });\n\n        es.addEventListener('log', (event) => {\n          if (logsSpinner.isSpinning) {\n            logsSpinner.succeed();\n          }\n          resetTimeout();\n          const data = JSON.parse(event.data);\n          logger.log(data.msg);\n        });\n\n        es.onerror = async () => {\n          retries += 1;\n          if (retries > MAX_RETRIES) {\n            logsSpinner.fail(\n              'We were unable to connect to the server to get build logs at this time.'\n            );\n            es.close();\n            clearExistingTimeout(); // Important to clear the event loop from remaining timeout - avoid to wait for nothing while the timeout is running\n            reject(new Error('Max retries reached'));\n          }\n        };\n      };\n\n      connect(url);\n    });\n  };\n};\n\nexport { buildLogsServiceFactory };\n"],"names":["buildLogsServiceFactory","logger","url","token","cliConfig","CONN_TIMEOUT","Number","buildLogsConnectionTimeout","MAX_RETRIES","buildLogsMaxRetries","Promise","resolve","reject","timeoutId","retries","connect","logsSpinner","spinner","start","indent","es","EventSource","headers","Authorization","clearExistingTimeout","clearTimeout","resetTimeout","setTimeout","isSpinning","fail","close","Error","onopen","addEventListener","event","data","JSON","parse","log","msg","succeed","onerror"],"mappings":";;AAGA,MAAMA,uBAAAA,GAA0B,CAAC,EAAEC,MAAM,EAAc,GAAA;IACrD,OAAO,OAAOC,KAAaC,KAAAA,EAAeC,SAAAA,GAAAA;QACxC,MAAMC,YAAAA,GAAeC,MAAAA,CAAOF,SAAAA,CAAUG,0BAA0B,CAAA;QAChE,MAAMC,WAAAA,GAAcF,MAAAA,CAAOF,SAAAA,CAAUK,mBAAmB,CAAA;QAExD,OAAO,IAAIC,OAAAA,CAAQ,CAACC,OAAAA,EAASC,MAAAA,GAAAA;AAC3B,YAAA,IAAIC,SAAAA,GAAmC,IAAA;AACvC,YAAA,IAAIC,OAAAA,GAAU,CAAA;AAEd,YAAA,MAAMC,UAAU,CAACb,GAAAA,GAAAA;AACf,gBAAA,MAAMc,WAAAA,GAAcf,MAAAA,CAAOgB,OAAO,CAAC,gDAAgDC,KAAK,EAAA;AACxFF,gBAAAA,WAAAA,CAAYG,MAAM,GAAG,CAAA;AACrB,gBAAA,MAAMC,EAAAA,GAAK,IAAIC,WAAAA,CAAY,CAAA,EAAGnB,KAAK,EAAE;oBACnCoB,OAAAA,EAAS;wBACPC,aAAAA,EAAe,CAAC,OAAO,EAAEpB,KAAAA,CAAAA;AAC3B;AACF,iBAAA,CAAA;AAEA,gBAAA,MAAMqB,oBAAAA,GAAuB,IAAA;AAC3B,oBAAA,IAAIX,SAAAA,EAAW;wBACbY,YAAAA,CAAaZ,SAAAA,CAAAA;AACf,oBAAA;AACF,gBAAA,CAAA;AAEA,gBAAA,MAAMa,YAAAA,GAAe,IA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