@middy/s3 5.1.0 → 5.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 (2) hide show
  1. package/index.js +76 -60
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,64 +1,80 @@
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
+ catchInvalidSignatureException
11
+ } from '@middy/util'
12
+ import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
3
13
  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);
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
+ const s3Middleware = (opts = {}) => {
27
+ const options = {
28
+ ...defaults,
29
+ ...opts
30
+ }
31
+ const fetch = (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, fetch)
59
+ }
60
+ const s3MiddlewareBefore = async (request) => {
61
+ if (!client) {
62
+ client = await createClient(options, request)
44
63
  }
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;
64
+ const { value } = processCache(options, fetch, 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
+ }
63
74
  }
75
+ export default s3Middleware
64
76
 
77
+ // used for TS type inference (see index.d.ts)
78
+ export function s3Req (req) {
79
+ return req
80
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/s3",
3
- "version": "5.1.0",
3
+ "version": "5.2.1",
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.1.0"
61
+ "@middy/util": "5.2.1"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@aws-sdk/client-s3": "^3.0.0",
65
- "@middy/core": "5.1.0",
65
+ "@middy/core": "5.2.1",
66
66
  "@types/aws-lambda": "^8.10.101",
67
67
  "aws-xray-sdk": "^3.3.3"
68
68
  },
69
- "gitHead": "bbdaf5843914921804ba085dd58117273febe6b5"
69
+ "gitHead": "4d55da221b9165b4b3e59a12632fd40a149a1e92"
70
70
  }