Repository: jiahaog/nativefier
File: src/infer/inferUserAgent.ts (TypeScript)

Code before:
  }
  const chromeVersion = electronVersionToChromeVersion[electronVersion];
  log.debug(
    `Associated electron v${electronVersion} to chrome v${chromeVersion}`,
  );
  return chromeVersion;
}

export function getUserAgentString(
  chromeVersion: string,
  platform: string,
): string {
  let userAgent: string;
  switch (platform) {
    case 'darwin':
    case 'mas':
      userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    case 'win32':
      userAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    case 'linux':
      userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    case 'all':
      userAgent = `Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    default:
      throw new Error(
        'Error invalid platform specified to getUserAgentString()',
      );
  }
  log.debug(
    `Given chrome ${chromeVersion} on ${platform},`,
    `using user agent: ${userAgent}`,
  );
  return userAgent;
}

export async function inferUserAgent(
  electronVersion: string,
  platform: string,
  url = ELECTRON_VERSIONS_URL,
): Promise<string> {
  log.debug(
    `Inferring user agent for electron ${electronVersion} / ${platform}`,
  );
  try {
    const chromeVersion = await getChromeVersionForElectronVersion(
      electronVersion,
      url,

Review comment (performance):
Why should `all` end up as AppleWebkit?

This, plus the other icon format problem, makes me not like this part of your changes. I understand your intention, but this `all` implementation feels rushed, with potential to subtly break quite a few things. 

Care to either do more refactor or abandon it? Also, in the first place, why do you need it? Is it just for the Docker pre-caching? (Of which, as commented above, I'm not super convinced about, as it seems too space hungry, consuming valuable time and space e.g. for `ia32`, `armv7l` or `mas` builds that probably <1% of our users actually use, and these users with specific needs can customize the Dockerfile to their needs). The existing repeated calls to nativefier in the Dockerfile to cache linux,mac,win were totally fine.

I'll listen, but it seems like a feature that is both problematic and [YAGNI](https://en.wikipedia.org/wiki/YAGNI) anyway, so if it's just for the Docker build I'd like it removed.

Code after:
  }
  const chromeVersion = electronVersionToChromeVersion[electronVersion];
  log.debug(
    `Associated electron v${electronVersion} to chrome v${chromeVersion}`,
  );
  return chromeVersion;
}

export function getUserAgentString(
  chromeVersion: string,
  platform: string,
): string {
  let userAgent: string;
  switch (platform) {
    case 'darwin':
    case 'mas':
      userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    case 'win32':
      userAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    case 'linux':
      userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
      break;
    default:
      throw new Error(
        'Error invalid platform specified to getUserAgentString()',
      );
  }
  log.debug(
    `Given chrome ${chromeVersion} on ${platform},`,
    `using user agent: ${userAgent}`,
  );
  return userAgent;
}

export async function inferUserAgent(
  electronVersion: string,
  platform: string,
  url = ELECTRON_VERSIONS_URL,
): Promise<string> {
  log.debug(
    `Inferring user agent for electron ${electronVersion} / ${platform}`,
  );
  try {
    const chromeVersion = await getChromeVersionForElectronVersion(
      electronVersion,
      url,
    );
    return getUserAgentString(chromeVersion, platform);
  } catch (e) {