Skip to content

Commit db6a083

Browse files
authored
Merge pull request #6 from alexmarqs/chore/migrate-to-aws-sdk-v3
feat: upgrade middleware to use AWS SDK v3
2 parents 78b87b7 + 7b7f79e commit db6a083

File tree

6 files changed

+1258
-231
lines changed

6 files changed

+1258
-231
lines changed

packages/large-response-middleware/package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@epilot/large-response-middleware",
3-
"version": "0.0.15",
3+
"version": "1.0.0-rc.2",
44
"license": "MIT",
55
"repository": {
66
"type": "git",
@@ -31,7 +31,11 @@
3131
"check-size": "size-limit",
3232
"lint": "biome check --write .",
3333
"prepublishOnly": "pnpm lint && pnpm build",
34-
"typecheck": "tsc --noEmit"
34+
"typecheck": "tsc --noEmit",
35+
"bump:prerelease": "npm version prerelease --preid rc --no-git-tag-version",
36+
"bump:patch": "npm version patch --no-git-tag-version",
37+
"bump:minor": "npm version minor --no-git-tag-version",
38+
"bump:major": "npm version major --no-git-tag-version"
3539
},
3640
"size-limit": [
3741
{
@@ -40,12 +44,14 @@
4044
}
4145
],
4246
"devDependencies": {
47+
"@aws-sdk/client-s3": "3.782.0",
48+
"@aws-sdk/s3-request-presigner": "3.782.0",
4349
"@babel/cli": "^7.23.4",
4450
"@babel/core": "^7.25.2",
4551
"@babel/preset-env": "^7.25.4",
4652
"@babel/preset-typescript": "^7.23.3",
4753
"@dazn/lambda-powertools-logger": "^1.28.1",
48-
"@middy/core": "^2.5.7",
54+
"@middy/core": "^3.6.2",
4955
"@rollup/plugin-babel": "^6.0.4",
5056
"@rollup/plugin-commonjs": "^25.0.7",
5157
"@rollup/plugin-json": "^6.1.0",
@@ -54,8 +60,6 @@
5460
"@size-limit/file": "^11.0.1",
5561
"@types/aws-lambda": "^8.10.130",
5662
"@types/node": "^20.10.5",
57-
"aws-lambda": "^1.0.7",
58-
"aws-sdk": "^2.816.0",
5963
"cross-env": "^7.0.3",
6064
"npm-run-all": "^4.1.5",
6165
"rollup": "^4.9.1",
@@ -68,10 +72,10 @@
6872
"vitest": "3.0.5"
6973
},
7074
"peerDependencies": {
75+
"@aws-sdk/client-s3": "^3.x",
76+
"@aws-sdk/s3-request-presigner": "^3.x",
7177
"@dazn/lambda-powertools-logger": "^1.28.1",
72-
"@middy/core": "^2.5.7",
73-
"aws-lambda": "^1.0.7",
74-
"aws-sdk": "^2.816.0"
78+
"@middy/core": ">=2.x"
7579
},
7680
"dependencies": {
7781
"core-js": "^3.34.0",

packages/large-response-middleware/src/file-storage-service.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import path from 'node:path';
22

33
import { getS3Client } from './s3/s3-client';
44

5+
import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
6+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
57
import type { FileUploadContext } from '.';
68

79
/**
@@ -15,22 +17,27 @@ export const uploadFile = async (params: FileUploadContext) => {
1517
const date = getFormattedDate();
1618
const outputKey = `${namespace}/${date}/${encodeURIComponent(params.fileName)}`;
1719

18-
await client
19-
.putObject({
20+
await client.send(
21+
new PutObjectCommand({
2022
Bucket: params.bucket,
2123
Key: outputKey,
2224
ContentType: params.contentType || 'text/plain',
2325
Body: JSON.stringify(params.content || {}),
2426
ACL: 'private',
25-
})
26-
.promise();
27-
28-
const url = await client.getSignedUrl('getObject', {
29-
Expires: 3600,
30-
Bucket: params.bucket,
31-
Key: outputKey,
32-
ResponseContentDisposition: `inline;filename=${path.basename(outputKey)}`,
33-
});
27+
}),
28+
);
29+
30+
const url = await getSignedUrl(
31+
client,
32+
new GetObjectCommand({
33+
Bucket: params.bucket,
34+
Key: outputKey,
35+
ResponseContentDisposition: `inline;filename=${path.basename(outputKey)}`,
36+
}),
37+
{
38+
expiresIn: 3600,
39+
},
40+
);
3441

3542
return { url, filename: outputKey };
3643
};

packages/large-response-middleware/src/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import Log from '@dazn/lambda-powertools-logger';
3-
import type { APIGatewayProxyEventV2 } from 'aws-lambda';
3+
import type * as Lambda from 'aws-lambda';
44
import { beforeEach, describe, expect, it, vi } from 'vitest';
55

66
import { getOrgIdFromContext } from './__tests__/util';
@@ -191,7 +191,7 @@ describe('withLargeResponseHandler', () => {
191191
const middleware = withLargeResponseHandler({
192192
thresholdWarn: 0.5,
193193
thresholdError: 0.9,
194-
customErrorMessage: (event: APIGatewayProxyEventV2) =>
194+
customErrorMessage: (event: Lambda.APIGatewayProxyEventV2) =>
195195
`Custom error message for ${event.requestContext?.requestId}`,
196196
sizeLimitInMB: 1,
197197
outputBucket: 'the-bucket-list',
@@ -243,7 +243,7 @@ describe('withLargeResponseHandler', () => {
243243
headers: {
244244
Accept: LARGE_RESPONSE_MIME_TYPE,
245245
},
246-
} as Partial<APIGatewayProxyEventV2>,
246+
} as Partial<Lambda.APIGatewayProxyEventV2>,
247247
response: {
248248
headers: {
249249
random: Buffer.alloc(0.85 * 1024 * 1024, 'a').toString(), // 0.85MB
@@ -301,7 +301,7 @@ describe('withLargeResponseHandler', () => {
301301
headers: {
302302
'Handle-Large-Response': 'true',
303303
},
304-
} as Partial<APIGatewayProxyEventV2>,
304+
} as Partial<Lambda.APIGatewayProxyEventV2>,
305305
response: {
306306
headers: {
307307
random: Buffer.alloc(0.85 * 1024 * 1024, 'a').toString(), // 0.85MB

packages/large-response-middleware/src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Log from '@dazn/lambda-powertools-logger';
22
import type middy from '@middy/core';
3-
import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
3+
import type * as Lambda from 'aws-lambda';
44
import yn from 'yn';
55

66
import { uploadFile } from './file-storage-service';
@@ -27,7 +27,7 @@ export type FileUploadContext = {
2727
fileName: string;
2828
};
2929

30-
export type CustomErrorMessage = string | ((event: APIGatewayProxyEventV2) => string);
30+
export type CustomErrorMessage = string | ((event: Lambda.APIGatewayProxyEventV2) => string);
3131

3232
export const withLargeResponseHandler = ({
3333
thresholdWarn,
@@ -42,13 +42,13 @@ export const withLargeResponseHandler = ({
4242
sizeLimitInMB: number;
4343
outputBucket: string;
4444
customErrorMessage?: CustomErrorMessage;
45-
groupRequestsBy?: (event: APIGatewayProxyEventV2) => string;
45+
groupRequestsBy?: (event: Lambda.APIGatewayProxyEventV2) => string;
4646
}) => {
4747
return {
4848
after: async (handlerRequestContext: middy.Request) => {
49-
const event = handlerRequestContext.event as APIGatewayProxyEventV2;
49+
const event = handlerRequestContext.event as Lambda.APIGatewayProxyEventV2;
5050
const requestHeaders = event?.headers || {};
51-
const response = handlerRequestContext.response as APIGatewayProxyStructuredResultV2;
51+
const response = handlerRequestContext.response as Lambda.APIGatewayProxyStructuredResultV2;
5252

5353
try {
5454
const groupId = groupRequestsBy?.(handlerRequestContext.event) || 'all';
@@ -187,7 +187,10 @@ export const safeUploadLargeResponse = async ({
187187
}
188188
};
189189

190-
function getCustomErrorMessage(customErrorMessage: CustomErrorMessage | undefined, event: APIGatewayProxyEventV2) {
190+
function getCustomErrorMessage(
191+
customErrorMessage: CustomErrorMessage | undefined,
192+
event: Lambda.APIGatewayProxyEventV2,
193+
) {
191194
return typeof customErrorMessage === 'function'
192195
? customErrorMessage(event)
193196
: (customErrorMessage ?? LARGE_RESPONSE_USER_INFO);

packages/large-response-middleware/src/s3/s3-client.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1+
import { S3Client, type S3ClientConfig } from '@aws-sdk/client-s3';
12
import Log from '@dazn/lambda-powertools-logger';
2-
import { S3 } from 'aws-sdk';
33

44
const AWS_ENDPOINT =
55
process.env.AWS_ENDPOINT ||
66
(process.env.STAGE === 'local' || process.env.NODE_ENV === 'local' ? 'http://host.docker.internal:4566' : undefined);
77

8-
export const getS3Client = async (options?: S3.Types.ClientConfiguration) => {
8+
export const getS3Client = async (options?: S3ClientConfig) => {
99
try {
10-
const client = new S3({
10+
const client = new S3Client({
1111
endpoint: AWS_ENDPOINT,
12-
s3ForcePathStyle: Boolean(AWS_ENDPOINT),
13-
httpOptions: {
14-
timeout: 60_000,
15-
...options?.httpOptions,
16-
},
12+
forcePathStyle: Boolean(AWS_ENDPOINT),
1713
...options,
1814
});
1915

0 commit comments

Comments
 (0)