@middy/s3-object-response 2.5.5 → 2.5.6
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 +54 -61
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,116 +1,109 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
let https = require('https');
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
URL
|
|
7
|
-
} = require('url');
|
|
1
|
+
let https = require('https')
|
|
2
|
+
const { URL } = require('url')
|
|
8
3
|
|
|
9
4
|
const {
|
|
10
5
|
canPrefetch,
|
|
11
6
|
createPrefetchClient,
|
|
12
7
|
createClient
|
|
13
|
-
} = require('@middy/util')
|
|
8
|
+
} = require('@middy/util')
|
|
14
9
|
|
|
15
|
-
const S3 = require('aws-sdk/clients/s3')
|
|
10
|
+
const S3 = require('aws-sdk/clients/s3') // v2
|
|
16
11
|
// const { S3 } = require('@aws-sdk/client-s3') // v3
|
|
17
12
|
|
|
18
|
-
|
|
19
13
|
const defaults = {
|
|
20
|
-
AwsClient: S3,
|
|
21
|
-
// Allow for XRay
|
|
14
|
+
AwsClient: S3, // Allow for XRay
|
|
22
15
|
awsClientOptions: {},
|
|
23
16
|
awsClientAssumeRole: undefined,
|
|
24
17
|
awsClientCapture: undefined,
|
|
25
18
|
httpsCapture: undefined,
|
|
26
19
|
disablePrefetch: false,
|
|
27
20
|
bodyType: undefined
|
|
28
|
-
}
|
|
21
|
+
}
|
|
29
22
|
|
|
30
23
|
const s3ObjectResponseMiddleware = (opts = {}) => {
|
|
31
|
-
const options = { ...defaults,
|
|
32
|
-
...opts
|
|
33
|
-
};
|
|
24
|
+
const options = { ...defaults, ...opts }
|
|
34
25
|
|
|
35
26
|
if (!['stream', 'promise'].includes(options.bodyType)) {
|
|
36
|
-
throw new Error('bodyType is invalid.')
|
|
27
|
+
throw new Error('bodyType is invalid.')
|
|
37
28
|
}
|
|
38
29
|
|
|
39
30
|
if (options.httpsCapture) {
|
|
40
|
-
https = options.httpsCapture(https)
|
|
31
|
+
https = options.httpsCapture(https)
|
|
41
32
|
}
|
|
42
33
|
|
|
43
|
-
let client
|
|
44
|
-
|
|
34
|
+
let client
|
|
45
35
|
if (canPrefetch(options)) {
|
|
46
|
-
client = createPrefetchClient(options)
|
|
36
|
+
client = createPrefetchClient(options)
|
|
47
37
|
}
|
|
48
38
|
|
|
49
|
-
const s3ObjectResponseMiddlewareBefore = async request => {
|
|
39
|
+
const s3ObjectResponseMiddlewareBefore = async (request) => {
|
|
50
40
|
const {
|
|
51
41
|
inputS3Url,
|
|
52
42
|
outputRoute,
|
|
53
43
|
outputToken
|
|
54
|
-
} = request.event.getObjectContext
|
|
44
|
+
} = request.event.getObjectContext
|
|
45
|
+
|
|
55
46
|
request.internal.s3ObjectResponse = {
|
|
56
47
|
RequestRoute: outputRoute,
|
|
57
48
|
RequestToken: outputToken
|
|
58
|
-
}
|
|
59
|
-
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const parsedInputS3Url = new URL(inputS3Url)
|
|
60
52
|
const fetchOptions = {
|
|
61
53
|
method: 'GET',
|
|
62
54
|
host: parsedInputS3Url.hostname,
|
|
63
55
|
path: parsedInputS3Url.pathname
|
|
64
|
-
}
|
|
65
|
-
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
66
|
-
};
|
|
56
|
+
}
|
|
67
57
|
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
request.context.s3Object = fetchType(options.bodyType, fetchOptions)
|
|
59
|
+
}
|
|
70
60
|
|
|
61
|
+
const s3ObjectResponseMiddlewareAfter = async (request) => {
|
|
71
62
|
if (!client) {
|
|
72
|
-
client = await createClient(options, request)
|
|
63
|
+
client = await createClient(options, request)
|
|
73
64
|
}
|
|
74
65
|
|
|
75
|
-
request.response.Body =
|
|
76
|
-
delete request.response.body
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
66
|
+
request.response.Body = request.response.Body ?? request.response.body
|
|
67
|
+
delete request.response.body
|
|
68
|
+
|
|
69
|
+
return client
|
|
70
|
+
.writeGetObjectResponse({
|
|
71
|
+
...request.response,
|
|
72
|
+
...request.internal.s3ObjectResponse
|
|
73
|
+
})
|
|
74
|
+
.promise()
|
|
75
|
+
.then(() => ({ statusCode: 200 })) // TODO test if needed
|
|
76
|
+
}
|
|
83
77
|
|
|
84
78
|
return {
|
|
85
79
|
before: s3ObjectResponseMiddlewareBefore,
|
|
86
80
|
after: s3ObjectResponseMiddlewareAfter
|
|
87
|
-
}
|
|
88
|
-
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
89
83
|
|
|
90
84
|
const fetchType = (type, fetchOptions) => {
|
|
91
85
|
if (type === 'stream') {
|
|
92
|
-
return fetchStream(fetchOptions)
|
|
86
|
+
return fetchStream(fetchOptions)
|
|
93
87
|
} else if (type === 'promise') {
|
|
94
|
-
return fetchPromise(fetchOptions)
|
|
88
|
+
return fetchPromise(fetchOptions)
|
|
95
89
|
}
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
96
92
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const fetchStream = fetchOptions => {
|
|
101
|
-
return https.request(fetchOptions);
|
|
102
|
-
};
|
|
93
|
+
const fetchStream = (fetchOptions) => {
|
|
94
|
+
return https.request(fetchOptions)
|
|
95
|
+
}
|
|
103
96
|
|
|
104
|
-
const fetchPromise = fetchOptions => {
|
|
97
|
+
const fetchPromise = (fetchOptions) => {
|
|
105
98
|
return new Promise((resolve, reject) => {
|
|
106
|
-
let data = ''
|
|
107
|
-
const stream = fetchStream(fetchOptions)
|
|
108
|
-
stream.on('data', chunk => {
|
|
109
|
-
data += chunk
|
|
110
|
-
})
|
|
111
|
-
stream.on('end', () => resolve(data))
|
|
112
|
-
stream.on('error', error => reject(error))
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
module.exports = s3ObjectResponseMiddleware
|
|
99
|
+
let data = ''
|
|
100
|
+
const stream = fetchStream(fetchOptions)
|
|
101
|
+
stream.on('data', (chunk) => {
|
|
102
|
+
data += chunk
|
|
103
|
+
})
|
|
104
|
+
stream.on('end', () => resolve(data))
|
|
105
|
+
stream.on('error', (error) => reject(error))
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
module.exports = s3ObjectResponseMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/s3-object-response",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.6",
|
|
4
4
|
"description": "S3 object response handling middleware for the middy framework",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"engines": {
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^2.5.
|
|
49
|
+
"@middy/util": "^2.5.6"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^2.5.
|
|
52
|
+
"@middy/core": "^2.5.6",
|
|
53
53
|
"aws-sdk": "^2.939.0",
|
|
54
54
|
"aws-xray-sdk": "^3.3.3"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
|
|
57
57
|
}
|