@middy/s3 5.0.3 → 5.2.0

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 (2) hide show
  1. package/index.js +73 -60
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,64 +1,77 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
- import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache,
9
+ jsonSafeParse
10
+ } from '@middy/util'
11
+ import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
3
12
  const defaults = {
4
- AwsClient: S3Client,
5
- awsClientOptions: {},
6
- awsClientAssumeRole: undefined,
7
- awsClientCapture: undefined,
8
- fetchData: {},
9
- disablePrefetch: false,
10
- cacheKey: 's3',
11
- cacheKeyExpiry: {},
12
- cacheExpiry: -1,
13
- setToContext: false
14
- };
15
- const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
16
- const s3Middleware = (opts = {})=>{
17
- const options = {
18
- ...defaults,
19
- ...opts
20
- };
21
- const fetch = (request, cachedValues = {})=>{
22
- const values = {};
23
- for (const internalKey of Object.keys(options.fetchData)){
24
- if (cachedValues[internalKey]) continue;
25
- values[internalKey] = client.send(new GetObjectCommand(options.fetchData[internalKey])).then(async (resp)=>{
26
- let value = await resp.Body.transformToString();
27
- if (contentTypePattern.test(resp.ContentType)) {
28
- value = jsonSafeParse(value);
29
- }
30
- return value;
31
- }).catch((e)=>{
32
- const value = getCache(options.cacheKey).value ?? {};
33
- value[internalKey] = undefined;
34
- modifyCache(options.cacheKey, value);
35
- throw e;
36
- });
37
- }
38
- return values;
39
- };
40
- let client;
41
- if (canPrefetch(options)) {
42
- client = createPrefetchClient(options);
43
- processCache(options, fetch);
13
+ AwsClient: S3Client,
14
+ awsClientOptions: {},
15
+ awsClientAssumeRole: undefined,
16
+ awsClientCapture: undefined,
17
+ fetchData: {},
18
+ disablePrefetch: false,
19
+ cacheKey: 's3',
20
+ cacheKeyExpiry: {},
21
+ cacheExpiry: -1,
22
+ setToContext: false
23
+ }
24
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/
25
+ const s3Middleware = (opts = {}) => {
26
+ const options = {
27
+ ...defaults,
28
+ ...opts
29
+ }
30
+ const fetch = (request, cachedValues = {}) => {
31
+ const values = {}
32
+ for (const internalKey of Object.keys(options.fetchData)) {
33
+ if (cachedValues[internalKey]) continue
34
+ values[internalKey] = client
35
+ .send(new GetObjectCommand(options.fetchData[internalKey]))
36
+ .then(async (resp) => {
37
+ let value = await resp.Body.transformToString()
38
+ if (contentTypePattern.test(resp.ContentType)) {
39
+ value = jsonSafeParse(value)
40
+ }
41
+ return value
42
+ })
43
+ .catch((e) => {
44
+ const value = getCache(options.cacheKey).value ?? {}
45
+ value[internalKey] = undefined
46
+ modifyCache(options.cacheKey, value)
47
+ throw e
48
+ })
49
+ }
50
+ return values
51
+ }
52
+ let client
53
+ if (canPrefetch(options)) {
54
+ client = createPrefetchClient(options)
55
+ processCache(options, fetch)
56
+ }
57
+ const s3MiddlewareBefore = async (request) => {
58
+ if (!client) {
59
+ client = await createClient(options, request)
44
60
  }
45
- const s3MiddlewareBefore = async (request)=>{
46
- if (!client) {
47
- client = await createClient(options, request);
48
- }
49
- const { value } = processCache(options, fetch, request);
50
- Object.assign(request.internal, value);
51
- if (options.setToContext) {
52
- const data = await getInternal(Object.keys(options.fetchData), request);
53
- Object.assign(request.context, data);
54
- }
55
- };
56
- return {
57
- before: s3MiddlewareBefore
58
- };
59
- };
60
- export default s3Middleware;
61
- export function s3Req(req) {
62
- return req;
61
+ const { value } = processCache(options, fetch, request)
62
+ Object.assign(request.internal, value)
63
+ if (options.setToContext) {
64
+ const data = await getInternal(Object.keys(options.fetchData), request)
65
+ Object.assign(request.context, data)
66
+ }
67
+ }
68
+ return {
69
+ before: s3MiddlewareBefore
70
+ }
63
71
  }
72
+ export default s3Middleware
64
73
 
74
+ // used for TS type inference (see index.d.ts)
75
+ export function s3Req (req) {
76
+ return req
77
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/s3",
3
- "version": "5.0.3",
3
+ "version": "5.2.0",
4
4
  "description": "S3 middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -58,13 +58,13 @@
58
58
  "url": "https://github.com/sponsors/willfarrell"
59
59
  },
60
60
  "dependencies": {
61
- "@middy/util": "5.0.3"
61
+ "@middy/util": "5.2.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@aws-sdk/client-s3": "^3.0.0",
65
- "@middy/core": "5.0.3",
65
+ "@middy/core": "5.2.0",
66
66
  "@types/aws-lambda": "^8.10.101",
67
67
  "aws-xray-sdk": "^3.3.3"
68
68
  },
69
- "gitHead": "87660575a7ac2b52e4153c407a4c63c9449dcd0d"
69
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
70
70
  }