@middy/s3-object-response 3.0.3 → 3.0.4
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.cjs +101 -1
- package/index.js +90 -1
- package/package.json +4 -4
package/index.cjs
CHANGED
|
@@ -1,3 +1,103 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _https = _interopRequireDefault(require("https"));
|
|
7
|
+
var _url = require("url");
|
|
8
|
+
var _util = require("@middy/util");
|
|
9
|
+
var _s3Js = _interopRequireDefault(require("aws-sdk/clients/s3.js"));
|
|
10
|
+
function _interopRequireDefault(obj) {
|
|
11
|
+
return obj && obj.__esModule ? obj : {
|
|
12
|
+
default: obj
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
var _response;
|
|
16
|
+
const defaults = {
|
|
17
|
+
AwsClient: _s3Js.default,
|
|
18
|
+
awsClientOptions: {},
|
|
19
|
+
awsClientAssumeRole: undefined,
|
|
20
|
+
awsClientCapture: undefined,
|
|
21
|
+
httpsCapture: undefined,
|
|
22
|
+
disablePrefetch: false,
|
|
23
|
+
bodyType: undefined,
|
|
24
|
+
__https: _https.default
|
|
25
|
+
};
|
|
26
|
+
let https = _https.default;
|
|
27
|
+
const s3ObjectResponseMiddleware = (opts = {})=>{
|
|
28
|
+
const options = {
|
|
29
|
+
...defaults,
|
|
30
|
+
...opts
|
|
31
|
+
};
|
|
32
|
+
if (![
|
|
33
|
+
'stream',
|
|
34
|
+
'promise'
|
|
35
|
+
].includes(options.bodyType)) {
|
|
36
|
+
throw new Error('[s3-object-response] bodyType is invalid');
|
|
37
|
+
}
|
|
38
|
+
if (options.httpsCapture) {
|
|
39
|
+
https = options.httpsCapture(options.__https);
|
|
40
|
+
}
|
|
41
|
+
let client;
|
|
42
|
+
if ((0, _util).canPrefetch(options)) {
|
|
43
|
+
client = (0, _util).createPrefetchClient(options);
|
|
44
|
+
}
|
|
45
|
+
const s3ObjectResponseMiddlewareBefore = async (request)=>{
|
|
46
|
+
const { inputS3Url , outputRoute , outputToken } = request.event.getObjectContext;
|
|
47
|
+
request.internal.s3ObjectResponse = {
|
|
48
|
+
RequestRoute: outputRoute,
|
|
49
|
+
RequestToken: outputToken
|
|
50
|
+
};
|
|
51
|
+
const parsedInputS3Url = new _url.URL(inputS3Url);
|
|
52
|
+
const fetchOptions = {
|
|
53
|
+
method: 'GET',
|
|
54
|
+
host: parsedInputS3Url.hostname,
|
|
55
|
+
path: parsedInputS3Url.pathname
|
|
56
|
+
};
|
|
57
|
+
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
58
|
+
};
|
|
59
|
+
const s3ObjectResponseMiddlewareAfter = async (request)=>{
|
|
60
|
+
if (!client) {
|
|
61
|
+
client = await (0, _util).createClient(options, request);
|
|
62
|
+
}
|
|
63
|
+
(_response = request.response).Body ?? (_response.Body = request.response.body);
|
|
64
|
+
delete request.response.body;
|
|
65
|
+
await client.writeGetObjectResponse({
|
|
66
|
+
...request.response,
|
|
67
|
+
...request.internal.s3ObjectResponse
|
|
68
|
+
}).promise();
|
|
69
|
+
return {
|
|
70
|
+
statusCode: 200
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
before: s3ObjectResponseMiddlewareBefore,
|
|
75
|
+
after: s3ObjectResponseMiddlewareAfter
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
const fetchType = (type, fetchOptions)=>{
|
|
79
|
+
if (type === 'stream') {
|
|
80
|
+
return fetchStream(fetchOptions);
|
|
81
|
+
} else if (type === 'promise') {
|
|
82
|
+
return fetchPromise(fetchOptions);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const fetchStream = (fetchOptions)=>{
|
|
86
|
+
return https.request(fetchOptions);
|
|
87
|
+
};
|
|
88
|
+
const fetchPromise = (fetchOptions)=>{
|
|
89
|
+
return new Promise((resolve, reject)=>{
|
|
90
|
+
let data = '';
|
|
91
|
+
const stream = fetchStream(fetchOptions);
|
|
92
|
+
stream.on('data', (chunk)=>{
|
|
93
|
+
data += chunk;
|
|
94
|
+
});
|
|
95
|
+
stream.on('end', ()=>resolve(data));
|
|
96
|
+
stream.on('error', (error)=>reject(error));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
var _default = s3ObjectResponseMiddleware;
|
|
100
|
+
module.exports = _default;
|
|
101
|
+
|
|
2
102
|
|
|
3
103
|
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,3 +1,92 @@
|
|
|
1
|
-
var _response;
|
|
1
|
+
var _response;
|
|
2
|
+
import __https from 'https';
|
|
3
|
+
import { URL } from 'url';
|
|
4
|
+
import { canPrefetch, createPrefetchClient, createClient } from '@middy/util';
|
|
5
|
+
import S3 from 'aws-sdk/clients/s3.js';
|
|
6
|
+
const defaults = {
|
|
7
|
+
AwsClient: S3,
|
|
8
|
+
awsClientOptions: {},
|
|
9
|
+
awsClientAssumeRole: undefined,
|
|
10
|
+
awsClientCapture: undefined,
|
|
11
|
+
httpsCapture: undefined,
|
|
12
|
+
disablePrefetch: false,
|
|
13
|
+
bodyType: undefined,
|
|
14
|
+
__https
|
|
15
|
+
};
|
|
16
|
+
let https = __https;
|
|
17
|
+
const s3ObjectResponseMiddleware = (opts = {})=>{
|
|
18
|
+
const options = {
|
|
19
|
+
...defaults,
|
|
20
|
+
...opts
|
|
21
|
+
};
|
|
22
|
+
if (![
|
|
23
|
+
'stream',
|
|
24
|
+
'promise'
|
|
25
|
+
].includes(options.bodyType)) {
|
|
26
|
+
throw new Error('[s3-object-response] bodyType is invalid');
|
|
27
|
+
}
|
|
28
|
+
if (options.httpsCapture) {
|
|
29
|
+
https = options.httpsCapture(options.__https);
|
|
30
|
+
}
|
|
31
|
+
let client;
|
|
32
|
+
if (canPrefetch(options)) {
|
|
33
|
+
client = createPrefetchClient(options);
|
|
34
|
+
}
|
|
35
|
+
const s3ObjectResponseMiddlewareBefore = async (request)=>{
|
|
36
|
+
const { inputS3Url , outputRoute , outputToken } = request.event.getObjectContext;
|
|
37
|
+
request.internal.s3ObjectResponse = {
|
|
38
|
+
RequestRoute: outputRoute,
|
|
39
|
+
RequestToken: outputToken
|
|
40
|
+
};
|
|
41
|
+
const parsedInputS3Url = new URL(inputS3Url);
|
|
42
|
+
const fetchOptions = {
|
|
43
|
+
method: 'GET',
|
|
44
|
+
host: parsedInputS3Url.hostname,
|
|
45
|
+
path: parsedInputS3Url.pathname
|
|
46
|
+
};
|
|
47
|
+
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
48
|
+
};
|
|
49
|
+
const s3ObjectResponseMiddlewareAfter = async (request)=>{
|
|
50
|
+
if (!client) {
|
|
51
|
+
client = await createClient(options, request);
|
|
52
|
+
}
|
|
53
|
+
(_response = request.response).Body ?? (_response.Body = request.response.body);
|
|
54
|
+
delete request.response.body;
|
|
55
|
+
await client.writeGetObjectResponse({
|
|
56
|
+
...request.response,
|
|
57
|
+
...request.internal.s3ObjectResponse
|
|
58
|
+
}).promise();
|
|
59
|
+
return {
|
|
60
|
+
statusCode: 200
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
before: s3ObjectResponseMiddlewareBefore,
|
|
65
|
+
after: s3ObjectResponseMiddlewareAfter
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const fetchType = (type, fetchOptions)=>{
|
|
69
|
+
if (type === 'stream') {
|
|
70
|
+
return fetchStream(fetchOptions);
|
|
71
|
+
} else if (type === 'promise') {
|
|
72
|
+
return fetchPromise(fetchOptions);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const fetchStream = (fetchOptions)=>{
|
|
76
|
+
return https.request(fetchOptions);
|
|
77
|
+
};
|
|
78
|
+
const fetchPromise = (fetchOptions)=>{
|
|
79
|
+
return new Promise((resolve, reject)=>{
|
|
80
|
+
let data = '';
|
|
81
|
+
const stream = fetchStream(fetchOptions);
|
|
82
|
+
stream.on('data', (chunk)=>{
|
|
83
|
+
data += chunk;
|
|
84
|
+
});
|
|
85
|
+
stream.on('end', ()=>resolve(data));
|
|
86
|
+
stream.on('error', (error)=>reject(error));
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
export default s3ObjectResponseMiddleware;
|
|
90
|
+
|
|
2
91
|
|
|
3
92
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/s3-object-response",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "S3 object response handling middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -55,12 +55,12 @@
|
|
|
55
55
|
},
|
|
56
56
|
"homepage": "https://middy.js.org",
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@middy/util": "3.0.
|
|
58
|
+
"@middy/util": "3.0.4"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@middy/core": "3.0.
|
|
61
|
+
"@middy/core": "3.0.4",
|
|
62
62
|
"aws-sdk": "^2.939.0",
|
|
63
63
|
"aws-xray-sdk": "^3.3.3"
|
|
64
64
|
},
|
|
65
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
|
|
66
66
|
}
|