@middy/s3-object-response 4.6.5 → 5.0.0-alpha.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.d.ts +13 -4
- package/index.js +54 -85
- package/package.json +5 -11
- package/index.cjs +0 -105
package/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Context as LambdaContext } from 'aws-lambda'
|
|
|
4
4
|
import { S3Client, S3ClientConfig } from '@aws-sdk/client-s3'
|
|
5
5
|
import { ClientRequest } from 'http'
|
|
6
6
|
|
|
7
|
-
interface
|
|
7
|
+
export interface S3ObjectResponseOptions<AwsS3Client = S3Client>
|
|
8
8
|
extends Pick<
|
|
9
9
|
MiddyOptions<AwsS3Client, S3ClientConfig>,
|
|
10
10
|
| 'AwsClient'
|
|
@@ -16,16 +16,25 @@ interface Options<AwsS3Client = S3Client>
|
|
|
16
16
|
bodyType?: 'stream' | 'promise'
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export type Context<TOptions extends
|
|
19
|
+
export type Context<TOptions extends S3ObjectResponseOptions | undefined> = LambdaContext & {
|
|
20
20
|
s3Object: TOptions extends { bodyType: 'stream' }
|
|
21
21
|
? ClientRequest
|
|
22
22
|
: TOptions extends { bodyType: 'promise' }
|
|
23
23
|
? Promise<any>
|
|
24
24
|
: never
|
|
25
|
+
} & {
|
|
26
|
+
s3ObjectFetch: Promise<Response>
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
export interface Internal extends Record<string, any> {
|
|
30
|
+
s3ObjectResponse: {
|
|
31
|
+
RequestRoute: string
|
|
32
|
+
RequestToken: string
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare function s3ObjectResponse<TOptions extends S3ObjectResponseOptions | undefined> (
|
|
28
37
|
options?: TOptions
|
|
29
|
-
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions
|
|
38
|
+
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal>
|
|
30
39
|
|
|
31
40
|
export default s3ObjectResponse
|
package/index.js
CHANGED
|
@@ -1,90 +1,59 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { canPrefetch, createPrefetchClient, createClient } from '@middy/util'
|
|
2
|
+
|
|
3
|
+
import { S3Client, WriteGetObjectResponseCommand } from '@aws-sdk/client-s3'
|
|
4
|
+
|
|
5
5
|
const defaults = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (options.httpsCapture) {
|
|
29
|
-
https = options.httpsCapture(options.__https);
|
|
6
|
+
AwsClient: S3Client,
|
|
7
|
+
awsClientOptions: {},
|
|
8
|
+
awsClientAssumeRole: undefined,
|
|
9
|
+
awsClientCapture: undefined,
|
|
10
|
+
disablePrefetch: false
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const s3ObjectResponseMiddleware = (opts = {}) => {
|
|
14
|
+
const options = { ...defaults, ...opts }
|
|
15
|
+
|
|
16
|
+
let client
|
|
17
|
+
if (canPrefetch(options)) {
|
|
18
|
+
client = createPrefetchClient(options)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const s3ObjectResponseMiddlewareBefore = async (request) => {
|
|
22
|
+
const { inputS3Url, outputRoute, outputToken } =
|
|
23
|
+
request.event.getObjectContext
|
|
24
|
+
|
|
25
|
+
request.internal.s3ObjectResponse = {
|
|
26
|
+
RequestRoute: outputRoute,
|
|
27
|
+
RequestToken: outputToken
|
|
30
28
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
|
|
30
|
+
request.context.s3ObjectFetch = fetch(inputS3Url)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const s3ObjectResponseMiddlewareAfter = async (request) => {
|
|
34
|
+
if (!client) {
|
|
35
|
+
client = await createClient(options, request)
|
|
34
36
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
request.response.Body ??= request.response.body;
|
|
54
|
-
delete request.response.body;
|
|
55
|
-
await client.send(new WriteGetObjectResponseCommand({
|
|
56
|
-
...request.internal.s3ObjectResponse,
|
|
57
|
-
...request.response
|
|
58
|
-
}));
|
|
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);
|
|
37
|
+
|
|
38
|
+
if (request.response.body) {
|
|
39
|
+
request.response.Body = request.response.body
|
|
40
|
+
delete request.response.body
|
|
73
41
|
}
|
|
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
42
|
|
|
43
|
+
await client.send(
|
|
44
|
+
new WriteGetObjectResponseCommand({
|
|
45
|
+
...request.internal.s3ObjectResponse,
|
|
46
|
+
...request.response
|
|
47
|
+
})
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return { statusCode: 200 }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
before: s3ObjectResponseMiddlewareBefore,
|
|
55
|
+
after: s3ObjectResponseMiddlewareAfter
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default s3ObjectResponseMiddleware
|
package/package.json
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/s3-object-response",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "S3 object response handling middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -66,13 +60,13 @@
|
|
|
66
60
|
"url": "https://github.com/sponsors/willfarrell"
|
|
67
61
|
},
|
|
68
62
|
"dependencies": {
|
|
69
|
-
"@middy/util": "
|
|
63
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
70
64
|
},
|
|
71
65
|
"devDependencies": {
|
|
72
66
|
"@aws-sdk/client-s3": "^3.0.0",
|
|
73
|
-
"@middy/core": "
|
|
67
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
74
68
|
"@types/aws-lambda": "^8.10.101",
|
|
75
69
|
"aws-xray-sdk": "^3.3.3"
|
|
76
70
|
},
|
|
77
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
78
72
|
}
|
package/index.cjs
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _https = /*#__PURE__*/ _interop_require_default(require("https"));
|
|
12
|
-
const _url = require("url");
|
|
13
|
-
const _util = require("@middy/util");
|
|
14
|
-
const _clients3 = require("@aws-sdk/client-s3");
|
|
15
|
-
function _interop_require_default(obj) {
|
|
16
|
-
return obj && obj.__esModule ? obj : {
|
|
17
|
-
default: obj
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
const defaults = {
|
|
21
|
-
AwsClient: _clients3.S3Client,
|
|
22
|
-
awsClientOptions: {},
|
|
23
|
-
awsClientAssumeRole: undefined,
|
|
24
|
-
awsClientCapture: undefined,
|
|
25
|
-
httpsCapture: undefined,
|
|
26
|
-
disablePrefetch: false,
|
|
27
|
-
bodyType: undefined,
|
|
28
|
-
// For mocking out only, rewire doesn't support ES Modules :(
|
|
29
|
-
__https: _https.default
|
|
30
|
-
};
|
|
31
|
-
let https = _https.default;
|
|
32
|
-
const s3ObjectResponseMiddleware = (opts = {})=>{
|
|
33
|
-
const options = {
|
|
34
|
-
...defaults,
|
|
35
|
-
...opts
|
|
36
|
-
};
|
|
37
|
-
if (![
|
|
38
|
-
'stream',
|
|
39
|
-
'promise'
|
|
40
|
-
].includes(options.bodyType)) {
|
|
41
|
-
throw new Error('[s3-object-response] bodyType is invalid');
|
|
42
|
-
}
|
|
43
|
-
if (options.httpsCapture) {
|
|
44
|
-
https = options.httpsCapture(options.__https);
|
|
45
|
-
}
|
|
46
|
-
let client;
|
|
47
|
-
if ((0, _util.canPrefetch)(options)) {
|
|
48
|
-
client = (0, _util.createPrefetchClient)(options);
|
|
49
|
-
}
|
|
50
|
-
const s3ObjectResponseMiddlewareBefore = async (request)=>{
|
|
51
|
-
const { inputS3Url, outputRoute, outputToken } = request.event.getObjectContext;
|
|
52
|
-
request.internal.s3ObjectResponse = {
|
|
53
|
-
RequestRoute: outputRoute,
|
|
54
|
-
RequestToken: outputToken
|
|
55
|
-
};
|
|
56
|
-
const parsedInputS3Url = new _url.URL(inputS3Url);
|
|
57
|
-
const fetchOptions = {
|
|
58
|
-
method: 'GET',
|
|
59
|
-
host: parsedInputS3Url.hostname,
|
|
60
|
-
path: parsedInputS3Url.pathname
|
|
61
|
-
};
|
|
62
|
-
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
63
|
-
};
|
|
64
|
-
const s3ObjectResponseMiddlewareAfter = async (request)=>{
|
|
65
|
-
if (!client) {
|
|
66
|
-
client = await (0, _util.createClient)(options, request);
|
|
67
|
-
}
|
|
68
|
-
request.response.Body ??= request.response.body;
|
|
69
|
-
delete request.response.body;
|
|
70
|
-
await client.send(new _clients3.WriteGetObjectResponseCommand({
|
|
71
|
-
...request.internal.s3ObjectResponse,
|
|
72
|
-
...request.response
|
|
73
|
-
}));
|
|
74
|
-
return {
|
|
75
|
-
statusCode: 200
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
return {
|
|
79
|
-
before: s3ObjectResponseMiddlewareBefore,
|
|
80
|
-
after: s3ObjectResponseMiddlewareAfter
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
const fetchType = (type, fetchOptions)=>{
|
|
84
|
-
if (type === 'stream') {
|
|
85
|
-
return fetchStream(fetchOptions);
|
|
86
|
-
} else if (type === 'promise') {
|
|
87
|
-
return fetchPromise(fetchOptions);
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
const fetchStream = (fetchOptions)=>{
|
|
91
|
-
return https.request(fetchOptions);
|
|
92
|
-
};
|
|
93
|
-
const fetchPromise = (fetchOptions)=>{
|
|
94
|
-
return new Promise((resolve, reject)=>{
|
|
95
|
-
let data = '';
|
|
96
|
-
const stream = fetchStream(fetchOptions);
|
|
97
|
-
stream.on('data', (chunk)=>{
|
|
98
|
-
data += chunk;
|
|
99
|
-
});
|
|
100
|
-
stream.on('end', ()=>resolve(data));
|
|
101
|
-
stream.on('error', (error)=>reject(error));
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
const _default = s3ObjectResponseMiddleware;
|
|
105
|
-
|