@middy/s3 6.1.6 → 6.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +58 -38
  2. package/index.js +74 -74
  3. package/package.json +69 -72
package/index.d.ts CHANGED
@@ -1,45 +1,65 @@
1
- import middy from '@middy/core'
2
- import { Options as MiddyOptions } from '@middy/util'
3
- import { Context as LambdaContext } from 'aws-lambda'
4
- import { GetObjectCommandInput, S3Client, S3ClientConfig } from '@aws-sdk/client-s3'
1
+ import type {
2
+ GetObjectCommandInput,
3
+ S3Client,
4
+ S3ClientConfig,
5
+ } from "@aws-sdk/client-s3";
6
+ import type middy from "@middy/core";
7
+ import type { Options as MiddyOptions } from "@middy/util";
8
+ import type { Context as LambdaContext } from "aws-lambda";
5
9
 
6
- export type GetObjectCommandInputNoChecksumMode = Omit<GetObjectCommandInput, 'ChecksumMode'> & {
7
- ChecksumMode?: never
8
- }
9
- export type ParamType<T> = GetObjectCommandInputNoChecksumMode & { __returnType?: T }
10
- export declare function s3Req<T> (req: GetObjectCommandInputNoChecksumMode): ParamType<T>
10
+ export type GetObjectCommandInputNoChecksumMode = Omit<
11
+ GetObjectCommandInput,
12
+ "ChecksumMode"
13
+ > & {
14
+ ChecksumMode?: never;
15
+ };
16
+ export type ParamType<T> = GetObjectCommandInputNoChecksumMode & {
17
+ __returnType?: T;
18
+ };
19
+ export declare function s3Req<T>(
20
+ req: GetObjectCommandInputNoChecksumMode,
21
+ ): ParamType<T>;
11
22
 
12
- export type S3Options<AwsS3Client = S3Client> = Omit<MiddyOptions<AwsS3Client, S3ClientConfig>, 'fetchData'>
13
- & {
14
- fetchData?: {
15
- [key: string]: GetObjectCommandInputNoChecksumMode | ParamType<unknown>
16
- }
17
- }
23
+ export type S3Options<AwsS3Client = S3Client> = Omit<
24
+ MiddyOptions<AwsS3Client, S3ClientConfig>,
25
+ "fetchData"
26
+ > & {
27
+ fetchData?: {
28
+ [key: string]: GetObjectCommandInputNoChecksumMode | ParamType<unknown>;
29
+ };
30
+ };
18
31
 
