const got = require('got');

module.exports = {
  getDigest,
  getTags,
};

const map = new Map();

async function getDigest(name, tag = 'latest') {
  const repository = name.includes('/') ? name : `library/${name}`;
  try {
    const authUrl = `https://auth.docker.io/token?service=registry.docker.io&scope=repository:${
      repository
    }:pull`;
    logger.debug(`Obtaining docker registry token for ${repository}`);
    const { token } = (await got(authUrl, { cache: map, json: true })).body;
    if (!token) {
      logger.warn('Failed to obtain docker registry token');
      return null;
    }
    logger.debug('Got docker registry token');
    const url = `https://index.docker.io/v2/${repository}/manifests/${tag}`;
    const headers = {
      Authorization: `Bearer ${token}`,
      Accept: 'application/vnd.docker.distribution.manifest.v2+json',
    };
    const digest = (await got(url, { cache: map, json: true, headers }))
      .headers['docker-content-digest'];
    logger.debug({ digest }, 'Got docker digest');
    return digest;
  } catch (err) {
    logger.info({ err, name, tag }, 'Error looking up docker image digest');
    return null;
  }
}

async function getTags(name) {
  const repository = name.includes('/') ? name : `library/${name}`;
  try {
    const authUrl = `https://auth.docker.io/token?service=registry.docker.io&scope=repository:${
      repository
    }:pull`;
    logger.debug(`Obtaining docker registry token for ${repository}`);
    const { token } = (await got(authUrl, { cache: map, json: true })).body;
    if (!token) {
      logger.warn('Failed to obtain docker registry token');
      return null;
    }
    logger.debug('Got docker registry token');
    const url = `https://index.docker.io/v2/${repository}/tags/list?n=10000`;
    const headers = {
      Authorization: `Bearer ${token}`,
      Accept: 'application/vnd.docker.distribution.manifest.v2+json',
    };
    const res = await got(url, { cache: map, json: true, headers });
    logger.debug({ tags: res.body.tags }, 'Got docker tags');
    return res.body.tags;
  } catch (err) {
    logger.warn({ err, name }, 'Error getting docker image tags');
    return null;
  }
}

@@ -73,32 +73,8 @@
 %7D;%0A%0A
-const map = new Map();%0A%0A
 asyn
@@ -431,36 +431,24 @@
 t(authUrl, %7B
- cache: map,
  json: true 
@@ -856,36 +856,24 @@
 t got(url, %7B
- cache: map,
  json: true,
@@ -884,23 +884,16 @@
 ders %7D))
-%0A      
 .headers
@@ -893,16 +893,23 @@
 headers%5B
+%0A      
 'docker-
@@ -923,16 +923,21 @@
 -digest'
+%0A    
 %5D;%0A    l
@@ -1464,36 +1464,24 @@
 t(authUrl, %7B
- cache: map,
  json: true 
@@ -1850,32 +1850,32 @@
 2+json',%0A    %7D;%0A
+
     const res = 
@@ -1894,20 +1894,8 @@
 l, %7B
- cache: map,
  jso

Revert "feat: docker registry cache (#1210)" (#1211)