@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.
- package/index.js +76 -60
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,64 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const contentTypePattern = /^application\/(.+\+)?json($|;.+)
|
|
16
|
-
const s3Middleware = (opts = {})=>{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
|
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
|
|
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
|
|
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": "
|
|
69
|
+
"gitHead": "4d55da221b9165b4b3e59a12632fd40a149a1e92"
|
|
70
70
|
}
|