Skip to content

feat: upgrade middleware to use AWS SDK v3 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/large-response-middleware/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@epilot/large-response-middleware",
"version": "0.0.15",
"version": "1.0.0-rc.2",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -31,7 +31,11 @@
"check-size": "size-limit",
"lint": "biome check --write .",
"prepublishOnly": "pnpm lint && pnpm build",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"bump:prerelease": "npm version prerelease --preid rc --no-git-tag-version",
"bump:patch": "npm version patch --no-git-tag-version",
"bump:minor": "npm version minor --no-git-tag-version",
"bump:major": "npm version major --no-git-tag-version"
},
"size-limit": [
{
Expand All @@ -40,12 +44,14 @@
}
],
"devDependencies": {
"@aws-sdk/client-s3": "3.782.0",
"@aws-sdk/s3-request-presigner": "3.782.0",
"@babel/cli": "^7.23.4",
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.23.3",
"@dazn/lambda-powertools-logger": "^1.28.1",
"@middy/core": "^2.5.7",
"@middy/core": "^3.6.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
Expand All @@ -54,8 +60,6 @@
"@size-limit/file": "^11.0.1",
"@types/aws-lambda": "^8.10.130",
"@types/node": "^20.10.5",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.816.0",
"cross-env": "^7.0.3",
"npm-run-all": "^4.1.5",
"rollup": "^4.9.1",
Expand All @@ -68,10 +72,10 @@
"vitest": "3.0.5"
},
"peerDependencies": {
"@aws-sdk/client-s3": "^3.x",
"@aws-sdk/s3-request-presigner": "^3.x",
"@dazn/lambda-powertools-logger": "^1.28.1",
"@middy/core": "^2.5.7",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.816.0"
"@middy/core": ">=2.x"
},
"dependencies": {
"core-js": "^3.34.0",
Expand Down
29 changes: 18 additions & 11 deletions packages/large-response-middleware/src/file-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from 'node:path';

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

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

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

await client
.putObject({
await client.send(
new PutObjectCommand({
Bucket: params.bucket,
Key: outputKey,
ContentType: params.contentType || 'text/plain',
Body: JSON.stringify(params.content || {}),
ACL: 'private',
})
.promise();

const url = await client.getSignedUrl('getObject', {
Expires: 3600,
Bucket: params.bucket,
Key: outputKey,
ResponseContentDisposition: `inline;filename=${path.basename(outputKey)}`,
});
}),
);

const url = await getSignedUrl(
client,
new GetObjectCommand({
Bucket: params.bucket,
Key: outputKey,
ResponseContentDisposition: `inline;filename=${path.basename(outputKey)}`,
}),
{
expiresIn: 3600,
},
);

return { url, filename: outputKey };
};
Expand Down
8 changes: 4 additions & 4 deletions packages/large-response-middleware/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import Log from '@dazn/lambda-powertools-logger';
import type { APIGatewayProxyEventV2 } from 'aws-lambda';
import type * as Lambda from 'aws-lambda';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { getOrgIdFromContext } from './__tests__/util';
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('withLargeResponseHandler', () => {
const middleware = withLargeResponseHandler({
thresholdWarn: 0.5,
thresholdError: 0.9,
customErrorMessage: (event: APIGatewayProxyEventV2) =>
customErrorMessage: (event: Lambda.APIGatewayProxyEventV2) =>
`Custom error message for ${event.requestContext?.requestId}`,
sizeLimitInMB: 1,
outputBucket: 'the-bucket-list',
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('withLargeResponseHandler', () => {
headers: {
Accept: LARGE_RESPONSE_MIME_TYPE,
},
} as Partial<APIGatewayProxyEventV2>,
} as Partial<Lambda.APIGatewayProxyEventV2>,
response: {
headers: {
random: Buffer.alloc(0.85 * 1024 * 1024, 'a').toString(), // 0.85MB
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('withLargeResponseHandler', () => {
headers: {
'Handle-Large-Response': 'true',
},
} as Partial<APIGatewayProxyEventV2>,
} as Partial<Lambda.APIGatewayProxyEventV2>,
response: {
headers: {
random: Buffer.alloc(0.85 * 1024 * 1024, 'a').toString(), // 0.85MB
Expand Down
15 changes: 9 additions & 6 deletions packages/large-response-middleware/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Log from '@dazn/lambda-powertools-logger';
import type middy from '@middy/core';
import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
import type * as Lambda from 'aws-lambda';
import yn from 'yn';

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

export type CustomErrorMessage = string | ((event: APIGatewayProxyEventV2) => string);
export type CustomErrorMessage = string | ((event: Lambda.APIGatewayProxyEventV2) => string);

export const withLargeResponseHandler = ({
thresholdWarn,
Expand All @@ -42,13 +42,13 @@ export const withLargeResponseHandler = ({
sizeLimitInMB: number;
outputBucket: string;
customErrorMessage?: CustomErrorMessage;
groupRequestsBy?: (event: APIGatewayProxyEventV2) => string;
groupRequestsBy?: (event: Lambda.APIGatewayProxyEventV2) => string;
}) => {
return {
after: async (handlerRequestContext: middy.Request) => {
const event = handlerRequestContext.event as APIGatewayProxyEventV2;
const event = handlerRequestContext.event as Lambda.APIGatewayProxyEventV2;
const requestHeaders = event?.headers || {};
const response = handlerRequestContext.response as APIGatewayProxyStructuredResultV2;
const response = handlerRequestContext.response as Lambda.APIGatewayProxyStructuredResultV2;

try {
const groupId = groupRequestsBy?.(handlerRequestContext.event) || 'all';
Expand Down Expand Up @@ -187,7 +187,10 @@ export const safeUploadLargeResponse = async ({
}
};

function getCustomErrorMessage(customErrorMessage: CustomErrorMessage | undefined, event: APIGatewayProxyEventV2) {
function getCustomErrorMessage(
customErrorMessage: CustomErrorMessage | undefined,
event: Lambda.APIGatewayProxyEventV2,
) {
return typeof customErrorMessage === 'function'
? customErrorMessage(event)
: (customErrorMessage ?? LARGE_RESPONSE_USER_INFO);
Expand Down
12 changes: 4 additions & 8 deletions packages/large-response-middleware/src/s3/s3-client.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { S3Client, type S3ClientConfig } from '@aws-sdk/client-s3';
import Log from '@dazn/lambda-powertools-logger';
import { S3 } from 'aws-sdk';

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

export const getS3Client = async (options?: S3.Types.ClientConfiguration) => {
export const getS3Client = async (options?: S3ClientConfig) => {
try {
const client = new S3({
const client = new S3Client({
endpoint: AWS_ENDPOINT,
s3ForcePathStyle: Boolean(AWS_ENDPOINT),
httpOptions: {
timeout: 60_000,
...options?.httpOptions,
},
forcePathStyle: Boolean(AWS_ENDPOINT),
...options,
});

Expand Down
Loading