Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Create separate module for S3 client
Browse files Browse the repository at this point in the history
  • Loading branch information
jvarho committed Apr 28, 2021
1 parent f18d6a6 commit 2b880a3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/libs/lambda-at-edge/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ export default [
{ filename: "api-handler", minify: false },
{ filename: "api-handler", minify: true },
{ filename: "image-handler", minify: false },
{ filename: "image-handler", minify: true }
{ filename: "image-handler", minify: true },
{ filename: "s3-client", minify: false },
{ filename: "s3-client", minify: true }
].map(generateConfig);
11 changes: 10 additions & 1 deletion packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ class Builder {
* @param shouldMinify
*/
async processAndCopyHandler(
handlerType: "api-handler" | "default-handler" | "image-handler",
handlerType:
| "api-handler"
| "default-handler"
| "image-handler"
| "s3-client",
destination: string,
shouldMinify: boolean
) {
Expand Down Expand Up @@ -298,6 +302,11 @@ class Builder {
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR, "index.js"),
!!this.buildOptions.minifyHandlers
),
this.processAndCopyHandler(
"s3-client",
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR, "s3-client.js"),
!!this.buildOptions.minifyHandlers
),
this.buildOptions?.handler
? fse.copy(
join(this.nextConfigDir, this.buildOptions.handler),
Expand Down
10 changes: 3 additions & 7 deletions packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ const handleOriginResponse = async ({
const bucketName = domainName.replace(`.s3.${region}.amazonaws.com`, "");

// Lazily import only S3Client to reduce init times until actually needed
const { S3Client } = await import("@aws-sdk/client-s3/S3Client");
const { S3Client } = require("./s3-client.js");

const s3 = new S3Client({
region: request.origin?.s3?.region,
Expand Down Expand Up @@ -675,9 +675,7 @@ const handleOriginResponse = async ({
ContentType: "text/html",
CacheControl: "public, max-age=0, s-maxage=2678400, must-revalidate"
};
const { PutObjectCommand } = await import(
"@aws-sdk/client-s3/commands/PutObjectCommand"
);
const { PutObjectCommand } = require("./s3-client.js");
await Promise.all([
s3.send(new PutObjectCommand(s3JsonParams)),
s3.send(new PutObjectCommand(s3HtmlParams))
Expand Down Expand Up @@ -742,9 +740,7 @@ const handleOriginResponse = async ({
body: html
};
} else {
const { GetObjectCommand } = await import(
"@aws-sdk/client-s3/commands/GetObjectCommand"
);
const { GetObjectCommand } = require("./s3-client.js");
// S3 Body is stream per: https://github.com/aws/aws-sdk-js-v3/issues/1096
const getStream = await import("get-stream");

Expand Down
9 changes: 9 additions & 0 deletions packages/libs/lambda-at-edge/src/s3-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GetObjectCommand } from "@aws-sdk/client-s3/commands/GetObjectCommand";
import { PutObjectCommand } from "@aws-sdk/client-s3/commands/PutObjectCommand";
import { S3Client } from "@aws-sdk/client-s3/S3Client";

module.exports = {
GetObjectCommand,
PutObjectCommand,
S3Client
};
3 changes: 2 additions & 1 deletion packages/libs/lambda-at-edge/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"node_modules",
"./src/api-handler.ts",
"./src/default-handler.ts",
"./src/image-handler.ts"
"./src/image-handler.ts",
"./src/s3-client.ts"
]
}
7 changes: 6 additions & 1 deletion packages/libs/lambda-at-edge/tsconfig.bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
"allowJs": true,
"resolveJsonModule": true
},
"include": ["./src/default-handler.ts", "./src/api-handler.ts", "./src/image-handler.ts"]
"include": [
"./src/default-handler.ts",
"./src/api-handler.ts",
"./src/image-handler.ts",
"./src/s3-client.ts"
]
}

0 comments on commit 2b880a3

Please sign in to comment.