Repository: cypress-io/cypress
File: packages/server/lib/cloud/studio/ensure_studio_bundle.ts (TypeScript)

Code before:

const DOWNLOAD_TIMEOUT = 30000

/**
 * Ensures that the studio bundle is downloaded and extracted into the given path
 * @param options - The options for the ensure studio bundle operation
 * @param options.studioUrl - The URL of the studio bundle
 * @param options.projectId - The project ID of the studio bundle
 * @param options.studioPath - The path to extract the studio bundle to
 * @param options.downloadTimeoutMs - The timeout for the download operation
 */
export const ensureStudioBundle = async ({
  studioUrl,
  projectId,
  studioPath,
  downloadTimeoutMs = DOWNLOAD_TIMEOUT,
}: EnsureStudioBundleOptions): Promise<Record<string, string>> => {
  const bundlePath = path.join(studioPath, 'bundle.tar')

  // First remove studioPath to ensure we have a clean slate
  await remove(studioPath)
  await ensureDir(studioPath)

  let timeoutId: NodeJS.Timeout

  const responseManifestSignature = await Promise.race([
    getStudioBundle({
      studioUrl,
      bundlePath,
    }),
    new Promise((_, reject) => {
      timeoutId = setTimeout(() => {
        reject(new Error('Studio bundle download timed out'))
      }, downloadTimeoutMs)
    }),
  ]).finally(() => {
    clearTimeout(timeoutId)
  }) as string

  await tar.extract({
    file: bundlePath,
    cwd: studioPath,
  })

  const manifestPath = path.join(studioPath, 'manifest.json')

  if (!(await pathExists(manifestPath))) {
    throw new Error('Unable to find studio manifest')
  }

  const manifestContents = await readFile(manifestPath, 'utf8')

Review comment (suggestion):
[nitpick] Consider explicitly annotating the type of 'responseManifestSignature' since it is cast to string, which can improve code clarity.
```suggestion
  const responseManifestSignature: string = await Promise.race([
```

Code after:

const DOWNLOAD_TIMEOUT = 30000

/**
 * Ensures that the studio bundle is downloaded and extracted into the given path
 * @param options - The options for the ensure studio bundle operation
 * @param options.studioUrl - The URL of the studio bundle
 * @param options.projectId - The project ID of the studio bundle
 * @param options.studioPath - The path to extract the studio bundle to
 * @param options.downloadTimeoutMs - The timeout for the download operation
 */
export const ensureStudioBundle = async ({
  studioUrl,
  projectId,
  studioPath,
  downloadTimeoutMs = DOWNLOAD_TIMEOUT,
}: EnsureStudioBundleOptions): Promise<Record<string, string>> => {
  const bundlePath = path.join(studioPath, 'bundle.tar')

  // First remove studioPath to ensure we have a clean slate
  await remove(studioPath)
  await ensureDir(studioPath)

  let timeoutId: NodeJS.Timeout

  const responseManifestSignature: string = await Promise.race([
    getStudioBundle({
      studioUrl,
      bundlePath,
    }),
    new Promise((_, reject) => {
      timeoutId = setTimeout(() => {
        reject(new Error('Studio bundle download timed out'))
      }, downloadTimeoutMs)
    }),
  ]).finally(() => {
    clearTimeout(timeoutId)
  }) as string

  await tar.extract({
    file: bundlePath,
    cwd: studioPath,
  })

  const manifestPath = path.join(studioPath, 'manifest.json')

  if (!(await pathExists(manifestPath))) {
    throw new Error('Unable to find studio manifest')
  }

  const manifestContents = await readFile(manifestPath, 'utf8')