19
- export type Context<TOptions extends S3Options | undefined> =
20
- TOptions extends { setToContext: true }
21
- ? TOptions extends { fetchData: infer TFetchData }
22
- ? LambdaContext & {
23
- [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
24
- ? T
25
- : unknown
26
- }
27
- : never
28
- : LambdaContext
32
+ export type Context<TOptions extends S3Options | undefined> = TOptions extends {
33
+ setToContext: true;
34
+ }
35
+ ? TOptions extends { fetchData: infer TFetchData }
36
+ ? LambdaContext & {
37
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
38
+ ? T
39
+ : unknown;
40
+ }
41
+ : never
42
+ : LambdaContext;
29
43
 
30
44
  export type Internal<TOptions extends S3Options | undefined> =
31
- TOptions extends S3Options
32
- ? TOptions extends { fetchData: infer TFetchData }
33
- ? {
34
- [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
35
- ? T
36
- : unknown
37
- }
38
- : {}
39
- : {}
45
+ TOptions extends S3Options
46
+ ? TOptions extends { fetchData: infer TFetchData }
47
+ ? {
48
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
49
+ ? T
50
+ : unknown;
51
+ }
52
+ : {}
53
+ : {};
40
54
 
41
- declare function s3Middleware<TOptions extends S3Options | undefined> (
42
- options?: TOptions
43
- ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
55
+ declare function s3Middleware<TOptions extends S3Options | undefined>(
56
+ options?: TOptions,
57
+ ): middy.MiddlewareObj<
58
+ unknown,
59
+ any,
60
+ Error,
61
+ Context<TOptions>,
62
+ Internal<TOptions>
63
+ >;
44
64
 
45
- export default s3Middleware
65
+ export default s3Middleware;
package/index.js CHANGED
@@ -1,80 +1,80 @@
1
+ import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
1
2
  import {
2
- canPrefetch,
3
- createPrefetchClient,
4
- createClient,
5
- getCache,
6
- getInternal,
7
- processCache,
8
- modifyCache,
9
- jsonSafeParse,
10
- catchInvalidSignatureException
11
- } from '@middy/util'
12
- import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
3
+ canPrefetch,
4
+ catchInvalidSignatureException,
5
+ createClient,
6
+ createPrefetchClient,
7
+ getCache,
8
+ getInternal,
9
+ jsonSafeParse,
10
+ modifyCache,
11
+ processCache,
12
+ } from "@middy/util";
13
13
  const defaults = {
14
- AwsClient: S3Client,
15
- awsClientOptions: {},
16
- awsClientAssumeRole: undefined,
17
- awsClientCapture: undefined,
18
- fetchData: {},
19
- disablePrefetch: false,
20
- cacheKey: 's3',
21
- cacheKeyExpiry: {},
22
- cacheExpiry: -1,
23
- setToContext: false
24
- }
25
- const contentTypePattern = /^application\/(.+\+)?json($|;.+)/
14
+ AwsClient: S3Client,
15
+ awsClientOptions: {},
16
+ awsClientAssumeRole: undefined,
17
+ awsClientCapture: undefined,
18
+ fetchData: {},
19
+ disablePrefetch: false,
20
+ cacheKey: "s3",
21
+ cacheKeyExpiry: {},
22
+ cacheExpiry: -1,
23
+ setToContext: false,
24
+ };
25
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
26
26
  const s3Middleware = (opts = {}) => {
27
- const options = {
28
- ...defaults,
29
- ...opts
30
- }
31
- const fetchRequest = (request, cachedValues = {}) => {
32
- const values = {}
33
- for (const internalKey of Object.keys(options.fetchData)) {
34
- if (cachedValues[internalKey]) continue
35
- const command = new GetObjectCommand(options.fetchData[internalKey])
36
- values[internalKey] = client
37
- .send(command)
38
- .catch((e) => catchInvalidSignatureException(e, client, command))
39
- .then(async (resp) => {
40
- let value = await resp.Body.transformToString()
41
- if (contentTypePattern.test(resp.ContentType)) {
42
- value = jsonSafeParse(value)
43
- }
44
- return value
45
- })
46
- .catch((e) => {
47
- const value = getCache(options.cacheKey).value ?? {}
48
- value[internalKey] = undefined
49
- modifyCache(options.cacheKey, value)
50
- throw e
51
- })
52
- }
53
- return values
54
- }
55
- let client
56
- if (canPrefetch(options)) {
57
- client = createPrefetchClient(options)
58
- processCache(options, fetchRequest)
59
- }
60
- const s3MiddlewareBefore = async (request) => {
61
- if (!client) {
62
- client = await createClient(options, request)
63
- }
64
- const { value } = processCache(options, fetchRequest, request)
65
- Object.assign(request.internal, value)
66
- if (options.setToContext) {
67
- const data = await getInternal(Object.keys(options.fetchData), request)
68
- Object.assign(request.context, data)
69
- }
70
- }
71
- return {
72
- before: s3MiddlewareBefore
73
- }
74
- }
75
- export default s3Middleware
27
+ const options = {
28
+ ...defaults,
29
+ ...opts,
30
+ };
31
+ const fetchRequest = (request, cachedValues = {}) => {
32
+ const values = {};
33
+ for (const internalKey of Object.keys(options.fetchData)) {
34
+ if (cachedValues[internalKey]) continue;
35
+ const command = new GetObjectCommand(options.fetchData[internalKey]);
36
+ values[internalKey] = client
37
+ .send(command)
38
+ .catch((e) => catchInvalidSignatureException(e, client, command))
39
+ .then(async (resp) => {
40
+ let value = await resp.Body.transformToString();
41
+ if (contentTypePattern.test(resp.ContentType)) {
42
+ value = jsonSafeParse(value);
43
+ }
44
+ return value;
45
+ })
46
+ .catch((e) => {
47
+ const value = getCache(options.cacheKey).value ?? {};
48
+ value[internalKey] = undefined;
49
+ modifyCache(options.cacheKey, value);
50
+ throw e;
51
+ });
52
+ }
53
+ return values;
54
+ };
55
+ let client;
56
+ if (canPrefetch(options)) {
57
+ client = createPrefetchClient(options);
58
+ processCache(options, fetchRequest);
59
+ }
60
+ const s3MiddlewareBefore = async (request) => {
61
+ if (!client) {
62
+ client = await createClient(options, request);
63
+ }
64
+ const { value } = processCache(options, fetchRequest, request);
65
+ Object.assign(request.internal, value);
66
+ if (options.setToContext) {
67
+ const data = await getInternal(Object.keys(options.fetchData), request);
68
+ Object.assign(request.context, data);
69
+ }
70
+ };
71
+ return {
72
+ before: s3MiddlewareBefore,
73
+ };
74
+ };
75
+ export default s3Middleware;
76
76
 
