All files / src/get index.js

50% Statements 9/18
25% Branches 1/4
50% Functions 1/2
50% Lines 9/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x         1x                     2x 2x 2x   2x 2x       2x               2x      
require('../bootstrap');
 
import s3 from '../adapters/s3';
import fetch from 'node-fetch';
 
const npm = async (registry, name) => {
  const response = await fetch(`${registry}${name}`);
 
  if (!response.ok) {
    throw new Error(`[${response.status}] Could Not Get Package: ${registry}${name}`);
  }
 
  return response.json();
};
 
export default async ({ path }, context, callback) => {
  const { registry, bucket, region } = process.env;
  const name = `${decodeURIComponent(path.name)}`;
  const storage = new s3({ region, bucket });
 
  try {
    const body = await storage.get(`${name}/index.json`);
 
    return callback(JSON.parse(body.toString()));
  } catch (error) {
    Iif (error.code === 'NoSuchKey') {
      try {
        const data = await npm(registry, name);
        return callback(null, data);
      } catch (error) {
        return callback(error);
      }
    }
    return callback(error);
  }
};