All files / src get.js

100% Statements 12/12
100% Branches 2/2
100% Functions 1/1
100% Lines 12/12
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 37 38 39 40 41 42        4x 4x 4x   4x 4x   1x         3x 2x 2x 1x         1x                 1x                
import npm from './adapters/npm';
import S3 from './adapters/s3';
 
export default async ({ pathParameters }, context, callback) => {
  const { registry, bucket, region } = process.env;
  const name = `${decodeURIComponent(pathParameters.name)}`;
  const storage = new S3({ region, bucket });
 
  try {
    const pkgBuffer = await storage.get(`${name}/index.json`);
 
    return callback(null, {
      statusCode: 200,
      body: pkgBuffer.toString(),
    });
  } catch (storageError) {
    if (storageError.code === 'NoSuchKey') {
      try {
        const data = await npm(registry, name);
        return callback(null, {
          statusCode: 200,
          body: JSON.stringify(data),
        });
      } catch (npmError) {
        return callback(null, {
          statusCode: npmError.status,
          body: JSON.stringify({
            error: npmError.message,
          }),
        });
      }
    }
 
    return callback(null, {
      statusCode: 500,
      body: JSON.stringify({
        error: storageError.message,
      }),
    });
  }
};