77
77
  // used for TS type inference (see index.d.ts)
78
- export function s3Req (req) {
79
- return req
78
+ export function s3Req(req) {
79
+ return req;
80
80
  }
package/package.json CHANGED
@@ -1,74 +1,71 @@
1
1
  {
2
- "name": "@middy/s3",
3
- "version": "6.1.6",
4
- "description": "S3 middleware for the middy framework",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=20"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "module": "./index.js",
14
- "exports": {
15
- ".": {
16
- "import": {
17
- "types": "./index.d.ts",
18
- "default": "./index.js"
19
- },
20
- "require": {
21
- "default": "./index.js"
22
- }
23
- }
24
- },
25
- "types": "index.d.ts",
26
- "files": [
27
- "index.js",
28
- "index.d.ts"
29
- ],
30
- "scripts": {
31
- "test": "npm run test:unit && npm run test:fuzz",
32
- "test:unit": "node --test __tests__/index.js",
33
- "test:fuzz": "node --test __tests__/fuzz.js",
34
- "test:benchmark": "node __benchmarks__/index.js"
35
- },
36
- "license": "MIT",
37
- "keywords": [
38
- "Lambda",
39
- "Middleware",
40
- "Serverless",
41
- "Framework",
42
- "AWS",
43
- "AWS Lambda",
44
- "Middy",
45
- "S3"
46
- ],
47
- "author": {
48
- "name": "Middy contributors",
49
- "url": "https://github.com/middyjs/middy/graphs/contributors"
50
- },
51
- "repository": {
52
- "type": "git",
53
- "url": "git+https://github.com/middyjs/middy.git",
54
- "directory": "packages/s3"
55
- },
56
- "bugs": {
57
- "url": "https://github.com/middyjs/middy/issues"
58
- },
59
- "homepage": "https://middy.js.org",
60
- "funding": {
61
- "type": "github",
62
- "url": "https://github.com/sponsors/willfarrell"
63
- },
64
- "dependencies": {
65
- "@middy/util": "6.1.6"
66
- },
67
- "devDependencies": {
68
- "@aws-sdk/client-s3": "^3.0.0",
69
- "@middy/core": "6.1.6",
70
- "@types/aws-lambda": "^8.10.101",
71
- "aws-xray-sdk": "^3.3.3"
72
- },
73
- "gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
2
+ "name": "@middy/s3",
3
+ "version": "6.2.1",
4
+ "description": "S3 middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=20"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "module": "./index.js",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "default": "./index.js"
22
+ }
23
+ }
24
+ },
25
+ "types": "index.d.ts",
26
+ "files": ["index.js", "index.d.ts"],
27
+ "scripts": {
28
+ "test": "npm run test:unit && npm run test:fuzz",
29
+ "test:unit": "node --test",
30
+ "test:fuzz": "node --test index.fuzz.js",
31
+ "test:perf": "node --test index.perf.js"
32
+ },
33
+ "license": "MIT",
34
+ "keywords": [
35
+ "Lambda",
36
+ "Middleware",
37
+ "Serverless",
38
+ "Framework",
39
+ "AWS",
40
+ "AWS Lambda",
41
+ "Middy",
42
+ "S3"
43
+ ],
44
+ "author": {
45
+ "name": "Middy contributors",
46
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git+https://github.com/middyjs/middy.git",
51
+ "directory": "packages/s3"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/middyjs/middy/issues"
55
+ },
56
+ "homepage": "https://middy.js.org",
57
+ "funding": {
58
+ "type": "github",
59
+ "url": "https://github.com/sponsors/willfarrell"
60
+ },
61
+ "dependencies": {
62
+ "@middy/util": "6.2.1"
63
+ },
64
+ "devDependencies": {
65
+ "@aws-sdk/client-s3": "^3.0.0",
66
+ "@middy/core": "6.2.1",
67
+ "@types/aws-lambda": "^8.10.101",
68
+ "aws-xray-sdk": "^3.3.3"
69
+ },
70
+ "gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
74
71
  }