Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

NPM cache: migrate ddb to s3 #3

Merged
merged 16 commits into from
Apr 30, 2020
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
end_of_line = lf
edgarpoce marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/coverage
docs
node_modules
.vscode
edgarpoce marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
"coverageThreshold": {
"global": {
"branches": 80,
"branches": 70,
"functions": 80,
"lines": 80,
"statements": -10
"statements": 80
edgarpoce marked this conversation as resolved.
Show resolved Hide resolved
}
},
"roots": [
Expand Down
148 changes: 147 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 53 additions & 32 deletions serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,6 @@ async function getConfig() {
"Type": "AWS::S3::Bucket",
"DeletionPolicy": "Retain"
}
const npmTableResourceName = "NpmTable";
npmResources[npmTableResourceName] = {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"AttributeDefinitions": [
{
"AttributeName": "PackageName",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "PackageName",
"KeyType": "HASH"
}
],
"BillingMode": "PAY_PER_REQUEST"
}
};

const npmFunctions = {
"npmHyphen": {
Expand All @@ -95,21 +76,41 @@ async function getConfig() {
"npmPackage": {
"handler": "src/npm/package/package.handler",
"environment": {
"DDB_TABLE": {
"Ref": npmTableResourceName
}
"BUCKET_NAME": {
"Ref": npmStorageBucketResourceName
},
"NPM_CACHE_DOWNLOAD_URI": {
"Fn::Join": [
"",
["https://",
{"Ref": "ApiGatewayRestApi"},
".execute-api.",
{"Ref": "AWS::Region"},
".",
{"Ref": "AWS::URLSuffix"},
"/${opt:stage}/npm-dlredirect"]
]
}
},
"iamRoleStatements": [
{
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem"
"s3:GetObject",
"s3:PutObject"
],
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
npmTableResourceName,
"Arn"
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
npmStorageBucketResourceName,
"Arn"
]
},
"/*"
]
]
}
}
Expand All @@ -129,8 +130,8 @@ async function getConfig() {
"handler": "src/npm/autoupdate/autoupdate.handler",
"timeout": 900,
"environment": {
"DDB_TABLE": {
"Ref": npmTableResourceName
"BUCKET_NAME": {
"Ref": npmStorageBucketResourceName
},
"NPM_CACHE_DOWNLOAD_URI": {
"Fn::Join": [
Expand All @@ -148,16 +149,36 @@ async function getConfig() {
"iamRoleStatements": [
{
"Action": [
"dynamodb:PutItem",
"dynamodb:Scan"
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
npmTableResourceName,
npmStorageBucketResourceName,
"Arn"
]
}
},
{
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Effect": "Allow",
"Resource": {
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
npmStorageBucketResourceName,
"Arn"
]
},
"/*"
]
]
}
}
],
"events": [
Expand Down
23 changes: 23 additions & 0 deletions src/npm/NpmApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { S3 } from "aws-sdk";
import { S3Cache } from "./S3Cache";
import { NpmPackage } from "./NpmPackage";

const BUCKET_NAME: string = process.env.BUCKET_NAME!;
const NPM_CACHE_DOWNLOAD_URI = process.env.NPM_CACHE_DOWNLOAD_URI!;

/*
* Entry point to access any service of the NPM artifact cacher.
* It creates all instances and provides a single point of access
* to lambda handlers.
*/
export class NpmApp {
readonly s3: S3 = new S3();
readonly s3Cache = new S3Cache(this.s3, BUCKET_NAME);

getNpmPackage(packageName: string) {
return new NpmPackage(this.s3Cache, NPM_CACHE_DOWNLOAD_URI, packageName);
}
}

// app singleton
export const app = new NpmApp();
Loading