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

Commit aa0602f

Browse files
authored
chore(deps): update aws-sdk v3 dependencies (#1848)
* restructuring to static imports as well since it doesn't impact bundling/perf much
1 parent 47455f9 commit aa0602f

15 files changed

+1193
-1937
lines changed

packages/libs/aws-common/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
},
3434
"homepage": "https://github.com/serverless-nextjs/serverless-next.js#readme",
3535
"dependencies": {
36-
"@aws-sdk/client-s3": "^3.35.0",
37-
"@aws-sdk/client-sqs": "^3.35.0",
36+
"@aws-sdk/client-s3": "^3.37.0",
37+
"@aws-sdk/client-sqs": "^3.37.0",
3838
"@sls-next/core": "link:../core"
3939
},
4040
"devDependencies": {

packages/libs/aws-common/yarn.lock

+554-551
Large diffs are not rendered by default.

packages/libs/lambda-at-edge/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
"typescript": "^4.4.4"
6969
},
7070
"dependencies": {
71-
"@aws-sdk/client-s3": "3.35.0",
72-
"@aws-sdk/client-sqs": "3.35.0",
71+
"@aws-sdk/client-s3": "^3.37.0",
72+
"@aws-sdk/client-sqs": "^3.37.0",
7373
"@sls-next/aws-common": "link:../aws-common",
7474
"@sls-next/core": "link:../core",
7575
"@vercel/nft": "^0.16.1",

packages/libs/lambda-at-edge/src/default-handler.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { triggerStaticRegeneration } from "./lib/triggerStaticRegeneration";
5151
import { s3StorePage } from "./s3/s3StorePage";
5252
import { createRedirectResponse } from "@sls-next/core/dist/module/route/redirect";
5353
import { redirect } from "@sls-next/core/dist/module/handle/redirect";
54+
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
5455

5556
const basePath = RoutesManifestJson.basePath;
5657

@@ -423,9 +424,6 @@ const handleOriginResponse = async ({
423424
return await responsePromise;
424425
}
425426

426-
// Lazily import only S3Client to reduce init times until actually needed
427-
const { S3Client } = await import("@aws-sdk/client-s3/src/S3Client");
428-
429427
const s3 = new S3Client({
430428
region: request.origin?.s3?.region,
431429
maxAttempts: 3
@@ -436,9 +434,6 @@ const handleOriginResponse = async ({
436434
if (fallbackRoute.isStatic) {
437435
const file = fallbackRoute.file.slice("pages".length);
438436
const s3Key = `${s3BasePath}static-pages/${manifest.buildId}${file}`;
439-
const { GetObjectCommand } = await import(
440-
"@aws-sdk/client-s3/src/commands/GetObjectCommand"
441-
);
442437
// S3 Body is stream per: https://github.com/aws/aws-sdk-js-v3/issues/1096
443438
const getStream = await import("get-stream");
444439

packages/libs/lambda-at-edge/src/lib/triggerStaticRegeneration.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { s3BucketNameFromEventRequest } from "../s3/s3BucketNameFromEventRequest";
22
import { RegenerationEvent } from "../types";
33
import * as crypto from "crypto";
4+
import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
45

56
interface TriggerStaticRegenerationOptions {
67
request: AWSLambda.CloudFrontRequest;
@@ -27,12 +28,6 @@ export const triggerStaticRegeneration = async (
2728
throw new Error("Expected region to be defined");
2829
}
2930

30-
// Dynamic imports don't get treeshook so we need to import as deep as possible for only the code we need.
31-
const { SQSClient } = await import("@aws-sdk/client-sqs/src/SQSClient");
32-
const { SendMessageCommand } = await import(
33-
"@aws-sdk/client-sqs/src/commands/SendMessageCommand"
34-
);
35-
3631
const sqs = new SQSClient({
3732
region,
3833
maxAttempts: 1

packages/libs/lambda-at-edge/src/render/renderStaticPage.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Readable } from "stream";
1313
import { triggerStaticRegeneration } from "../lib/triggerStaticRegeneration";
1414
import { s3StorePage } from "../s3/s3StorePage";
1515
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "http";
16+
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
1617

1718
/**
1819
* This function is experimental to allow rendering static pages fully from the handler.
@@ -63,16 +64,11 @@ export const renderStaticPage = async ({
6364
}
6465

6566
// Render response from S3
66-
// Lazily import only S3Client to reduce init times until actually needed
67-
const { S3Client } = await import("@aws-sdk/client-s3/src/S3Client");
6867
const s3 = new S3Client({
6968
region: request.origin?.s3?.region,
7069
maxAttempts: 3
7170
});
7271
const s3BasePath = basePath ? `${basePath.replace(/^\//, "")}/` : "";
73-
const { GetObjectCommand } = await import(
74-
"@aws-sdk/client-s3/src/commands/GetObjectCommand"
75-
);
7672
// S3 Body is stream per: https://github.com/aws/aws-sdk-js-v3/issues/1096
7773
const getStream = await import("get-stream");
7874
const s3Params = {

packages/libs/lambda-at-edge/src/s3/s3StorePage.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
2+
13
interface S3StorePageOptions {
24
basePath: string | undefined;
35
uri: string;
@@ -19,8 +21,6 @@ interface S3StorePageOptions {
1921
export const s3StorePage = async (
2022
options: S3StorePageOptions
2123
): Promise<{ cacheControl: string | undefined; expires: Date | undefined }> => {
22-
const { S3Client } = await import("@aws-sdk/client-s3/src/S3Client");
23-
2424
const s3 = new S3Client({
2525
region: options.region,
2626
maxAttempts: 3
@@ -61,9 +61,6 @@ export const s3StorePage = async (
6161
Expires: expires
6262
};
6363

64-
const { PutObjectCommand } = await import(
65-
"@aws-sdk/client-s3/src/commands/PutObjectCommand"
66-
);
6764
await Promise.all([
6865
s3.send(new PutObjectCommand(s3JsonParams)),
6966
s3.send(new PutObjectCommand(s3HtmlParams))

packages/libs/lambda-at-edge/tests/default-handler/default-handler-origin-response.test.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ import {
55
CloudFrontHeaders,
66
CloudFrontResponse
77
} from "aws-lambda";
8-
import { S3Client } from "@aws-sdk/client-s3/src/S3Client";
8+
import { S3Client } from "@aws-sdk/client-s3";
9+
import { jest } from "@jest/globals";
910

10-
jest.mock("@aws-sdk/client-s3/src/S3Client", () =>
11+
jest.mock("@aws-sdk/client-s3", () =>
1112
require("../mocks/s3/aws-sdk-s3-client.mock")
1213
);
1314

14-
jest.mock("@aws-sdk/client-s3/src/commands/GetObjectCommand", () =>
15-
require("../mocks/s3/aws-sdk-s3-client-get-object-command.mock")
16-
);
17-
18-
jest.mock("@aws-sdk/client-s3/src/commands/PutObjectCommand", () =>
19-
require("../mocks/s3/aws-sdk-s3-client-put-object-command.mock")
20-
);
21-
2215
jest.mock(
2316
"../../src/manifest.json",
2417
() => require("./default-build-manifest-origin-response.json"),

packages/libs/lambda-at-edge/tests/default-handler/default-handler-with-basepath-origin-response.test.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@ import {
55
CloudFrontHeaders,
66
CloudFrontResponse
77
} from "aws-lambda";
8-
import { S3Client } from "@aws-sdk/client-s3/src/S3Client";
8+
import { S3Client } from "@aws-sdk/client-s3";
99

10-
jest.mock("@aws-sdk/client-s3/src/S3Client", () =>
10+
jest.mock("@aws-sdk/client-s3", () =>
1111
require("../mocks/s3/aws-sdk-s3-client.mock")
1212
);
1313

14-
jest.mock("@aws-sdk/client-s3/src/commands/GetObjectCommand", () =>
15-
require("../mocks/s3/aws-sdk-s3-client-get-object-command.mock")
16-
);
17-
18-
jest.mock("@aws-sdk/client-s3/src/commands/PutObjectCommand", () =>
19-
require("../mocks/s3/aws-sdk-s3-client-put-object-command.mock")
20-
);
21-
2214
jest.mock(
2315
"../../src/manifest.json",
2416
() => require("./default-build-manifest-origin-response.json"),

packages/libs/lambda-at-edge/tests/mocks/s3/aws-sdk-s3-client.mock.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Readable } from "stream";
2+
import { jest } from "@jest/globals";
23

3-
export const mockSend = jest.fn((input) => {
4+
export const mockSend = jest.fn((input: any) => {
45
if (input.Command === "GetObjectCommand") {
56
// Simulate fallback page cache control headers
67
const isFallback = /\[.*]/.test(input.Key as string);
@@ -18,8 +19,35 @@ export const mockSend = jest.fn((input) => {
1819
});
1920

2021
const MockS3Client = jest.fn(() => ({
21-
constructor: () => {},
22+
constructor: () => {
23+
// intentionally empty
24+
},
2225
send: mockSend
2326
}));
2427

25-
export { MockS3Client as S3Client };
28+
// This mock makes it easier to unit test by returning params with the command name
29+
const MockGetObjectCommand = jest.fn((params: Record<string, string>) => {
30+
return {
31+
...{
32+
Command: "GetObjectCommand"
33+
},
34+
...params
35+
};
36+
});
37+
38+
// This mock makes it easier to unit test by returning params with the command name
39+
const MockPutObjectCommand = jest.fn((params: any) => {
40+
return {
41+
...{
42+
Command: "PutObjectCommand"
43+
},
44+
...params
45+
};
46+
});
47+
48+
export {
49+
MockS3Client as S3Client,
50+
MockGetObjectCommand as GetObjectCommand,
51+
MockGetObjectCommand,
52+
MockPutObjectCommand as PutObjectCommand
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { jest } from "@jest/globals";
2+
3+
const mockSend = jest.fn();
4+
const mockS3Client = jest.fn(() => ({
5+
send: mockSend
6+
}));
7+
const mockGetObjectCommand = jest.fn();
8+
const mockPutObjectCommand = jest.fn();
9+
10+
export {
11+
mockS3Client as S3Client,
12+
mockGetObjectCommand as GetObjectCommand,
13+
mockPutObjectCommand as PutObjectCommand,
14+
mockS3Client,
15+
mockGetObjectCommand,
16+
mockPutObjectCommand,
17+
mockSend
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { jest } from "@jest/globals";
2+
3+
const mockSQSClient = jest.fn();
4+
const mockSendMessageCommand = jest.fn();
5+
6+
export {
7+
mockSQSClient as SQSClient,
8+
mockSendMessageCommand as SendMessageCommand,
9+
mockSQSClient,
10+
mockSendMessageCommand
11+
};

packages/libs/lambda-at-edge/tests/s3/s3StorePage.test.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { s3StorePage } from "../../src/s3/s3StorePage";
2+
import { jest } from "@jest/globals";
23

3-
const mockSend = jest.fn();
4-
const mockPutObjectCommand = jest.fn();
4+
import {
5+
mockSend,
6+
mockPutObjectCommand
7+
} from "../mocks/s3/aws-sdk-s3-client.mock2";
58

6-
jest.mock("@aws-sdk/client-s3/src/S3Client", () => ({
7-
S3Client: jest.fn(() => ({
8-
send: mockSend
9-
}))
10-
}));
11-
12-
jest.mock("@aws-sdk/client-s3/src/commands/PutObjectCommand", () => ({
13-
PutObjectCommand: mockPutObjectCommand
14-
}));
9+
jest.mock("@aws-sdk/client-s3", () =>
10+
require("../mocks/s3/aws-sdk-s3-client.mock2")
11+
);
1512

1613
describe("S3StorePage Tests", () => {
1714
it.each`

packages/libs/lambda-at-edge/tests/utils/triggerStaticRegeneration.test.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { triggerStaticRegeneration } from "../../src/lib/triggerStaticRegeneration";
2+
import {
3+
mockSQSClient,
4+
mockSendMessageCommand
5+
} from "../mocks/sqs/aws-sdk-sqs-client.mock";
26
import { jest } from "@jest/globals";
37

8+
jest.mock("@aws-sdk/client-sqs", () =>
9+
require("../mocks/sqs/aws-sdk-sqs-client.mock")
10+
);
11+
412
describe("triggerStaticRegeneration()", () => {
5-
const mockSQSClient = jest.fn();
6-
const mockSendMessageCommand = jest.fn();
713
beforeEach(() => {
814
mockSQSClient.mockReset();
9-
jest.mock("@aws-sdk/client-sqs/src/SQSClient", () => ({
10-
__esModule: true,
11-
SQSClient: mockSQSClient
12-
}));
13-
jest.mock("@aws-sdk/client-sqs/src/commands/SendMessageCommand", () => ({
14-
_esModule: true,
15-
SendMessageCommand: mockSendMessageCommand
16-
}));
1715
});
1816

1917
const options = {

0 commit comments

Comments
 (0)