@middy/s3 6.1.6 → 6.2.0
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 +58 -38
- package/index.js +74 -74
- package/package.json +69 -72
package/index.d.ts
CHANGED
|
@@ -1,45 +1,65 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type {
|
|
2
|
+
GetObjectCommandInput,
|
|
3
|
+
S3Client,
|
|
4
|
+
S3ClientConfig,
|
|
5
|
+
} from "@aws-sdk/client-s3";
|
|
6
|
+
import type middy from "@middy/core";
|
|
7
|
+
import type { Options as MiddyOptions } from "@middy/util";
|
|
8
|
+
import type { Context as LambdaContext } from "aws-lambda";
|
|
5
9
|
|
|
6
|
-
export type GetObjectCommandInputNoChecksumMode = Omit<
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
export type GetObjectCommandInputNoChecksumMode = Omit<
|
|
11
|
+
GetObjectCommandInput,
|
|
12
|
+
"ChecksumMode"
|
|
13
|
+
> & {
|
|
14
|
+
ChecksumMode?: never;
|
|
15
|
+
};
|
|
16
|
+
export type ParamType<T> = GetObjectCommandInputNoChecksumMode & {
|
|
17
|
+
__returnType?: T;
|
|
18
|
+
};
|
|
19
|
+
export declare function s3Req<T>(
|
|
20
|
+
req: GetObjectCommandInputNoChecksumMode,
|
|
21
|
+
): ParamType<T>;
|
|
11
22
|
|
|
12
|
-
export type S3Options<AwsS3Client = S3Client> = Omit<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
export type S3Options<AwsS3Client = S3Client> = Omit<
|
|
24
|
+
MiddyOptions<AwsS3Client, S3ClientConfig>,
|
|
25
|
+
"fetchData"
|
|
26
|
+
> & {
|
|
27
|
+
fetchData?: {
|
|
28
|
+
[key: string]: GetObjectCommandInputNoChecksumMode | ParamType<unknown>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
18
31
|
|
|
19
|
-
export type Context<TOptions extends S3Options | undefined> =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
export type Context<TOptions extends S3Options | undefined> = TOptions extends {
|
|
33
|
+
setToContext: true;
|
|
34
|
+
}
|
|
35
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
36
|
+
? LambdaContext & {
|
|
37
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
|
|
38
|
+
? T
|
|
39
|
+
: unknown;
|
|
40
|
+
}
|
|
41
|
+
: never
|
|
42
|
+
: LambdaContext;
|
|
29
43
|
|
|
30
44
|
export type Internal<TOptions extends S3Options | undefined> =
|
|
31
|
-
TOptions extends S3Options
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
TOptions extends S3Options
|
|
46
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
47
|
+
? {
|
|
48
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
|
|
49
|
+
? T
|
|
50
|
+
: unknown;
|
|
51
|
+
}
|
|
52
|
+
: {}
|
|
53
|
+
: {};
|
|
40
54
|
|
|
41
|
-
declare function s3Middleware<TOptions extends S3Options | undefined>
|
|
42
|
-
|
|
43
|
-
): middy.MiddlewareObj<
|
|
55
|
+
declare function s3Middleware<TOptions extends S3Options | undefined>(
|
|
56
|
+
options?: TOptions,
|
|
57
|
+
): middy.MiddlewareObj<
|
|
58
|
+
unknown,
|
|
59
|
+
any,
|
|
60
|
+
Error,
|
|
61
|
+
Context<TOptions>,
|
|
62
|
+
Internal<TOptions>
|
|
63
|
+
>;
|
|
44
64
|
|
|
45
|
-
export default s3Middleware
|
|
65
|
+
export default s3Middleware;
|
package/index.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
+
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
1
2
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from
|
|
12
|
-
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
|
|
3
|
+
canPrefetch,
|
|
4
|
+
catchInvalidSignatureException,
|
|
5
|
+
createClient,
|
|
6
|
+
createPrefetchClient,
|
|
7
|
+
getCache,
|
|
8
|
+
getInternal,
|
|
9
|
+
jsonSafeParse,
|
|
10
|
+
modifyCache,
|
|
11
|
+
processCache,
|
|
12
|
+
} from "@middy/util";
|
|
13
13
|
const defaults = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
const contentTypePattern = /^application\/(.+\+)?json($|;.+)
|
|
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
26
|
const s3Middleware = (opts = {}) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
export default s3Middleware
|
|
27
|
+
const options = {
|
|
28
|
+
...defaults,
|
|
29
|
+
...opts,
|
|
30
|
+
};
|
|
31
|
+
const fetchRequest = (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, fetchRequest);
|
|
59
|
+
}
|
|
60
|
+
const s3MiddlewareBefore = async (request) => {
|
|
61
|
+
if (!client) {
|
|
62
|
+
client = await createClient(options, request);
|
|
63
|
+
}
|
|
64
|
+
const { value } = processCache(options, fetchRequest, 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
|
+
};
|
|
74
|
+
};
|
|
75
|
+
export default s3Middleware;
|
|
76
76
|
|
|
77
77
|
// used for TS type inference (see index.d.ts)
|
|
78
|
-
export function s3Req
|
|
79
|
-
|
|
78
|
+
export function s3Req(req) {
|
|
79
|
+
return req;
|
|
80
80
|
}
|
package/package.json
CHANGED
|
@@ -1,74 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"aws-xray-sdk": "^3.3.3"
|
|
72
|
-
},
|
|
73
|
-
"gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
|
|
2
|
+
"name": "@middy/s3",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "S3 middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"S3"
|
|
43
|
+
],
|
|
44
|
+
"author": {
|
|
45
|
+
"name": "Middy contributors",
|
|
46
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
51
|
+
"directory": "packages/s3"
|
|
52
|
+
},
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
57
|
+
"funding": {
|
|
58
|
+
"type": "github",
|
|
59
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@middy/util": "6.2.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@aws-sdk/client-s3": "^3.0.0",
|
|
66
|
+
"@middy/core": "6.2.0",
|
|
67
|
+
"@types/aws-lambda": "^8.10.101",
|
|
68
|
+
"aws-xray-sdk": "^3.3.3"
|
|
69
|
+
},
|
|
70
|
+
"gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
|
|
74
71
|
}
|